linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rikard Falkeborn <rikard.falkeborn@gmail.com>
To: Sasha Levin <sashal@kernel.org>
Cc: Rikard Falkeborn <rikard.falkeborn@gmail.com>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Andrew Morton <akpm@linux-foundation.org>,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	Kees Cook <keescook@chromium.org>, Borislav Petkov <bp@alien8.de>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	Haren Myneni <haren@us.ibm.com>, Joe Perches <joe@perches.com>,
	Johannes Berg <johannes@sipsolutions.net>,
	Ingo Molnar <mingo@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH AUTOSEL 5.4 52/84] linux/bits.h: add compile time sanity check of GENMASK inputs
Date: Thu, 23 Apr 2020 23:40:23 +0200	[thread overview]
Message-ID: <20200423214023.GA1153@rikard> (raw)
In-Reply-To: <20200422005735.GW1809@sasha-vm>

On Tue, Apr 21, 2020 at 08:57:35PM -0400, Sasha Levin wrote:
> On Wed, Apr 15, 2020 at 09:40:32PM +0200, Rikard Falkeborn wrote:
> > On Wed, Apr 15, 2020 at 07:44:09AM -0400, Sasha Levin wrote:
> > > From: Rikard Falkeborn <rikard.falkeborn@gmail.com>
> > > 
> > > [ Upstream commit 295bcca84916cb5079140a89fccb472bb8d1f6e2 ]
> > > 
> > > 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
> > > 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_ON_ZERO() must be used
> > > instead of BUILD_BUG_ON().
> > > 
> > > __builtin_constant_p does not evaluate is argument, it only checks if it
> > > is a constant or not at compile time, and __builtin_choose_expr does not
> > > evaluate the expression that is not chosen.  Therefore, GENMASK(x++, 0)
> > > does only evaluate x++ once.
> > > 
> > > Commit 95b980d62d52 ("linux/bits.h: make BIT(), GENMASK(), and friends
> > > available in assembly") made the macros in linux/bits.h available in
> > > assembly.  Since BUILD_BUG_OR_ZERO() is not asm compatible, disable the
> > > checks if the file is included in an asm file.
> > > 
> > > Due to bugs in GCC versions before 4.9 [0], disable the check if building
> > > with a too old GCC compiler.
> > > 
> > > [0]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449
> > > 
> > > Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>
> > > Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> > > Reviewed-by: Masahiro Yamada <yamada.masahiro@socionext.com>
> > > Reviewed-by: Kees Cook <keescook@chromium.org>
> > > Cc: Borislav Petkov <bp@alien8.de>
> > > Cc: Geert Uytterhoeven <geert@linux-m68k.org>
> > > Cc: Haren Myneni <haren@us.ibm.com>
> > > Cc: Joe Perches <joe@perches.com>
> > > Cc: Johannes Berg <johannes@sipsolutions.net>
> > > Cc: lkml <linux-kernel@vger.kernel.org>
> > > Cc: Ingo Molnar <mingo@redhat.com>
> > > Cc: Thomas Gleixner <tglx@linutronix.de>
> > > Link: http://lkml.kernel.org/r/20200308193954.2372399-1-rikard.falkeborn@gmail.com
> > > Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> > > Signed-off-by: Sasha Levin <sashal@kernel.org>
> > > ---
> > >  include/linux/bits.h | 22 ++++++++++++++++++++--
> > >  1 file changed, 20 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/include/linux/bits.h b/include/linux/bits.h
> > > index 669d69441a625..f108302a3121c 100644
> > > --- a/include/linux/bits.h
> > > +++ b/include/linux/bits.h
> > > @@ -18,12 +18,30 @@
> > >   * position @h. For example
> > >   * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
> > >   */
> > > -#define GENMASK(h, l) \
> > > +#if !defined(__ASSEMBLY__) && \
> > > +	(!defined(CONFIG_CC_IS_GCC) || CONFIG_GCC_VERSION >= 49000)
> > > +#include <linux/build_bug.h>
> > > +#define GENMASK_INPUT_CHECK(h, l) \
> > > +	(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
> > > +		__builtin_constant_p((l) > (h)), (l) > (h), 0)))
> > > +#else
> > > +/*
> > > + * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
> > > + * disable the input check if that is the case.
> > > + */
> > > +#define GENMASK_INPUT_CHECK(h, l) 0
> > > +#endif
> > > +
> > > +#define __GENMASK(h, l) \
> > >  	(((~UL(0)) - (UL(1) << (l)) + 1) & \
> > >  	 (~UL(0) >> (BITS_PER_LONG - 1 - (h))))
> > > +#define GENMASK(h, l) \
> > > +	(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
> > > 
> > > -#define GENMASK_ULL(h, l) \
> > > +#define __GENMASK_ULL(h, l) \
> > >  	(((~ULL(0)) - (ULL(1) << (l)) + 1) & \
> > >  	 (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
> > > +#define GENMASK_ULL(h, l) \
> > > +	(GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l))
> > > 
> > >  #endif	/* __LINUX_BITS_H */
> > > --
> > > 2.20.1
> > > 
> > 
> > This does not really fix anything, it's compile time prevention, so I
> > don't know how appropriate this is for stable (it was also picked for
> > 5.5 and 5.6, but I'm just replying here now, I can ping the other
> > selections if necessary if the patch should be dropped)?
> > 
> > Also, for 5.4, it does somewhat depend on commit 8788994376d8
> > ("linux/build_bug.h: change type to int"). Without it, there may be a
> > subtle integer promotion issue if sizeof(size_t) > sizeof(unsigned long)
> > (I don't *think* such platform exists, but I don't have a warm a fuzzy
> > feeling about it).
> 
> I'll drop it from this selection, but ideally I'd like to have it in.
> 
> The codebase is different between Linus's tree and the stable trees, and
> I'm worried that some of these GENMASK issues were fixed upstream and
> not in the stable trees. Getting this patch back would help us fix those
> with minimal risk.
> 
> -- 
> Thanks,
> Sasha

Ok, I see. If you think it's good to backport it to the stable trees,
I'm all for it. I've been thinking a bit about commit 8788994376d8 
("linux/build_bug.h: change type to int"), I *think* it is probably fine
without it, but I'd rather be safe than sorry, but it's your call
really.

Rikard

Rikard

  reply	other threads:[~2020-04-23 21:40 UTC|newest]

Thread overview: 87+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-15 11:43 [PATCH AUTOSEL 5.4 01/84] drm/ttm: flush the fence on the bo after we individualize the reservation object Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 02/84] clk: Don't cache errors from clk_ops::get_phase() Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 03/84] clk: at91: usb: continue if clk_hw_round_rate() return zero Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 04/84] net/mlx5e: Enforce setting of a single FEC mode Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 05/84] f2fs: fix the panic in do_checkpoint() Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 06/84] arm64: dts: librem5-devkit: add a vbus supply to usb0 Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 07/84] ARM: dts: rockchip: fix vqmmc-supply property name for rk3188-bqedison2qc Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 08/84] arm64: dts: allwinner: a64: Fix display clock register range Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 09/84] power: supply: bq27xxx_battery: Silence deferred-probe error Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 10/84] clk: tegra: Fix Tegra PMC clock out parents Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 11/84] arm64: tegra: Add PCIe endpoint controllers nodes for Tegra194 Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 12/84] arm64: tegra: Fix Tegra194 PCIe compatible string Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 13/84] arm64: dts: clearfog-gt-8k: set gigabit PHY reset deassert delay Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 14/84] soc: imx: gpc: fix power up sequencing Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 15/84] dma-coherent: fix integer overflow in the reserved-memory dma allocation Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 16/84] rtc: 88pm860x: fix possible race condition Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 17/84] NFS: alloc_nfs_open_context() must use the file cred when available Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 18/84] NFSv4/pnfs: Return valid stateids in nfs_layout_find_inode_by_stateid() Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 19/84] NFSv4.2: error out when relink swapfile Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 20/84] ARM: dts: rockchip: fix lvds-encoder ports subnode for rk3188-bqedison2qc Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 21/84] KVM: PPC: Book3S HV: Fix H_CEDE return code for nested guests Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 22/84] f2fs: fix to show norecovery mount option Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 23/84] phy: uniphier-usb3ss: Add Pro5 support Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 24/84] NFS: direct.c: Fix memory leak of dreq when nfs_get_lock_context fails Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 25/84] f2fs: Fix mount failure due to SPO after a successful online resize FS Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 26/84] f2fs: Add a new CP flag to help fsck fix resize SPO issues Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 27/84] s390/cpuinfo: fix wrong output when CPU0 is offline Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 28/84] hibernate: Allow uswsusp to write to swap Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 29/84] btrfs: handle NULL roots in btrfs_put/btrfs_grab_fs_root Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 30/84] btrfs: add RCU locks around block group initialization Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 31/84] powerpc/prom_init: Pass the "os-term" message to hypervisor Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 32/84] powerpc/maple: Fix declaration made after definition Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 33/84] s390/cpum_sf: Fix wrong page count in error message Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 34/84] ext4: do not commit super on read-only bdev Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 35/84] ext4: fix incorrect group count in ext4_fill_super error message Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 36/84] ext4: fix incorrect inodes per group in " Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 37/84] um: ubd: Prevent buffer overrun on command completion Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 38/84] cifs: Allocate encryption header through kmalloc Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 39/84] cxgb4: fix MPS index overwrite when setting MAC address Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 40/84] slcan: Don't transmit uninitialized stack data in padding Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 41/84] net: qualcomm: rmnet: Allow configuration updates to existing devices Sasha Levin
2020-04-15 11:43 ` [PATCH AUTOSEL 5.4 42/84] mm/hugetlb: fix build failure with HUGETLB_PAGE but not HUGEBTLBFS Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 43/84] net: stmmac: dwmac1000: fix out-of-bounds mac address reg setting Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 44/84] net: dsa: mt7530: fix null pointer dereferencing in port5 setup Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 45/84] tun: Don't put_page() for all negative return values from XDP program Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 46/84] mlxsw: spectrum_flower: Do not stop at FLOW_ACTION_VLAN_MANGLE Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 47/84] net: dsa: bcm_sf2: Do not register slave MDIO bus with OF Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 48/84] drm/nouveau/svm: check for SVM initialized before migrating Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 49/84] drm/nouveau/svm: fix vma range check for migration Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 50/84] include/linux/swapops.h: correct guards for non_swap_entry() Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 51/84] percpu_counter: fix a data race at vm_committed_as Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 52/84] linux/bits.h: add compile time sanity check of GENMASK inputs Sasha Levin
2020-04-15 19:40   ` Rikard Falkeborn
2020-04-22  0:57     ` Sasha Levin
2020-04-23 21:40       ` Rikard Falkeborn [this message]
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 53/84] compiler.h: fix error in BUILD_BUG_ON() reporting Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 54/84] KVM: s390: vsie: Fix possible race when shadowing region 3 tables Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 55/84] drm/nouveau: workaround runpm fail by disabling PCI power management on certain intel bridges Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 56/84] leds: core: Fix warning message when init_data Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 57/84] net: dsa: bcm_sf2: Ensure correct sub-node is parsed Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 58/84] x86: ACPI: fix CPU hotplug deadlock Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 59/84] net: phy: micrel: kszphy_resume(): add delay after genphy_resume() before accessing PHY registers Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 60/84] csky: Fixup cpu speculative execution to IO area Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 61/84] drm/amdkfd: kfree the wrong pointer Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 62/84] NFS: Fix memory leaks in nfs_pageio_stop_mirroring() Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 63/84] csky: Fixup get wrong psr value from phyical reg Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 64/84] f2fs: fix NULL pointer dereference in f2fs_write_begin() Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 65/84] ACPICA: Fixes for acpiExec namespace init file Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 66/84] mfd: dln2: Fix sanity checking for endpoints Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 67/84] um: falloc.h needs to be directly included for older libc Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 68/84] drm/vc4: Fix HDMI mode validation Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 69/84] iommu/virtio: Fix freeing of incomplete domains Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 70/84] iommu/vt-d: Fix mm reference leak Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 71/84] SUNRPC: fix krb5p mount to provide large enough buffer in rq_rcvsize Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 72/84] ext2: fix empty body warnings when -Wextra is used Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 73/84] iommu/vt-d: Silence RCU-list debugging warning in dmar_find_atsr() Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 74/84] iommu/vt-d: Fix page request descriptor size Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 75/84] ovl: fix value of i_ino for lower hardlink corner case Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 76/84] ext2: fix debug reference to ext2_xattr_cache Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 77/84] sunrpc: Fix gss_unwrap_resp_integ() again Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 78/84] csky: Fixup init_fpu compile warning with __init Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 79/84] power: supply: axp288_fuel_gauge: Broaden vendor check for Intel Compute Sticks Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 80/84] platform/chrome: cros_ec_rpmsg: Fix race with host event Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 81/84] libnvdimm: Out of bounds read in __nd_ioctl() Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 82/84] acpi/nfit: improve bounds checking for 'func' Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 83/84] iommu/amd: Fix the configuration of GCR3 table root pointer Sasha Levin
2020-04-15 11:44 ` [PATCH AUTOSEL 5.4 84/84] f2fs: fix to wait all node page writeback Sasha Levin

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=20200423214023.GA1153@rikard \
    --to=rikard.falkeborn@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=geert@linux-m68k.org \
    --cc=haren@us.ibm.com \
    --cc=joe@perches.com \
    --cc=johannes@sipsolutions.net \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=yamada.masahiro@socionext.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;
as well as URLs for NNTP newsgroup(s).