1
From cdc6b35345e5bcfe92bb2b52ef003f94ceedd40d 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 2/3] AppArmor: compatibility patch for v5 interface
6
Signed-off-by: John Johansen <john.johansen@canonical.com>
8
security/apparmor/Kconfig | 9 +
9
security/apparmor/Makefile | 1 +
10
security/apparmor/apparmorfs-24.c | 287 ++++++++++++++++++++++++++++++++
11
security/apparmor/apparmorfs.c | 18 ++-
12
security/apparmor/include/apparmorfs.h | 6 +
13
5 files changed, 319 insertions(+), 2 deletions(-)
14
create mode 100644 security/apparmor/apparmorfs-24.c
16
diff --git a/security/apparmor/Kconfig b/security/apparmor/Kconfig
17
index 9b9013b..51ebf96 100644
18
--- a/security/apparmor/Kconfig
19
+++ b/security/apparmor/Kconfig
20
@@ -29,3 +29,12 @@ config SECURITY_APPARMOR_BOOTPARAM_VALUE
23
If you are unsure how to answer this question, answer 1.
25
+config SECURITY_APPARMOR_COMPAT_24
26
+ bool "Enable AppArmor 2.4 compatability"
27
+ depends on SECURITY_APPARMOR
30
+ This option enables compatability with AppArmor 2.4. It is
31
+ recommended if compatability with older versions of AppArmor
33
diff --git a/security/apparmor/Makefile b/security/apparmor/Makefile
34
index 7cefef9..0bb604b 100644
35
--- a/security/apparmor/Makefile
36
+++ b/security/apparmor/Makefile
37
@@ -5,6 +5,7 @@ obj-$(CONFIG_SECURITY_APPARMOR) += apparmor.o
38
apparmor-y := apparmorfs.o audit.o capability.o context.o ipc.o lib.o match.o \
39
path.o domain.o policy.o policy_unpack.o procattr.o lsm.o \
40
resource.o sid.o file.o net.o
41
+apparmor-$(CONFIG_SECURITY_APPARMOR_COMPAT_24) += apparmorfs-24.o
43
clean-files := capability_names.h rlim_names.h af_names.h
45
diff --git a/security/apparmor/apparmorfs-24.c b/security/apparmor/apparmorfs-24.c
47
index 0000000..dc8c744
49
+++ b/security/apparmor/apparmorfs-24.c
52
+ * AppArmor security module
54
+ * This file contains AppArmor /sys/kernel/secrutiy/apparmor interface functions
56
+ * Copyright (C) 1998-2008 Novell/SUSE
57
+ * Copyright 2009-2010 Canonical Ltd.
59
+ * This program is free software; you can redistribute it and/or
60
+ * modify it under the terms of the GNU General Public License as
61
+ * published by the Free Software Foundation, version 2 of the
65
+ * This file contain functions providing an interface for <= AppArmor 2.4
66
+ * compatibility. It is dependent on CONFIG_SECURITY_APPARMOR_COMPAT_24
67
+ * being set (see Makefile).
70
+#include <linux/security.h>
71
+#include <linux/vmalloc.h>
72
+#include <linux/module.h>
73
+#include <linux/seq_file.h>
74
+#include <linux/uaccess.h>
75
+#include <linux/namei.h>
77
+#include "include/apparmor.h"
78
+#include "include/audit.h"
79
+#include "include/context.h"
80
+#include "include/policy.h"
83
+/* apparmor/matching */
84
+static ssize_t aa_matching_read(struct file *file, char __user *buf,
85
+ size_t size, loff_t *ppos)
87
+ const char matching[] = "pattern=aadfa audit perms=crwxamlk/ "
90
+ return simple_read_from_buffer(buf, size, ppos, matching,
91
+ sizeof(matching) - 1);
94
+const struct file_operations aa_fs_matching_fops = {
95
+ .read = aa_matching_read,
98
+/* apparmor/features */
99
+static ssize_t aa_features_read(struct file *file, char __user *buf,
100
+ size_t size, loff_t *ppos)
102
+ const char features[] = "file=3.1 capability=2.0 network=1.0 "
103
+ "change_hat=1.5 change_profile=1.1 " "aanamespaces=1.1 rlimit=1.1";
105
+ return simple_read_from_buffer(buf, size, ppos, features,
106
+ sizeof(features) - 1);
109
+const struct file_operations aa_fs_features_fops = {
110
+ .read = aa_features_read,
114
+ * __next_namespace - find the next namespace to list
115
+ * @root: root namespace to stop search at (NOT NULL)
116
+ * @ns: current ns position (NOT NULL)
118
+ * Find the next namespace from @ns under @root and handle all locking needed
119
+ * while switching current namespace.
121
+ * Returns: next namespace or NULL if at last namespace under @root
122
+ * NOTE: will not unlock root->lock
124
+static struct aa_namespace *__next_namespace(struct aa_namespace *root,
125
+ struct aa_namespace *ns)
127
+ struct aa_namespace *parent;
129
+ /* is next namespace a child */
130
+ if (!list_empty(&ns->sub_ns)) {
131
+ struct aa_namespace *next;
132
+ next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
133
+ read_lock(&next->lock);
137
+ /* check if the next ns is a sibling, parent, gp, .. */
138
+ parent = ns->parent;
140
+ read_unlock(&ns->lock);
141
+ list_for_each_entry_continue(ns, &parent->sub_ns, base.list) {
142
+ read_lock(&ns->lock);
145
+ if (parent == root)
148
+ parent = parent->parent;
155
+ * __first_profile - find the first profile in a namespace
156
+ * @root: namespace that is root of profiles being displayed (NOT NULL)
157
+ * @ns: namespace to start in (NOT NULL)
159
+ * Returns: unrefcounted profile or NULL if no profile
161
+static struct aa_profile *__first_profile(struct aa_namespace *root,
162
+ struct aa_namespace *ns)
164
+ for ( ; ns; ns = __next_namespace(root, ns)) {
165
+ if (!list_empty(&ns->base.profiles))
166
+ return list_first_entry(&ns->base.profiles,
167
+ struct aa_profile, base.list);
173
+ * __next_profile - step to the next profile in a profile tree
174
+ * @profile: current profile in tree (NOT NULL)
176
+ * Perform a depth first taversal on the profile tree in a namespace
178
+ * Returns: next profile or NULL if done
179
+ * Requires: profile->ns.lock to be held
181
+static struct aa_profile *__next_profile(struct aa_profile *p)
183
+ struct aa_profile *parent;
184
+ struct aa_namespace *ns = p->ns;
186
+ /* is next profile a child */
187
+ if (!list_empty(&p->base.profiles))
188
+ return list_first_entry(&p->base.profiles, typeof(*p),
191
+ /* is next profile a sibling, parent sibling, gp, subling, .. */
192
+ parent = p->parent;
194
+ list_for_each_entry_continue(p, &parent->base.profiles,
198
+ parent = parent->parent;
201
+ /* is next another profile in the namespace */
202
+ list_for_each_entry_continue(p, &ns->base.profiles, base.list)
209
+ * next_profile - step to the next profile in where ever it may be
210
+ * @root: root namespace (NOT NULL)
211
+ * @profile: current profile (NOT NULL)
213
+ * Returns: next profile or NULL if there isn't one
215
+static struct aa_profile *next_profile(struct aa_namespace *root,
216
+ struct aa_profile *profile)
218
+ struct aa_profile *next = __next_profile(profile);
222
+ /* finished all profiles in namespace move to next namespace */
223
+ return __first_profile(root, __next_namespace(root, profile->ns));
227
+ * p_start - start a depth first traversal of profile tree
228
+ * @f: seq_file to fill
229
+ * @pos: current position
231
+ * Returns: first profile under current namespace or NULL if none found
233
+ * acquires first ns->lock
235
+static void *p_start(struct seq_file *f, loff_t *pos)
236
+ __acquires(root->lock)
238
+ struct aa_profile *profile = NULL;
239
+ struct aa_namespace *root = aa_current_profile()->ns;
241
+ f->private = aa_get_namespace(root);
244
+ /* find the first profile */
245
+ read_lock(&root->lock);
246
+ profile = __first_profile(root, root);
248
+ /* skip to position */
249
+ for (; profile && l > 0; l--)
250
+ profile = next_profile(root, profile);
256
+ * p_next - read the next profile entry
257
+ * @f: seq_file to fill
258
+ * @p: profile previously returned
259
+ * @pos: current position
261
+ * Returns: next profile after @p or NULL if none
263
+ * may acquire/release locks in namespace tree as necessary
265
+static void *p_next(struct seq_file *f, void *p, loff_t *pos)
267
+ struct aa_profile *profile = p;
268
+ struct aa_namespace *root = f->private;
271
+ return next_profile(root, profile);
275
+ * p_stop - stop depth first traversal
276
+ * @f: seq_file we are filling
277
+ * @p: the last profile writen
279
+ * Release all locking done by p_start/p_next on namespace tree
281
+static void p_stop(struct seq_file *f, void *p)
282
+ __releases(root->lock)
284
+ struct aa_profile *profile = p;
285
+ struct aa_namespace *root = f->private, *ns;
288
+ for (ns = profile->ns; ns && ns != root; ns = ns->parent)
289
+ read_unlock(&ns->lock);
291
+ read_unlock(&root->lock);
292
+ aa_put_namespace(root);
296
+ * seq_show_profile - show a profile entry
297
+ * @f: seq_file to file
298
+ * @p: current position (profile) (NOT NULL)
300
+ * Returns: error on failure
302
+static int seq_show_profile(struct seq_file *f, void *p)
304
+ struct aa_profile *profile = (struct aa_profile *)p;
305
+ struct aa_namespace *root = f->private;
307
+ if (profile->ns != root)
308
+ seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
309
+ seq_printf(f, "%s (%s)\n", profile->base.hname,
310
+ COMPLAIN_MODE(profile) ? "complain" : "enforce");
315
+static const struct seq_operations aa_fs_profiles_op = {
319
+ .show = seq_show_profile,
322
+static int profiles_open(struct inode *inode, struct file *file)
324
+ return seq_open(file, &aa_fs_profiles_op);
327
+static int profiles_release(struct inode *inode, struct file *file)
329
+ return seq_release(inode, file);
332
+const struct file_operations aa_fs_profiles_fops = {
333
+ .open = profiles_open,
335
+ .llseek = seq_lseek,
336
+ .release = profiles_release,
338
diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c
339
index 0848292..28c52ac 100644
340
--- a/security/apparmor/apparmorfs.c
341
+++ b/security/apparmor/apparmorfs.c
342
@@ -187,7 +187,11 @@ void __init aa_destroy_aafs(void)
343
aafs_remove(".remove");
344
aafs_remove(".replace");
345
aafs_remove(".load");
347
+#ifdef CONFIG_SECURITY_APPARMOR_COMPAT_24
348
+ aafs_remove("profiles");
349
+ aafs_remove("matching");
350
+ aafs_remove("features");
352
securityfs_remove(aa_fs_dentry);
355
@@ -218,7 +222,17 @@ int __init aa_create_aafs(void)
360
+#ifdef CONFIG_SECURITY_APPARMOR_COMPAT_24
361
+ error = aafs_create("matching", 0444, &aa_fs_matching_fops);
364
+ error = aafs_create("features", 0444, &aa_fs_features_fops);
368
+ error = aafs_create("profiles", 0440, &aa_fs_profiles_fops);
371
error = aafs_create(".load", 0640, &aa_fs_profile_load);
374
diff --git a/security/apparmor/include/apparmorfs.h b/security/apparmor/include/apparmorfs.h
375
index cb1e93a..14f955c 100644
376
--- a/security/apparmor/include/apparmorfs.h
377
+++ b/security/apparmor/include/apparmorfs.h
380
extern void __init aa_destroy_aafs(void);
382
+#ifdef CONFIG_SECURITY_APPARMOR_COMPAT_24
383
+extern const struct file_operations aa_fs_matching_fops;
384
+extern const struct file_operations aa_fs_features_fops;
385
+extern const struct file_operations aa_fs_profiles_fops;
388
#endif /* __AA_APPARMORFS_H */