Skip to content

Commit c106296

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 7555e3391a329b11aca6de082d3a789a5223fc6c)
1 parent b4eaeba commit c106296

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

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

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

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

@@ -272,7 +275,7 @@ static ssize_t settings_zms_load_one(struct settings_store *cs, const char *name
272275
return -EINVAL;
273276
}
274277

275-
name_hash = sys_hash32(name, strlen(name)) & ZMS_HASH_MASK;
278+
name_hash = sys_hash32(name, strnlen(name, SETTINGS_FULL_NAME_LEN)) & ZMS_HASH_MASK;
276279
for (int i = 0; i <= cf->hash_collision_num; i++) {
277280
name_hash = ZMS_UPDATE_COLLISION_NUM(name_hash, i);
278281
/* Get the name entry from ZMS */
@@ -305,7 +308,7 @@ static int settings_zms_load(struct settings_store *cs, const struct settings_lo
305308
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
306309
struct settings_zms_read_fn_arg read_fn_arg;
307310
struct settings_hash_linked_list settings_element;
308-
char name[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
311+
char name[SETTINGS_FULL_NAME_LEN];
309312
ssize_t rc1;
310313
ssize_t rc2;
311314
uint32_t ll_hash_id;
@@ -430,7 +433,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
430433
{
431434
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
432435
struct settings_hash_linked_list settings_element;
433-
char rdname[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
436+
char rdname[SETTINGS_FULL_NAME_LEN];
434437
uint32_t name_hash;
435438
uint32_t collision_num = 0;
436439
bool delete;
@@ -448,7 +451,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
448451
/* Find out if we are doing a delete */
449452
delete = ((value == NULL) || (val_len == 0));
450453

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

@@ -601,7 +604,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
601604
no_ll_update:
602605
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
603606
/* Now let's write the name */
604-
rc = zms_write(&cf->cf_zms, name_hash, name, strlen(name));
607+
rc = zms_write(&cf->cf_zms, name_hash, name, strnlen(name, SETTINGS_FULL_NAME_LEN));
605608
if (rc < 0) {
606609
return rc;
607610
}
@@ -622,7 +625,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
622625
static ssize_t settings_zms_get_val_len(struct settings_store *cs, const char *name)
623626
{
624627
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
625-
char r_name[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
628+
char r_name[SETTINGS_FULL_NAME_LEN];
626629
ssize_t rc = 0;
627630
uint32_t name_hash;
628631

@@ -631,7 +634,7 @@ static ssize_t settings_zms_get_val_len(struct settings_store *cs, const char *n
631634
return -EINVAL;
632635
}
633636

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

0 commit comments

Comments
 (0)