mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qing Zhao <qing.zhao@oracle.com>
To: Kees Cook <kees@kernel.org>
Cc: Thorsten Blum <thorsten.blum@linux.dev>,
	Bill Wendling <morbo@google.com>, Peter Rosin <peda@axentia.se>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-hardening@vger.kernel.org"
	<linux-hardening@vger.kernel.org>
Subject: Re: [RESEND PATCH] mux: Convert mux_control_ops to a flex array member in mux_chip
Date: Wed, 5 Mar 2025 17:31:57 +0000	[thread overview]
Message-ID: <20C5E493-4FB3-4347-844B-1AFE6B9FCDA4@oracle.com> (raw)
In-Reply-To: <202503041935.AE2093CFFA@keescook>



> On Mar 4, 2025, at 23:57, Kees Cook <kees@kernel.org> wrote:
> 
> On Tue, Mar 04, 2025 at 09:58:21AM +0100, Thorsten Blum wrote:
>> On 3. Mar 2025, at 19:44, Kees Cook wrote:
>>> On Mon, Mar 03, 2025 at 12:02:22AM +0100, Thorsten Blum wrote:
>>>> Convert mux_control_ops to a flexible array member at the end of the
>>>> mux_chip struct and add the __counted_by() compiler attribute to
>>>> improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
>>>> CONFIG_FORTIFY_SOURCE.
>>>> 
>>>> Use struct_size() to calculate the number of bytes to allocate for a new
>>>> mux chip and to remove the following Coccinelle/coccicheck warning:
>>>> 
>>>> WARNING: Use struct_size
>>>> 
>>>> Use size_add() to safely add any extra bytes.
>>>> 
>>>> Compile-tested only.
>>> 
>>> I believe this will fail at runtime. Note that sizeof_priv follows the
>>> allocation, so at the very least, you'd need to update:
>>> 
>>> static inline void *mux_chip_priv(struct mux_chip *mux_chip)
>>> {
>>>      return &mux_chip->mux[mux_chip->controllers];
>>> }
>>> 
>>> to not use the mux array itself as a location reference because it will
>>> be seen as out of bounds.
>> 
>> Getting the address doesn't fail at runtime, does it? For this example
>> it works, but maybe I'm missing some compiler flag?
>> 
>> https://godbolt.org/z/qTEdqn9WW
> 
> Uhn. I can't explain that. :( We've seen this calculation get tripped
> in the real world, though:
> 
> https://git.kernel.org/linus/a26a5107bc52
> 
> But yeah, when I build local test cases, grabbing an integral trips it,
> but taking an address does not, as your godbolt shows. This makes no
> sense to me at all.
> 
> Here's my version, doing a direct comparison of int to *(int *) ...
> https://godbolt.org/z/e1bKGz739
> 
> #include <stdlib.h>
> #include <stdio.h>
> 
> struct foo {
>    int count;
>    int array[] __attribute__((__counted_by__(count)));
> };
> 
> int main(int argc, char *argv[]) {
>    int num_elems = 2 + argc;
> 
>    struct foo *p = malloc(sizeof(*p) + num_elems * sizeof(*p->array) + sizeof(int));
>    p->count = num_elems;
> 
>    // this correctly trips sanitizer:
>    int val = p->array[num_elems];
>    printf("%d\n", val);
> 
>    // this does not?!
>    int *valp = &p->array[num_elems];
>    printf("%p %d\n", valp, *valp);
> 
>    return 0;
> }
> 
> Qing and Bill, are you able to explain this? If I set p->count = 0, 1, or
> 2, this trips. Is this somehow an off-by-one error in both GCC and Clang?

This does look like a bug in the compiler, could you please file a bug against GCC on this?

thanks.

Qing
> 
> -- 
> Kees Cook


  parent reply	other threads:[~2025-03-05 17:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-02 23:02 Thorsten Blum
2025-03-03 18:44 ` Kees Cook
2025-03-04  8:58   ` Thorsten Blum
2025-03-05  4:57     ` Kees Cook
2025-03-05 17:31       ` Qing Zhao
2025-03-05 17:31       ` Qing Zhao [this message]
2025-03-05 22:42         ` Kees Cook
2025-03-07 11:32   ` Thorsten Blum
2025-04-07 18:20     ` Kees Cook
2025-04-13 12:42       ` Thorsten Blum
2025-04-29 11:55         ` Thorsten Blum
2025-03-18 16:27 Thorsten Blum

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=20C5E493-4FB3-4347-844B-1AFE6B9FCDA4@oracle.com \
    --to=qing.zhao@oracle.com \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=morbo@google.com \
    --cc=peda@axentia.se \
    --cc=thorsten.blum@linux.dev \
    /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

all inboxes | Powered by JetHome®