Skip to content

Commit 1acd63a

Browse files
committed
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 Signed-off-by: Riadh Ghaddab <[email protected]>
1 parent 934e9d4 commit 1acd63a

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

subsys/settings/include/settings/settings_zms.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ extern "C" {
5454
* if a settings element is deleted it won't be found.
5555
*/
5656

57+
#define SETTINGS_FULL_NAME_LEN SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1
58+
5759
#define ZMS_LL_HEAD_HASH_ID 0x80000000
5860
#define ZMS_DATA_ID_OFFSET 0x40000000
5961
#define ZMS_HASH_MASK GENMASK(29, CONFIG_SETTINGS_ZMS_MAX_COLLISIONS_BITS + 1)

subsys/settings/src/settings_zms.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static int settings_zms_load_one(struct settings_store *cs, const char *name, ch
206206
size_t buf_len)
207207
{
208208
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
209-
char r_name[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
209+
char r_name[SETTINGS_FULL_NAME_LEN];
210210
ssize_t rc = 0;
211211
uint32_t name_hash;
212212

@@ -215,7 +215,7 @@ static int settings_zms_load_one(struct settings_store *cs, const char *name, ch
215215
return -EINVAL;
216216
}
217217

218-
name_hash = sys_hash32(name, strlen(name)) & ZMS_HASH_MASK;
218+
name_hash = sys_hash32(name, strnlen(name, SETTINGS_FULL_NAME_LEN)) & ZMS_HASH_MASK;
219219
for (int i = 0; i <= cf->hash_collision_num; i++) {
220220
name_hash = ZMS_UPDATE_COLLISION_NUM(name_hash, i);
221221
/* Get the name entry from ZMS */
@@ -247,15 +247,17 @@ static int settings_zms_load(struct settings_store *cs, const struct settings_lo
247247
struct settings_zms *cf = CONTAINER_OF(cs, struct settings_zms, cf_store);
248248
struct settings_zms_read_fn_arg read_fn_arg;
249249
struct settings_hash_linked_list settings_element;
250-
char name[SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1];
250+
char name[SETTINGS_FULL_NAME_LEN];
251251
ssize_t rc1;
252252
ssize_t rc2;
253253
uint32_t ll_hash_id;
254254
uint32_t name_hash;
255255

256256
/* If arg->subtree is not null we must load settings in that subtree */
257257
if (arg->subtree != NULL) {
258-
name_hash = sys_hash32(arg->subtree, strlen(arg->subtree)) & ZMS_HASH_MASK;
258+
name_hash =
259+
sys_hash32(arg->subtree, strnlen(arg->subtree, SETTINGS_FULL_NAME_LEN)) &
260+
ZMS_HASH_MASK;
259261
for (int i = 0; i <= cf->hash_collision_num; i++) {
260262
name_hash = ZMS_UPDATE_COLLISION_NUM(name_hash, i);
261263
/* Get the name entry from ZMS */
@@ -418,7 +420,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
418420
/* Find out if we are doing a delete */
419421
delete = ((value == NULL) || (val_len == 0));
420422

421-
name_hash = sys_hash32(name, strlen(name)) & ZMS_HASH_MASK;
423+
name_hash = sys_hash32(name, strnlen(name, SETTINGS_FULL_NAME_LEN)) & ZMS_HASH_MASK;
422424
/* MSB is always 1 */
423425
name_hash |= BIT(31);
424426

@@ -571,7 +573,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
571573
no_ll_update:
572574
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
573575
/* Now let's write the name */
574-
rc = zms_write(&cf->cf_zms, name_hash, name, strlen(name));
576+
rc = zms_write(&cf->cf_zms, name_hash, name, strnlen(name, SETTINGS_FULL_NAME_LEN));
575577
if (rc < 0) {
576578
return rc;
577579
}

0 commit comments

Comments
 (0)