Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp1333563lqg; Sun, 3 Mar 2024 05:39:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWzyWWocEuvnvxCtwArFOxJOF3fsuYJ9aJSVgDur9RpebkOJa4ekECbjwcuW2JVeOH3BYq+Fn2iTtv+SPhYUVFdGTYxUuNK5OHt2Ncs1w== X-Google-Smtp-Source: AGHT+IF7V4f5LV/Lr5VFUDTRlVd1NjqgxwYrtTmJ9RmMn8Gwvk4Lm8p03DovdOX+WFh3JesHcEN3 X-Received: by 2002:a05:6830:4ab:b0:6e4:d6e0:4776 with SMTP id l11-20020a05683004ab00b006e4d6e04776mr3986680otd.15.1709473146204; Sun, 03 Mar 2024 05:39:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709473146; cv=pass; d=google.com; s=arc-20160816; b=WLQWmHOQMATAft5AJiRfz5owIZmQgyhCVWBptIWzjALszMqhc0YW7gFHKHDNVLXmXR z5J3i/aYvFLXs6QMGFUi3emNWzRcne4CRPpJMP7u5r1uZ3jGYR3KQber663w4K0PQrdS wQKF3UEldxMj9MEoVMrMTRskDznNY81LfraAxQtyjGVsqs9MKSzC96Xqj3fGon+HcCJv FlNAB/AAA8VtEuzJsvSzVvK/OS+Iab5KASSRA/X7n1IEV+igBdu6iaUhIqW+z8JMrgz5 i8g0vVJ/M4ptlHt2JWUbCjDjBCmO1EC7O0t5sB1z6u5SUSt9qYAaYtNdNVcbWBAOQuL5 EbJw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=AaYg+84UpnvI55N5mXLGMaC2OyI6mbvpzT3hDzv5rt4=; fh=7NCXhyfDAYex43rnKgAyHV3WuRDp0w541SDArACpUtM=; b=rlEhSgrwYy31nlpL1KZInkaHTZnPz8+usrVXqqWP5u63WjFEewuxdQwYXFHBURzeUN C0wEPejuu9Eef7eI2AwBhHrlJ41qIwjLTcd48hWtA4uAxEaxtgWbUI2FRYHEaGAtpNtf QHt4CbxablPPzMBn78QiELXht8tV4u2i/XGHZw+1lGta0x3V+/t0Xgi4RWCSzvGR1ExI X211ln4eOiev6CzCHovASU5It6/VrjU6MU6j3uxtdoGjz/GpgMbOEhCwHsxJKDuY9hpC lYRvSTv1z/K0UiReBb2pwbmRlDv/BfC3GbHzOYaD5pCbt3foVliOT5UMd98Q8hKFIuvH WBFw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Xbo95VRU; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-89758-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89758-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id p30-20020a637f5e000000b005dc80f256ccsi6859528pgn.849.2024.03.03.05.39.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Mar 2024 05:39:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-89758-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Xbo95VRU; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-89758-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89758-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D81EC284F8E for ; Sun, 3 Mar 2024 13:39:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A6698548E2; Sun, 3 Mar 2024 13:39:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Xbo95VRU" Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 22C90566D for ; Sun, 3 Mar 2024 13:38:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709473139; cv=none; b=t5cZN1WOOke5H9qafEpBsL/rf2NhO3FOjmYC/qNnUBlp/UFO2UoWREKk77OM/jW1/ud0cmi5xoUdHiPdtF6gOK6BMt/TF2YY7NnzOeMEPYcG2/KQ+pdLPLWTLP2bbAT+Qq/DmBkw4j4fPttPp/E5Vj5kxZ6YMAFJzvkbq+QiC/c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709473139; c=relaxed/simple; bh=u+VLJpzpqT1+0pLTOvmbqH+wEH8WeQLz4rBd86XNMfk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Z0pXvLCjETfI5RFIpa1kjDjBAc9TL7VjQFyuB4ggToCi33J55o+trkjDyrq+fe5h2CizMFwAqk4Y30fBBI3nbenGY+TjvyhDcVB1hdg18lMgjUY2f27ALbjxULUUDwPEw4CKGMr2jEOBcSnL5H34VocouArU4PLnFD6Ym/wO9fU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Xbo95VRU; arc=none smtp.client-ip=209.85.221.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-33d146737e6so2605319f8f.0 for ; Sun, 03 Mar 2024 05:38:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709473136; x=1710077936; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=AaYg+84UpnvI55N5mXLGMaC2OyI6mbvpzT3hDzv5rt4=; b=Xbo95VRU1lOg66buFemKcvayhrMXEDal0x3FHlj81+UbhU66SjqmhtyOyl/RpE8iY6 Mbv/yh65utXmS6UkLt/UTxrpyNoZIeGxhzaQFSwJdyQrUUzbfk96MPS9MHFYHrkbG4fw zEiRXS8t+Wr/DSiiIVn4a12iX97QYRnoDmeF0Dm7rK7/i5mfnTATwLuYscNX/3oIqHLz 1CavBIZ4kV6ws8gNna7EkjDW4v7LUZ0gD7AjJJEeDSwWdPD85KMdbKoCZpOp8ZueMYj5 frkfBwP/kyHSnixCVAPJAtG9Nd5tW8F+sYJAVPbWT7lb5NxtJgwkTeqVfNoR/bkOTT3o p4kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709473136; x=1710077936; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=AaYg+84UpnvI55N5mXLGMaC2OyI6mbvpzT3hDzv5rt4=; b=jUwnxraERC76SW6zDYnnqcpBi3LCVCvp6QpkckVhBgPwGRWd49zVs5aIu45uDusFS/ mUSkk1LY8/J1ADOYojpPxwG3ntuh1/6osNhhauxVSx+Tb7eHEGU8yn1GCapJ7ynk42Y/ TyEZYkv3bSIY13CJqUmqXpgPD0V1VNv3fWrj4ifCKoNZFX/kteCobc5TC/bSg030rLae Fbqk1qaH2HDOW6ynCGexP8QIsh/voPvcuGe4K5rmE7h6Ydsajr9tSzIB/hYNZItfQfyM We4ub40QCbzcZgYMatLUQVdYaf10yESkAhFei4bZeIouOMuZ17vRCJqqwdyMlX7eNRcv EJSg== X-Forwarded-Encrypted: i=1; AJvYcCXaXh54EGBBk/671QXOfuZlR3tvw7xEOQ8HAgXDgdRR/JQ/jT8NHYBlddibwjKaprw92zEgqKKFbBj5J7pNlu9wY2XKpgYnNGd+7gSI X-Gm-Message-State: AOJu0Yx4VV8jYOY+R40IVwLdJmmUzTZo75Z+bHGrEvK+BnFcvOI6kiGg PbZqUm6DNT969q7kKum8xQUs47fUMIpQTTDQZmN+ElIKX+ALGeJ+ X-Received: by 2002:adf:cc06:0:b0:33e:6ac:691b with SMTP id x6-20020adfcc06000000b0033e06ac691bmr4913224wrh.9.1709473136200; Sun, 03 Mar 2024 05:38:56 -0800 (PST) Received: from localhost ([2a00:23cc:d20f:ba01:bb66:f8b2:a0e8:6447]) by smtp.gmail.com with ESMTPSA id az23-20020adfe197000000b0033d97bd5ddasm9822234wrb.85.2024.03.03.05.38.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Mar 2024 05:38:55 -0800 (PST) Date: Sun, 3 Mar 2024 13:38:54 +0000 From: Lorenzo Stoakes To: Kefeng Wang Cc: Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Yue Sun Subject: Re: [PATCH] mm: memory: fix shift-out-of-bounds in fault_around_bytes_set Message-ID: References: <20240302064312.2358924-1-wangkefeng.wang@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240302064312.2358924-1-wangkefeng.wang@huawei.com> On Sat, Mar 02, 2024 at 02:43:12PM +0800, Kefeng Wang wrote: > The rounddown_pow_of_two(0) is undefined, so val = 0 is not allowed in > the fault_around_bytes_set(), and leads to shift-out-of-bounds, > > UBSAN: shift-out-of-bounds in include/linux/log2.h:67:13 > shift exponent 4294967295 is too large for 64-bit type 'long unsigned int' > CPU: 7 PID: 107 Comm: sh Not tainted 6.8.0-rc6-next-20240301 #294 > Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 > Call trace: > dump_backtrace+0x94/0xec > show_stack+0x18/0x24 > dump_stack_lvl+0x78/0x90 > dump_stack+0x18/0x24 > ubsan_epilogue+0x10/0x44 > __ubsan_handle_shift_out_of_bounds+0x98/0x134 > fault_around_bytes_set+0xa4/0xb0 > simple_attr_write_xsigned.isra.0+0xe4/0x1ac > simple_attr_write+0x18/0x24 > debugfs_attr_write+0x4c/0x98 > vfs_write+0xd0/0x4b0 > ksys_write+0x6c/0xfc > __arm64_sys_write+0x1c/0x28 > invoke_syscall+0x44/0x104 > el0_svc_common.constprop.0+0x40/0xe0 > do_el0_svc+0x1c/0x28 > el0_svc+0x34/0xdc > el0t_64_sync_handler+0xc0/0xc4 > el0t_64_sync+0x190/0x194 > ---[ end trace ]--- > > Fix it by setting the minimum val to PAGE_SIZE. > > Reported-by: Yue Sun > Closes: https://lore.kernel.org/all/CAEkJfYPim6DQqW1GqCiHLdh2-eweqk1fGyXqs3JM+8e1qGge8w@mail.gmail.com/ > Fixes: 53d36a56d8c4 ("mm: prefer fault_around_pages to fault_around_bytes") > Signed-off-by: Kefeng Wang > --- > mm/memory.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/mm/memory.c b/mm/memory.c > index abd4f33d62c9..e17669d4f72f 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -4776,7 +4776,8 @@ static int fault_around_bytes_set(void *data, u64 val) > * The minimum value is 1 page, however this results in no fault-around > * at all. See should_fault_around(). > */ > - fault_around_pages = max(rounddown_pow_of_two(val) >> PAGE_SHIFT, 1UL); > + val = max(val, PAGE_SIZE); > + fault_around_pages = rounddown_pow_of_two(val) >> PAGE_SHIFT; > > return 0; > } > -- > 2.27.0 > Thanks, this was an oversight on my part, this fix looks good: Reviewed-by: Lorenzo Stoakes