1
From a89f0bb2dfd82445e58f5f6bfceb40ab8bee543f 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 | 2 +
10
security/apparmor/apparmorfs-24.c | 287 ++++++++++++++++++++++++++++++++
11
security/apparmor/apparmorfs.c | 18 ++-
12
security/apparmor/include/apparmorfs.h | 6 +
13
5 files changed, 320 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 a9a1db0..e5e8968 100644
35
--- a/security/apparmor/Makefile
36
+++ b/security/apparmor/Makefile
37
@@ -6,6 +6,8 @@ apparmor-y := apparmorfs.o audit.o capability.o context.o ipc.o lib.o match.o \
38
path.o domain.o policy.o policy_unpack.o procattr.o lsm.o \
39
resource.o sid.o file.o net.o
41
+apparmor-$(CONFIG_SECURITY_APPARMOR_COMPAT_24) += apparmorfs-24.o
43
clean-files: capability_names.h af_names.h
45
quiet_cmd_make-caps = GEN $@
46
diff --git a/security/apparmor/apparmorfs-24.c b/security/apparmor/apparmorfs-24.c
48
index 0000000..dc8c744
50
+++ b/security/apparmor/apparmorfs-24.c
53
+ * AppArmor security module
55
+ * This file contains AppArmor /sys/kernel/secrutiy/apparmor interface functions
57
+ * Copyright (C) 1998-2008 Novell/SUSE
58
+ * Copyright 2009-2010 Canonical Ltd.
60
+ * This program is free software; you can redistribute it and/or
61
+ * modify it under the terms of the GNU General Public License as
62
+ * published by the Free Software Foundation, version 2 of the
66
+ * This file contain functions providing an interface for <= AppArmor 2.4
67
+ * compatibility. It is dependent on CONFIG_SECURITY_APPARMOR_COMPAT_24
68
+ * being set (see Makefile).
71
+#include <linux/security.h>
72
+#include <linux/vmalloc.h>
73
+#include <linux/module.h>
74
+#include <linux/seq_file.h>
75
+#include <linux/uaccess.h>
76
+#include <linux/namei.h>
78
+#include "include/apparmor.h"
79
+#include "include/audit.h"
80
+#include "include/context.h"
81
+#include "include/policy.h"
84
+/* apparmor/matching */
85
+static ssize_t aa_matching_read(struct file *file, char __user *buf,
86
+ size_t size, loff_t *ppos)
88
+ const char matching[] = "pattern=aadfa audit perms=crwxamlk/ "
91
+ return simple_read_from_buffer(buf, size, ppos, matching,
92
+ sizeof(matching) - 1);
95
+const struct file_operations aa_fs_matching_fops = {
96
+ .read = aa_matching_read,
99
+/* apparmor/features */
100
+static ssize_t aa_features_read(struct file *file, char __user *buf,
101
+ size_t size, loff_t *ppos)
103
+ const char features[] = "file=3.1 capability=2.0 network=1.0 "
104
+ "change_hat=1.5 change_profile=1.1 " "aanamespaces=1.1 rlimit=1.1";
106
+ return simple_read_from_buffer(buf, size, ppos, features,
107
+ sizeof(features) - 1);
110
+const struct file_operations aa_fs_features_fops = {
111
+ .read = aa_features_read,
115
+ * __next_namespace - find the next namespace to list
116
+ * @root: root namespace to stop search at (NOT NULL)
117
+ * @ns: current ns position (NOT NULL)
119
+ * Find the next namespace from @ns under @root and handle all locking needed
120
+ * while switching current namespace.
122
+ * Returns: next namespace or NULL if at last namespace under @root
123
+ * NOTE: will not unlock root->lock
125
+static struct aa_namespace *__next_namespace(struct aa_namespace *root,
126
+ struct aa_namespace *ns)
128
+ struct aa_namespace *parent;
130
+ /* is next namespace a child */
131
+ if (!list_empty(&ns->sub_ns)) {
132
+ struct aa_namespace *next;
133
+ next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
134
+ read_lock(&next->lock);
138
+ /* check if the next ns is a sibling, parent, gp, .. */
139
+ parent = ns->parent;
141
+ read_unlock(&ns->lock);
142
+ list_for_each_entry_continue(ns, &parent->sub_ns, base.list) {
143
+ read_lock(&ns->lock);
146
+ if (parent == root)
149
+ parent = parent->parent;
156
+ * __first_profile - find the first profile in a namespace
157
+ * @root: namespace that is root of profiles being displayed (NOT NULL)
158
+ * @ns: namespace to start in (NOT NULL)
160
+ * Returns: unrefcounted profile or NULL if no profile
162
+static struct aa_profile *__first_profile(struct aa_namespace *root,
163
+ struct aa_namespace *ns)
165
+ for ( ; ns; ns = __next_namespace(root, ns)) {
166
+ if (!list_empty(&ns->base.profiles))
167
+ return list_first_entry(&ns->base.profiles,
168
+ struct aa_profile, base.list);
174
+ * __next_profile - step to the next profile in a profile tree
175
+ * @profile: current profile in tree (NOT NULL)
177
+ * Perform a depth first taversal on the profile tree in a namespace
179
+ * Returns: next profile or NULL if done
180
+ * Requires: profile->ns.lock to be held
182
+static struct aa_profile *__next_profile(struct aa_profile *p)
184
+ struct aa_profile *parent;
185
+ struct aa_namespace *ns = p->ns;
187
+ /* is next profile a child */
188
+ if (!list_empty(&p->base.profiles))
189
+ return list_first_entry(&p->base.profiles, typeof(*p),
192
+ /* is next profile a sibling, parent sibling, gp, subling, .. */
193
+ parent = p->parent;
195
+ list_for_each_entry_continue(p, &parent->base.profiles,
199
+ parent = parent->parent;
202
+ /* is next another profile in the namespace */
203
+ list_for_each_entry_continue(p, &ns->base.profiles, base.list)
210
+ * next_profile - step to the next profile in where ever it may be
211
+ * @root: root namespace (NOT NULL)
212
+ * @profile: current profile (NOT NULL)
214
+ * Returns: next profile or NULL if there isn't one
216
+static struct aa_profile *next_profile(struct aa_namespace *root,
217
+ struct aa_profile *profile)
219
+ struct aa_profile *next = __next_profile(profile);
223
+ /* finished all profiles in namespace move to next namespace */
224
+ return __first_profile(root, __next_namespace(root, profile->ns));
228
+ * p_start - start a depth first traversal of profile tree
229
+ * @f: seq_file to fill
230
+ * @pos: current position
232
+ * Returns: first profile under current namespace or NULL if none found
234
+ * acquires first ns->lock
236
+static void *p_start(struct seq_file *f, loff_t *pos)
237
+ __acquires(root->lock)
239
+ struct aa_profile *profile = NULL;
240
+ struct aa_namespace *root = aa_current_profile()->ns;
242
+ f->private = aa_get_namespace(root);
245
+ /* find the first profile */
246
+ read_lock(&root->lock);
247
+ profile = __first_profile(root, root);
249
+ /* skip to position */
250
+ for (; profile && l > 0; l--)
251
+ profile = next_profile(root, profile);
257
+ * p_next - read the next profile entry
258
+ * @f: seq_file to fill
259
+ * @p: profile previously returned
260
+ * @pos: current position
262
+ * Returns: next profile after @p or NULL if none
264
+ * may acquire/release locks in namespace tree as necessary
266
+static void *p_next(struct seq_file *f, void *p, loff_t *pos)
268
+ struct aa_profile *profile = p;
269
+ struct aa_namespace *root = f->private;
272
+ return next_profile(root, profile);
276
+ * p_stop - stop depth first traversal
277
+ * @f: seq_file we are filling
278
+ * @p: the last profile writen
280
+ * Release all locking done by p_start/p_next on namespace tree
282
+static void p_stop(struct seq_file *f, void *p)
283
+ __releases(root->lock)
285
+ struct aa_profile *profile = p;
286
+ struct aa_namespace *root = f->private, *ns;
289
+ for (ns = profile->ns; ns && ns != root; ns = ns->parent)
290
+ read_unlock(&ns->lock);
292
+ read_unlock(&root->lock);
293
+ aa_put_namespace(root);
297
+ * seq_show_profile - show a profile entry
298
+ * @f: seq_file to file
299
+ * @p: current position (profile) (NOT NULL)
301
+ * Returns: error on failure
303
+static int seq_show_profile(struct seq_file *f, void *p)
305
+ struct aa_profile *profile = (struct aa_profile *)p;
306
+ struct aa_namespace *root = f->private;
308
+ if (profile->ns != root)
309
+ seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
310
+ seq_printf(f, "%s (%s)\n", profile->base.hname,
311
+ COMPLAIN_MODE(profile) ? "complain" : "enforce");
316
+static const struct seq_operations aa_fs_profiles_op = {
320
+ .show = seq_show_profile,
323
+static int profiles_open(struct inode *inode, struct file *file)
325
+ return seq_open(file, &aa_fs_profiles_op);
328
+static int profiles_release(struct inode *inode, struct file *file)
330
+ return seq_release(inode, file);
333
+const struct file_operations aa_fs_profiles_fops = {
334
+ .open = profiles_open,
336
+ .llseek = seq_lseek,
337
+ .release = profiles_release,
339
diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c
340
index 0848292..28c52ac 100644
341
--- a/security/apparmor/apparmorfs.c
342
+++ b/security/apparmor/apparmorfs.c
343
@@ -187,7 +187,11 @@ void __init aa_destroy_aafs(void)
344
aafs_remove(".remove");
345
aafs_remove(".replace");
346
aafs_remove(".load");
348
+#ifdef CONFIG_SECURITY_APPARMOR_COMPAT_24
349
+ aafs_remove("profiles");
350
+ aafs_remove("matching");
351
+ aafs_remove("features");
353
securityfs_remove(aa_fs_dentry);
356
@@ -218,7 +222,17 @@ int __init aa_create_aafs(void)
361
+#ifdef CONFIG_SECURITY_APPARMOR_COMPAT_24
362
+ error = aafs_create("matching", 0444, &aa_fs_matching_fops);
365
+ error = aafs_create("features", 0444, &aa_fs_features_fops);
369
+ error = aafs_create("profiles", 0440, &aa_fs_profiles_fops);
372
error = aafs_create(".load", 0640, &aa_fs_profile_load);
375
diff --git a/security/apparmor/include/apparmorfs.h b/security/apparmor/include/apparmorfs.h
376
index cb1e93a..14f955c 100644
377
--- a/security/apparmor/include/apparmorfs.h
378
+++ b/security/apparmor/include/apparmorfs.h
381
extern void __init aa_destroy_aafs(void);
383
+#ifdef CONFIG_SECURITY_APPARMOR_COMPAT_24
384
+extern const struct file_operations aa_fs_matching_fops;
385
+extern const struct file_operations aa_fs_features_fops;
386
+extern const struct file_operations aa_fs_profiles_fops;
389
#endif /* __AA_APPARMORFS_H */