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}, \"\\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));
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));
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));
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));
122 /* a sequence of two nlmsg objects */
125 char padding[NLMSG_ALIGN(sizeof(struct req)) - sizeof(struct req)];
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));
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));
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),
151 /* message length is not enough for the second struct nlmsghdr */
152 rc = sendto(fd, reqs, sizeof(*reqs) - sizeof(req->nlh), MSG_DONTWAIT,
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);
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));
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);
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;
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) {
193 printf("{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
195 msgs[i].nlmsg_len, NLM_F_DUMP, msgs[i].nlmsg_seq);
197 printf(", ...], %u, MSG_DONTWAIT, NULL, 0) = %s\n", msg_len, errstr);
201 test_nlmsgerr(const int fd)
203 struct nlmsgerr *err;
204 struct nlmsghdr *nlh;
205 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
208 /* error message without enough room for the error code */
210 nlh->nlmsg_len = NLMSG_HDRLEN + 4;
211 nlh->nlmsg_type = NLMSG_ERROR;
212 nlh->nlmsg_flags = NLM_F_REQUEST;
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));
222 nlh->nlmsg_len = NLMSG_HDRLEN + 2;
224 memmove(nlh, nlh0, sizeof(*nlh));
225 memcpy(NLMSG_DATA(nlh), "42", 2);
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));
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;
240 err = NLMSG_DATA(nlh);
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));
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));
256 nlh->nlmsg_len += sizeof(err->msg.nlmsg_len);
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));
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;
272 err = NLMSG_DATA(nlh);
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;
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));
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;
296 err = NLMSG_DATA(nlh);
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);
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));
317 test_nlmsg_done(const int fd)
319 struct nlmsghdr *nlh;
320 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
322 const int num = 0xfacefeed;
324 /* NLMSG_DONE message without enough room for an integer payload */
326 *nlh = (struct nlmsghdr) {
327 .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
328 .nlmsg_type = NLMSG_DONE,
329 .nlmsg_flags = NLM_F_MULTI
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));
338 /* NLMSG_DONE message with enough room for an oddly short payload */
339 nlh->nlmsg_len = NLMSG_HDRLEN + 2;
341 /* Beware of unaligned access to nlh members. */
342 memmove(nlh, nlh0, sizeof(*nlh));
343 memcpy(NLMSG_DATA(nlh), "42", 2);
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));
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
357 memcpy(NLMSG_DATA(nlh), &num, sizeof(num));
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));
367 const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
370 if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
371 perror_msg_and_fail("asprintf");
373 if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
374 perror_msg_and_skip("getxattr");
381 puts("+++ exited with 0 +++");
387 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")