From a9027581bdb6af73fbd2e3702bc79bee782b95cf Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Mon, 2 Dec 2019 17:25:28 +0900 Subject: [PATCH] Properly size response buffer in ifc_act_on_address Currently, any netlink error reported in ifc_act_on_address is reported as EINVAL, because the receive buffer is too short and the NLMSG_OK check fails on it. Make the buffer long enough so the function can return the correct netlink error to the caller. Test: new test coverage in other CL in this topic Change-Id: Ica752db834a0fd614312b800d88721826be08d56 --- libnetutils/ifc_utils.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c index 4e7340125..8212eba29 100644 --- a/libnetutils/ifc_utils.c +++ b/libnetutils/ifc_utils.c @@ -267,19 +267,13 @@ int ifc_act_on_address(int action, const char *name, const char *address, struct { struct nlmsghdr n; struct ifaddrmsg r; - // Allow for IPv6 address, headers, IPv4 broadcast addr and padding. - char attrbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + - NLMSG_ALIGN(sizeof(struct rtattr)) + - NLMSG_ALIGN(INET6_ADDRLEN) + - NLMSG_ALIGN(sizeof(struct rtattr)) + - NLMSG_ALIGN(INET_ADDRLEN)]; + // Allow for IPv4 or IPv6 address, headers, IPv4 broadcast address and padding. + char attrbuf[NLMSG_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(INET6_ADDRLEN) + + NLMSG_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(INET_ADDRLEN)]; } req; struct rtattr *rta; struct nlmsghdr *nh; struct nlmsgerr *err; - char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + - NLMSG_ALIGN(sizeof(struct nlmsgerr)) + - NLMSG_ALIGN(sizeof(struct nlmsghdr))]; // Get interface ID. ifindex = if_nametoindex(name); @@ -348,6 +342,7 @@ int ifc_act_on_address(int action, const char *name, const char *address, return -saved_errno; } + char buf[NLMSG_ALIGN(sizeof(struct nlmsgerr)) + sizeof(req)]; len = recv(s, buf, sizeof(buf), 0); saved_errno = errno; close(s);