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
This commit is contained in:
Lorenzo Colitti 2019-12-02 17:25:28 +09:00
parent c3d38969ca
commit a9027581bd

View file

@ -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);