* UBSAN: annotation to skip sanitization in variable that will wrap
@ 2024-08-14 17:10 Breno Leitao
2024-08-14 21:05 ` Justin Stitt
0 siblings, 1 reply; 6+ messages in thread
From: Breno Leitao @ 2024-08-14 17:10 UTC (permalink / raw)
To: kees, elver, andreyknvl, ryabinin.a.a
Cc: kasan-dev, linux-hardening, axboe, asml.silence, netdev
Hello,
I am seeing some signed-integer-overflow in percpu reference counters.
UBSAN: signed-integer-overflow in ./arch/arm64/include/asm/atomic_lse.h:204:1
-9223372036854775808 - 1 cannot be represented in type 's64' (aka 'long long')
Call trace:
handle_overflow
__ubsan_handle_sub_overflow
percpu_ref_put_many
css_put
cgroup_sk_free
__sk_destruct
__sk_free
sk_free
unix_release_sock
unix_release
sock_close
This overflow is probably happening in percpu_ref->percpu_ref_data->count.
Looking at the code documentation, it seems that overflows are fine in
per-cpu values. The lib/percpu-refcount.c code comment says:
* Note that the counter on a particular cpu can (and will) wrap - this
* is fine, when we go to shutdown the percpu counters will all sum to
* the correct value
Is there a way to annotate the code to tell UBSAN that this overflow is
expected and it shouldn't be reported?
Thanks
--breno
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: UBSAN: annotation to skip sanitization in variable that will wrap
2024-08-14 17:10 UBSAN: annotation to skip sanitization in variable that will wrap Breno Leitao
@ 2024-08-14 21:05 ` Justin Stitt
2024-08-15 16:20 ` Kees Cook
2024-08-15 17:58 ` Breno Leitao
0 siblings, 2 replies; 6+ messages in thread
From: Justin Stitt @ 2024-08-14 21:05 UTC (permalink / raw)
To: Breno Leitao
Cc: kees, elver, andreyknvl, ryabinin.a.a, kasan-dev, linux-hardening,
axboe, asml.silence, netdev
Hi,
On Wed, Aug 14, 2024 at 10:10 AM Breno Leitao <leitao@debian.org> wrote:
>
> Hello,
>
> I am seeing some signed-integer-overflow in percpu reference counters.
it is brave of you to enable this sanitizer :>)
>
> UBSAN: signed-integer-overflow in ./arch/arm64/include/asm/atomic_lse.h:204:1
> -9223372036854775808 - 1 cannot be represented in type 's64' (aka 'long long')
> Call trace:
>
> handle_overflow
> __ubsan_handle_sub_overflow
> percpu_ref_put_many
> css_put
> cgroup_sk_free
> __sk_destruct
> __sk_free
> sk_free
> unix_release_sock
> unix_release
> sock_close
>
> This overflow is probably happening in percpu_ref->percpu_ref_data->count.
>
> Looking at the code documentation, it seems that overflows are fine in
> per-cpu values. The lib/percpu-refcount.c code comment says:
>
> * Note that the counter on a particular cpu can (and will) wrap - this
> * is fine, when we go to shutdown the percpu counters will all sum to
> * the correct value
>
> Is there a way to annotate the code to tell UBSAN that this overflow is
> expected and it shouldn't be reported?
Great question.
1) There exists some new-ish macros in overflow.h that perform
wrapping arithmetic without triggering sanitizer splats -- check out
the wrapping_* suite of macros.
2) I have a Clang attribute in the works [1] that would enable you to
annotate expressions or types that are expected to wrap and will
therefore silence arithmetic overflow/truncation sanitizers. If you
think this could help make the kernel better then I'd appreciate a +1
on that PR so it can get some more review from compiler people! Kees
and I have some other Clang features in the works that will allow for
better mitigation strategies for intended overflow in the kernel.
3) Kees can probably chime in with some other methods of getting the
sanitizer to shush -- we've been doing some work together in this
space. Also check out [2]
>
> Thanks
> --breno
>
>
[1]: https://github.com/llvm/llvm-project/pull/86618
[2]: https://lwn.net/Articles/979747/ (Arithmetic overflow mitigation
in the kernel; Jul 1 2024)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: UBSAN: annotation to skip sanitization in variable that will wrap
2024-08-14 21:05 ` Justin Stitt
@ 2024-08-15 16:20 ` Kees Cook
2024-08-15 17:58 ` Breno Leitao
1 sibling, 0 replies; 6+ messages in thread
From: Kees Cook @ 2024-08-15 16:20 UTC (permalink / raw)
To: Justin Stitt
Cc: Breno Leitao, elver, andreyknvl, ryabinin.a.a, kasan-dev,
linux-hardening, axboe, asml.silence, netdev
On Wed, Aug 14, 2024 at 02:05:49PM -0700, Justin Stitt wrote:
> Hi,
>
> On Wed, Aug 14, 2024 at 10:10 AM Breno Leitao <leitao@debian.org> wrote:
> >
> > Hello,
> >
> > I am seeing some signed-integer-overflow in percpu reference counters.
>
> it is brave of you to enable this sanitizer :>)
>
> >
> > UBSAN: signed-integer-overflow in ./arch/arm64/include/asm/atomic_lse.h:204:1
> > -9223372036854775808 - 1 cannot be represented in type 's64' (aka 'long long')
> > Call trace:
> >
> > handle_overflow
> > __ubsan_handle_sub_overflow
> > percpu_ref_put_many
> > css_put
> > cgroup_sk_free
> > __sk_destruct
> > __sk_free
> > sk_free
> > unix_release_sock
> > unix_release
> > sock_close
> >
> > This overflow is probably happening in percpu_ref->percpu_ref_data->count.
> >
> > Looking at the code documentation, it seems that overflows are fine in
> > per-cpu values. The lib/percpu-refcount.c code comment says:
> >
> > * Note that the counter on a particular cpu can (and will) wrap - this
> > * is fine, when we go to shutdown the percpu counters will all sum to
> > * the correct value
> >
> > Is there a way to annotate the code to tell UBSAN that this overflow is
> > expected and it shouldn't be reported?
>
> Great question.
>
> 1) There exists some new-ish macros in overflow.h that perform
> wrapping arithmetic without triggering sanitizer splats -- check out
> the wrapping_* suite of macros.
>
> 2) I have a Clang attribute in the works [1] that would enable you to
> annotate expressions or types that are expected to wrap and will
> therefore silence arithmetic overflow/truncation sanitizers. If you
> think this could help make the kernel better then I'd appreciate a +1
> on that PR so it can get some more review from compiler people! Kees
> and I have some other Clang features in the works that will allow for
> better mitigation strategies for intended overflow in the kernel.
>
> 3) Kees can probably chime in with some other methods of getting the
> sanitizer to shush -- we've been doing some work together in this
> space. Also check out [2]
I haven't checked closely yet, but I *think* top 4 patches here[1]
(proposed here[2]) fix the atomics issues. The haven't landed due to
atomics maintainers wanting differing behavior from the compiler that
Justin is still working on (the "wraps" attribute alluded to above[3]).
-Kees
[1] https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=dev/v6.8-rc2/signed-overflow-sanitizer
[2] https://lore.kernel.org/linux-hardening/20240424191225.work.780-kees@kernel.org/
[3] https://github.com/llvm/llvm-project/pull/86618
--
Kees Cook
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: UBSAN: annotation to skip sanitization in variable that will wrap
2024-08-14 21:05 ` Justin Stitt
2024-08-15 16:20 ` Kees Cook
@ 2024-08-15 17:58 ` Breno Leitao
2024-08-15 18:40 ` Jens Axboe
1 sibling, 1 reply; 6+ messages in thread
From: Breno Leitao @ 2024-08-15 17:58 UTC (permalink / raw)
To: Justin Stitt
Cc: kees, elver, andreyknvl, ryabinin.a.a, kasan-dev, linux-hardening,
axboe, asml.silence, netdev
Hello Justin,
On Wed, Aug 14, 2024 at 02:05:49PM -0700, Justin Stitt wrote:
> > I am seeing some signed-integer-overflow in percpu reference counters.
>
> it is brave of you to enable this sanitizer :>)
UBSAN has been somehow useful to pick some problems, so, I try to invest
some time understanding what UBSAN, and see how much it can help when
solving "unexpected" and misterious issues, which is something that
challenges me.
> > Is there a way to annotate the code to tell UBSAN that this overflow is
> > expected and it shouldn't be reported?
> Great question.
>
> 1) There exists some new-ish macros in overflow.h that perform
> wrapping arithmetic without triggering sanitizer splats -- check out
> the wrapping_* suite of macros.
do they work for atomic? I suppose we also need to have them added to
this_cpu_add(), this_cpu_sub() helpers.
> 2) I have a Clang attribute in the works [1] that would enable you to
> annotate expressions or types that are expected to wrap and will
> therefore silence arithmetic overflow/truncation sanitizers. If you
> think this could help make the kernel better then I'd appreciate a +1
> on that PR so it can get some more review from compiler people! Kees
> and I have some other Clang features in the works that will allow for
> better mitigation strategies for intended overflow in the kernel.
Thanks. I've added a +1 there.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: UBSAN: annotation to skip sanitization in variable that will wrap
2024-08-15 17:58 ` Breno Leitao
@ 2024-08-15 18:40 ` Jens Axboe
2024-08-16 19:47 ` Kees Cook
0 siblings, 1 reply; 6+ messages in thread
From: Jens Axboe @ 2024-08-15 18:40 UTC (permalink / raw)
To: Breno Leitao, Justin Stitt
Cc: kees, elver, andreyknvl, ryabinin.a.a, kasan-dev, linux-hardening,
asml.silence, netdev
On 8/15/24 11:58 AM, Breno Leitao wrote:
>> 1) There exists some new-ish macros in overflow.h that perform
>> wrapping arithmetic without triggering sanitizer splats -- check out
>> the wrapping_* suite of macros.
>
> do they work for atomic? I suppose we also need to have them added to
> this_cpu_add(), this_cpu_sub() helpers.
I don't think so, it's the bias added specifically to the atomic_long_t
that's the issue with the percpu refs.
--
Jens Axboe
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: UBSAN: annotation to skip sanitization in variable that will wrap
2024-08-15 18:40 ` Jens Axboe
@ 2024-08-16 19:47 ` Kees Cook
0 siblings, 0 replies; 6+ messages in thread
From: Kees Cook @ 2024-08-16 19:47 UTC (permalink / raw)
To: Jens Axboe
Cc: Breno Leitao, Justin Stitt, elver, andreyknvl, ryabinin.a.a,
kasan-dev, linux-hardening, asml.silence, netdev
On Thu, Aug 15, 2024 at 12:40:12PM -0600, Jens Axboe wrote:
> On 8/15/24 11:58 AM, Breno Leitao wrote:
> >> 1) There exists some new-ish macros in overflow.h that perform
> >> wrapping arithmetic without triggering sanitizer splats -- check out
> >> the wrapping_* suite of macros.
> >
> > do they work for atomic? I suppose we also need to have them added to
> > this_cpu_add(), this_cpu_sub() helpers.
>
> I don't think so, it's the bias added specifically to the atomic_long_t
> that's the issue with the percpu refs.
Yeah, the future annotations will be variable attributes, so it should
be much nicer to apply.
--
Kees Cook
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-08-16 19:47 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-14 17:10 UBSAN: annotation to skip sanitization in variable that will wrap Breno Leitao
2024-08-14 21:05 ` Justin Stitt
2024-08-15 16:20 ` Kees Cook
2024-08-15 17:58 ` Breno Leitao
2024-08-15 18:40 ` Jens Axboe
2024-08-16 19:47 ` Kees Cook
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).