]> granicus.if.org Git - strace/blob - tests/netlink_protocol.c
netlink: print unrecognized netlink payload in hex
[strace] / tests / netlink_protocol.c
1 /*
2  * Check decoding of netlink protocol.
3  *
4  * Copyright (c) 2014-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
18  *
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.
29  */
30
31 #include "tests.h"
32
33 #ifdef HAVE_SYS_XATTR_H
34
35 # include <stdio.h>
36 # include <stdlib.h>
37 # include <string.h>
38 # include <unistd.h>
39 # include <sys/xattr.h>
40 # include <netinet/in.h>
41 # include "netlink.h"
42 # include <linux/sock_diag.h>
43 # include <linux/netlink_diag.h>
44
45 static void
46 send_query(const int fd)
47 {
48         static const struct req {
49                 struct nlmsghdr nlh;
50                 const char magic[4];
51         } c_req = {
52                 .nlh = {
53                         .nlmsg_len = sizeof(struct req),
54                         .nlmsg_type = NLMSG_NOOP,
55                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
56                 },
57                 .magic = "abcd"
58         };
59         struct req *const req = tail_memdup(&c_req, sizeof(c_req));
60         long rc;
61         const char *errstr;
62
63         /* zero address */
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));
67
68         /* zero length */
69         rc = sendto(fd, req, 0, MSG_DONTWAIT, NULL, 0);
70         printf("sendto(%d, \"\", 0, MSG_DONTWAIT, NULL, 0) = %s\n",
71                fd, sprintrc(rc));
72
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",
76                fd, sprintrc(rc));
77
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));
83
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));
88
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}, \"\\x61\\x62\\x63\\x64\"}"
93                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
94                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
95                (unsigned) sizeof(*req), sprintrc(rc));
96
97         /* a single message without data */
98         req->nlh.nlmsg_len = sizeof(req->nlh);
99         rc = sendto(fd, &req->nlh, sizeof(req->nlh), MSG_DONTWAIT, NULL, 0);
100         printf("sendto(%d, {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
101                ", seq=0, pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
102                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
103                (unsigned) sizeof(req->nlh), sprintrc(rc));
104
105         /* nlmsg_len > whole message length */
106         req->nlh.nlmsg_len = sizeof(*req) + 8;
107         rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
108         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
109                ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
110                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
111                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
112                (unsigned) sizeof(*req), sprintrc(rc));
113
114         /* nlmsg_len < sizeof(struct nlmsghdr) */
115         req->nlh.nlmsg_len = 8;
116         rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
117         printf("sendto(%d, {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
118                ", seq=0, pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
119                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
120                (unsigned) sizeof(*req), sprintrc(rc));
121
122         /* a sequence of two nlmsg objects */
123         struct reqs {
124                 struct req req1;
125                 char padding[NLMSG_ALIGN(sizeof(struct req)) - sizeof(struct req)];
126                 struct req req2;
127         } *const reqs = tail_alloc(sizeof(*reqs));
128         memcpy(&reqs->req1, &c_req, sizeof(c_req));
129         memcpy(&reqs->req2, &c_req, sizeof(c_req));
130
131         rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
132         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
133                ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
134                ", {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
135                ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}]"
136                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
137                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
138                reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
139                (unsigned) sizeof(*reqs), sprintrc(rc));
140
141         /* unfetchable second struct nlmsghdr */
142         void *const efault2 = tail_memdup(&reqs->req1, sizeof(reqs->req1));
143         rc = sendto(fd, efault2, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
144         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
145                ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
146                ", %p], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
147                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
148                &((struct reqs *) efault2)->req2, (unsigned) sizeof(*reqs),
149                sprintrc(rc));
150
151         /* message length is not enough for the second struct nlmsghdr */
152         rc = sendto(fd, reqs, sizeof(*reqs) - sizeof(req->nlh), MSG_DONTWAIT,
153                     NULL, 0);
154         errstr = sprintrc(rc);
155         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
156                ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}, ",
157                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP);
158         print_quoted_memory((void *) &reqs->req2.nlh,
159                             sizeof(reqs->req2) - sizeof(req->nlh));
160         printf("], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
161                (unsigned) (sizeof(*reqs) - sizeof(req->nlh)), errstr);
162
163         /* second nlmsg_len < sizeof(struct nlmsghdr) */
164         reqs->req2.nlh.nlmsg_len = 4;
165         rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
166         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
167                ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
168                ", {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
169                ", seq=0, pid=0}], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
170                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
171                reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
172                (unsigned) sizeof(*reqs), sprintrc(rc));
173
174         /* abbreviated output */
175 # define ABBREV_LEN (DEFAULT_STRLEN + 1)
176         const unsigned int msg_len = sizeof(struct nlmsghdr) * ABBREV_LEN;
177         struct nlmsghdr *const msgs = tail_alloc(msg_len);
178         unsigned int i;
179         for (i = 0; i < ABBREV_LEN; ++i) {
180                 msgs[i].nlmsg_len = sizeof(*msgs);
181                 msgs[i].nlmsg_type = NLMSG_NOOP;
182                 msgs[i].nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
183                 msgs[i].nlmsg_seq = i;
184                 msgs[i].nlmsg_pid = 0;
185         }
186
187         rc = sendto(fd, msgs, msg_len, MSG_DONTWAIT, NULL, 0);
188         errstr = sprintrc(rc);
189         printf("sendto(%d, [", fd);
190         for (i = 0; i < DEFAULT_STRLEN; ++i) {
191                 if (i)
192                         printf(", ");
193                 printf("{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
194                        ", seq=%u, pid=0}",
195                        msgs[i].nlmsg_len, NLM_F_DUMP, msgs[i].nlmsg_seq);
196         }
197         printf(", ...], %u, MSG_DONTWAIT, NULL, 0) = %s\n", msg_len, errstr);
198 }
199
200 static void
201 test_nlmsgerr(const int fd)
202 {
203         struct nlmsgerr *err;
204         struct nlmsghdr *nlh;
205         void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
206         long rc;
207
208         /* error message without enough room for the error code */
209         nlh = nlh0;
210         nlh->nlmsg_len = NLMSG_HDRLEN + 4;
211         nlh->nlmsg_type = NLMSG_ERROR;
212         nlh->nlmsg_flags = NLM_F_REQUEST;
213         nlh->nlmsg_seq = 0;
214         nlh->nlmsg_pid = 0;
215
216         rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
217         printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
218                ", seq=0, pid=0}, %p}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
219                fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
220                nlh->nlmsg_len, sprintrc(rc));
221
222         nlh->nlmsg_len = NLMSG_HDRLEN + 2;
223         nlh = nlh0 - 2;
224         memmove(nlh, nlh0, sizeof(*nlh));
225         memcpy(NLMSG_DATA(nlh), "42", 2);
226
227         rc = sendto(fd, nlh, NLMSG_HDRLEN + 2, MSG_DONTWAIT, NULL, 0);
228         printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
229                ", seq=0, pid=0}, \"\\x34\\x32\"}"
230                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
231                fd, NLMSG_HDRLEN + 2, NLMSG_HDRLEN + 2, sprintrc(rc));
232
233         /* error message with room for the error code only */
234         nlh = nlh0 - sizeof(err->error);
235         nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(err->error);
236         nlh->nlmsg_type = NLMSG_ERROR;
237         nlh->nlmsg_flags = NLM_F_REQUEST;
238         nlh->nlmsg_seq = 0;
239         nlh->nlmsg_pid = 0;
240         err = NLMSG_DATA(nlh);
241         err->error = 42;
242
243         rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
244         printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
245                ", seq=0, pid=0}, {error=42}}, %u, MSG_DONTWAIT, NULL, 0)"
246                " = %s\n", fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc));
247
248         err->error = -1;
249
250         rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
251         printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
252                ", seq=0, pid=0}, {error=-EPERM}}, %u, MSG_DONTWAIT, NULL, 0)"
253                " = %s\n", fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc));
254
255         err->error = -32767;
256         nlh->nlmsg_len += sizeof(err->msg.nlmsg_len);
257
258         rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
259         printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
260                ", seq=0, pid=0}, {error=-32767, msg=%p}}"
261                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
262                fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
263                nlh->nlmsg_len, sprintrc(rc));
264
265         /* error message with room for the error code and a header */
266         nlh = nlh0 - sizeof(*err);
267         nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(*err);
268         nlh->nlmsg_type = NLMSG_ERROR;
269         nlh->nlmsg_flags = NLM_F_REQUEST;
270         nlh->nlmsg_seq = 0;
271         nlh->nlmsg_pid = 0;
272         err = NLMSG_DATA(nlh);
273         err->error = -13;
274         err->msg.nlmsg_len = NLMSG_HDRLEN;
275         err->msg.nlmsg_type = NLMSG_NOOP;
276         err->msg.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
277         err->msg.nlmsg_seq = 42;
278         err->msg.nlmsg_pid = 1234;
279
280         rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
281         printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
282                ", seq=0, pid=0}, {error=-EACCES"
283                ", msg={len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
284                ", seq=%u, pid=%u}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
285                fd, nlh->nlmsg_len, err->msg.nlmsg_len, NLM_F_DUMP,
286                err->msg.nlmsg_seq, err->msg.nlmsg_pid,
287                nlh->nlmsg_len, sprintrc(rc));
288
289         /* error message with room for the error code, a header, and some data */
290         nlh = nlh0 - sizeof(*err) - 4;
291         nlh->nlmsg_len = NLMSG_HDRLEN + sizeof(*err) + 4;
292         nlh->nlmsg_type = NLMSG_ERROR;
293         nlh->nlmsg_flags = NLM_F_REQUEST;
294         nlh->nlmsg_seq = 0;
295         nlh->nlmsg_pid = 0;
296         err = NLMSG_DATA(nlh);
297         err->error = -13;
298         err->msg.nlmsg_len = NLMSG_HDRLEN + 4;
299         err->msg.nlmsg_type = NLMSG_NOOP;
300         err->msg.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
301         err->msg.nlmsg_seq = 421;
302         err->msg.nlmsg_pid = 12345;
303         memcpy(NLMSG_DATA(&err->msg), "abcd", 4);
304
305         rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
306         printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
307                ", seq=0, pid=0}, {error=-EACCES"
308                ", msg={{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
309                ", seq=%u, pid=%u}, \"\\x61\\x62\\x63\\x64\"}}}"
310                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
311                fd, nlh->nlmsg_len, err->msg.nlmsg_len, NLM_F_DUMP,
312                err->msg.nlmsg_seq, err->msg.nlmsg_pid,
313                nlh->nlmsg_len, sprintrc(rc));
314 }
315
316 static void
317 test_nlmsg_done(const int fd)
318 {
319         struct nlmsghdr *nlh;
320         void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
321         long rc;
322         const int num = 0xfacefeed;
323
324         /* NLMSG_DONE message without enough room for an integer payload */
325         nlh = nlh0;
326         *nlh = (struct nlmsghdr) {
327                 .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
328                 .nlmsg_type = NLMSG_DONE,
329                 .nlmsg_flags = NLM_F_MULTI
330         };
331
332         rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
333         printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
334                ", seq=0, pid=0}, %p}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
335                fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
336                nlh->nlmsg_len, sprintrc(rc));
337
338         /* NLMSG_DONE message with enough room for an oddly short payload */
339         nlh->nlmsg_len = NLMSG_HDRLEN + 2;
340         nlh = nlh0 - 2;
341         /* Beware of unaligned access to nlh members. */
342         memmove(nlh, nlh0, sizeof(*nlh));
343         memcpy(NLMSG_DATA(nlh), "42", 2);
344
345         rc = sendto(fd, nlh, NLMSG_HDRLEN + 2, MSG_DONTWAIT, NULL, 0);
346         printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI, seq=0"
347                ", pid=0}, \"\\x34\\x32\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
348                fd, NLMSG_HDRLEN + 2, NLMSG_HDRLEN + 2, sprintrc(rc));
349
350         /* NLMSG_DONE message with enough room for an integer payload */
351         nlh = nlh0 - sizeof(num);
352         *nlh = (struct nlmsghdr) {
353                 .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
354                 .nlmsg_type = NLMSG_DONE,
355                 .nlmsg_flags = NLM_F_MULTI
356         };
357         memcpy(NLMSG_DATA(nlh), &num, sizeof(num));
358
359         rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
360         printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
361                ", seq=0, pid=0}, %d}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
362                fd, nlh->nlmsg_len, num, nlh->nlmsg_len, sprintrc(rc));
363 }
364
365 int main(void)
366 {
367         const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
368
369         char *path;
370         if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
371                 perror_msg_and_fail("asprintf");
372         char buf[256];
373         if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
374                 perror_msg_and_skip("getxattr");
375         free(path);
376
377         send_query(fd);
378         test_nlmsgerr(fd);
379         test_nlmsg_done(fd);
380
381         puts("+++ exited with 0 +++");
382         return 0;
383 }
384
385 #else
386
387 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")
388
389 #endif