[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <202206211327.F8936F0783@keescook>
Date: Tue, 21 Jun 2022 13:31:13 -0700
From: Kees Cook <keescook@...omium.org>
To: Greg KH <gregkh@...uxfoundation.org>
Cc: Coccinelle <cocci@...teme.lip6.fr>,
linux-hardening@...r.kernel.org,
Julia Lawall <Julia.Lawall@...ia.fr>
Subject: Re: replacing memcpy() calls with direct assignment
On Tue, Jun 21, 2022 at 09:05:36PM +0200, Greg KH wrote:
> On Tue, Jun 21, 2022 at 11:37:10AM -0700, Kees Cook wrote:
> > Hello Coccinelle gurus! :)
> >
> > I recently spent way too long looking at a weird bug in Clang that I
> > eventually worked around by just replacing a memcpy() with a direct
> > assignment. It really was very mechanical, and seems like it might be a
> > common code pattern in the kernel. Swapping these would make the code
> > much more readable, I think. Here's the example:
> >
> >
> > https://lore.kernel.org/linux-hardening/20220616052312.292861-1-keescook@chromium.org/
> >
> > - memcpy(&host_image->image_section_info[i],
> > - &fw_image->fw_section_info[i],
> > - sizeof(struct fw_section_info_st));
> > + host_image->image_section_info[i] = fw_image->fw_section_info[i];
>
> Ick, that hids the fact that you are doing a potentially huge memory
> copy here.
>
> And would it also prevent the compiler from using our optimized memcpy()
> function and replacing it with whatever it wanted to use instead?
What? Uh, quite the reverse, in fact. The compiler is MUCH better about
doing those kinds of optimizations. The commit log details that there's
no binary difference, in fact, with this change.
> What clang bug does this fix such that it warrants us hiding this
> information away from the developers?
Hiding? This makes the code significantly more clear. Doing an assignment
makes it clear they're the same type, etc, etc. Obscuring all that with
a memcpy() makes no sense.
As for the bug in Clang, it's triggered by a UBSAN_BOUNDS bug that is
still being investigated.
--
Kees Cook
Powered by blists - more mailing lists