[RESEND PATCH v4 1/5] sysctl: add helper functions to extract table->extra1/extra2

From: Wen Yang
Date: Tue Oct 08 2024 - 11:15:09 EST


Add some sysctl helper functions to avoid direct access to
table->extra1/extra2.

Signed-off-by: Wen Yang <wen.yang@xxxxxxxxx>
Cc: Luis Chamberlain <mcgrof@xxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Cc: Joel Granados <j.granados@xxxxxxxxxxx>
Cc: Eric W. Biederman <ebiederm@xxxxxxxxxxxx>
Cc: Christian Brauner <brauner@xxxxxxxxxx>
Cc: Dave Young <dyoung@xxxxxxxxxx>
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
fs/proc/proc_sysctl.c | 21 +++++++++-----------
include/linux/sysctl.h | 44 ++++++++++++++++++++++++++++++++++++++++++
kernel/sysctl.c | 20 +++++++++----------
3 files changed, 63 insertions(+), 22 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index fac566065ed5..90c99eb1abf6 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -1115,18 +1115,15 @@ static int sysctl_check_table_array(const char *path, const struct ctl_table *ta
if (table->maxlen != sizeof(u8))
err |= sysctl_err(path, table, "array not allowed");

- if (table->extra1) {
- extra = *(unsigned int *) table->extra1;
- if (extra > 255U)
- err |= sysctl_err(path, table,
- "range value too large for proc_dou8vec_minmax");
- }
- if (table->extra2) {
- extra = *(unsigned int *) table->extra2;
- if (extra > 255U)
- err |= sysctl_err(path, table,
- "range value too large for proc_dou8vec_minmax");
- }
+ extra = sysctl_range_min_u8(table);
+ if (extra > 255U)
+ err |= sysctl_err(path, table,
+ "range value too large for proc_dou8vec_minmax\n");
+
+ extra = sysctl_range_max_u8(table);
+ if (extra > 255U)
+ err |= sysctl_err(path, table,
+ "range value too large for proc_dou8vec_minmax\n");
}

if (table->proc_handler == proc_dobool) {
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 202855befa8b..edfcb22b1e3d 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -212,6 +212,50 @@ struct ctl_table_root {
#define register_sysctl(path, table) \
register_sysctl_sz(path, table, ARRAY_SIZE(table))

+#define __SYSCTL_RANGE_MIN(_a, _b, _c) (((_a)->extra1) ? *(_b((_a)->extra1)) : (_c))
+
+#define __SYSCTL_RANGE_MAX(_a, _b, _c) (((_a)->extra2) ? *(_b((_a)->extra2)) : (_c))
+
+static inline unsigned int sysctl_range_min_u8(const struct ctl_table *table)
+{
+ return (unsigned int)__SYSCTL_RANGE_MIN(table, (unsigned int *), 0);
+}
+
+static inline unsigned int sysctl_range_max_u8(const struct ctl_table *table)
+{
+ return (unsigned int)__SYSCTL_RANGE_MAX(table, (unsigned int *), U8_MAX);
+}
+
+static inline int sysctl_range_min_int(const struct ctl_table *table)
+{
+ return (int)__SYSCTL_RANGE_MIN(table, (int *), INT_MIN);
+}
+
+static inline int sysctl_range_max_int(const struct ctl_table *table)
+{
+ return (int)__SYSCTL_RANGE_MAX(table, (int *), INT_MAX);
+}
+
+static inline unsigned int sysctl_range_min_uint(const struct ctl_table *table)
+{
+ return (unsigned int)__SYSCTL_RANGE_MIN(table, (unsigned int *), 0);
+}
+
+static inline unsigned int sysctl_range_max_uint(const struct ctl_table *table)
+{
+ return (unsigned int)__SYSCTL_RANGE_MAX(table, (unsigned int *), UINT_MAX);
+}
+
+static inline unsigned long sysctl_range_min_ulong(const struct ctl_table *table)
+{
+ return (unsigned long)__SYSCTL_RANGE_MIN(table, (unsigned long *), 0);
+}
+
+static inline unsigned long sysctl_range_max_ulong(const struct ctl_table *table)
+{
+ return (unsigned long)__SYSCTL_RANGE_MAX(table, (unsigned long *), ULONG_MAX);
+}
+
#ifdef CONFIG_SYSCTL

void proc_sys_poll_notify(struct ctl_table_poll *poll);
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 92305cdbb94a..86de15638e31 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -869,8 +869,8 @@ int proc_dointvec_minmax(const struct ctl_table *table, int write,
{
struct proc_minmax_conv_param param;

- param.min = (table->extra1) ? *(int *) table->extra1 : INT_MIN;
- param.max = (table->extra2) ? *(int *) table->extra2 : INT_MAX;
+ param.min = sysctl_range_min_int(table);
+ param.max = sysctl_range_max_int(table);
return do_proc_dointvec(table, write, buffer, lenp, ppos,
do_proc_dointvec_minmax_conv, &param);
}
@@ -923,8 +923,8 @@ int proc_douintvec_minmax(const struct ctl_table *table, int write,
{
struct proc_minmax_conv_param param;

- param.min = (table->extra1) ? *(unsigned int *) table->extra1 : 0;
- param.max = (table->extra2) ? *(unsigned int *) table->extra2 : UINT_MAX;
+ param.min = sysctl_range_min_uint(table);
+ param.max = sysctl_range_max_uint(table);
return do_proc_douintvec(table, write, buffer, lenp, ppos,
do_proc_douintvec_minmax_conv, &param);
}
@@ -959,8 +959,8 @@ int proc_dou8vec_minmax(const struct ctl_table *table, int write,
if (table->maxlen != sizeof(u8))
return -EINVAL;

- param.min = (table->extra1) ? *(unsigned int *) table->extra1 : 0;
- param.max = (table->extra2) ? *(unsigned int *) table->extra2 : 255U;
+ param.min = sysctl_range_min_u8(table);
+ param.max = sysctl_range_max_u8(table);
tmp = *table;

tmp.maxlen = sizeof(val);
@@ -1012,8 +1012,8 @@ static int __do_proc_doulongvec_minmax(void *data,
}

i = data;
- min = (table->extra1) ? *(unsigned long *) table->extra1 : 0;
- max = (table->extra2) ? *(unsigned long *) table->extra2 : ULONG_MAX;
+ min = sysctl_range_min_ulong(table);
+ max = sysctl_range_max_ulong(table);

vleft = table->maxlen / sizeof(unsigned long);
left = *lenp;
@@ -1250,8 +1250,8 @@ int proc_dointvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
{
struct proc_minmax_conv_param param;

- param.min = (table->extra1) ? *(int *) table->extra1 : INT_MIN;
- param.max = (table->extra2) ? *(int *) table->extra2 : INT_MAX;
+ param.min = sysctl_range_min_int(table);
+ param.max = sysctl_range_max_int(table);
return do_proc_dointvec(table, write, buffer, lenp, ppos,
do_proc_dointvec_ms_jiffies_minmax_conv, &param);
}
--
2.25.1