Skip to content

Commit

Permalink
string.h: Introduce memset_startat() for wiping trailing members and …
Browse files Browse the repository at this point in the history
…padding

A common idiom in kernel code is to wipe the contents of a structure
starting from a given member. These open-coded cases are usually difficult
to read and very sensitive to struct layout changes. Like memset_after(),
introduce a new helper, memset_startat() that takes the target struct
instance, the byte to write, and the member name where zeroing should
start.

Note that this doesn't zero padding preceding the target member. For
those cases, memset_after() should be used on the preceding member.

Cc: Steffen Klassert <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Francis Laniel <[email protected]>
Cc: Vincenzo Frascino <[email protected]>
Cc: Daniel Axtens <[email protected]>
Cc: [email protected]
Signed-off-by: Kees Cook <[email protected]>
  • Loading branch information
kees committed Oct 18, 2021
1 parent caf283d commit 6dbefad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/linux/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,24 @@ static inline void memcpy_and_pad(void *dest, size_t dest_len,
sizeof(*(obj)) - offsetofend(typeof(*(obj)), member)); \
})

/**
* memset_startat - Set a value starting at a member to the end of a struct
*
* @obj: Address of target struct instance
* @v: Byte value to repeatedly write
* @member: struct member to start writing at
*
* Note that if there is padding between the prior member and the target
* member, memset_after() should be used to clear the prior padding.
*/
#define memset_startat(obj, v, member) \
({ \
u8 *__ptr = (u8 *)(obj); \
typeof(v) __val = (v); \
memset(__ptr + offsetof(typeof(*(obj)), member), __val, \
sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \
})

/**
* str_has_prefix - Test if a string has a given prefix
* @str: The string to test
Expand Down
11 changes: 11 additions & 0 deletions lib/memcpy_kunit.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ static void memset_test(struct kunit *test)
0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
},
};
struct some_bytes startat = {
.data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
},
};
struct some_bytes dest = { };
int count, value;
u8 *ptr;
Expand Down Expand Up @@ -258,6 +265,10 @@ static void memset_test(struct kunit *test)
memset_after(&dest, 0x72, three);
compare("memset_after()", dest, after);

/* Verify memset_startat() */
dest = control;
memset_startat(&dest, 0x79, four);
compare("memset_startat()", dest, startat);
#undef TEST_OP
}

Expand Down

0 comments on commit 6dbefad

Please sign in to comment.