From nobody Thu Oct 2 00:46:20 2025 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 5C7182367C5 for ; Thu, 25 Sep 2025 13:20:44 +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=1758806444; cv=none; b=cSMm8aLvyuhgT9t4Wdqj3aPp/SA3b/ZNq7Mt29KGojRpjm+azBheHR6Rh836t3mAx3cW93Y8PMg+anp2QssracETm2uWvTFdRleqGrYy40dzu9v5YWDIT7+Kkr5iPV25ekdljki8IoL6NPNk8eFpYCR2xXrKA6iaat5BfVixpZU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758806444; c=relaxed/simple; bh=x8LCJhegC97O6xERC5LDearaauqtJIxIIcuU8JYgDQ8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=aVAjgKsP13Q9Rch2zPXbbfE9YrrYsTq83WFk+j8DtS0zZiQqtxvBsuJMzWgBypxaT6OtOcotehC3WaUsfk7Lvba6C4GcdnT44x7z+c/0dRqQUStXqWcgt+2MMynwXX3O0SlZXi/Kn2dcWXbIxRhgxr4Id+YDa7ZpOvM7r5O2NPs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bxeNLm0D; 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="bxeNLm0D" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BA7BFC4CEF0; Thu, 25 Sep 2025 13:20:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758806444; bh=x8LCJhegC97O6xERC5LDearaauqtJIxIIcuU8JYgDQ8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bxeNLm0DZWNdN7BMMB3CuQoBsPHFjJYTQ53k4EOVbDH5FO/5+WOseIpE3pWzT13LJ VTJ5tt7YEN6pasYHgBhtU3mg6z0dRUv4KBPhaB8v58Jng2au8Jr+xOhyOx5nRSurNV r8ooLZd5lc27XKJ2bdcLJudFZdipZgBTb0nlwqpYwNrzPObtolPLiJPRRSEIMcSzWX eiRd6p6rM3tNuEpUpcD0NbwgRtBqFuff215OHpEtBgK9D5vAtDw/rWK8xehkEjZm9P iqs1OOawbFZveOXGIFboTNMolqpy3CQLmH6Td8EWLz/BrxRFZUe8/T5O7rY/DWdBHU zbNu4IUSNsIYw== Date: Thu, 25 Sep 2025 15:20:37 +0200 From: Alejandro Colomar To: linux-kernel@vger.kernel.org Cc: Alejandro Colomar , Marco Elver , Kees Cook , Christopher Bazley , Alexander Potapenko , Dmitry Vyukov , Jann Horn , Andrew Morton , Linus Torvalds , Rasmus Villemoes , Michal Hocko , Al Viro Subject: [PATCH v1 1/3] array_size.h: Add ENDOF() Message-ID: <0b3d2fde03a0b11643d5a9bb56d7a903c58320cf.1758806023.git.alx@kernel.org> 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-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This macro is useful to calculate the second argument to sprintf_trunc_end(), avoiding off-by-one bugs. Cc: Kees Cook Cc: Christopher Bazley Cc: Rasmus Villemoes Cc: Marco Elver Cc: Michal Hocko Cc: Linus Torvalds Cc: Al Viro Signed-off-by: Alejandro Colomar Message-ID: <37b1088dbd01a21d2f9d460aa510726119b3bcb0.1752193588.git.alx@ke= rnel.org> --- include/linux/array_size.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/linux/array_size.h b/include/linux/array_size.h index 06d7d83196ca..781bdb70d939 100644 --- a/include/linux/array_size.h +++ b/include/linux/array_size.h @@ -10,4 +10,10 @@ */ #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(= arr)) =20 +/** + * ENDOF - get a pointer to one past the last element in array @a + * @a: array + */ +#define ENDOF(a) (a + ARRAY_SIZE(a)) + #endif /* _LINUX_ARRAY_SIZE_H */ --=20 2.51.0 From nobody Thu Oct 2 00:46:20 2025 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 B977D265CA8 for ; Thu, 25 Sep 2025 13:20:51 +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=1758806451; cv=none; b=hvar5J+c1hpAEaxqjSenlmkD4UC27tKhGdZGL7q0AONzjF4wR7Mxd1iD4sK1VkHcpPY2wliMusMBILxe8oUYgEriDd0ciF16kNqIpAFiWv2S9Z1KA7hJcNvGZC0gJXFBH74GLXCWq27y+fMsRym0rYQgRsb+3o0lfq0cYgqvUJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758806451; c=relaxed/simple; bh=4d3Ij8ruJRTlJEB94A/a0pL/HtNqz2IJeGpX2wtPsV8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QH+9lCss9D6dlzJkt4DLEVL5J9JToS/GU+9cMWVA/YgBEO7muIkeTUY4YdebaVipWbJ4XzjkHGNhK7BwRgRW31F3/lvsvV9mRcujjiw4hyZUbO/VxKoAtQCIxMHZluzPnwuQZGNmNpvv4ywTqOwOPenorj5hjspig5GvXSaKAi0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GVkXhoEX; 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="GVkXhoEX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2BD4BC113D0; Thu, 25 Sep 2025 13:20:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758806451; bh=4d3Ij8ruJRTlJEB94A/a0pL/HtNqz2IJeGpX2wtPsV8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=GVkXhoEXilKkOjTEb8ioW/Yzsw8YCM8wbejZfFApnpoimIs16eqUtGSN1SPSxQMN2 6QeS0zS9PRkwfA+3frQKjXZ5qAjKjmi+RaEj5T3uaUvNGeGT+7kpuOMNTmu239E5A1 6fNzhqonp3msszrEadp78MI4LynybCvsP8nEANCQFLLjifKN3bHiiyspA79z4jek5u ieTFjiiEjhNEBcpJ7iEpxu0wAB9/lDuyFa6+84ACWPdMEZmPI/brRlbHwzp/AmHGfm kPRToyy+U9/gm45DkW38Xq1gjbFSIUj79ZeMtXemYqtFODYikwO//nHAF1S6uTwsiy KFn6ms/O1gOSw== Date: Thu, 25 Sep 2025 15:20:44 +0200 From: Alejandro Colomar To: linux-kernel@vger.kernel.org Cc: Alejandro Colomar , Marco Elver , Kees Cook , Christopher Bazley , Alexander Potapenko , Dmitry Vyukov , Jann Horn , Andrew Morton , Linus Torvalds , Rasmus Villemoes , Michal Hocko , Al Viro Subject: [PATCH v1 2/3] mm: Fix benign off-by-one bugs Message-ID: <87445e701574058b142e036c3b8a0f505086ab64.1758806023.git.alx@kernel.org> 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-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We were wasting a byte due to an off-by-one bug. s[c]nprintf() doesn't write more than $2 bytes including the null byte, so trying to pass 'size-1' there is wasting one byte. Acked-by: Marco Elver Cc: Kees Cook Cc: Christopher Bazley Cc: Alexander Potapenko Cc: Dmitry Vyukov Cc: Alexander Potapenko Cc: Jann Horn Cc: Andrew Morton Cc: Linus Torvalds Cc: Rasmus Villemoes Cc: Marco Elver Cc: Michal Hocko Cc: Al Viro Signed-off-by: Alejandro Colomar Message-ID: <515445ae064d4b8599899bf0d8b480dadd2ff843.1752182685.git.alx@ke= rnel.org> --- mm/kfence/kfence_test.c | 4 ++-- mm/kmsan/kmsan_test.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c index 00034e37bc9f..1ad81e6b27ea 100644 --- a/mm/kfence/kfence_test.c +++ b/mm/kfence/kfence_test.c @@ -110,7 +110,7 @@ static bool report_matches(const struct expect_report *= r) =20 /* Title */ cur =3D expect[0]; - end =3D &expect[0][sizeof(expect[0]) - 1]; + end =3D ENDOF(expect[0]); switch (r->type) { case KFENCE_ERROR_OOB: cur +=3D scnprintf(cur, end - cur, "BUG: KFENCE: out-of-bounds %s", @@ -140,7 +140,7 @@ static bool report_matches(const struct expect_report *= r) =20 /* Access information */ cur =3D expect[1]; - end =3D &expect[1][sizeof(expect[1]) - 1]; + end =3D ENDOF(expect[1]); =20 switch (r->type) { case KFENCE_ERROR_OOB: diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c index c6c5b2bbede0..758405d8b7a7 100644 --- a/mm/kmsan/kmsan_test.c +++ b/mm/kmsan/kmsan_test.c @@ -105,7 +105,7 @@ static bool report_matches(const struct expect_report *= r) =20 /* Title */ cur =3D expected_header; - end =3D &expected_header[sizeof(expected_header) - 1]; + end =3D ENDOF(expected_header); =20 cur +=3D scnprintf(cur, end - cur, "BUG: KMSAN: %s", r->error_type); =20 --=20 2.51.0 From nobody Thu Oct 2 00:46:20 2025 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 EDF4023909C for ; Thu, 25 Sep 2025 13:20:58 +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=1758806459; cv=none; b=hpnJTskbrvoylC0XieHmX6+608Z35hbNqdaQqs6cJvwsSjVghSo/v6hAfQHWbYmr5QFi+YWoZcLSTU4b2Ce9dzA70vpN6WAe61MxPkCRtYXpAQWBm8eH6r90fGx9smaWlxzJf6Fusz6h1NdnkeMQ5XK1172QkAhVkUthKkFdOdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758806459; c=relaxed/simple; bh=YDDEpU1bcy05koezpIh2vlp6eVzNiz9AaW8llrtCap0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mWRPSL+8IbyTx0IuSWsDm88z+ImlOAzHKtr6Ml7ArBcUysQcMi6N0z9ej8ZPlFZiAHn6ASk2OQ7SKkXYLMdbAhNorKQYHvh8Me25QKM06vmdps2JfblJgNIfs4irHrItcUuxLJSnkdBLFO49APs4f+Hevetd3SbSunSG6Mwk/Gw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=W4nLUWL2; 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="W4nLUWL2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36CFAC4CEF0; Thu, 25 Sep 2025 13:20:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758806458; bh=YDDEpU1bcy05koezpIh2vlp6eVzNiz9AaW8llrtCap0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=W4nLUWL2A+zzUCo6d9H59e2guNgttVusX1whLsHfzD0901R3uhhgUZVNCWE9J98jk nzCbIalJ5wuUQrgeE8HSwYwgCHasKLd9rbUugh6SSO2+5AktQnwQGOGodSSDfjfBud wgWhlJBNFlKPFJhk7QreZKRCFPX54oyvRBYLPDEtG02aa2piAQlfItygjqw2fKtZh7 q17FiZ1Xq4m44sHyQCss2j6JelDA4KSnl10SjRk9xoGlkooJdLXf/nocOMGbtCKBzd n46TUrJsg3qVm8OK+rsORt5QrF/EbOo3GJPaKpAPjjJj3RrSA6oOqwUf8j/IO8x/GA m0zFjsj47Xvfg== Date: Thu, 25 Sep 2025 15:20:52 +0200 From: Alejandro Colomar To: linux-kernel@vger.kernel.org Cc: Alejandro Colomar , Marco Elver , Kees Cook , Christopher Bazley , Alexander Potapenko , Dmitry Vyukov , Jann Horn , Andrew Morton , Linus Torvalds , Rasmus Villemoes , Michal Hocko , Al Viro Subject: [PATCH v1 3/3] kernel: Fix off-by-one benign 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-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We were wasting a byte due to an off-by-one bug. s[c]nprintf() doesn't write more than $2 bytes including the null byte, so trying to pass 'size-1' there is wasting one byte. This is essentially the same as the previous commit, in a different file. Cc: Marco Elver Cc: Kees Cook Cc: Christopher Bazley Cc: Alexander Potapenko Cc: Dmitry Vyukov Cc: Alexander Potapenko Cc: Jann Horn Cc: Andrew Morton Cc: Linus Torvalds Cc: Rasmus Villemoes Cc: Marco Elver Cc: Michal Hocko Cc: Al Viro Signed-off-by: Alejandro Colomar --- kernel/kcsan/kcsan_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c index c2871180edcc..621a60a86b39 100644 --- a/kernel/kcsan/kcsan_test.c +++ b/kernel/kcsan/kcsan_test.c @@ -176,7 +176,7 @@ static bool __report_matches(const struct expect_report= *r) =20 /* Title */ cur =3D expect[0]; - end =3D &expect[0][sizeof(expect[0]) - 1]; + end =3D ENDOF(expect[0]); cur +=3D scnprintf(cur, end - cur, "BUG: KCSAN: %s in ", is_assert ? "assert: race" : "data-race"); if (r->access[1].fn) { @@ -200,7 +200,7 @@ static bool __report_matches(const struct expect_report= *r) =20 /* Access 1 */ cur =3D expect[1]; - end =3D &expect[1][sizeof(expect[1]) - 1]; + end =3D ENDOF(expect[1]); if (!r->access[1].fn) cur +=3D scnprintf(cur, end - cur, "race at unknown origin, with "); =20 --=20 2.51.0