Skip to content

Commit 5bc6526

Browse files
committed
read nlmsghdr first before reading the content
so we can get the total length of the payload and allocate buffer properly instead of allocating one large buffer. Signed-off-by: Daniel, Dao Quang Minh <[email protected]>
1 parent 3c3a437 commit 5bc6526

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

libcontainer/nsenter/nsexec.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,15 @@ void nsexec()
109109
exit(1);
110110
}
111111

112-
static char nlbuf[16384];
113-
struct iovec iov = { nlbuf, sizeof(nlbuf) };
114-
struct msghdr msg;
112+
char nlbuf[NLMSG_HDRLEN];
115113
struct nlmsghdr *nh;
116-
117-
memset(&msg, 0, sizeof(msg));
118-
msg.msg_name = &nh;
119-
msg.msg_namelen = sizeof(nh);
120-
msg.msg_iov = &iov;
121-
msg.msg_iovlen = 1;
122-
while (1) {
123-
len = recvmsg(pipenum, &msg, 0);
124-
if (len <= 0) {
125-
pr_perror("invalid netlink init message size %d", len);
126-
exit(1);
127-
}
128-
break;
114+
len = read(pipenum, nlbuf, NLMSG_HDRLEN);
115+
if (len <= 0) {
116+
pr_perror("invalid netlink init message size %d", len);
117+
exit(1);
129118
}
130119

131120
nh = (struct nlmsghdr *)nlbuf;
132-
if (NLMSG_OK(nh, len) != 1) {
133-
pr_perror("malformed message");
134-
exit(1);
135-
};
136121
if (nh->nlmsg_type == NLMSG_ERROR) {
137122
pr_perror("failed to read netlink message");
138123
exit(1);
@@ -141,12 +126,18 @@ void nsexec()
141126
pr_perror("unexpected msg type %d", nh->nlmsg_type);
142127
exit(1);
143128
}
129+
// read the netlink payload
130+
len = NLMSG_PAYLOAD(nh, 0);
131+
char data[len];
132+
len = read(pipenum, data, len);
133+
if (len <= 0) {
134+
pr_perror("failed to read netlink message data with len %d", len);
135+
exit(1);
136+
}
144137

145-
int total = NLMSG_PAYLOAD(nh, 0);
146-
char *data = NLMSG_DATA(nh);
147138
int start = 0;
148139
struct nlattr *attr;
149-
while (start < total) {
140+
while (start < len) {
150141
int payload_len;
151142
attr = (struct nlattr *)((void *)data + start);
152143
start += NLA_HDRLEN;

0 commit comments

Comments
 (0)