Merge branch 'master' into next
[linux-2.6.git] / security / lsm_audit.c
1 /*
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>
17 #include <linux/gfp.h>
18 #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  */
42 int 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  */
111 int 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
186 static 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))
191                 audit_log_format(ab, " %s=%pI6c", name1, addr);
192         if (port)
193                 audit_log_format(ab, " %s=%d", name2, ntohs(port));
194 }
195
196 static 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  */
210 static void dump_common_audit_data(struct audit_buffer *ab,
211                                    struct common_audit_data *a)
212 {
213         struct inode *inode = NULL;
214         struct task_struct *tsk = current;
215
216         if (a->tsk)
217                 tsk = a->tsk;
218         if (tsk && tsk->pid) {
219                 audit_log_format(ab, " pid=%d comm=", tsk->pid);
220                 audit_log_untrustedstring(ab, tsk->comm);
221         }
222
223         switch (a->type) {
224         case LSM_AUDIT_DATA_NONE:
225                 return;
226         case LSM_AUDIT_DATA_IPC:
227                 audit_log_format(ab, " key=%d ", a->u.ipc_id);
228                 break;
229         case LSM_AUDIT_DATA_CAP:
230                 audit_log_format(ab, " capability=%d ", a->u.cap);
231                 break;
232         case LSM_AUDIT_DATA_FS:
233                 if (a->u.fs.path.dentry) {
234                         struct dentry *dentry = a->u.fs.path.dentry;
235                         if (a->u.fs.path.mnt) {
236                                 audit_log_d_path(ab, "path=", &a->u.fs.path);
237                         } else {
238                                 audit_log_format(ab, " name=");
239                                 audit_log_untrustedstring(ab,
240                                                  dentry->d_name.name);
241                         }
242                         inode = dentry->d_inode;
243                 } else if (a->u.fs.inode) {
244                         struct dentry *dentry;
245                         inode = a->u.fs.inode;
246                         dentry = d_find_alias(inode);
247                         if (dentry) {
248                                 audit_log_format(ab, " name=");
249                                 audit_log_untrustedstring(ab,
250                                                  dentry->d_name.name);
251                                 dput(dentry);
252                         }
253                 }
254                 if (inode)
255                         audit_log_format(ab, " dev=%s ino=%lu",
256                                         inode->i_sb->s_id,
257                                         inode->i_ino);
258                 break;
259         case LSM_AUDIT_DATA_TASK:
260                 tsk = a->u.tsk;
261                 if (tsk && tsk->pid) {
262                         audit_log_format(ab, " pid=%d comm=", tsk->pid);
263                         audit_log_untrustedstring(ab, tsk->comm);
264                 }
265                 break;
266         case LSM_AUDIT_DATA_NET:
267                 if (a->u.net.sk) {
268                         struct sock *sk = a->u.net.sk;
269                         struct unix_sock *u;
270                         int len = 0;
271                         char *p = NULL;
272
273                         switch (sk->sk_family) {
274                         case AF_INET: {
275                                 struct inet_sock *inet = inet_sk(sk);
276
277                                 print_ipv4_addr(ab, inet->inet_rcv_saddr,
278                                                 inet->inet_sport,
279                                                 "laddr", "lport");
280                                 print_ipv4_addr(ab, inet->inet_daddr,
281                                                 inet->inet_dport,
282                                                 "faddr", "fport");
283                                 break;
284                         }
285                         case AF_INET6: {
286                                 struct inet_sock *inet = inet_sk(sk);
287                                 struct ipv6_pinfo *inet6 = inet6_sk(sk);
288
289                                 print_ipv6_addr(ab, &inet6->rcv_saddr,
290                                                 inet->inet_sport,
291                                                 "laddr", "lport");
292                                 print_ipv6_addr(ab, &inet6->daddr,
293                                                 inet->inet_dport,
294                                                 "faddr", "fport");
295                                 break;
296                         }
297                         case AF_UNIX:
298                                 u = unix_sk(sk);
299                                 if (u->dentry) {
300                                         struct path path = {
301                                                 .dentry = u->dentry,
302                                                 .mnt = u->mnt
303                                         };
304                                         audit_log_d_path(ab, "path=", &path);
305                                         break;
306                                 }
307                                 if (!u->addr)
308                                         break;
309                                 len = u->addr->len-sizeof(short);
310                                 p = &u->addr->name->sun_path[0];
311                                 audit_log_format(ab, " path=");
312                                 if (*p)
313                                         audit_log_untrustedstring(ab, p);
314                                 else
315                                         audit_log_n_hex(ab, p, len);
316                                 break;
317                         }
318                 }
319
320                 switch (a->u.net.family) {
321                 case AF_INET:
322                         print_ipv4_addr(ab, a->u.net.v4info.saddr,
323                                         a->u.net.sport,
324                                         "saddr", "src");
325                         print_ipv4_addr(ab, a->u.net.v4info.daddr,
326                                         a->u.net.dport,
327                                         "daddr", "dest");
328                         break;
329                 case AF_INET6:
330                         print_ipv6_addr(ab, &a->u.net.v6info.saddr,
331                                         a->u.net.sport,
332                                         "saddr", "src");
333                         print_ipv6_addr(ab, &a->u.net.v6info.daddr,
334                                         a->u.net.dport,
335                                         "daddr", "dest");
336                         break;
337                 }
338                 if (a->u.net.netif > 0) {
339                         struct net_device *dev;
340
341                         /* NOTE: we always use init's namespace */
342                         dev = dev_get_by_index(&init_net, a->u.net.netif);
343                         if (dev) {
344                                 audit_log_format(ab, " netif=%s", dev->name);
345                                 dev_put(dev);
346                         }
347                 }
348                 break;
349 #ifdef CONFIG_KEYS
350         case LSM_AUDIT_DATA_KEY:
351                 audit_log_format(ab, " key_serial=%u", a->u.key_struct.key);
352                 if (a->u.key_struct.key_desc) {
353                         audit_log_format(ab, " key_desc=");
354                         audit_log_untrustedstring(ab, a->u.key_struct.key_desc);
355                 }
356                 break;
357 #endif
358         case LSM_AUDIT_DATA_KMOD:
359                 audit_log_format(ab, " kmod=");
360                 audit_log_untrustedstring(ab, a->u.kmod_name);
361                 break;
362         } /* switch (a->type) */
363 }
364
365 /**
366  * common_lsm_audit - generic LSM auditing function
367  * @a:  auxiliary audit data
368  *
369  * setup the audit buffer for common security information
370  * uses callback to print LSM specific information
371  */
372 void common_lsm_audit(struct common_audit_data *a)
373 {
374         struct audit_buffer *ab;
375
376         if (a == NULL)
377                 return;
378         /* we use GFP_ATOMIC so we won't sleep */
379         ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_AVC);
380
381         if (ab == NULL)
382                 return;
383
384         if (a->lsm_pre_audit)
385                 a->lsm_pre_audit(ab, a);
386
387         dump_common_audit_data(ab, a);
388
389         if (a->lsm_post_audit)
390                 a->lsm_post_audit(ab, a);
391
392         audit_log_end(ab);
393 }