blob: 2e846052cbf439671c3b7e6ace9d13efe2e66cf1 [file] [log] [blame]
Etienne Basset6e837fb2009-04-08 20:39:40 +02001/*
2 * common LSM auditing functions
3 *
4 * Based on code written for SELinux by :
5 * Stephen Smalley, <sds@epoch.ncsc.mil>
6 * James Morris <jmorris@redhat.com>
7 * Author : Etienne Basset, <etienne.basset@ensta.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 */
13
14#include <linux/types.h>
15#include <linux/stddef.h>
16#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Etienne Basset6e837fb2009-04-08 20:39:40 +020018#include <linux/fs.h>
19#include <linux/init.h>
20#include <net/sock.h>
21#include <linux/un.h>
22#include <net/af_unix.h>
23#include <linux/audit.h>
24#include <linux/ipv6.h>
25#include <linux/ip.h>
26#include <net/ip.h>
27#include <net/ipv6.h>
28#include <linux/tcp.h>
29#include <linux/udp.h>
30#include <linux/dccp.h>
31#include <linux/sctp.h>
32#include <linux/lsm_audit.h>
33
34/**
35 * ipv4_skb_to_auditdata : fill auditdata from skb
36 * @skb : the skb
37 * @ad : the audit data to fill
38 * @proto : the layer 4 protocol
39 *
40 * return 0 on success
41 */
42int ipv4_skb_to_auditdata(struct sk_buff *skb,
43 struct common_audit_data *ad, u8 *proto)
44{
45 int ret = 0;
46 struct iphdr *ih;
47
48 ih = ip_hdr(skb);
49 if (ih == NULL)
50 return -EINVAL;
51
52 ad->u.net.v4info.saddr = ih->saddr;
53 ad->u.net.v4info.daddr = ih->daddr;
54
55 if (proto)
56 *proto = ih->protocol;
57 /* non initial fragment */
58 if (ntohs(ih->frag_off) & IP_OFFSET)
59 return 0;
60
61 switch (ih->protocol) {
62 case IPPROTO_TCP: {
63 struct tcphdr *th = tcp_hdr(skb);
64 if (th == NULL)
65 break;
66
67 ad->u.net.sport = th->source;
68 ad->u.net.dport = th->dest;
69 break;
70 }
71 case IPPROTO_UDP: {
72 struct udphdr *uh = udp_hdr(skb);
73 if (uh == NULL)
74 break;
75
76 ad->u.net.sport = uh->source;
77 ad->u.net.dport = uh->dest;
78 break;
79 }
80 case IPPROTO_DCCP: {
81 struct dccp_hdr *dh = dccp_hdr(skb);
82 if (dh == NULL)
83 break;
84
85 ad->u.net.sport = dh->dccph_sport;
86 ad->u.net.dport = dh->dccph_dport;
87 break;
88 }
89 case IPPROTO_SCTP: {
90 struct sctphdr *sh = sctp_hdr(skb);
91 if (sh == NULL)
92 break;
93 ad->u.net.sport = sh->source;
94 ad->u.net.dport = sh->dest;
95 break;
96 }
97 default:
98 ret = -EINVAL;
99 }
100 return ret;
101}
102#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
103/**
104 * ipv6_skb_to_auditdata : fill auditdata from skb
105 * @skb : the skb
106 * @ad : the audit data to fill
107 * @proto : the layer 4 protocol
108 *
109 * return 0 on success
110 */
111int ipv6_skb_to_auditdata(struct sk_buff *skb,
112 struct common_audit_data *ad, u8 *proto)
113{
114 int offset, ret = 0;
115 struct ipv6hdr *ip6;
116 u8 nexthdr;
117
118 ip6 = ipv6_hdr(skb);
119 if (ip6 == NULL)
120 return -EINVAL;
121 ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr);
122 ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr);
123 ret = 0;
124 /* IPv6 can have several extension header before the Transport header
125 * skip them */
126 offset = skb_network_offset(skb);
127 offset += sizeof(*ip6);
128 nexthdr = ip6->nexthdr;
129 offset = ipv6_skip_exthdr(skb, offset, &nexthdr);
130 if (offset < 0)
131 return 0;
132 if (proto)
133 *proto = nexthdr;
134 switch (nexthdr) {
135 case IPPROTO_TCP: {
136 struct tcphdr _tcph, *th;
137
138 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
139 if (th == NULL)
140 break;
141
142 ad->u.net.sport = th->source;
143 ad->u.net.dport = th->dest;
144 break;
145 }
146 case IPPROTO_UDP: {
147 struct udphdr _udph, *uh;
148
149 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
150 if (uh == NULL)
151 break;
152
153 ad->u.net.sport = uh->source;
154 ad->u.net.dport = uh->dest;
155 break;
156 }
157 case IPPROTO_DCCP: {
158 struct dccp_hdr _dccph, *dh;
159
160 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
161 if (dh == NULL)
162 break;
163
164 ad->u.net.sport = dh->dccph_sport;
165 ad->u.net.dport = dh->dccph_dport;
166 break;
167 }
168 case IPPROTO_SCTP: {
169 struct sctphdr _sctph, *sh;
170
171 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
172 if (sh == NULL)
173 break;
174 ad->u.net.sport = sh->source;
175 ad->u.net.dport = sh->dest;
176 break;
177 }
178 default:
179 ret = -EINVAL;
180 }
181 return ret;
182}
183#endif
184
185
186static inline void print_ipv6_addr(struct audit_buffer *ab,
187 struct in6_addr *addr, __be16 port,
188 char *name1, char *name2)
189{
190 if (!ipv6_addr_any(addr))
Paul Moored8116592009-09-23 13:46:00 -0400191 audit_log_format(ab, " %s=%pI6c", name1, addr);
Etienne Basset6e837fb2009-04-08 20:39:40 +0200192 if (port)
193 audit_log_format(ab, " %s=%d", name2, ntohs(port));
194}
195
196static inline void print_ipv4_addr(struct audit_buffer *ab, __be32 addr,
197 __be16 port, char *name1, char *name2)
198{
199 if (addr)
200 audit_log_format(ab, " %s=%pI4", name1, &addr);
201 if (port)
202 audit_log_format(ab, " %s=%d", name2, ntohs(port));
203}
204
205/**
206 * dump_common_audit_data - helper to dump common audit data
207 * @a : common audit data
208 *
209 */
210static void dump_common_audit_data(struct audit_buffer *ab,
211 struct common_audit_data *a)
212{
Etienne Basset6e837fb2009-04-08 20:39:40 +0200213 struct task_struct *tsk = current;
214
215 if (a->tsk)
216 tsk = a->tsk;
217 if (tsk && tsk->pid) {
218 audit_log_format(ab, " pid=%d comm=", tsk->pid);
219 audit_log_untrustedstring(ab, tsk->comm);
220 }
221
222 switch (a->type) {
Eric Pariscb84aa92010-04-27 17:20:38 -0400223 case LSM_AUDIT_DATA_NONE:
Thomas Liu2bf49692009-07-14 12:14:09 -0400224 return;
Etienne Basset6e837fb2009-04-08 20:39:40 +0200225 case LSM_AUDIT_DATA_IPC:
226 audit_log_format(ab, " key=%d ", a->u.ipc_id);
227 break;
228 case LSM_AUDIT_DATA_CAP:
229 audit_log_format(ab, " capability=%d ", a->u.cap);
230 break;
Eric Parisf48b7392011-04-25 12:54:27 -0400231 case LSM_AUDIT_DATA_PATH: {
232 struct dentry *dentry = a->u.path.dentry;
233 struct inode *inode;
234
235 if (a->u.path.mnt) {
236 audit_log_d_path(ab, "path=", &a->u.path);
237 } else {
238 audit_log_format(ab, " name=");
239 audit_log_untrustedstring(ab,
240 dentry->d_name.name);
Etienne Basset6e837fb2009-04-08 20:39:40 +0200241 }
Eric Parisf48b7392011-04-25 12:54:27 -0400242 inode = dentry->d_inode;
Etienne Basset6e837fb2009-04-08 20:39:40 +0200243 if (inode)
244 audit_log_format(ab, " dev=%s ino=%lu",
245 inode->i_sb->s_id,
246 inode->i_ino);
247 break;
Eric Parisf48b7392011-04-25 12:54:27 -0400248 }
249 case LSM_AUDIT_DATA_INODE: {
250 struct dentry *dentry;
251 struct inode *inode;
252
253 inode = a->u.inode;
254 dentry = d_find_alias(inode);
255 if (dentry) {
256 audit_log_format(ab, " name=");
257 audit_log_untrustedstring(ab,
258 dentry->d_name.name);
259 dput(dentry);
260 }
261 audit_log_format(ab, " dev=%s ino=%lu", inode->i_sb->s_id,
262 inode->i_ino);
263 break;
264 }
Etienne Basset6e837fb2009-04-08 20:39:40 +0200265 case LSM_AUDIT_DATA_TASK:
266 tsk = a->u.tsk;
267 if (tsk && tsk->pid) {
268 audit_log_format(ab, " pid=%d comm=", tsk->pid);
269 audit_log_untrustedstring(ab, tsk->comm);
270 }
271 break;
272 case LSM_AUDIT_DATA_NET:
273 if (a->u.net.sk) {
274 struct sock *sk = a->u.net.sk;
275 struct unix_sock *u;
276 int len = 0;
277 char *p = NULL;
278
279 switch (sk->sk_family) {
280 case AF_INET: {
281 struct inet_sock *inet = inet_sk(sk);
282
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000283 print_ipv4_addr(ab, inet->inet_rcv_saddr,
284 inet->inet_sport,
Etienne Basset6e837fb2009-04-08 20:39:40 +0200285 "laddr", "lport");
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000286 print_ipv4_addr(ab, inet->inet_daddr,
287 inet->inet_dport,
Etienne Basset6e837fb2009-04-08 20:39:40 +0200288 "faddr", "fport");
289 break;
290 }
291 case AF_INET6: {
292 struct inet_sock *inet = inet_sk(sk);
293 struct ipv6_pinfo *inet6 = inet6_sk(sk);
294
295 print_ipv6_addr(ab, &inet6->rcv_saddr,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000296 inet->inet_sport,
Etienne Basset6e837fb2009-04-08 20:39:40 +0200297 "laddr", "lport");
298 print_ipv6_addr(ab, &inet6->daddr,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000299 inet->inet_dport,
Etienne Basset6e837fb2009-04-08 20:39:40 +0200300 "faddr", "fport");
301 break;
302 }
303 case AF_UNIX:
304 u = unix_sk(sk);
305 if (u->dentry) {
306 struct path path = {
307 .dentry = u->dentry,
308 .mnt = u->mnt
309 };
310 audit_log_d_path(ab, "path=", &path);
311 break;
312 }
313 if (!u->addr)
314 break;
315 len = u->addr->len-sizeof(short);
316 p = &u->addr->name->sun_path[0];
317 audit_log_format(ab, " path=");
318 if (*p)
319 audit_log_untrustedstring(ab, p);
320 else
321 audit_log_n_hex(ab, p, len);
322 break;
323 }
324 }
325
326 switch (a->u.net.family) {
327 case AF_INET:
328 print_ipv4_addr(ab, a->u.net.v4info.saddr,
329 a->u.net.sport,
330 "saddr", "src");
331 print_ipv4_addr(ab, a->u.net.v4info.daddr,
332 a->u.net.dport,
333 "daddr", "dest");
334 break;
335 case AF_INET6:
336 print_ipv6_addr(ab, &a->u.net.v6info.saddr,
337 a->u.net.sport,
338 "saddr", "src");
339 print_ipv6_addr(ab, &a->u.net.v6info.daddr,
340 a->u.net.dport,
341 "daddr", "dest");
342 break;
343 }
344 if (a->u.net.netif > 0) {
345 struct net_device *dev;
346
347 /* NOTE: we always use init's namespace */
348 dev = dev_get_by_index(&init_net, a->u.net.netif);
349 if (dev) {
350 audit_log_format(ab, " netif=%s", dev->name);
351 dev_put(dev);
352 }
353 }
354 break;
355#ifdef CONFIG_KEYS
356 case LSM_AUDIT_DATA_KEY:
357 audit_log_format(ab, " key_serial=%u", a->u.key_struct.key);
358 if (a->u.key_struct.key_desc) {
359 audit_log_format(ab, " key_desc=");
360 audit_log_untrustedstring(ab, a->u.key_struct.key_desc);
361 }
362 break;
363#endif
Eric Parisdd8dbf22009-11-03 16:35:32 +1100364 case LSM_AUDIT_DATA_KMOD:
365 audit_log_format(ab, " kmod=");
366 audit_log_untrustedstring(ab, a->u.kmod_name);
367 break;
Etienne Basset6e837fb2009-04-08 20:39:40 +0200368 } /* switch (a->type) */
369}
370
371/**
372 * common_lsm_audit - generic LSM auditing function
373 * @a: auxiliary audit data
374 *
375 * setup the audit buffer for common security information
376 * uses callback to print LSM specific information
377 */
378void common_lsm_audit(struct common_audit_data *a)
379{
380 struct audit_buffer *ab;
381
382 if (a == NULL)
383 return;
384 /* we use GFP_ATOMIC so we won't sleep */
385 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_AVC);
386
387 if (ab == NULL)
388 return;
389
390 if (a->lsm_pre_audit)
391 a->lsm_pre_audit(ab, a);
392
393 dump_common_audit_data(ab, a);
394
395 if (a->lsm_post_audit)
396 a->lsm_post_audit(ab, a);
397
398 audit_log_end(ab);
399}