Skip to content

Commit eec4844

Browse files
teknoravertorvalds
authored andcommitted
proc/sysctl: add shared variables for range check
In the sysctl code the proc_dointvec_minmax() function is often used to validate the user supplied value between an allowed range. This function uses the extra1 and extra2 members from struct ctl_table as minimum and maximum allowed value. On sysctl handler declaration, in every source file there are some readonly variables containing just an integer which address is assigned to the extra1 and extra2 members, so the sysctl range is enforced. The special values 0, 1 and INT_MAX are very often used as range boundary, leading duplication of variables like zero=0, one=1, int_max=INT_MAX in different source files: $ git grep -E '\.extra[12].*&(zero|one|int_max)' |wc -l 248 Add a const int array containing the most commonly used values, some macros to refer more easily to the correct array member, and use them instead of creating a local one for every object file. This is the bloat-o-meter output comparing the old and new binary compiled with the default Fedora config: # scripts/bloat-o-meter -d vmlinux.o.old vmlinux.o add/remove: 2/2 grow/shrink: 0/2 up/down: 24/-188 (-164) Data old new delta sysctl_vals - 12 +12 __kstrtab_sysctl_vals - 12 +12 max 14 10 -4 int_max 16 - -16 one 68 - -68 zero 128 28 -100 Total: Before=20583249, After=20583085, chg -0.00% [[email protected]: tipc: remove two unused variables] Link: http://lkml.kernel.org/r/[email protected] [[email protected]: fix net/ipv6/sysctl_net_ipv6.c] [[email protected]: proc/sysctl: make firmware loader table conditional] Link: http://lkml.kernel.org/r/[email protected] [[email protected]: fix fs/eventpoll.c] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Matteo Croce <[email protected]> Signed-off-by: Arnd Bergmann <[email protected]> Acked-by: Kees Cook <[email protected]> Reviewed-by: Aaron Tomlin <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Stephen Rothwell <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3710969 commit eec4844

File tree

33 files changed

+270
-322
lines changed

33 files changed

+270
-322
lines changed

arch/s390/appldata/appldata_base.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,13 @@ appldata_timer_handler(struct ctl_table *ctl, int write,
220220
void __user *buffer, size_t *lenp, loff_t *ppos)
221221
{
222222
int timer_active = appldata_timer_active;
223-
int zero = 0;
224-
int one = 1;
225223
int rc;
226224
struct ctl_table ctl_entry = {
227225
.procname = ctl->procname,
228226
.data = &timer_active,
229227
.maxlen = sizeof(int),
230-
.extra1 = &zero,
231-
.extra2 = &one,
228+
.extra1 = SYSCTL_ZERO,
229+
.extra2 = SYSCTL_ONE,
232230
};
233231

234232
rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
@@ -255,13 +253,12 @@ appldata_interval_handler(struct ctl_table *ctl, int write,
255253
void __user *buffer, size_t *lenp, loff_t *ppos)
256254
{
257255
int interval = appldata_interval;
258-
int one = 1;
259256
int rc;
260257
struct ctl_table ctl_entry = {
261258
.procname = ctl->procname,
262259
.data = &interval,
263260
.maxlen = sizeof(int),
264-
.extra1 = &one,
261+
.extra1 = SYSCTL_ONE,
265262
};
266263

267264
rc = proc_dointvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
@@ -289,13 +286,11 @@ appldata_generic_handler(struct ctl_table *ctl, int write,
289286
struct list_head *lh;
290287
int rc, found;
291288
int active;
292-
int zero = 0;
293-
int one = 1;
294289
struct ctl_table ctl_entry = {
295290
.data = &active,
296291
.maxlen = sizeof(int),
297-
.extra1 = &zero,
298-
.extra2 = &one,
292+
.extra1 = SYSCTL_ZERO,
293+
.extra2 = SYSCTL_ONE,
299294
};
300295

301296
found = 0;

arch/s390/kernel/topology.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,13 @@ static int topology_ctl_handler(struct ctl_table *ctl, int write,
587587
{
588588
int enabled = topology_is_enabled();
589589
int new_mode;
590-
int zero = 0;
591-
int one = 1;
592590
int rc;
593591
struct ctl_table ctl_entry = {
594592
.procname = ctl->procname,
595593
.data = &enabled,
596594
.maxlen = sizeof(int),
597-
.extra1 = &zero,
598-
.extra2 = &one,
595+
.extra1 = SYSCTL_ZERO,
596+
.extra2 = SYSCTL_ONE,
599597
};
600598

601599
rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);

arch/x86/entry/vdso/vdso32-setup.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,15 @@ subsys_initcall(sysenter_setup);
6565
/* Register vsyscall32 into the ABI table */
6666
#include <linux/sysctl.h>
6767

68-
static const int zero;
69-
static const int one = 1;
70-
7168
static struct ctl_table abi_table2[] = {
7269
{
7370
.procname = "vsyscall32",
7471
.data = &vdso32_enabled,
7572
.maxlen = sizeof(int),
7673
.mode = 0644,
7774
.proc_handler = proc_dointvec_minmax,
78-
.extra1 = (int *)&zero,
79-
.extra2 = (int *)&one,
75+
.extra1 = SYSCTL_ZERO,
76+
.extra2 = SYSCTL_ONE,
8077
},
8178
{}
8279
};

arch/x86/kernel/itmt.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,15 @@ static int sched_itmt_update_handler(struct ctl_table *table, int write,
6565
return ret;
6666
}
6767

68-
static unsigned int zero;
69-
static unsigned int one = 1;
7068
static struct ctl_table itmt_kern_table[] = {
7169
{
7270
.procname = "sched_itmt_enabled",
7371
.data = &sysctl_sched_itmt_enabled,
7472
.maxlen = sizeof(unsigned int),
7573
.mode = 0644,
7674
.proc_handler = sched_itmt_update_handler,
77-
.extra1 = &zero,
78-
.extra2 = &one,
75+
.extra1 = SYSCTL_ZERO,
76+
.extra2 = SYSCTL_ONE,
7977
},
8078
{}
8179
};

drivers/base/firmware_loader/fallback_table.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,34 @@
1616
* firmware fallback configuration table
1717
*/
1818

19-
static unsigned int zero;
20-
static unsigned int one = 1;
21-
2219
struct firmware_fallback_config fw_fallback_config = {
2320
.force_sysfs_fallback = IS_ENABLED(CONFIG_FW_LOADER_USER_HELPER_FALLBACK),
2421
.loading_timeout = 60,
2522
.old_timeout = 60,
2623
};
2724
EXPORT_SYMBOL_GPL(fw_fallback_config);
2825

26+
#ifdef CONFIG_SYSCTL
2927
struct ctl_table firmware_config_table[] = {
3028
{
3129
.procname = "force_sysfs_fallback",
3230
.data = &fw_fallback_config.force_sysfs_fallback,
3331
.maxlen = sizeof(unsigned int),
3432
.mode = 0644,
3533
.proc_handler = proc_douintvec_minmax,
36-
.extra1 = &zero,
37-
.extra2 = &one,
34+
.extra1 = SYSCTL_ZERO,
35+
.extra2 = SYSCTL_ONE,
3836
},
3937
{
4038
.procname = "ignore_sysfs_fallback",
4139
.data = &fw_fallback_config.ignore_sysfs_fallback,
4240
.maxlen = sizeof(unsigned int),
4341
.mode = 0644,
4442
.proc_handler = proc_douintvec_minmax,
45-
.extra1 = &zero,
46-
.extra2 = &one,
43+
.extra1 = SYSCTL_ZERO,
44+
.extra2 = SYSCTL_ONE,
4745
},
4846
{ }
4947
};
5048
EXPORT_SYMBOL_GPL(firmware_config_table);
49+
#endif

drivers/gpu/drm/i915/i915_perf.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@
274274
#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
275275

276276
/* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
277-
static int zero;
278-
static int one = 1;
279277
static u32 i915_perf_stream_paranoid = true;
280278

281279
/* The maximum exponent the hardware accepts is 63 (essentially it selects one
@@ -3366,16 +3364,16 @@ static struct ctl_table oa_table[] = {
33663364
.maxlen = sizeof(i915_perf_stream_paranoid),
33673365
.mode = 0644,
33683366
.proc_handler = proc_dointvec_minmax,
3369-
.extra1 = &zero,
3370-
.extra2 = &one,
3367+
.extra1 = SYSCTL_ZERO,
3368+
.extra2 = SYSCTL_ONE,
33713369
},
33723370
{
33733371
.procname = "oa_max_sample_rate",
33743372
.data = &i915_oa_max_sample_rate,
33753373
.maxlen = sizeof(i915_oa_max_sample_rate),
33763374
.mode = 0644,
33773375
.proc_handler = proc_dointvec_minmax,
3378-
.extra1 = &zero,
3376+
.extra1 = SYSCTL_ZERO,
33793377
.extra2 = &oa_sample_rate_hard_limit,
33803378
},
33813379
{}

drivers/hv/vmbus_drv.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,6 @@ static struct kmsg_dumper hv_kmsg_dumper = {
11971197
};
11981198

11991199
static struct ctl_table_header *hv_ctl_table_hdr;
1200-
static int zero;
1201-
static int one = 1;
12021200

12031201
/*
12041202
* sysctl option to allow the user to control whether kmsg data should be
@@ -1211,8 +1209,8 @@ static struct ctl_table hv_ctl_table[] = {
12111209
.maxlen = sizeof(int),
12121210
.mode = 0644,
12131211
.proc_handler = proc_dointvec_minmax,
1214-
.extra1 = &zero,
1215-
.extra2 = &one
1212+
.extra1 = SYSCTL_ZERO,
1213+
.extra2 = SYSCTL_ONE
12161214
},
12171215
{}
12181216
};

drivers/tty/tty_ldisc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,17 +855,15 @@ void tty_ldisc_deinit(struct tty_struct *tty)
855855
tty->ldisc = NULL;
856856
}
857857

858-
static int zero;
859-
static int one = 1;
860858
static struct ctl_table tty_table[] = {
861859
{
862860
.procname = "ldisc_autoload",
863861
.data = &tty_ldisc_autoload,
864862
.maxlen = sizeof(tty_ldisc_autoload),
865863
.mode = 0644,
866864
.proc_handler = proc_dointvec,
867-
.extra1 = &zero,
868-
.extra2 = &one,
865+
.extra1 = SYSCTL_ZERO,
866+
.extra2 = SYSCTL_ONE,
869867
},
870868
{ }
871869
};

drivers/xen/balloon.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,15 @@ static int xen_hotplug_unpopulated;
7777

7878
#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
7979

80-
static int zero;
81-
static int one = 1;
82-
8380
static struct ctl_table balloon_table[] = {
8481
{
8582
.procname = "hotplug_unpopulated",
8683
.data = &xen_hotplug_unpopulated,
8784
.maxlen = sizeof(int),
8885
.mode = 0644,
8986
.proc_handler = proc_dointvec_minmax,
90-
.extra1 = &zero,
91-
.extra2 = &one,
87+
.extra1 = SYSCTL_ZERO,
88+
.extra2 = SYSCTL_ONE,
9289
},
9390
{ }
9491
};

fs/eventpoll.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static LIST_HEAD(tfile_check_list);
291291

292292
#include <linux/sysctl.h>
293293

294-
static long zero;
294+
static long long_zero;
295295
static long long_max = LONG_MAX;
296296

297297
struct ctl_table epoll_table[] = {
@@ -301,7 +301,7 @@ struct ctl_table epoll_table[] = {
301301
.maxlen = sizeof(max_user_watches),
302302
.mode = 0644,
303303
.proc_handler = proc_doulongvec_minmax,
304-
.extra1 = &zero,
304+
.extra1 = &long_zero,
305305
.extra2 = &long_max,
306306
},
307307
{ }

0 commit comments

Comments
 (0)