linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Julia Lawall <julia.lawall@inria.fr>
To: Kees Cook <keescook@chromium.org>
Cc: Coccinelle <cocci@systeme.lip6.fr>, linux-hardening@vger.kernel.org
Subject: Re: replacing memcpy() calls with direct assignment
Date: Tue, 21 Jun 2022 15:50:39 -0400 (EDT)	[thread overview]
Message-ID: <alpine.DEB.2.22.394.2206211544160.2194@hadrien> (raw)
In-Reply-To: <202206211109.A819E8118@keescook>



On Tue, 21 Jun 2022, 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];
>
> Is there a way to reduce the size of this cocci rule? I had to
> explicitly spell out each "address of" condition separately, though I'd
> expect them to be internal aliases, but I'd get output like:
>
>  *&dst = src;
>
> etc

I don't disagree with Greg, but I will still answer the question :)

>
> @direct_assignment@
> type TYPE;
> TYPE DST, SRC;
> TYPE *DPTR;
> TYPE *SPTR;
> @@
>
> (
> - memcpy(&DST, &SRC, sizeof(TYPE))
> + DST = SRC
> |
> - memcpy(&DST, &SRC, sizeof(DST))
> + DST = SRC
> |
> - memcpy(&DST, &SRC, sizeof(SRC))
> + DST = SRC
> |
>
> - memcpy(&DST, SPTR, sizeof(TYPE))
> + DST = *SPTR
> |
> - memcpy(&DST, SPTR, sizeof(DST))
> + DST = *SPTR
> |
> - memcpy(&DST, SPTR, sizeof(*SPTR))
> + DST = *SPTR
> |
>
> - memcpy(DPTR, &SRC, sizeof(TYPE))
> + *DPTR = SRC
> |
> - memcpy(DPTR, &SRC, sizeof(DST))
> + *DPTR = SRC
> |
> - memcpy(DPTR, &SRC, sizeof(SRC))
> + *DPTR = SRC
> |
>
> - memcpy(DPTR, SPTR, sizeof(TYPE))
> + *DPTR = *SPTR
> |
> - memcpy(DPTR, SPTR, sizeof(*DST))
> + *DPTR = *SPTR
> |
> - memcpy(DPTR, SPTR, sizeof(*SRC))
> + *DPTR = *SPTR
> )

You can make a disjunction for the sizeof, eg in the last case:

\(sizeof(TYPE)\|sizeof(*DST)\|sizeof(*SRC)\)

That would reduce the number of lines by 2/3.

Note that it would not be good to put

sizeof( \(TYPE\|*DST\|*SRC\) )

because the C rules for parentheses with sizeof in the type case are
different than the rules in the expression case.

On the other hand, I believe that the above rule will require SRC and DST
to have known types, while such a type is only necessary for the
sizeof(TYPE) case.  So it would be better to have one rule for the
sizeof(TYPE) case, and another rule for the other sizeof cases.
In the second rule, SRC and DST can just be expressions.

julia

      parent reply	other threads:[~2022-06-21 19:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-21 18:37 replacing memcpy() calls with direct assignment Kees Cook
2022-06-21 19:05 ` Greg KH
2022-06-21 20:31   ` Kees Cook
2022-06-21 20:43     ` Greg KH
2022-06-21 19:50 ` Julia Lawall [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.22.394.2206211544160.2194@hadrien \
    --to=julia.lawall@inria.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).