Re: [PATCH] wifi: nl80211: drop impossible negative band check

From: Johannes Berg

Date: Wed Feb 04 2026 - 06:12:57 EST


On Wed, 2026-02-04 at 17:13 +0800, sun jian wrote:
> On Wed, Feb 4, 2026 at 4:36 PM Johannes Berg <johannes@xxxxxxxxxxxxxxxx> 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