]> granicus.if.org Git - strace/blob - netlink.c
tests: check decoding of NETLINK_SOCK_DIAG message types
[strace] / netlink.c
1 /*
2  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2016-2017 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "defs.h"
31 #include <sys/socket.h>
32 #include <linux/audit.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/xfrm.h>
36 #include "xlat/netlink_flags.h"
37 #include "xlat/netlink_protocols.h"
38 #include "xlat/netlink_types.h"
39 #include "xlat/nl_audit_types.h"
40 #include "xlat/nl_netfilter_msg_types.h"
41 #include "xlat/nl_netfilter_subsys_ids.h"
42 #include "xlat/nl_route_types.h"
43 #include "xlat/nl_selinux_types.h"
44 #include "xlat/nl_sock_diag_types.h"
45 #include "xlat/nl_xfrm_types.h"
46
47 #undef NLMSG_HDRLEN
48 #define NLMSG_HDRLEN NLMSG_ALIGN(sizeof(struct nlmsghdr))
49
50 /*
51  * Fetch a struct nlmsghdr from the given address.
52  */
53 static bool
54 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
55                const kernel_ulong_t addr, const kernel_ulong_t len)
56 {
57         if (len < sizeof(struct nlmsghdr)) {
58                 printstrn(tcp, addr, len);
59                 return false;
60         }
61
62         if (umove_or_printaddr(tcp, addr, nlmsghdr))
63                 return false;
64
65         return true;
66 }
67
68 enum {
69         NL_FAMILY_ERROR = -1,
70         NL_FAMILY_DEFAULT = -2
71 };
72
73 static int
74 get_fd_nl_family(struct tcb *const tcp, const int fd)
75 {
76         const unsigned long inode = getfdinode(tcp, fd);
77         if (!inode)
78                 return NL_FAMILY_ERROR;
79
80         const char *const details = get_sockaddr_by_inode(tcp, fd, inode);
81         if (!details)
82                 return NL_FAMILY_ERROR;
83
84         const char *const nl_details = STR_STRIP_PREFIX(details, "NETLINK:[");
85         if (nl_details == details)
86                 return NL_FAMILY_ERROR;
87
88         const struct xlat *xlats = netlink_protocols;
89         for (; xlats->str; ++xlats) {
90                 const char *name = STR_STRIP_PREFIX(xlats->str, "NETLINK_");
91                 if (!strncmp(nl_details, name, strlen(name)))
92                         return xlats->val;
93         }
94
95         if (*nl_details >= '0' && *nl_details <= '9')
96                 return atoi(nl_details);
97
98         return NL_FAMILY_ERROR;
99 }
100
101 static const struct {
102         const struct xlat *const xlat;
103         const char *const dflt;
104 } nlmsg_types[] = {
105         [NETLINK_AUDIT] = { nl_audit_types, "AUDIT_???" },
106         [NETLINK_NETFILTER] = { nl_netfilter_subsys_ids, "NFNL_SUBSYS_???" },
107         [NETLINK_ROUTE] = { nl_route_types, "RTM_???" },
108         [NETLINK_SELINUX] = { nl_selinux_types, "SELNL_MSG_???" },
109         [NETLINK_SOCK_DIAG] = { nl_sock_diag_types, "SOCK_DIAG_???" },
110         [NETLINK_XFRM] = { nl_xfrm_types, "XFRM_MSG_???" }
111 };
112
113 /*
114  * As all valid netlink families are positive integers, use unsigned int
115  * for family here to filter out NL_FAMILY_ERROR and NL_FAMILY_DEFAULT.
116  */
117 static void
118 decode_nlmsg_type(const uint16_t type, const unsigned int family)
119 {
120         if (family < ARRAY_SIZE(nlmsg_types)
121             && nlmsg_types[family].xlat) {
122                 if (family == NETLINK_NETFILTER) {
123                         /* Reserved control nfnetlink messages first. */
124                         const char *text = xlookup(nl_netfilter_msg_types,
125                                                    type);
126                         if (text) {
127                                 tprints(text);
128                                 return;
129                         }
130
131                         /*
132                          * Other netfilter message types are split
133                          * in two pieces: 8 bits subsystem and 8 bits type.
134                          */
135                         const uint8_t subsys_id = (uint8_t) (type >> 8);
136                         const uint8_t msg_type = (uint8_t) type;
137
138                         printxval(nlmsg_types[family].xlat, subsys_id,
139                                   nlmsg_types[family].dflt);
140
141                         /*
142                          * The type is subsystem specific,
143                          * print it in numeric format for now.
144                          */
145                         tprintf("<<8|%#x", msg_type);
146                 } else {
147                         printxval(nlmsg_types[family].xlat, type,
148                                   nlmsg_types[family].dflt);
149                 }
150         } else {
151                 printxval(netlink_types, type, "NLMSG_???");
152         }
153 }
154
155 static int
156 print_nlmsghdr(struct tcb *tcp,
157                const int fd,
158                int family,
159                const struct nlmsghdr *const nlmsghdr)
160 {
161         /* print the whole structure regardless of its nlmsg_len */
162
163         tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
164
165         const int hdr_family = (nlmsghdr->nlmsg_type < NLMSG_MIN_TYPE)
166                                ? NL_FAMILY_DEFAULT
167                                : (family != NL_FAMILY_DEFAULT
168                                   ? family : get_fd_nl_family(tcp, fd));
169
170         decode_nlmsg_type(nlmsghdr->nlmsg_type, hdr_family);
171
172         tprints(", flags=");
173         printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
174
175         tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
176                 nlmsghdr->nlmsg_pid);
177
178         return family != NL_FAMILY_DEFAULT ? family : hdr_family;
179 }
180
181 static void
182 decode_nlmsghdr_with_payload(struct tcb *const tcp,
183                              const int fd,
184                              int family,
185                              const struct nlmsghdr *const nlmsghdr,
186                              const kernel_ulong_t addr,
187                              const kernel_ulong_t len);
188
189 static void
190 decode_nlmsgerr(struct tcb *const tcp,
191                 const int fd,
192                 const int family,
193                 kernel_ulong_t addr,
194                 kernel_ulong_t len)
195 {
196         struct nlmsgerr err;
197
198         if (len < sizeof(err.error)) {
199                 printstrn(tcp, addr, len);
200                 return;
201         }
202
203         if (umove_or_printaddr(tcp, addr, &err.error))
204                 return;
205
206         tprints("{error=");
207         if (err.error < 0 && (unsigned) -err.error < nerrnos) {
208                 tprintf("-%s", errnoent[-err.error]);
209         } else {
210                 tprintf("%d", err.error);
211         }
212
213         addr += offsetof(struct nlmsgerr, msg);
214         len -= offsetof(struct nlmsgerr, msg);
215
216         if (len) {
217                 tprints(", msg=");
218                 if (fetch_nlmsghdr(tcp, &err.msg, addr, len)) {
219                         decode_nlmsghdr_with_payload(tcp, fd, family,
220                                                      &err.msg, addr, len);
221                 }
222         }
223
224         tprints("}");
225 }
226
227 static void
228 decode_payload(struct tcb *const tcp,
229                const int fd,
230                const int family,
231                const struct nlmsghdr *const nlmsghdr,
232                const kernel_ulong_t addr,
233                const kernel_ulong_t len)
234 {
235         if (nlmsghdr->nlmsg_type == NLMSG_ERROR) {
236                 decode_nlmsgerr(tcp, fd, family, addr, len);
237                 return;
238         } else if (nlmsghdr->nlmsg_type == NLMSG_DONE && len == sizeof(int)) {
239                 int num;
240
241                 if (!umove_or_printaddr(tcp, addr, &num))
242                         tprintf("%d", num);
243                 return;
244         }
245
246         printstrn(tcp, addr, len);
247 }
248
249 static void
250 decode_nlmsghdr_with_payload(struct tcb *const tcp,
251                              const int fd,
252                              int family,
253                              const struct nlmsghdr *const nlmsghdr,
254                              const kernel_ulong_t addr,
255                              const kernel_ulong_t len)
256 {
257         tprints("{");
258
259         family = print_nlmsghdr(tcp, fd, family, nlmsghdr);
260
261         unsigned int nlmsg_len =
262                 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
263         if (nlmsg_len > NLMSG_HDRLEN) {
264                 tprints(", ");
265                 decode_payload(tcp, fd, family, nlmsghdr, addr + NLMSG_HDRLEN,
266                                                      nlmsg_len - NLMSG_HDRLEN);
267         }
268
269         tprints("}");
270 }
271
272 void
273 decode_netlink(struct tcb *const tcp,
274                const int fd,
275                kernel_ulong_t addr,
276                kernel_ulong_t len)
277 {
278         struct nlmsghdr nlmsghdr;
279         bool print_array = false;
280         unsigned int elt;
281
282         for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
283                 if (abbrev(tcp) && elt == max_strlen) {
284                         tprints("...");
285                         break;
286                 }
287
288                 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
289                 kernel_ulong_t next_addr = 0;
290                 kernel_ulong_t next_len = 0;
291
292                 if (nlmsghdr.nlmsg_len >= NLMSG_HDRLEN) {
293                         next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
294
295                         if (next_len && addr + nlmsg_len > addr)
296                                 next_addr = addr + nlmsg_len;
297                 }
298
299                 if (!print_array && next_addr) {
300                         tprints("[");
301                         print_array = true;
302                 }
303
304                 decode_nlmsghdr_with_payload(tcp, fd, NL_FAMILY_DEFAULT,
305                                              &nlmsghdr, addr, len);
306
307                 if (!next_addr)
308                         break;
309
310                 tprints(", ");
311                 addr = next_addr;
312                 len = next_len;
313         }
314
315         if (print_array) {
316                 tprints("]");
317         }
318 }