From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C049A606A32C for ; Tue, 17 Nov 2026 14:26:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=a786Rfbu Received: by kanga.kvack.org (Postfix) id D058F6B05B8; Tue, 17 Nov 2026 09:26:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CDD226B05B9; Tue, 17 Nov 2026 09:26:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF3846B05BA; Tue, 17 Nov 2026 09:26:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A1E6A6B05B8 for ; Tue, 17 Nov 2026 09:26:38 -0500 (EST) Received: from smtpin02.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1F97C1203C3 for ; Tue, 17 Nov 2026 14:26:38 +0000 (UTC) X-FDA: 85444836876.02.AF71599 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf18.hostedemail.com (Postfix) with ESMTP id 7DD141C0005 for ; Tue, 17 Nov 2026 14:26:36 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=a786Rfbu; spf=pass (imf18.hostedemail.com: domain of arnd@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=arnd@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1794925596; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=rlQlNnIZ/8+czgBS03uRUWP6VlvdPXDkWQ0M5ulQFY4=; b=G9cWLGMyxI54Pc/dzif8iSOOEX6Gg3M9Quiq8JTAtdFdg4yqul/dyPOEQaAktl88fqonBZ qZMAPaDGazBIFqXqqr4lVA4laCjQrxJuR4yx2KlzY5qLi8dzA7rXfzupCLz5cWd22slvmK oOvPwXig+XANHKKFb8RtVKIGNOfKG/I= ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1794925596; b=GWypiqgxYdGOVPd0y5xl/eecTfMXYOAYzqP2+bLRfyGA3R++tx0/gzq/a77kqL1bWXSAmV xISuekDPdpKB/WVXxfW/zDaGGfmlYE1ju3xOYS0mMOcxKBCWdFfKpjOptyjbmywBhXCTIQ cQ0tqjghnRoh3oOFNMpFEUsUu0D4xP4= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=a786Rfbu; spf=pass (imf18.hostedemail.com: domain of arnd@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=arnd@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 5A38840AF4; Tue, 17 Nov 2026 14:26:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D97441F000C4; Tue, 17 Nov 2026 14:26:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794925595; bh=rlQlNnIZ/8+czgBS03uRUWP6VlvdPXDkWQ0M5ulQFY4=; h=From:To:Cc:Subject:Date; b=a786Rfbua0e0KJuPtqYPrAIOolUAlNtoH1SVZATqAvSLv5QCYzTn6e5ZZA3a3CXny cfYU2pdCDUr+8PSpqVQH45cyGXtnH0KcTKXZChHeqW3yCdz2yvjSv3dER9ldKvWinU pRybFcIIIcJ1x1gVcDse8TFcN3hO2F6bLrYBQvCAo6D7TOleOtwQ53V5oGlr7EKEM0 kK5eXgT7ULE0fn5gQuNToj51Fn2ORWLRIoFLkaNQ4ZmXOovexDaF5bhF8n25xErblJ uVZYrvqMo9ETwQcBDzyZjuDO+zGRShjt5/qw3N8oqLjMr8+LEV7EHw19Fgm3n0obgH 2R7eQdFuYIeTQ== From: Arnd Bergmann To: David Hildenbrand , Gregory Price , Andrew Morton , Arnd Bergmann Cc: larips , stable@vger.kernel.org, Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , "Lorenzo Stoakes (ARM)" , Brendan Jackman , Johannes Weiner , Jackie Liu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm/mempolicy: fix copy_nodes_to_user() for odd inputs Date: Tue, 17 Nov 2026 15:25:53 +0100 Message-ID: <20261117142628.3931151-1-arnd@kernel.org> X-Mailer: git-send-email 2.53.0 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 7DD141C0005 X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: 3jajortfugpmkfnoh1isq7tnkhmjmih8 X-HE-Tag: 1794925596-166012 X-HE-Meta: U2FsdGVkX19E7rFdQRvbrYw65bwsgoaVZoHGHMGtMJTK6oEvgff8DRv0iEbw02vdQL8UCOS391dhHnvfAx4pZ+BY7Oh/elIYP+FtAn8nqjQ8GARV+iOdzohUGf4QQQLT7m1sA0c7zT3YWBRiYZNIkwWRVXlbvvsVfVF4vE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lPgadA6TV40KAe1b8nb1ix9t+zn9UrjxGotT/Jr5adLufjdeaZ/K6vMv8TCZ+VeCKtWNUckqiAW6NkP9FBPDBTAyXjBaxKHiQzzRmxfWRXy9t0u/y54HOzrzymQzU1x3g/y8e/NHDWIUszh/+b4qQwxrujk0Zv6am+/njxfwIQskDPOIl4h7f6u+uXBG4MpBE0PA== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Arnd Bergmann The ALIGN(maxnode-1, 64) check has an off-by-one error that leads to rounding down too much, and ends up not copying the last word when userspace asks for one bit over a multiple of 64. For compat syscalls, the effect is worse, as the actual calculation for the size does include the extra word, but the check for the input size does not take it into account and can leak a word of stack data. In addition, the size calculation rounds up to multiples of 64 bits instead of multiples of word size as documented in the man page and expected by numactl. Fortunately numactl always passes multiples of 64 here, so none of this affects userspace we care about. Finally, the 'nbytes' calculation for compat mode makes no sense since that is the size of the kernel's internal representation, not the userspace side. Fix the 'copy' calculation to address all of the above. Note that the off-by-one error has been in there since the beginning, but commit e130242dc351 ("mm: simplify compat numa syscalls") is where we starting copying the wrong data. Reported-by: larips Reported-by: Gregory Price (Meta) Fixes: e130242dc351 ("mm: simplify compat numa syscalls") Link: https://lore.kernel.org/all/20261117101910.1820066-1-gourry@gourry.net/ Cc: stable@vger.kernel.org Signed-off-by: Arnd Bergmann --- Please double-check the calculation and retest this with the original reproducer. The off-by-one error does explain all of this, I'm still worried about getting this version wrong again as everyone including myself seems to have missed this in the past 25 years and there may still be additional subtle issues around it. Apparently we had many instances of the same off-by-one in this file but all (most?) others got fixed inadvertently by other changes in the past. --- mm/mempolicy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 6d662a9a8c74..f318e1155770 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1749,12 +1749,12 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, nodemask_t *nodes) { - unsigned long copy = ALIGN(maxnode-1, 64) / 8; + unsigned long copy = ALIGN(maxnode, BITS_PER_LONG) / 8; unsigned int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long); bool compat = in_compat_syscall(); if (compat) - nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t); + copy = ALIGN(maxnode, BITS_PER_COMPAT_LONG) / 8; if (copy > nbytes) { if (copy > PAGE_SIZE) -- 2.53.0