ipv4: validate IPV4_DEVCONF attributes properly
Subsystems (3)
PATCH v4 1/1 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.
---
v2: no changes
v3: dropped the fixes tag and adjusted the indentation of the policy, MEDIUM_ID is now NLA_S32
v4: used NLA_POLICY_MIN() for MEDIUM_ID so the min value takes effect properly, make ARP_ACCEPT accept 2 as valid value and checked each attribute again so they match the documentation. Tested with a custom script using libnl
--- net/ipv4/devinet.c | 55 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-)
net/ipv4/devinet.c +45 -10
@@ -2063,12 +2063,50 @@ 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 ] =NLA_POLICY_MIN (NLA_S32 ,-1 ), + [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 ,2 ), + [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_BC_FORWARDING ] =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_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 +2117,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
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Thu, 12 Mar 2026 15:26:37 +0100 you wrote:
As the
IPV4_DEVCONFnetlink attributes are not being validated, it is possible to use netlink to set read-only values likemc_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_DEVCONFattributes which are nested inIFLA_INET_CONF.[...]
Here is the summary with links:
- [net-next,v4] ipv4: validate IPV4_DEVCONF attributes properly
https://git.kernel.org/netdev/net-next/c/fa8fca88714c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html