|
|
Subscribe / Log in / New account

Re: [RFC PATCH 03/13] usb: remove the usage of the list iterator after the loop

Thread information [Search the linux-kernel archive]
 [RFC PATCH 00/13] Proposal for speculative safe list iterator Jakob Koschel
 ` [RFC PATCH 01/13] list: introduce speculative safe list_for_each_entry() Jakob Koschel
   ` Greg Kroah-Hartman
     ` Jann Horn
   ` Jann Horn
     ` Jakob
   ` Jann Horn
 ` [RFC PATCH 02/13] scripts: coccinelle: adapt to find list_for_each_entry nospec issues Jakob Koschel
 ` [RFC PATCH 03/13] usb: remove the usage of the list iterator after the loop Jakob Koschel
   ` Linus Torvalds
     ` Jakob
       ` Jakob
         ` Greg Kroah-Hartman
           ` Linus Torvalds
           ` Cristiano Giuffrida
             ` Greg Kroah-Hartman
       ` Linus Torvalds [this message]
         ` Linus Torvalds
           ` Linus Torvalds
           ` Arnd Bergmann
             ` Linus Torvalds
               ` Arnd Bergmann
                 ` Linus Torvalds
                   ` Nathan Chancellor
               ` Linus Torvalds
                 ` David Laight
                 ` Uecker, Martin
                   ` Linus Torvalds
                     ` Martin Uecker
                       ` Miguel Ojeda
                         ` Martin Uecker
                           ` Miguel Ojeda
                             ` Linus Torvalds
                               ` Martin Uecker
           ` Segher Boessenkool
             ` Arnd Bergmann
               ` Linus Torvalds
                 ` Segher Boessenkool
               ` Segher Boessenkool
                 ` David Laight
                   ` Segher Boessenkool
                     ` Miguel Ojeda
                       ` Segher Boessenkool
                         ` Linus Torvalds
                           ` David Laight
                         ` Miguel Ojeda
                 ` Arnd Bergmann
                   ` Segher Boessenkool
 ` [RFC PATCH 04/13] vfio/mdev: " Jakob Koschel
   ` Jason Gunthorpe
     ` Jakob
       ` Linus Torvalds
         ` Jason Gunthorpe
           ` Linus Torvalds
             ` Jakob
               ` Linus Torvalds
                 ` Jakob
             ` Rasmus Villemoes
               ` Linus Torvalds
 ` [RFC PATCH 05/13] drivers/perf: " Jakob Koschel
 ` [RFC PATCH 06/13] ARM: mmp: " Jakob Koschel
 ` [RFC PATCH 07/13] udp_tunnel: " Jakob Koschel
   ` Christophe JAILLET
     ` Dan Carpenter
 ` [RFC PATCH 08/13] net: dsa: future proof usage of " Jakob Koschel
 ` [RFC PATCH 09/13] drbd: " Jakob Koschel
 ` [RFC PATCH 10/13] powerpc/spufs: " Jakob Koschel
 ` [RFC PATCH 11/13] ath6kl: remove use " Jakob Koschel
 ` [RFC PATCH 12/13] staging: greybus: audio: Remove usage " Jakob Koschel
 ` [RFC PATCH 13/13] scsi: mpt3sas: comment about invalid usage of the list iterator Jakob Koschel

From:  Linus Torvalds <torvalds-AT-linux-foundation.org>
To:  Jakob <jakobkoschel-AT-gmail.com>, Arnd Bergmann <arnd-AT-arndb.de>
Subject:  Re: [RFC PATCH 03/13] usb: remove the usage of the list iterator after the loop
Date:  Wed, 23 Feb 2022 10:47:00 -0800
Message-ID:  <CAHk-=wiyCH7xeHcmiFJ-YgXUy2Jaj7pnkdKpcovt8fYbVFW3TA@mail.gmail.com>
Cc:  Linux Kernel Mailing List <linux-kernel-AT-vger.kernel.org>, linux-arch <linux-arch-AT-vger.kernel.org>, Greg Kroah-Hartman <gregkh-AT-linuxfoundation.org>, Thomas Gleixner <tglx-AT-linutronix.de>, Andy Shevchenko <andriy.shevchenko-AT-linux.intel.com>, Andrew Morton <akpm-AT-linux-foundation.org>, Kees Cook <keescook-AT-chromium.org>, Mike Rapoport <rppt-AT-kernel.org>, "Gustavo A. R. Silva" <gustavo-AT-embeddedor.com>, Brian Johannesmeyer <bjohannesmeyer-AT-gmail.com>, Cristiano Giuffrida <c.giuffrida-AT-vu.nl>, "Bos, H.J." <h.j.bos-AT-vu.nl>

[ Arnd was already on the participants, but I moved him from 'Cc:' to
'To:', just because I think this is once again tangentially related to
the whole "c99 base" thing ]

On Wed, Feb 23, 2022 at 6:13 AM Jakob <jakobkoschel@gmail.com> wrote:
>
> I'm sorry for having created the confusion. I made this patch to support
> the speculative safe list_for_each_entry() version but it is not actually
> related to that. I do believe that this an actual bug and *could*
> *potentially* be misused. I'll follow up with an example to illustrate that.

Ok, so this is just a regular bug, plain and simple.

The problem being that the list_for_each_entry() will iterate over
each list entry - but at the end of the loop it will not point at any
entry at all (it will have a pointer value that is related to the
*HEAD* of the list, but that is not necessarily the same kind of entry
that the list members are.

Honestly, I think this kind of fix should have been done entirely separately.

In fact, I think the change to list_for_each_entry() should have been
done not as "fix type speculation", but as a much more interesting
"fix the list iterators".

The whole reason this kind of non-speculative bug can happen is that
we historically didn't have C99-style "declare variables in loops". So
list_for_each_entry() - and all the other ones - fundamentally always
leaks the last HEAD entry out of the loop, simply because we couldn't
declare the iterator variable in the loop itself.

(And by "couldn't", I mean "without making for special syntax": we do
exactly that in "for_each_thread ()" and friends, but they have an
"end_for_each_thread()" thing at the end).

So what I'd personally *really* like to see would be for us to - once
again - look at using "-std=gnu99", and fix the whole "leak final
invalid pointer outside the loop".

Then the type speculation thing would be an entirely separate patch.

Because honestly, I kind of hate the completely random type
speculation patch. It fixes one particular type of loop, and not even
one that seems all that special.

But we still don't do "gnu99", because we had some odd problem with
some ancient gcc versions that broke documented initializers.

I honestly _thought_ we had gotten over that already. I think the
problem cases were gcc-4.9 and older, and now we require gcc-5.1, and
we could just use "--std=gnu99" for the kernel, and we could finally
start using variable declarations in for-statements.

Arnd - remind me, please.. Was there some other problem than just gcc-4.9?

                 Linus


Copyright © 2024, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds