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, \"abcd\", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
87 fd, (unsigned) sizeof(req->magic), sprintrc(rc));
89 /* a single message with some data */
90 rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
91 printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
92 ", seq=0, pid=0}, \"abcd\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
93 fd, req->nlh.nlmsg_len, NLM_F_DUMP,
94 (unsigned) sizeof(*req), sprintrc(rc));
96 /* a single message without data */
97 req->nlh.nlmsg_len = sizeof(req->nlh);
98 rc = sendto(fd, &req->nlh, sizeof(req->nlh), MSG_DONTWAIT, NULL, 0);
99 printf("sendto(%d, {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
100 ", seq=0, pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
101 fd, req->nlh.nlmsg_len, NLM_F_DUMP,
102 (unsigned) sizeof(req->nlh), sprintrc(rc));
104 /* nlmsg_len > whole message length */
105 req->nlh.nlmsg_len = sizeof(*req) + 8;
106 rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
107 printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
108 ", seq=0, pid=0}, \"abcd\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
109 fd, req->nlh.nlmsg_len, NLM_F_DUMP,
110 (unsigned) sizeof(*req), sprintrc(rc));
112 /* nlmsg_len < sizeof(struct nlmsghdr) */
113 req->nlh.nlmsg_len = 8;
114 rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
115 printf("sendto(%d, {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
116 ", seq=0, pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
117 fd, req->nlh.nlmsg_len, NLM_F_DUMP,
118 (unsigned) sizeof(*req), sprintrc(rc));
120 /* a sequence of two nlmsg objects */
123 char padding[NLMSG_ALIGN(sizeof(struct req)) - sizeof(struct req)];
125 } *const reqs = tail_alloc(sizeof(*reqs));
126 memcpy(&reqs->req1, &c_req, sizeof(c_req));
127 memcpy(&reqs->req2, &c_req, sizeof(c_req));
129 rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
130 printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
131 ", seq=0, pid=0}, \"abcd\"}, {{len=%u, type=NLMSG_NOOP"
132 ", flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}, \"abcd\"}]"
133 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
134 fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
135 reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
136 (unsigned) sizeof(*reqs), sprintrc(rc));
138 /* unfetchable second struct nlmsghdr */
139 void *const efault2 = tail_memdup(&reqs->req1, sizeof(reqs->req1));
140 rc = sendto(fd, efault2, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
141 printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
142 ", seq=0, pid=0}, \"abcd\"}, %p], %u, MSG_DONTWAIT, NULL, 0)"
144 fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
145 &((struct reqs *) efault2)->req2, (unsigned) sizeof(*reqs),
148 /* message length is not enough for the second struct nlmsghdr */
149 rc = sendto(fd, reqs, sizeof(*reqs) - sizeof(req->nlh), MSG_DONTWAIT,
151 errstr = sprintrc(rc);
152 printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
153 ", seq=0, pid=0}, \"abcd\"}, ",
154 fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP);
155 print_quoted_memory((void *) &reqs->req2.nlh,
156 sizeof(reqs->req2) - sizeof(req->nlh));
157 printf("], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
158 (unsigned) (sizeof(*reqs) - sizeof(req->nlh)), errstr);
160 /* second nlmsg_len < sizeof(struct nlmsghdr) */
161 reqs->req2.nlh.nlmsg_len = 4;
162 rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
163 printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
164 ", seq=0, pid=0}, \"abcd\"}, {len=%u, type=NLMSG_NOOP"
165 ", flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}], %u"
166 ", MSG_DONTWAIT, NULL, 0) = %s\n",
167 fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
168 reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
169 (unsigned) sizeof(*reqs), sprintrc(rc));
171 /* abbreviated output */
172 # define ABBREV_LEN (DEFAULT_STRLEN + 1)
173 const unsigned int msg_len = sizeof(struct nlmsghdr) * ABBREV_LEN;
174 struct nlmsghdr *const msgs = tail_alloc(msg_len);
176 for (i = 0; i < ABBREV_LEN; ++i) {
177 msgs[i].nlmsg_len = sizeof(*msgs);
178 msgs[i].nlmsg_type = NLMSG_NOOP;
179 msgs[i].nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
180 msgs[i].nlmsg_seq = i;
181 msgs[i].nlmsg_pid = 0;
184 rc = sendto(fd, msgs, msg_len, MSG_DONTWAIT, NULL, 0);
185 errstr = sprintrc(rc);
186 printf("sendto(%d, [", fd);
187 for (i = 0; i < DEFAULT_STRLEN; ++i) {
190 printf("{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
192 msgs[i].nlmsg_len, NLM_F_DUMP, msgs[i].nlmsg_seq);
194 printf(", ...], %u, MSG_DONTWAIT, NULL, 0) = %s\n", msg_len, errstr);
198 test_nlmsgerr(const int fd)
200 struct nlmsgerr *err;
201 struct nlmsghdr *nlh;
202 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
205 /* error message without enough room for the error code */
207 nlh->nlmsg_len = NLMSG_HDRLEN + 4;
208 nlh->nlmsg_type = NLMSG_ERROR;
209 nlh->nlmsg_flags = NLM_F_REQUEST;
213 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
214 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
215 ", seq=0, pid=0}, %p}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
216 fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
217 nlh->nlmsg_len, sprintrc(rc));
219 nlh->nlmsg_len = NLMSG_HDRLEN + 2;
221 memmove(nlh, nlh0, sizeof(*nlh));
222 memcpy(NLMSG_DATA(nlh), "42", 2);
224 rc = sendto(fd, nlh, NLMSG_HDRLEN + 2, MSG_DONTWAIT, NULL, 0);
225 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
226 ", seq=0, pid=0}, \"42\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
227 fd, NLMSG_HDRLEN + 2, NLMSG_HDRLEN + 2, sprintrc(rc));
229 /* error message with room for the error code only */
230 nlh = nlh0 - sizeof(err->error);
231 nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(err->error);
232 nlh->nlmsg_type = NLMSG_ERROR;
233 nlh->nlmsg_flags = NLM_F_REQUEST;
236 err = NLMSG_DATA(nlh);
239 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
240 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
241 ", seq=0, pid=0}, {error=42}}, %u, MSG_DONTWAIT, NULL, 0)"
242 " = %s\n", fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc));
246 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
247 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
248 ", seq=0, pid=0}, {error=-EPERM}}, %u, MSG_DONTWAIT, NULL, 0)"
249 " = %s\n", fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc));
252 nlh->nlmsg_len += sizeof(err->msg.nlmsg_len);
254 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
255 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
256 ", seq=0, pid=0}, {error=-32767, msg=%p}}"
257 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
258 fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
259 nlh->nlmsg_len, sprintrc(rc));
261 /* error message with room for the error code and a header */
262 nlh = nlh0 - sizeof(*err);
263 nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(*err);
264 nlh->nlmsg_type = NLMSG_ERROR;
265 nlh->nlmsg_flags = NLM_F_REQUEST;
268 err = NLMSG_DATA(nlh);
270 err->msg.nlmsg_len = NLMSG_HDRLEN;
271 err->msg.nlmsg_type = NLMSG_NOOP;
272 err->msg.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
273 err->msg.nlmsg_seq = 42;
274 err->msg.nlmsg_pid = 1234;
276 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
277 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
278 ", seq=0, pid=0}, {error=-EACCES"
279 ", msg={len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
280 ", seq=%u, pid=%u}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
281 fd, nlh->nlmsg_len, err->msg.nlmsg_len, NLM_F_DUMP,
282 err->msg.nlmsg_seq, err->msg.nlmsg_pid,
283 nlh->nlmsg_len, sprintrc(rc));
285 /* error message with room for the error code, a header, and some data */
286 nlh = nlh0 - sizeof(*err) - 4;
287 nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(*err) + 4;
288 nlh->nlmsg_type = NLMSG_ERROR;
289 nlh->nlmsg_flags = NLM_F_REQUEST;
292 err = NLMSG_DATA(nlh);
294 err->msg.nlmsg_len = NLMSG_HDRLEN + 4;
295 err->msg.nlmsg_type = NLMSG_NOOP;
296 err->msg.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
297 err->msg.nlmsg_seq = 421;
298 err->msg.nlmsg_pid = 12345;
299 memcpy(NLMSG_DATA(&err->msg), "abcd", 4);
301 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
302 printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
303 ", seq=0, pid=0}, {error=-EACCES"
304 ", msg={{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
305 ", seq=%u, pid=%u}, \"abcd\"}}}, %u, MSG_DONTWAIT, NULL, 0)"
307 fd, nlh->nlmsg_len, err->msg.nlmsg_len, NLM_F_DUMP,
308 err->msg.nlmsg_seq, err->msg.nlmsg_pid,
309 nlh->nlmsg_len, sprintrc(rc));
313 test_nlmsg_done(const int fd)
315 struct nlmsghdr *nlh;
316 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
318 const int num = 0xfacefeed;
320 /* NLMSG_DONE message without enough room for an integer payload */
322 *nlh = (struct nlmsghdr) {
323 .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
324 .nlmsg_type = NLMSG_DONE,
325 .nlmsg_flags = NLM_F_MULTI
328 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
329 printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
330 ", seq=0, pid=0}, %p}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
331 fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
332 nlh->nlmsg_len, sprintrc(rc));
334 /* NLMSG_DONE message with enough room for an oddly short payload */
335 nlh->nlmsg_len = NLMSG_HDRLEN + 2;
337 /* Beware of unaligned access to nlh members. */
338 memmove(nlh, nlh0, sizeof(*nlh));
339 memcpy(NLMSG_DATA(nlh), "42", 2);
341 rc = sendto(fd, nlh, NLMSG_HDRLEN + 2, MSG_DONTWAIT, NULL, 0);
342 printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
343 ", seq=0, pid=0}, \"42\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
344 fd, NLMSG_HDRLEN + 2, NLMSG_HDRLEN + 2, sprintrc(rc));
346 /* NLMSG_DONE message with enough room for an integer payload */
347 nlh = nlh0 - sizeof(num);
348 *nlh = (struct nlmsghdr) {
349 .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
350 .nlmsg_type = NLMSG_DONE,
351 .nlmsg_flags = NLM_F_MULTI
353 memcpy(NLMSG_DATA(nlh), &num, sizeof(num));
355 rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
356 printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
357 ", seq=0, pid=0}, %d}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
358 fd, nlh->nlmsg_len, num, nlh->nlmsg_len, sprintrc(rc));
363 const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
366 if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
367 perror_msg_and_fail("asprintf");
369 if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
370 perror_msg_and_skip("getxattr");
377 puts("+++ exited with 0 +++");
383 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")