mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] lib/string_helpers: add ESCAPE_BACKSLASH flag to string_escape_mem()
@ 2026-09-26 20:56 Muhammad Bilal
  2026-09-26 20:56 ` [PATCH 2/2] platform/x86: hp-bioscfg: fix heap OOB read and non-ASCII truncation in hp_get_string_from_buffer() Muhammad Bilal
  2026-09-28  8:11 ` [PATCH 1/2] lib/string_helpers: add ESCAPE_BACKSLASH flag to string_escape_mem() Andy Shevchenko
  0 siblings, 2 replies; 8+ messages in thread
From: Muhammad Bilal @ 2026-09-26 20:56 UTC (permalink / raw)
  To: Kees Cook
  Cc: Muhammad Bilal, Andy Shevchenko, Ilpo Järvinen,
	linux-hardening, linux-kernel

ESCAPE_SPECIAL escapes '\\', '\a', '\e' and '"' as one group. A caller
that wants '\\' escaped but needs '"' left untouched, because it has
its own handling for quotes or because quoting is not meaningful in
its output, currently has no way to pull just the backslash case out
of that set.

Add ESCAPE_BACKSLASH, which escapes only '\\'. It can be combined with
ESCAPE_SPACE, or used on its own, without pulling in ESCAPE_SPECIAL's
quote and control-character handling.

This only adds a new opt-in flag; no existing caller changes
behavior.

Suggested-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Muhammad Bilal <meatuni001@gmail.com>
---
 include/linux/string_helpers.h |  3 ++-
 lib/string_helpers.c           | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 3fb88a1..111114d 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -72,8 +72,9 @@ static inline int string_unescape_any_inplace(char *buf)
 #define ESCAPE_NA		BIT(6)
 #define ESCAPE_NAP		BIT(7)
 #define ESCAPE_APPEND		BIT(8)
+#define ESCAPE_BACKSLASH	BIT(9)
 
-#define ESCAPE_ALL_MASK		GENMASK(8, 0)
+#define ESCAPE_ALL_MASK		GENMASK(9, 0)
 
 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
 		unsigned int flags, const char *only);
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 98d6ed0..f35234a 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -438,6 +438,24 @@ static bool escape_special(unsigned char c, char **dst, char *end)
 	return true;
 }
 
+static bool escape_backslash(unsigned char c, char **dst, char *end)
+{
+	char *out = *dst;
+
+	if (c != '\\')
+		return false;
+
+	if (out < end)
+		*out = '\\';
+	++out;
+	if (out < end)
+		*out = '\\';
+	++out;
+
+	*dst = out;
+	return true;
+}
+
 static bool escape_null(unsigned char c, char **dst, char *end)
 {
 	char *out = *dst;
@@ -558,6 +576,8 @@ static bool escape_hex(unsigned char c, char **dst, char *end)
  *		escape only non-printable or non-ascii characters
  *	%ESCAPE_APPEND:
  *		append characters from @only to be escaped by the given classes
+ *	%ESCAPE_BACKSLASH:
+ *		'\\' - backslash, without the rest of %ESCAPE_SPECIAL
  *
  * %ESCAPE_APPEND would help to pass additional characters to the escaped, when
  * one of %ESCAPE_NP, %ESCAPE_NA, or %ESCAPE_NAP is provided.
@@ -628,6 +648,9 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
 		if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end))
 			continue;
 
+		if (flags & ESCAPE_BACKSLASH && escape_backslash(c, &p, end))
+			continue;
+
 		if (flags & ESCAPE_NULL && escape_null(c, &p, end))
 			continue;
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-09-28 20:08 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-26 20:56 [PATCH 1/2] lib/string_helpers: add ESCAPE_BACKSLASH flag to string_escape_mem() Muhammad Bilal
2026-09-26 20:56 ` [PATCH 2/2] platform/x86: hp-bioscfg: fix heap OOB read and non-ASCII truncation in hp_get_string_from_buffer() Muhammad Bilal
2026-09-28 11:32   ` Ilpo Järvinen
2026-09-28  8:11 ` [PATCH 1/2] lib/string_helpers: add ESCAPE_BACKSLASH flag to string_escape_mem() Andy Shevchenko
2026-09-28  8:17   ` Andy Shevchenko
2026-09-28 12:59   ` Ilpo Järvinen
2026-09-28 13:38     ` Andy Shevchenko
2026-09-28 20:08       ` Muhammad Bilal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®