Skip to content

Commit c416a8d

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 c416a8d

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

libcontainer/nsenter/nsexec.c

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

112-
static char nlbuf[16384];
112+
static char nlbuf[NLMSG_HDRLEN];
113113
struct iovec iov = { nlbuf, sizeof(nlbuf) };
114114
struct msghdr msg;
115115
struct nlmsghdr *nh;
116116

117117
memset(&msg, 0, sizeof(msg));
118-
msg.msg_name = &nh;
119-
msg.msg_namelen = sizeof(nh);
120118
msg.msg_iov = &iov;
121119
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;
120+
121+
// read the netlink header
122+
len = recvmsg(pipenum, &msg, 0);
123+
if (len <= 0) {
124+
pr_perror("invalid netlink init message size %d", len);
125+
exit(1);
129126
}
130127

131128
nh = (struct nlmsghdr *)nlbuf;
132-
if (NLMSG_OK(nh, len) != 1) {
133-
pr_perror("malformed message");
134-
exit(1);
135-
};
136129
if (nh->nlmsg_type == NLMSG_ERROR) {
137130
pr_perror("failed to read netlink message");
138131
exit(1);
@@ -141,12 +134,18 @@ void nsexec()
141134
pr_perror("unexpected msg type %d", nh->nlmsg_type);
142135
exit(1);
143136
}
137+
// read the netlink payload
138+
len = NLMSG_PAYLOAD(nh, 0);
139+
char data[len];
140+
len = read(pipenum, data, len);
141+
if (len <= 0) {
142+
pr_perror("failed to read netlink message data with len %d", len);
143+
exit(1);
144+
}
144145

145-
int total = NLMSG_PAYLOAD(nh, 0);
146-
char *data = NLMSG_DATA(nh);
147146
int start = 0;
148147
struct nlattr *attr;
149-
while (start < total) {
148+
while (start < len) {
150149
int payload_len;
151150
attr = (struct nlattr *)((void *)data + start);
152151
start += NLA_HDRLEN;

0 commit comments

Comments
 (0)