From: Muhammad Bilal <meatuni001@gmail.com>
To: Kees Cook <kees@kernel.org>
Cc: "Muhammad Bilal" <meatuni001@gmail.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] lib/string_helpers: add ESCAPE_BACKSLASH flag to string_escape_mem()
Date: Sun, 27 Sep 2026 01:56:54 +0500 [thread overview]
Message-ID: <20260926205708.384308-1-meatuni001@gmail.com> (raw)
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
next reply other threads:[~2026-09-26 20:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-26 20:56 Muhammad Bilal [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260926205708.384308-1-meatuni001@gmail.com \
--to=meatuni001@gmail.com \
--cc=andy@kernel.org \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®