Skip to contentSkip to conversation

sched_ext: Fix kernel OOB memory read in scx_bpf_x_bstr kfuncs

PATCH In review Masoud Aghasi 2 participants 3 messages Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched_ext: Fix kernel OOB memory read in scx_bpf_x_bstr kfuncs

The format string arguments of the sched_ext BPF kfuncs ending in _bstr() are passed to bpf_bprintf_prepare(), which searches for a terminating NUL using strnchr().

Previously, the verifier only checked that these pointers were accessible for a single byte. It did not guarantee that the format strings were NUL-terminated. Consequently, an unterminated format string could cause an out-of-bounds read during the initial strnchr() call.

Additionally, format strings larger than INT_MAX could cause signed integer overflow in the subsequent formatting loop, which uses an int index. This could result in out-of-bounds accesses regardless of whether the format string was NUL-terminated.

Annotate the format string arguments with __str so the verifier validates that they point to valid NUL-terminated strings.

Also, limit the maximum format string length to INT_MAX to prevent signed integer overflow in the formatting loop.

Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")

---
v2:
- Make the format string declaration in scx_bpf_bstr_preamble() const-qualified to satisfy the verifier's read-only string requirement.

- Update kernel-doc parameter names to reflect the fmt__str annotations.

- Clarify the description of the out-of-bounds read bug.

v1: https://lore.kernel.org/sched-ext/[email protected]/

 kernel/sched/ext/ext.c                   | 26 ++++++++++++------------
 kernel/sched/ext/internal.h              |  2 +-
 kernel/sched/ext/sub.c                   |  8 ++++----
 tools/sched_ext/include/scx/common.bpf.h | 10 ++++-----
 4 files changed, 23 insertions(+), 23 deletions(-)
kernel/sched/ext/ext.c +13 -13
index a4a93ed97f7b..adf2ed869827 100644
@@ -11357,7 +11357,7 @@ __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,
+			 size_t line_size, const char *fmt, unsigned long long *data,
 			 u32 data__sz)
 {
 	struct bpf_bprintf_data bprintf_data = { .get_bin_args = true };
@@ -11375,7 +11375,7 @@ 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,
+	ret = bpf_bprintf_prepare(fmt, INT_MAX, data_buf, data__sz / 8,
 				  &bprintf_data);
 	if (ret < 0) {
 		scx_error(sch, "format preparation failed (%d)", ret);
@@ -11405,7 +11405,7 @@ static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf,
  */
 __printf(5, 0)
 bool scx_exit_bstr(struct scx_sched *sch, enum scx_exit_kind kind,
-		   s64 exit_code, struct scx_sched *fmt_blame, char *fmt,
+		   s64 exit_code, struct scx_sched *fmt_blame, const char *fmt,
 		   unsigned long long *data, u32 data__sz)
 {
 	struct scx_exit_info *ei = sch->exit_info;
@@ -11432,7 +11432,7 @@ __bpf_kfunc_start_defs();
 /**
  * scx_bpf_exit_bstr - Gracefully exit the BPF scheduler.
  * @exit_code: Exit value to pass to user space via struct scx_exit_info.
- * @fmt: error message format string
+ * @fmt__str: exit message format string
  * @data: format string parameters packaged using ___bpf_fill() macro
  * @data__sz: @data len, must end in '__sz' for the verifier
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
@@ -11441,7 +11441,7 @@ __bpf_kfunc_start_defs();
  * disabling.
  */
 __printf(2, 0)
-__bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
+__bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, const char *fmt__str,
 				   unsigned long long *data, u32 data__sz,
 				   const struct bpf_prog_aux *aux)
 {
@@ -11451,13 +11451,13 @@ __bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
 
 	sch = scx_prog_sched(aux);
 	if (likely(sch))
-		scx_exit_bstr(sch, SCX_EXIT_UNREG_BPF, exit_code, sch, fmt,
+		scx_exit_bstr(sch, SCX_EXIT_UNREG_BPF, exit_code, sch, fmt__str,
 			      data, data__sz);
 }
 
 /**
  * scx_bpf_error_bstr - Indicate fatal error
- * @fmt: error message format string
+ * @fmt__str: error message format string
  * @data: format string parameters packaged using ___bpf_fill() macro
  * @data__sz: @data len, must end in '__sz' for the verifier
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
@@ -11466,7 +11466,7 @@ __bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
  * disabling.
  */
 __printf(1, 0)
-__bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
+__bpf_kfunc void scx_bpf_error_bstr(const char *fmt__str, unsigned long long *data,
 				    u32 data__sz, const struct bpf_prog_aux *aux)
 {
 	struct scx_sched *sch;
@@ -11475,13 +11475,13 @@ __bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
 
 	sch = scx_prog_sched(aux);
 	if (likely(sch))
-		scx_exit_bstr(sch, SCX_EXIT_ERROR_BPF, 0, sch, fmt, data,
+		scx_exit_bstr(sch, SCX_EXIT_ERROR_BPF, 0, sch, fmt__str, data,
 			      data__sz);
 }
 
 /**
  * scx_bpf_dump_bstr - Generate extra debug dump specific to the BPF scheduler
- * @fmt: format string
+ * @fmt__str: format string
  * @data: format string parameters packaged using ___bpf_fill() macro
  * @data__sz: @data len, must end in '__sz' for the verifier
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
@@ -11493,7 +11493,7 @@ __bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
  * multiple calls. The last line is automatically terminated.
  */
 __printf(1, 0)
-__bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data,
+__bpf_kfunc void scx_bpf_dump_bstr(const char *fmt__str, unsigned long long *data,
 				   u32 data__sz, const struct bpf_prog_aux *aux)
 {
 	struct scx_sched *sch;
@@ -11514,10 +11514,10 @@ __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,
-			    sizeof(buf->line) - dd->cursor, fmt, data, data__sz);
+			    sizeof(buf->line) - dd->cursor, fmt__str, 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, fmt__str, data, data__sz, ret);
 		return;
 	}
 
kernel/sched/ext/internal.h +1 -1
index c77f62cd7bc9..bdc968b71f1d 100644
@@ -2349,7 +2349,7 @@ int scx_sched_sysfs_add(struct scx_sched *sch);
 bool scx_is_descendant(struct scx_sched *sch, struct scx_sched *ancestor);
 __printf(5, 0) bool scx_exit_bstr(struct scx_sched *sch, enum scx_exit_kind kind,
 				  s64 exit_code, struct scx_sched *fmt_blame,
-				  char *fmt, unsigned long long *data, u32 data__sz);
+				  const char *fmt, unsigned long long *data, u32 data__sz);
 
 extern raw_spinlock_t scx_sched_lock;
 extern struct mutex scx_enable_mutex;
kernel/sched/ext/sub.c +4 -4
index 7c069a289766..d10f85a31789 100644
@@ -3162,7 +3162,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
 /**
  * scx_bpf_sub_kill_bstr - Kill a direct child sub-scheduler
  * @cgroup_id: cgroup id of the direct child to kill
- * @fmt: reason message format string
+ * @fmt__str: reason message format string
  * @data: format string parameters packaged using ___bpf_fill() macro
  * @data__sz: @data len, must end in '__sz' for the verifier
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
@@ -3180,7 +3180,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
  * Naming a sched that exists but is not a direct child aborts the parent.
  */
 __printf(2, 0)
-__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
+__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str,
 				      unsigned long long *data, u32 data__sz,
 				      const struct bpf_prog_aux *aux)
 {
@@ -3207,7 +3207,7 @@ __bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
 		return -EINVAL;
 	}
 
-	scx_exit_bstr(child, SCX_EXIT_PARENT_KILL, 0, parent, fmt, data, data__sz);
+	scx_exit_bstr(child, SCX_EXIT_PARENT_KILL, 0, parent, fmt__str, data, data__sz);
 	return 0;
 }
 
@@ -3237,7 +3237,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
 	return -EOPNOTSUPP;
 }
 
-__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
+__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str,
 				      unsigned long long *data, u32 data__sz,
 				      const struct bpf_prog_aux *aux)
 {
tools/sched_ext/include/scx/common.bpf.h +5 -5
index 6194484d4d9a..dbddcc833174 100644
@@ -74,9 +74,9 @@ int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, u64 flags) __k
 struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) __ksym __weak;
 void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) __ksym __weak;
 bool scx_bpf_dsq_reenq_task(struct bpf_iter_scx_dsq *it, struct task_struct *p) __ksym __weak;
-void scx_bpf_exit_bstr(s64 exit_code, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak;
-void scx_bpf_error_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym;
-void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym __weak;
+void scx_bpf_exit_bstr(s64 exit_code, const char *fmt__str, unsigned long long *data, u32 data__sz) __ksym __weak;
+void scx_bpf_error_bstr(const char *fmt__str, unsigned long long *data, u32 data_len) __ksym;
+void scx_bpf_dump_bstr(const char *fmt__str, unsigned long long *data, u32 data_len) __ksym __weak;
 u32 scx_bpf_cpuperf_cap(s32 cpu) __ksym __weak;
 u32 scx_bpf_cpuperf_cur(s32 cpu) __ksym __weak;
 void scx_bpf_cpuperf_set(s32 cpu, u32 perf) __ksym __weak;
@@ -123,7 +123,7 @@ u32 scx_bpf_cidperf_cur(s32 cid) __ksym __weak;
 s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps, const struct scx_cmask __arena *cmask__arena, struct scx_cmask __arena *denied_out__arena__nullable) __ksym __weak;
 void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps, const struct scx_cmask __arena *cmask__arena) __ksym __weak;
 s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask __arena *out__arena) __ksym __weak;
-s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak;
+s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str, unsigned long long *data, u32 data__sz) __ksym __weak;
 
 /*
  * Use the following as @it__iter when calling scx_bpf_dsq_move[_vtime]() from
@@ -146,7 +146,7 @@ void ___scx_bpf_bstr_format_checker(const char *fmt, ...) {}
  * refer to the initialized list of inputs to the bstr kfunc.
  */
 #define scx_bpf_bstr_preamble(fmt, args...)					\
-	static char ___fmt[] = fmt;						\
+	static const char ___fmt[] = fmt;						\
 	/*									\
 	 * Note that __param[] must have at least one				\
 	 * element to keep the verifier happy.					\
Signature · 1 line
2.47.3
reviewer
#1 lore raw

Hi Masoud,

Quoted 15 lines · Masoud Aghasi

The format string arguments of the sched_ext BPF kfuncs ending in _bstr() are passed to bpf_bprintf_prepare(), which searches for a terminating NUL using strnchr().

Previously, the verifier only checked that these pointers were accessible for a single byte. It did not guarantee that the format strings were NUL-terminated. Consequently, an unterminated format string could cause an out-of-bounds read during the initial strnchr() call.

Additionally, format strings larger than INT_MAX could cause signed integer overflow in the subsequent formatting loop, which uses an int index. This could result in out-of-bounds accesses regardless of whether the format string was NUL-terminated.

Quoted 8 lines

Annotate the format string arguments with __str so the verifier validates that they point to valid NUL-terminated strings.

Also, limit the maximum format string length to INT_MAX to prevent signed integer overflow in the formatting loop.

Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")

There's one issue I didn't consider in my previous review, this breaks compatibility with existing BPF schedulers in both directions (see below).

Quoted 60 lines

---
v2:
- Make the format string declaration in scx_bpf_bstr_preamble() const-qualified to satisfy the verifier's read-only string requirement.

- Update kernel-doc parameter names to reflect the fmt__str annotations.

- Clarify the description of the out-of-bounds read bug.

v1: https://lore.kernel.org/sched-ext/[email protected]/

 kernel/sched/ext/ext.c                   | 26 ++++++++++++------------
 kernel/sched/ext/internal.h              |  2 +-
 kernel/sched/ext/sub.c                   |  8 ++++----
 tools/sched_ext/include/scx/common.bpf.h | 10 ++++-----
 4 files changed, 23 insertions(+), 23 deletions(-)
kernel/sched/ext/ext.c +4 -4
index a4a93ed97f7b..adf2ed869827 100644
@@ -11357,7 +11357,7 @@ __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,
+			 size_t line_size, const char *fmt, unsigned long long *data,
 			 u32 data__sz)
 {
 	struct bpf_bprintf_data bprintf_data = { .get_bin_args = true };
@@ -11375,7 +11375,7 @@ 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,
+	ret = bpf_bprintf_prepare(fmt, INT_MAX, data_buf, data__sz / 8,
 				  &bprintf_data);
 	if (ret < 0) {
 		scx_error(sch, "format preparation failed (%d)", ret);
@@ -11405,7 +11405,7 @@ static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf,
  */
 __printf(5, 0)
 bool scx_exit_bstr(struct scx_sched *sch, enum scx_exit_kind kind,
-		   s64 exit_code, struct scx_sched *fmt_blame, char *fmt,
+		   s64 exit_code, struct scx_sched *fmt_blame, const char *fmt,
 		   unsigned long long *data, u32 data__sz)
 {
 	struct scx_exit_info *ei = sch->exit_info;
@@ -11432,7 +11432,7 @@ __bpf_kfunc_start_defs();
 /**
  * scx_bpf_exit_bstr - Gracefully exit the BPF scheduler.
  * @exit_code: Exit value to pass to user space via struct scx_exit_info.
- * @fmt: error message format string
+ * @fmt__str: exit message format string
  * @data: format string parameters packaged using ___bpf_fill() macro
  * @data__sz: @data len, must end in '__sz' for the verifier
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs

kernel/sched/ext/ext.c __bpf_kfunc_start_defs(); · line 11446

Quoted 7 lines
kernel/sched/ext/ext.c +1 -1
@@ -11441,7 +11441,7 @@ __bpf_kfunc_start_defs();
  * disabling.
  */
 __printf(2, 0)
-__bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
+__bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, const char *fmt__str,
 				   unsigned long long *data, u32 data__sz,
 				   const struct bpf_prog_aux *aux)

__str requires the format to be a NUL-terminated string in a read-only map. Existing schedulers built with the current common.bpf.h define the format as static char ___fmt[], which ends up in writable .data, so they would all fail to load on a kernel with this patch applied.

Quoted 144 lines
kernel/sched/ext/ext.c +8 -8
 {
@@ -11451,13 +11451,13 @@ __bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
 
 	sch = scx_prog_sched(aux);
 	if (likely(sch))
-		scx_exit_bstr(sch, SCX_EXIT_UNREG_BPF, exit_code, sch, fmt,
+		scx_exit_bstr(sch, SCX_EXIT_UNREG_BPF, exit_code, sch, fmt__str,
 			      data, data__sz);
 }
 
 /**
  * scx_bpf_error_bstr - Indicate fatal error
- * @fmt: error message format string
+ * @fmt__str: error message format string
  * @data: format string parameters packaged using ___bpf_fill() macro
  * @data__sz: @data len, must end in '__sz' for the verifier
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
@@ -11466,7 +11466,7 @@ __bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
  * disabling.
  */
 __printf(1, 0)
-__bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
+__bpf_kfunc void scx_bpf_error_bstr(const char *fmt__str, unsigned long long *data,
 				    u32 data__sz, const struct bpf_prog_aux *aux)
 {
 	struct scx_sched *sch;
@@ -11475,13 +11475,13 @@ __bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
 
 	sch = scx_prog_sched(aux);
 	if (likely(sch))
-		scx_exit_bstr(sch, SCX_EXIT_ERROR_BPF, 0, sch, fmt, data,
+		scx_exit_bstr(sch, SCX_EXIT_ERROR_BPF, 0, sch, fmt__str, data,
 			      data__sz);
 }
 
 /**
  * scx_bpf_dump_bstr - Generate extra debug dump specific to the BPF scheduler
- * @fmt: format string
+ * @fmt__str: format string
  * @data: format string parameters packaged using ___bpf_fill() macro
  * @data__sz: @data len, must end in '__sz' for the verifier
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
@@ -11493,7 +11493,7 @@ __bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
  * multiple calls. The last line is automatically terminated.
  */
 __printf(1, 0)
-__bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data,
+__bpf_kfunc void scx_bpf_dump_bstr(const char *fmt__str, unsigned long long *data,
 				   u32 data__sz, const struct bpf_prog_aux *aux)
 {
 	struct scx_sched *sch;
@@ -11514,10 +11514,10 @@ __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,
-			    sizeof(buf->line) - dd->cursor, fmt, data, data__sz);
+			    sizeof(buf->line) - dd->cursor, fmt__str, 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, fmt__str, data, data__sz, ret);
 		return;
 	}
 
kernel/sched/ext/internal.h +1 -1
index c77f62cd7bc9..bdc968b71f1d 100644
@@ -2349,7 +2349,7 @@ int scx_sched_sysfs_add(struct scx_sched *sch);
 bool scx_is_descendant(struct scx_sched *sch, struct scx_sched *ancestor);
 __printf(5, 0) bool scx_exit_bstr(struct scx_sched *sch, enum scx_exit_kind kind,
 				  s64 exit_code, struct scx_sched *fmt_blame,
-				  char *fmt, unsigned long long *data, u32 data__sz);
+				  const char *fmt, unsigned long long *data, u32 data__sz);
 
 extern raw_spinlock_t scx_sched_lock;
 extern struct mutex scx_enable_mutex;
kernel/sched/ext/sub.c +4 -4
index 7c069a289766..d10f85a31789 100644
@@ -3162,7 +3162,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
 /**
  * scx_bpf_sub_kill_bstr - Kill a direct child sub-scheduler
  * @cgroup_id: cgroup id of the direct child to kill
- * @fmt: reason message format string
+ * @fmt__str: reason message format string
  * @data: format string parameters packaged using ___bpf_fill() macro
  * @data__sz: @data len, must end in '__sz' for the verifier
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
@@ -3180,7 +3180,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
  * Naming a sched that exists but is not a direct child aborts the parent.
  */
 __printf(2, 0)
-__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
+__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str,
 				      unsigned long long *data, u32 data__sz,
 				      const struct bpf_prog_aux *aux)
 {
@@ -3207,7 +3207,7 @@ __bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
 		return -EINVAL;
 	}
 
-	scx_exit_bstr(child, SCX_EXIT_PARENT_KILL, 0, parent, fmt, data, data__sz);
+	scx_exit_bstr(child, SCX_EXIT_PARENT_KILL, 0, parent, fmt__str, data, data__sz);
 	return 0;
 }
 
@@ -3237,7 +3237,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
 	return -EOPNOTSUPP;
 }
 
-__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
+__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str,
 				      unsigned long long *data, u32 data__sz,
 				      const struct bpf_prog_aux *aux)
 {
tools/sched_ext/include/scx/common.bpf.h +4 -4
index 6194484d4d9a..dbddcc833174 100644
@@ -74,9 +74,9 @@ int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, u64 flags) __k
 struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) __ksym __weak;
 void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) __ksym __weak;
 bool scx_bpf_dsq_reenq_task(struct bpf_iter_scx_dsq *it, struct task_struct *p) __ksym __weak;
-void scx_bpf_exit_bstr(s64 exit_code, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak;
-void scx_bpf_error_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym;
-void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym __weak;
+void scx_bpf_exit_bstr(s64 exit_code, const char *fmt__str, unsigned long long *data, u32 data__sz) __ksym __weak;
+void scx_bpf_error_bstr(const char *fmt__str, unsigned long long *data, u32 data_len) __ksym;
+void scx_bpf_dump_bstr(const char *fmt__str, unsigned long long *data, u32 data_len) __ksym __weak;
 u32 scx_bpf_cpuperf_cap(s32 cpu) __ksym __weak;
 u32 scx_bpf_cpuperf_cur(s32 cpu) __ksym __weak;
 void scx_bpf_cpuperf_set(s32 cpu, u32 perf) __ksym __weak;
@@ -123,7 +123,7 @@ u32 scx_bpf_cidperf_cur(s32 cid) __ksym __weak;
 s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps, const struct scx_cmask __arena *cmask__arena, struct scx_cmask __arena *denied_out__arena__nullable) __ksym __weak;
 void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps, const struct scx_cmask __arena *cmask__arena) __ksym __weak;
 s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask __arena *out__arena) __ksym __weak;
-s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak;
+s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str, unsigned long long *data, u32 data__sz) __ksym __weak;
 
 /*
  * Use the following as @it__iter when calling scx_bpf_dsq_move[_vtime]() from

tools/sched_ext/include/scx/common.bpf.h void ___scx_bpf_bstr_format_checker(const char *fmt, ...) {} · line 149

Quoted 5 lines
tools/sched_ext/include/scx/common.bpf.h +1 -1
@@ -146,7 +146,7 @@ void ___scx_bpf_bstr_format_checker(const char *fmt, ...) {}
  * refer to the initialized list of inputs to the bstr kfunc.
  */
 #define scx_bpf_bstr_preamble(fmt, args...)					\
-	static char ___fmt[] = fmt;						\
+	static const char ___fmt[] = fmt;						\

And this breaks the other direction: schedulers rebuilt with the new header place the format in .rodata, while older kernels expect a writable char *fmt, so they would fail to load on kernels without this patch.

Quoted 7 lines
tools/sched_ext/include/scx/common.bpf.h +0 -0
 	/*									\
 	 * Note that __param[] must have at least one				\
 	 * element to keep the verifier happy.					\
Signature · 1 line
2.47.3

I think there may be a simpler way to fix this that keeps the kfunc prototypes and the headers unchanged, so we don't break the existent BPF schedulers. Let me run a few tests...

Thanks,
-Andrea

reviewer
#2 lore raw
Quoted 24 lines · Andrea Righi

Hi Masoud,

Quoted 21 lines · Masoud Aghasi

The format string arguments of the sched_ext BPF kfuncs ending in _bstr() are passed to bpf_bprintf_prepare(), which searches for a terminating NUL using strnchr().

Previously, the verifier only checked that these pointers were accessible for a single byte. It did not guarantee that the format strings were NUL-terminated. Consequently, an unterminated format string could cause an out-of-bounds read during the initial strnchr() call.

Additionally, format strings larger than INT_MAX could cause signed integer overflow in the subsequent formatting loop, which uses an int index. This could result in out-of-bounds accesses regardless of whether the format string was NUL-terminated.

Annotate the format string arguments with __str so the verifier validates that they point to valid NUL-terminated strings.

Also, limit the maximum format string length to INT_MAX to prevent signed integer overflow in the formatting loop.

Quoted 6 lines
Quoted 2 lines
Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")

There's one issue I didn't consider in my previous review, this breaks compatibility with existing BPF schedulers in both directions (see below).

This should fix the issue maintaining backward and forward compatibility with the BPF schedulers:

https://lore.kernel.org/r/[email protected]

Masoud, can you take a look and see if it makes sense?

Thanks!
-Andrea