xref: /openbmc/linux/net/sysctl_net.c (revision 0db00e5d86dc793aab9722ad3728d99166eb7d96)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /* -*- linux-c -*-
31da177e4SLinus Torvalds  * sysctl_net.c: sysctl interface to net subsystem.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Begun April 1, 1996, Mike Shaver.
61da177e4SLinus Torvalds  * Added /proc/sys/net directories for each protocol family. [MS]
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Revision 1.2  1996/05/08  20:24:40  shaver
91da177e4SLinus Torvalds  * Added bits for NET_BRIDGE and the NET_IPV4_ARP stuff and
101da177e4SLinus Torvalds  * NET_IPV4_IP_FORWARD.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <linux/mm.h>
16bc3b2d7fSPaul Gortmaker #include <linux/export.h>
171da177e4SLinus Torvalds #include <linux/sysctl.h>
1895bdfccbSEric W. Biederman #include <linux/nsproxy.h>
191da177e4SLinus Torvalds 
20496a22b0SRussell King #include <net/sock.h>
21496a22b0SRussell King 
221da177e4SLinus Torvalds #ifdef CONFIG_INET
2320380731SArnaldo Carvalho de Melo #include <net/ip.h>
241da177e4SLinus Torvalds #endif
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #ifdef CONFIG_NET
2720380731SArnaldo Carvalho de Melo #include <linux/if_ether.h>
281da177e4SLinus Torvalds #endif
291da177e4SLinus Torvalds 
3073455092SAl Viro static struct ctl_table_set *
net_ctl_header_lookup(struct ctl_table_root * root)3113bcc6a2SEric W. Biederman net_ctl_header_lookup(struct ctl_table_root *root)
3295bdfccbSEric W. Biederman {
3313bcc6a2SEric W. Biederman 	return &current->nsproxy->net_ns->sysctls;
3473455092SAl Viro }
3573455092SAl Viro 
is_seen(struct ctl_table_set * set)3673455092SAl Viro static int is_seen(struct ctl_table_set *set)
3773455092SAl Viro {
3873455092SAl Viro 	return &current->nsproxy->net_ns->sysctls == set;
3995bdfccbSEric W. Biederman }
4095bdfccbSEric W. Biederman 
414ecb9009SStephen Hemminger /* Return standard mode bits for table entry. */
net_ctl_permissions(struct ctl_table_header * head,struct ctl_table * table)4273f7ef43SEric W. Biederman static int net_ctl_permissions(struct ctl_table_header *head,
434ecb9009SStephen Hemminger 			       struct ctl_table *table)
444ecb9009SStephen Hemminger {
45cff10976SEric W. Biederman 	struct net *net = container_of(head->set, struct net, sysctls);
46cff10976SEric W. Biederman 
474ecb9009SStephen Hemminger 	/* Allow network administrator to have same access as root. */
48d6169b02STyler Hicks 	if (ns_capable_noaudit(net->user_ns, CAP_NET_ADMIN)) {
494ecb9009SStephen Hemminger 		int mode = (table->mode >> 6) & 7;
504ecb9009SStephen Hemminger 		return (mode << 6) | (mode << 3) | mode;
514ecb9009SStephen Hemminger 	}
52e79c6a4fSDmitry Torokhov 
534ecb9009SStephen Hemminger 	return table->mode;
544ecb9009SStephen Hemminger }
554ecb9009SStephen Hemminger 
net_ctl_set_ownership(struct ctl_table_header * head,kuid_t * uid,kgid_t * gid)56e79c6a4fSDmitry Torokhov static void net_ctl_set_ownership(struct ctl_table_header *head,
57e79c6a4fSDmitry Torokhov 				  kuid_t *uid, kgid_t *gid)
58e79c6a4fSDmitry Torokhov {
59e79c6a4fSDmitry Torokhov 	struct net *net = container_of(head->set, struct net, sysctls);
60e79c6a4fSDmitry Torokhov 	kuid_t ns_root_uid;
61e79c6a4fSDmitry Torokhov 	kgid_t ns_root_gid;
62e79c6a4fSDmitry Torokhov 
63e79c6a4fSDmitry Torokhov 	ns_root_uid = make_kuid(net->user_ns, 0);
64e79c6a4fSDmitry Torokhov 	if (uid_valid(ns_root_uid))
65e79c6a4fSDmitry Torokhov 		*uid = ns_root_uid;
66e79c6a4fSDmitry Torokhov 
67e79c6a4fSDmitry Torokhov 	ns_root_gid = make_kgid(net->user_ns, 0);
68e79c6a4fSDmitry Torokhov 	if (gid_valid(ns_root_gid))
69e79c6a4fSDmitry Torokhov 		*gid = ns_root_gid;
70e79c6a4fSDmitry Torokhov }
71e79c6a4fSDmitry Torokhov 
7295bdfccbSEric W. Biederman static struct ctl_table_root net_sysctl_root = {
7395bdfccbSEric W. Biederman 	.lookup = net_ctl_header_lookup,
744ecb9009SStephen Hemminger 	.permissions = net_ctl_permissions,
75e79c6a4fSDmitry Torokhov 	.set_ownership = net_ctl_set_ownership,
7695bdfccbSEric W. Biederman };
7795bdfccbSEric W. Biederman 
sysctl_net_init(struct net * net)782c8c1e72SAlexey Dobriyan static int __net_init sysctl_net_init(struct net *net)
7995bdfccbSEric W. Biederman {
809eb47c26SEric W. Biederman 	setup_sysctl_set(&net->sysctls, &net_sysctl_root, is_seen);
8195bdfccbSEric W. Biederman 	return 0;
8295bdfccbSEric W. Biederman }
8395bdfccbSEric W. Biederman 
sysctl_net_exit(struct net * net)842c8c1e72SAlexey Dobriyan static void __net_exit sysctl_net_exit(struct net *net)
8595bdfccbSEric W. Biederman {
8697324cd8SEric W. Biederman 	retire_sysctl_set(&net->sysctls);
8795bdfccbSEric W. Biederman }
8895bdfccbSEric W. Biederman 
8995bdfccbSEric W. Biederman static struct pernet_operations sysctl_pernet_ops = {
9095bdfccbSEric W. Biederman 	.init = sysctl_net_init,
9195bdfccbSEric W. Biederman 	.exit = sysctl_net_exit,
9295bdfccbSEric W. Biederman };
9395bdfccbSEric W. Biederman 
94bc8a3694SEric W. Biederman static struct ctl_table_header *net_header;
net_sysctl_init(void)952ca794e5SEric W. Biederman __init int net_sysctl_init(void)
9695bdfccbSEric W. Biederman {
97bc8a3694SEric W. Biederman 	static struct ctl_table empty[1];
98bc8a3694SEric W. Biederman 	int ret = -ENOMEM;
99bc8a3694SEric W. Biederman 	/* Avoid limitations in the sysctl implementation by
100bc8a3694SEric W. Biederman 	 * registering "/proc/sys/net" as an empty directory not in a
101bc8a3694SEric W. Biederman 	 * network namespace.
102bc8a3694SEric W. Biederman 	 */
1039edbfe92SJoel Granados 	net_header = register_sysctl_sz("net", empty, 0);
104bc8a3694SEric W. Biederman 	if (!net_header)
105bc8a3694SEric W. Biederman 		goto out;
10695bdfccbSEric W. Biederman 	ret = register_pernet_subsys(&sysctl_pernet_ops);
10795bdfccbSEric W. Biederman 	if (ret)
108ce9d9b8eSLi RongQing 		goto out1;
10995bdfccbSEric W. Biederman out:
11095bdfccbSEric W. Biederman 	return ret;
111ce9d9b8eSLi RongQing out1:
112ce9d9b8eSLi RongQing 	unregister_sysctl_table(net_header);
113ce9d9b8eSLi RongQing 	net_header = NULL;
114ce9d9b8eSLi RongQing 	goto out;
11595bdfccbSEric W. Biederman }
11695bdfccbSEric W. Biederman 
11731c4d2f1SJonathon Reinhart /* Verify that sysctls for non-init netns are safe by either:
11831c4d2f1SJonathon Reinhart  * 1) being read-only, or
11931c4d2f1SJonathon Reinhart  * 2) having a data pointer which points outside of the global kernel/module
12031c4d2f1SJonathon Reinhart  *    data segment, and rather into the heap where a per-net object was
12131c4d2f1SJonathon Reinhart  *    allocated.
12231c4d2f1SJonathon Reinhart  */
ensure_safe_net_sysctl(struct net * net,const char * path,struct ctl_table * table,size_t table_size)12331c4d2f1SJonathon Reinhart static void ensure_safe_net_sysctl(struct net *net, const char *path,
124*95d49778SJoel Granados 				   struct ctl_table *table, size_t table_size)
12531c4d2f1SJonathon Reinhart {
12631c4d2f1SJonathon Reinhart 	struct ctl_table *ent;
12731c4d2f1SJonathon Reinhart 
12831c4d2f1SJonathon Reinhart 	pr_debug("Registering net sysctl (net %p): %s\n", net, path);
129*95d49778SJoel Granados 	ent = table;
130*95d49778SJoel Granados 	for (size_t i = 0; i < table_size && ent->procname; ent++, i++) {
13131c4d2f1SJonathon Reinhart 		unsigned long addr;
13231c4d2f1SJonathon Reinhart 		const char *where;
13331c4d2f1SJonathon Reinhart 
13431c4d2f1SJonathon Reinhart 		pr_debug("  procname=%s mode=%o proc_handler=%ps data=%p\n",
13531c4d2f1SJonathon Reinhart 			 ent->procname, ent->mode, ent->proc_handler, ent->data);
13631c4d2f1SJonathon Reinhart 
13731c4d2f1SJonathon Reinhart 		/* If it's not writable inside the netns, then it can't hurt. */
13831c4d2f1SJonathon Reinhart 		if ((ent->mode & 0222) == 0) {
13931c4d2f1SJonathon Reinhart 			pr_debug("    Not writable by anyone\n");
14031c4d2f1SJonathon Reinhart 			continue;
14131c4d2f1SJonathon Reinhart 		}
14231c4d2f1SJonathon Reinhart 
14331c4d2f1SJonathon Reinhart 		/* Where does data point? */
14431c4d2f1SJonathon Reinhart 		addr = (unsigned long)ent->data;
14531c4d2f1SJonathon Reinhart 		if (is_module_address(addr))
14631c4d2f1SJonathon Reinhart 			where = "module";
147a20deb3aSKefeng Wang 		else if (is_kernel_core_data(addr))
14831c4d2f1SJonathon Reinhart 			where = "kernel";
14931c4d2f1SJonathon Reinhart 		else
15031c4d2f1SJonathon Reinhart 			continue;
15131c4d2f1SJonathon Reinhart 
15231c4d2f1SJonathon Reinhart 		/* If it is writable and points to kernel/module global
15331c4d2f1SJonathon Reinhart 		 * data, then it's probably a netns leak.
15431c4d2f1SJonathon Reinhart 		 */
15531c4d2f1SJonathon Reinhart 		WARN(1, "sysctl %s/%s: data points to %s global data: %ps\n",
15631c4d2f1SJonathon Reinhart 		     path, ent->procname, where, ent->data);
15731c4d2f1SJonathon Reinhart 
15831c4d2f1SJonathon Reinhart 		/* Make it "safe" by dropping writable perms */
15931c4d2f1SJonathon Reinhart 		ent->mode &= ~0222;
16031c4d2f1SJonathon Reinhart 	}
16131c4d2f1SJonathon Reinhart }
16231c4d2f1SJonathon Reinhart 
register_net_sysctl_sz(struct net * net,const char * path,struct ctl_table * table,size_t table_size)163*95d49778SJoel Granados struct ctl_table_header *register_net_sysctl_sz(struct net *net,
164*95d49778SJoel Granados 						const char *path,
165*95d49778SJoel Granados 						struct ctl_table *table,
166*95d49778SJoel Granados 						size_t table_size)
167ab41a2caSEric W. Biederman {
168*95d49778SJoel Granados 	int count;
169bff97cf1SJoel Granados 	struct ctl_table *entry;
170bff97cf1SJoel Granados 
17131c4d2f1SJonathon Reinhart 	if (!net_eq(net, &init_net))
172*95d49778SJoel Granados 		ensure_safe_net_sysctl(net, path, table, table_size);
17331c4d2f1SJonathon Reinhart 
174*95d49778SJoel Granados 	entry = table;
175*95d49778SJoel Granados 	for (count = 0 ; count < table_size && entry->procname; entry++, count++)
176*95d49778SJoel Granados 		;
177bff97cf1SJoel Granados 
178bff97cf1SJoel Granados 	return __register_sysctl_table(&net->sysctls, path, table, count);
179ab41a2caSEric W. Biederman }
180*95d49778SJoel Granados EXPORT_SYMBOL_GPL(register_net_sysctl_sz);
181ab41a2caSEric W. Biederman 
unregister_net_sysctl_table(struct ctl_table_header * header)18295bdfccbSEric W. Biederman void unregister_net_sysctl_table(struct ctl_table_header *header)
18395bdfccbSEric W. Biederman {
184ab59859dSHarvey Harrison 	unregister_sysctl_table(header);
18595bdfccbSEric W. Biederman }
18695bdfccbSEric W. Biederman EXPORT_SYMBOL_GPL(unregister_net_sysctl_table);
187