UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.git] / security / apparmor / apparmorfs-24.c
1 /*
2  * AppArmor security module
3  *
4  * This file contains AppArmor /sys/kernel/secrutiy/apparmor interface functions
5  *
6  * Copyright (C) 1998-2008 Novell/SUSE
7  * Copyright 2009-2010 Canonical Ltd.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation, version 2 of the
12  * License.
13  *
14  *
15  * This file contain functions providing an interface for <= AppArmor 2.4
16  * compatibility.  It is dependent on CONFIG_SECURITY_APPARMOR_COMPAT_24
17  * being set (see Makefile).
18  */
19
20 #include <linux/security.h>
21 #include <linux/vmalloc.h>
22 #include <linux/module.h>
23 #include <linux/seq_file.h>
24 #include <linux/uaccess.h>
25 #include <linux/namei.h>
26
27 #include "include/apparmor.h"
28 #include "include/audit.h"
29 #include "include/context.h"
30 #include "include/policy.h"
31
32
33 /* apparmor/matching */
34 static ssize_t aa_matching_read(struct file *file, char __user *buf,
35                                 size_t size, loff_t *ppos)
36 {
37         const char matching[] = "pattern=aadfa audit perms=crwxamlk/ "
38             "user::other";
39
40         return simple_read_from_buffer(buf, size, ppos, matching,
41                                        sizeof(matching) - 1);
42 }
43
44 const struct file_operations aa_fs_matching_fops = {
45         .read = aa_matching_read,
46 };
47
48 /* apparmor/features */
49 static ssize_t aa_features_read(struct file *file, char __user *buf,
50                                 size_t size, loff_t *ppos)
51 {
52         const char features[] = "file=3.1 capability=2.0 network=1.0 "
53             "change_hat=1.5 change_profile=1.1 " "aanamespaces=1.1 rlimit=1.1";
54
55         return simple_read_from_buffer(buf, size, ppos, features,
56                                        sizeof(features) - 1);
57 }
58
59 const struct file_operations aa_fs_features_fops = {
60         .read = aa_features_read,
61 };
62
63 /**
64  * __next_namespace - find the next namespace to list
65  * @root: root namespace to stop search at (NOT NULL)
66  * @ns: current ns position (NOT NULL)
67  *
68  * Find the next namespace from @ns under @root and handle all locking needed
69  * while switching current namespace.
70  *
71  * Returns: next namespace or NULL if at last namespace under @root
72  * NOTE: will not unlock root->lock
73  */
74 static struct aa_namespace *__next_namespace(struct aa_namespace *root,
75                                              struct aa_namespace *ns)
76 {
77         struct aa_namespace *parent;
78
79         /* is next namespace a child */
80         if (!list_empty(&ns->sub_ns)) {
81                 struct aa_namespace *next;
82                 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
83                 read_lock(&next->lock);
84                 return next;
85         }
86
87         /* check if the next ns is a sibling, parent, gp, .. */
88         parent = ns->parent;
89         while (parent) {
90                 read_unlock(&ns->lock);
91                 list_for_each_entry_continue(ns, &parent->sub_ns, base.list) {
92                         read_lock(&ns->lock);
93                         return ns;
94                 }
95                 if (parent == root)
96                         return NULL;
97                 ns = parent;
98                 parent = parent->parent;
99         }
100
101         return NULL;
102 }
103
104 /**
105  * __first_profile - find the first profile in a namespace
106  * @root: namespace that is root of profiles being displayed (NOT NULL)
107  * @ns: namespace to start in   (NOT NULL)
108  *
109  * Returns: unrefcounted profile or NULL if no profile
110  */
111 static struct aa_profile *__first_profile(struct aa_namespace *root,
112                                           struct aa_namespace *ns)
113 {
114         for ( ; ns; ns = __next_namespace(root, ns)) {
115                 if (!list_empty(&ns->base.profiles))
116                         return list_first_entry(&ns->base.profiles,
117                                                 struct aa_profile, base.list);
118         }
119         return NULL;
120 }
121
122 /**
123  * __next_profile - step to the next profile in a profile tree
124  * @profile: current profile in tree (NOT NULL)
125  *
126  * Perform a depth first taversal on the profile tree in a namespace
127  *
128  * Returns: next profile or NULL if done
129  * Requires: profile->ns.lock to be held
130  */
131 static struct aa_profile *__next_profile(struct aa_profile *p)
132 {
133         struct aa_profile *parent;
134         struct aa_namespace *ns = p->ns;
135
136         /* is next profile a child */
137         if (!list_empty(&p->base.profiles))
138                 return list_first_entry(&p->base.profiles, typeof(*p),
139                                         base.list);
140
141         /* is next profile a sibling, parent sibling, gp, subling, .. */
142         parent = p->parent;
143         while (parent) {
144                 list_for_each_entry_continue(p, &parent->base.profiles,
145                                              base.list)
146                                 return p;
147                 p = parent;
148                 parent = parent->parent;
149         }
150
151         /* is next another profile in the namespace */
152         list_for_each_entry_continue(p, &ns->base.profiles, base.list)
153                 return p;
154
155         return NULL;
156 }
157
158 /**
159  * next_profile - step to the next profile in where ever it may be
160  * @root: root namespace  (NOT NULL)
161  * @profile: current profile  (NOT NULL)
162  *
163  * Returns: next profile or NULL if there isn't one
164  */
165 static struct aa_profile *next_profile(struct aa_namespace *root,
166                                        struct aa_profile *profile)
167 {
168         struct aa_profile *next = __next_profile(profile);
169         if (next)
170                 return next;
171
172         /* finished all profiles in namespace move to next namespace */
173         return __first_profile(root, __next_namespace(root, profile->ns));
174 }
175
176 /**
177  * p_start - start a depth first traversal of profile tree
178  * @f: seq_file to fill
179  * @pos: current position
180  *
181  * Returns: first profile under current namespace or NULL if none found
182  *
183  * acquires first ns->lock
184  */
185 static void *p_start(struct seq_file *f, loff_t *pos)
186         __acquires(root->lock)
187 {
188         struct aa_profile *profile = NULL;
189         struct aa_namespace *root = aa_current_profile()->ns;
190         loff_t l = *pos;
191         f->private = aa_get_namespace(root);
192
193
194         /* find the first profile */
195         read_lock(&root->lock);
196         profile = __first_profile(root, root);
197
198         /* skip to position */
199         for (; profile && l > 0; l--)
200                 profile = next_profile(root, profile);
201
202         return profile;
203 }
204
205 /**
206  * p_next - read the next profile entry
207  * @f: seq_file to fill
208  * @p: profile previously returned
209  * @pos: current position
210  *
211  * Returns: next profile after @p or NULL if none
212  *
213  * may acquire/release locks in namespace tree as necessary
214  */
215 static void *p_next(struct seq_file *f, void *p, loff_t *pos)
216 {
217         struct aa_profile *profile = p;
218         struct aa_namespace *root = f->private;
219         (*pos)++;
220
221         return next_profile(root, profile);
222 }
223
224 /**
225  * p_stop - stop depth first traversal
226  * @f: seq_file we are filling
227  * @p: the last profile writen
228  *
229  * Release all locking done by p_start/p_next on namespace tree
230  */
231 static void p_stop(struct seq_file *f, void *p)
232         __releases(root->lock)
233 {
234         struct aa_profile *profile = p;
235         struct aa_namespace *root = f->private, *ns;
236
237         if (profile) {
238                 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
239                         read_unlock(&ns->lock);
240         }
241         read_unlock(&root->lock);
242         aa_put_namespace(root);
243 }
244
245 /**
246  * seq_show_profile - show a profile entry
247  * @f: seq_file to file
248  * @p: current position (profile)    (NOT NULL)
249  *
250  * Returns: error on failure
251  */
252 static int seq_show_profile(struct seq_file *f, void *p)
253 {
254         struct aa_profile *profile = (struct aa_profile *)p;
255         struct aa_namespace *root = f->private;
256
257         if (profile->ns != root)
258                 seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
259         seq_printf(f, "%s (%s)\n", profile->base.hname,
260                    COMPLAIN_MODE(profile) ? "complain" : "enforce");
261
262         return 0;
263 }
264
265 static const struct seq_operations aa_fs_profiles_op = {
266         .start = p_start,
267         .next = p_next,
268         .stop = p_stop,
269         .show = seq_show_profile,
270 };
271
272 static int profiles_open(struct inode *inode, struct file *file)
273 {
274         return seq_open(file, &aa_fs_profiles_op);
275 }
276
277 static int profiles_release(struct inode *inode, struct file *file)
278 {
279         return seq_release(inode, file);
280 }
281
282 const struct file_operations aa_fs_profiles_fops = {
283         .open = profiles_open,
284         .read = seq_read,
285         .llseek = seq_lseek,
286         .release = profiles_release,
287 };