[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CABFUUZEiMFoGg4r+9q1FabbwD1Pfd0oZZeNxFqnM22nA1xn8UQ@mail.gmail.com>
Date: Wed, 4 Feb 2026 17:13:18 +0800
From: sun jian <sun.jian.kdev@...il.com>
To: Johannes Berg <johannes@...solutions.net>
Cc: linux-wireless@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] wifi: nl80211: drop impossible negative band check
On Wed, Feb 4, 2026 at 4:36 PM Johannes Berg <johannes@...solutions.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.
> 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...
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.
>
> Am I wrong?
I think the "enum may be signed" concern is valid in general, but for
this particular assignment the masking guarantees the value is always in
a non-negative range.
Thanks,
Sun Jian
Powered by blists - more mailing lists