2 * AppArmor security module
4 * This file contains AppArmor /sys/kernel/secrutiy/apparmor interface functions
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
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
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).
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>
27 #include "include/apparmor.h"
28 #include "include/audit.h"
29 #include "include/context.h"
30 #include "include/policy.h"
33 /* apparmor/matching */
34 static ssize_t aa_matching_read(struct file *file, char __user *buf,
35 size_t size, loff_t *ppos)
37 const char matching[] = "pattern=aadfa audit perms=crwxamlk/ "
40 return simple_read_from_buffer(buf, size, ppos, matching,
41 sizeof(matching) - 1);
44 const struct file_operations aa_fs_matching_fops = {
45 .read = aa_matching_read,
48 /* apparmor/features */
49 static ssize_t aa_features_read(struct file *file, char __user *buf,
50 size_t size, loff_t *ppos)
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";
55 return simple_read_from_buffer(buf, size, ppos, features,
56 sizeof(features) - 1);
59 const struct file_operations aa_fs_features_fops = {
60 .read = aa_features_read,
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)
68 * Find the next namespace from @ns under @root and handle all locking needed
69 * while switching current namespace.
71 * Returns: next namespace or NULL if at last namespace under @root
72 * NOTE: will not unlock root->lock
74 static struct aa_namespace *__next_namespace(struct aa_namespace *root,
75 struct aa_namespace *ns)
77 struct aa_namespace *parent;
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);
87 /* check if the next ns is a sibling, parent, gp, .. */
90 read_unlock(&ns->lock);
91 list_for_each_entry_continue(ns, &parent->sub_ns, base.list) {
98 parent = parent->parent;
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)
109 * Returns: unrefcounted profile or NULL if no profile
111 static struct aa_profile *__first_profile(struct aa_namespace *root,
112 struct aa_namespace *ns)
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);
123 * __next_profile - step to the next profile in a profile tree
124 * @profile: current profile in tree (NOT NULL)
126 * Perform a depth first taversal on the profile tree in a namespace
128 * Returns: next profile or NULL if done
129 * Requires: profile->ns.lock to be held
131 static struct aa_profile *__next_profile(struct aa_profile *p)
133 struct aa_profile *parent;
134 struct aa_namespace *ns = p->ns;
136 /* is next profile a child */
137 if (!list_empty(&p->base.profiles))
138 return list_first_entry(&p->base.profiles, typeof(*p),
141 /* is next profile a sibling, parent sibling, gp, subling, .. */
144 list_for_each_entry_continue(p, &parent->base.profiles,
148 parent = parent->parent;
151 /* is next another profile in the namespace */
152 list_for_each_entry_continue(p, &ns->base.profiles, base.list)
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)
163 * Returns: next profile or NULL if there isn't one
165 static struct aa_profile *next_profile(struct aa_namespace *root,
166 struct aa_profile *profile)
168 struct aa_profile *next = __next_profile(profile);
172 /* finished all profiles in namespace move to next namespace */
173 return __first_profile(root, __next_namespace(root, profile->ns));
177 * p_start - start a depth first traversal of profile tree
178 * @f: seq_file to fill
179 * @pos: current position
181 * Returns: first profile under current namespace or NULL if none found
183 * acquires first ns->lock
185 static void *p_start(struct seq_file *f, loff_t *pos)
186 __acquires(root->lock)
188 struct aa_profile *profile = NULL;
189 struct aa_namespace *root = aa_current_profile()->ns;
191 f->private = aa_get_namespace(root);
194 /* find the first profile */
195 read_lock(&root->lock);
196 profile = __first_profile(root, root);
198 /* skip to position */
199 for (; profile && l > 0; l--)
200 profile = next_profile(root, profile);
206 * p_next - read the next profile entry
207 * @f: seq_file to fill
208 * @p: profile previously returned
209 * @pos: current position
211 * Returns: next profile after @p or NULL if none
213 * may acquire/release locks in namespace tree as necessary
215 static void *p_next(struct seq_file *f, void *p, loff_t *pos)
217 struct aa_profile *profile = p;
218 struct aa_namespace *root = f->private;
221 return next_profile(root, profile);
225 * p_stop - stop depth first traversal
226 * @f: seq_file we are filling
227 * @p: the last profile writen
229 * Release all locking done by p_start/p_next on namespace tree
231 static void p_stop(struct seq_file *f, void *p)
232 __releases(root->lock)
234 struct aa_profile *profile = p;
235 struct aa_namespace *root = f->private, *ns;
238 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
239 read_unlock(&ns->lock);
241 read_unlock(&root->lock);
242 aa_put_namespace(root);
246 * seq_show_profile - show a profile entry
247 * @f: seq_file to file
248 * @p: current position (profile) (NOT NULL)
250 * Returns: error on failure
252 static int seq_show_profile(struct seq_file *f, void *p)
254 struct aa_profile *profile = (struct aa_profile *)p;
255 struct aa_namespace *root = f->private;
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");
265 static const struct seq_operations aa_fs_profiles_op = {
269 .show = seq_show_profile,
272 static int profiles_open(struct inode *inode, struct file *file)
274 return seq_open(file, &aa_fs_profiles_op);
277 static int profiles_release(struct inode *inode, struct file *file)
279 return seq_release(inode, file);
282 const struct file_operations aa_fs_profiles_fops = {
283 .open = profiles_open,
286 .release = profiles_release,