Skip to content

Commit 61d9b56

Browse files
mcgroftorvalds
authored andcommitted
sysctl: add unsigned int range support
To keep parity with regular int interfaces provide the an unsigned int proc_douintvec_minmax() which allows you to specify a range of allowed valid numbers. Adding proc_douintvec_minmax_sysadmin() is easy but we can wait for an actual user for that. Link: http://lkml.kernel.org/r/20170519033554.18592-6-mcgrof@kernel.org Signed-off-by: Luis R. Rodriguez <mcgrof@kernel.org> Acked-by: Kees Cook <keescook@chromium.org> Cc: Subash Abhinov Kasiviswanathan <subashab@codeaurora.org> Cc: Heinrich Schuchardt <xypron.glpk@gmx.de> Cc: Kees Cook <keescook@chromium.org> Cc: "David S. Miller" <davem@davemloft.net> Cc: Ingo Molnar <mingo@redhat.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4f2fec0 commit 61d9b56

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎fs/proc/proc_sysctl.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,8 @@ static int sysctl_check_table_array(const char *path, struct ctl_table *table)
10651065
{
10661066
int err = 0;
10671067

1068-
if (table->proc_handler == proc_douintvec) {
1068+
if ((table->proc_handler == proc_douintvec) ||
1069+
(table->proc_handler == proc_douintvec_minmax)) {
10691070
if (table->maxlen != sizeof(unsigned int))
10701071
err |= sysctl_err(path, table, "array now allowed");
10711072
}
@@ -1083,6 +1084,7 @@ static int sysctl_check_table(const char *path, struct ctl_table *table)
10831084
if ((table->proc_handler == proc_dostring) ||
10841085
(table->proc_handler == proc_dointvec) ||
10851086
(table->proc_handler == proc_douintvec) ||
1087+
(table->proc_handler == proc_douintvec_minmax) ||
10861088
(table->proc_handler == proc_dointvec_minmax) ||
10871089
(table->proc_handler == proc_dointvec_jiffies) ||
10881090
(table->proc_handler == proc_dointvec_userhz_jiffies) ||

‎include/linux/sysctl.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ extern int proc_douintvec(struct ctl_table *, int,
4747
void __user *, size_t *, loff_t *);
4848
extern int proc_dointvec_minmax(struct ctl_table *, int,
4949
void __user *, size_t *, loff_t *);
50+
extern int proc_douintvec_minmax(struct ctl_table *table, int write,
51+
void __user *buffer, size_t *lenp,
52+
loff_t *ppos);
5053
extern int proc_dointvec_jiffies(struct ctl_table *, int,
5154
void __user *, size_t *, loff_t *);
5255
extern int proc_dointvec_userhz_jiffies(struct ctl_table *, int,

‎kernel/sysctl.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,65 @@ int proc_dointvec_minmax(struct ctl_table *table, int write,
25672567
do_proc_dointvec_minmax_conv, &param);
25682568
}
25692569

2570+
struct do_proc_douintvec_minmax_conv_param {
2571+
unsigned int *min;
2572+
unsigned int *max;
2573+
};
2574+
2575+
static int do_proc_douintvec_minmax_conv(unsigned long *lvalp,
2576+
unsigned int *valp,
2577+
int write, void *data)
2578+
{
2579+
struct do_proc_douintvec_minmax_conv_param *param = data;
2580+
2581+
if (write) {
2582+
unsigned int val = *lvalp;
2583+
2584+
if ((param->min && *param->min > val) ||
2585+
(param->max && *param->max < val))
2586+
return -ERANGE;
2587+
2588+
if (*lvalp > UINT_MAX)
2589+
return -EINVAL;
2590+
*valp = val;
2591+
} else {
2592+
unsigned int val = *valp;
2593+
*lvalp = (unsigned long) val;
2594+
}
2595+
2596+
return 0;
2597+
}
2598+
2599+
/**
2600+
* proc_douintvec_minmax - read a vector of unsigned ints with min/max values
2601+
* @table: the sysctl table
2602+
* @write: %TRUE if this is a write to the sysctl file
2603+
* @buffer: the user buffer
2604+
* @lenp: the size of the user buffer
2605+
* @ppos: file position
2606+
*
2607+
* Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
2608+
* values from/to the user buffer, treated as an ASCII string. Negative
2609+
* strings are not allowed.
2610+
*
2611+
* This routine will ensure the values are within the range specified by
2612+
* table->extra1 (min) and table->extra2 (max). There is a final sanity
2613+
* check for UINT_MAX to avoid having to support wrap around uses from
2614+
* userspace.
2615+
*
2616+
* Returns 0 on success.
2617+
*/
2618+
int proc_douintvec_minmax(struct ctl_table *table, int write,
2619+
void __user *buffer, size_t *lenp, loff_t *ppos)
2620+
{
2621+
struct do_proc_douintvec_minmax_conv_param param = {
2622+
.min = (unsigned int *) table->extra1,
2623+
.max = (unsigned int *) table->extra2,
2624+
};
2625+
return do_proc_douintvec(table, write, buffer, lenp, ppos,
2626+
do_proc_douintvec_minmax_conv, &param);
2627+
}
2628+
25702629
static void validate_coredump_safety(void)
25712630
{
25722631
#ifdef CONFIG_COREDUMP
@@ -3066,6 +3125,12 @@ int proc_dointvec_minmax(struct ctl_table *table, int write,
30663125
return -ENOSYS;
30673126
}
30683127

3128+
int proc_douintvec_minmax(struct ctl_table *table, int write,
3129+
void __user *buffer, size_t *lenp, loff_t *ppos)
3130+
{
3131+
return -ENOSYS;
3132+
}
3133+
30693134
int proc_dointvec_jiffies(struct ctl_table *table, int write,
30703135
void __user *buffer, size_t *lenp, loff_t *ppos)
30713136
{
@@ -3108,6 +3173,7 @@ EXPORT_SYMBOL(proc_dointvec);
31083173
EXPORT_SYMBOL(proc_douintvec);
31093174
EXPORT_SYMBOL(proc_dointvec_jiffies);
31103175
EXPORT_SYMBOL(proc_dointvec_minmax);
3176+
EXPORT_SYMBOL_GPL(proc_douintvec_minmax);
31113177
EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
31123178
EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
31133179
EXPORT_SYMBOL(proc_dostring);

0 commit comments

Comments
 (0)