Skip to content

Commit

Permalink
read nlmsghdr first before reading the content
Browse files Browse the repository at this point in the history
so we can get the total length of the payload and allocate buffer properly
instead of allocating one large buffer.
  • Loading branch information
dqminh committed Nov 22, 2015
1 parent 3c3a437 commit b4c14ba
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions libcontainer/nsenter/nsexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,23 @@ void nsexec()
exit(1);
}

static char nlbuf[16384];
static char nlbuf[NLMSG_HDRLEN];
struct iovec iov = { nlbuf, sizeof(nlbuf) };
struct msghdr msg;
struct nlmsghdr *nh;

memset(&msg, 0, sizeof(msg));
msg.msg_name = &nh;
msg.msg_namelen = sizeof(nh);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
while (1) {
len = recvmsg(pipenum, &msg, 0);
if (len <= 0) {
pr_perror("invalid netlink init message size %d", len);
exit(1);
}
break;

// read the netlink header
len = recvmsg(pipenum, &msg, 0);
if (len <= 0) {
pr_perror("invalid netlink init message size %d", len);
exit(1);
}

nh = (struct nlmsghdr *)nlbuf;
if (NLMSG_OK(nh, len) != 1) {
pr_perror("malformed message");
exit(1);
};
if (nh->nlmsg_type == NLMSG_ERROR) {
pr_perror("failed to read netlink message");
exit(1);
Expand All @@ -141,12 +134,18 @@ void nsexec()
pr_perror("unexpected msg type %d", nh->nlmsg_type);
exit(1);
}
// read the netlink payload
len = NLMSG_PAYLOAD(nh, 0);
char data[len];
len = read(pipenum, data, len);
if (len <= 0) {
pr_perror("failed to read netlink message data with len %d", len);
exit(1);
}

int total = NLMSG_PAYLOAD(nh, 0);
char *data = NLMSG_DATA(nh);
int start = 0;
struct nlattr *attr;
while (start < total) {
while (start < len) {
int payload_len;
attr = (struct nlattr *)((void *)data + start);
start += NLA_HDRLEN;
Expand Down

0 comments on commit b4c14ba

Please sign in to comment.