ipv4: validate IPV4_DEVCONF attributes properly
Subsystems (3)
PATCH 1/2 ipv4: validate IPV4_DEVCONF attributes properly
As the IPV4_DEVCONF netlink attributes are not being validated, it is possible to use netlink to set read-only values like mc_forwarding. In addition, valid ranges are not being validated neither but that is less relevant as they aren't in sysctl.
To avoid similar situations in the future, define a NLA policy for IPV4_DEVCONF attributes which are nested in IFLA_INET_CONF.
Please note that MEDIUM_ID is defined as NLA_U32 too because currently its usage through netlink is broken for its valid value -1. Modifying the type to NLA_S32 would break existing users of set/get netlink operation.
--- net/ipv4/devinet.c | 81 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 10 deletions(-)
net/ipv4/devinet.c +71 -10
@@ -2063,12 +2063,76 @@ static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = { [IFLA_INET_CONF] = { .type = NLA_NESTED }, }; +static const struct nla_policy inet_devconf_policy[IPV4_DEVCONF_MAX +1 ] = { + [IPV4_DEVCONF_FORWARDING ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_MC_FORWARDING ] = { .type =NLA_REJECT }, + [IPV4_DEVCONF_PROXY_ARP ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_ACCEPT_REDIRECTS ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_SECURE_REDIRECTS ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_SEND_REDIRECTS ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_SHARED_MEDIA ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_RP_FILTER ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,2 ), + [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_BOOTP_RELAY ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_LOG_MARTIANS ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_TAG ] = { .type =NLA_U32 }, + [IPV4_DEVCONF_ARPFILTER ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_MEDIUM_ID ] = { .type =NLA_U32 }, + [IPV4_DEVCONF_NOXFRM ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_NOPOLICY ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_FORCE_IGMP_VERSION ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,3 ), + [IPV4_DEVCONF_ARP_ANNOUNCE ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,2 ), + [IPV4_DEVCONF_ARP_IGNORE ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,8 ), + [IPV4_DEVCONF_PROMOTE_SECONDARIES ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_ARP_ACCEPT ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_ARP_NOTIFY ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_ACCEPT_LOCAL ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_SRC_VMARK ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_PROXY_ARP_PVLAN ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_ROUTE_LOCALNET ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL ] = { .type =NLA_U32 }, + [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL ] = { .type =NLA_U32 }, + [IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_DROP_UNICAST_IN_L2_MULTICAST ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_DROP_GRATUITOUS_ARP ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_BC_FORWARDING ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), + [IPV4_DEVCONF_ARP_EVICT_NOCARRIER ] =NLA_POLICY_RANGE (NLA_U32 , +0 ,1 ), +}; +static int inet_validate_link_af (const struct net_device *dev,const struct nlattr *nla,struct netlink_ext_ack *extack) { -struct nlattr *a, *tb[IFLA_INET_MAX +1 ]; -int err, rem; +struct nlattr *tb[IFLA_INET_MAX +1 ], *nested_tb[IPV4_DEVCONF_MAX +1 ]; +int err;if (dev && !__in_dev_get_rtnl (dev))return -EAFNOSUPPORT ;
@@ -2079,15 +2143,12 @@ static int inet_validate_link_af(const struct net_device *dev,return err;if (tb[IFLA_INET_CONF ]) { -nla_for_each_nested (a, tb[IFLA_INET_CONF ], rem) { -int cfgid =nla_type (a); + err =nla_parse_nested (nested_tb,IPV4_DEVCONF_MAX , + tb[IFLA_INET_CONF ], inet_devconf_policy, + extack); -if (nla_len (a) <4 ) -return -EINVAL ; - -if (cfgid <=0 || cfgid >IPV4_DEVCONF_MAX ) -return -EINVAL ; - } +if (err <0 ) +return err; }return 0 ;
Signature · 1 line
2.53.0
PATCH 2/2 ipv4: bump rt_genid when a relevant devconf value changes through netlink
When modifying IPv4 devconf values using netlink for some relevant fields the rt_cache_flush() call was missing. In addition, if forwarding is enabled on the interface then disable LRO.
This is needed to avoid possible connectivity issues and ease the responsabilities of user space tools.
--- net/ipv4/devinet.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
net/ipv4/devinet.c +22 -1
@@ -2159,6 +2159,8 @@ static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla, {struct in_device *in_dev =__in_dev_get_rtnl (dev);struct nlattr *a, *tb[IFLA_INET_MAX +1 ]; +struct net *net =dev_net (in_dev->dev); +bool flush_cache = false;int rem;if (!in_dev)
@@ -2168,8 +2170,27 @@ static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla,return -EINVAL ;if (tb[IFLA_INET_CONF ]) { -nla_for_each_nested (a, tb[IFLA_INET_CONF ], rem) +nla_for_each_nested (a, tb[IFLA_INET_CONF ], rem) {ipv4_devconf_set (in_dev,nla_type (a),nla_get_u32 (a)); + +switch (nla_type (a)) { +case IPV4_DEVCONF_FORWARDING : +if (nla_get_u32 (a)) +dev_disable_lro (in_dev->dev); + fallthrough; +case IPV4_DEVCONF_NOXFRM : +case IPV4_DEVCONF_NOPOLICY : +case IPV4_DEVCONF_PROMOTE_SECONDARIES : +case IPV4_DEVCONF_ROUTE_LOCALNET : +case IPV4_DEVCONF_DROP_UNICAST_IN_L2_MULTICAST : + flush_cache = true; +break ; +default : +break ; + } + } +if (flush_cache) +rt_cache_flush (net); }return 0 ;
Signature · 1 line
2.53.0
Simon Horman
reviewerRe: [PATCH 2/2 net-next] ipv4: bump rt_genid when a relevant devconf value changes through netlink
Quoted 18 lines · Fernando Fernandez Mancera
When modifying IPv4 devconf values using netlink for some relevant fields the
rt_cache_flush()call was missing. In addition, if forwarding is enabled on the interface then disable LRO.This is needed to avoid possible connectivity issues and ease the responsabilities of user space tools.
Fixes 9f0f7272ac95 ("ipv4: AF_INET link address family")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/ipv4/devinet.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)net/ipv4/devinet.c +0
-0
net/ipv4/devinet.c static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla, · line 2166
Quoted 8 lines
net/ipv4/devinet.c +2
-0@@ -2159,6 +2159,8 @@ static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla, {struct in_device *in_dev =__in_dev_get_rtnl (dev);struct nlattr *a, *tb[IFLA_INET_MAX +1 ]; +struct net *net =dev_net (in_dev->dev); +bool flush_cache = false;int rem;if (!in_dev)
Hi Fernando,
Here it is assumed that in_dev may be NULL.
But a few lines above it is now dereferenced unconditionally.
Flagged by Smatch.
Signature · 1 line
pw-bot: cr
Re: [PATCH 2/2 net-next] ipv4: bump rt_genid when a relevant devconf value changes through netlink
Quoted 26 lines · Simon Horman
Quoted 25 lines · Fernando Fernandez Mancera
When modifying IPv4 devconf values using netlink for some relevant fields the
rt_cache_flush()call was missing. In addition, if forwarding is enabled on the interface then disable LRO.This is needed to avoid possible connectivity issues and ease the responsabilities of user space tools.
Fixes 9f0f7272ac95 ("ipv4: AF_INET link address family")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/ipv4/devinet.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)net/ipv4/devinet.c +2
-0@@ -2159,6 +2159,8 @@ static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla, {struct in_device *in_dev =__in_dev_get_rtnl (dev);struct nlattr *a, *tb[IFLA_INET_MAX +1 ]; +struct net *net =dev_net (in_dev->dev); +bool flush_cache = false;int rem;
Quoted 9 lines
net/ipv4/devinet.c static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla, · line 2166
Quoted 1 line
net/ipv4/devinet.c +0
-0if (!in_dev)Hi Fernando,
Here it is assumed that
in_devmay be NULL.
But a few lines above it is now dereferenced unconditionally.Flagged by Smatch.
Whoops. My bad. Thank you Simon.