[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <0f4a5233b75e6484a6236d85d2b506c96ea41ef1.1726910671.git.wen.yang@linux.dev>
Date: Sat, 21 Sep 2024 17:29:00 +0800
From: Wen Yang <wen.yang@...ux.dev>
To: "Eric W . Biederman" <ebiederm@...ssion.com>,
Luis Chamberlain <mcgrof@...nel.org>,
Kees Cook <keescook@...omium.org>,
Joel Granados <j.granados@...sung.com>
Cc: Christian Brauner <brauner@...nel.org>,
Thomas Weißschuh <thomas@...ch.de>,
linux-kernel@...r.kernel.org,
Wen Yang <wen.yang@...ux.dev>,
Dave Young <dyoung@...hat.com>
Subject: [PATCH v4 1/5] sysctl: add helper functions to extract table->extra1/extra2
Add some sysctl helper functions to avoid direct access to
table->extra1/extra2.
Signed-off-by: Wen Yang <wen.yang@...ux.dev>
Cc: Luis Chamberlain <mcgrof@...nel.org>
Cc: Kees Cook <keescook@...omium.org>
Cc: Joel Granados <j.granados@...sung.com>
Cc: Thomas Weißschuh <thomas@...ch.de>
Cc: Eric W. Biederman <ebiederm@...ssion.com>
Cc: Christian Brauner <brauner@...nel.org>
Cc: Dave Young <dyoung@...hat.com>
Cc: linux-kernel@...r.kernel.org
---
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 d11ebc055ce0..3177e229ec46 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -1109,18 +1109,15 @@ static int sysctl_check_table_array(const char *path, struct ctl_table *table)
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 aa4c6d44aaa0..48f907850c87 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, ¶m);
}
@@ -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, ¶m);
}
@@ -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, ¶m);
}
--
2.25.1
Powered by blists - more mailing lists