Skip to contentSkip to conversation

ipv4: ipmr: add socket type checks to ipmr_ioctl()

PATCH In review Suchit Karunakaran 2 participants 3 messages Jan 23 – Jan 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipv4: ipmr: add socket type checks to ipmr_ioctl()

This is the IPv4 counterpart to commit ("ipv6: ip6mr: add socket type checks to ip6mr_ioctl()") [1].

Similar to the IPv6 issue, ipmr_ioctl() and ipmr_compat_ioctl() access raw_sk(sk)->ipmr_table without first verifying that the socket is a raw socket with IPPROTO_IGMP protocol.

This allows a permission bypass where a user with CAP_NET_RAW can create a non-IGMP raw socket (e.g., IPPROTO_UDP, IPPROTO_TCP, or any other protocol) and use SIOCGETVIFCNT or SIOCGETSGCNT ioctls to query IPv4 multicast routing statistics. This bypasses the access control that restricts mroute operations to IGMP sockets only.

Add socket type and protocol checks at the beginning of both ipmr_ioctl() and ipmr_compat_ioctl() to ensure only IGMP raw sockets can access multicast routing ioctls.

[1] https://lore.kernel.org/all/[email protected]/

---
 net/ipv4/ipmr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/ipv4/ipmr.c +8 -0
index ca9eaee4c2ef..eae03a1b8f66 100644
@@ -1643,6 +1643,10 @@ int ipmr_ioctl(struct sock *sk, int cmd, void *arg)
 	struct sioc_sg_req *sr;
 	struct mr_table *mrt;
 
+	if (sk->sk_type != SOCK_RAW ||
+	    inet_sk(sk)->inet_num != IPPROTO_IGMP)
+		return -EOPNOTSUPP;
+
 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
 	if (!mrt)
 		return -ENOENT;
@@ -1711,6 +1715,10 @@ int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
 	struct net *net = sock_net(sk);
 	struct mr_table *mrt;
 
+	if (sk->sk_type != SOCK_RAW ||
+	    inet_sk(sk)->inet_num != IPPROTO_IGMP)
+		return -EOPNOTSUPP;
+
 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
 	if (!mrt)
 		return -ENOENT;
Signature · 1 line
2.52.0
#1 lore raw
Quoted 8 lines · Suchit Karunakaran

This is the IPv4 counterpart to commit ("ipv6: ip6mr: add socket type checks to ip6mr_ioctl()") [1].

Similar to the IPv6 issue, ipmr_ioctl() and ipmr_compat_ioctl() access raw_sk(sk)->ipmr_table without first verifying that the socket is a raw socket with IPPROTO_IGMP protocol.

Quoted 5 lines

This allows a permission bypass where a user with CAP_NET_RAW can create a non-IGMP raw socket (e.g., IPPROTO_UDP, IPPROTO_TCP, or any other protocol) and use SIOCGETVIFCNT or SIOCGETSGCNT ioctls to query IPv4 multicast routing statistics. This bypasses the access control that restricts mroute operations to IGMP sockets only.

Where has this been documented? An RFC perhaps ?

This change could break applications if they were unaware of such rules.

I fail to see how querying statistics could be a risk.

What about the RTNL_FAMILY_IPMR rtnetlink interface ? I am sure it is available to any user.

#2 lore raw
Quoted 17 lines · Eric Dumazet
Quoted 8 lines · Suchit Karunakaran

This is the IPv4 counterpart to commit ("ipv6: ip6mr: add socket type checks to ip6mr_ioctl()") [1].

Similar to the IPv6 issue, ipmr_ioctl() and ipmr_compat_ioctl() access raw_sk(sk)->ipmr_table without first verifying that the socket is a raw socket with IPPROTO_IGMP protocol.

Quoted 5 lines

This allows a permission bypass where a user with CAP_NET_RAW can create a non-IGMP raw socket (e.g., IPPROTO_UDP, IPPROTO_TCP, or any other protocol) and use SIOCGETVIFCNT or SIOCGETSGCNT ioctls to query IPv4 multicast routing statistics. This bypasses the access control that restricts mroute operations to IGMP sockets only.

Quoted 8 lines

Where has this been documented? An RFC perhaps ?

This change could break applications if they were unaware of such rules.

I fail to see how querying statistics could be a risk.

What about the RTNL_FAMILY_IPMR rtnetlink interface ? I am sure it is available to any user.

Hi Eric. The primary motivations for this patch are the IPv6 counterpart mentioned in the commit message and the commit 5e1859f.