[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <364936c4a83abcddabb08504b4e26a7b92daef68.1726365007.git.wen.yang@linux.dev>
Date: Sun, 15 Sep 2024 10:08:27 +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>,
linux-kernel@...r.kernel.org,
Wen Yang <wen.yang@...ux.dev>,
Dave Young <dyoung@...hat.com>
Subject: [PATCH v3 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: 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 | 40 ++++++++++++++++++++++++++++++++++++++++
kernel/sysctl.c | 20 ++++++++++----------
3 files changed, 59 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..20e3914ec53f 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -212,6 +212,46 @@ struct ctl_table_root {
#define register_sysctl(path, table) \
register_sysctl_sz(path, table, ARRAY_SIZE(table))
+static inline unsigned int sysctl_range_min_u8(const struct ctl_table *table)
+{
+ return (table->extra1) ? *(unsigned int *) table->extra1 : 0;
+}
+
+static inline unsigned int sysctl_range_max_u8(const struct ctl_table *table)
+{
+ return (table->extra2) ? *(unsigned int *) table->extra2 : U8_MAX;
+}
+
+static inline int sysctl_range_min_int(const struct ctl_table *table)
+{
+ return (table->extra1) ? *(int *) table->extra1 : INT_MIN;
+}
+
+static inline int sysctl_range_max_int(const struct ctl_table *table)
+{
+ return (table->extra2) ? *(int *) table->extra2 : INT_MAX;
+}
+
+static inline unsigned int sysctl_range_min_uint(const struct ctl_table *table)
+{
+ return (table->extra1) ? *(unsigned int *) table->extra1 : 0;
+}
+
+static inline unsigned int sysctl_range_max_uint(const struct ctl_table *table)
+{
+ return (table->extra2) ? *(unsigned int *) table->extra2 : UINT_MAX;
+}
+
+static inline unsigned long sysctl_range_min_ulong(const struct ctl_table *table)
+{
+ return (table->extra1) ? *(unsigned long *) table->extra1 : 0;
+}
+
+static inline unsigned long sysctl_range_max_ulong(const struct ctl_table *table)
+{
+ return (table->extra2) ? *(unsigned long *) table->extra2 : 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