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 <j.granados@samsung.com>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
This commit is contained in:
Joel Granados 2023-08-09 12:50:03 +02:00 committed by Luis Chamberlain
parent 385a5dc9e5
commit c899710fe7
22 changed files with 82 additions and 28 deletions

View File

@ -1274,7 +1274,9 @@ static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; }
#ifdef CONFIG_SYSCTL #ifdef CONFIG_SYSCTL
struct ctl_table *ipv6_icmp_sysctl_init(struct net *net); struct ctl_table *ipv6_icmp_sysctl_init(struct net *net);
size_t ipv6_icmp_sysctl_table_size(void);
struct ctl_table *ipv6_route_sysctl_init(struct net *net); struct ctl_table *ipv6_route_sysctl_init(struct net *net);
size_t ipv6_route_sysctl_table_size(struct net *net);
int ipv6_sysctl_register(void); int ipv6_sysctl_register(void);
void ipv6_sysctl_unregister(void); void ipv6_sysctl_unregister(void);
#endif #endif

View File

@ -3779,6 +3779,7 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
const char *dev_name_source; const char *dev_name_source;
char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ]; char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
char *p_name; char *p_name;
size_t neigh_vars_size;
t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL_ACCOUNT); t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL_ACCOUNT);
if (!t) if (!t)
@ -3790,11 +3791,13 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
t->neigh_vars[i].extra2 = p; t->neigh_vars[i].extra2 = p;
} }
neigh_vars_size = ARRAY_SIZE(t->neigh_vars);
if (dev) { if (dev) {
dev_name_source = dev->name; dev_name_source = dev->name;
/* Terminate the table early */ /* Terminate the table early */
memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0, memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL])); sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
neigh_vars_size = NEIGH_VAR_BASE_REACHABLE_TIME_MS + 1;
} else { } else {
struct neigh_table *tbl = p->tbl; struct neigh_table *tbl = p->tbl;
dev_name_source = "default"; dev_name_source = "default";
@ -3841,8 +3844,9 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s", snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
p_name, dev_name_source); p_name, dev_name_source);
t->sysctl_header = t->sysctl_header = register_net_sysctl_sz(neigh_parms_net(p),
register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars); neigh_path, t->neigh_vars,
neigh_vars_size);
if (!t->sysctl_header) if (!t->sysctl_header)
goto free; goto free;

View File

@ -712,7 +712,8 @@ static __net_init int sysctl_core_net_init(struct net *net)
tmp->data += (char *)net - (char *)&init_net; tmp->data += (char *)net - (char *)&init_net;
} }
net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl); net->core.sysctl_hdr = register_net_sysctl_sz(net, "net/core", tbl,
ARRAY_SIZE(netns_core_table));
if (net->core.sysctl_hdr == NULL) if (net->core.sysctl_hdr == NULL)
goto err_reg; goto err_reg;

View File

@ -360,6 +360,7 @@ static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
struct ctl_table_header *hdr; struct ctl_table_header *hdr;
struct netns_ieee802154_lowpan *ieee802154_lowpan = struct netns_ieee802154_lowpan *ieee802154_lowpan =
net_ieee802154_lowpan(net); net_ieee802154_lowpan(net);
size_t table_size = ARRAY_SIZE(lowpan_frags_ns_ctl_table);
table = lowpan_frags_ns_ctl_table; table = lowpan_frags_ns_ctl_table;
if (!net_eq(net, &init_net)) { if (!net_eq(net, &init_net)) {
@ -369,8 +370,10 @@ static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
goto err_alloc; goto err_alloc;
/* Don't export sysctls to unprivileged users */ /* Don't export sysctls to unprivileged users */
if (net->user_ns != &init_user_ns) if (net->user_ns != &init_user_ns) {
table[0].procname = NULL; table[0].procname = NULL;
table_size = 0;
}
} }
table[0].data = &ieee802154_lowpan->fqdir->high_thresh; table[0].data = &ieee802154_lowpan->fqdir->high_thresh;
@ -379,7 +382,8 @@ static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
table[1].extra2 = &ieee802154_lowpan->fqdir->high_thresh; table[1].extra2 = &ieee802154_lowpan->fqdir->high_thresh;
table[2].data = &ieee802154_lowpan->fqdir->timeout; table[2].data = &ieee802154_lowpan->fqdir->timeout;
hdr = register_net_sysctl(net, "net/ieee802154/6lowpan", table); hdr = register_net_sysctl_sz(net, "net/ieee802154/6lowpan", table,
table_size);
if (hdr == NULL) if (hdr == NULL)
goto err_reg; goto err_reg;

View File

@ -2720,7 +2720,8 @@ static __net_init int devinet_init_net(struct net *net)
goto err_reg_dflt; goto err_reg_dflt;
err = -ENOMEM; err = -ENOMEM;
forw_hdr = register_net_sysctl(net, "net/ipv4", tbl); forw_hdr = register_net_sysctl_sz(net, "net/ipv4", tbl,
ARRAY_SIZE(ctl_forward_entry));
if (!forw_hdr) if (!forw_hdr)
goto err_reg_ctl; goto err_reg_ctl;
net->ipv4.forw_hdr = forw_hdr; net->ipv4.forw_hdr = forw_hdr;

View File

@ -615,7 +615,8 @@ static int __net_init ip4_frags_ns_ctl_register(struct net *net)
table[2].data = &net->ipv4.fqdir->timeout; table[2].data = &net->ipv4.fqdir->timeout;
table[3].data = &net->ipv4.fqdir->max_dist; table[3].data = &net->ipv4.fqdir->max_dist;
hdr = register_net_sysctl(net, "net/ipv4", table); hdr = register_net_sysctl_sz(net, "net/ipv4", table,
ARRAY_SIZE(ip4_frags_ns_ctl_table));
if (!hdr) if (!hdr)
goto err_reg; goto err_reg;

View File

@ -3592,6 +3592,7 @@ static struct ctl_table ipv4_route_netns_table[] = {
static __net_init int sysctl_route_net_init(struct net *net) static __net_init int sysctl_route_net_init(struct net *net)
{ {
struct ctl_table *tbl; struct ctl_table *tbl;
size_t table_size = ARRAY_SIZE(ipv4_route_netns_table);
tbl = ipv4_route_netns_table; tbl = ipv4_route_netns_table;
if (!net_eq(net, &init_net)) { if (!net_eq(net, &init_net)) {
@ -3603,8 +3604,10 @@ static __net_init int sysctl_route_net_init(struct net *net)
/* Don't export non-whitelisted sysctls to unprivileged users */ /* Don't export non-whitelisted sysctls to unprivileged users */
if (net->user_ns != &init_user_ns) { if (net->user_ns != &init_user_ns) {
if (tbl[0].procname != ipv4_route_flush_procname) if (tbl[0].procname != ipv4_route_flush_procname) {
tbl[0].procname = NULL; tbl[0].procname = NULL;
table_size = 0;
}
} }
/* Update the variables to point into the current struct net /* 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)
} }
tbl[0].extra1 = net; tbl[0].extra1 = net;
net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl); net->ipv4.route_hdr = register_net_sysctl_sz(net, "net/ipv4/route",
tbl, table_size);
if (!net->ipv4.route_hdr) if (!net->ipv4.route_hdr)
goto err_reg; goto err_reg;
return 0; return 0;

View File

@ -1519,7 +1519,8 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
} }
} }
net->ipv4.ipv4_hdr = register_net_sysctl(net, "net/ipv4", table); net->ipv4.ipv4_hdr = register_net_sysctl_sz(net, "net/ipv4", table,
ARRAY_SIZE(ipv4_net_table));
if (!net->ipv4.ipv4_hdr) if (!net->ipv4.ipv4_hdr)
goto err_reg; goto err_reg;

View File

@ -178,7 +178,8 @@ static __net_init int xfrm4_net_sysctl_init(struct net *net)
table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh; table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
} }
hdr = register_net_sysctl(net, "net/ipv4", table); hdr = register_net_sysctl_sz(net, "net/ipv4", table,
ARRAY_SIZE(xfrm4_policy_table));
if (!hdr) if (!hdr)
goto err_reg; goto err_reg;

View File

@ -7086,7 +7086,8 @@ static int __addrconf_sysctl_register(struct net *net, char *dev_name,
snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
p->sysctl_header = register_net_sysctl(net, path, table); p->sysctl_header = register_net_sysctl_sz(net, path, table,
ARRAY_SIZE(addrconf_sysctl));
if (!p->sysctl_header) if (!p->sysctl_header)
goto free; goto free;

View File

@ -1226,4 +1226,9 @@ struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
} }
return table; return table;
} }
size_t ipv6_icmp_sysctl_table_size(void)
{
return ARRAY_SIZE(ipv6_icmp_table_template);
}
#endif #endif

View File

@ -470,7 +470,8 @@ static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
table[1].extra2 = &net->ipv6.fqdir->high_thresh; table[1].extra2 = &net->ipv6.fqdir->high_thresh;
table[2].data = &net->ipv6.fqdir->timeout; table[2].data = &net->ipv6.fqdir->timeout;
hdr = register_net_sysctl(net, "net/ipv6", table); hdr = register_net_sysctl_sz(net, "net/ipv6", table,
ARRAY_SIZE(ip6_frags_ns_ctl_table));
if (!hdr) if (!hdr)
goto err_reg; goto err_reg;

View File

@ -6455,6 +6455,15 @@ struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
return table; return table;
} }
size_t ipv6_route_sysctl_table_size(struct net *net)
{
/* Don't export sysctls to unprivileged users */
if (net->user_ns != &init_user_ns)
return 1;
return ARRAY_SIZE(ipv6_route_table_template);
}
#endif #endif
static int __net_init ip6_route_net_init(struct net *net) static int __net_init ip6_route_net_init(struct net *net)

View File

@ -275,17 +275,23 @@ static int __net_init ipv6_sysctl_net_init(struct net *net)
if (!ipv6_icmp_table) if (!ipv6_icmp_table)
goto out_ipv6_route_table; goto out_ipv6_route_table;
net->ipv6.sysctl.hdr = register_net_sysctl(net, "net/ipv6", ipv6_table); net->ipv6.sysctl.hdr = register_net_sysctl_sz(net, "net/ipv6",
ipv6_table,
ARRAY_SIZE(ipv6_table_template));
if (!net->ipv6.sysctl.hdr) if (!net->ipv6.sysctl.hdr)
goto out_ipv6_icmp_table; goto out_ipv6_icmp_table;
net->ipv6.sysctl.route_hdr = net->ipv6.sysctl.route_hdr = register_net_sysctl_sz(net,
register_net_sysctl(net, "net/ipv6/route", ipv6_route_table); "net/ipv6/route",
ipv6_route_table,
ipv6_route_sysctl_table_size(net));
if (!net->ipv6.sysctl.route_hdr) if (!net->ipv6.sysctl.route_hdr)
goto out_unregister_ipv6_table; goto out_unregister_ipv6_table;
net->ipv6.sysctl.icmp_hdr = net->ipv6.sysctl.icmp_hdr = register_net_sysctl_sz(net,
register_net_sysctl(net, "net/ipv6/icmp", ipv6_icmp_table); "net/ipv6/icmp",
ipv6_icmp_table,
ipv6_icmp_sysctl_table_size());
if (!net->ipv6.sysctl.icmp_hdr) if (!net->ipv6.sysctl.icmp_hdr)
goto out_unregister_route_table; goto out_unregister_route_table;

View File

@ -205,7 +205,8 @@ static int __net_init xfrm6_net_sysctl_init(struct net *net)
table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh; table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
} }
hdr = register_net_sysctl(net, "net/ipv6", table); hdr = register_net_sysctl_sz(net, "net/ipv6", table,
ARRAY_SIZE(xfrm6_policy_table));
if (!hdr) if (!hdr)
goto err_reg; goto err_reg;

View File

@ -1419,7 +1419,8 @@ static int mpls_dev_sysctl_register(struct net_device *dev,
snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name); snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
mdev->sysctl = register_net_sysctl(net, path, table); mdev->sysctl = register_net_sysctl_sz(net, path, table,
ARRAY_SIZE(mpls_dev_table));
if (!mdev->sysctl) if (!mdev->sysctl)
goto free; goto free;
@ -2689,7 +2690,8 @@ static int mpls_net_init(struct net *net)
for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++) for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
table[i].data = (char *)net + (uintptr_t)table[i].data; table[i].data = (char *)net + (uintptr_t)table[i].data;
net->mpls.ctl = register_net_sysctl(net, "net/mpls", table); net->mpls.ctl = register_net_sysctl_sz(net, "net/mpls", table,
ARRAY_SIZE(mpls_table));
if (net->mpls.ctl == NULL) { if (net->mpls.ctl == NULL) {
kfree(table); kfree(table);
return -ENOMEM; return -ENOMEM;

View File

@ -150,7 +150,8 @@ static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
table[4].data = &pernet->stale_loss_cnt; table[4].data = &pernet->stale_loss_cnt;
table[5].data = &pernet->pm_type; table[5].data = &pernet->pm_type;
hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table); hdr = register_net_sysctl_sz(net, MPTCP_SYSCTL_PATH, table,
ARRAY_SIZE(mptcp_sysctl_table));
if (!hdr) if (!hdr)
goto err_reg; goto err_reg;

View File

@ -565,7 +565,8 @@ static __net_init int rds_tcp_init_net(struct net *net)
} }
tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size; tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size; tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl); rtn->rds_tcp_sysctl = register_net_sysctl_sz(net, "net/rds/tcp", tbl,
ARRAY_SIZE(rds_tcp_sysctl_table));
if (!rtn->rds_tcp_sysctl) { if (!rtn->rds_tcp_sysctl) {
pr_warn("could not register sysctl\n"); pr_warn("could not register sysctl\n");
err = -ENOMEM; err = -ENOMEM;

View File

@ -612,7 +612,9 @@ int sctp_sysctl_net_register(struct net *net)
table[SCTP_PF_RETRANS_IDX].extra2 = &net->sctp.ps_retrans; table[SCTP_PF_RETRANS_IDX].extra2 = &net->sctp.ps_retrans;
table[SCTP_PS_RETRANS_IDX].extra1 = &net->sctp.pf_retrans; table[SCTP_PS_RETRANS_IDX].extra1 = &net->sctp.pf_retrans;
net->sctp.sysctl_header = register_net_sysctl(net, "net/sctp", table); net->sctp.sysctl_header = register_net_sysctl_sz(net, "net/sctp",
table,
ARRAY_SIZE(sctp_net_table));
if (net->sctp.sysctl_header == NULL) { if (net->sctp.sysctl_header == NULL) {
kfree(table); kfree(table);
return -ENOMEM; return -ENOMEM;

View File

@ -81,7 +81,8 @@ int __net_init smc_sysctl_net_init(struct net *net)
table[i].data += (void *)net - (void *)&init_net; table[i].data += (void *)net - (void *)&init_net;
} }
net->smc.smc_hdr = register_net_sysctl(net, "net/smc", table); net->smc.smc_hdr = register_net_sysctl_sz(net, "net/smc", table,
ARRAY_SIZE(smc_table));
if (!net->smc.smc_hdr) if (!net->smc.smc_hdr)
goto err_reg; goto err_reg;

View File

@ -36,7 +36,8 @@ int __net_init unix_sysctl_register(struct net *net)
table[0].data = &net->unx.sysctl_max_dgram_qlen; table[0].data = &net->unx.sysctl_max_dgram_qlen;
} }
net->unx.ctl = register_net_sysctl(net, "net/unix", table); net->unx.ctl = register_net_sysctl_sz(net, "net/unix", table,
ARRAY_SIZE(unix_table));
if (net->unx.ctl == NULL) if (net->unx.ctl == NULL)
goto err_reg; goto err_reg;

View File

@ -44,6 +44,7 @@ static struct ctl_table xfrm_table[] = {
int __net_init xfrm_sysctl_init(struct net *net) int __net_init xfrm_sysctl_init(struct net *net)
{ {
struct ctl_table *table; struct ctl_table *table;
size_t table_size = ARRAY_SIZE(xfrm_table);
__xfrm_sysctl_init(net); __xfrm_sysctl_init(net);
@ -56,10 +57,13 @@ int __net_init xfrm_sysctl_init(struct net *net)
table[3].data = &net->xfrm.sysctl_acq_expires; table[3].data = &net->xfrm.sysctl_acq_expires;
/* Don't export sysctls to unprivileged users */ /* Don't export sysctls to unprivileged users */
if (net->user_ns != &init_user_ns) if (net->user_ns != &init_user_ns) {
table[0].procname = NULL; table[0].procname = NULL;
table_size = 0;
}
net->xfrm.sysctl_hdr = register_net_sysctl(net, "net/core", table); net->xfrm.sysctl_hdr = register_net_sysctl_sz(net, "net/core", table,
table_size);
if (!net->xfrm.sysctl_hdr) if (!net->xfrm.sysctl_hdr)
goto out_register; goto out_register;
return 0; return 0;