Skip to contentSkip to conversation

proc/sysctl: add shared variables for range check

PATCH Superseded Matteo Croce 3 participants 5 messages Apr 17, 2019

Subsystems (29)
Networking DRM drivers x86 VFS +25 more subsystems

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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)\b' |wc -l
    245

This patch adds three const variables for the most commonly used values, and use them instead of creating a local one for every object file.

---
 arch/s390/appldata/appldata_base.c            |  15 +-
 arch/s390/kernel/topology.c                   |   6 +-
 arch/x86/entry/vdso/vdso32-setup.c            |   7 +-
 arch/x86/kernel/itmt.c                        |   6 +-
 drivers/base/firmware_loader/fallback_table.c |  11 +-
 drivers/gpu/drm/i915/i915_perf.c              |   8 +-
 drivers/hv/vmbus_drv.c                        |   6 +-
 drivers/s390/char/sclp_async.c                |   7 +-
 drivers/tty/tty_ldisc.c                       |   6 +-
 drivers/xen/balloon.c                         |   7 +-
 fs/eventpoll.c                                |   3 +-
 fs/notify/inotify/inotify_user.c              |   8 +-
 fs/proc/proc_sysctl.c                         |   5 +
 include/linux/sysctl.h                        |   4 +
 ipc/ipc_sysctl.c                              |  35 ++--
 kernel/pid_namespace.c                        |   3 +-
 kernel/sysctl.c                               | 193 +++++++++---------
 kernel/ucount.c                               |   6 +-
 net/core/neighbour.c                          |  20 +-
 net/core/sysctl_net_core.c                    |  34 ++-
 net/dccp/sysctl.c                             |  16 +-
 net/ipv4/sysctl_net_ipv4.c                    |  58 +++---
 net/ipv6/addrconf.c                           |   6 +-
 net/ipv6/route.c                              |   7 +-
 net/ipv6/sysctl_net_ipv6.c                    |   8 +-
 net/mpls/af_mpls.c                            |  10 +-
 net/netfilter/ipvs/ip_vs_ctl.c                |   3 +-
 net/rxrpc/sysctl.c                            |   9 +-
 net/sctp/sysctl.c                             |  35 ++--
 net/sunrpc/xprtrdma/transport.c               |   3 +-
 security/keys/sysctl.c                        |  26 ++-
 security/loadpin/loadpin.c                    |   6 +-
 security/yama/yama_lsm.c                      |   3 +-
 33 files changed, 261 insertions(+), 319 deletions(-)
arch/s390/appldata/appldata_base.c +5 -10
index e4b58240ec53..82ae75b5ead6 100644
@@ -220,15 +220,13 @@ appldata_timer_handler(struct ctl_table *ctl, int write,
 			   void __user *buffer, size_t *lenp, loff_t *ppos)
 {
 	int timer_active = appldata_timer_active;
-	int zero = 0;
-	int one = 1;
 	int rc;
 	struct ctl_table ctl_entry = {
 		.procname	= ctl->procname,
 		.data		= &timer_active,
 		.maxlen		= sizeof(int),
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	};
 
 	rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
@@ -255,13 +253,12 @@ appldata_interval_handler(struct ctl_table *ctl, int write,
 			   void __user *buffer, size_t *lenp, loff_t *ppos)
 {
 	int interval = appldata_interval;
-	int one = 1;
 	int rc;
 	struct ctl_table ctl_entry = {
 		.procname	= ctl->procname,
 		.data		= &interval,
 		.maxlen		= sizeof(int),
-		.extra1		= &one,
+		.extra1		= (void *)&sysctl_one,
 	};
 
 	rc = proc_dointvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
@@ -289,13 +286,11 @@ appldata_generic_handler(struct ctl_table *ctl, int write,
 	struct list_head *lh;
 	int rc, found;
 	int active;
-	int zero = 0;
-	int one = 1;
 	struct ctl_table ctl_entry = {
 		.data		= &active,
 		.maxlen		= sizeof(int),
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	};
 
 	found = 0;
arch/s390/kernel/topology.c +2 -4
index 8964a3f60aad..347eb433c0a5 100644
@@ -587,15 +587,13 @@ static int topology_ctl_handler(struct ctl_table *ctl, int write,
 {
 	int enabled = topology_is_enabled();
 	int new_mode;
-	int zero = 0;
-	int one = 1;
 	int rc;
 	struct ctl_table ctl_entry = {
 		.procname	= ctl->procname,
 		.data		= &enabled,
 		.maxlen		= sizeof(int),
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	};
 
 	rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
arch/x86/entry/vdso/vdso32-setup.c +2 -5
index 42d4c89f990e..c7cdd8bfa3e9 100644
@@ -65,9 +65,6 @@ subsys_initcall(sysenter_setup);
 /* Register vsyscall32 into the ABI table */
 #include <linux/sysctl.h>
 
-static const int zero;
-static const int one = 1;
-
 static struct ctl_table abi_table2[] = {
 	{
 		.procname	= "vsyscall32",
@@ -75,8 +72,8 @@ static struct ctl_table abi_table2[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= (int *)&zero,
-		.extra2		= (int *)&one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{}
 };
arch/x86/kernel/itmt.c +2 -4
index d177940aa090..54fc00adba00 100644
@@ -69,8 +69,6 @@ static int sched_itmt_update_handler(struct ctl_table *table, int write,
 	return ret;
 }
 
-static unsigned int zero;
-static unsigned int one = 1;
 static struct ctl_table itmt_kern_table[] = {
 	{
 		.procname	= "sched_itmt_enabled",
@@ -78,8 +76,8 @@ static struct ctl_table itmt_kern_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= sched_itmt_update_handler,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{}
 };
drivers/base/firmware_loader/fallback_table.c +4 -7
index 776dd69cf5be..5632c0a23a23 100644
@@ -16,9 +16,6 @@
  * firmware fallback configuration table
  */
 
-static unsigned int zero;
-static unsigned int one = 1;
-
 struct firmware_fallback_config fw_fallback_config = {
 	.force_sysfs_fallback = IS_ENABLED(CONFIG_FW_LOADER_USER_HELPER_FALLBACK),
 	.loading_timeout = 60,
@@ -33,8 +30,8 @@ struct ctl_table firmware_config_table[] = {
 		.maxlen         = sizeof(unsigned int),
 		.mode           = 0644,
 		.proc_handler   = proc_douintvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{
 		.procname	= "ignore_sysfs_fallback",
@@ -42,8 +39,8 @@ struct ctl_table firmware_config_table[] = {
 		.maxlen         = sizeof(unsigned int),
 		.mode           = 0644,
 		.proc_handler   = proc_douintvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{ }
 };
drivers/gpu/drm/i915/i915_perf.c +3 -5
index 9ebf99f3d8d3..dab38ad50804 100644
@@ -271,8 +271,6 @@
 #define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
 
 /* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
-static int zero;
-static int one = 1;
 static u32 i915_perf_stream_paranoid = true;
 
 /* The maximum exponent the hardware accepts is 63 (essentially it selects one
@@ -3345,8 +3343,8 @@ static struct ctl_table oa_table[] = {
 	 .maxlen = sizeof(i915_perf_stream_paranoid),
 	 .mode = 0644,
 	 .proc_handler = proc_dointvec_minmax,
-	 .extra1 = &zero,
-	 .extra2 = &one,
+	 .extra1 = (void *)&sysctl_zero,
+	 .extra2 = (void *)&sysctl_one,
 	 },
 	{
 	 .procname = "oa_max_sample_rate",
@@ -3354,7 +3352,7 @@ static struct ctl_table oa_table[] = {
 	 .maxlen = sizeof(i915_oa_max_sample_rate),
 	 .mode = 0644,
 	 .proc_handler = proc_dointvec_minmax,
-	 .extra1 = &zero,
+	 .extra1 = (void *)&sysctl_zero,
 	 .extra2 = &oa_sample_rate_hard_limit,
 	 },
 	{}
drivers/hv/vmbus_drv.c +2 -4
index 000b53e5a17a..53e11b7a7fff 100644
@@ -1190,8 +1190,6 @@ static struct kmsg_dumper hv_kmsg_dumper = {
 };
 
 static struct ctl_table_header *hv_ctl_table_hdr;
-static int zero;
-static int one = 1;
 
 /*
  * sysctl option to allow the user to control whether kmsg data should be
@@ -1204,8 +1202,8 @@ static struct ctl_table hv_ctl_table[] = {
 		.maxlen         = sizeof(int),
 		.mode           = 0644,
 		.proc_handler   = proc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &one
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one
 	},
 	{}
 };
drivers/s390/char/sclp_async.c +2 -5
index e69b12a40636..1a04b064ad57 100644
@@ -64,9 +64,6 @@ static struct notifier_block call_home_panic_nb = {
 	.priority = INT_MAX,
 };
 
-static int zero;
-static int one = 1;
-
 static struct ctl_table callhome_table[] = {
 	{
 		.procname	= "callhome",
@@ -74,8 +71,8 @@ static struct ctl_table callhome_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{}
 };
drivers/tty/tty_ldisc.c +2 -4
index e38f104db174..8ae085bf5587 100644
@@ -855,8 +855,6 @@ void tty_ldisc_deinit(struct tty_struct *tty)
 	tty->ldisc = NULL;
 }
 
-static int zero;
-static int one = 1;
 static struct ctl_table tty_table[] = {
 	{
 		.procname	= "ldisc_autoload",
@@ -864,8 +862,8 @@ static struct ctl_table tty_table[] = {
 		.maxlen		= sizeof(tty_ldisc_autoload),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{ }
 };
drivers/xen/balloon.c +2 -5
index d37dd5bb7a8f..915894b47136 100644
@@ -77,9 +77,6 @@ static int xen_hotplug_unpopulated;
 
 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
 
-static int zero;
-static int one = 1;
-
 static struct ctl_table balloon_table[] = {
 	{
 		.procname	= "hotplug_unpopulated",
@@ -87,8 +84,8 @@ static struct ctl_table balloon_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1         = &zero,
-		.extra2         = &one,
+		.extra1         = (void *)&sysctl_zero,
+		.extra2         = (void *)&sysctl_one,
 	},
 	{ }
 };
fs/eventpoll.c +1 -2
index 4a0e98d87fcc..c8391c574394 100644
@@ -296,7 +296,6 @@ static LIST_HEAD(tfile_check_list);
 
 #include <linux/sysctl.h>
 
-static long zero;
 static long long_max = LONG_MAX;
 
 struct ctl_table epoll_table[] = {
@@ -306,7 +305,7 @@ struct ctl_table epoll_table[] = {
 		.maxlen		= sizeof(max_user_watches),
 		.mode		= 0644,
 		.proc_handler	= proc_doulongvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &long_max,
 	},
 	{ }
fs/notify/inotify/inotify_user.c +3 -5
index 7b53598c8804..cc6164260c76 100644
@@ -54,8 +54,6 @@ struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
 
 #include <linux/sysctl.h>
 
-static int zero;
-
 struct ctl_table inotify_table[] = {
 	{
 		.procname	= "max_user_instances",
@@ -63,7 +61,7 @@ struct ctl_table inotify_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 	},
 	{
 		.procname	= "max_user_watches",
@@ -71,7 +69,7 @@ struct ctl_table inotify_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 	},
 	{
 		.procname	= "max_queued_events",
@@ -79,7 +77,7 @@ struct ctl_table inotify_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero
+		.extra1		= (void *)&sysctl_zero
 	},
 	{ }
 };
fs/proc/proc_sysctl.c +5 -0
index d65390727541..e7a96169fced 100644
@@ -21,6 +21,11 @@ static const struct inode_operations proc_sys_inode_operations;
 static const struct file_operations proc_sys_dir_file_operations;
 static const struct inode_operations proc_sys_dir_operations;
 
+/* shared constants to be used in various sysctls */
+const int sysctl_zero = 0;
+const int sysctl_one = 1;
+const int sysctl_int_max = INT_MAX;
+
 /* Support for permanently empty directories */
 
 struct ctl_table sysctl_mount_point[] = {
include/linux/sysctl.h +4 -0
index b769ecfcc3bd..f3b191799747 100644
@@ -37,6 +37,10 @@ struct ctl_table_root;
 struct ctl_table_header;
 struct ctl_dir;
 
+extern const int sysctl_zero;
+extern const int sysctl_one;
+extern const int sysctl_int_max;
+
 typedef int proc_handler (struct ctl_table *ctl, int write,
 			  void __user *buffer, size_t *lenp, loff_t *ppos);
 
ipc/ipc_sysctl.c +16 -19
index 49f9bf4ffc7f..238f08235588 100644
@@ -117,9 +117,6 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,
 #define proc_ipc_sem_dointvec	   NULL
 #endif
 
-static int zero;
-static int one = 1;
-static int int_max = INT_MAX;
 static int ipc_mni = IPCMNI;
 
 static struct ctl_table ipc_kern_table[] = {
@@ -143,7 +140,7 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(init_ipc_ns.shm_ctlmni),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &ipc_mni,
 	},
 	{
@@ -152,8 +149,8 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(init_ipc_ns.shm_rmid_forced),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax_orphans,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{
 		.procname	= "msgmax",
@@ -161,8 +158,8 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(init_ipc_ns.msg_ctlmax),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &int_max,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "msgmni",
@@ -170,7 +167,7 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(init_ipc_ns.msg_ctlmni),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &ipc_mni,
 	},
 	{
@@ -179,8 +176,8 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_auto_msgmni,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{
 		.procname	=  "msgmnb",
@@ -188,8 +185,8 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(init_ipc_ns.msg_ctlmnb),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &int_max,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "sem",
@@ -205,8 +202,8 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &int_max,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "msg_next_id",
@@ -214,8 +211,8 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &int_max,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "shm_next_id",
@@ -223,8 +220,8 @@ static struct ctl_table ipc_kern_table[] = {
 		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
 		.mode		= 0644,
 		.proc_handler	= proc_ipc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &int_max,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 #endif
 	{}
kernel/pid_namespace.c +1 -2
index aa6e72fb7c08..ddbb51bc4968 100644
@@ -290,14 +290,13 @@ static int pid_ns_ctl_handler(struct ctl_table *table, int write,
 }
 
 extern int pid_max;
-static int zero = 0;
 static struct ctl_table pid_ns_ctl_table[] = {
 	{
 		.procname = "ns_last_pid",
 		.maxlen = sizeof(int),
 		.mode = 0666, /* permissions are checked in the handler */
 		.proc_handler = pid_ns_ctl_handler,
-		.extra1 = &zero,
+		.extra1 = (void *)&sysctl_zero,
 		.extra2 = &pid_max,
 	},
 	{ }
kernel/sysctl.c +95 -98

595 lines not on this page · raw message

kernel/ucount.c +2 -4
index f48d1b6376a4..ba7b8282d299 100644
@@ -57,16 +57,14 @@ static struct ctl_table_root set_root = {
 	.permissions = set_permissions,
 };
 
-static int zero = 0;
-static int int_max = INT_MAX;
 #define UCOUNT_ENTRY(name)				\
 	{						\
 		.procname	= name,			\
 		.maxlen		= sizeof(int),		\
 		.mode		= 0644,			\
 		.proc_handler	= proc_dointvec_minmax,	\
-		.extra1		= &zero,		\
-		.extra2		= &int_max,		\
+		.extra1		= (void *)&sysctl_zero,		\
+		.extra2		= (void *)&sysctl_int_max,	\
 	}
 static struct ctl_table user_table[] = {
 	UCOUNT_ENTRY("max_user_namespaces"),
net/core/neighbour.c +9 -11
index 30f6fd8f68e0..0668e87c091c 100644
@@ -3352,8 +3352,6 @@ void neigh_app_ns(struct neighbour *n)
 EXPORT_SYMBOL(neigh_app_ns);
 
 #ifdef CONFIG_SYSCTL
-static int zero;
-static int int_max = INT_MAX;
 static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
 
 static int proc_unres_qlen(struct ctl_table *ctl, int write,
@@ -3362,7 +3360,7 @@ static int proc_unres_qlen(struct ctl_table *ctl, int write,
 	int size, ret;
 	struct ctl_table tmp = *ctl;
 
-	tmp.extra1 = &zero;
+	tmp.extra1 = (void *)&sysctl_zero;
 	tmp.extra2 = &unres_qlen_max;
 	tmp.data = &size;
 
@@ -3427,8 +3425,8 @@ static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
 	struct ctl_table tmp = *ctl;
 	int ret;
 
-	tmp.extra1 = &zero;
-	tmp.extra2 = &int_max;
+	tmp.extra1 = (void *)&sysctl_zero;
+	tmp.extra2 = (void *)&sysctl_int_max;
 
 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
 	neigh_proc_update(ctl, write);
@@ -3573,24 +3571,24 @@ static struct neigh_sysctl_table {
 			.procname	= "gc_thresh1",
 			.maxlen		= sizeof(int),
 			.mode		= 0644,
-			.extra1 	= &zero,
-			.extra2		= &int_max,
+			.extra1 	= (void *)&sysctl_zero,
+			.extra2		= (void *)&sysctl_int_max,
 			.proc_handler	= proc_dointvec_minmax,
 		},
 		[NEIGH_VAR_GC_THRESH2] = {
 			.procname	= "gc_thresh2",
 			.maxlen		= sizeof(int),
 			.mode		= 0644,
-			.extra1 	= &zero,
-			.extra2		= &int_max,
+			.extra1 	= (void *)&sysctl_zero,
+			.extra2		= (void *)&sysctl_int_max,
 			.proc_handler	= proc_dointvec_minmax,
 		},
 		[NEIGH_VAR_GC_THRESH3] = {
 			.procname	= "gc_thresh3",
 			.maxlen		= sizeof(int),
 			.mode		= 0644,
-			.extra1 	= &zero,
-			.extra2		= &int_max,
+			.extra1 	= (void *)&sysctl_zero,
+			.extra2		= (void *)&sysctl_int_max,
 			.proc_handler	= proc_dointvec_minmax,
 		},
 		{},
net/core/sysctl_net_core.c +16 -18
index 84bf2861f45f..5b9b81bfaca4 100644
@@ -22,8 +22,6 @@
 #include <net/busy_poll.h>
 #include <net/pkt_sched.h>
 
-static int zero = 0;
-static int one = 1;
 static int two __maybe_unused = 2;
 static int min_sndbuf = SOCK_MIN_SNDBUF;
 static int min_rcvbuf = SOCK_MIN_RCVBUF;
@@ -390,10 +388,10 @@ static struct ctl_table net_core_table[] = {
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax_bpf_enable,
 # ifdef CONFIG_BPF_JIT_ALWAYS_ON
-		.extra1		= &one,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_one,
+		.extra2		= (void *)&sysctl_one,
 # else
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &two,
 # endif
 	},
@@ -404,7 +402,7 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0600,
 		.proc_handler	= proc_dointvec_minmax_bpf_restricted,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &two,
 	},
 	{
@@ -413,8 +411,8 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0600,
 		.proc_handler	= proc_dointvec_minmax_bpf_restricted,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 # endif
 	{
@@ -461,8 +459,8 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &one
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one
 	},
 #ifdef CONFIG_RPS
 	{
@@ -493,7 +491,7 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 	},
 	{
 		.procname	= "busy_read",
@@ -501,7 +499,7 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 	},
 #endif
 #ifdef CONFIG_NET_SCHED
@@ -533,7 +531,7 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &one,
+		.extra1		= (void *)&sysctl_one,
 		.extra2		= &max_skb_frags,
 	},
 	{
@@ -542,7 +540,7 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 	},
 	{
 		.procname	= "fb_tunnels_only_for_init_net",
@@ -550,8 +548,8 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{
 		.procname	= "devconf_inherit_init_net",
@@ -559,7 +557,7 @@ static struct ctl_table net_core_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &two,
 	},
 	{ }
@@ -571,7 +569,7 @@ static struct ctl_table netns_core_table[] = {
 		.data		= &init_net.core.sysctl_somaxconn,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.proc_handler	= proc_dointvec_minmax
 	},
 	{ }
net/dccp/sysctl.c +7 -9
index 53731e45403c..2b3c427f4861 100644
@@ -19,9 +19,7 @@
 #endif
 
 /* Boundary values */
-static int		zero     = 0,
-			one      = 1,
-			u8_max   = 0xFF;
+static int		u8_max   = 0xFF;
 static unsigned long	seqw_min = DCCPF_SEQ_WMIN,
 			seqw_max = 0xFFFFFFFF;		/* maximum on 32 bit */
 
@@ -41,7 +39,7 @@ static struct ctl_table dccp_default_table[] = {
 		.maxlen		= sizeof(sysctl_dccp_rx_ccid),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &u8_max,		/* RFC 4340, 10. */
 	},
 	{
@@ -50,7 +48,7 @@ static struct ctl_table dccp_default_table[] = {
 		.maxlen		= sizeof(sysctl_dccp_tx_ccid),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &u8_max,		/* RFC 4340, 10. */
 	},
 	{
@@ -59,7 +57,7 @@ static struct ctl_table dccp_default_table[] = {
 		.maxlen		= sizeof(sysctl_dccp_request_retries),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &one,
+		.extra1		= (void *)&sysctl_one,
 		.extra2		= &u8_max,
 	},
 	{
@@ -68,7 +66,7 @@ static struct ctl_table dccp_default_table[] = {
 		.maxlen		= sizeof(sysctl_dccp_retries1),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &u8_max,
 	},
 	{
@@ -77,7 +75,7 @@ static struct ctl_table dccp_default_table[] = {
 		.maxlen		= sizeof(sysctl_dccp_retries2),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &u8_max,
 	},
 	{
@@ -86,7 +84,7 @@ static struct ctl_table dccp_default_table[] = {
 		.maxlen		= sizeof(sysctl_dccp_tx_qlen),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 	},
 	{
 		.procname	= "sync_ratelimit",
net/ipv4/sysctl_net_ipv4.c +28 -30

190 lines not on this page · raw message

net/ipv6/addrconf.c +2 -4
index 4ae17a966ae3..40f7125de64c 100644
@@ -6417,8 +6417,6 @@ int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
 }
 
 static int minus_one = -1;
-static const int zero = 0;
-static const int one = 1;
 static const int two_five_five = 255;
 
 static const struct ctl_table addrconf_sysctl[] = {
@@ -6435,7 +6433,7 @@ static const struct ctl_table addrconf_sysctl[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= (void *)&one,
+		.extra1		= (void *)(void *)&sysctl_one,
 		.extra2		= (void *)&two_five_five,
 	},
 	{
@@ -6794,7 +6792,7 @@ static const struct ctl_table addrconf_sysctl[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= (void *)&zero,
+		.extra1		= (void *)(void *)&sysctl_zero,
 		.extra2		= (void *)&two_five_five,
 	},
 	{
net/ipv6/route.c +2 -5
index 0302e0eb07af..5d2e4d407dc0 100644
@@ -5143,9 +5143,6 @@ int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
 	return 0;
 }
 
-static int zero;
-static int one = 1;
-
 static struct ctl_table ipv6_route_table_template[] = {
 	{
 		.procname	=	"flush",
@@ -5223,8 +5220,8 @@ static struct ctl_table ipv6_route_table_template[] = {
 		.maxlen		=	sizeof(int),
 		.mode		=	0644,
 		.proc_handler	=	proc_dointvec,
-		.extra1		=	&zero,
-		.extra2		=	&one,
+		.extra1		=	(void *)&sysctl_zero,
+		.extra2		=	(void *)&sysctl_one,
 	},
 	{ }
 };
net/ipv6/sysctl_net_ipv6.c +3 -5
index e15cd37024fd..b1e51369bd07 100644
@@ -21,8 +21,6 @@
 #include <net/calipso.h>
 #endif
 
-static int zero;
-static int one = 1;
 static int auto_flowlabels_min;
 static int auto_flowlabels_max = IP6_AUTO_FLOW_LABEL_MAX;
 
@@ -149,8 +147,8 @@ static struct ctl_table ipv6_table_template[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler   = proc_rt6_multipath_hash_policy,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{
 		.procname	= "seg6_flowlabel",
@@ -176,7 +174,7 @@ static struct ctl_table ipv6_rotable[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &one
+		.extra1		= (void *)&sysctl_one
 	},
 #ifdef CONFIG_NETLABEL
 	{
net/mpls/af_mpls.c +4 -6
index f7c544592ec8..4600a3176f49 100644
@@ -36,8 +36,6 @@
 
 #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
 
-static int zero = 0;
-static int one = 1;
 static int label_limit = (1 << 20) - 1;
 static int ttl_max = 255;
 
@@ -2604,7 +2602,7 @@ static int mpls_platform_labels(struct ctl_table *table, int write,
 		.data		= &platform_labels,
 		.maxlen		= sizeof(int),
 		.mode		= table->mode,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &label_limit,
 	};
 
@@ -2633,8 +2631,8 @@ static const struct ctl_table mpls_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,
 	},
 	{
 		.procname	= "default_ttl",
@@ -2642,7 +2640,7 @@ static const struct ctl_table mpls_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &one,
+		.extra1		= (void *)&sysctl_one,
 		.extra2		= &ttl_max,
 	},
 	{ }
net/netfilter/ipvs/ip_vs_ctl.c +1 -2
index 053cd96b9c76..f5dfdddaa6bc 100644
@@ -1655,7 +1655,6 @@ static int ip_vs_zero_all(struct netns_ipvs *ipvs)
 
 #ifdef CONFIG_SYSCTL
 
-static int zero;
 static int three = 3;
 
 static int
@@ -1864,7 +1863,7 @@ static struct ctl_table vs_vars[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &three,
 	},
 	{
net/rxrpc/sysctl.c +4 -5
index d75bd15151e6..d3deb5e90ab3 100644
@@ -15,7 +15,6 @@
 #include "ar-internal.h"
 
 static struct ctl_table_header *rxrpc_sysctl_reg_table;
-static const unsigned int one = 1;
 static const unsigned int four = 4;
 static const unsigned int thirtytwo = 32;
 static const unsigned int n_65535 = 65535;
@@ -101,7 +100,7 @@ static struct ctl_table rxrpc_sysctl_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= (void *)&one,
+		.extra1		= (void *)(void *)&sysctl_one,
 		.extra2		= (void *)&rxrpc_max_client_connections,
 	},
 	{
@@ -119,7 +118,7 @@ static struct ctl_table rxrpc_sysctl_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= (void *)&one,
+		.extra1		= (void *)(void *)&sysctl_one,
 		.extra2		= (void *)&n_max_acks,
 	},
 	{
@@ -128,7 +127,7 @@ static struct ctl_table rxrpc_sysctl_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= (void *)&one,
+		.extra1		= (void *)(void *)&sysctl_one,
 		.extra2		= (void *)&n_65535,
 	},
 	{
@@ -137,7 +136,7 @@ static struct ctl_table rxrpc_sysctl_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= (void *)&one,
+		.extra1		= (void *)(void *)&sysctl_one,
 		.extra2		= (void *)&four,
 	},
 
net/sctp/sysctl.c +16 -19
index 33ca5b73cdb3..f2734d904c2e 100644
@@ -40,10 +40,7 @@
 #include <net/sctp/sctp.h>
 #include <linux/sysctl.h>
 
-static int zero = 0;
-static int one = 1;
 static int timer_max = 86400000; /* ms in one day */
-static int int_max = INT_MAX;
 static int sack_timer_min = 1;
 static int sack_timer_max = 500;
 static int addr_scope_max = SCTP_SCOPE_POLICY_MAX;
@@ -107,7 +104,7 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1         = &one,
+		.extra1         = (void *)&sysctl_one,
 		.extra2         = &timer_max
 	},
 	{
@@ -116,7 +113,7 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_sctp_do_rto_min,
-		.extra1         = &one,
+		.extra1         = (void *)&sysctl_one,
 		.extra2         = &init_net.sctp.rto_max
 	},
 	{
@@ -152,8 +149,8 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &int_max
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "cookie_preserve_enable",
@@ -175,7 +172,7 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1         = &one,
+		.extra1         = (void *)&sysctl_one,
 		.extra2         = &timer_max
 	},
 	{
@@ -193,7 +190,7 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1         = &one,
+		.extra1         = (void *)&sysctl_one,
 		.extra2         = &timer_max
 	},
 	{
@@ -202,8 +199,8 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &one,
-		.extra2		= &int_max
+		.extra1		= (void *)&sysctl_one,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "path_max_retrans",
@@ -211,8 +208,8 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &one,
-		.extra2		= &int_max
+		.extra1		= (void *)&sysctl_one,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "max_init_retransmits",
@@ -220,8 +217,8 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &one,
-		.extra2		= &int_max
+		.extra1		= (void *)&sysctl_one,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "pf_retrans",
@@ -229,8 +226,8 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
-		.extra2		= &int_max
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_int_max,
 	},
 	{
 		.procname	= "sndbuf_policy",
@@ -301,7 +298,7 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &addr_scope_max,
 	},
 	{
@@ -310,7 +307,7 @@ static struct ctl_table sctp_net_table[] = {
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
 		.proc_handler	= &proc_dointvec_minmax,
-		.extra1		= &one,
+		.extra1		= (void *)&sysctl_one,
 		.extra2		= &rwnd_scale_max,
 	},
 	{
net/sunrpc/xprtrdma/transport.c +1 -2
index 5d261353bd90..f8632ff19659 100644
@@ -80,7 +80,6 @@ static unsigned int min_slot_table_size = RPCRDMA_MIN_SLOT_TABLE;
 static unsigned int max_slot_table_size = RPCRDMA_MAX_SLOT_TABLE;
 static unsigned int min_inline_size = RPCRDMA_MIN_INLINE;
 static unsigned int max_inline_size = RPCRDMA_MAX_INLINE;
-static unsigned int zero;
 static unsigned int max_padding = PAGE_SIZE;
 static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS;
 static unsigned int max_memreg = RPCRDMA_LAST - 1;
@@ -122,7 +121,7 @@ static struct ctl_table xr_tunables_table[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &zero,
+		.extra1		= (void *)&sysctl_zero,
 		.extra2		= &max_padding,
 	},
 	{
security/keys/sysctl.c +12 -14
index b68faa1a5cfd..366dde373a04 100644
@@ -13,8 +13,6 @@
 #include <linux/sysctl.h>
 #include "internal.h"
 
-static const int zero, one = 1, max = INT_MAX;
-
 struct ctl_table key_sysctls[] = {
 	{
 		.procname = "maxkeys",
@@ -22,8 +20,8 @@ struct ctl_table key_sysctls[] = {
 		.maxlen = sizeof(unsigned),
 		.mode = 0644,
 		.proc_handler = proc_dointvec_minmax,
-		.extra1 = (void *) &one,
-		.extra2 = (void *) &max,
+		.extra1 = (void *) &sysctl_one,
+		.extra2 = (void *) &sysctl_int_max,
 	},
 	{
 		.procname = "maxbytes",
@@ -31,8 +29,8 @@ struct ctl_table key_sysctls[] = {
 		.maxlen = sizeof(unsigned),
 		.mode = 0644,
 		.proc_handler = proc_dointvec_minmax,
-		.extra1 = (void *) &one,
-		.extra2 = (void *) &max,
+		.extra1 = (void *) &sysctl_one,
+		.extra2 = (void *) &sysctl_int_max,
 	},
 	{
 		.procname = "root_maxkeys",
@@ -40,8 +38,8 @@ struct ctl_table key_sysctls[] = {
 		.maxlen = sizeof(unsigned),
 		.mode = 0644,
 		.proc_handler = proc_dointvec_minmax,
-		.extra1 = (void *) &one,
-		.extra2 = (void *) &max,
+		.extra1 = (void *) &sysctl_one,
+		.extra2 = (void *) &sysctl_int_max,
 	},
 	{
 		.procname = "root_maxbytes",
@@ -49,8 +47,8 @@ struct ctl_table key_sysctls[] = {
 		.maxlen = sizeof(unsigned),
 		.mode = 0644,
 		.proc_handler = proc_dointvec_minmax,
-		.extra1 = (void *) &one,
-		.extra2 = (void *) &max,
+		.extra1 = (void *) &sysctl_one,
+		.extra2 = (void *) &sysctl_int_max,
 	},
 	{
 		.procname = "gc_delay",
@@ -58,8 +56,8 @@ struct ctl_table key_sysctls[] = {
 		.maxlen = sizeof(unsigned),
 		.mode = 0644,
 		.proc_handler = proc_dointvec_minmax,
-		.extra1 = (void *) &zero,
-		.extra2 = (void *) &max,
+		.extra1 = (void *) &sysctl_zero,
+		.extra2 = (void *) &sysctl_int_max,
 	},
 #ifdef CONFIG_PERSISTENT_KEYRINGS
 	{
@@ -68,8 +66,8 @@ struct ctl_table key_sysctls[] = {
 		.maxlen = sizeof(unsigned),
 		.mode = 0644,
 		.proc_handler = proc_dointvec_minmax,
-		.extra1 = (void *) &zero,
-		.extra2 = (void *) &max,
+		.extra1 = (void *) &sysctl_zero,
+		.extra2 = (void *) &sysctl_int_max,
 	},
 #endif
 	{ }
security/loadpin/loadpin.c +2 -4
index 055fb0a64169..e5cb1fcae497 100644
@@ -49,8 +49,6 @@ static struct super_block *pinned_root;
 static DEFINE_SPINLOCK(pinned_root_spinlock);
 
 #ifdef CONFIG_SYSCTL
-static int zero;
-static int one = 1;
 
 static struct ctl_path loadpin_sysctl_path[] = {
 	{ .procname = "kernel", },
@@ -65,8 +63,8 @@ static struct ctl_table loadpin_sysctl_table[] = {
 		.maxlen         = sizeof(int),
 		.mode           = 0644,
 		.proc_handler   = proc_dointvec_minmax,
-		.extra1         = &zero,
-		.extra2         = &one,
+		.extra1         = (void *)&sysctl_zero,
+		.extra2         = (void *)&sysctl_one,
 	},
 	{ }
 };
security/yama/yama_lsm.c +1 -2
index efac68556b45..036125ecbaad 100644
@@ -449,7 +449,6 @@ static int yama_dointvec_minmax(struct ctl_table *table, int write,
 	return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
 }
 
-static int zero;
 static int max_scope = YAMA_SCOPE_NO_ATTACH;
 
 static struct ctl_path yama_sysctl_path[] = {
@@ -465,7 +464,7 @@ static struct ctl_table yama_sysctl_table[] = {
 		.maxlen         = sizeof(int),
 		.mode           = 0644,
 		.proc_handler   = yama_dointvec_minmax,
-		.extra1         = &zero,
+		.extra1         = (void *)&sysctl_zero,
 		.extra2         = &max_scope,
 	},
 	{ }
Signature · 1 line
2.20.1
#1 lore raw
Quoted 13 lines · On Wed, 17 Apr 2019 02:59:43 +0200 Matteo Croce <[email protected]> wrote:

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:

Quoted 6 lines
    $ git grep -E '\.extra[12].*&(zero|one|int_max)\b' |wc -l
    245

This patch adds three const variables for the most commonly used values, and use them instead of creating a local one for every object file.

Nice. A few thoughts:

Quoted 7 lines
arch/s390/appldata/appldata_base.c +0 -1
@@ -220,15 +220,13 @@ appldata_timer_handler(struct ctl_table *ctl, int write,
 			   void __user *buffer, size_t *lenp, loff_t *ppos)
 {
 	int timer_active = appldata_timer_active;
-	int zero = 0;

arch/s390/appldata/appldata_base.c appldata_timer_handler(struct ctl_table *ctl, int write, · line 229

Quoted 10 lines
arch/s390/appldata/appldata_base.c +2 -3
-	int one = 1;
 	int rc;
 	struct ctl_table ctl_entry = {
 		.procname	= ctl->procname,
 		.data		= &timer_active,
 		.maxlen		= sizeof(int),
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,

The casts are ugly, and by casting away constness they introduce the risk that some errant could could change the value of 0, 1 and INT_MAX! Maybe - perhaps trying to do that would cause a segv but still, they're ugly.

A proper fix would require changing extra1 and extra2 to const void *. Perhaps that would be unfeasibly extensive?

fs/proc/proc_sysctl.c static const struct inode_operations proc_sys_inode_operations; · line 27

Quoted 12 lines

...

fs/proc/proc_sysctl.c +4 -0
@@ -21,6 +21,11 @@ static const struct inode_operations proc_sys_inode_operations;
 static const struct file_operations proc_sys_dir_file_operations;
 static const struct inode_operations proc_sys_dir_operations;
 
+/* shared constants to be used in various sysctls */
+const int sysctl_zero = 0;
+const int sysctl_one = 1;
+const int sysctl_int_max = INT_MAX;

Don't these require EXPORT_SYMBOL()?

#2 lore raw
Quoted 58 lines · Andrew Morton
Quoted 1 line · On Wed, 17 Apr 2019 02:59:43 +0200 Matteo Croce <[email protected]> wrote:

In the sysctl code the proc_dointvec_minmax() function is often used

to

Quoted 1 line

validate the user supplied value between an allowed range. This

function

Quoted 1 line

uses the extra1 and extra2 members from struct ctl_table as minimum

and

Quoted 3 lines

maximum allowed value.

On sysctl handler declaration, in every source file there are some

readonly

Quoted 1 line

variables containing just an integer which address is assigned to

the

Quoted 3 lines

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,

Quoted 1 line

leading duplication of variables like zero=0, one=1, int_max=INT_MAX

in

Quoted 6 lines

different source files:

    $ git grep -E '\.extra[12].*&(zero|one|int_max)\b' |wc -l
    245

This patch adds three const variables for the most commonly used

values,

Quoted 2 lines

and use them instead of creating a local one for every object file.

Nice. A few thoughts:

arch/s390/appldata/appldata_base.c appldata_timer_handler(struct ctl_table *ctl,

Quoted 3 lines
arch/s390/appldata/appldata_base.c +0 -0
@@ -220,15 +220,13 @@ appldata_timer_handler(struct ctl_table *ctl,

int write,

Quoted 4 lines
arch/s390/appldata/appldata_base.c +0 -1
 			   void __user *buffer, size_t *lenp, loff_t *ppos)
 {
 	int timer_active = appldata_timer_active;
-	int zero = 0;

arch/s390/appldata/appldata_base.c appldata_timer_handler(struct ctl_table *ctl, int write, · line 229

Quoted 10 lines
arch/s390/appldata/appldata_base.c +2 -3
-	int one = 1;
 	int rc;
 	struct ctl_table ctl_entry = {
 		.procname	= ctl->procname,
 		.data		= &timer_active,
 		.maxlen		= sizeof(int),
-		.extra1		= &zero,
-		.extra2		= &one,
+		.extra1		= (void *)&sysctl_zero,
+		.extra2		= (void *)&sysctl_one,

The casts are ugly, and by casting away constness they introduce the risk that some errant could could change the value of 0, 1 and INT_MAX!
Maybe - perhaps trying to do that would cause a segv but still, they're ugly.

Quoted 4 lines

A proper fix would require changing extra1 and extra2 to const void *.

Perhaps that would be unfeasibly extensive?

Hi Andrew,

I agree that the casts are ugly, but the "casts discards const qualifier" is way more ugly, so I have no choice.

I though about declaring extra1,2 as const, I quickly checked for code which write into these pointers and I found none, but I only looked for one, two and int_max values.

We could do a deeper search to see if other values are safe to turn to const.

Quoted 9 lines

fs/proc/proc_sysctl.c static const struct inode_operations

Quoted 5 lines

...

fs/proc/proc_sysctl.c +0 -0
@@ -21,6 +21,11 @@ static const struct inode_operations

proc_sys_inode_operations;

Quoted 3 lines
fs/proc/proc_sysctl.c +0 -0
 static const struct file_operations proc_sys_dir_file_operations;
 static const struct inode_operations proc_sys_dir_operations;
 
Quoted 6 lines

fs/proc/proc_sysctl.c static const struct inode_operations proc_sys_inode_operations; · line 27

Quoted 4 lines
fs/proc/proc_sysctl.c +4 -0
+/* shared constants to be used in various sysctls */
+const int sysctl_zero = 0;
+const int sysctl_one = 1;
+const int sysctl_int_max = INT_MAX;

Don't these require EXPORT_SYMBOL()?

Yes, for kernel modules, as the kbuild bot just pointed out.

Regards,

Signature · 2 lines
Matteo Croce
per aspera ad upstream
#3 lore raw
In reply to Matteo Croce
Thank you for the patch! Yet something to improve:

Hi Matteo,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.1-rc5]
[cannot apply to next-20190416]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Matteo-Croce/proc-sysctl-add-shared-variables-for-range-check/20190417-105810
config: x86_64-randconfig-m0-04170939 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

ERROR: "sysctl_zero" [net/sctp/sctp.ko] undefined!
ERROR: "sysctl_one" [net/sctp/sctp.ko] undefined!
ERROR: "sysctl_int_max" [net/sctp/sctp.ko] undefined!

>> ERROR: "sysctl_one" [net/rxrpc/rxrpc.ko] undefined!
>> ERROR: "sysctl_zero" [net/ipv6/ipv6.ko] undefined!
>> ERROR: "sysctl_one" [net/ipv6/ipv6.ko] undefined!

make[2]: *** [__modpost] Error 1
make[2]: Target '_modpost' not remade because of errors. make[1]: *** [modules] Error 2
make[1]: Target '_all' not remade because of errors.

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw
In reply to Matteo Croce
Thank you for the patch! Yet something to improve:

Hi Matteo,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.1-rc5]
[cannot apply to next-20190416]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Matteo-Croce/proc-sysctl-add-shared-variables-for-range-check/20190417-105810
config: i386-randconfig-m1-04170939 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

ERROR: "sysctl_zero" [net/sctp/sctp.ko] undefined!
ERROR: "sysctl_one" [net/sctp/sctp.ko] undefined!
ERROR: "sysctl_int_max" [net/sctp/sctp.ko] undefined!

>> ERROR: "sysctl_zero" [net/mpls/mpls_router.ko] undefined!
>> ERROR: "sysctl_one" [net/mpls/mpls_router.ko] undefined!

ERROR: "sysctl_zero" [net/ipv6/ipv6.ko] undefined!
ERROR: "sysctl_one" [net/ipv6/ipv6.ko] undefined!
ERROR: "sysctl_zero" [drivers/gpu/drm/i915/i915.ko] undefined!
ERROR: "sysctl_one" [drivers/gpu/drm/i915/i915.ko] undefined!

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation