1 From 1b5e29dcf1c18938e62e23f24e9a19c01b861561 Mon Sep 17 00:00:00 2001
2 From: John Johansen <john.johansen@canonical.com>
3 Date: Thu, 22 Jul 2010 02:32:02 -0700
4 Subject: [PATCH 1/4] UBUNTU: SAUCE: AppArmor: Add profile introspection file
7 Add the dynamic profiles file to the interace, to allow load policy
10 Signed-off-by: John Johansen <john.johansen@canonical.com>
11 Acked-by: Kees Cook <kees@ubuntu.com>
13 security/apparmor/Kconfig | 9 ++
14 security/apparmor/apparmorfs.c | 231 +++++++++++++++++++++++++++++++++++++++++
15 2 files changed, 240 insertions(+)
17 diff --git a/security/apparmor/Kconfig b/security/apparmor/Kconfig
18 index 9b9013b..51ebf96 100644
19 --- a/security/apparmor/Kconfig
20 +++ b/security/apparmor/Kconfig
21 @@ -29,3 +29,12 @@ config SECURITY_APPARMOR_BOOTPARAM_VALUE
24 If you are unsure how to answer this question, answer 1.
26 +config SECURITY_APPARMOR_COMPAT_24
27 + bool "Enable AppArmor 2.4 compatability"
28 + depends on SECURITY_APPARMOR
31 + This option enables compatability with AppArmor 2.4. It is
32 + recommended if compatability with older versions of AppArmor
34 diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c
35 index 16c15ec..42b7c9f 100644
36 --- a/security/apparmor/apparmorfs.c
37 +++ b/security/apparmor/apparmorfs.c
38 @@ -182,6 +182,234 @@ const struct file_operations aa_fs_seq_file_ops = {
39 .release = single_release,
42 +#ifdef CONFIG_SECURITY_APPARMOR_COMPAT_24
44 + * __next_namespace - find the next namespace to list
45 + * @root: root namespace to stop search at (NOT NULL)
46 + * @ns: current ns position (NOT NULL)
48 + * Find the next namespace from @ns under @root and handle all locking needed
49 + * while switching current namespace.
51 + * Returns: next namespace or NULL if at last namespace under @root
52 + * NOTE: will not unlock root->lock
54 +static struct aa_namespace *__next_namespace(struct aa_namespace *root,
55 + struct aa_namespace *ns)
57 + struct aa_namespace *parent;
59 + /* is next namespace a child */
60 + if (!list_empty(&ns->sub_ns)) {
61 + struct aa_namespace *next;
62 + next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
63 + read_lock(&next->lock);
67 + /* check if the next ns is a sibling, parent, gp, .. */
68 + parent = ns->parent;
70 + read_unlock(&ns->lock);
71 + list_for_each_entry_continue(ns, &parent->sub_ns, base.list) {
72 + read_lock(&ns->lock);
78 + parent = parent->parent;
85 + * __first_profile - find the first profile in a namespace
86 + * @root: namespace that is root of profiles being displayed (NOT NULL)
87 + * @ns: namespace to start in (NOT NULL)
89 + * Returns: unrefcounted profile or NULL if no profile
91 +static struct aa_profile *__first_profile(struct aa_namespace *root,
92 + struct aa_namespace *ns)
94 + for ( ; ns; ns = __next_namespace(root, ns)) {
95 + if (!list_empty(&ns->base.profiles))
96 + return list_first_entry(&ns->base.profiles,
97 + struct aa_profile, base.list);
103 + * __next_profile - step to the next profile in a profile tree
104 + * @profile: current profile in tree (NOT NULL)
106 + * Perform a depth first taversal on the profile tree in a namespace
108 + * Returns: next profile or NULL if done
109 + * Requires: profile->ns.lock to be held
111 +static struct aa_profile *__next_profile(struct aa_profile *p)
113 + struct aa_profile *parent;
114 + struct aa_namespace *ns = p->ns;
116 + /* is next profile a child */
117 + if (!list_empty(&p->base.profiles))
118 + return list_first_entry(&p->base.profiles, typeof(*p),
121 + /* is next profile a sibling, parent sibling, gp, subling, .. */
122 + parent = p->parent;
124 + list_for_each_entry_continue(p, &parent->base.profiles,
128 + parent = parent->parent;
131 + /* is next another profile in the namespace */
132 + list_for_each_entry_continue(p, &ns->base.profiles, base.list)
139 + * next_profile - step to the next profile in where ever it may be
140 + * @root: root namespace (NOT NULL)
141 + * @profile: current profile (NOT NULL)
143 + * Returns: next profile or NULL if there isn't one
145 +static struct aa_profile *next_profile(struct aa_namespace *root,
146 + struct aa_profile *profile)
148 + struct aa_profile *next = __next_profile(profile);
152 + /* finished all profiles in namespace move to next namespace */
153 + return __first_profile(root, __next_namespace(root, profile->ns));
157 + * p_start - start a depth first traversal of profile tree
158 + * @f: seq_file to fill
159 + * @pos: current position
161 + * Returns: first profile under current namespace or NULL if none found
163 + * acquires first ns->lock
165 +static void *p_start(struct seq_file *f, loff_t *pos)
166 + __acquires(root->lock)
168 + struct aa_profile *profile = NULL;
169 + struct aa_namespace *root = aa_current_profile()->ns;
171 + f->private = aa_get_namespace(root);
174 + /* find the first profile */
175 + read_lock(&root->lock);
176 + profile = __first_profile(root, root);
178 + /* skip to position */
179 + for (; profile && l > 0; l--)
180 + profile = next_profile(root, profile);
186 + * p_next - read the next profile entry
187 + * @f: seq_file to fill
188 + * @p: profile previously returned
189 + * @pos: current position
191 + * Returns: next profile after @p or NULL if none
193 + * may acquire/release locks in namespace tree as necessary
195 +static void *p_next(struct seq_file *f, void *p, loff_t *pos)
197 + struct aa_profile *profile = p;
198 + struct aa_namespace *root = f->private;
201 + return next_profile(root, profile);
205 + * p_stop - stop depth first traversal
206 + * @f: seq_file we are filling
207 + * @p: the last profile writen
209 + * Release all locking done by p_start/p_next on namespace tree
211 +static void p_stop(struct seq_file *f, void *p)
212 + __releases(root->lock)
214 + struct aa_profile *profile = p;
215 + struct aa_namespace *root = f->private, *ns;
218 + for (ns = profile->ns; ns && ns != root; ns = ns->parent)
219 + read_unlock(&ns->lock);
221 + read_unlock(&root->lock);
222 + aa_put_namespace(root);
226 + * seq_show_profile - show a profile entry
227 + * @f: seq_file to file
228 + * @p: current position (profile) (NOT NULL)
230 + * Returns: error on failure
232 +static int seq_show_profile(struct seq_file *f, void *p)
234 + struct aa_profile *profile = (struct aa_profile *)p;
235 + struct aa_namespace *root = f->private;
237 + if (profile->ns != root)
238 + seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
239 + seq_printf(f, "%s (%s)\n", profile->base.hname,
240 + COMPLAIN_MODE(profile) ? "complain" : "enforce");
245 +static const struct seq_operations aa_fs_profiles_op = {
249 + .show = seq_show_profile,
252 +static int profiles_open(struct inode *inode, struct file *file)
254 + return seq_open(file, &aa_fs_profiles_op);
257 +static int profiles_release(struct inode *inode, struct file *file)
259 + return seq_release(inode, file);
262 +const struct file_operations aa_fs_profiles_fops = {
263 + .open = profiles_open,
265 + .llseek = seq_lseek,
266 + .release = profiles_release,
268 +#endif /* CONFIG_SECURITY_APPARMOR_COMPAT_24 */
270 /** Base file system setup **/
272 static struct aa_fs_entry aa_fs_entry_file[] = {
273 @@ -210,6 +438,9 @@ static struct aa_fs_entry aa_fs_entry_apparmor[] = {
274 AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
275 AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
276 AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
277 +#ifdef CONFIG_SECURITY_APPARMOR_COMPAT_24
278 + AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
280 AA_FS_DIR("features", aa_fs_entry_features),