From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72C822652BD for ; Wed, 10 Dec 2025 22:46:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765406792; cv=none; b=JloQzDwuVZZdPlhNHupeiC24cH5luNEPYARGbGi/4jMCHwfal/omsLmuNmgx9UdkFvXAW/QE2Rnzdc5Qm6TDdOHlcJu0J7dOM6M+wRwC0Iq0wRBqiHfnVhYMu8gv9mNwD2aPk04IfMZpCU65euewJX+d/M5RBu+m1Vr5OPkLLR4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765406792; c=relaxed/simple; bh=Fxnj/rAIkI6I++vejdH2wjowK+XQNikPUDfMZas4d6w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=KmA9gv78PSrL0rgndi2QD06H6CDtG4hvwk8ayOtErdWLiFMz+vusil8o9WkcoI9ifmfvnUY8lKj4s+1BM1ot4UhLXGo/lg+qCVuJplcmjNDPsRZyoi3nOccY+ys1qK0Wi0d+8NGAOvDRpdMyQt717RKkjPqVY/32VDVImoCTZi8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TlR9UQpf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TlR9UQpf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25142C4CEF1; Wed, 10 Dec 2025 22:46:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1765406792; bh=Fxnj/rAIkI6I++vejdH2wjowK+XQNikPUDfMZas4d6w=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=TlR9UQpfL9LMzzpy3fDxNkAjdfGKRCUYtqdpteEirsFfXr64EbgHfwGbwACvH64h9 CDSgKKlGxilM5VF1noV5Bgrx/+vePd5GRelSM6Lel8nqyUvQb0kmVMLuPaawn4PRDR VoMvm54tv/MGduxSkf4D6JSNwl6N2KK6xvV+Pguixkj/QQcF2ydJxkoKevk9Hx0DX6 EBMg9mapth1/ajySOnl141dLt2kg26C+NY4ufAUZ9WhSJxlbFMYgBke4ZXwCUdgaY4 KNpx7ur3diSmsvdB3id6B6TESC77qU4/5T7gFTzS9AcpZIs7D6vw2HN/xctIFozciy 8MK/p+tJmhmPw== Date: Wed, 10 Dec 2025 23:46:27 +0100 From: Alejandro Colomar To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Alejandro Colomar , Kees Cook , Christopher Bazley , Rasmus Villemoes , Marco Elver , Michal Hocko , Linus Torvalds , Al Viro , Alexander Potapenko , Dmitry Vyukov , Jann Horn , Andrew Morton , "Maciej W. Rozycki" Subject: [PATCH v5 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Message-ID: X-Mailer: git-send-email 2.51.0 References: 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=utf-8 Content-Disposition: inline In-Reply-To: Add ARRAY_END(), and use it to fix off-by-one bugs ARRAY_END() is a macro to calculate a pointer to one past the last element of an array argument. This is a very common pointer, which is used to iterate over all elements of an array: for (T *p = a; p < ARRAY_END(a); p++) ... Of course, this pointer should never be dereferenced. A pointer one past the last element of an array should not be dereferenced; it's perfectly fine to hold such a pointer --and a good thing to do--, but the only thing it should be used for is comparing it with other pointers derived from the same array. Due to how special these pointers are, it would be good to use consistent naming. It's common to name such a pointer 'end' --in fact, we have many such cases in the kernel--. C++ even standardized this name with std::end(). Let's try naming such pointers 'end', and try also avoid using 'end' for pointers that are not the result of ARRAY_END(). It has been incorrectly suggested that these pointers are dangerous, and that they should never be used, suggesting to use something like #define ARRAY_LAST(a) ((a) + ARRAY_SIZE(a) - 1) for (T *p = a; p <= ARRAY_LAST(a); p++) ... This is bogus, as it doesn't scale down to arrays of 0 elements. In the case of an array of 0 elements, ARRAY_LAST() would underflow the pointer, which not only it can't be dereferenced, it can't even be held (it produces Undefined Behavior). That would be a footgun. Such arrays don't exist per the ISO C standard; however, GCC supports them as an extension (with partial support, though; GCC has a few bugs which need to be fixed). This patch set fixes a few places where it was intended to use the array end (that is, one past the last element), but accidentally a pointer to the last element was used instead, thus wasting one byte. It also replaces other places where the array end was correctly calculated with ARRAY_SIZE(), by using the simpler ARRAY_END(). Also, there was one drivers/ file that already defined this macro. We remove that definition, to not conflict with this one. Cc: Kees Cook Cc: Christopher Bazley Cc: Rasmus Villemoes Cc: Marco Elver Cc: Michal Hocko Cc: Linus Torvalds Cc: Al Viro Cc: Alexander Potapenko Cc: Dmitry Vyukov Cc: Jann Horn Cc: Andrew Morton Cc: "Maciej W. Rozycki" In-Reply-To: --- Hi, I've rebased v5 on top of v6.18. The patches are identical (see range-diff below). I've tested them again, to make sure they're fine, although I'm not sure I have enabled all configs to build all of this code. Please make sure everything builds, or let me know if (and how) I should test anything else. Some details of the .config I used for testing: $ cat .config | grep KUNIT_TEST | grep -v -e ^# -e CRYPTO CONFIG_KCSAN_KUNIT_TEST=y CONFIG_KFENCE_KUNIT_TEST=y >From v6.18: $ sudo dmesg | grep -inC2 kfence | sed 's/^...//' > tmp/kfence_master; >From this branch: $ sudo dmesg | grep -inC2 kfence | sed 's/^...//' > tmp/kfence_endof; Results: $ diff -U0 \ <(cat tmp/kfence_master \ | sed 's/0x[0-9a-f]*/0x????/g' \ | sed 's/[[:digit:]]\.[[:digit:]]\+/?.?/g' \ | sed 's/#[[:digit:]]\+/#???/g' \ | sed 's/././') \ <(cat tmp/kfence_endof \ | sed 's/0x[0-9a-f]*/0x????/g' \ | sed 's/[[:digit:]]\.[[:digit:]]\+/?.?/g' \ | sed 's/#[[:digit:]]\+/#???/g' \ | sed 's/././'); --- /dev/fd/63 2025-12-10 23:45:12.512438818 +0100 +++ /dev/fd/62 2025-12-10 23:45:12.516438854 +0100 @@ -17 +17 @@ -.-[ 68?.?] allocated by task 2516 on cpu 21 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2515 on cpu 8 at 68?.?s (?.?s ago): @@ -31 +31 @@ -.-[ 68?.?] allocated by task 2516 on cpu 0 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2515 on cpu 1 at 68?.?s (?.?s ago): @@ -45 +45 @@ -.-[ 68?.?] allocated by task 2518 on cpu 10 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2517 on cpu 4 at 68?.?s (?.?s ago): @@ -59 +59 @@ -.-[ 68?.?] allocated by task 2518 on cpu 2 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2517 on cpu 4 at 68?.?s (?.?s ago): @@ -73 +73 @@ -.-[ 68?.?] allocated by task 2520 on cpu 10 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2519 on cpu 14 at 68?.?s (?.?s ago): @@ -87 +87 @@ -.-[ 68?.?] allocated by task 2522 on cpu 10 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2521 on cpu 14 at 68?.?s (?.?s ago): @@ -101 +101 @@ -.-[ 68?.?] allocated by task 2524 on cpu 10 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2523 on cpu 14 at 68?.?s (?.?s ago): @@ -115 +115 @@ -.-[ 68?.?] allocated by task 2526 on cpu 15 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2525 on cpu 18 at 68?.?s (?.?s ago): @@ -129 +129 @@ -.-[ 68?.?] allocated by task 2532 on cpu 8 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2531 on cpu 15 at 68?.?s (?.?s ago): @@ -143 +143 @@ -.-[ 68?.?] allocated by task 2534 on cpu 8 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2533 on cpu 15 at 68?.?s (?.?s ago): @@ -157 +157 @@ -.-[ 68?.?] allocated by task 2536 on cpu 8 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2535 on cpu 15 at 68?.?s (?.?s ago): @@ -171 +171 @@ -.-[ 68?.?] allocated by task 2538 on cpu 14 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2537 on cpu 15 at 68?.?s (?.?s ago): @@ -185 +185 @@ -.-[ 68?.?] allocated by task 2540 on cpu 8 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2539 on cpu 3 at 68?.?s (?.?s ago): @@ -199 +199 @@ -.-[ 68?.?] allocated by task 2540 on cpu 8 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2539 on cpu 3 at 68?.?s (?.?s ago): @@ -213 +213 @@ -.-[ 68?.?] allocated by task 2542 on cpu 8 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2541 on cpu 15 at 68?.?s (?.?s ago): @@ -227 +227 @@ -.-[ 68?.?] allocated by task 2542 on cpu 1 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2541 on cpu 1 at 68?.?s (?.?s ago): @@ -241 +241 @@ -.-[ 68?.?] allocated by task 2552 on cpu 23 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2551 on cpu 11 at 68?.?s (?.?s ago): @@ -255 +255 @@ -.-[ 68?.?] allocated by task 2554 on cpu 1 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2553 on cpu 5 at 68?.?s (?.?s ago): @@ -275 +275 @@ -.-[ 68?.?] allocated by task 2564 on cpu 9 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2563 on cpu 13 at 68?.?s (?.?s ago): @@ -289 +289 @@ -.-[ 68?.?] allocated by task 2566 on cpu 9 at 68?.?s (?.?s ago): +.-[ 68?.?] allocated by task 2565 on cpu 15 at 68?.?s (?.?s ago): Have a lovely night! Alex Alejandro Colomar (4): array_size.h: Add ARRAY_END() mm: Fix benign off-by-one bugs kernel: Fix off-by-one benign bugs mm: Use ARRAY_END() instead of open-coding it drivers/block/floppy.c | 2 -- include/linux/array_size.h | 6 ++++++ kernel/kcsan/kcsan_test.c | 4 ++-- mm/kfence/kfence_test.c | 4 ++-- mm/kmemleak.c | 2 +- mm/kmsan/kmsan_test.c | 2 +- mm/memcontrol-v1.c | 4 ++-- 7 files changed, 14 insertions(+), 10 deletions(-) Range-diff against v4: 1: 9f87d6208a6c = 1: 5973cfb67419 array_size.h: Add ARRAY_END() 2: ac55d92551e4 = 2: 9c38dd009c17 mm: Fix benign off-by-one bugs 3: ca8dec7f5bc9 = 3: b4a945a4d40b kernel: Fix off-by-one benign bugs 4: 980c8fe8a6de = 4: e7bde864b039 mm: Use ARRAY_END() instead of open-coding it -- 2.51.0