Skip to content

Commit c33078e

Browse files
dsaherndavem330
authored andcommitted
net/ipv4: Update inet_dump_ifaddr for strict data checking
Update inet_dump_ifaddr for strict data checking. If the flag is set, the dump request is expected to have an ifaddrmsg struct as the header potentially followed by one or more attributes. Any data passed in the header or as an attribute is taken as a request to influence the data returned. Only values supported by the dump handler are allowed to be non-0 or set in the request. At the moment only the IFA_TARGET_NETNSID attribute is supported. Follow on patches can support for other fields (e.g., honor ifa_index and only return data for the given device index). Signed-off-by: David Ahern <[email protected]> Acked-by: Christian Brauner <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 89d3552 commit c33078e

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed

net/ipv4/devinet.c

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,17 +1660,70 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
16601660
return -EMSGSIZE;
16611661
}
16621662

1663+
static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
1664+
struct inet_fill_args *fillargs,
1665+
struct net **tgt_net, struct sock *sk,
1666+
struct netlink_ext_ack *extack)
1667+
{
1668+
struct nlattr *tb[IFA_MAX+1];
1669+
struct ifaddrmsg *ifm;
1670+
int err, i;
1671+
1672+
if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1673+
NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
1674+
return -EINVAL;
1675+
}
1676+
1677+
ifm = nlmsg_data(nlh);
1678+
if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
1679+
NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
1680+
return -EINVAL;
1681+
}
1682+
if (ifm->ifa_index) {
1683+
NL_SET_ERR_MSG(extack, "ipv4: Filter by device index not supported for address dump");
1684+
return -EINVAL;
1685+
}
1686+
1687+
err = nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
1688+
ifa_ipv4_policy, extack);
1689+
if (err < 0)
1690+
return err;
1691+
1692+
for (i = 0; i <= IFA_MAX; ++i) {
1693+
if (!tb[i])
1694+
continue;
1695+
1696+
if (i == IFA_TARGET_NETNSID) {
1697+
struct net *net;
1698+
1699+
fillargs->netnsid = nla_get_s32(tb[i]);
1700+
1701+
net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
1702+
if (IS_ERR(net)) {
1703+
NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id");
1704+
return PTR_ERR(net);
1705+
}
1706+
*tgt_net = net;
1707+
} else {
1708+
NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request");
1709+
return -EINVAL;
1710+
}
1711+
}
1712+
1713+
return 0;
1714+
}
1715+
16631716
static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
16641717
{
1718+
const struct nlmsghdr *nlh = cb->nlh;
16651719
struct inet_fill_args fillargs = {
16661720
.portid = NETLINK_CB(cb->skb).portid,
1667-
.seq = cb->nlh->nlmsg_seq,
1721+
.seq = nlh->nlmsg_seq,
16681722
.event = RTM_NEWADDR,
16691723
.flags = NLM_F_MULTI,
16701724
.netnsid = -1,
16711725
};
16721726
struct net *net = sock_net(skb->sk);
1673-
struct nlattr *tb[IFA_MAX+1];
16741727
struct net *tgt_net = net;
16751728
int h, s_h;
16761729
int idx, s_idx;
@@ -1684,16 +1737,13 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
16841737
s_idx = idx = cb->args[1];
16851738
s_ip_idx = ip_idx = cb->args[2];
16861739

1687-
if (nlmsg_parse(cb->nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX,
1688-
ifa_ipv4_policy, cb->extack) >= 0) {
1689-
if (tb[IFA_TARGET_NETNSID]) {
1690-
fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
1740+
if (cb->strict_check) {
1741+
int err;
16911742

1692-
tgt_net = rtnl_get_net_ns_capable(skb->sk,
1693-
fillargs.netnsid);
1694-
if (IS_ERR(tgt_net))
1695-
return PTR_ERR(tgt_net);
1696-
}
1743+
err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
1744+
skb->sk, cb->extack);
1745+
if (err < 0)
1746+
return err;
16971747
}
16981748

16991749
for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {

0 commit comments

Comments
 (0)