lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <d18482fae2c6ca7cdb955aff4c724b007ef45aba.camel@perches.com>
Date:   Wed, 31 Jul 2019 19:57:48 -0700
From:   Joe Perches <joe@...ches.com>
To:     Masahiro Yamada <yamada.masahiro@...ionext.com>,
        Rikard Falkeborn <rikard.falkeborn@...il.com>
Cc:     Andrew Morton <akpm@...ux-foundation.org>,
        Johannes Berg <johannes@...solutions.net>,
        Linux Kernel Mailing List <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] linux/bits.h: Add compile time sanity check of GENMASK
 inputs

On Thu, 2019-08-01 at 11:50 +0900, Masahiro Yamada wrote:
> On Thu, Aug 1, 2019 at 4:04 AM Rikard Falkeborn
> <rikard.falkeborn@...il.com> wrote:
> > GENMASK() and GENMASK_ULL() are supposed to be called with the high bit
> > as the first argument and the low bit as the second argument. Mixing
> > them will return a mask with zero bits set.
> > 
> > Recent commits show getting this wrong is not uncommon, see e.g.
> > commit aa4c0c9091b0 ("net: stmmac: Fix misuses of GENMASK macro") and
> > commit 9bdd7bb3a844 ("clocksource/drivers/npcm: Fix misuse of GENMASK
> > macro").
> > 
> > To prevent such mistakes from appearing again, add compile time sanity
> > checking to the arguments of GENMASK() and GENMASK_ULL(). If both the
> > arguments are known at compile time, and the low bit is higher than the
> > high bit, break the build to detect the mistake immediately.
> > 
> > Since GENMASK() is used in declarations, BUILD_BUG_OR_ZERO() must be
> > used instead of BUILD_BUG_ON(), and __is_constexpr() must be used instead
> > of __builtin_constant_p().
> > 
> > Commit 95b980d62d52 ("linux/bits.h: make BIT(), GENMASK(), and friends
> > available in assembly") made the macros in linux/bits.h available in
> > assembly. Since neither BUILD_BUG_OR_ZERO() or __is_constexpr() are asm
> > compatible, disable the checks if the file is included in an asm file.
> > 
> > Signed-off-by: Rikard Falkeborn <rikard.falkeborn@...il.com>
> > ---
> > Joe Perches sent a series to fix the existing misuses of GENMASK() that
> > needs to be merged before this to avoid build failures. Currently, 7 of
> > the patches were not in Linus tree, and 2 were not in linux-next.
> > 
> > Also, there's currently no asm users of bits.h, but since it was made
> > asm-compatible just two weeks ago it would be a shame to break it right
> > away...
[]
> > diff --git a/include/linux/bits.h b/include/linux/bits.h
[]
> >  #define GENMASK(h, l) \
> > +       (GENMASK_INPUT_CHECK(h, l) + \
> >         (((~UL(0)) - (UL(1) << (l)) + 1) & \
> > -        (~UL(0) >> (BITS_PER_LONG - 1 - (h))))
> > +        (~UL(0) >> (BITS_PER_LONG - 1 - (h)))))
> > 
> >  #define GENMASK_ULL(h, l) \
> > +       (GENMASK_INPUT_CHECK(h, l) + \
> >         (((~ULL(0)) - (ULL(1) << (l)) + 1) & \
> > -        (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
> > +        (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h)))))
> 
> This is getting cluttered with so many parentheses.
> 
> One way of clean-up is to rename the current macros as follows:
> 
>    GENMASK()    ->  __GENMASK()
>    GENMASK_UL() ->  __GENMASK_ULL()
> 
> Then,
> 
> #define GENMASK(h, l)       (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
> #define GENMASK_ULL(h, l)   (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l))

Much nicer.  It may be better still to use avoid
multiple dereferences of each argument.

Also it'd be useful to rename h and l to something like
high_bit and low_bit or high and low.


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ