Skip to contentSkip to conversation

Do not listen if rtnl_send() fails in ip link iplink_have_newlink() test

PATCH In review Petr Písař 2 participants 6 messages Dec 11, 2013 – Jan 3, 2014

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

PATCH 1/1 Do not listen if rtnl_send() fails in ip link iplink_have_newlink() test

If rtnl_send() fails in iplink_have_newlink() test, listening for response will result in indefinite hang. This can be demonstrated by "ip link show" while SELinux preventing from sending the RTM_NEWLINK over netlink.

This patch checks for the return value as is done at all other rtnl_send() calls.

---
 ip/iplink.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ip/iplink.c +2 -2
index 58b6c20..f7d9e17 100644
@@ -178,8 +178,8 @@ static int iplink_have_newlink(void)
 		req.n.nlmsg_type = RTM_NEWLINK;
 		req.i.ifi_family = AF_UNSPEC;
 
-		rtnl_send(&rth, &req.n, req.n.nlmsg_len);
-		rtnl_listen(&rth, accept_msg, NULL);
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) >= 0)
+			rtnl_listen(&rth, accept_msg, NULL);
 	}
 	return have_rtnl_newlink;
 }
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 18 lines · On Wed, 11 Dec 2013 14:01:29 +0100 Petr Písař <[email protected]> wrote:

If rtnl_send() fails in iplink_have_newlink() test, listening for response will result in indefinite hang. This can be demonstrated by "ip link show" while SELinux preventing from sending the RTM_NEWLINK over netlink.

This patch checks for the return value as is done at all other rtnl_send() calls.

---
 ip/iplink.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ip/iplink.c +0 -0
index 58b6c20..f7d9e17 100644

ip/iplink.c static int iplink_have_newlink(void) · line 185

Quoted 10 lines
ip/iplink.c +2 -2
@@ -178,8 +178,8 @@ static int iplink_have_newlink(void)
 		req.n.nlmsg_type = RTM_NEWLINK;
 		req.i.ifi_family = AF_UNSPEC;
 
-		rtnl_send(&rth, &req.n, req.n.nlmsg_len);
-		rtnl_listen(&rth, accept_msg, NULL);
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) >= 0)
+			rtnl_listen(&rth, accept_msg, NULL);
 	}
 	return have_rtnl_newlink;
 }

I think it should print an error messag, not silently ignore the send failure.

#2 lore raw

PATCH 1/1 Do not listen if rtnl_send() fails in ip link iplink_have_newlink() test

If rtnl_send() fails in iplink_have_newlink() test, listening for response will result in indefinite hang. This can be demonstrated by "ip link show" while SELinux preventing from sending the RTM_NEWLINK over netlink.

This patch checks for the return value as is done at all other rtnl_send() calls. It falls back to IOCTL in case of failure.

---
 ip/iplink.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
ip/iplink.c +7 -2
index 58b6c20..5132868 100644
@@ -178,8 +178,13 @@ static int iplink_have_newlink(void)
 		req.n.nlmsg_type = RTM_NEWLINK;
 		req.i.ifi_family = AF_UNSPEC;
 
-		rtnl_send(&rth, &req.n, req.n.nlmsg_len);
-		rtnl_listen(&rth, accept_msg, NULL);
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
+			perror("Could not check for "
+				"link configuration over netlink support");
+			have_rtnl_newlink = 0;
+		} else {
+			rtnl_listen(&rth, accept_msg, NULL);
+		}
 	}
 	return have_rtnl_newlink;
 }
Signature · 1 line
1.8.3.1
#3 lore raw

I took your idea and enhanced it to all of iproute2 by doing the following:

From c4b6330a3a033bd9c9b0664c5f844493137ae599 Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <[email protected]>
Date: Fri, 20 Dec 2013 08:15:02 -0800
Subject: [PATCH] check return value of rtnl_send and related functions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Use warn_unused_result to enforce checking return value of rtnl_send, and fix where the errors are.

Suggested by initial patch from Petr Písař <[email protected]>

---
 include/libnetlink.h |   28 ++++++++++++++++++++--------
 ip/iplink.c          |    5 ++++-
 ip/ipnetconf.c       |    5 ++++-
 misc/arpd.c          |    6 +++++-
 misc/ss.c            |    4 +++-
 5 files changed, 36 insertions(+), 12 deletions(-)
include/libnetlink.h +20 -8
index ec3d657..fe7d5d3 100644
@@ -22,13 +22,22 @@ struct rtnl_handle
 
 extern int rcvbuf;
 
-extern int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions);
-extern int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions, int protocol);
+extern int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
+	__attribute__((warn_unused_result));
+
+extern int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
+			     int protocol)
+	__attribute__((warn_unused_result));
+
 extern void rtnl_close(struct rtnl_handle *rth);
-extern int rtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type);
+extern int rtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type)
+	__attribute__((warn_unused_result));
 extern int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int fam, int type,
-				    __u32 filt_mask);
-extern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len);
+				    __u32 filt_mask)
+	__attribute__((warn_unused_result));
+extern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
+			     int len)
+	__attribute__((warn_unused_result));
 
 typedef int (*rtnl_filter_t)(const struct sockaddr_nl *,
 			     struct nlmsghdr *n, void *);
@@ -44,9 +53,12 @@ extern int rtnl_dump_filter_l(struct rtnl_handle *rth,
 extern int rtnl_dump_filter(struct rtnl_handle *rth, rtnl_filter_t filter,
 			    void *arg);
 extern int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
-		     unsigned groups, struct nlmsghdr *answer);
-extern int rtnl_send(struct rtnl_handle *rth, const void *buf, int);
-extern int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int);
+		     unsigned groups, struct nlmsghdr *answer)
+	__attribute__((warn_unused_result));
+extern int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
+	__attribute__((warn_unused_result));
+extern int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
+	__attribute__((warn_unused_result));
 
 extern int addattr(struct nlmsghdr *n, int maxlen, int type);
 extern int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
ip/iplink.c +4 -1
index 58b6c20..e0c14e6 100644
@@ -178,7 +178,10 @@ static int iplink_have_newlink(void)
 		req.n.nlmsg_type = RTM_NEWLINK;
 		req.i.ifi_family = AF_UNSPEC;
 
-		rtnl_send(&rth, &req.n, req.n.nlmsg_len);
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
+			perror("request send failed");
+			exit(1);
+		}
 		rtnl_listen(&rth, accept_msg, NULL);
 	}
 	return have_rtnl_newlink;
ip/ipnetconf.c +4 -1
index 9a77ecb..37aaf45 100644
@@ -161,7 +161,10 @@ static int do_show(int argc, char **argv)
 			addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
 				  &filter.ifindex, sizeof(filter.ifindex));
 
-		rtnl_send(&rth, &req.n, req.n.nlmsg_len);
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
+			perror("Can not send request");
+			exit(1);
+		}
 		rtnl_listen(&rth, print_netconf, stdout);
 	} else {
 dump:
misc/arpd.c +5 -1
index ec9d570..d293b70 100644
@@ -428,7 +428,11 @@ static int do_one_request(struct nlmsghdr *n)
 
 static void load_initial_table(void)
 {
-	rtnl_wilddump_request(&rth, AF_INET, RTM_GETNEIGH);
+	if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETNEIGH) < 0) {
+		perrror("dump request failed");
+		exit(1);
+	}
+		
 }
 
 static void get_kern_msg(void)
misc/ss.c +3 -1
index 6f38ae7..e59ca5c 100644
@@ -996,7 +996,9 @@ static int xll_initted = 0;
 static void xll_init(void)
 {
 	struct rtnl_handle rth;
-	rtnl_open(&rth, 0);
+	if (rtnl_open(&rth, 0) < 0)
+		exit(1);
+
 	ll_init_map(&rth);
 	rtnl_close(&rth);
 	xll_initted = 1;
Signature · 1 line
1.7.10.4
author
#4 lore raw
Quoted 13 lines · Stephen Hemminger

I took your idea and enhanced it to all of iproute2 by doing the following:

From c4b6330a3a033bd9c9b0664c5f844493137ae599 Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <[email protected]>
Date: Fri, 20 Dec 2013 08:15:02 -0800
Subject: [PATCH] check return value of rtnl_send and related functions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Use warn_unused_result to enforce checking return value of rtnl_send, and fix where the errors are.

[...]

Quoted 6 lines
ip/iplink.c +0 -0
index 58b6c20..e0c14e6 100644
@@ -178,7 +178,10 @@ static int iplink_have_newlink(void)
 		req.n.nlmsg_type = RTM_NEWLINK;

ip/iplink.c static int iplink_have_newlink(void) · line 187

Quoted 10 lines
ip/iplink.c +4 -1
 		req.i.ifi_family = AF_UNSPEC;
 
-		rtnl_send(&rth, &req.n, req.n.nlmsg_len);
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
+			perror("request send failed");
+			exit(1);
+		}
 		rtnl_listen(&rth, accept_msg, NULL);
 	}
 	return have_rtnl_newlink;

This one exits instead of falling back to IOCTL. iplink_have_newlink() is called even from iplink_usage(). This even prevents from printing usage.

I think failure in this very place should be treated as I suggested.

-- Petr

#5 lore raw
Quoted 35 lines · On Thu, 2 Jan 2014 08:42:57 +0100 Petr Pisar <[email protected]> wrote:
Quoted 13 lines · Stephen Hemminger

I took your idea and enhanced it to all of iproute2 by doing the following:

From c4b6330a3a033bd9c9b0664c5f844493137ae599 Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <[email protected]>
Date: Fri, 20 Dec 2013 08:15:02 -0800
Subject: [PATCH] check return value of rtnl_send and related functions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Use warn_unused_result to enforce checking return value of rtnl_send, and fix where the errors are.

[...]

Quoted 6 lines
ip/iplink.c +0 -0
index 58b6c20..e0c14e6 100644
@@ -178,7 +178,10 @@ static int iplink_have_newlink(void)
 		req.n.nlmsg_type = RTM_NEWLINK;

ip/iplink.c static int iplink_have_newlink(void) · line 187

Quoted 10 lines
ip/iplink.c +4 -1
 		req.i.ifi_family = AF_UNSPEC;
 
-		rtnl_send(&rth, &req.n, req.n.nlmsg_len);
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
+			perror("request send failed");
+			exit(1);
+		}
 		rtnl_listen(&rth, accept_msg, NULL);
 	}
 	return have_rtnl_newlink;

This one exits instead of falling back to IOCTL. iplink_have_newlink() is called even from iplink_usage(). This even prevents from printing usage.

Quoted 3 lines

I think failure in this very place should be treated as I suggested.

-- Petr

The code gets used in multiple paths, some should fail and others have fallback. I will fix the cases where fallback is possible.