sched_ext: Fix unbounded format string reads in the bstr kfuncs
Subsystems (2)
Andrea Righi
reviewerPATCH 1/1 sched_ext: Fix unbounded format string reads in the bstr kfuncs
The format string arguments of the sched_ext bstr kfuncs (scx_bpf_exit_bstr(), scx_bpf_error_bstr(), scx_bpf_dump_bstr() and scx_bpf_sub_kill_bstr()) are passed to bpf_bprintf_prepare() with a UINT_MAX length. The verifier only checks that the pointer is readable for a single byte, so an unterminated format makes strnchr() scan past the end of the BPF object, possibly into unmapped memory. If the scan covers more than INT_MAX bytes before finding a NUL, it can also overflow the int index of the parsing loop.
Moreover, the format usually lives in a writable .data map, so it can be modified by the BPF program between bpf_bprintf_prepare() and bstr_printf(), making the latter interpret arguments that have not been validated and prepared.
Fix this by taking a snapshot of the format into a private kernel buffer using a bounded, fault-safe read and using only the snapshot for both preparation and formatting. Formats of SCX_BSTR_FMT_LEN (1024) bytes or more, including the NUL, are rejected with -E2BIG, which doesn't affect any realistic format, as messages are already limited to SCX_EXIT_MSG_LEN.
The snapshot is too large for the stack (these kfuncs can run in NMI), so store it next to the existing output buffers, which already have a single user at any time: scx_exit_info (per scheduler, written only by the winner of scx_claim_exit()) for the exit path, and scx_bstr_buf (serialized by scx_dump_lock) for the dump path.
Note that the bounded read may still access memory beyond the BPF object up to SCX_BSTR_FMT_LEN bytes. This is not a concern, since loading a struct_ops program requires CAP_PERFMON, which already allows reading arbitrary kernel memory via bpf_probe_read_kernel_str().
The kfunc prototypes and the BPF-side wrappers are unchanged, so existing BPF schedulers keep working on fixed kernels and newly built ones keep working on older kernels.
--- kernel/sched/ext/ext.c | 50 ++++++++++++++++++++++++++----------- kernel/sched/ext/internal.h | 4 +++ kernel/sched/ext/types.h | 1 + 3 files changed, 41 insertions(+), 14 deletions(-)
kernel/sched/ext/ext.c +36 -14
@@ -7389,6 +7389,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 +7406,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 +11357,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 +11398,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 +11439,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);
@@ -11513,11 +11535,11 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, }/* 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); + dd->prefix, buf->fmt, data, data__sz, ret);return ; }
kernel/sched/ext/internal.h +4 -0
@@ -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
@@ -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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched_ext: same-CPU re-entrancy in scx_bpf_dump_bstr() corrupts buf->fmt --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched_ext: same-CPU re-entrancy in scx_bpf_dump_bstr() corrupts buf->fmt
--
commit 327433557b482c491e440670458830800354c885
Author: Andrea Righi <[email protected]>
sched_ext: Fix unbounded format string reads in the bstr kfuncs
This commit copies the BPF-supplied format string into a bounded kernel buffer (scx_exit_info->fmt or scx_bstr_buf->fmt) before running bpf_bprintf_prepare() and bstr_printf().
Quoted 4 lines
kernel/sched/ext/ext.c +0
-0
[ ... ]
Quoted 63 lines
kernel/sched/ext/ext.c +29
-9@@ -11283,14 +11285,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);@@ -11303,18 +11326,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; }
[ ... ]
Quoted 4 lines
kernel/sched/ext/ext.c +0
-0@@ -11441,11 +11463,11 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, }/* append the formatted string to the line buf */
kernel/sched/ext/ext.c __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, · line 11473
Quoted 10 lines
kernel/sched/ext/ext.c +2
-2- 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); + dd->prefix, buf->fmt, data, data__sz, ret);return ; }
[Severity: Medium]
Can same-CPU re-entrancy into scx_bpf_dump_bstr() overwrite buf->fmt between bpf_bprintf_prepare() and bstr_printf()?
scx_bpf_dump_bstr() is registered in scx_kfunc_ids_any and is not excluded by scx_kfunc_ids_no_tracing, so a BPF_PROG_TYPE_TRACING program can invoke it. Because scx_bpf_dump_bstr() only checks raw_smp_processor_id() != dd->cpu, a tracing program firing on dd->cpu during a dump (for example on bpf_bprintf_prepare() or from an NMI) passes that check and shares scx_dump_data.buf.fmt:
scx_bpf_dump_bstr() [outer, fmt="hello", data__sz=0]
__bstr_format()
scx_snapshot_fmt(buf->fmt, ..., "hello")
bpf_bprintf_prepare(buf->fmt, ...)
-> bprintf_data.bin_args = NULL
<fentry/NMI tracing prog on dd->cpu>
scx_bpf_dump_bstr() [inner, fmt="%s", data__sz=8]
__bstr_format()
scx_snapshot_fmt(buf->fmt, ..., "%s") <-- overwrites buf->fmt
bstr_printf(..., buf->fmt, NULL) <-- NULL pointer dereferenceWhen the outer call resumes in bstr_printf(), it parses the inner call's format string in buf->fmt against the outer bprintf_data.bin_args, causing a NULL pointer dereference (when the outer data__sz is 0) or reading unvalidated arguments from bin_args.
Adding scx_bpf_dump_bstr to scx_kfunc_ids_no_tracing (or guarding dd with a busy flag on dd->cpu) would prevent this re-entrancy.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Andrea,
Quoted 85 lines · Andrea Righi
The format string arguments of the
sched_extbstr kfuncs (scx_bpf_exit_bstr(),scx_bpf_error_bstr(),scx_bpf_dump_bstr()andscx_bpf_sub_kill_bstr()) are passed tobpf_bprintf_prepare()with aUINT_MAXlength. The verifier only checks that the pointer is readable for a single byte, so an unterminated format makesstrnchr()scan past the end of the BPF object, possibly into unmapped memory. If the scan covers more thanINT_MAXbytes before finding a NUL, it can also overflow the int index of the parsing loop.Moreover, the format usually lives in a writable .data map, so it can be modified by the BPF program between
bpf_bprintf_prepare()andbstr_printf(), making the latter interpret arguments that have not been validated and prepared.Fix this by taking a snapshot of the format into a private kernel buffer using a bounded, fault-safe read and using only the snapshot for both preparation and formatting. Formats of
SCX_BSTR_FMT_LEN(1024) bytes or more, including the NUL, are rejected with-E2BIG, which doesn't affect any realistic format, as messages are already limited toSCX_EXIT_MSG_LEN.The snapshot is too large for the stack (these kfuncs can run in NMI), so store it next to the existing output buffers, which already have a single user at any time:
scx_exit_info(per scheduler, written only by the winner ofscx_claim_exit()) for the exit path, andscx_bstr_buf(serialized byscx_dump_lock) for the dump path.Note that the bounded read may still access memory beyond the BPF object up to
SCX_BSTR_FMT_LENbytes. This is not a concern, since loading astruct_opsprogram requiresCAP_PERFMON, which already allows reading arbitrary kernel memory viabpf_probe_read_kernel_str().The kfunc prototypes and the BPF-side wrappers are unchanged, so existing BPF schedulers keep working on fixed kernels and newly built ones keep working on older kernels.
Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")Cc [email protected] # v6.12+Reported-by Masoud Aghasi <[email protected]>Signed-off-by Andrea Righi <[email protected]>--- kernel/sched/ext/ext.c | 50 ++++++++++++++++++++++++++----------- kernel/sched/ext/internal.h | 4 +++ kernel/sched/ext/types.h | 1 + 3 files changed, 41 insertions(+), 14 deletions(-)kernel/sched/ext/ext.c +9
-5@@ -7389,6 +7389,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 +7406,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 +11357,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) +{
kernel/sched/ext/ext.c __bpf_kfunc void scx_bpf_reenqueue_local___v2(const struct bpf_prog_aux *aux) · line 11375
Quoted 10 lines
kernel/sched/ext/ext.c +10
-0+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 ; +} +
Thanks for the fix. I see 2 potential limitations with this approach:
1- as you mentioned, the bounded nofault read does not establish
the actual readable object boundary.
2- couldn't the source be modified concurrently while the snapshot is
being copied? (e.g. through updating the writable array value from userspace)
Quoted 91 lines
kernel/sched/ext/ext.c +17
-9+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 +11398,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 +11439,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);@@ -11513,11 +11535,11 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, }/* 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); + dd->prefix, buf->fmt, data, data__sz, ret);return ; }kernel/sched/ext/internal.h +4
-0@@ -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 +0
-0
kernel/sched/ext/types.h enum scx_consts { · line 42
Quoted 7 lines
kernel/sched/ext/types.h +1
-0@@ -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,
That said, I agree the compatibility concern is real. I trust your judgement in weighing the benefit of keeping existing schedulers working more heavily than these rare potential consequences.
The INT_MAX overflow issue that could lead to out-of-bounds accesses through negative offsets is resolved anyway.
Andrea Righi
reviewerHi Masoud,
Quoted 94 lines · Masoud Aghasi
Hi Andrea,
Quoted 91 lines · Andrea Righi
The format string arguments of the
sched_extbstr kfuncs (scx_bpf_exit_bstr(),scx_bpf_error_bstr(),scx_bpf_dump_bstr()andscx_bpf_sub_kill_bstr()) are passed tobpf_bprintf_prepare()with aUINT_MAXlength. The verifier only checks that the pointer is readable for a single byte, so an unterminated format makesstrnchr()scan past the end of the BPF object, possibly into unmapped memory. If the scan covers more thanINT_MAXbytes before finding a NUL, it can also overflow the int index of the parsing loop.Moreover, the format usually lives in a writable .data map, so it can be modified by the BPF program between
bpf_bprintf_prepare()andbstr_printf(), making the latter interpret arguments that have not been validated and prepared.Fix this by taking a snapshot of the format into a private kernel buffer using a bounded, fault-safe read and using only the snapshot for both preparation and formatting. Formats of
SCX_BSTR_FMT_LEN(1024) bytes or more, including the NUL, are rejected with-E2BIG, which doesn't affect any realistic format, as messages are already limited toSCX_EXIT_MSG_LEN.The snapshot is too large for the stack (these kfuncs can run in NMI), so store it next to the existing output buffers, which already have a single user at any time:
scx_exit_info(per scheduler, written only by the winner ofscx_claim_exit()) for the exit path, andscx_bstr_buf(serialized byscx_dump_lock) for the dump path.Note that the bounded read may still access memory beyond the BPF object up to
SCX_BSTR_FMT_LENbytes. This is not a concern, since loading astruct_opsprogram requiresCAP_PERFMON, which already allows reading arbitrary kernel memory viabpf_probe_read_kernel_str().The kfunc prototypes and the BPF-side wrappers are unchanged, so existing BPF schedulers keep working on fixed kernels and newly built ones keep working on older kernels.
Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")Cc [email protected] # v6.12+Reported-by Masoud Aghasi <[email protected]>Signed-off-by Andrea Righi <[email protected]>--- kernel/sched/ext/ext.c | 50 ++++++++++++++++++++++++++----------- kernel/sched/ext/internal.h | 4 +++ kernel/sched/ext/types.h | 1 + 3 files changed, 41 insertions(+), 14 deletions(-)kernel/sched/ext/ext.c +15
-5@@ -7389,6 +7389,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 +7406,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 +11357,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)
Quoted 8 lines
kernel/sched/ext/ext.c __bpf_kfunc void scx_bpf_reenqueue_local___v2(const struct bpf_prog_aux *aux) · line 11375
Quoted 4 lines
kernel/sched/ext/ext.c +4
-0+return -E2BIG ; +return 0 ; +} +Thanks for the fix. I see 2 potential limitations with this approach:
1- as you mentioned, the bounded nofault read does not establish
the actual readable object boundary.
Right, the kernel doesn't know where the object containing the format ends, so an unterminated format can make the copy read past it.
However, the formats generated by scx_bpf_exit/error/dump() are static variables in .data, which libbpf creates as a BPF_F_MMAPABLE array map. Such maps are backed by a dedicated, zeroed, page-aligned vmalloc area, followed by a vmalloc guard page. So, if the format is not terminated, the copy either hits the zero padding after the variables (finding a NUL), or the guard page, which strncpy_from_kernel_nofault() handles by returning -EFAULT. In both cases it never reads outside the map.
Only a format passed explicitly from other memory could read into a neighbouring object, but the read is still bounded to SCX_BSTR_FMT_LEN, can't fault, and the program has CAP_PERFMON, which already allows reading arbitrary kernel memory via bpf_probe_read_kernel_str().
Quoted 4 lines
2- couldn't the source be modified concurrently while the snapshot is
being copied? (e.g. through updating the writable array value from userspace)
Yes, the snapshot isn't atomic, but it doesn't need to be: what matters is that bpf_bprintf_prepare() and bstr_printf() see exactly the same bytes. Once copied, the snapshot is private and always NUL-terminated, so a concurrent writer can only produce a garbled message (or one rejected by bpf_bprintf_prepare()), but never a string that is formatted without being validated first. The data arguments already behave the same way.
And the issue pointed out by Sashiko is also valid:
https://lore.kernel.org/r/[email protected]
However, calling scx_bpf_dump() from a tracing program doesn't really make sense IMHO: it's only meaningful from ops.dump() and friends, and called from any other CPU it just aborts the scheduler.
I'll send a separate follow-up patch that adds scx_bpf_dump_bstr() to scx_kfunc_ids_no_tracing, rejecting such programs at load time.
Thanks for reviewing this!
-Andrea
Tejun Heo
reviewerHello, Andrea.
kernel/sched/ext/ext.c __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, · line 11539
Quoted 7 lines · Andrea Righi
kernel/sched/ext/ext.c +1
-1@@ -11513,11 +11535,11 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, }/* 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);
The reentrancy that sashiko pointed out reproduces. With an fexit program on bpf_bprintf_prepare() calling scx_bpf_dump() on the dump CPU and the outer call passing no args, bstr_printf() runs the inner format on NULL bin_args:
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:strlen+0x25/0x70 Call Trace: <IRQ> bstr_printf+0x182/0x8d0 __bstr_format+0x12e/0x1b0 scx_bpf_dump_bstr+0xe2/0x310 bpf_prog_1088b804fb0352ba_bstr_dump+0x117/0x1ce bpf__sched_ext_ops_dump+0x48/0xa4 scx_dump_state+0x4b9/0xe00 scx_disable_irq_workfn+0x7b/0xf0
Without the patch, the same test only loses dump lines, so the oops is new with the shared buf->fmt. scx_kfunc_ids_no_tracing was added in this cycle and this is headed to stable, so can you close it in this patch instead of a follow-up?
Thanks.
--
tejun
Andrea Righi
reviewerHi Tejun,
Quoted 28 lines · Tejun Heo
Hello, Andrea.
kernel/sched/ext/ext.c __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, · line 11539
Quoted 7 lines · Andrea Righi
kernel/sched/ext/ext.c +1
-1@@ -11513,11 +11535,11 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, }/* 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);The reentrancy that sashiko pointed out reproduces. With an fexit program on
bpf_bprintf_prepare()callingscx_bpf_dump()on the dump CPU and the outer call passing no args,bstr_printf()runs the inner format on NULLbin_args:KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:strlen+0x25/0x70 Call Trace: <IRQ> bstr_printf+0x182/0x8d0 __bstr_format+0x12e/0x1b0 scx_bpf_dump_bstr+0xe2/0x310 bpf_prog_1088b804fb0352ba_bstr_dump+0x117/0x1ce bpf__sched_ext_ops_dump+0x48/0xa4 scx_dump_state+0x4b9/0xe00 scx_disable_irq_workfn+0x7b/0xf0
Quoted 4 lines
Without the patch, the same test only loses dump lines, so the oops is new with the shared
buf->fmt.scx_kfunc_ids_no_tracingwas added in this cycle and this is headed to stable, so can you close it in this patch instead of a follow-up?
Meh, I noticed this email after I sent a v2, which disallows scx_bpf_dump_bstr() from tracing programs, but that doesn't help stable. I'll send a v3 that detects nested invocations in scx_bpf_dump_bstr() and bails out before touching the shared buffers, so the fix is self-contained.
Thanks,
-Andrea