Skip to contentSkip to conversation

libnetlink: fix leak and using unused memory on error

PATCH In review Stephen Hemminger 3 participants 4 messages Sep 14, 2018 – Sep 17, 2018

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

PATCH 1/1 libnetlink: fix leak and using unused memory on error

If an error happens in multi-segment message (tc only) then report the error and stop processing further responses. This also fixes refering to the buffer after free.

The sequence check is not necessary here because the response message has already been validated to be in the window of the sequence number of the iov.

Fixes 7b2ee50c0cd5 ("hv_netvsc: common detach logic")
---
 lib/libnetlink.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
lib/libnetlink.c +9 -14
index 928de1dd16d8..586809292594 100644
@@ -617,7 +617,6 @@ static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
 	msg.msg_iovlen = 1;
 	i = 0;
 	while (1) {
-next:
 		status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
 		++i;
 
@@ -660,27 +659,23 @@ next:
 
 				if (l < sizeof(struct nlmsgerr)) {
 					fprintf(stderr, "ERROR truncated\n");
-				} else if (!err->error) {
+					free(buf);
+					return -1;
+				}
+
+				if (!err->error)
 					/* check messages from kernel */
 					nl_dump_ext_ack(h, errfn);
 
-					if (answer)
-						*answer = (struct nlmsghdr *)buf;
-					else
-						free(buf);
-					if (h->nlmsg_seq == seq)
-						return 0;
-					else if (i < iovlen)
-						goto next;
-					return 0;
-				}
-
 				if (rtnl->proto != NETLINK_SOCK_DIAG &&
 				    show_rtnl_err)
 					rtnl_talk_error(h, err, errfn);
 
 				errno = -err->error;
-				free(buf);
+				if (answer)
+					*answer = (struct nlmsghdr *)buf;
+				else
+					free(buf);
 				return -i;
 			}
 
Signature · 1 line
2.18.0
#1 lore raw
Quoted 11 lines · Stephen Hemminger

If an error happens in multi-segment message (tc only) then report the error and stop processing further responses. This also fixes refering to the buffer after free.

The sequence check is not necessary here because the response message has already been validated to be in the window of the sequence number of the iov.

Fixes 7b2ee50c0cd5 ("hv_netvsc: common detach logic")
Quoted 56 lines
---
 lib/libnetlink.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
lib/libnetlink.c +9 -15
index 928de1dd16d8..586809292594 100644
@@ -617,7 +617,6 @@ static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
        msg.msg_iovlen = 1;
        i = 0;
        while (1) {
-next:
                status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
                ++i;
 
@@ -660,27 +659,23 @@ next:
 
                                if (l < sizeof(struct nlmsgerr)) {
                                        fprintf(stderr, "ERROR truncated\n");
-                               } else if (!err->error) {
+                                       free(buf);
+                                       return -1;
+                               }
+
+                               if (!err->error)
                                        /* check messages from kernel */
                                        nl_dump_ext_ack(h, errfn);
 
-                                       if (answer)
-                                               *answer = (struct nlmsghdr *)buf;
-                                       else
-                                               free(buf);
-                                       if (h->nlmsg_seq == seq)
-                                               return 0;
-                                       else if (i < iovlen)
-                                               goto next;
-                                       return 0;
-                               }
-
                                if (rtnl->proto != NETLINK_SOCK_DIAG &&
                                    show_rtnl_err)
                                        rtnl_talk_error(h, err, errfn);
 
                                errno = -err->error;
-                               free(buf);
+                               if (answer)
+                                       *answer = (struct nlmsghdr *)buf;
+                               else
+                                       free(buf);
                                return -i;
                        }
 
--

2.18.0

#2 lore raw
Quoted 6 lines · Mahesh Bandewar (महेश बंडेवार)
Quoted 4 lines · Stephen Hemminger

If an error happens in multi-segment message (tc only) then report the error and stop processing further responses. This also fixes refering to the buffer after free.

Quoted 6 lines
Quoted 6 lines

The sequence check is not necessary here because the response message has already been validated to be in the window of the sequence number of the iov.

Fixes 7b2ee50c0cd5 ("hv_netvsc: common detach logic")
$ git show 7b2ee50c0cd5
fatal: ambiguous argument '7b2ee50c0cd5': unknown revision or path not
in the working tree.

but we knew that since iproute2 does not have hv_netsvc code. Copy and paste error?

#3 lore raw
In reply to Stephen Hemminger
Quoted 10 lines · On Thu, 13 Sep 2018 12:33:38 -0700 Stephen Hemminger…

If an error happens in multi-segment message (tc only) then report the error and stop processing further responses. This also fixes refering to the buffer after free.

The sequence check is not necessary here because the response message has already been validated to be in the window of the sequence number of the iov.

Fixes 7b2ee50c0cd5 ("hv_netvsc: common detach logic")
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Bad habit of working on two projects as once.