- Update to 2.6.25-rc3.
[linux-flexiantxendom0-3.2.10.git] / security / apparmor / module_interface.c
1 /*
2  *      Copyright (C) 1998-2007 Novell/SUSE
3  *
4  *      This program is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU General Public License as
6  *      published by the Free Software Foundation, version 2 of the
7  *      License.
8  *
9  *      AppArmor userspace policy interface
10  */
11
12 #include <asm/unaligned.h>
13
14 #include "apparmor.h"
15 #include "inline.h"
16
17 /*
18  * This mutex is used to synchronize profile adds, replacements, and
19  * removals: we only allow one of these operations at a time.
20  * We do not use the profile list lock here in order to avoid blocking
21  * exec during those operations.  (Exec involves a profile list lookup
22  * for named-profile transitions.)
23  */
24 DEFINE_MUTEX(aa_interface_lock);
25
26 /*
27  * The AppArmor interface treats data as a type byte followed by the
28  * actual data.  The interface has the notion of a a named entry
29  * which has a name (AA_NAME typecode followed by name string) followed by
30  * the entries typecode and data.  Named types allow for optional
31  * elements and extensions to be added and tested for without breaking
32  * backwards compatability.
33  */
34
35 enum aa_code {
36         AA_U8,
37         AA_U16,
38         AA_U32,
39         AA_U64,
40         AA_NAME,        /* same as string except it is items name */
41         AA_STRING,
42         AA_BLOB,
43         AA_STRUCT,
44         AA_STRUCTEND,
45         AA_LIST,
46         AA_LISTEND,
47         AA_ARRAY,
48         AA_ARRAYEND,
49 };
50
51 /*
52  * aa_ext is the read of the buffer containing the serialized profile.  The
53  * data is copied into a kernel buffer in apparmorfs and then handed off to
54  * the unpack routines.
55  */
56 struct aa_ext {
57         void *start;
58         void *end;
59         void *pos;      /* pointer to current position in the buffer */
60         u32 version;
61 };
62
63 static inline int aa_inbounds(struct aa_ext *e, size_t size)
64 {
65         return (size <= e->end - e->pos);
66 }
67
68 /**
69  * aa_u16_chunck - test and do bounds checking for a u16 size based chunk
70  * @e: serialized data read head
71  * @chunk: start address for chunk of data
72  *
73  * return the size of chunk found with the read head at the end of
74  * the chunk.
75  */
76 static size_t aa_is_u16_chunk(struct aa_ext *e, char **chunk)
77 {
78         void *pos = e->pos;
79         size_t size = 0;
80
81         if (!aa_inbounds(e, sizeof(u16)))
82                 goto fail;
83         size = le16_to_cpu(get_unaligned((u16 *)e->pos));
84         e->pos += sizeof(u16);
85         if (!aa_inbounds(e, size))
86                 goto fail;
87         *chunk = e->pos;
88         e->pos += size;
89         return size;
90
91 fail:
92         e->pos = pos;
93         return 0;
94 }
95
96 static inline int aa_is_X(struct aa_ext *e, enum aa_code code)
97 {
98         if (!aa_inbounds(e, 1))
99                 return 0;
100         if (*(u8 *) e->pos != code)
101                 return 0;
102         e->pos++;
103         return 1;
104 }
105
106 /**
107  * aa_is_nameX - check is the next element is of type X with a name of @name
108  * @e: serialized data extent information
109  * @code: type code
110  * @name: name to match to the serialized element.
111  *
112  * check that the next serialized data element is of type X and has a tag
113  * name @name.  If @name is specified then there must be a matching
114  * name element in the stream.  If @name is NULL any name element will be
115  * skipped and only the typecode will be tested.
116  * returns 1 on success (both type code and name tests match) and the read
117  * head is advanced past the headers
118  * returns %0 if either match failes, the read head does not move
119  */
120 static int aa_is_nameX(struct aa_ext *e, enum aa_code code, const char *name)
121 {
122         void *pos = e->pos;
123         /*
124          * Check for presence of a tagname, and if present name size
125          * AA_NAME tag value is a u16.
126          */
127         if (aa_is_X(e, AA_NAME)) {
128                 char *tag;
129                 size_t size = aa_is_u16_chunk(e, &tag);
130                 /* if a name is specified it must match. otherwise skip tag */
131                 if (name && (!size || strcmp(name, tag)))
132                         goto fail;
133         } else if (name) {
134                 /* if a name is specified and there is no name tag fail */
135                 goto fail;
136         }
137
138         /* now check if type code matches */
139         if (aa_is_X(e, code))
140                 return 1;
141
142 fail:
143         e->pos = pos;
144         return 0;
145 }
146
147 static int aa_is_u16(struct aa_ext *e, u16 *data, const char *name)
148 {
149         void *pos = e->pos;
150         if (aa_is_nameX(e, AA_U16, name)) {
151                 if (!aa_inbounds(e, sizeof(u16)))
152                         goto fail;
153                 if (data)
154                         *data = le16_to_cpu(get_unaligned((u16 *)e->pos));
155                 e->pos += sizeof(u16);
156                 return 1;
157         }
158 fail:
159         e->pos = pos;
160         return 0;
161 }
162
163 static int aa_is_u32(struct aa_ext *e, u32 *data, const char *name)
164 {
165         void *pos = e->pos;
166         if (aa_is_nameX(e, AA_U32, name)) {
167                 if (!aa_inbounds(e, sizeof(u32)))
168                         goto fail;
169                 if (data)
170                         *data = le32_to_cpu(get_unaligned((u32 *)e->pos));
171                 e->pos += sizeof(u32);
172                 return 1;
173         }
174 fail:
175         e->pos = pos;
176         return 0;
177 }
178
179 static size_t aa_is_array(struct aa_ext *e, const char *name)
180 {
181         void *pos = e->pos;
182         if (aa_is_nameX(e, AA_ARRAY, name)) {
183                 int size;
184                 if (!aa_inbounds(e, sizeof(u16)))
185                         goto fail;
186                 size = (int) le16_to_cpu(get_unaligned((u16 *)e->pos));
187                 e->pos += sizeof(u16);
188                 return size;
189         }
190 fail:
191         e->pos = pos;
192         return 0;
193 }
194
195 static size_t aa_is_blob(struct aa_ext *e, char **blob, const char *name)
196 {
197         void *pos = e->pos;
198         if (aa_is_nameX(e, AA_BLOB, name)) {
199                 u32 size;
200                 if (!aa_inbounds(e, sizeof(u32)))
201                         goto fail;
202                 size = le32_to_cpu(get_unaligned((u32 *)e->pos));
203                 e->pos += sizeof(u32);
204                 if (aa_inbounds(e, (size_t) size)) {
205                         * blob = e->pos;
206                         e->pos += size;
207                         return size;
208                 }
209         }
210 fail:
211         e->pos = pos;
212         return 0;
213 }
214
215 static int aa_is_dynstring(struct aa_ext *e, char **string, const char *name)
216 {
217         char *src_str;
218         size_t size = 0;
219         void *pos = e->pos;
220         *string = NULL;
221         if (aa_is_nameX(e, AA_STRING, name) &&
222             (size = aa_is_u16_chunk(e, &src_str))) {
223                 char *str;
224                 if (!(str = kmalloc(size, GFP_KERNEL)))
225                         goto fail;
226                 memcpy(str, src_str, size);
227                 *string = str;
228         }
229
230         return size;
231
232 fail:
233         e->pos = pos;
234         return 0;
235 }
236
237 /**
238  * aa_unpack_dfa - unpack a file rule dfa
239  * @e: serialized data extent information
240  *
241  * returns dfa or ERR_PTR
242  */
243 struct aa_dfa *aa_unpack_dfa(struct aa_ext *e)
244 {
245         char *blob = NULL;
246         size_t size, error = 0;
247         struct aa_dfa *dfa = NULL;
248
249         size = aa_is_blob(e, &blob, "aadfa");
250         if (size) {
251                 dfa = aa_match_alloc();
252                 if (dfa) {
253                         /*
254                          * The dfa is aligned with in the blob to 8 bytes
255                          * from the beginning of the stream.
256                          */
257                         size_t sz = blob - (char *) e->start;
258                         size_t pad = ALIGN(sz, 8) - sz;
259                         error = unpack_dfa(dfa, blob + pad, size - pad);
260                         if (!error)
261                                 error = verify_dfa(dfa);
262                 } else {
263                         error = -ENOMEM;
264                 }
265
266                 if (error) {
267                         aa_match_free(dfa);
268                         dfa = ERR_PTR(error);
269                 }
270         }
271
272         return dfa;
273 }
274
275 /**
276  * aa_unpack_profile - unpack a serialized profile
277  * @e: serialized data extent information
278  * @operation: operation profile is being unpacked for
279  */
280 static struct aa_profile *aa_unpack_profile(struct aa_ext *e,
281                                             const char *operation)
282 {
283         struct aa_profile *profile = NULL;
284         struct aa_audit sa;
285         size_t size = 0;
286         int i;
287
288         int error = -EPROTO;
289
290         profile = alloc_aa_profile();
291         if (!profile)
292                 return ERR_PTR(-ENOMEM);
293
294         /* check that we have the right struct being passed */
295         if (!aa_is_nameX(e, AA_STRUCT, "profile"))
296                 goto fail;
297         if (!aa_is_dynstring(e, &profile->name, NULL))
298                 goto fail;
299
300         /* per profile debug flags (complain, audit) */
301         if (!aa_is_nameX(e, AA_STRUCT, "flags"))
302                 goto fail;
303         if (!aa_is_u32(e, NULL, NULL))
304                 goto fail;
305         if (!aa_is_u32(e, &(profile->flags.complain), NULL))
306                 goto fail;
307         if (!aa_is_u32(e, &(profile->flags.audit), NULL))
308                 goto fail;
309         if (!aa_is_nameX(e, AA_STRUCTEND, NULL))
310                 goto fail;
311
312         /* XXX: This supports only the low order capabilities. -jeffm */
313         if (!aa_is_u32(e, &(profile->capabilities.cap[0]), NULL))
314                 goto fail;
315
316         size = aa_is_array(e, "net_allowed_af");
317         if (size) {
318                 if (size > AF_MAX)
319                         goto fail;
320
321                 for (i = 0; i < size; i++) {
322                         if (!aa_is_u16(e, &profile->network_families[i], NULL))
323                                 goto fail;
324                 }
325                 if (!aa_is_nameX(e, AA_ARRAYEND, NULL))
326                         goto fail;
327                 /* allow unix domain and netlink sockets they are handled
328                  * by IPC
329                  */
330         }
331         profile->network_families[AF_UNIX] = 0xffff;
332         profile->network_families[AF_NETLINK] = 0xffff;
333
334         /* get file rules */
335         profile->file_rules = aa_unpack_dfa(e);
336         if (IS_ERR(profile->file_rules)) {
337                 error = PTR_ERR(profile->file_rules);
338                 profile->file_rules = NULL;
339                 goto fail;
340         }
341
342         if (!aa_is_nameX(e, AA_STRUCTEND, NULL))
343                 goto fail;
344
345         return profile;
346
347 fail:
348         memset(&sa, 0, sizeof(sa));
349         sa.operation = operation;
350         sa.gfp_mask = GFP_KERNEL;
351         sa.name = profile && profile->name ? profile->name : "unknown";
352         sa.info = "failed to unpack profile";
353         aa_audit_status(NULL, &sa);
354
355         if (profile)
356                 free_aa_profile(profile);
357
358         return ERR_PTR(error);
359 }
360
361 /**
362  * aa_verify_head - unpack serialized stream header
363  * @e: serialized data read head
364  * @operation: operation header is being verified for
365  *
366  * returns error or 0 if header is good
367  */
368 static int aa_verify_header(struct aa_ext *e, const char *operation)
369 {
370         /* get the interface version */
371         if (!aa_is_u32(e, &e->version, "version")) {
372                 struct aa_audit sa;
373                 memset(&sa, 0, sizeof(sa));
374                 sa.operation = operation;
375                 sa.gfp_mask = GFP_KERNEL;
376                 sa.info = "invalid profile format";
377                 aa_audit_status(NULL, &sa);
378                 return -EPROTONOSUPPORT;
379         }
380
381         /* check that the interface version is currently supported */
382         if (e->version != 3) {
383                 struct aa_audit sa;
384                 memset(&sa, 0, sizeof(sa));
385                 sa.operation = operation;
386                 sa.gfp_mask = GFP_KERNEL;
387                 sa.info = "unsupported interface version";
388                 aa_audit_status(NULL, &sa);
389                 return -EPROTONOSUPPORT;
390         }
391         return 0;
392 }
393
394 /**
395  * aa_add_profile - Unpack and add a new profile to the profile list
396  * @data: serialized data stream
397  * @size: size of the serialized data stream
398  */
399 ssize_t aa_add_profile(void *data, size_t size)
400 {
401         struct aa_profile *profile = NULL;
402         struct aa_ext e = {
403                 .start = data,
404                 .end = data + size,
405                 .pos = data
406         };
407         ssize_t error = aa_verify_header(&e, "profile_load");
408         if (error)
409                 return error;
410
411         profile = aa_unpack_profile(&e, "profile_load");
412         if (IS_ERR(profile))
413                 return PTR_ERR(profile);
414
415         mutex_lock(&aa_interface_lock);
416         write_lock(&profile_list_lock);
417         if (__aa_find_profile(profile->name, &profile_list)) {
418                 /* A profile with this name exists already. */
419                 write_unlock(&profile_list_lock);
420                 mutex_unlock(&aa_interface_lock);
421                 aa_put_profile(profile);
422                 return -EEXIST;
423         }
424         list_add(&profile->list, &profile_list);
425         write_unlock(&profile_list_lock);
426         mutex_unlock(&aa_interface_lock);
427
428         return size;
429 }
430
431 /**
432  * task_replace - replace a task's profile
433  * @task: task to replace profile on
434  * @new_cxt: new aa_task_context to do replacement with
435  * @new_profile: new profile
436  */
437 static inline void task_replace(struct task_struct *task,
438                                 struct aa_task_context *new_cxt,
439                                 struct aa_profile *new_profile)
440 {
441         struct aa_task_context *cxt = aa_task_context(task);
442
443         AA_DEBUG("%s: replacing profile for task %d "
444                  "profile=%s (%p)\n",
445                  __FUNCTION__,
446                  cxt->task->pid,
447                  cxt->profile->name, cxt->profile);
448
449         aa_change_task_context(task, new_cxt, new_profile, cxt->cookie,
450                                cxt->previous_profile);
451 }
452
453 /**
454  * aa_replace_profile - replace a profile on the profile list
455  * @udata: serialized data stream
456  * @size: size of the serialized data stream
457  *
458  * unpack and replace a profile on the profile list and uses of that profile
459  * by any aa_task_context.  If the profile does not exist on the profile list
460  * it is added.  Return %0 or error.
461  */
462 ssize_t aa_replace_profile(void *udata, size_t size)
463 {
464         struct aa_profile *old_profile, *new_profile;
465         struct aa_task_context *new_cxt;
466         struct aa_ext e = {
467                 .start = udata,
468                 .end = udata + size,
469                 .pos = udata
470         };
471         ssize_t error = aa_verify_header(&e, "profile_replace");
472         if (error)
473                 return error;
474
475         new_profile = aa_unpack_profile(&e, "profile_replace");
476         if (IS_ERR(new_profile))
477                 return PTR_ERR(new_profile);
478
479         mutex_lock(&aa_interface_lock);
480         write_lock(&profile_list_lock);
481         old_profile = __aa_find_profile(new_profile->name, &profile_list);
482         if (old_profile) {
483                 lock_profile(old_profile);
484                 old_profile->isstale = 1;
485                 unlock_profile(old_profile);
486                 list_del_init(&old_profile->list);
487         }
488         list_add(&new_profile->list, &profile_list);
489         write_unlock(&profile_list_lock);
490
491         if (!old_profile)
492                 goto out;
493
494         /*
495          * Replacement needs to allocate a new aa_task_context for each
496          * task confined by old_profile.  To do this the profile locks
497          * are only held when the actual switch is done per task.  While
498          * looping to allocate a new aa_task_context the old_task list
499          * may get shorter if tasks exit/change their profile but will
500          * not get longer as new task will not use old_profile detecting
501          * that is stale.
502          */
503         do {
504                 new_cxt = aa_alloc_task_context(GFP_KERNEL | __GFP_NOFAIL);
505
506                 lock_both_profiles(old_profile, new_profile);
507                 if (!list_empty(&old_profile->task_contexts)) {
508                         struct task_struct *task =
509                                 list_entry(old_profile->task_contexts.next,
510                                            struct aa_task_context, list)->task;
511                         task_lock(task);
512                         task_replace(task, new_cxt, new_profile);
513                         task_unlock(task);
514                         new_cxt = NULL;
515                 }
516                 unlock_both_profiles(old_profile, new_profile);
517         } while (!new_cxt);
518         aa_free_task_context(new_cxt);
519         aa_put_profile(old_profile);
520
521 out:
522         mutex_unlock(&aa_interface_lock);
523         return size;
524 }
525
526 /**
527  * aa_remove_profile - remove a profile from the system
528  * @name: name of the profile to remove
529  * @size: size of the name
530  *
531  * remove a profile from the profile list and all aa_task_context references
532  * to said profile.
533  */
534 ssize_t aa_remove_profile(const char *name, size_t size)
535 {
536         struct aa_profile *profile;
537
538         mutex_lock(&aa_interface_lock);
539         write_lock(&profile_list_lock);
540         profile = __aa_find_profile(name, &profile_list);
541         if (!profile) {
542                 write_unlock(&profile_list_lock);
543                 mutex_unlock(&aa_interface_lock);
544                 return -ENOENT;
545         }
546
547         /* Remove the profile from each task context it is on. */
548         lock_profile(profile);
549         profile->isstale = 1;
550         aa_unconfine_tasks(profile);
551         unlock_profile(profile);
552
553         /* Release the profile itself. */
554         list_del_init(&profile->list);
555         aa_put_profile(profile);
556         write_unlock(&profile_list_lock);
557         mutex_unlock(&aa_interface_lock);
558
559         return size;
560 }
561
562 /**
563  * free_aa_profile_kref - free aa_profile by kref (called by aa_put_profile)
564  * @kr: kref callback for freeing of a profile
565  */
566 void free_aa_profile_kref(struct kref *kref)
567 {
568         struct aa_profile *p=container_of(kref, struct aa_profile, count);
569
570         free_aa_profile(p);
571 }
572
573 /**
574  * alloc_aa_profile - allocate, initialize and return a new profile
575  * Returns NULL on failure.
576  */
577 struct aa_profile *alloc_aa_profile(void)
578 {
579         struct aa_profile *profile;
580
581         profile = kzalloc(sizeof(*profile), GFP_KERNEL);
582         AA_DEBUG("%s(%p)\n", __FUNCTION__, profile);
583         if (profile) {
584                 INIT_LIST_HEAD(&profile->list);
585                 kref_init(&profile->count);
586                 INIT_LIST_HEAD(&profile->task_contexts);
587                 spin_lock_init(&profile->lock);
588         }
589         return profile;
590 }
591
592 /**
593  * free_aa_profile - free a profile
594  * @profile: the profile to free
595  *
596  * Free a profile, its hats and null_profile. All references to the profile,
597  * its hats and null_profile must have been put.
598  *
599  * If the profile was referenced from a task context, free_aa_profile() will
600  * be called from an rcu callback routine, so we must not sleep here.
601  */
602 void free_aa_profile(struct aa_profile *profile)
603 {
604         AA_DEBUG("%s(%p)\n", __FUNCTION__, profile);
605
606         if (!profile)
607                 return;
608
609         /* profile is still on global profile list -- invalid */
610         if (!list_empty(&profile->list)) {
611                 AA_ERROR("%s: internal error, "
612                          "profile '%s' still on global list\n",
613                          __FUNCTION__,
614                          profile->name);
615                 BUG();
616         }
617
618         aa_match_free(profile->file_rules);
619
620         if (profile->name) {
621                 AA_DEBUG("%s: %s\n", __FUNCTION__, profile->name);
622                 kfree(profile->name);
623         }
624
625         kfree(profile);
626 }
627
628 /**
629  * aa_unconfine_tasks - remove tasks on a profile's task context list
630  * @profile: profile to remove tasks from
631  *
632  * Assumes that @profile lock is held.
633  */
634 void aa_unconfine_tasks(struct aa_profile *profile)
635 {
636         while (!list_empty(&profile->task_contexts)) {
637                 struct task_struct *task =
638                         list_entry(profile->task_contexts.next,
639                                    struct aa_task_context, list)->task;
640                 task_lock(task);
641                 aa_change_task_context(task, NULL, NULL, 0, NULL);
642                 task_unlock(task);
643         }
644 }