Skip to content

Commit c899710

Browse files
Joelgranadosmcgrof
authored andcommitted
networking: Update to register_net_sysctl_sz
Move from register_net_sysctl to register_net_sysctl_sz for all the networking related files. Do this while making sure to mirror the NULL assignments with a table_size of zero for the unprivileged users. We need to move to the new function in preparation for when we change SIZE_MAX to ARRAY_SIZE() in the register_net_sysctl macro. Failing to do so would erroneously allow ARRAY_SIZE() to be called on a pointer. We hold off the SIZE_MAX to ARRAY_SIZE change until we have migrated all the relevant net sysctl registering functions to register_net_sysctl_sz in subsequent commits. An additional size function was added to the following files in order to calculate the size of an array that is defined in another file: include/net/ipv6.h net/ipv6/icmp.c net/ipv6/route.c net/ipv6/sysctl_net_ipv6.c Signed-off-by: Joel Granados <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 385a5dc commit c899710

File tree

22 files changed

+82
-28
lines changed

22 files changed

+82
-28
lines changed

include/net/ipv6.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,9 @@ static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; }
12741274

12751275
#ifdef CONFIG_SYSCTL
12761276
struct ctl_table *ipv6_icmp_sysctl_init(struct net *net);
1277+
size_t ipv6_icmp_sysctl_table_size(void);
12771278
struct ctl_table *ipv6_route_sysctl_init(struct net *net);
1279+
size_t ipv6_route_sysctl_table_size(struct net *net);
12781280
int ipv6_sysctl_register(void);
12791281
void ipv6_sysctl_unregister(void);
12801282
#endif

net/core/neighbour.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,7 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
37793779
const char *dev_name_source;
37803780
char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
37813781
char *p_name;
3782+
size_t neigh_vars_size;
37823783

37833784
t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL_ACCOUNT);
37843785
if (!t)
@@ -3790,11 +3791,13 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
37903791
t->neigh_vars[i].extra2 = p;
37913792
}
37923793

3794+
neigh_vars_size = ARRAY_SIZE(t->neigh_vars);
37933795
if (dev) {
37943796
dev_name_source = dev->name;
37953797
/* Terminate the table early */
37963798
memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
37973799
sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
3800+
neigh_vars_size = NEIGH_VAR_BASE_REACHABLE_TIME_MS + 1;
37983801
} else {
37993802
struct neigh_table *tbl = p->tbl;
38003803
dev_name_source = "default";
@@ -3841,8 +3844,9 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
38413844

38423845
snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
38433846
p_name, dev_name_source);
3844-
t->sysctl_header =
3845-
register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
3847+
t->sysctl_header = register_net_sysctl_sz(neigh_parms_net(p),
3848+
neigh_path, t->neigh_vars,
3849+
neigh_vars_size);
38463850
if (!t->sysctl_header)
38473851
goto free;
38483852

net/core/sysctl_net_core.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ static __net_init int sysctl_core_net_init(struct net *net)
712712
tmp->data += (char *)net - (char *)&init_net;
713713
}
714714

715-
net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
715+
net->core.sysctl_hdr = register_net_sysctl_sz(net, "net/core", tbl,
716+
ARRAY_SIZE(netns_core_table));
716717
if (net->core.sysctl_hdr == NULL)
717718
goto err_reg;
718719

net/ieee802154/6lowpan/reassembly.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
360360
struct ctl_table_header *hdr;
361361
struct netns_ieee802154_lowpan *ieee802154_lowpan =
362362
net_ieee802154_lowpan(net);
363+
size_t table_size = ARRAY_SIZE(lowpan_frags_ns_ctl_table);
363364

364365
table = lowpan_frags_ns_ctl_table;
365366
if (!net_eq(net, &init_net)) {
@@ -369,8 +370,10 @@ static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
369370
goto err_alloc;
370371

371372
/* Don't export sysctls to unprivileged users */
372-
if (net->user_ns != &init_user_ns)
373+
if (net->user_ns != &init_user_ns) {
373374
table[0].procname = NULL;
375+
table_size = 0;
376+
}
374377
}
375378

376379
table[0].data = &ieee802154_lowpan->fqdir->high_thresh;
@@ -379,7 +382,8 @@ static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
379382
table[1].extra2 = &ieee802154_lowpan->fqdir->high_thresh;
380383
table[2].data = &ieee802154_lowpan->fqdir->timeout;
381384

382-
hdr = register_net_sysctl(net, "net/ieee802154/6lowpan", table);
385+
hdr = register_net_sysctl_sz(net, "net/ieee802154/6lowpan", table,
386+
table_size);
383387
if (hdr == NULL)
384388
goto err_reg;
385389

net/ipv4/devinet.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2720,7 +2720,8 @@ static __net_init int devinet_init_net(struct net *net)
27202720
goto err_reg_dflt;
27212721

27222722
err = -ENOMEM;
2723-
forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
2723+
forw_hdr = register_net_sysctl_sz(net, "net/ipv4", tbl,
2724+
ARRAY_SIZE(ctl_forward_entry));
27242725
if (!forw_hdr)
27252726
goto err_reg_ctl;
27262727
net->ipv4.forw_hdr = forw_hdr;

net/ipv4/ip_fragment.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ static int __net_init ip4_frags_ns_ctl_register(struct net *net)
615615
table[2].data = &net->ipv4.fqdir->timeout;
616616
table[3].data = &net->ipv4.fqdir->max_dist;
617617

618-
hdr = register_net_sysctl(net, "net/ipv4", table);
618+
hdr = register_net_sysctl_sz(net, "net/ipv4", table,
619+
ARRAY_SIZE(ip4_frags_ns_ctl_table));
619620
if (!hdr)
620621
goto err_reg;
621622

net/ipv4/route.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,6 +3592,7 @@ static struct ctl_table ipv4_route_netns_table[] = {
35923592
static __net_init int sysctl_route_net_init(struct net *net)
35933593
{
35943594
struct ctl_table *tbl;
3595+
size_t table_size = ARRAY_SIZE(ipv4_route_netns_table);
35953596

35963597
tbl = ipv4_route_netns_table;
35973598
if (!net_eq(net, &init_net)) {
@@ -3603,8 +3604,10 @@ static __net_init int sysctl_route_net_init(struct net *net)
36033604

36043605
/* Don't export non-whitelisted sysctls to unprivileged users */
36053606
if (net->user_ns != &init_user_ns) {
3606-
if (tbl[0].procname != ipv4_route_flush_procname)
3607+
if (tbl[0].procname != ipv4_route_flush_procname) {
36073608
tbl[0].procname = NULL;
3609+
table_size = 0;
3610+
}
36083611
}
36093612

36103613
/* Update the variables to point into the current struct net
@@ -3615,7 +3618,8 @@ static __net_init int sysctl_route_net_init(struct net *net)
36153618
}
36163619
tbl[0].extra1 = net;
36173620

3618-
net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl);
3621+
net->ipv4.route_hdr = register_net_sysctl_sz(net, "net/ipv4/route",
3622+
tbl, table_size);
36193623
if (!net->ipv4.route_hdr)
36203624
goto err_reg;
36213625
return 0;

net/ipv4/sysctl_net_ipv4.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,8 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
15191519
}
15201520
}
15211521

1522-
net->ipv4.ipv4_hdr = register_net_sysctl(net, "net/ipv4", table);
1522+
net->ipv4.ipv4_hdr = register_net_sysctl_sz(net, "net/ipv4", table,
1523+
ARRAY_SIZE(ipv4_net_table));
15231524
if (!net->ipv4.ipv4_hdr)
15241525
goto err_reg;
15251526

net/ipv4/xfrm4_policy.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ static __net_init int xfrm4_net_sysctl_init(struct net *net)
178178
table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
179179
}
180180

181-
hdr = register_net_sysctl(net, "net/ipv4", table);
181+
hdr = register_net_sysctl_sz(net, "net/ipv4", table,
182+
ARRAY_SIZE(xfrm4_policy_table));
182183
if (!hdr)
183184
goto err_reg;
184185

net/ipv6/addrconf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7086,7 +7086,8 @@ static int __addrconf_sysctl_register(struct net *net, char *dev_name,
70867086

70877087
snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
70887088

7089-
p->sysctl_header = register_net_sysctl(net, path, table);
7089+
p->sysctl_header = register_net_sysctl_sz(net, path, table,
7090+
ARRAY_SIZE(addrconf_sysctl));
70907091
if (!p->sysctl_header)
70917092
goto free;
70927093

0 commit comments

Comments
 (0)