string.h: Introduce memset_startat() for wiping trailing members and padding
authorKees Cook <keescook@chromium.org>
Tue, 18 May 2021 03:16:57 +0000 (20:16 -0700)
committerKees Cook <keescook@chromium.org>
Mon, 18 Oct 2021 19:28:52 +0000 (12:28 -0700)
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 <steffen.klassert@secunet.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Francis Laniel <laniel_francis@privacyrequired.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Daniel Axtens <dja@axtens.net>
Cc: netdev@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
include/linux/string.h
lib/memcpy_kunit.c

index da490c2154a95c9d28ec1e27c288c35ef49c1aef..5a36608144a9fd46fe7eb847456a4eac0a5a0b52 100644 (file)
@@ -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
index 5c5b4f3221d947cd0bebe6e0e27648c99903e4f9..62f8ffcbbaa352cd99812f2e38f27b56e6a542f9 100644 (file)
@@ -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;
@@ -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
 }