Skip to contentSkip to conversation

bpf: allow UTF-8 literals in bpf_bprintf_prepare()

PATCH Applied Yihan Ding 4 participants 7 messages Apr 16

Subsystems (3)
lore
Share & tools
lore

Yihan Ding

author
#0 lore raw

v3 cover bpf: allow UTF-8 literals in bpf_bprintf_prepare()

bpf_bprintf_prepare() currently rejects any non-ASCII byte in format strings, so helpers such as bpf_trace_printk() fail to emit UTF-8 literal text even when those bytes are not part of a format specifier.

Keep plain text permissive while continuing to parse '%' sequences as ASCII-only. Patch 1 updates snprintf_negative() at the same time so the selftests stay consistent during bisection. Patch 2 then extends trace_printk coverage for both the valid UTF-8 literal case and the invalid non-ASCII-after-'%' case.

Changes in v3:
- drop Suggested-by trailers and move review credit into this changelog
- update test_snprintf_negative() in patch 1/2 so plain non-ASCII text is accepted while non-ASCII after '%' is still rejected, keeping ./test_progs -t snprintf aligned with the new behavior.
- clarify the trace_printk negative case with an explicit invalid format string and comment
- address Paul Chaignon's review feedback and keep the negative coverage requested earlier by Alan Maguire

Changes in v2:
- split the core change and selftest updates into two patches
- drop unnecessary isspace()/ispunct() casts
- add comments to clarify plain-text vs format-specifier handling
- add a negative selftest for non-ASCII bytes inside '%' sequences

Testing:
- Reproduced on x86_64 without the core fix: ASCII trace output works, while UTF-8 literal text in bpf_trace_printk() is rejected and produces no trace output
- Verified with tools/testing/selftests/bpf: ./test_progs -t trace_printk
- Verified with tools/testing/selftests/bpf: ./test_progs -t snprintf

Yihan Ding (2):
bpf: allow UTF-8 literals in bpf_bprintf_prepare() selftests/bpf: cover UTF-8 trace_printk output

 kernel/bpf/helpers.c                          | 17 ++++++++++-
 .../selftests/bpf/prog_tests/snprintf.c       |  3 +-
 .../selftests/bpf/prog_tests/trace_printk.c   | 28 +++++++++++++++----
 .../selftests/bpf/progs/trace_printk.c        | 10 +++++++
 4 files changed, 50 insertions(+), 8 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v3 1/2 bpf: allow UTF-8 literals in bpf_bprintf_prepare()

bpf_bprintf_prepare() only needs ASCII parsing for conversion specifiers. Plain text can safely carry bytes >= 0x80, so allow UTF-8 literals outside '%' sequences while keeping ASCII control bytes rejected and format specifiers ASCII-only.

This keeps existing parsing rules for format directives unchanged, while allowing helpers such as bpf_trace_printk() to emit UTF-8 literal text.

Update test_snprintf_negative() in the same commit so selftests keep matching the new plain-text vs format-specifier split during bisection.

Fixes 48cac3f4a96d ("bpf: Implement formatted output helpers with bstr_printf")
---
 kernel/bpf/helpers.c                            | 17 ++++++++++++++++-
 .../testing/selftests/bpf/prog_tests/snprintf.c |  3 ++-
 2 files changed, 18 insertions(+), 2 deletions(-)
kernel/bpf/helpers.c +16 -1
index 6eb6c82ed2ee..d51f1b612f1d 100644
@@ -845,7 +845,13 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 		data->buf = buffers->buf;
 
 	for (i = 0; i < fmt_size; i++) {
-		if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
+		unsigned char c = fmt[i];
+
+		/*
+		 * Permit bytes >= 0x80 in plain text so UTF-8 literals can pass
+		 * through unchanged, while still rejecting ASCII control bytes.
+		 */
+		if (isascii(c) && !isprint(c) && !isspace(c)) {
 			err = -EINVAL;
 			goto out;
 		}
@@ -867,6 +873,15 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 		 * always access fmt[i + 1], in the worst case it will be a 0
 		 */
 		i++;
+		c = fmt[i];
+		/*
+		 * The format parser below only understands ASCII conversion
+		 * specifiers and modifiers, so reject non-ASCII after '%'.
+		 */
+		if (!isascii(c)) {
+			err = -EINVAL;
+			goto out;
+		}
 
 		/* skip optional "[0 +-][num]" width formatting field */
 		while (fmt[i] == '0' || fmt[i] == '+'  || fmt[i] == '-' ||
tools/testing/selftests/bpf/prog_tests/snprintf.c +2 -1
index 594441acb707..4e4a82d54f79 100644
@@ -114,7 +114,8 @@ static void test_snprintf_negative(void)
 	ASSERT_ERR(load_single_snprintf("%--------"), "invalid specifier 5");
 	ASSERT_ERR(load_single_snprintf("%lc"), "invalid specifier 6");
 	ASSERT_ERR(load_single_snprintf("%llc"), "invalid specifier 7");
-	ASSERT_ERR(load_single_snprintf("\x80"), "non ascii character");
+	ASSERT_OK(load_single_snprintf("\x80"), "non ascii plain text");
+	ASSERT_ERR(load_single_snprintf("%\x80"), "non ascii in specifier");
 	ASSERT_ERR(load_single_snprintf("\x1"), "non printable character");
 	ASSERT_ERR(load_single_snprintf("%p%"), "invalid specifier 8");
 	ASSERT_ERR(load_single_snprintf("%s%"), "invalid specifier 9");
Signature · 1 line
2.20.1
#2 lore raw
Sashiko AI review found 1 potential issue(s): - [Medium] UTF-8 literals are incorrectly rejected when placed immediately after '%s' or '%p' format specifiers. --

Sashiko AI review found 1 potential issue(s):
- [Medium] UTF-8 literals are incorrectly rejected when placed immediately after '%s' or '%p' format specifiers. --

commit c7ffc79748019b47230d65f7c4646b2bba78f020
Author: Yihan Ding <[email protected]>

bpf: allow UTF-8 literals in bpf_bprintf_prepare()

This commit modifies bpf_bprintf_prepare() to permit bytes >= 0x80 in plain text, allowing UTF-8 literals to pass through unchanged while still rejecting ASCII control bytes.

Quoted 9 lines
kernel/bpf/helpers.c +0 -1
index bb95e287b0dc8..2bb60200c2665 100644
@@ -845,7 +845,13 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 		data->buf = buffers->buf;
 
 	for (i = 0; i < fmt_size; i++) {
-		if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {

kernel/bpf/helpers.c int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args, · line 857

Quoted 10 lines
kernel/bpf/helpers.c +7 -0
+		unsigned char c = fmt[i];
+
+		/*
+		 * Permit bytes >= 0x80 in plain text so UTF-8 literals can pass
+		 * through unchanged, while still rejecting ASCII control bytes.
+		 */
+		if (isascii(c) && !isprint(c) && !isspace(c)) {
 			err = -EINVAL;
 			goto out;
 		}

Does this change overlook the lookahead checks performed on characters immediately following format specifiers like %s and %p?

For example, looking at the parsing logic for %s:

kernel/bpf/helpers.c:bpf_bprintf_prepare() {
    ...
		} else if (fmt[i] == 's') {
			fmt_ptype = fmt[i];
fmt_str:
			if (fmt[i + 1] != 0 &&
			    !isspace(fmt[i + 1]) &&
			    !ispunct(fmt[i + 1])) {
				err = -EINVAL;
				goto out;
			}
    ...
}

Because the initial loop now permits bytes >= 0x80, these bytes can appear immediately after a format specifier. If a UTF-8 literal is placed immediately after the specifier, such as in the string "%s你好", fmt[i + 1] will be the first byte of the UTF-8 character.

Since isspace() and ispunct() only match ASCII characters, both will evaluate to false. Will this cause the parser to incorrectly reject valid UTF-8 strings with -EINVAL?

A similar lookahead check exists for %p:

kernel/bpf/helpers.c:bpf_bprintf_prepare() {
    ...
		if (fmt[i] == 'p') {
			sizeof_cur_arg = sizeof(long);

			if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) ||
			    ispunct(fmt[i + 1])) {
				if (tmp_buf)
					cur_arg = raw_args[num_spec];
				goto nocopy_fmt;
			}
    ...
}

If fmt[i + 1] is a UTF-8 character, it won't match isspace() or ispunct(). It will then fall through to the %pI4/%pi4 checks and also be rejected with -EINVAL.

Can the lookahead checks be updated to also permit bytes >= 0x80, matching the new plain text parsing rules?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Acked-by on v3 1/2
#3 lore raw
In reply to Yihan Ding
Quoted 24 lines · Yihan Ding

bpf_bprintf_prepare() only needs ASCII parsing for conversion specifiers. Plain text can safely carry bytes >= 0x80, so allow UTF-8 literals outside '%' sequences while keeping ASCII control bytes rejected and format specifiers ASCII-only.

This keeps existing parsing rules for format directives unchanged, while allowing helpers such as bpf_trace_printk() to emit UTF-8 literal text.

Update test_snprintf_negative() in the same commit so selftests keep matching the new plain-text vs format-specifier split during bisection.

Fixes 48cac3f4a96d ("bpf: Implement formatted output helpers with bstr_printf")
---
 kernel/bpf/helpers.c                            | 17 ++++++++++++++++-
 .../testing/selftests/bpf/prog_tests/snprintf.c |  3 ++-
 2 files changed, 18 insertions(+), 2 deletions(-)
kernel/bpf/helpers.c +0 -0
index 6eb6c82ed2ee..d51f1b612f1d 100644

kernel/bpf/helpers.c int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args, · line 848

Quoted 5 lines
kernel/bpf/helpers.c +1 -1
@@ -845,7 +845,13 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 		data->buf = buffers->buf;
 
 	for (i = 0; i < fmt_size; i++) {
-		if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
+		unsigned char c = fmt[i];

I'm a bit unsure this extra variable is worth it, but it's probably not worth sending a v4 just for that.

Quoted 30 lines
kernel/bpf/helpers.c +15 -0
+
+		/*
+		 * Permit bytes >= 0x80 in plain text so UTF-8 literals can pass
+		 * through unchanged, while still rejecting ASCII control bytes.
+		 */
+		if (isascii(c) && !isprint(c) && !isspace(c)) {
 			err = -EINVAL;
 			goto out;
 		}
@@ -867,6 +873,15 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 		 * always access fmt[i + 1], in the worst case it will be a 0
 		 */
 		i++;
+		c = fmt[i];
+		/*
+		 * The format parser below only understands ASCII conversion
+		 * specifiers and modifiers, so reject non-ASCII after '%'.
+		 */
+		if (!isascii(c)) {
+			err = -EINVAL;
+			goto out;
+		}
 
 		/* skip optional "[0 +-][num]" width formatting field */
 		while (fmt[i] == '0' || fmt[i] == '+'  || fmt[i] == '-' ||
tools/testing/selftests/bpf/prog_tests/snprintf.c +0 -0
index 594441acb707..4e4a82d54f79 100644

tools/testing/selftests/bpf/prog_tests/snprintf.c static void test_snprintf_negative(void) · line 118

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/snprintf.c +2 -1
@@ -114,7 +114,8 @@ static void test_snprintf_negative(void)
 	ASSERT_ERR(load_single_snprintf("%--------"), "invalid specifier 5");
 	ASSERT_ERR(load_single_snprintf("%lc"), "invalid specifier 6");
 	ASSERT_ERR(load_single_snprintf("%llc"), "invalid specifier 7");
-	ASSERT_ERR(load_single_snprintf("\x80"), "non ascii character");
+	ASSERT_OK(load_single_snprintf("\x80"), "non ascii plain text");
+	ASSERT_ERR(load_single_snprintf("%\x80"), "non ascii in specifier");
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/snprintf.c +0 -0
 	ASSERT_ERR(load_single_snprintf("\x1"), "non printable character");
 	ASSERT_ERR(load_single_snprintf("%p%"), "invalid specifier 8");
 	ASSERT_ERR(load_single_snprintf("%s%"), "invalid specifier 9");
Signature · 1 line
2.20.1
author
#4 lore raw

PATCH v3 2/2 selftests/bpf: cover UTF-8 trace_printk output

In reply to Yihan Ding

Extend trace_printk coverage to verify that UTF-8 literal text is emitted successfully and that '%' parsing still rejects non-ASCII bytes once format parsing starts.

Use an explicitly invalid format string for the negative case so the ASCII-only parser expectation is visible from the test code itself.

---
 .../selftests/bpf/prog_tests/trace_printk.c   | 28 +++++++++++++++----
 .../selftests/bpf/progs/trace_printk.c        | 10 +++++++
 2 files changed, 32 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/trace_printk.c +22 -6
index e56e88596d64..a5a8104c1ddd 100644
@@ -6,18 +6,21 @@
 #include "trace_printk.lskel.h"
 
 #define SEARCHMSG	"testing,testing"
+#define SEARCHMSG_UTF8	"中文,测试"
 
 static void trace_pipe_cb(const char *str, void *data)
 {
 	if (strstr(str, SEARCHMSG) != NULL)
-		(*(int *)data)++;
+		((int *)data)[0]++;
+	if (strstr(str, SEARCHMSG_UTF8))
+		((int *)data)[1]++;
 }
 
 void serial_test_trace_printk(void)
 {
 	struct trace_printk_lskel__bss *bss;
 	struct trace_printk_lskel *skel;
-	int err = 0, found = 0;
+	int err = 0, found[2] = {};
 
 	skel = trace_printk_lskel__open();
 	if (!ASSERT_OK_PTR(skel, "trace_printk__open"))
@@ -46,11 +49,24 @@ void serial_test_trace_printk(void)
 	if (!ASSERT_GT(bss->trace_printk_ret, 0, "bss->trace_printk_ret"))
 		goto cleanup;
 
-	/* verify our search string is in the trace buffer */
-	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, &found, 1000),
-		 "read_trace_pipe_iter");
+	if (!ASSERT_GT(bss->trace_printk_utf8_ran, 0, "bss->trace_printk_utf8_ran"))
+		goto cleanup;
+
+	if (!ASSERT_GT(bss->trace_printk_utf8_ret, 0, "bss->trace_printk_utf8_ret"))
+		goto cleanup;
+
+	if (!ASSERT_LT(bss->trace_printk_invalid_spec_ret, 0,
+		       "bss->trace_printk_invalid_spec_ret"))
+		goto cleanup;
+
+	/* verify our search strings are in the trace buffer */
+	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, found, 1000),
+		  "read_trace_pipe_iter");
+
+	if (!ASSERT_EQ(found[0], bss->trace_printk_ran, "found"))
+		goto cleanup;
 
-	if (!ASSERT_EQ(found, bss->trace_printk_ran, "found"))
+	if (!ASSERT_EQ(found[1], bss->trace_printk_utf8_ran, "found_utf8"))
 		goto cleanup;
 
 cleanup:
tools/testing/selftests/bpf/progs/trace_printk.c +10 -0
index 6695478c2b25..f4c538ec3ebd 100644
@@ -10,13 +10,23 @@ char _license[] SEC("license") = "GPL";
 
 int trace_printk_ret = 0;
 int trace_printk_ran = 0;
+int trace_printk_invalid_spec_ret = 0;
+int trace_printk_utf8_ret = 0;
+int trace_printk_utf8_ran = 0;
 
 const char fmt[] = "Testing,testing %d\n";
+static const char utf8_fmt[] = "中文,测试 %d\n";
+/* Non-ASCII bytes after '%' must still be rejected. */
+static const char invalid_spec_fmt[] = "%\x80\n";
 
 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
 int sys_enter(void *ctx)
 {
 	trace_printk_ret = bpf_trace_printk(fmt, sizeof(fmt),
 					    ++trace_printk_ran);
+	trace_printk_utf8_ret = bpf_trace_printk(utf8_fmt, sizeof(utf8_fmt),
+						 ++trace_printk_utf8_ran);
+	trace_printk_invalid_spec_ret = bpf_trace_printk(invalid_spec_fmt,
+							 sizeof(invalid_spec_fmt));
 	return 0;
 }
Signature · 1 line
2.20.1
Acked-by on v3 2/2
#5 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: cover UTF-8 trace_printk output

Quoted 8 lines · Yihan Ding

Extend trace_printk coverage to verify that UTF-8 literal text is emitted successfully and that '%' parsing still rejects non-ASCII bytes once format parsing starts.

Use an explicitly invalid format string for the negative case so the ASCII-only parser expectation is visible from the test code itself.

The test makes sense and I verified it fails as expected without the fix.

Quoted 93 lines
---
 .../selftests/bpf/prog_tests/trace_printk.c   | 28 +++++++++++++++----
 .../selftests/bpf/progs/trace_printk.c        | 10 +++++++
 2 files changed, 32 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/trace_printk.c +22 -6
index e56e88596d64..a5a8104c1ddd 100644
@@ -6,18 +6,21 @@
 #include "trace_printk.lskel.h"
 
 #define SEARCHMSG	"testing,testing"
+#define SEARCHMSG_UTF8	"中文,测试"
 
 static void trace_pipe_cb(const char *str, void *data)
 {
 	if (strstr(str, SEARCHMSG) != NULL)
-		(*(int *)data)++;
+		((int *)data)[0]++;
+	if (strstr(str, SEARCHMSG_UTF8))
+		((int *)data)[1]++;
 }
 
 void serial_test_trace_printk(void)
 {
 	struct trace_printk_lskel__bss *bss;
 	struct trace_printk_lskel *skel;
-	int err = 0, found = 0;
+	int err = 0, found[2] = {};
 
 	skel = trace_printk_lskel__open();
 	if (!ASSERT_OK_PTR(skel, "trace_printk__open"))
@@ -46,11 +49,24 @@ void serial_test_trace_printk(void)
 	if (!ASSERT_GT(bss->trace_printk_ret, 0, "bss->trace_printk_ret"))
 		goto cleanup;
 
-	/* verify our search string is in the trace buffer */
-	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, &found, 1000),
-		 "read_trace_pipe_iter");
+	if (!ASSERT_GT(bss->trace_printk_utf8_ran, 0, "bss->trace_printk_utf8_ran"))
+		goto cleanup;
+
+	if (!ASSERT_GT(bss->trace_printk_utf8_ret, 0, "bss->trace_printk_utf8_ret"))
+		goto cleanup;
+
+	if (!ASSERT_LT(bss->trace_printk_invalid_spec_ret, 0,
+		       "bss->trace_printk_invalid_spec_ret"))
+		goto cleanup;
+
+	/* verify our search strings are in the trace buffer */
+	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, found, 1000),
+		  "read_trace_pipe_iter");
+
+	if (!ASSERT_EQ(found[0], bss->trace_printk_ran, "found"))
+		goto cleanup;
 
-	if (!ASSERT_EQ(found, bss->trace_printk_ran, "found"))
+	if (!ASSERT_EQ(found[1], bss->trace_printk_utf8_ran, "found_utf8"))
 		goto cleanup;
 
 cleanup:
tools/testing/selftests/bpf/progs/trace_printk.c +10 -0
index 6695478c2b25..f4c538ec3ebd 100644
@@ -10,13 +10,23 @@ char _license[] SEC("license") = "GPL";
 
 int trace_printk_ret = 0;
 int trace_printk_ran = 0;
+int trace_printk_invalid_spec_ret = 0;
+int trace_printk_utf8_ret = 0;
+int trace_printk_utf8_ran = 0;
 
 const char fmt[] = "Testing,testing %d\n";
+static const char utf8_fmt[] = "中文,测试 %d\n";
+/* Non-ASCII bytes after '%' must still be rejected. */
+static const char invalid_spec_fmt[] = "%\x80\n";
 
 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
 int sys_enter(void *ctx)
 {
 	trace_printk_ret = bpf_trace_printk(fmt, sizeof(fmt),
 					    ++trace_printk_ran);
+	trace_printk_utf8_ret = bpf_trace_printk(utf8_fmt, sizeof(utf8_fmt),
+						 ++trace_printk_utf8_ran);
+	trace_printk_invalid_spec_ret = bpf_trace_printk(invalid_spec_fmt,
+							 sizeof(invalid_spec_fmt));
 	return 0;
 }
Signature · 1 line
2.20.1
#6 lore raw
In reply to Yihan Ding
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 11 lines · On Thu, 16 Apr 2026 20:01:40 +0800 you wrote:

bpf_bprintf_prepare() currently rejects any non-ASCII byte in format strings, so helpers such as bpf_trace_printk() fail to emit UTF-8 literal text even when those bytes are not part of a format specifier.

Keep plain text permissive while continuing to parse '%' sequences as ASCII-only. Patch 1 updates snprintf_negative() at the same time so the selftests stay consistent during bisection. Patch 2 then extends trace_printk coverage for both the valid UTF-8 literal case and the invalid non-ASCII-after-'%' case.

[...]

Here is the summary with links:
- [bpf,v3,1/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare()

    https://git.kernel.org/bpf/bpf/c/b960430ea886
  - [bpf,v3,2/2] selftests/bpf: cover UTF-8 trace_printk output
    https://git.kernel.org/bpf/bpf/c/4198ff31edb1

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html