Skip to content

Commit 82647fe

Browse files
committed
[nrf fromlist] settings: zms: use the safe function strnlen instead of strlen
if the provided name in argument is not null this could lead to un undefined behavior. Use strnlen to make this safe Upstream PR #: 87792 Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit 415a82e63b87f51fd16876ba6eb4ece1eb1dd9a5)
1 parent 943d900 commit 82647fe

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

include/zephyr/settings/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ extern "C" {
4545
*/
4646
#define SETTINGS_EXTRA_LEN ((SETTINGS_MAX_DIR_DEPTH - 1) + 2)
4747

48+
/* Maximum Settings name length including separators */
49+
#define SETTINGS_FULL_NAME_LEN SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1
50+
4851
/**
4952
* Function used to read the data from the settings storage in
5053
* h_set handler implementations.

subsys/settings/src/settings_zms.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
#define _POSIX_C_SOURCE 200809L /* for strnlen() */
7+
68
#include <errno.h>
79
#include <string.h>
810

@@ -218,13 +220,14 @@ static int settings_zms_load_subtree(struct settings_store *cs, const struct set
218220
{
219221
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
220222
struct settings_zms_read_fn_arg read_fn_arg;
221-
char name[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
223+
char name[SETTINGS_FULL_NAME_LEN];
222224
ssize_t rc1;
223225
ssize_t rc2;
224226
uint32_t name_hash;
225227
int ret = 0;
226228

227-
name_hash = sys_hash32(arg->subtree, strlen(arg->subtree)) & ZMS_HASH_MASK;
229+
name_hash = sys_hash32(arg->subtree, strnlen(arg->subtree, SETTINGS_FULL_NAME_LEN)) &
230+
ZMS_HASH_MASK;
228231
for (int i = 0; i <= cf->hash_collision_num; i++) {
229232
name_hash = ZMS_UPDATE_COLLISION_NUM(name_hash, i);
230233
/* Get the name entry from ZMS */
@@ -249,8 +252,8 @@ static int settings_zms_load_subtree(struct settings_store *cs, const struct set
249252
read_fn_arg.fs = &cf->cf_zms;
250253
read_fn_arg.id = ZMS_NAME_ID_FROM_HASH(name_hash) + ZMS_DATA_ID_OFFSET;
251254

252-
ret = settings_call_set_handler(arg->subtree, rc2, settings_zms_read_fn, &read_fn_arg,
253-
(void *)arg);
255+
ret = settings_call_set_handler(arg->subtree, rc2, settings_zms_read_fn,
256+
&read_fn_arg, (void *)arg);
254257
/* We should return here as there are no need to look for the next
255258
* hash collision */
256259
return ret;
@@ -264,7 +267,7 @@ static ssize_t settings_zms_load_one(struct settings_store *cs, const char *name
264267
size_t buf_len)
265268
{
266269
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
267-
char r_name[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
270+
char r_name[SETTINGS_FULL_NAME_LEN];
268271
ssize_t rc = 0;
269272
uint32_t name_hash;
270273

@@ -273,7 +276,7 @@ static ssize_t settings_zms_load_one(struct settings_store *cs, const char *name
273276
return -EINVAL;
274277
}
275278

276-
name_hash = sys_hash32(name, strlen(name)) & ZMS_HASH_MASK;
279+
name_hash = sys_hash32(name, strnlen(name, SETTINGS_FULL_NAME_LEN)) & ZMS_HASH_MASK;
277280
for (int i = 0; i <= cf->hash_collision_num; i++) {
278281
name_hash = ZMS_UPDATE_COLLISION_NUM(name_hash, i);
279282
/* Get the name entry from ZMS */
@@ -306,7 +309,7 @@ static int settings_zms_load(struct settings_store *cs, const struct settings_lo
306309
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
307310
struct settings_zms_read_fn_arg read_fn_arg;
308311
struct settings_hash_linked_list settings_element;
309-
char name[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
312+
char name[SETTINGS_FULL_NAME_LEN];
310313
ssize_t rc1;
311314
ssize_t rc2;
312315
uint32_t ll_hash_id;
@@ -431,7 +434,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
431434
{
432435
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
433436
struct settings_hash_linked_list settings_element;
434-
char rdname[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
437+
char rdname[SETTINGS_FULL_NAME_LEN];
435438
uint32_t name_hash;
436439
uint32_t collision_num = 0;
437440
bool delete;
@@ -449,7 +452,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
449452
/* Find out if we are doing a delete */
450453
delete = ((value == NULL) || (val_len == 0));
451454

452-
name_hash = sys_hash32(name, strlen(name)) & ZMS_HASH_MASK;
455+
name_hash = sys_hash32(name, strnlen(name, SETTINGS_FULL_NAME_LEN)) & ZMS_HASH_MASK;
453456
/* MSB is always 1 */
454457
name_hash |= BIT(31);
455458

@@ -603,7 +606,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
603606
no_ll_update:
604607
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
605608
/* Now let's write the name */
606-
rc = zms_write(&cf->cf_zms, name_hash, name, strlen(name));
609+
rc = zms_write(&cf->cf_zms, name_hash, name, strnlen(name, SETTINGS_FULL_NAME_LEN));
607610
if (rc < 0) {
608611
return rc;
609612
}
@@ -624,7 +627,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
624627
static ssize_t settings_zms_get_val_len(struct settings_store *cs, const char *name)
625628
{
626629
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
627-
char r_name[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
630+
char r_name[SETTINGS_FULL_NAME_LEN];
628631
ssize_t rc = 0;
629632
uint32_t name_hash;
630633

@@ -633,7 +636,7 @@ static ssize_t settings_zms_get_val_len(struct settings_store *cs, const char *n
633636
return -EINVAL;
634637
}
635638

636-
name_hash = sys_hash32(name, strlen(name)) & ZMS_HASH_MASK;
639+
name_hash = sys_hash32(name, strnlen(name, SETTINGS_FULL_NAME_LEN)) & ZMS_HASH_MASK;
637640
for (int i = 0; i <= cf->hash_collision_num; i++) {
638641
name_hash = ZMS_UPDATE_COLLISION_NUM(name_hash, i);
639642
/* Get the name entry from ZMS */

0 commit comments

Comments
 (0)