From: Peter Zijlstra <peterz@infradead.org>
To: Kees Cook <kees@kernel.org>
Cc: Erick Archer <erick.archer@outlook.com>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
"Liang, Kan" <kan.liang@linux.intel.com>,
Thomas Gleixner <tglx@linutronix.de>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>,
Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
Matthew Wilcox <mawilcox@microsoft.com>,
x86@kernel.org, linux-perf-users@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org,
llvm@lists.linux.dev
Subject: Re: [PATCH v4 0/3] Hardening perf subsystem
Date: Tue, 11 Jun 2024 09:55:42 +0200 [thread overview]
Message-ID: <20240611075542.GD8774@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <202406101438.BC43514F@keescook>
On Mon, Jun 10, 2024 at 02:46:09PM -0700, Kees Cook wrote:
> > I really detest this thing because it makes what was trivially readable
> > into something opaque. Get me that type qualifier that traps on overflow
> > and write plain C. All this __builtin_overflow garbage is just that,
> > unreadable nonsense.
>
> It's more readable than container_of(),
Yeah, no. container_of() is absolutely trivial and very readable.
container_of_const() a lot less so.
(one static_assert() removed)
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
((type *)(__mptr - offsetof(type, member))); })
Which is very clear indeed in what it does. Compare with:
#define struct_size(p, member, count) \
__builtin_choose_expr(__is_constexpr(count), \
sizeof(*(p)) + flex_array_size(p, member, count), \
size_add(sizeof(*(p)), flex_array_size(p, member, count)))
And I still have no idea :-(
> IMO. "give me the struct size
> for variable VAR, which has a flexible array MEMBER, when we have COUNT
> many of them": struct_size(VAR, MEMBER, COUNT). It's more readable, more
> robust, and provides saturation in the face of potential wrap-around.
I'm sure you know what it does. Thing is, I don't care because I can
trivially write it myself and not have to care and I'll have forgotten
all about it the moment I sent this email.
It just doesn't make sense to wrap something as utterly trivial as:
size = sizeof(*p) + num*sizeof(p->foo);
We're going to have to agree to disagree on this.
Note how I naturally get the order wrong?
[[ There is the whole FMA angle to this, that is, fundamentally this is a
multiply-accumulate, but the problem there is the same that I noted,
there is no fixed order, a+b*c and a*b+c are both very common
definitions -- although I lean towards the latter being the correct one,
given the order in the naming. I suppose this is a long winded way of
saying that:
#define struct_size(p, member, num) \
mult_add_no_overflow(num, sizeof(p->member), sizeof(*p))
would be *FAR* more readable. And then I still think struct_size() is
less readable than its expansion. ]]
> > > This provides __counted_by coverage, and I think this is important to
> > > gain in ever place we can. Given that this is part of a ring buffer
> > > implementation that is arbitrarily sized, this is exactly the kind of
> > > place I'd like to see __counted_by used. This is a runtime robustness
> > > improvement, so I don't see this a "churn" at all.
> >
> > Again, mixed in with that other crap. Anyway, remind me wth this
> > __counted_by thing actually does?
>
> It provides annotation for the compiler to perform run-time bounds
> checking on dynamically sized arrays. i.e. CONFIG_FORTIFY_SOURCE and
> CONFIG_UBSAN_BOUNDS can actually reason about annotated flexible arrays
> instead of just saying "oh no a flexible array, I give up".
Some day I'll have to look at this FORTIFY_SOURCE and see what it
actually does I suppose :/
> > > Peter, for patches 1 and 3, if you'd prefer not to carry them, I could
> > > put them in the hardening tree to keep them out of your way. It seems
> > > clear you don't want patch 2 at all.
> >
> > I prefer to not have struct_size() anywhere at all. Please just write
> > readable code.
>
> That ship has sailed, and it has been keeping things at bay for a while
> now. As we make progress on making the compiler able to do this more
> naturally, we can work on replacing struct_size(), but it's in use
> globally and it's useful both for catching runtime mistakes and for
> catching compile-time mistakes (the flexible array has to match the
> variable's struct).
I coulnd't quickly find a single instance in the code I care about. So
nothing is sailing afaict.
next prev parent reply other threads:[~2024-06-11 7:56 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-01 16:56 [PATCH v4 0/3] Hardening perf subsystem Erick Archer
2024-06-08 8:50 ` Erick Archer
2024-06-10 10:06 ` Peter Zijlstra
2024-06-10 17:28 ` Kees Cook
2024-06-10 20:05 ` Peter Zijlstra
2024-06-10 21:46 ` Kees Cook
2024-06-11 7:55 ` Peter Zijlstra [this message]
2024-06-12 19:01 ` Kees Cook
2024-06-12 22:08 ` Peter Zijlstra
2024-06-12 23:23 ` Kees Cook
2024-06-14 10:17 ` Peter Zijlstra
2024-06-15 16:09 ` Martin Uecker
2024-06-17 17:28 ` Kees Cook
2024-06-18 8:22 ` Peter Zijlstra
2024-06-20 18:26 ` Kees Cook
2024-06-17 17:19 ` Kees Cook
2024-06-18 8:28 ` Peter Zijlstra
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=20240611075542.GD8774@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bp@alien8.de \
--cc=christophe.jaillet@wanadoo.fr \
--cc=dave.hansen@linux.intel.com \
--cc=erick.archer@outlook.com \
--cc=gustavoars@kernel.org \
--cc=hpa@zytor.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=justinstitt@google.com \
--cc=kan.liang@linux.intel.com \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=mark.rutland@arm.com \
--cc=mawilcox@microsoft.com \
--cc=mingo@redhat.com \
--cc=morbo@google.com \
--cc=namhyung@kernel.org \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=tglx@linutronix.de \
--cc=x86@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.