From nobody Fri Sep 25 04:40:51 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (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 CACEF4BD35E; Wed, 16 Sep 2026 17:38:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580320; cv=none; b=rW5PuxBAv4hm4hjvEbB2+CVkesg0DJtf9KV6yPtYLCx47rBTR9GU2yC0rPCU3VJ+bagUDzW2GcqJs+9+eqUzGk2gwhSZaq/iMf4PiftyptYZ8pFiC5Em3v14OV34Cl0F0VnwfsU915pXLpPU/bEO/lT16LFWfLbVCyMfXE0KBIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580320; c=relaxed/simple; bh=Tmo6w2TfPA14tRt2/cUWegDWuBIQPArxHc2/r1qjpT4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IB3zKITIIHi4X5nX9VOzKyK11nOjY3zDDJSSHwS7ULs35KIbwsjfi8MaivpNilr4du4rSdu83jQLR80bue/MvkskJKkkOyQw1CqGFBKcgh0GF4w13ymj2zu97d+iofjq9YyzMkk1QwalGXokeZJgM2aaCQdsHpD/tpRJLhfAYAQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de X-PTX-Original-Recipient: jre@pengutronix.de X-PTX-Original-Recipient: kernel@pengutronix.de X-PTX-Original-Recipient: akpm@linux-foundation.org X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-PTX-Original-Recipient: linux-hardening@vger.kernel.org X-PTX-Original-Recipient: andy@kernel.org X-PTX-Original-Recipient: kees@kernel.org Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 212A3200A7A; Wed, 16 Sep 2026 19:38:15 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x6taJ-001Go9-0A; Wed, 16 Sep 2026 19:38:15 +0200 From: Jonas Rebmann Date: Wed, 16 Sep 2026 19:38:06 +0200 Subject: [PATCH 1/5] lib/tests: string_helpers: check null terminator too 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-Transfer-Encoding: quoted-printable Message-Id: <20260916-string_unescape-v1-1-7f8bd986fa33@pengutronix.de> References: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> In-Reply-To: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> To: Kees Cook , Andy Shevchenko , Andrew Morton Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1366; i=jre@pengutronix.de; h=from:subject:message-id; bh=Tmo6w2TfPA14tRt2/cUWegDWuBIQPArxHc2/r1qjpT4=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshadYVlCr/hJynlkkRx3TDbxfqczgdsm8ssPnPd/Spwa crjO9PPdJSyMIhxMciKKbLEqskpCBn7XzertIuFmcPKBDKEgYtTACYirMHwz8qm8OhHj+P2n6Y+ eykn9bzukk3eveLm2MlrIo6vzM7ZnMTI0NkSk1+kUX1+5vzX76KcumrmacWXzGW74GG/XGPhvdu h/AA= X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 string_unescape() returns the number of character written to dst, not counting the null terminator which is always written. Ensure string_unescape has included the null terminator by adding a separate check. While at it, improve output for failed tests by showing the memory dump even if the length differs. Signed-off-by: Jonas Rebmann --- lib/tests/string_helpers_kunit.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_ku= nit.c index 9fbe91079c7e..9bc3acffaf2f 100644 --- a/lib/tests/string_helpers_kunit.c +++ b/lib/tests/string_helpers_kunit.c @@ -22,7 +22,7 @@ static void test_string_check_buf(struct kunit *test, char *out_real, size_t q_real, char *out_test, size_t q_test) { - KUNIT_ASSERT_EQ_MSG(test, q_real, q_test, "name:%s", name); + KUNIT_EXPECT_EQ_MSG(test, q_real, q_test, "name:%s", name); KUNIT_EXPECT_MEMEQ_MSG(test, out_test, out_real, q_test, "name:%s", name); } @@ -103,6 +103,7 @@ static void test_string_unescape(struct kunit *test, =20 test_string_check_buf(test, name, flags, in, p - 1, out_real, q_real, out_test, q_test); + KUNIT_EXPECT_EQ_MSG(test, out_real[q_real], '\0', "name:%s", name); } =20 struct test_string_1 { --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Fri Sep 25 04:40:51 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (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 CAE234C652F; Wed, 16 Sep 2026 17:38:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580318; cv=none; b=KzzJUONWyxRzacBW7BWKq5tNlpSgPBFieX1wSA7QJDkVnbnNUWQAFip9RdGZ/rOGoQL508KnP5wFQH4v7lSt+0cl/ezEU2hxKGWGyeicaGUT72e6/oanMeskzS4QpUhp8Pd/Y8oRebvEJcBH8084t5lUvIHy7CN163LDz2AOTME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580318; c=relaxed/simple; bh=ECun2AALw4gQB+9hdl9j+YzzMYCXapHOZtRm+GpHsDA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nMDcnKsc3Y8EHepIHj6cJpEUdueonmuZBPS39NOSBmZEG40AbrNYMYMg1a/m/w8onjThiklh3rR6h2YhNBCGm2W3hIUkh/j5xyHvjpxKzaRGGEXn046664SdlxbRpsgoHPrP9/B8sMshUuwZIgkJByJpSVYYtwF1QJX1Nv3SE2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de X-PTX-Original-Recipient: jre@pengutronix.de X-PTX-Original-Recipient: kernel@pengutronix.de X-PTX-Original-Recipient: akpm@linux-foundation.org X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-PTX-Original-Recipient: linux-hardening@vger.kernel.org X-PTX-Original-Recipient: andy@kernel.org X-PTX-Original-Recipient: kees@kernel.org Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 317D32028FE; Wed, 16 Sep 2026 19:38:15 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x6taJ-001Go9-0J; Wed, 16 Sep 2026 19:38:15 +0200 From: Jonas Rebmann Date: Wed, 16 Sep 2026 19:38:07 +0200 Subject: [PATCH 2/5] lib/tests: string_helpers: drop unused parameters 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-Transfer-Encoding: quoted-printable Message-Id: <20260916-string_unescape-v1-2-7f8bd986fa33@pengutronix.de> References: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> In-Reply-To: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> To: Kees Cook , Andy Shevchenko , Andrew Morton Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1538; i=jre@pengutronix.de; h=from:subject:message-id; bh=ECun2AALw4gQB+9hdl9j+YzzMYCXapHOZtRm+GpHsDA=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshadYWV57nK1ZIPb5Xi5f8teun3PfO55wl1RfEVEy6X3 567Kv0ve0cpC4MYF4OsmCJLrJqcgpCx/3WzSrtYmDmsTCBDGLg4BWAiaTWMDNOX9UzUD5qRdpb1 eGNR62xtJfnWuX6qTdqLy3bMvFT1k5HhNzvrXabcozkdrlP8zGO2fzH8HmbbfXmRbYL0kdJjERY WnAA= X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 Drop the unused parameters from test_string_check_buf() to improve readability. Signed-off-by: Jonas Rebmann --- lib/tests/string_helpers_kunit.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_ku= nit.c index 9bc3acffaf2f..1ed652f762d1 100644 --- a/lib/tests/string_helpers_kunit.c +++ b/lib/tests/string_helpers_kunit.c @@ -18,7 +18,6 @@ =20 static void test_string_check_buf(struct kunit *test, const char *name, unsigned int flags, - char *in, size_t p, char *out_real, size_t q_real, char *out_test, size_t q_test) { @@ -101,8 +100,7 @@ static void test_string_unescape(struct kunit *test, q_real =3D string_unescape(in, out_real, q_real, flags); } =20 - test_string_check_buf(test, name, flags, in, p - 1, out_real, q_real, - out_test, q_test); + test_string_check_buf(test, name, flags, out_real, q_real, out_test, q_te= st); KUNIT_EXPECT_EQ_MSG(test, out_real[q_real], '\0', "name:%s", name); } =20 @@ -457,8 +455,7 @@ static void test_string_escape(struct kunit *test, cons= t char *name, =20 q_real =3D string_escape_mem(in, p, out_real, out_size, flags, esc); =20 - test_string_check_buf(test, name, flags, in, p, out_real, q_real, out_tes= t, - q_test); + test_string_check_buf(test, name, flags, out_real, q_real, out_test, q_te= st); =20 test_string_escape_overflow(test, in, p, flags, esc, q_test, name); } --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Fri Sep 25 04:40:51 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (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 1CE884D7D35; Wed, 16 Sep 2026 17:38:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580320; cv=none; b=SDmT2f4JSFhJa6NC4n1tMuCvcNn99crZlOfr+ylXgS1n2P8v8RUeqVu8KYPLSreyKmISqcfpKKA0+Q9yEPusKr0jZp7lJa7Z3KlnnR1kXEqHqu1nT4/FEuEHveRAA9gE/Qd6QOp/xGLi5Y3nUQBucP77dKZ3lHLjEFnTf/fUoeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580320; c=relaxed/simple; bh=rVt8YGwnDLRp84WV00lbEvbeEVulHf6uFDxTB/Nn2/w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gshCjq9xvUotqlERhK51JAoIoi+a+gsVvLNqjo17MqUmYlFOaxuNRdA/DFg217xyKeDGnu77n9Ms8XRl6OzE2dUxt8OBQPoV4bG45SJF6LFMdbNfb7LDogqhj/DUEGZCKWWlalSVH0OI6MhAIm03hDuQ7gjD8uQn1IpoLmuNB9o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de X-PTX-Original-Recipient: jre@pengutronix.de X-PTX-Original-Recipient: kernel@pengutronix.de X-PTX-Original-Recipient: akpm@linux-foundation.org X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-PTX-Original-Recipient: linux-hardening@vger.kernel.org X-PTX-Original-Recipient: andy@kernel.org X-PTX-Original-Recipient: kees@kernel.org Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 406AD202B14; Wed, 16 Sep 2026 19:38:15 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x6taJ-001Go9-0T; Wed, 16 Sep 2026 19:38:15 +0200 From: Jonas Rebmann Date: Wed, 16 Sep 2026 19:38:08 +0200 Subject: [PATCH 3/5] lib/tests: string_helpers: introduce test_string_unescape_one 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-Transfer-Encoding: quoted-printable Message-Id: <20260916-string_unescape-v1-3-7f8bd986fa33@pengutronix.de> References: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> In-Reply-To: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> To: Kees Cook , Andy Shevchenko , Andrew Morton Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2968; i=jre@pengutronix.de; h=from:subject:message-id; bh=rVt8YGwnDLRp84WV00lbEvbeEVulHf6uFDxTB/Nn2/w=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshadYX1Zc2KMLvXSZ5XnPQPvF2wbe4BJtNy62cGnzheh BZfWHsiq6OUhUGMi0FWTJElVk1OQcjY/7pZpV0szBxWJpAhDFycAjCRqp+MDN+exURdn2PzJnih 2Mprt3xWRfvJyDlZyU/dPlvRI239MiWGf6aPT5wS96pVTWhs+fvH9D7z3ZwD9784TPVey6GwQ+2 JKg8A X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 The existing test_string_unescape() function follows a complex procedure where it, given a set of UNESCAPE flags, appends multiple test fragments and predicts their unescape result for the chosen set of flags. Rename test_string_unescape() to a more descriptive test_string_unescape_combined In preparation to add simple regression tests, introduce test_string_unescape_one() which asserts on exactly one call to string_unescape. Add some tests for corner cases which already pass. Signed-off-by: Jonas Rebmann --- lib/tests/string_helpers_kunit.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_ku= nit.c index 1ed652f762d1..3c6fa7324965 100644 --- a/lib/tests/string_helpers_kunit.c +++ b/lib/tests/string_helpers_kunit.c @@ -55,9 +55,9 @@ static const struct test_string strings[] =3D { }, }; =20 -static void test_string_unescape(struct kunit *test, - const char *name, unsigned int flags, - bool inplace) +static void test_string_unescape_combined(struct kunit *test, + const char *name, unsigned int flags, + bool inplace) { int q_real =3D 256; char *in =3D kunit_kzalloc(test, q_real, GFP_KERNEL); @@ -596,14 +596,33 @@ static void test_upper_lower(struct kunit *test) } } =20 +static void test_string_unescape_one(struct kunit *test, + const char *name, unsigned int flags, + char *src, size_t len, + char *out_test, size_t q_test) +{ + char *out_real =3D kunit_kzalloc(test, len, GFP_KERNEL); + int q_real; + + q_real =3D string_unescape(src, out_real, len, flags); + test_string_check_buf(test, name, flags, out_real, q_real, out_test, q_te= st); +} + static void test_unescape(struct kunit *test) { unsigned int i; =20 for (i =3D 0; i < UNESCAPE_ALL_MASK + 1; i++) - test_string_unescape(test, "unescape", i, false); - test_string_unescape(test, "unescape inplace", - get_random_u32_below(UNESCAPE_ALL_MASK + 1), true); + test_string_unescape_combined(test, "unescape", i, false); + test_string_unescape_combined(test, "unescape inplace", + get_random_u32_below(UNESCAPE_ALL_MASK + 1), true); + + test_string_unescape_one(test, "simple case", UNESCAPE_HEX | UNESCAPE_SPE= CIAL, "ABC", 6, "ABC", 3); + test_string_unescape_one(test, "single escape", UNESCAPE_HEX | UNESCAPE_S= PECIAL, "A\\x42C", 6, "ABC", 3); + test_string_unescape_one(test, "escape before end", UNESCAPE_HEX, "B\\qX"= , 4, "B\\q", 3); + test_string_unescape_one(test, "escape at end", UNESCAPE_HEX, "a\\qX", 3,= "a\\", 2); + test_string_unescape_one(test, "backslash before escape", UNESCAPE_HEX, "= \\\\x41B", 12, "\\\\x41B", 6); + test_string_unescape_one(test, "backslash escape", UNESCAPE_HEX | UNESCAP= E_SPECIAL, "\\\\x41B", 16, "\\x41B", 5); } =20 static void test_escape(struct kunit *test) --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Fri Sep 25 04:40:51 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (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 4C38949B465; Wed, 16 Sep 2026 17:38:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580322; cv=none; b=hst9gLNquwV7vyKYFCyNsFEHgTURkQxNWd8xoYXVEiWJtTb0Y0fZOXAvnfo/FzyO2x3E7kMbcESWpZZzeziXKoXkcb1jXU/OgYou+q8RC2ZiMRUELkklRWRgJs9Is/Xk1FtwX8MkXMcfREGRzDS4FvLdyFJ2COt6HcXtfQt2Trc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580322; c=relaxed/simple; bh=oOaOFMuRewgFEERst252OeHfIiFurUrlHBR44bK5Gqc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jEGJ9OjtE724ZiEdnYsKTHjis8NUee3BZxLAXDmTYiJm82by7syswCCb6Fynb69qcVn2Q89coIC0Id9PWaStfWw+9isJX0yXtEfCgacLyq8WNMDLSyNAjA6OXy3SDC/i8D8umonjWs3i3QpScG3efR6TrXF9DD/xOUQVQjEbc1Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de X-PTX-Original-Recipient: jre@pengutronix.de X-PTX-Original-Recipient: kernel@pengutronix.de X-PTX-Original-Recipient: akpm@linux-foundation.org X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-PTX-Original-Recipient: linux-hardening@vger.kernel.org X-PTX-Original-Recipient: andy@kernel.org X-PTX-Original-Recipient: kees@kernel.org Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 4B6672059EC; Wed, 16 Sep 2026 19:38:15 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x6taJ-001Go9-0d; Wed, 16 Sep 2026 19:38:15 +0200 From: Jonas Rebmann Date: Wed, 16 Sep 2026 19:38:09 +0200 Subject: [PATCH 4/5] lib/string_helpers: use full destination buffer in string_unescape() 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-Transfer-Encoding: quoted-printable Message-Id: <20260916-string_unescape-v1-4-7f8bd986fa33@pengutronix.de> References: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> In-Reply-To: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> To: Kees Cook , Andy Shevchenko , Andrew Morton Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2338; i=jre@pengutronix.de; h=from:subject:message-id; bh=oOaOFMuRewgFEERst252OeHfIiFurUrlHBR44bK5Gqc=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshadYWtZWeQ18tDj61iKrObq58VvcgL7A26JNrm8/1F4 6uAe/2LOkpZGMS4GGTFFFli1eQUhIz9r5tV2sXCzGFlAhnCwMUpABOJ3sDwz9Qws3DFwZUrNJlP NCkZtK7g3F4c+zoyPN7u0sl40+91dowMNwQW6TOG75icPNnR9G9Xftl2pYXL71nVOrjtVdwmseQ nIwA= X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 Although all of the available sequences expand to exactly one byte, the current implementation decrements the remaining bytes in the destination buffer twice, effectively shortening it by one byte per each unescaped character. The extra decrement is only needed in the one case where a single loop iteration produces two output bytes: when the sequence turns out not to be a valid escape sequence, the previously skipped backslash has to be emitted before the character is copied verbatim. Add a kunit regression-test that unescapes into a barely long enough 3 buffer. Fixes: 16c7fa05829e ("lib/string_helpers: introduce generic string_unescape= ") Signed-off-by: Jonas Rebmann --- lib/string_helpers.c | 2 +- lib/tests/string_helpers_kunit.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/string_helpers.c b/lib/string_helpers.c index 98d6ed0eaab7..cb41ef9d8c5b 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -331,7 +331,6 @@ int string_unescape(char *src, char *dst, size_t size, = unsigned int flags) while (*src && --size) { if (src[0] =3D=3D '\\' && src[1] !=3D '\0' && size > 1) { src++; - size--; =20 if (flags & UNESCAPE_SPACE && unescape_space(&src, &out)) @@ -350,6 +349,7 @@ int string_unescape(char *src, char *dst, size_t size, = unsigned int flags) continue; =20 *out++ =3D '\\'; + size--; } *out++ =3D *src++; } diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_ku= nit.c index 3c6fa7324965..2e02c680cbb2 100644 --- a/lib/tests/string_helpers_kunit.c +++ b/lib/tests/string_helpers_kunit.c @@ -623,6 +623,9 @@ static void test_unescape(struct kunit *test) test_string_unescape_one(test, "escape at end", UNESCAPE_HEX, "a\\qX", 3,= "a\\", 2); test_string_unescape_one(test, "backslash before escape", UNESCAPE_HEX, "= \\\\x41B", 12, "\\\\x41B", 6); test_string_unescape_one(test, "backslash escape", UNESCAPE_HEX | UNESCAP= E_SPECIAL, "\\\\x41B", 16, "\\x41B", 5); + + test_string_unescape_one(test, "short buffer", UNESCAPE_HEX, "\\x41\\x41B= ", 4, "AAB", 3); + test_string_unescape_one(test, "unrecognized escape at end", UNESCAPE_HEX= , "B\\qX", 4, "B\\q", 3); } =20 static void test_escape(struct kunit *test) --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Fri Sep 25 04:40:51 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (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 6FDF14D6C23; Wed, 16 Sep 2026 17:38:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580334; cv=none; b=bJDr7HOc+jZ9ngMzyVwd9Hz3nQ9nY5mxRIqIK4giSIyEYX9juNKTVeDlcQP9JOhF+ddjxyRT2n+r3mG7sXBil67GPiBiC8uJTrSLa4h5Y/oAhL2kpGV+2C10suMAf1QVBgOT07Zh/0EJJEAH7c/crNj1gDdP/a4U+1HK1oxq16c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789580334; c=relaxed/simple; bh=1xBOa/arXWhUJUP6g5m17JkL1URolc5p+IQdAXLKJ/M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JgGSmamELerhU6BDrK7Is5YWg6mCS+zACDjk2s8PdTN6HUlvzfQdWPwxvdxn0cPNm3uB3EOVnd1baocy7Ir8xQNM2qpDFa8nKeny7qDtFikbbvTYzKS/50JALkqEHZgWSl3hADSK+h8Xj8kYcRzLdeykBq/FY/y2xFXTvPCsWdY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de X-PTX-Original-Recipient: jre@pengutronix.de X-PTX-Original-Recipient: kernel@pengutronix.de X-PTX-Original-Recipient: akpm@linux-foundation.org X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-PTX-Original-Recipient: linux-hardening@vger.kernel.org X-PTX-Original-Recipient: andy@kernel.org X-PTX-Original-Recipient: kees@kernel.org Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 53C512059ED; Wed, 16 Sep 2026 19:38:15 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x6taJ-001Go9-0m; Wed, 16 Sep 2026 19:38:15 +0200 From: Jonas Rebmann Date: Wed, 16 Sep 2026 19:38:10 +0200 Subject: [PATCH 5/5] lib/string_helpers: fix counting of remaining bytes in string_unescape() 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-Transfer-Encoding: quoted-printable Message-Id: <20260916-string_unescape-v1-5-7f8bd986fa33@pengutronix.de> References: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> In-Reply-To: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de> To: Kees Cook , Andy Shevchenko , Andrew Morton Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2281; i=jre@pengutronix.de; h=from:subject:message-id; bh=1xBOa/arXWhUJUP6g5m17JkL1URolc5p+IQdAXLKJ/M=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshadYVNz/jirVZhl00dzbwfT7Od9J7l/T9PqrTwCl/Fr RSZBTN/d5SyMIhxMciKKbLEqskpCBn7XzertIuFmcPKBDKEgYtTACYSsJzhN3tu7aefdxZesDQ5 W/vO96/aQpP2Y47KR5Pdkqxsu143LWP4HxkmeYtZy1LffsJ9xbL9+c/KJi8sLjyR7fFGYoJQRfV aFgA= X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 All of the available sequences expand to exactly one byte, the size check in the loop condition is sufficient for the case of an escaped character too. Otherwise, an escape sequence that should be unescaped to the last character before terminating with null in the destination buffer will be output as backslash instead of the escaped character. The only exception is when encountering a backslash that turns out to not start a valid escape sequence and both the backslash and the character following are handled in one iteration. Move the check there. Add a kunit regression-test that unescapes a character to right in front of the null terminator of the destination buffer. Fixes: 16c7fa05829e ("lib/string_helpers: introduce generic string_unescape= ") Signed-off-by: Jonas Rebmann --- lib/string_helpers.c | 5 +++-- lib/tests/string_helpers_kunit.c | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/string_helpers.c b/lib/string_helpers.c index cb41ef9d8c5b..4a621f884bde 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -329,7 +329,7 @@ int string_unescape(char *src, char *dst, size_t size, = unsigned int flags) size =3D SIZE_MAX; =20 while (*src && --size) { - if (src[0] =3D=3D '\\' && src[1] !=3D '\0' && size > 1) { + if (src[0] =3D=3D '\\' && src[1] !=3D '\0') { src++; =20 if (flags & UNESCAPE_SPACE && @@ -349,7 +349,8 @@ int string_unescape(char *src, char *dst, size_t size, = unsigned int flags) continue; =20 *out++ =3D '\\'; - size--; + if (!--size) + break; } *out++ =3D *src++; } diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_ku= nit.c index 2e02c680cbb2..10763a01be83 100644 --- a/lib/tests/string_helpers_kunit.c +++ b/lib/tests/string_helpers_kunit.c @@ -626,6 +626,8 @@ static void test_unescape(struct kunit *test) =20 test_string_unescape_one(test, "short buffer", UNESCAPE_HEX, "\\x41\\x41B= ", 4, "AAB", 3); test_string_unescape_one(test, "unrecognized escape at end", UNESCAPE_HEX= , "B\\qX", 4, "B\\q", 3); + + test_string_unescape_one(test, "end of buffer", UNESCAPE_HEX, "B\\x41", 3= , "BA", 2); } =20 static void test_escape(struct kunit *test) --=20 2.56.0.rc0.108.gf0ef1b96a0