Skip to contentSkip to conversation

sysctl: add proc_dointvec_jiffies_minmax to limit the min/max write value

PATCH In review Zhiqiang Liu 3 participants 7 messages Apr 17, 2019 – Jul 2, 2019

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

PATCH 1/1 sysctl: add proc_dointvec_jiffies_minmax to limit the min/max write value

From: Zhiqiang Liu <[email protected]>

In proc_dointvec_jiffies func, the write value is only checked whether it is larger than INT_MAX. If the write value is less than zero, it can also be successfully writen in the data.

However, in some scenarios, users would adopt the data to set timers or check whether time is expired. Generally, the data will be cast to an unsigned type variable, then the negative data becomes a very large unsigned value, which leads to long waits or other unpredictable problems.

Here, we add a new func, proc_dointvec_jiffies_minmax, to limit the min/max write value, which is similar to the proc_dointvec_minmax func.

---
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 44 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/linux/sysctl.h +2 -0
index b769ecf..8bde8a0 100644
@@ -53,6 +53,8 @@ extern int proc_douintvec_minmax(struct ctl_table *table, int write,
 				 loff_t *ppos);
 extern int proc_dointvec_jiffies(struct ctl_table *, int,
 				 void __user *, size_t *, loff_t *);
+extern int proc_dointvec_jiffies_minmax(struct ctl_table *, int,
+				 void __user *, size_t *, loff_t *);
 extern int proc_dointvec_userhz_jiffies(struct ctl_table *, int,
 					void __user *, size_t *, loff_t *);
 extern int proc_dointvec_ms_jiffies(struct ctl_table *, int,
kernel/sysctl.c +43 -1

72 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

Friendly ping...

Quoted 117 lines

From: Zhiqiang Liu <[email protected]>

In proc_dointvec_jiffies func, the write value is only checked whether it is larger than INT_MAX. If the write value is less than zero, it can also be successfully writen in the data.

However, in some scenarios, users would adopt the data to set timers or check whether time is expired. Generally, the data will be cast to an unsigned type variable, then the negative data becomes a very large unsigned value, which leads to long waits or other unpredictable problems.

Here, we add a new func, proc_dointvec_jiffies_minmax, to limit the min/max write value, which is similar to the proc_dointvec_minmax func.

---
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 44 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/linux/sysctl.h +2 -0
index b769ecf..8bde8a0 100644
@@ -53,6 +53,8 @@ extern int proc_douintvec_minmax(struct ctl_table *table, int write,
 				 loff_t *ppos);
 extern int proc_dointvec_jiffies(struct ctl_table *, int,
 				 void __user *, size_t *, loff_t *);
+extern int proc_dointvec_jiffies_minmax(struct ctl_table *, int,
+				 void __user *, size_t *, loff_t *);
 extern int proc_dointvec_userhz_jiffies(struct ctl_table *, int,
 					void __user *, size_t *, loff_t *);
 extern int proc_dointvec_ms_jiffies(struct ctl_table *, int,
kernel/sysctl.c +43 -1
index c9ec050..8e1eb59 100644
@@ -2967,10 +2967,15 @@ static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,
 					 int *valp,
 					 int write, void *data)
 {
+	struct do_proc_dointvec_minmax_conv_param *param = data;
+
 	if (write) {
 		if (*lvalp > INT_MAX / HZ)
 			return 1;
 		*valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
+		if ((param->min && (*param->min)*HZ > *valp) ||
+		    (param->max && (*param->max)*HZ < *valp))
+			return -EINVAL;
 	} else {
 		int val = *valp;
 		unsigned long lval;
@@ -3053,7 +3058,37 @@ int proc_dointvec_jiffies(struct ctl_table *table, int write,
 			  void __user *buffer, size_t *lenp, loff_t *ppos)
 {
     return do_proc_dointvec(table,write,buffer,lenp,ppos,
-		    	    do_proc_dointvec_jiffies_conv,NULL);
+			    do_proc_dointvec_jiffies_conv, NULL);
+}
+
+/**
+ * proc_dointvec_jiffies_minmax - read a vector of integers as seconds with min/max values
+ * @table: the sysctl table
+ * @write: %TRUE if this is a write to the sysctl file
+ * @buffer: the user buffer
+ * @lenp: the size of the user buffer
+ * @ppos: file position
+ *
+ * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
+ * values from/to the user buffer, treated as an ASCII string.
+ * The values read are assumed to be in seconds, and are converted into
+ * jiffies.
+ *
+ * This routine will ensure the values are within the range specified by
+ * table->extra1 (min) and table->extra2 (max).
+ *
+ * Returns 0 on success or -EINVAL on write when the range check fails.
+ */
+int proc_dointvec_jiffies_minmax(struct ctl_table *table, int write,
+			  void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct do_proc_dointvec_minmax_conv_param param = {
+		.min = (int *) table->extra1,
+		.max = (int *) table->extra2,
+	};
+
+	return do_proc_dointvec(table, write, buffer, lenp, ppos,
+				do_proc_dointvec_jiffies_conv, &param);
 }
 
 /**
@@ -3301,6 +3336,12 @@ int proc_dointvec_jiffies(struct ctl_table *table, int write,
 	return -ENOSYS;
 }
 
+int proc_dointvec_jiffies_minmax(struct ctl_table *table, int write,
+		    void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	return -ENOSYS;
+}
+
 int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
 		    void __user *buffer, size_t *lenp, loff_t *ppos)
 {
@@ -3359,6 +3400,7 @@ static int proc_dointvec_minmax_bpf_stats(struct ctl_table *table, int write,
 EXPORT_SYMBOL(proc_dointvec);
 EXPORT_SYMBOL(proc_douintvec);
 EXPORT_SYMBOL(proc_dointvec_jiffies);
+EXPORT_SYMBOL(proc_dointvec_jiffies_minmax);
 EXPORT_SYMBOL(proc_dointvec_minmax);
 EXPORT_SYMBOL_GPL(proc_douintvec_minmax);
 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
#2 lore raw

Friendly ping...

Quoted 124 lines · Zhiqiang Liu

Friendly ping...

Quoted 110 lines

From: Zhiqiang Liu <[email protected]>

In proc_dointvec_jiffies func, the write value is only checked whether it is larger than INT_MAX. If the write value is less than zero, it can also be successfully writen in the data.

However, in some scenarios, users would adopt the data to set timers or check whether time is expired. Generally, the data will be cast to an unsigned type variable, then the negative data becomes a very large unsigned value, which leads to long waits or other unpredictable problems.

Here, we add a new func, proc_dointvec_jiffies_minmax, to limit the min/max write value, which is similar to the proc_dointvec_minmax func.

---
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 44 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
include/linux/sysctl.h +2 -0
index b769ecf..8bde8a0 100644
@@ -53,6 +53,8 @@ extern int proc_douintvec_minmax(struct ctl_table *table, int write,
 				 loff_t *ppos);
 extern int proc_dointvec_jiffies(struct ctl_table *, int,
 				 void __user *, size_t *, loff_t *);
+extern int proc_dointvec_jiffies_minmax(struct ctl_table *, int,
+				 void __user *, size_t *, loff_t *);
 extern int proc_dointvec_userhz_jiffies(struct ctl_table *, int,
 					void __user *, size_t *, loff_t *);
 extern int proc_dointvec_ms_jiffies(struct ctl_table *, int,
kernel/sysctl.c +42 -1
index c9ec050..8e1eb59 100644
@@ -2967,10 +2967,15 @@ static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,
 					 int *valp,
 					 int write, void *data)
 {
+	struct do_proc_dointvec_minmax_conv_param *param = data;
+
 	if (write) {
 		if (*lvalp > INT_MAX / HZ)
 			return 1;
 		*valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
+		if ((param->min && (*param->min)*HZ > *valp) ||
+		    (param->max && (*param->max)*HZ < *valp))
+			return -EINVAL;
 	} else {
 		int val = *valp;
 		unsigned long lval;
@@ -3053,7 +3058,37 @@ int proc_dointvec_jiffies(struct ctl_table *table, int write,
 			  void __user *buffer, size_t *lenp, loff_t *ppos)
 {
     return do_proc_dointvec(table,write,buffer,lenp,ppos,
-		    	    do_proc_dointvec_jiffies_conv,NULL);
+			    do_proc_dointvec_jiffies_conv, NULL);
+}
+
+/**
+ * proc_dointvec_jiffies_minmax - read a vector of integers as seconds with min/max values
+ * @table: the sysctl table
+ * @write: %TRUE if this is a write to the sysctl file
+ * @buffer: the user buffer
+ * @lenp: the size of the user buffer
+ * @ppos: file position
+ *
+ * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
+ * values from/to the user buffer, treated as an ASCII string.
+ * The values read are assumed to be in seconds, and are converted into
+ * jiffies.
+ *
+ * This routine will ensure the values are within the range specified by
+ * table->extra1 (min) and table->extra2 (max).
+ *
+ * Returns 0 on success or -EINVAL on write when the range check fails.
+ */
+int proc_dointvec_jiffies_minmax(struct ctl_table *table, int write,
+			  void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct do_proc_dointvec_minmax_conv_param param = {
+		.min = (int *) table->extra1,
+		.max = (int *) table->extra2,
+	};
+
+	return do_proc_dointvec(table, write, buffer, lenp, ppos,
+				do_proc_dointvec_jiffies_conv, &param);
 }
 
 /**
@@ -3301,6 +3336,12 @@ int proc_dointvec_jiffies(struct ctl_table *table, int write,
 	return -ENOSYS;
 }
 
+int proc_dointvec_jiffies_minmax(struct ctl_table *table, int write,
+		    void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	return -ENOSYS;
+}
+
 int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
 		    void __user *buffer, size_t *lenp, loff_t *ppos)
 {

kernel/sysctl.c static int proc_dointvec_minmax_bpf_stats(struct ctl_table *table, int write, · line 3406

Quoted 7 lines
kernel/sysctl.c +1 -0
@@ -3359,6 +3400,7 @@ static int proc_dointvec_minmax_bpf_stats(struct ctl_table *table, int write,
 EXPORT_SYMBOL(proc_dointvec);
 EXPORT_SYMBOL(proc_douintvec);
 EXPORT_SYMBOL(proc_dointvec_jiffies);
+EXPORT_SYMBOL(proc_dointvec_jiffies_minmax);
 EXPORT_SYMBOL(proc_dointvec_minmax);
 EXPORT_SYMBOL_GPL(proc_douintvec_minmax);
 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);

.

#3 lore raw
Quoted 5 lines · Zhiqiang Liu

Friendly ping...

Quoted 2 lines · Zhiqiang Liu

Friendly ping...

Hi!

(Please include akpm on CC for next versions of this, as he's likely the person to take this patch.)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

From: Zhiqiang Liu <[email protected]>

In proc_dointvec_jiffies func, the write value is only checked whether it is larger than INT_MAX. If the write value is less than zero, it can also be successfully writen in the data.

This appears to be "be design", but I see many "unsigned int" users that might be tricked into giant values... (for example, see net/netfilter/nf_conntrack_standalone.c)

Should proc_dointvec_jiffies() just be fixed to disallow negative values entirely? Looking at the implementation, it seems to be very intentional about accepting negative values.

However, when I looked through a handful of proc_dointvec_jiffies() users, it looks like they're all expecting a positive value. Many in the networking subsystem are, in fact, writing to unsigned long variables, as I mentioned.

Are there real-world cases of wanting to set a negative jiffie value via proc_dointvec_jiffies()?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

However, in some scenarios, users would adopt the data to set timers or check whether time is expired. Generally, the data will be cast to an unsigned type variable, then the negative data becomes a very large unsigned value, which leads to long waits or other unpredictable problems.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Here, we add a new func, proc_dointvec_jiffies_minmax, to limit the min/max write value, which is similar to the proc_dointvec_minmax func.

If proc_dointvec_jiffies() can't just be fixed, where will the new function get used? It seems all the "unsigned int" users could benefit.

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 4 lines · Zhiqiang Liu

(Please include akpm on CC for next versions of this, as he's likely the person to take this patch.)

Thanks for your advice. And sorry to reply you so late.

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In proc_dointvec_jiffies func, the write value is only checked whether it is larger than INT_MAX. If the write value is less than zero, it can also be successfully writen in the data.

This appears to be "be design", but I see many "unsigned int" users that might be tricked into giant values... (for example, see net/netfilter/nf_conntrack_standalone.c)

Quoted 9 lines

Should proc_dointvec_jiffies() just be fixed to disallow negative values entirely? Looking at the implementation, it seems to be very intentional about accepting negative values.

However, when I looked through a handful of proc_dointvec_jiffies() users, it looks like they're all expecting a positive value. Many in the networking subsystem are, in fact, writing to unsigned long variables, as I mentioned.

I totally agree with you. And I also cannot find an scenario that expects negative values. Consideing the "negative" scenario may be exist, I add the proc_dointvec_jiffies_minmax like proc_dointvec_minmax.

Quoted 2 lines

Are there real-world cases of wanting to set a negative jiffie value via proc_dointvec_jiffies()?

Until now, I do not find such cases.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Here, we add a new func, proc_dointvec_jiffies_minmax, to limit the min/max write value, which is similar to the proc_dointvec_minmax func.

If proc_dointvec_jiffies() can't just be fixed, where will the new function get used? It seems all the "unsigned int" users could benefit.

I tend to add the proc_dointvec_jiffies_minmax func to provide more choices and not change the previous use of proc_dointvec_jiffies func.

Thanks for your reply again.

#5 lore raw

friendly ping ...

Quoted 47 lines · Zhiqiang Liu
Quoted 4 lines · Zhiqiang Liu

(Please include akpm on CC for next versions of this, as he's likely the person to take this patch.)

Thanks for your advice. And sorry to reply you so late.

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In proc_dointvec_jiffies func, the write value is only checked whether it is larger than INT_MAX. If the write value is less than zero, it can also be successfully writen in the data.

This appears to be "be design", but I see many "unsigned int" users that might be tricked into giant values... (for example, see net/netfilter/nf_conntrack_standalone.c)

Quoted 9 lines

Should proc_dointvec_jiffies() just be fixed to disallow negative values entirely? Looking at the implementation, it seems to be very intentional about accepting negative values.

However, when I looked through a handful of proc_dointvec_jiffies() users, it looks like they're all expecting a positive value. Many in the networking subsystem are, in fact, writing to unsigned long variables, as I mentioned.

I totally agree with you. And I also cannot find an scenario that expects negative values. Consideing the "negative" scenario may be exist, I add the proc_dointvec_jiffies_minmax like proc_dointvec_minmax.

Quoted 2 lines

Are there real-world cases of wanting to set a negative jiffie value via proc_dointvec_jiffies()?

Until now, I do not find such cases.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Here, we add a new func, proc_dointvec_jiffies_minmax, to limit the min/max write value, which is similar to the proc_dointvec_minmax func.

If proc_dointvec_jiffies() can't just be fixed, where will the new function get used? It seems all the "unsigned int" users could benefit.

I tend to add the proc_dointvec_jiffies_minmax func to provide more choices and not change the previous use of proc_dointvec_jiffies func.

Thanks for your reply again.

.

#6 lore raw
In reply to Zhiqiang Liu
Quoted 13 lines · Zhiqiang Liu
Quoted 13 lines · Zhiqiang Liu
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In proc_dointvec_jiffies func, the write value is only checked whether it is larger than INT_MAX. If the write value is less than zero, it can also be successfully writen in the data.

This appears to be "be design", but I see many "unsigned int" users that might be tricked into giant values... (for example, see net/netfilter/nf_conntrack_standalone.c)

Should proc_dointvec_jiffies() just be fixed to disallow negative values entirely? Looking at the implementation, it seems to be very intentional about accepting negative values.

Quoted 8 lines
Quoted 5 lines

However, when I looked through a handful of proc_dointvec_jiffies() users, it looks like they're all expecting a positive value. Many in the networking subsystem are, in fact, writing to unsigned long variables, as I mentioned.

I totally agree with you. And I also cannot find an scenario that expects negative values. Consideing the "negative" scenario may be exist, I add the proc_dointvec_jiffies_minmax like proc_dointvec_minmax.

If no negative values exist, and there is no real point to it, then just rename the existing one and update the docs.

Luis