Skip to content

Commit 3201522

Browse files
committed
fix lldpad netlink heap access overrun
Signed-off-by: Chunjie Zhu <chunjie.zhu@cloud.com>
1 parent 00b3c78 commit 3201522

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lldp_util.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,9 @@ static struct nla_policy ifla_info_policy[IFLA_INFO_MAX + 1] =
672672

673673
int is_macvtap(const char *ifname)
674674
{
675-
int ret, s;
675+
int ret, s, realsize;
676676
struct nlmsghdr *nlh;
677+
void *temp;
677678
struct ifinfomsg *ifinfo;
678679
struct nlattr *tb[IFLA_MAX+1],
679680
*tb2[IFLA_INFO_MAX+1];
@@ -684,14 +685,12 @@ int is_macvtap(const char *ifname)
684685
return false;
685686
}
686687

687-
nlh = malloc(NLMSG_SIZE);
688+
nlh = calloc(1, NLMSG_SIZE);
688689

689690
if (!nlh) {
690691
goto out;
691692
}
692693

693-
memset(nlh, 0, NLMSG_SIZE);
694-
695694
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
696695
nlh->nlmsg_type = RTM_GETLINK;
697696
nlh->nlmsg_flags = NLM_F_REQUEST;
@@ -706,10 +705,23 @@ int is_macvtap(const char *ifname)
706705
goto out_free;
707706
}
708707

709-
memset(nlh, 0, NLMSG_SIZE);
708+
do {
709+
realsize = recv(s, NULL, 0, MSG_DONTWAIT | MSG_PEEK | MSG_TRUNC);
710+
} while ((realsize < 0) && errno == EINTR);
711+
712+
if (realsize < 0) {
713+
goto out_free;
714+
}
715+
716+
temp = realloc(nlh, realsize);
717+
if (!temp) {
718+
goto out_free;
719+
}
720+
memset(temp, 0, realsize);
721+
nlh = temp;
710722

711723
do {
712-
ret = recv(s, (void *) nlh, NLMSG_SIZE, MSG_DONTWAIT);
724+
ret = recv(s, (void *) nlh, realsize, MSG_DONTWAIT);
713725
} while ((ret < 0) && errno == EINTR);
714726

715727
if (nlmsg_parse(nlh, sizeof(struct ifinfomsg),

0 commit comments

Comments
 (0)