Skip to contentSkip to conversation

cpumask: Introduce possible_cpu_safe()

PATCH Superseded Dan Carpenter 3 participants 9 messages Apr 4, 2019 – May 3, 2019

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

PATCH 1/2 cpumask: Introduce possible_cpu_safe()

There have been two cases recently where we pass user a controlled "cpu" to possible_cpus(). That's not allowed. If it's invalid, it will trigger a WARN_ONCE() and an out of bounds read which could result in an Oops.

This patch introduces possible_cpu_safe() which first checks to see if the cpu is valid, turns off speculation and then checks if the cpu is possible.

---
 include/linux/cpumask.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/linux/cpumask.h +18 -0
index 147bdec42215..515179760c54 100644
@@ -11,6 +11,7 @@
 #include <linux/threads.h>
 #include <linux/bitmap.h>
 #include <linux/bug.h>
+#include <linux/nospec.h>
 
 /* Don't assign or return these: may not be this big! */
 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
@@ -102,6 +103,7 @@ extern struct cpumask __cpu_active_mask;
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
 #define cpu_possible(cpu)	cpumask_test_cpu((cpu), cpu_possible_mask)
+#define cpu_possible_safe(cpu)	cpumask_test_cpu_safe((cpu), cpu_possible_mask)
 #define cpu_present(cpu)	cpumask_test_cpu((cpu), cpu_present_mask)
 #define cpu_active(cpu)		cpumask_test_cpu((cpu), cpu_active_mask)
 #else
@@ -111,6 +113,7 @@ extern struct cpumask __cpu_active_mask;
 #define num_active_cpus()	1U
 #define cpu_online(cpu)		((cpu) = 0)
 #define cpu_possible(cpu)	((cpu) = 0)
+#define cpu_possible_safe(cpu)  ((cpu) = 0)
 #define cpu_present(cpu)	((cpu) = 0)
 #define cpu_active(cpu)		((cpu) = 0)
 #endif
@@ -344,6 +347,21 @@ static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
 	return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
 }
 
+/**
+ * cpumask_test_cpu_safe - test for a cpu in a cpumask
+ * @cpu: cpu number
+ * @cpumask: the cpumask pointer
+ *
+ * Returns 1 if @cpu is valid and set in @cpumask, else returns 0
+ */
+static inline int cpumask_test_cpu_safe(int cpu, const struct cpumask *cpumask)
+{
+	if ((unsigned int)cpu >= nr_cpu_ids)
+		return 0;
+	cpu = array_index_nospec(cpu, NR_CPUS);
+	return test_bit(cpu, cpumask_bits(cpumask));
+}
+
 /**
  * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
  * @cpu: cpu number (< nr_cpu_ids)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 io_uring: Potential Oops in io_sq_offload_start()

The "p->sq_thread_cpu" variable is an integer which comes from the user. We can't pass values greater than NR_CPUS to cpu_possible() or it could lead to an out of bound read and possibly an Oops.

The recently added cpu_possible_safe() function can handle unfiltered input so lets use that instead. This patch also removes the call to array_index_nospec() since that is handled in cpu_possible_safe().

Fixes 6c271ce2f1d5 ("io_uring: add submission polling")
---
 fs/io_uring.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/io_uring.c +2 -5
index c0fecb9d889f..9789887b0d36 100644
@@ -2240,16 +2240,13 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 		ctx->sq_thread_idle = HZ;
 
 	ret = -EINVAL;
-	if (!cpu_possible(p->sq_thread_cpu))
+	if (!cpu_possible_safe(p->sq_thread_cpu))
 		goto err;
 
 	if (ctx->flags & IORING_SETUP_SQPOLL) {
 		if (p->flags & IORING_SETUP_SQ_AFF) {
-			int cpu;
-
-			cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
 			ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
-							ctx, cpu,
+							ctx, p->sq_thread_cpu,
 							"io_uring-sq");
 		} else {
 			ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
Signature · 1 line
2.17.1
#2 lore raw
In reply to Dan Carpenter
Quoted 8 lines · Dan Carpenter

There have been two cases recently where we pass user a controlled "cpu" to possible_cpus(). That's not allowed. If it's invalid, it will trigger a WARN_ONCE() and an out of bounds read which could result in an Oops.

This patch introduces possible_cpu_safe() which first checks to see if the cpu is valid, turns off speculation and then checks if the cpu is possible.

Why cannot we do the check in possible_cpu directly? Is it used from any hot path? I am quite skeptical people will use the new helper consistently.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
In reply to Dan Carpenter
Quoted 7 lines · Dan Carpenter

There have been two cases recently where we pass user a controlled "cpu" to possible_cpus(). That's not allowed. If it's invalid, it will trigger a WARN_ONCE() and an out of bounds read which could result in an Oops.

include/linux/cpumask.h +2 -0
+/**
+ * cpumask_test_cpu_safe - test for a cpu in a cpumask

include/linux/cpumask.h static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask) · line 361

Quoted 10 lines
include/linux/cpumask.h +10 -0
+ * @cpu: cpu number
+ * @cpumask: the cpumask pointer
+ *
+ * Returns 1 if @cpu is valid and set in @cpumask, else returns 0
+ */
+static inline int cpumask_test_cpu_safe(int cpu, const struct cpumask *cpumask)
+{
+	if ((unsigned int)cpu >= nr_cpu_ids)
+		return 0;
+	cpu = array_index_nospec(cpu, NR_CPUS);

That should be:

	cpu = array_index_nospec(cpu, nr_cpu_ids);

NR_CPUS might still be out-of-bounds for dynamically allocated cpumasks.

include/linux/cpumask.h static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask) · line 363

Quoted 2 lines
include/linux/cpumask.h +2 -0
+	return test_bit(cpu, cpumask_bits(cpumask));
+}

That said; I don't particularly like this interface not its naming, how about something like:

static inline unsigned int cpumask_validate_cpu(unsigned int cpu)

{
	if (cpu >= nr_cpumask_bits)
		return nr_cpumask_bits;
	return array_index_nospec(cpu, nr_cpumask_bits);
}

Which you can then use like:

	cpu = cpumask_validate_cpu(user_cpu);
	if (cpu >= nr_cpu_ids)
		return -ENORANGE;

	/* @cpu is valid, do what needs doing */
#3 lore raw
In reply to Michal Hocko
Quoted 6 lines · Michal Hocko
Quoted 5 lines · Dan Carpenter

There have been two cases recently where we pass user a controlled "cpu" to possible_cpus(). That's not allowed. If it's invalid, it will trigger a WARN_ONCE() and an out of bounds read which could result in an Oops.

Quoted 7 lines
Quoted 3 lines

This patch introduces possible_cpu_safe() which first checks to see if the cpu is valid, turns off speculation and then checks if the cpu is possible.

Why cannot we do the check in possible_cpu directly? Is it used from any hot path? I am quite skeptical people will use the new helper consistently.

Why only possible? What is to say stop anyone from using garbage (aka. user input) in any other of the cpumask APIs.

I'd much rather have the explicit validate call and keep assuming @cpu as used in the rest of the API is sane.

#5 lore raw

PATCH v2 1/2 cpumask: Introduce possible_cpu_safe()

In reply to Peter Zijlstra

There have been two cases recently where we pass user a controlled "cpu" to possible_cpus(). That's not allowed. If it's invalid, it will trigger a WARN_ONCE() and an out of bounds read which could result in an Oops.

This patch introduces possible_cpu_safe() which first checks to see if the cpu is valid, turns off speculation and then checks if the cpu is possible.

---
v2: Use nr_cpumask_bits instead of NR_CPUS.
Split cpumask_validate_cpu() into a separate function.

I still left cpumask_test_cpu_safe() return 0 for invalid cpus, instead of returning -ERANGE I feel it's simpler to stay consistent with the normal possible_cpu() function.

 include/linux/cpumask.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/cpumask.h +25 -0
index 147bdec42215..f371e44cb5ff 100644
@@ -11,6 +11,7 @@
 #include <linux/threads.h>
 #include <linux/bitmap.h>
 #include <linux/bug.h>
+#include <linux/nospec.h>
 
 /* Don't assign or return these: may not be this big! */
 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
@@ -102,6 +103,7 @@ extern struct cpumask __cpu_active_mask;
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
 #define cpu_possible(cpu)	cpumask_test_cpu((cpu), cpu_possible_mask)
+#define cpu_possible_safe(cpu)	cpumask_test_cpu_safe((cpu), cpu_possible_mask)
 #define cpu_present(cpu)	cpumask_test_cpu((cpu), cpu_present_mask)
 #define cpu_active(cpu)		cpumask_test_cpu((cpu), cpu_active_mask)
 #else
@@ -111,6 +113,7 @@ extern struct cpumask __cpu_active_mask;
 #define num_active_cpus()	1U
 #define cpu_online(cpu)		((cpu) = 0)
 #define cpu_possible(cpu)	((cpu) = 0)
+#define cpu_possible_safe(cpu)  ((cpu) = 0)
 #define cpu_present(cpu)	((cpu) = 0)
 #define cpu_active(cpu)		((cpu) = 0)
 #endif
@@ -344,6 +347,28 @@ static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
 	return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
 }
 
+static inline unsigned int cpumask_validate_cpu(unsigned int cpu)
+{
+	if (cpu >= nr_cpumask_bits)
+		return nr_cpumask_bits;
+	return array_index_nospec(cpu, nr_cpumask_bits);
+}
+
+/**
+ * cpumask_test_cpu_safe - test for a cpu in a cpumask
+ * @cpu: cpu number
+ * @cpumask: the cpumask pointer
+ *
+ * Returns 1 if @cpu is valid and set in @cpumask, else returns 0
+ */
+static inline int cpumask_test_cpu_safe(int cpu, const struct cpumask *cpumask)
+{
+	cpu = cpumask_validate_cpu(cpu);
+	if (cpu >= nr_cpu_ids)
+		return 0;
+	return test_bit(cpu, cpumask_bits(cpumask));
+}
+
 /**
  * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
  * @cpu: cpu number (< nr_cpu_ids)
Signature · 1 line
2.17.1
#6 lore raw

PATCH v2 2/2 io_uring: Potential Oops in io_sq_offload_start()

In reply to Peter Zijlstra

The "p->sq_thread_cpu" variable is an integer which comes from the user. We can't pass values greater than NR_CPUS to cpu_possible() or it could lead to an out of bound read and possibly an Oops.

The recently added cpu_possible_safe() function can handle unfiltered input so lets use that instead. This patch also removes the call to array_index_nospec() since that is handled in cpu_possible_safe().

Fixes 6c271ce2f1d5 ("io_uring: add submission polling")

---
No change.

 fs/io_uring.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/io_uring.c +2 -5
index c0fecb9d889f..9789887b0d36 100644
@@ -2240,16 +2240,13 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 		ctx->sq_thread_idle = HZ;
 
 	ret = -EINVAL;
-	if (!cpu_possible(p->sq_thread_cpu))
+	if (!cpu_possible_safe(p->sq_thread_cpu))
 		goto err;
 
 	if (ctx->flags & IORING_SETUP_SQPOLL) {
 		if (p->flags & IORING_SETUP_SQ_AFF) {
-			int cpu;
-
-			cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
 			ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
-							ctx, cpu,
+							ctx, p->sq_thread_cpu,
 							"io_uring-sq");
 		} else {
 			ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
Signature · 1 line
2.17.1
#7 lore raw

Re: [PATCH v2 2/2] io_uring: Potential Oops in io_sq_offload_start()

The io_uring patches are slated for v5.7 so we should figure out a solution for this bug.

regrds,
dan carpenter

#8 lore raw

Re: [PATCH v2 2/2] io_uring: Potential Oops in io_sq_offload_start()

Quoted 3 lines · Dan Carpenter

The io_uring patches are slated for v5.7 so we should figure out a solution for this bug.

Never mind. We merged a different fix. 975554b03edd ("io_uring: fix SQPOLL cpu validation").

regards,
dan carpenter