diff options
| author | Kees Cook <[email protected]> | 2021-05-17 20:16:57 -0700 |
|---|---|---|
| committer | Kees Cook <[email protected]> | 2021-10-18 12:28:52 -0700 |
| commit | 4797632f4f1d8af4e0670adcb97bf9800dc3beca (patch) | |
| tree | d0c145b4b5ac8461f45a2dcf346945faaec8d080 /include/linux/string.h | |
| parent | bb95ebbe89a7854368be061acefb22040fbcc486 (diff) | |
string.h: Introduce memset_after() for wiping trailing members/padding
A common idiom in kernel code is to wipe the contents of a structure
after a given member. This is especially useful in places where there is
trailing padding. These open-coded cases are usually difficult to read
and very sensitive to struct layout changes. Introduce a new helper,
memset_after() that takes the target struct instance, the byte to write,
and the member name after which the zeroing should start.
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]>
Diffstat (limited to 'include/linux/string.h')
| -rw-r--r-- | include/linux/string.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h index ac1c769a5a80..da490c2154a9 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -272,6 +272,23 @@ static inline void memcpy_and_pad(void *dest, size_t dest_len, } /** + * memset_after - Set a value after a struct member to the end of a struct + * + * @obj: Address of target struct instance + * @v: Byte value to repeatedly write + * @member: after which struct member to start writing bytes + * + * This is good for clearing padding following the given member. + */ +#define memset_after(obj, v, member) \ +({ \ + u8 *__ptr = (u8 *)(obj); \ + typeof(v) __val = (v); \ + memset(__ptr + offsetofend(typeof(*(obj)), member), __val, \ + sizeof(*(obj)) - offsetofend(typeof(*(obj)), member)); \ +}) + +/** * str_has_prefix - Test if a string has a given prefix * @str: The string to test * @prefix: The string to see if @str starts with |