Skip to contentSkip to conversation

ovpn: support operations on interfaces in foreign netns

PATCH New Marco Baffo 1 participant 3 messages Sep 22

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

PATCH 1/3 ovpn: support operations on interfaces in foreign netns

ovpn currently looks up the target interface in the network namespace associated with the netlink socket. This prevents a userspace process from controlling an ovpn interface located in another namespace.

Add the optional OVPN_A_TARGET_NETNSID attribute. When present, it identifies the namespace containing the target interface relative to the namespace of the requesting Netlink socket. Resolve the ID and perform the interface lookup there. When absent, preserve the existing lookup behavior.

Allow the attribute in all peer and key request policies, including peer dump requests.

---
 Documentation/netlink/specs/ovpn.yaml | 27 ++++++++++++++++
 drivers/net/ovpn/netlink-gen.c        | 45 ++++++++++++++++-----------
 drivers/net/ovpn/netlink.c            | 34 +++++++++++++++++---
 include/uapi/linux/ovpn.h             |  1 +
 4 files changed, 84 insertions(+), 23 deletions(-)
Documentation/netlink/specs/ovpn.yaml +27 -0
index ac50d1d7c00a6..3d8055282ebe5 100644
@@ -341,12 +341,20 @@ attribute-sets:
         type: nest
         doc: Peer specific cipher configuration
         nested-attributes: keyconf
+      -
+        name: target-netnsid
+        type: s32
+        doc: >-
+          ID of the network namespace containing the ovpn interface, relative to
+          the network namespace of the requesting Netlink socket
   -
     name: ovpn-peer-new-input
     subset-of: ovpn
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: peer
         nested-attributes: peer-new-input
@@ -356,6 +364,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: peer
         nested-attributes: peer-set-input
@@ -365,6 +375,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: peer
         nested-attributes: peer-del-input
@@ -374,6 +386,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: keyconf
         nested-attributes: keyconf-get
@@ -383,6 +397,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: keyconf
         nested-attributes: keyconf-swap-input
@@ -392,6 +408,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: keyconf
         nested-attributes: keyconf-del-input
@@ -409,6 +427,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - peer
     -
       name: peer-set
@@ -421,6 +440,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - peer
     -
       name: peer-get
@@ -433,6 +453,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - peer
         reply:
           attributes:
@@ -441,6 +462,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
         reply:
           attributes:
             - peer
@@ -455,6 +477,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - peer
     -
       name: peer-del-ntf
@@ -473,6 +496,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - keyconf
     -
       name: key-get
@@ -485,6 +509,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - keyconf
         reply:
           attributes:
@@ -500,6 +525,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - keyconf
     -
       name: key-swap-ntf
@@ -519,6 +545,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - keyconf
 
     -
drivers/net/ovpn/netlink-gen.c +27 -18

145 lines not on this page · raw message

drivers/net/ovpn/netlink.c +29 -5
index 4dad852941982..04df2c0288a5d 100644
@@ -9,6 +9,7 @@
 #include <linux/netdevice.h>
 #include <linux/types.h>
 #include <net/genetlink.h>
+#include <net/rtnetlink.h>
 
 #include <uapi/linux/ovpn.h>
 
@@ -26,31 +27,52 @@ MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME);
 /**
  * ovpn_get_dev_from_attrs - retrieve the ovpn private data from the netdevice
  *			     a netlink message is targeting
- * @net: network namespace where to look for the interface
+ * @sk: requesting netlink socket
  * @info: generic netlink info from the user request
  * @tracker: tracker object to be used for the netdev reference acquisition
  *
  * Return: the ovpn private data, if found, or an error otherwise
  */
 static struct ovpn_priv *
-ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
+ovpn_get_dev_from_attrs(struct sock *sk, const struct genl_info *info,
 			netdevice_tracker *tracker)
 {
+	struct net *target_net = NULL;
+	struct net *net = sock_net(sk);
 	struct ovpn_priv *ovpn;
 	struct net_device *dev;
-	int ifindex;
+	int ifindex, target_id;
 
 	if (GENL_REQ_ATTR_CHECK(info, OVPN_A_IFINDEX))
 		return ERR_PTR(-EINVAL);
 
 	ifindex = nla_get_u32(info->attrs[OVPN_A_IFINDEX]);
 
+	if (info->attrs[OVPN_A_TARGET_NETNSID]) {
+		/* Target netns IDs are relative to the requesting Netlink socket */
+		target_id = nla_get_s32(info->attrs[OVPN_A_TARGET_NETNSID]);
+		target_net = rtnl_get_net_ns_capable(sk, target_id);
+		if (IS_ERR(target_net)) {
+			if (PTR_ERR(target_net) == -EACCES)
+				NL_SET_ERR_MSG_MOD(info->extack,
+						   "insufficient permission for target network namespace");
+			else
+				NL_SET_ERR_MSG_MOD(info->extack,
+						   "invalid target network namespace ID");
+			NL_SET_BAD_ATTR(info->extack,
+					info->attrs[OVPN_A_TARGET_NETNSID]);
+			return ERR_CAST(target_net);
+		}
+		net = target_net;
+	}
+
 	rcu_read_lock();
 	dev = dev_get_by_index_rcu(net, ifindex);
 	if (!dev) {
 		rcu_read_unlock();
 		NL_SET_ERR_MSG_MOD(info->extack,
 				   "ifindex does not match any interface");
+		put_net(target_net);
 		return ERR_PTR(-ENODEV);
 	}
 
@@ -59,12 +81,14 @@ ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
 		NL_SET_ERR_MSG_MOD(info->extack,
 				   "specified interface is not ovpn");
 		NL_SET_BAD_ATTR(info->extack, info->attrs[OVPN_A_IFINDEX]);
+		put_net(target_net);
 		return ERR_PTR(-EINVAL);
 	}
 
 	ovpn = netdev_priv(dev);
 	netdev_hold(dev, tracker, GFP_ATOMIC);
 	rcu_read_unlock();
+	put_net(target_net);
 
 	return ovpn;
 }
@@ -73,7 +97,7 @@ int ovpn_nl_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
 		     struct genl_info *info)
 {
 	netdevice_tracker *tracker = (netdevice_tracker *)&info->user_ptr[1];
-	struct ovpn_priv *ovpn = ovpn_get_dev_from_attrs(genl_info_net(info),
+	struct ovpn_priv *ovpn = ovpn_get_dev_from_attrs(NETLINK_CB(skb).sk,
 							 info, tracker);
 
 	if (IS_ERR(ovpn))
@@ -738,7 +762,7 @@ int ovpn_nl_peer_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
 	struct ovpn_priv *ovpn;
 	struct ovpn_peer *peer;
 
-	ovpn = ovpn_get_dev_from_attrs(sock_net(cb->skb->sk), info, &tracker);
+	ovpn = ovpn_get_dev_from_attrs(NETLINK_CB(cb->skb).sk, info, &tracker);
 	if (IS_ERR(ovpn))
 		return PTR_ERR(ovpn);
 
include/uapi/linux/ovpn.h +1 -0
index 06690090a1a95..3fdf24182df43 100644
@@ -85,6 +85,7 @@ enum {
 	OVPN_A_IFINDEX = 1,
 	OVPN_A_PEER,
 	OVPN_A_KEYCONF,
+	OVPN_A_TARGET_NETNSID,
 
 	__OVPN_A_MAX,
 	OVPN_A_MAX = (__OVPN_A_MAX - 1)
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#1 lore raw

PATCH 2/3 ovpn: send notifications in the device netns

vpn notifications are multicast in the network namespace of the peer transport socket, but carry an ifindex that is only meaningful in the ovpn device namespace. If the two namespaces differ, listeners resolve it to an unrelated interface, as ifindexes are numbered per netns.

Multicast notifications in the device namespace instead. This also avoids depending on the transport socket when sending a notification.

Fixes 89d3c0e4612a ("ovpn: kill key and notify userspace in case of IV exhaustion")
Fixes a215d253c17a ("ovpn: notify userspace when a peer is deleted")
Fixes c841b676da98 ("ovpn: notify userspace on client float event")
---
 drivers/net/ovpn/netlink.c | 44 +++++---------------------------------
 1 file changed, 5 insertions(+), 39 deletions(-)
drivers/net/ovpn/netlink.c +5 -39
index 04df2c0288a5d..5c94d34219c73 100644
@@ -1188,7 +1188,6 @@ int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info)
  */
 int ovpn_nl_peer_del_notify(struct ovpn_peer *peer)
 {
-	struct ovpn_socket *sock;
 	struct sk_buff *msg;
 	struct nlattr *attr;
 	int ret = -EMSGSIZE;
@@ -1221,23 +1220,12 @@ int ovpn_nl_peer_del_notify(struct ovpn_peer *peer)
 		goto err_cancel_msg;
 
 	nla_nest_end(msg, attr);
-
 	genlmsg_end(msg, hdr);
-
-	rcu_read_lock();
-	sock = rcu_dereference(peer->sock);
-	if (!sock) {
-		ret = -EINVAL;
-		goto err_unlock;
-	}
-	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg, 0,
-				OVPN_NLGRP_PEERS, GFP_ATOMIC);
-	rcu_read_unlock();
+	genlmsg_multicast_netns(&ovpn_nl_family, dev_net(peer->ovpn->dev), msg,
+				0, OVPN_NLGRP_PEERS, GFP_ATOMIC);
 
 	return 0;
 
-err_unlock:
-	rcu_read_unlock();
 err_cancel_msg:
 	genlmsg_cancel(msg, hdr);
 err_free_msg:
@@ -1255,7 +1243,6 @@ int ovpn_nl_peer_del_notify(struct ovpn_peer *peer)
 int ovpn_nl_peer_float_notify(struct ovpn_peer *peer,
 			      const struct sockaddr_storage *ss)
 {
-	struct ovpn_socket *sock;
 	struct sockaddr_in6 *sa6;
 	struct sockaddr_in *sa;
 	struct sk_buff *msg;
@@ -1305,21 +1292,11 @@ int ovpn_nl_peer_float_notify(struct ovpn_peer *peer,
 
 	nla_nest_end(msg, attr);
 	genlmsg_end(msg, hdr);
-
-	rcu_read_lock();
-	sock = rcu_dereference(peer->sock);
-	if (!sock) {
-		ret = -EINVAL;
-		goto err_unlock;
-	}
-	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg,
+	genlmsg_multicast_netns(&ovpn_nl_family, dev_net(peer->ovpn->dev), msg,
 				0, OVPN_NLGRP_PEERS, GFP_ATOMIC);
-	rcu_read_unlock();
 
 	return 0;
 
-err_unlock:
-	rcu_read_unlock();
 err_cancel_msg:
 	genlmsg_cancel(msg, hdr);
 err_free_msg:
@@ -1336,7 +1313,6 @@ int ovpn_nl_peer_float_notify(struct ovpn_peer *peer,
  */
 int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id)
 {
-	struct ovpn_socket *sock;
 	struct nlattr *k_attr;
 	struct sk_buff *msg;
 	int ret = -EMSGSIZE;
@@ -1370,20 +1346,10 @@ int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id)
 
 	nla_nest_end(msg, k_attr);
 	genlmsg_end(msg, hdr);
-
-	rcu_read_lock();
-	sock = rcu_dereference(peer->sock);
-	if (!sock) {
-		ret = -EINVAL;
-		goto err_unlock;
-	}
-	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg, 0,
-				OVPN_NLGRP_PEERS, GFP_ATOMIC);
-	rcu_read_unlock();
+	genlmsg_multicast_netns(&ovpn_nl_family, dev_net(peer->ovpn->dev), msg,
+				0, OVPN_NLGRP_PEERS, GFP_ATOMIC);
 
 	return 0;
-err_unlock:
-	rcu_read_unlock();
 err_cancel_msg:
 	genlmsg_cancel(msg, hdr);
 err_free_msg:
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#2 lore raw

PATCH 3/3 selftests: ovpn: test operations in a foreign netns

In reply to Marco Baffo

Extend ovpn-cli with -n to resolve an interface in a named network namespace and include OVPN_A_TARGET_NETNSID in peer and key requests.

Use -n for existing peer and key operations that do not create transport sockets, exercising foreign-netns requests throughout the test suite.

---
 tools/testing/selftests/net/ovpn/common.sh    |   8 +-
 tools/testing/selftests/net/ovpn/ovpn-cli.c   | 265 +++++++++++++++++-
 .../selftests/net/ovpn/test-close-socket.sh   |   4 +-
 tools/testing/selftests/net/ovpn/test-mark.sh |   6 +-
 tools/testing/selftests/net/ovpn/test.sh      |  50 ++--
 5 files changed, 288 insertions(+), 45 deletions(-)
tools/testing/selftests/net/ovpn/common.sh +4 -4
index 2d844eb3aa6e3..4f2510fb6d7f7 100644
@@ -221,7 +221,7 @@ ovpn_add_peer() {
 				${OVPN_UDP_PEERS_FILE}
 
 			for p in $(seq 1 ${OVPN_NUM_PEERS}); do
-				ip netns exec "${server_ns}" ${OVPN_CLI} \
+				"${OVPN_CLI}" -n "${server_ns}" \
 					new_key tun0 ${p} 1 0 ${OVPN_ALG} 0 \
 					data64.key
 			done
@@ -244,7 +244,7 @@ ovpn_add_peer() {
 			ip netns exec "${peer_ns}" ${OVPN_CLI} new_peer \
 				tun${1} ${PEER_ID} ${TX_ID} ${LPORT} ${RADDR} \
 				${RPORT}
-			ip netns exec "${peer_ns}" ${OVPN_CLI} new_key tun${1} \
+			"${OVPN_CLI}" -n "${peer_ns}" new_key tun${1} \
 				${PEER_ID} 1 0 ${OVPN_ALG} 1 data64.key
 		fi
 	else
@@ -252,8 +252,8 @@ ovpn_add_peer() {
 			(ip netns exec "${server_ns}" ${OVPN_CLI} listen tun0 \
 				1 ${M_ID} ${OVPN_TCP_PEERS_FILE} && {
 				for p in $(seq 1 ${OVPN_NUM_PEERS}); do
-					ip netns exec "${server_ns}" \
-						${OVPN_CLI} new_key tun0 ${p} \
+					"${OVPN_CLI}" -n "${server_ns}" \
+						new_key tun0 ${p} \
 						1 0 ${OVPN_ALG} 0 data64.key
 				done
 			}) &
tools/testing/selftests/net/ovpn/ovpn-cli.c +254 -11

359 lines not on this page · raw message

tools/testing/selftests/net/ovpn/test-close-socket.sh +2 -2
index ec9a51bbf3c92..0e4e33b2973fd 100755
@@ -40,10 +40,10 @@ ovpn_prepare_network() {
 	for p in $(seq 1 ${OVPN_NUM_PEERS}); do
 		peer_ns="ovpn_peer${p}"
 		ovpn_cmd_ok "set peer0 timeout for peer ${p}" \
-			ip netns exec ovpn_peer0 ${OVPN_CLI} set_peer tun0 \
+			"${OVPN_CLI}" -n ovpn_peer0 set_peer tun0 \
 				${p} 60 120
 		ovpn_cmd_ok "set peer${p} timeout for peer ${p}" \
-			ip netns exec "${peer_ns}" ${OVPN_CLI} set_peer \
+			"${OVPN_CLI}" -n "${peer_ns}" set_peer \
 				tun${p} $((p + OVPN_ID_OFFSET)) 60 120
 	done
 }
tools/testing/selftests/net/ovpn/test-mark.sh +3 -3
index 7c1d56e9c525d..8a9ca52f86be1 100755
@@ -42,7 +42,7 @@ ovpn_mark_prepare_network() {
 			ASYMM "${OVPN_UDP_PEERS_FILE}" "${MARK}"
 	for p in $(seq 1 3); do
 		ovpn_cmd_ok "install server key for peer ${p}" \
-			ip netns exec ovpn_peer0 "${OVPN_CLI}" new_key tun0 \
+			"${OVPN_CLI}" -n ovpn_peer0 new_key tun0 \
 				"${p}" 1 0 "${OVPN_ALG}" 0 data64.key
 	done
 
@@ -53,10 +53,10 @@ ovpn_mark_prepare_network() {
 	for p in $(seq 1 3); do
 		peer_ns="ovpn_peer${p}"
 		ovpn_cmd_ok "set peer0 timeout for peer ${p}" \
-			ip netns exec ovpn_peer0 "${OVPN_CLI}" set_peer tun0 \
+			"${OVPN_CLI}" -n ovpn_peer0 set_peer tun0 \
 				"${p}" 60 120
 		ovpn_cmd_ok "set peer${p} timeout for peer ${p}" \
-			ip netns exec "${peer_ns}" "${OVPN_CLI}" set_peer \
+			"${OVPN_CLI}" -n "${peer_ns}" set_peer \
 				tun"${p}" $((p + OVPN_ID_OFFSET)) 60 120
 	done
 }
tools/testing/selftests/net/ovpn/test.sh +25 -25

134 lines not on this page · raw message

Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel