LWN: Comments on "Strict memcpy() bounds checking for the kernel" https://lwn.net/Articles/864521/ This is a special feed containing comments posted to the individual LWN article titled "Strict memcpy() bounds checking for the kernel". en-us Sat, 11 Oct 2025 00:39:49 +0000 Sat, 11 Oct 2025 00:39:49 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/972138/ https://lwn.net/Articles/972138/ ukleinek <div class="FormattedComment"> The syntax in the example is wrong.<br> <p> struct foo {<br> int one;<br> struct_group(thing,<br> int two; // &lt;-- ; here<br> int three; // &lt;-- and here<br> );<br> int four;<br> };<br> </div> Thu, 02 May 2024 09:59:12 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864955/ https://lwn.net/Articles/864955/ willy <div class="FormattedComment"> Of course, the kernel disables SIMD so that it doesn&#x27;t use the XMM/YMM registers, and thus they don&#x27;t have to be saved/restored at system call time.<br> </div> Tue, 03 Aug 2021 02:22:29 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864906/ https://lwn.net/Articles/864906/ kees <div class="FormattedComment"> Right, and if any __randomize_layout struct wanted to use struct_group() it could as those would be kept together. It actually makes things easier.<br> </div> Mon, 02 Aug 2021 15:07:11 +0000 drastic performance gains https://lwn.net/Articles/864819/ https://lwn.net/Articles/864819/ jreiser In the linked file perf_memcpy_s.c, I saw uniform weighting of chunks that are all multiples of 1 KiB. What happens for a model that is closer to actual usage in the Linux kernel? The kernel has obvious special cases for filesystems, network stack, and general management of pages; but what about the rest? Measuring the task of re-compiling and re-building the kernel under the configurations "all module" and "all yes" are two interesting cases. <p> In user space, a very large portion of allocations by malloc() are for 100 bytes or fewer, which constrains memcpy to "small" lengths on average. See <a href="http://www.linuxplumbersconf.net/2016/ocw//system/presentations/3921/original/LPC%202016%20-%20linux%20and%20glibc_%20The%204.5TiB%20malloc%20API%20trace.pdf">http://www.linuxplumbersconf.net/2016/ocw//system/presentations/3921/original/LPC%202016%20-%20linux%20and%20glibc_%20The%204.5TiB%20malloc%20API%20trace.pdf</a>. Mon, 02 Aug 2021 00:25:19 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864806/ https://lwn.net/Articles/864806/ HenrikH <div class="FormattedComment"> replacing that with a pure memset (yes I know that your example didn&#x27;t clear all the members, but still) resulted in even less code.<br> </div> Sun, 01 Aug 2021 13:23:29 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864799/ https://lwn.net/Articles/864799/ epa <div class="FormattedComment"> It seems like ‘copy the following members of a struct’ or ‘copy all except these’ or ‘copy from this point to this point’ would be useful extensions to the C language.<br> </div> Sun, 01 Aug 2021 08:41:42 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864759/ https://lwn.net/Articles/864759/ jengelh <div class="FormattedComment"> Yes, it will also optimize for uint8_t. You just need enough bytes overall (looks like &gt;= 8) to make it worthwhile for gcc to switch to SIMD-based copying.<br> For bitfields there is a bit of a problem, for which I filed <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101705">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101705</a> .<br> </div> Sat, 31 Jul 2021 13:27:30 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864746/ https://lwn.net/Articles/864746/ CAFxX <div class="FormattedComment"> Seems like it can do even better than memcpy, in that it can handle cases where memcpy would waste work: <a rel="nofollow" href="https://godbolt.org/z/aa31McEjx">https://godbolt.org/z/aa31McEjx</a><br> </div> Sat, 31 Jul 2021 13:22:36 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864756/ https://lwn.net/Articles/864756/ Hello71 <div class="FormattedComment"> no: <a rel="nofollow" href="https://gcc.godbolt.org/z/aP8GWb487">https://gcc.godbolt.org/z/aP8GWb487</a><br> <p> i don&#x27;t understand what &quot;running afoul of new members added to S&quot; means anyways. it seems to me that in the vast majority of cases, when new members are added, one would want them to be copied too. otherwise, write memcpy(dest, src, sizeof(*dest) - offsetof(struct S, last_member) + sizeof(dest-&gt;last_member))?<br> </div> Sat, 31 Jul 2021 12:38:56 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864751/ https://lwn.net/Articles/864751/ rurban <div class="FormattedComment"> BOS works much better in clang than gcc. I implemented those tricks in my str*/mem* variants, and clang could detect much more compile-time sizes, and it has diagnose_if to throw the same compile-time warnings as the runtime libc.<br> <a rel="nofollow" href="https://github.com/rurban/safeclib/blob/master/include/safe_compile.h">https://github.com/rurban/safeclib/blob/master/include/sa...</a><br> <p> With drastic performance gains. <a rel="nofollow" href="https://github.com/rurban/safeclib/blob/master/tests/perf_memcpy_s.c">https://github.com/rurban/safeclib/blob/master/tests/perf...</a><br> </div> Sat, 31 Jul 2021 11:20:56 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864738/ https://lwn.net/Articles/864738/ bartoc <div class="FormattedComment"> Interestingly that struct group macro is essentially recreating part of gcc&#x27;s plan9-extensions (presumably they can from plan9). I will admit that using a typedef as the indication of &quot;I want the member name and the anon-struct behavior&quot; is pretty odd. Also it turns on other stuff (like the object oriented implicit pointer casts).<br> </div> Sat, 31 Jul 2021 07:05:59 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864734/ https://lwn.net/Articles/864734/ alison <div class="FormattedComment"> <font class="QuotedText">&gt;See the GCC documentation for more information on __builtin_object_size(). </font><br> <p> I wondered if __builtin_object_size() would work with Clang. Here is another few-day-old Cook patch in that regard: <a href="https://lkml.org/lkml/2021/7/27/1161">https://lkml.org/lkml/2021/7/27/1161</a><br> <p> Which I take to mean &quot;not in current release.&quot;<br> </div> Sat, 31 Jul 2021 04:31:20 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864718/ https://lwn.net/Articles/864718/ pbonzini <div class="FormattedComment"> Does that still work when some fields are u8 or bool and some are even bitfields? Is the compiler able to produce the same code as with memcpy?<br> </div> Fri, 30 Jul 2021 21:54:18 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864717/ https://lwn.net/Articles/864717/ mathstuf <div class="FormattedComment"> Isn&#x27;t that just for static structures full of function pointers (basically vtables)? Presumably `sk_buff` structures and other such things being `memcpy`&#x27;d to are not constant once the kernel is booted.<br> </div> Fri, 30 Jul 2021 21:33:02 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864715/ https://lwn.net/Articles/864715/ post-factum <div class="FormattedComment"> How does it play with the struct layout randomisation?<br> </div> Fri, 30 Jul 2021 21:07:37 +0000 Strict memcpy() bounds checking for the kernel https://lwn.net/Articles/864714/ https://lwn.net/Articles/864714/ jengelh <div class="FormattedComment"> #include &lt;string.h&gt;<br> struct S { int one, two, three, four; };<br> void lefttoright(struct S *d, struct S *s) {<br> d-&gt;one = s-&gt;one; d-&gt;two = s-&gt;two; d-&gt;three = s-&gt;three; d-&gt;four = s-&gt;four; }<br> <p> Explicit field copies. More to type, but no running afoul of new members added to S. gcc-11.1.1 -O3 is capable to produce the same x86_64 asm as a field-insensitive memcpy(d,s,sizeof(*s)) would on -O2.<br> </div> Fri, 30 Jul 2021 20:28:18 +0000