Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
6e837fb1 EB |
2 | /* |
3 | * common LSM auditing functions | |
4 | * | |
5 | * Based on code written for SELinux by : | |
9da4f4f9 | 6 | * Stephen Smalley |
6e837fb1 EB |
7 | * James Morris <jmorris@redhat.com> |
8 | * Author : Etienne Basset, <etienne.basset@ensta.org> | |
6e837fb1 EB |
9 | */ |
10 | ||
11 | #include <linux/types.h> | |
12 | #include <linux/stddef.h> | |
13 | #include <linux/kernel.h> | |
5a0e3ad6 | 14 | #include <linux/gfp.h> |
6e837fb1 EB |
15 | #include <linux/fs.h> |
16 | #include <linux/init.h> | |
17 | #include <net/sock.h> | |
18 | #include <linux/un.h> | |
19 | #include <net/af_unix.h> | |
20 | #include <linux/audit.h> | |
21 | #include <linux/ipv6.h> | |
22 | #include <linux/ip.h> | |
23 | #include <net/ip.h> | |
24 | #include <net/ipv6.h> | |
25 | #include <linux/tcp.h> | |
26 | #include <linux/udp.h> | |
6e837fb1 EB |
27 | #include <linux/sctp.h> |
28 | #include <linux/lsm_audit.h> | |
59438b46 | 29 | #include <linux/security.h> |
6e837fb1 EB |
30 | |
31 | /** | |
32 | * ipv4_skb_to_auditdata : fill auditdata from skb | |
33 | * @skb : the skb | |
34 | * @ad : the audit data to fill | |
35 | * @proto : the layer 4 protocol | |
36 | * | |
37 | * return 0 on success | |
38 | */ | |
39 | int ipv4_skb_to_auditdata(struct sk_buff *skb, | |
40 | struct common_audit_data *ad, u8 *proto) | |
41 | { | |
42 | int ret = 0; | |
43 | struct iphdr *ih; | |
44 | ||
45 | ih = ip_hdr(skb); | |
48c62af6 EP |
46 | ad->u.net->v4info.saddr = ih->saddr; |
47 | ad->u.net->v4info.daddr = ih->daddr; | |
6e837fb1 EB |
48 | |
49 | if (proto) | |
50 | *proto = ih->protocol; | |
51 | /* non initial fragment */ | |
52 | if (ntohs(ih->frag_off) & IP_OFFSET) | |
53 | return 0; | |
54 | ||
55 | switch (ih->protocol) { | |
56 | case IPPROTO_TCP: { | |
57 | struct tcphdr *th = tcp_hdr(skb); | |
6e837fb1 | 58 | |
48c62af6 EP |
59 | ad->u.net->sport = th->source; |
60 | ad->u.net->dport = th->dest; | |
6e837fb1 EB |
61 | break; |
62 | } | |
63 | case IPPROTO_UDP: { | |
64 | struct udphdr *uh = udp_hdr(skb); | |
6e837fb1 | 65 | |
48c62af6 EP |
66 | ad->u.net->sport = uh->source; |
67 | ad->u.net->dport = uh->dest; | |
6e837fb1 EB |
68 | break; |
69 | } | |
6e837fb1 EB |
70 | case IPPROTO_SCTP: { |
71 | struct sctphdr *sh = sctp_hdr(skb); | |
4847c0eb | 72 | |
48c62af6 EP |
73 | ad->u.net->sport = sh->source; |
74 | ad->u.net->dport = sh->dest; | |
6e837fb1 EB |
75 | break; |
76 | } | |
77 | default: | |
78 | ret = -EINVAL; | |
79 | } | |
80 | return ret; | |
81 | } | |
1a93a6ea | 82 | #if IS_ENABLED(CONFIG_IPV6) |
6e837fb1 EB |
83 | /** |
84 | * ipv6_skb_to_auditdata : fill auditdata from skb | |
85 | * @skb : the skb | |
86 | * @ad : the audit data to fill | |
87 | * @proto : the layer 4 protocol | |
88 | * | |
89 | * return 0 on success | |
90 | */ | |
91 | int ipv6_skb_to_auditdata(struct sk_buff *skb, | |
92 | struct common_audit_data *ad, u8 *proto) | |
93 | { | |
94 | int offset, ret = 0; | |
95 | struct ipv6hdr *ip6; | |
96 | u8 nexthdr; | |
75f2811c | 97 | __be16 frag_off; |
6e837fb1 EB |
98 | |
99 | ip6 = ipv6_hdr(skb); | |
48c62af6 EP |
100 | ad->u.net->v6info.saddr = ip6->saddr; |
101 | ad->u.net->v6info.daddr = ip6->daddr; | |
6e837fb1 EB |
102 | /* IPv6 can have several extension header before the Transport header |
103 | * skip them */ | |
104 | offset = skb_network_offset(skb); | |
105 | offset += sizeof(*ip6); | |
106 | nexthdr = ip6->nexthdr; | |
75f2811c | 107 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
6e837fb1 EB |
108 | if (offset < 0) |
109 | return 0; | |
110 | if (proto) | |
111 | *proto = nexthdr; | |
112 | switch (nexthdr) { | |
113 | case IPPROTO_TCP: { | |
114 | struct tcphdr _tcph, *th; | |
115 | ||
116 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); | |
117 | if (th == NULL) | |
118 | break; | |
119 | ||
48c62af6 EP |
120 | ad->u.net->sport = th->source; |
121 | ad->u.net->dport = th->dest; | |
6e837fb1 EB |
122 | break; |
123 | } | |
124 | case IPPROTO_UDP: { | |
125 | struct udphdr _udph, *uh; | |
126 | ||
127 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); | |
128 | if (uh == NULL) | |
129 | break; | |
130 | ||
48c62af6 EP |
131 | ad->u.net->sport = uh->source; |
132 | ad->u.net->dport = uh->dest; | |
6e837fb1 EB |
133 | break; |
134 | } | |
6e837fb1 EB |
135 | case IPPROTO_SCTP: { |
136 | struct sctphdr _sctph, *sh; | |
137 | ||
138 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); | |
139 | if (sh == NULL) | |
140 | break; | |
48c62af6 EP |
141 | ad->u.net->sport = sh->source; |
142 | ad->u.net->dport = sh->dest; | |
6e837fb1 EB |
143 | break; |
144 | } | |
145 | default: | |
146 | ret = -EINVAL; | |
147 | } | |
148 | return ret; | |
149 | } | |
150 | #endif | |
151 | ||
152 | ||
153 | static inline void print_ipv6_addr(struct audit_buffer *ab, | |
41dd9596 | 154 | const struct in6_addr *addr, __be16 port, |
b0966c7c | 155 | const char *name1, const char *name2) |
6e837fb1 EB |
156 | { |
157 | if (!ipv6_addr_any(addr)) | |
d8116591 | 158 | audit_log_format(ab, " %s=%pI6c", name1, addr); |
6e837fb1 EB |
159 | if (port) |
160 | audit_log_format(ab, " %s=%d", name2, ntohs(port)); | |
161 | } | |
162 | ||
163 | static inline void print_ipv4_addr(struct audit_buffer *ab, __be32 addr, | |
b0966c7c | 164 | __be16 port, const char *name1, const char *name2) |
6e837fb1 EB |
165 | { |
166 | if (addr) | |
167 | audit_log_format(ab, " %s=%pI4", name1, &addr); | |
168 | if (port) | |
169 | audit_log_format(ab, " %s=%d", name2, ntohs(port)); | |
170 | } | |
171 | ||
172 | /** | |
9b08a166 | 173 | * audit_log_lsm_data - helper to log common LSM audit data |
610b17b0 | 174 | * @ab : the audit buffer |
6e837fb1 | 175 | * @a : common audit data |
6e837fb1 | 176 | */ |
9b08a166 MS |
177 | void audit_log_lsm_data(struct audit_buffer *ab, |
178 | const struct common_audit_data *a) | |
6e837fb1 | 179 | { |
07f62eb6 | 180 | /* |
4432b507 | 181 | * To keep stack sizes in check force programmers to notice if they |
07f62eb6 EP |
182 | * start making this union too large! See struct lsm_network_audit |
183 | * as an example of how to deal with large data. | |
184 | */ | |
185 | BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2); | |
186 | ||
6e837fb1 | 187 | switch (a->type) { |
cb84aa9b | 188 | case LSM_AUDIT_DATA_NONE: |
2bf49690 | 189 | return; |
6e837fb1 | 190 | case LSM_AUDIT_DATA_IPC: |
8e71168e | 191 | audit_log_format(ab, " ipc_key=%d ", a->u.ipc_id); |
6e837fb1 EB |
192 | break; |
193 | case LSM_AUDIT_DATA_CAP: | |
194 | audit_log_format(ab, " capability=%d ", a->u.cap); | |
195 | break; | |
f48b7399 | 196 | case LSM_AUDIT_DATA_PATH: { |
f48b7399 EP |
197 | struct inode *inode; |
198 | ||
c158a35c | 199 | audit_log_d_path(ab, " path=", &a->u.path); |
a269434d | 200 | |
c6f493d6 | 201 | inode = d_backing_inode(a->u.path.dentry); |
41fdc305 KC |
202 | if (inode) { |
203 | audit_log_format(ab, " dev="); | |
204 | audit_log_untrustedstring(ab, inode->i_sb->s_id); | |
205 | audit_log_format(ab, " ino=%lu", inode->i_ino); | |
206 | } | |
a269434d EP |
207 | break; |
208 | } | |
43af5de7 VG |
209 | case LSM_AUDIT_DATA_FILE: { |
210 | struct inode *inode; | |
211 | ||
212 | audit_log_d_path(ab, " path=", &a->u.file->f_path); | |
213 | ||
214 | inode = file_inode(a->u.file); | |
215 | if (inode) { | |
216 | audit_log_format(ab, " dev="); | |
217 | audit_log_untrustedstring(ab, inode->i_sb->s_id); | |
218 | audit_log_format(ab, " ino=%lu", inode->i_ino); | |
219 | } | |
220 | break; | |
221 | } | |
671a2781 JVS |
222 | case LSM_AUDIT_DATA_IOCTL_OP: { |
223 | struct inode *inode; | |
224 | ||
225 | audit_log_d_path(ab, " path=", &a->u.op->path); | |
226 | ||
227 | inode = a->u.op->path.dentry->d_inode; | |
228 | if (inode) { | |
229 | audit_log_format(ab, " dev="); | |
230 | audit_log_untrustedstring(ab, inode->i_sb->s_id); | |
231 | audit_log_format(ab, " ino=%lu", inode->i_ino); | |
232 | } | |
233 | ||
8b31f456 | 234 | audit_log_format(ab, " ioctlcmd=0x%hx", a->u.op->cmd); |
671a2781 JVS |
235 | break; |
236 | } | |
a269434d EP |
237 | case LSM_AUDIT_DATA_DENTRY: { |
238 | struct inode *inode; | |
239 | ||
240 | audit_log_format(ab, " name="); | |
d36a1dd9 | 241 | spin_lock(&a->u.dentry->d_lock); |
a269434d | 242 | audit_log_untrustedstring(ab, a->u.dentry->d_name.name); |
d36a1dd9 | 243 | spin_unlock(&a->u.dentry->d_lock); |
a269434d | 244 | |
c6f493d6 | 245 | inode = d_backing_inode(a->u.dentry); |
41fdc305 KC |
246 | if (inode) { |
247 | audit_log_format(ab, " dev="); | |
248 | audit_log_untrustedstring(ab, inode->i_sb->s_id); | |
249 | audit_log_format(ab, " ino=%lu", inode->i_ino); | |
250 | } | |
6e837fb1 | 251 | break; |
f48b7399 EP |
252 | } |
253 | case LSM_AUDIT_DATA_INODE: { | |
254 | struct dentry *dentry; | |
255 | struct inode *inode; | |
256 | ||
23d8f5b6 | 257 | rcu_read_lock(); |
f48b7399 | 258 | inode = a->u.inode; |
23d8f5b6 | 259 | dentry = d_find_alias_rcu(inode); |
f48b7399 EP |
260 | if (dentry) { |
261 | audit_log_format(ab, " name="); | |
d36a1dd9 AV |
262 | spin_lock(&dentry->d_lock); |
263 | audit_log_untrustedstring(ab, dentry->d_name.name); | |
264 | spin_unlock(&dentry->d_lock); | |
f48b7399 | 265 | } |
41fdc305 KC |
266 | audit_log_format(ab, " dev="); |
267 | audit_log_untrustedstring(ab, inode->i_sb->s_id); | |
268 | audit_log_format(ab, " ino=%lu", inode->i_ino); | |
23d8f5b6 | 269 | rcu_read_unlock(); |
f48b7399 EP |
270 | break; |
271 | } | |
5deeb5ce RGB |
272 | case LSM_AUDIT_DATA_TASK: { |
273 | struct task_struct *tsk = a->u.tsk; | |
f1dc4867 | 274 | if (tsk) { |
fa2bea2f | 275 | pid_t pid = task_tgid_nr(tsk); |
f1dc4867 | 276 | if (pid) { |
b00083ae | 277 | char tskcomm[sizeof(tsk->comm)]; |
5c5bc97e | 278 | audit_log_format(ab, " opid=%d ocomm=", pid); |
5deeb5ce | 279 | audit_log_untrustedstring(ab, |
b00083ae | 280 | get_task_comm(tskcomm, tsk)); |
f1dc4867 | 281 | } |
6e837fb1 EB |
282 | } |
283 | break; | |
5deeb5ce | 284 | } |
6e837fb1 | 285 | case LSM_AUDIT_DATA_NET: |
48c62af6 | 286 | if (a->u.net->sk) { |
41dd9596 | 287 | const struct sock *sk = a->u.net->sk; |
b064ba9c | 288 | const struct unix_sock *u; |
ae3b5641 | 289 | struct unix_address *addr; |
6e837fb1 EB |
290 | int len = 0; |
291 | char *p = NULL; | |
292 | ||
293 | switch (sk->sk_family) { | |
294 | case AF_INET: { | |
abc17a11 | 295 | const struct inet_sock *inet = inet_sk(sk); |
6e837fb1 | 296 | |
c720c7e8 ED |
297 | print_ipv4_addr(ab, inet->inet_rcv_saddr, |
298 | inet->inet_sport, | |
6e837fb1 | 299 | "laddr", "lport"); |
c720c7e8 ED |
300 | print_ipv4_addr(ab, inet->inet_daddr, |
301 | inet->inet_dport, | |
6e837fb1 EB |
302 | "faddr", "fport"); |
303 | break; | |
304 | } | |
c2bb06db | 305 | #if IS_ENABLED(CONFIG_IPV6) |
6e837fb1 | 306 | case AF_INET6: { |
abc17a11 | 307 | const struct inet_sock *inet = inet_sk(sk); |
6e837fb1 | 308 | |
efe4208f | 309 | print_ipv6_addr(ab, &sk->sk_v6_rcv_saddr, |
c720c7e8 | 310 | inet->inet_sport, |
6e837fb1 | 311 | "laddr", "lport"); |
efe4208f | 312 | print_ipv6_addr(ab, &sk->sk_v6_daddr, |
c720c7e8 | 313 | inet->inet_dport, |
6e837fb1 EB |
314 | "faddr", "fport"); |
315 | break; | |
316 | } | |
c2bb06db | 317 | #endif |
6e837fb1 EB |
318 | case AF_UNIX: |
319 | u = unix_sk(sk); | |
ae3b5641 AV |
320 | addr = smp_load_acquire(&u->addr); |
321 | if (!addr) | |
322 | break; | |
40ffe67d AV |
323 | if (u->path.dentry) { |
324 | audit_log_d_path(ab, " path=", &u->path); | |
6e837fb1 EB |
325 | break; |
326 | } | |
ae3b5641 AV |
327 | len = addr->len-sizeof(short); |
328 | p = &addr->name->sun_path[0]; | |
6e837fb1 EB |
329 | audit_log_format(ab, " path="); |
330 | if (*p) | |
331 | audit_log_untrustedstring(ab, p); | |
332 | else | |
333 | audit_log_n_hex(ab, p, len); | |
334 | break; | |
335 | } | |
336 | } | |
337 | ||
48c62af6 | 338 | switch (a->u.net->family) { |
6e837fb1 | 339 | case AF_INET: |
48c62af6 EP |
340 | print_ipv4_addr(ab, a->u.net->v4info.saddr, |
341 | a->u.net->sport, | |
6e837fb1 | 342 | "saddr", "src"); |
48c62af6 EP |
343 | print_ipv4_addr(ab, a->u.net->v4info.daddr, |
344 | a->u.net->dport, | |
6e837fb1 EB |
345 | "daddr", "dest"); |
346 | break; | |
347 | case AF_INET6: | |
48c62af6 EP |
348 | print_ipv6_addr(ab, &a->u.net->v6info.saddr, |
349 | a->u.net->sport, | |
6e837fb1 | 350 | "saddr", "src"); |
48c62af6 EP |
351 | print_ipv6_addr(ab, &a->u.net->v6info.daddr, |
352 | a->u.net->dport, | |
6e837fb1 EB |
353 | "daddr", "dest"); |
354 | break; | |
355 | } | |
48c62af6 | 356 | if (a->u.net->netif > 0) { |
6e837fb1 EB |
357 | struct net_device *dev; |
358 | ||
359 | /* NOTE: we always use init's namespace */ | |
48c62af6 | 360 | dev = dev_get_by_index(&init_net, a->u.net->netif); |
6e837fb1 EB |
361 | if (dev) { |
362 | audit_log_format(ab, " netif=%s", dev->name); | |
363 | dev_put(dev); | |
364 | } | |
365 | } | |
366 | break; | |
367 | #ifdef CONFIG_KEYS | |
368 | case LSM_AUDIT_DATA_KEY: | |
369 | audit_log_format(ab, " key_serial=%u", a->u.key_struct.key); | |
370 | if (a->u.key_struct.key_desc) { | |
371 | audit_log_format(ab, " key_desc="); | |
372 | audit_log_untrustedstring(ab, a->u.key_struct.key_desc); | |
373 | } | |
374 | break; | |
375 | #endif | |
dd8dbf2e EP |
376 | case LSM_AUDIT_DATA_KMOD: |
377 | audit_log_format(ab, " kmod="); | |
378 | audit_log_untrustedstring(ab, a->u.kmod_name); | |
379 | break; | |
cfc4d882 DJ |
380 | case LSM_AUDIT_DATA_IBPKEY: { |
381 | struct in6_addr sbn_pfx; | |
382 | ||
383 | memset(&sbn_pfx.s6_addr, 0, | |
384 | sizeof(sbn_pfx.s6_addr)); | |
385 | memcpy(&sbn_pfx.s6_addr, &a->u.ibpkey->subnet_prefix, | |
386 | sizeof(a->u.ibpkey->subnet_prefix)); | |
387 | audit_log_format(ab, " pkey=0x%x subnet_prefix=%pI6c", | |
388 | a->u.ibpkey->pkey, &sbn_pfx); | |
389 | break; | |
390 | } | |
ab861dfc DJ |
391 | case LSM_AUDIT_DATA_IBENDPORT: |
392 | audit_log_format(ab, " device=%s port_num=%u", | |
393 | a->u.ibendport->dev_name, | |
394 | a->u.ibendport->port); | |
395 | break; | |
59438b46 | 396 | case LSM_AUDIT_DATA_LOCKDOWN: |
f1d9b23c RGB |
397 | audit_log_format(ab, " lockdown_reason=\"%s\"", |
398 | lockdown_reasons[a->u.reason]); | |
59438b46 | 399 | break; |
c29722fa CG |
400 | case LSM_AUDIT_DATA_ANONINODE: |
401 | audit_log_format(ab, " anonclass=%s", a->u.anonclass); | |
402 | break; | |
2ef6fc99 TW |
403 | case LSM_AUDIT_DATA_NLMSGTYPE: |
404 | audit_log_format(ab, " nl-msgtype=%hu", a->u.nlmsg_type); | |
405 | break; | |
6e837fb1 EB |
406 | } /* switch (a->type) */ |
407 | } | |
408 | ||
9b08a166 MS |
409 | /** |
410 | * dump_common_audit_data - helper to dump common audit data | |
411 | * @ab : the audit buffer | |
412 | * @a : common audit data | |
413 | */ | |
414 | static void dump_common_audit_data(struct audit_buffer *ab, | |
415 | const struct common_audit_data *a) | |
416 | { | |
417 | char comm[sizeof(current->comm)]; | |
418 | ||
419 | audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current)); | |
420 | audit_log_untrustedstring(ab, get_task_comm(comm, current)); | |
421 | audit_log_lsm_data(ab, a); | |
422 | } | |
423 | ||
6e837fb1 EB |
424 | /** |
425 | * common_lsm_audit - generic LSM auditing function | |
426 | * @a: auxiliary audit data | |
b61c37f5 LT |
427 | * @pre_audit: lsm-specific pre-audit callback |
428 | * @post_audit: lsm-specific post-audit callback | |
6e837fb1 EB |
429 | * |
430 | * setup the audit buffer for common security information | |
431 | * uses callback to print LSM specific information | |
432 | */ | |
b61c37f5 LT |
433 | void common_lsm_audit(struct common_audit_data *a, |
434 | void (*pre_audit)(struct audit_buffer *, void *), | |
435 | void (*post_audit)(struct audit_buffer *, void *)) | |
6e837fb1 EB |
436 | { |
437 | struct audit_buffer *ab; | |
438 | ||
439 | if (a == NULL) | |
440 | return; | |
441 | /* we use GFP_ATOMIC so we won't sleep */ | |
cdfb6b34 | 442 | ab = audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN, |
a20b62bd | 443 | AUDIT_AVC); |
6e837fb1 EB |
444 | |
445 | if (ab == NULL) | |
446 | return; | |
447 | ||
b61c37f5 LT |
448 | if (pre_audit) |
449 | pre_audit(ab, a); | |
6e837fb1 EB |
450 | |
451 | dump_common_audit_data(ab, a); | |
452 | ||
b61c37f5 LT |
453 | if (post_audit) |
454 | post_audit(ab, a); | |
6e837fb1 EB |
455 | |
456 | audit_log_end(ab); | |
457 | } |