mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Johannes Berg <johannes@sipsolutions.net>
To: sun jian <sun.jian.kdev@gmail.com>
Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] wifi: nl80211: drop impossible negative band check
Date: Wed, 04 Feb 2026 12:11:20 +0100	[thread overview]
Message-ID: <074b66ab3d63e6640ecd6962c074702225fba19e.camel@sipsolutions.net> (raw)
In-Reply-To: <CABFUUZEiMFoGg4r+9q1FabbwD1Pfd0oZZeNxFqnM22nA1xn8UQ@mail.gmail.com> (sfid-20260204_101330_872267_A6E97739)

On Wed, 2026-02-04 at 17:13 +0800, sun jian wrote:
> On Wed, Feb 4, 2026 at 4:36 PM Johannes Berg <johannes@sipsolutions.net> wrote:
> > 
> > On Wed, 2026-02-04 at 16:18 +0800, Sun Jian wrote:
> > > band is derived from nla_type() of a nested netlink attribute, which isAgreed — in general the enum underlying type can be signed.
> > > a masked u16 value and therefore cannot be negative. Drop the dead
> > > "band < 0" checks and keep the upper bound validation.
> > 
> > I've seen this before, but I'm not really convinced it is entirely
> > correct. C says:
> > 
> >    All enumerations have an underlying type. The underlying type can be
> >    explicitly specified using an enum type specifier and is its fixed
> >    underlying type. If it is not explicitly specified, the underlying
> >    type is the enumeration’s compatible type, which is either char or a
> >    standard or extended signed or unsigned integer type.
> > 
> 
> Agreed — in general the enum underlying type can be signed.

But nothing says it cannot be "signed char".

> > It would thus _seem_ to be possible for an enum to generally be a signed
> > type, and therefore a 'signed short', and therefore an nla_type() that's
> > a u16 could end up with a negative value...

I was just using 'signed short' as an example, but your argument:

> The key detail here is that band isn't assigned the raw __u16
> nla->nla_type, but nla_type().
> 
> And nla_type() is effectively:
> nla->nla_type & NLA_TYPE_MASK
> 
> and NLA_TYPE_MASK clears the two high flag bits:
> NLA_F_NESTED (1 << 15)
> NLA_F_NET_BYTEORDER (1 << 14)
> 
> So the result is restricted to the low 14 bits, i.e. 0..0x3fff.
> 
> With that restriction, even if enum nl80211_band ended up with a signed
> 16-bit underlying type, the sign bit (bit 15) can never be set by
> nla_type(), so the value cannot become negative.

applies _only_ to signed short, not to signed char?

Now we can argue a "sane compiler" won't do that, and we can also argue
that "gcc and clang are sane compilers", although sometimes I definitely
have doubts about the latter ;-)

johannes

  reply	other threads:[~2026-02-04 11:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-04  8:18 Sun Jian
2026-02-04  8:36 ` Johannes Berg
2026-02-04  9:13   ` sun jian
2026-02-04 11:11     ` Johannes Berg [this message]
2026-02-04 11:21       ` sun jian
2026-02-04 11:29         ` Johannes Berg
2026-02-04 11:33           ` sun jian
2026-02-04 16:20             ` Jeff Johnson
2026-02-06  2:16               ` sun jian

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=074b66ab3d63e6640ecd6962c074702225fba19e.camel@sipsolutions.net \
    --to=johannes@sipsolutions.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=sun.jian.kdev@gmail.com \
    /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®