Skip to contentSkip to conversation

sched_ext: Fix unbounded format string reads in the bstr kfuncs

PATCH Applied Andrea Righi 2 participants 2 messages Nov 30 – Dec 1

Subsystems (2)
lore
Share & tools
lore

Andrea Righi

author reviewer
#0 lore raw

PATCH v3 1/1 sched_ext: Fix unbounded format string reads in the bstr kfuncs

The bstr kfuncs pass their format string to bpf_bprintf_prepare() with a UINT_MAX length, but the verifier only checks that a single byte is readable. An unterminated format makes strnchr() scan past the end of the BPF object (overflowing the int parsing index past INT_MAX bytes), and a format in writable .data can change between bpf_bprintf_prepare() and bstr_printf(), which then interprets unvalidated arguments.

Fix this by snapshotting the format into a private kernel buffer with a bounded, fault-safe read and using only the snapshot for both steps. Formats of SCX_BSTR_FMT_LEN (1024) bytes or more, including the NUL, are rejected with -E2BIG. The buffer lives next to the existing output buffers in scx_exit_info and scx_bstr_buf, which already have a single user at any time, and nested scx_bpf_dump_bstr() calls from tracing programs on the dumping CPU are ignored so they can't replace the snapshot of the outer call.

The bounded read may still go up to SCX_BSTR_FMT_LEN bytes past the BPF object, which is fine since loading struct_ops requires CAP_PERFMON. The kfunc prototypes are unchanged, so existing and newly built BPF schedulers keep working on both old and fixed kernels.

Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
Cc [email protected] # v6.12+

---
Changes in v3:
- Drop the no_tracing patch and ignore nested scx_bpf_dump_bstr() calls on the dumping CPU instead, so the fix is self-contained (Tejun)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add a patch to disallow scx_bpf_dump_bstr() from tracing programs, preventing a nested call on the dumping CPU from overwriting the format snapshot of the outer call (Sashiko)
- Link to v1: https://lore.kernel.org/r/[email protected]

 kernel/sched/ext/ext.c      | 68 ++++++++++++++++++++++++++++---------
 kernel/sched/ext/internal.h |  4 +++
 kernel/sched/ext/types.h    |  1 +
 3 files changed, 57 insertions(+), 16 deletions(-)
kernel/sched/ext/ext.c +52 -16
index a4a93ed97f7b6..1f99b47feb7c6 100644
@@ -272,6 +272,7 @@ static DEFINE_RAW_SPINLOCK(scx_dump_lock);
 struct scx_dump_data {
 	s32			cpu;
 	bool			first;
+	bool			busy;
 	s32			cursor;
 	struct seq_buf		*s;
 	const char		*prefix;
@@ -7389,6 +7390,7 @@ void scx_bypass(struct scx_sched *sch, bool bypass)
 static void free_exit_info(struct scx_exit_info *ei)
 {
 	kvfree(ei->dump);
+	kfree(ei->fmt);
 	kfree(ei->msg);
 	kfree(ei->bt);
 	kfree(ei);
@@ -7405,9 +7407,10 @@ static struct scx_exit_info *alloc_exit_info(size_t exit_dump_len)
 	ei->exit_cpu = -1;
 	ei->bt = kzalloc_objs(ei->bt[0], SCX_EXIT_BT_LEN);
 	ei->msg = kzalloc(SCX_EXIT_MSG_LEN, GFP_KERNEL);
+	ei->fmt = kzalloc(SCX_BSTR_FMT_LEN, GFP_KERNEL);
 	ei->dump = kvzalloc(exit_dump_len, GFP_KERNEL);
 
-	if (!ei->bt || !ei->msg || !ei->dump) {
+	if (!ei->bt || !ei->msg || !ei->fmt || !ei->dump) {
 		free_exit_info(ei);
 		return NULL;
 	}
@@ -11355,14 +11358,35 @@ __bpf_kfunc void scx_bpf_reenqueue_local___v2(const struct bpf_prog_aux *aux)
 
 __bpf_kfunc_end_defs();
 
-__printf(5, 0)
-static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf,
-			 size_t line_size, char *fmt, unsigned long long *data,
-			 u32 data__sz)
+/*
+ * @fmt may be unterminated and writable by BPF, use only the bounded copy in
+ * @dst to prepare and format the string.
+ */
+static s32 scx_snapshot_fmt(char *dst, size_t size, const char *fmt)
+{
+	long ret = strncpy_from_kernel_nofault(dst, fmt, size);
+
+	if (ret < 0)
+		return ret;
+	/* @size means truncated or NUL in the last byte, reject both */
+	if (ret == size)
+		return -E2BIG;
+	return 0;
+}
+
+static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *fmt_buf,
+			 char *line_buf, size_t line_size, const char *fmt,
+			 unsigned long long *data, u32 data__sz)
 {
 	struct bpf_bprintf_data bprintf_data = { .get_bin_args = true };
 	s32 ret;
 
+	ret = scx_snapshot_fmt(fmt_buf, SCX_BSTR_FMT_LEN, fmt);
+	if (ret < 0) {
+		scx_error(sch, "failed to read format string (%d)", ret);
+		return ret;
+	}
+
 	if (data__sz % 8 || data__sz > MAX_BPRINTF_VARARGS * 8 ||
 	    (data__sz && !data)) {
 		scx_error(sch, "invalid data=%p and data__sz=%u", (void *)data, data__sz);
@@ -11375,18 +11399,17 @@ static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf,
 		return ret;
 	}
 
-	ret = bpf_bprintf_prepare(fmt, UINT_MAX, data_buf, data__sz / 8,
-				  &bprintf_data);
+	ret = bpf_bprintf_prepare(fmt_buf, SCX_BSTR_FMT_LEN, data_buf,
+				  data__sz / 8, &bprintf_data);
 	if (ret < 0) {
 		scx_error(sch, "format preparation failed (%d)", ret);
 		return ret;
 	}
 
-	ret = bstr_printf(line_buf, line_size, fmt,
-			  bprintf_data.bin_args);
+	ret = bstr_printf(line_buf, line_size, fmt_buf, bprintf_data.bin_args);
 	bpf_bprintf_cleanup(&bprintf_data);
 	if (ret < 0) {
-		scx_error(sch, "(\"%s\", %p, %u) failed to format", fmt, data, data__sz);
+		scx_error(sch, "(\"%s\", %p, %u) failed to format", fmt_buf, data, data__sz);
 		return ret;
 	}
 
@@ -11417,8 +11440,8 @@ bool scx_exit_bstr(struct scx_sched *sch, enum scx_exit_kind kind,
 	if (!scx_claim_exit(sch, kind))
 		return false;
 
-	ret = __bstr_format(fmt_blame, data_buf, ei->msg, SCX_EXIT_MSG_LEN,
-			    fmt, data, data__sz);
+	ret = __bstr_format(fmt_blame, data_buf, ei->fmt, ei->msg,
+			    SCX_EXIT_MSG_LEN, fmt, data, data__sz);
 	if (ret < 0)
 		scnprintf(ei->msg, SCX_EXIT_MSG_LEN,
 			  "exit message formatting failed (%d)", ret);
@@ -11512,20 +11535,30 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data,
 		return;
 	}
 
+	/*
+	 * A tracing program can call this on the dumping CPU in the middle of
+	 * another invocation. Ignore nested calls, they would clobber the shared
+	 * buffers of the outer one.
+	 */
+	if (READ_ONCE(dd->busy))
+		return;
+	WRITE_ONCE(dd->busy, true);
+	barrier();
+
 	/* append the formatted string to the line buf */
-	ret = __bstr_format(sch, buf->data, buf->line + dd->cursor,
+	ret = __bstr_format(sch, buf->data, buf->fmt, buf->line + dd->cursor,
 			    sizeof(buf->line) - dd->cursor, fmt, data, data__sz);
 	if (ret < 0) {
 		scx_dump_line(dd->s, "%s[!] (\"%s\", %p, %u) failed to format (%d)",
-			      dd->prefix, fmt, data, data__sz, ret);
-		return;
+			      dd->prefix, buf->fmt, data, data__sz, ret);
+		goto out;
 	}
 
 	dd->cursor += ret;
 	dd->cursor = min_t(s32, dd->cursor, sizeof(buf->line));
 
 	if (!dd->cursor)
-		return;
+		goto out;
 
 	/*
 	 * If the line buf overflowed or ends in a newline, flush it into the
@@ -11537,6 +11570,9 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data,
 	 */
 	if (dd->cursor >= sizeof(buf->line) || buf->line[dd->cursor - 1] == '\n')
 		ops_dump_flush();
+out:
+	barrier();
+	WRITE_ONCE(dd->busy, false);
 }
 
 /**
kernel/sched/ext/internal.h +4 -0
index c77f62cd7bc9a..9fa3bc0913beb 100644
@@ -124,6 +124,9 @@ struct scx_exit_info {
 	/* informational message */
 	char			*msg;
 
+	/* snapshot of the BPF-supplied format string for @msg */
+	char			*fmt;
+
 	/* debug dump */
 	char			*dump;
 };
@@ -2251,6 +2254,7 @@ struct scx_enable_cmd {
 /* string formatting from BPF */
 struct scx_bstr_buf {
 	u64			data[MAX_BPRINTF_VARARGS];
+	char			fmt[SCX_BSTR_FMT_LEN];
 	char			line[SCX_EXIT_MSG_LEN];
 };
 
kernel/sched/ext/types.h +1 -0
index 38514d6bf1495..6450e94cc8405 100644
@@ -36,6 +36,7 @@ enum scx_consts {
 
 	SCX_EXIT_BT_LEN			= 64,
 	SCX_EXIT_MSG_LEN		= 1024,
+	SCX_BSTR_FMT_LEN		= 1024,
 	SCX_EXIT_DUMP_DFL_LEN		= 32768,
 
 	SCX_CPUPERF_ONE			= SCHED_CAPACITY_SCALE,
Signature · 1 line
2.56.1
reviewer Applied
#1 lore raw

Applied to sched_ext/for-7.4-fixes.

Thanks.

--
tejun