2 * Check decoding of netlink protocol.
4 * Copyright (c) 2014-2017 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifdef HAVE_SYS_XATTR_H
39 # include <sys/xattr.h>
40 # include <netinet/in.h>
42 # include <linux/sock_diag.h>
43 # include <linux/netlink_diag.h>
46 send_query(const int fd)
48 static const struct req {
53 .nlmsg_len = sizeof(struct req),
54 .nlmsg_type = NLMSG_NOOP,
55 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
59 struct req *const req = tail_memdup(&c_req, sizeof(c_req));
64 rc = sendto(fd, NULL, sizeof(*req), MSG_DONTWAIT, NULL, 0);
65 printf("sendto(%d, NULL, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
66 fd, (unsigned) sizeof(*req), sprintrc(rc));
69 rc = sendto(fd, req, 0, MSG_DONTWAIT, NULL, 0);
70 printf("sendto(%d, \"\", 0, MSG_DONTWAIT, NULL, 0) = %s\n",
73 /* zero address and length */
74 rc = sendto(fd, NULL, 0, MSG_DONTWAIT, NULL, 0);
75 printf("sendto(%d, NULL, 0, MSG_DONTWAIT, NULL, 0) = %s\n",
78 /* unfetchable struct nlmsghdr */
79 const void *const efault = tail_alloc(sizeof(struct nlmsghdr) - 1);
80 rc = sendto(fd, efault, sizeof(struct nlmsghdr), MSG_DONTWAIT, NULL, 0);
81 printf("sendto(%d, %p, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
82 fd, efault, (unsigned) sizeof(struct nlmsghdr), sprintrc(rc));
84 /* whole message length < sizeof(struct nlmsghdr) */
85 rc = sendto(fd, req->magic, sizeof(req->magic), MSG_DONTWAIT, NULL, 0);
86 printf("sendto(%d, \"\\x61\\x62\\x63\\x64\""
87 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
88 fd, (unsigned) sizeof(req->magic), sprintrc(rc));
90 /* a single message with some data */
91 rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
92 printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
93 ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
94 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
95 fd, req->nlh.nlmsg_len, NLM_F_DUMP,
96 (unsigned) sizeof(*req), sprintrc(rc));
98 /* a single message without data */
99 req->nlh.nlmsg_len = sizeof(req->nlh);
100 rc = sendto(fd, &req->nlh, sizeof(req->nlh), MSG_DONTWAIT, NULL, 0);
101 printf("sendto(%d, {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
102 ", seq=0, pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
103 fd, req->nlh.nlmsg_len, NLM_F_DUMP,
104 (unsigned) sizeof(req->nlh), sprintrc(rc));
106 /* nlmsg_len > whole message length */
107 req->nlh.nlmsg_len = sizeof(*req) + 8;
108 rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
109 printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
110 ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
111 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
112 fd, req->nlh.nlmsg_len, NLM_F_DUMP,
113 (unsigned) sizeof(*req), sprintrc(rc));
115 /* nlmsg_len < sizeof(struct nlmsghdr) */
116 req->nlh.nlmsg_len = 8;
117 rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
118 printf("sendto(%d, {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
119 ", seq=0, pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
120 fd, req->nlh.nlmsg_len, NLM_F_DUMP,
121 (unsigned) sizeof(*req), sprintrc(rc));
123 /* a sequence of two nlmsg objects */
126 char padding[NLMSG_ALIGN(sizeof(struct req)) - sizeof(struct req)];
128 } *const reqs = tail_alloc(sizeof(*reqs));
129 memcpy(&reqs->req1, &c_req, sizeof(c_req));
130 memcpy(&reqs->req2, &c_req, sizeof(c_req));
132 rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
133 printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
134 ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
135 ", {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
136 ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}]"
137 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
138 fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
139 reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
140 (unsigned) sizeof(*reqs), sprintrc(rc));
142 /* unfetchable second struct nlmsghdr */
143 void *const efault2 = tail_memdup(&reqs->req1, sizeof(reqs->req1));
144 rc = sendto(fd, efault2, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
145 printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
146 ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
147 ", %p], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
148 fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
149 &((struct reqs *) efault2)->req2, (unsigned) sizeof(*reqs),
152 /* message length is not enough for the second struct nlmsghdr */
153 rc = sendto(fd, reqs, sizeof(*reqs) - sizeof(req->nlh), MSG_DONTWAIT,
155 errstr = sprintrc(rc);
156 printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
157 ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}, ",
158 fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP);
159 print_quoted_hex(&reqs->req2.nlh,
160 sizeof(reqs->req2) - sizeof(req->nlh));
161 printf("], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
162 (unsigned) (sizeof(*reqs) - sizeof(req->nlh)), errstr);
164 /* second nlmsg_len < sizeof(struct nlmsghdr) */
165 reqs->req2.nlh.nlmsg_len = 4;
166 rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
167 printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
168 ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
169 ", {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
170 ", seq=0, pid=0}], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
171 fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
172 reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
173 (unsigned) sizeof(*reqs), sprintrc(rc));
175 /* abbreviated output */
176 # define ABBREV_LEN (DEFAULT_STRLEN + 1)
177 const unsigned int msg_len = sizeof(struct nlmsghdr) * ABBREV_LEN;
178 struct nlmsghdr *const msgs = tail_alloc(msg_len);
180 for (i = 0; i < ABBREV_LEN; ++i) {
181 msgs[i].nlmsg_len = sizeof(*msgs);
182 msgs[i].nlmsg_type = NLMSG_NOOP;
183 msgs[i].nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
184 msgs[i].nlmsg_seq = i;
185 msgs[i].nlmsg_pid = 0;
188 rc = sendto(fd, msgs, msg_len, MSG_DONTWAIT, NULL, 0);
189 errstr = sprintrc(rc);
190 printf("sendto(%d, [", fd);
191 for (i = 0; i < DEFAULT_STRLEN; ++i) {
194 printf("{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
196 msgs[i].nlmsg_len, NLM_F_DUMP, msgs[i].nlmsg_seq);
198 printf(", ...], %u, MSG_DONTWAIT, NULL, 0) = %s\n", msg_len, errstr);
202 test_nlmsgerr(const int fd)
204 struct nlmsgerr *err;
205 struct nlmsghdr *nlh;
206 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
209 /* error message without enough room for the error code */
211 nlh->nlmsg_len = NLMSG_HDRLEN + 4;
212 nlh->nlmsg_type = NLMSG_ERROR;
213 nlh->nlmsg_flags = NLM_F_REQUEST;
217 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
218 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
219 ", seq=0, pid=0}, %p}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
220 fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
221 nlh->nlmsg_len, sprintrc(rc));
223 nlh->nlmsg_len = NLMSG_HDRLEN + 2;
225 memmove(nlh, nlh0, sizeof(*nlh));
226 memcpy(NLMSG_DATA(nlh), "42", 2);
228 rc = sendto(fd, nlh, NLMSG_HDRLEN + 2, MSG_DONTWAIT, NULL, 0);
229 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
230 ", seq=0, pid=0}, \"\\x34\\x32\"}"
231 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
232 fd, NLMSG_HDRLEN + 2, NLMSG_HDRLEN + 2, sprintrc(rc));
234 /* error message with room for the error code only */
235 nlh = nlh0 - sizeof(err->error);
236 nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(err->error);
237 nlh->nlmsg_type = NLMSG_ERROR;
238 nlh->nlmsg_flags = NLM_F_REQUEST;
241 err = NLMSG_DATA(nlh);
244 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
245 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
246 ", seq=0, pid=0}, {error=42}}, %u, MSG_DONTWAIT, NULL, 0)"
247 " = %s\n", fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc));
251 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
252 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
253 ", seq=0, pid=0}, {error=-EPERM}}, %u, MSG_DONTWAIT, NULL, 0)"
254 " = %s\n", fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc));
257 nlh->nlmsg_len += sizeof(err->msg.nlmsg_len);
259 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
260 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
261 ", seq=0, pid=0}, {error=-32767, msg=%p}}"
262 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
263 fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
264 nlh->nlmsg_len, sprintrc(rc));
266 /* error message with room for the error code and a header */
267 nlh = nlh0 - sizeof(*err);
268 nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(*err);
269 nlh->nlmsg_type = NLMSG_ERROR;
270 nlh->nlmsg_flags = NLM_F_REQUEST;
273 err = NLMSG_DATA(nlh);
275 err->msg.nlmsg_len = NLMSG_HDRLEN;
276 err->msg.nlmsg_type = NLMSG_NOOP;
277 err->msg.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
278 err->msg.nlmsg_seq = 42;
279 err->msg.nlmsg_pid = 1234;
281 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
282 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
283 ", seq=0, pid=0}, {error=-EACCES"
284 ", msg={len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
285 ", seq=%u, pid=%u}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
286 fd, nlh->nlmsg_len, err->msg.nlmsg_len, NLM_F_DUMP,
287 err->msg.nlmsg_seq, err->msg.nlmsg_pid,
288 nlh->nlmsg_len, sprintrc(rc));
290 /* error message with room for the error code, a header, and some data */
291 nlh = nlh0 - sizeof(*err) - 4;
292 nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(*err) + 4;
293 nlh->nlmsg_type = NLMSG_ERROR;
294 nlh->nlmsg_flags = NLM_F_REQUEST;
297 err = NLMSG_DATA(nlh);
299 err->msg.nlmsg_len = NLMSG_HDRLEN + 4;
300 err->msg.nlmsg_type = NLMSG_NOOP;
301 err->msg.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
302 err->msg.nlmsg_seq = 421;
303 err->msg.nlmsg_pid = 12345;
304 memcpy(NLMSG_DATA(&err->msg), "abcd", 4);
306 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
307 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
308 ", seq=0, pid=0}, {error=-EACCES"
309 ", msg={{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
310 ", seq=%u, pid=%u}, \"\\x61\\x62\\x63\\x64\"}}}"
311 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
312 fd, nlh->nlmsg_len, err->msg.nlmsg_len, NLM_F_DUMP,
313 err->msg.nlmsg_seq, err->msg.nlmsg_pid,
314 nlh->nlmsg_len, sprintrc(rc));
318 test_nlmsg_done(const int fd)
320 struct nlmsghdr *nlh;
321 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
323 const int num = 0xfacefeed;
325 /* NLMSG_DONE message without enough room for an integer payload */
327 *nlh = (struct nlmsghdr) {
328 .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
329 .nlmsg_type = NLMSG_DONE,
330 .nlmsg_flags = NLM_F_MULTI
333 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
334 printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
335 ", seq=0, pid=0}, %p}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
336 fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
337 nlh->nlmsg_len, sprintrc(rc));
339 /* NLMSG_DONE message with enough room for an oddly short payload */
340 nlh->nlmsg_len = NLMSG_HDRLEN + 2;
342 /* Beware of unaligned access to nlh members. */
343 memmove(nlh, nlh0, sizeof(*nlh));
344 memcpy(NLMSG_DATA(nlh), "42", 2);
346 rc = sendto(fd, nlh, NLMSG_HDRLEN + 2, MSG_DONTWAIT, NULL, 0);
347 printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI, seq=0"
348 ", pid=0}, \"\\x34\\x32\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
349 fd, NLMSG_HDRLEN + 2, NLMSG_HDRLEN + 2, sprintrc(rc));
351 /* NLMSG_DONE message with enough room for an integer payload */
352 nlh = nlh0 - sizeof(num);
353 *nlh = (struct nlmsghdr) {
354 .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
355 .nlmsg_type = NLMSG_DONE,
356 .nlmsg_flags = NLM_F_MULTI
358 memcpy(NLMSG_DATA(nlh), &num, sizeof(num));
360 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
361 printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
362 ", seq=0, pid=0}, %d}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
363 fd, nlh->nlmsg_len, num, nlh->nlmsg_len, sprintrc(rc));
366 #if defined NLM_F_CAPPED || defined NLM_F_ACK_TLVS
368 test_ack_flags(const int fd)
371 struct nlmsghdr nlh = {
372 .nlmsg_len = sizeof(nlh),
373 .nlmsg_type = NLMSG_ERROR,
377 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CAPPED,
378 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
379 printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
380 ", flags=NLM_F_REQUEST|NLM_F_CAPPED, seq=0, pid=0}"
381 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
382 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
385 #ifdef NLM_F_ACK_TLVS
386 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK_TLVS;
387 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
388 printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
389 ", flags=NLM_F_REQUEST|NLM_F_ACK_TLVS, seq=0, pid=0}"
390 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
391 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
394 #if defined NLM_F_CAPPED && defined NLM_F_ACK_TLVS
395 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CAPPED | NLM_F_ACK_TLVS;
396 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
397 printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
398 ", flags=NLM_F_REQUEST|NLM_F_CAPPED|NLM_F_ACK_TLVS, seq=0, pid=0}"
399 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
400 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
407 const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
410 if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
411 perror_msg_and_fail("asprintf");
413 if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
414 perror_msg_and_skip("getxattr");
420 #if defined NLM_F_CAPPED || defined NLM_F_ACK_TLVS
424 puts("+++ exited with 0 +++");
430 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")