]>
Commit | Line | Data |
---|---|---|
63e2b423 JJ |
1 | /* |
2 | * AppArmor security module | |
3 | * | |
4 | * This file contains AppArmor /sys/kernel/security/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 | #include <linux/security.h> | |
16 | #include <linux/vmalloc.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/seq_file.h> | |
19 | #include <linux/uaccess.h> | |
20 | #include <linux/namei.h> | |
e74abcf3 | 21 | #include <linux/capability.h> |
63e2b423 JJ |
22 | |
23 | #include "include/apparmor.h" | |
24 | #include "include/apparmorfs.h" | |
25 | #include "include/audit.h" | |
26 | #include "include/context.h" | |
27 | #include "include/policy.h" | |
28 | ||
29 | /** | |
30 | * aa_simple_write_to_buffer - common routine for getting policy from user | |
31 | * @op: operation doing the user buffer copy | |
32 | * @userbuf: user buffer to copy data from (NOT NULL) | |
3ed02ada | 33 | * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) |
63e2b423 JJ |
34 | * @copy_size: size of data to copy from user buffer |
35 | * @pos: position write is at in the file (NOT NULL) | |
36 | * | |
37 | * Returns: kernel buffer containing copy of user buffer data or an | |
38 | * ERR_PTR on failure. | |
39 | */ | |
40 | static char *aa_simple_write_to_buffer(int op, const char __user *userbuf, | |
41 | size_t alloc_size, size_t copy_size, | |
42 | loff_t *pos) | |
43 | { | |
44 | char *data; | |
45 | ||
3ed02ada JJ |
46 | BUG_ON(copy_size > alloc_size); |
47 | ||
63e2b423 JJ |
48 | if (*pos != 0) |
49 | /* only writes from pos 0, that is complete writes */ | |
50 | return ERR_PTR(-ESPIPE); | |
51 | ||
52 | /* | |
53 | * Don't allow profile load/replace/remove from profiles that don't | |
54 | * have CAP_MAC_ADMIN | |
55 | */ | |
56 | if (!aa_may_manage_policy(op)) | |
57 | return ERR_PTR(-EACCES); | |
58 | ||
59 | /* freed by caller to simple_write_to_buffer */ | |
60 | data = kvmalloc(alloc_size); | |
61 | if (data == NULL) | |
62 | return ERR_PTR(-ENOMEM); | |
63 | ||
64 | if (copy_from_user(data, userbuf, copy_size)) { | |
65 | kvfree(data); | |
66 | return ERR_PTR(-EFAULT); | |
67 | } | |
68 | ||
69 | return data; | |
70 | } | |
71 | ||
72 | ||
73 | /* .load file hook fn to load policy */ | |
74 | static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, | |
75 | loff_t *pos) | |
76 | { | |
77 | char *data; | |
78 | ssize_t error; | |
79 | ||
80 | data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos); | |
81 | ||
82 | error = PTR_ERR(data); | |
83 | if (!IS_ERR(data)) { | |
84 | error = aa_replace_profiles(data, size, PROF_ADD); | |
85 | kvfree(data); | |
86 | } | |
87 | ||
88 | return error; | |
89 | } | |
90 | ||
91 | static const struct file_operations aa_fs_profile_load = { | |
6038f373 AB |
92 | .write = profile_load, |
93 | .llseek = default_llseek, | |
63e2b423 JJ |
94 | }; |
95 | ||
96 | /* .replace file hook fn to load and/or replace policy */ | |
97 | static ssize_t profile_replace(struct file *f, const char __user *buf, | |
98 | size_t size, loff_t *pos) | |
99 | { | |
100 | char *data; | |
101 | ssize_t error; | |
102 | ||
103 | data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos); | |
104 | error = PTR_ERR(data); | |
105 | if (!IS_ERR(data)) { | |
106 | error = aa_replace_profiles(data, size, PROF_REPLACE); | |
107 | kvfree(data); | |
108 | } | |
109 | ||
110 | return error; | |
111 | } | |
112 | ||
113 | static const struct file_operations aa_fs_profile_replace = { | |
6038f373 AB |
114 | .write = profile_replace, |
115 | .llseek = default_llseek, | |
63e2b423 JJ |
116 | }; |
117 | ||
118 | /* .remove file hook fn to remove loaded policy */ | |
119 | static ssize_t profile_remove(struct file *f, const char __user *buf, | |
120 | size_t size, loff_t *pos) | |
121 | { | |
122 | char *data; | |
123 | ssize_t error; | |
124 | ||
125 | /* | |
126 | * aa_remove_profile needs a null terminated string so 1 extra | |
127 | * byte is allocated and the copied data is null terminated. | |
128 | */ | |
129 | data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos); | |
130 | ||
131 | error = PTR_ERR(data); | |
132 | if (!IS_ERR(data)) { | |
133 | data[size] = 0; | |
134 | error = aa_remove_profiles(data, size); | |
135 | kvfree(data); | |
136 | } | |
137 | ||
138 | return error; | |
139 | } | |
140 | ||
141 | static const struct file_operations aa_fs_profile_remove = { | |
6038f373 AB |
142 | .write = profile_remove, |
143 | .llseek = default_llseek, | |
63e2b423 JJ |
144 | }; |
145 | ||
e74abcf3 KC |
146 | static int aa_fs_seq_show(struct seq_file *seq, void *v) |
147 | { | |
148 | struct aa_fs_entry *fs_file = seq->private; | |
149 | ||
150 | if (!fs_file) | |
151 | return 0; | |
152 | ||
153 | switch (fs_file->v_type) { | |
154 | case AA_FS_TYPE_BOOLEAN: | |
155 | seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); | |
156 | break; | |
a9bf8e9f KC |
157 | case AA_FS_TYPE_STRING: |
158 | seq_printf(seq, "%s\n", fs_file->v.string); | |
159 | break; | |
e74abcf3 KC |
160 | case AA_FS_TYPE_U64: |
161 | seq_printf(seq, "%#08lx\n", fs_file->v.u64); | |
162 | break; | |
163 | default: | |
164 | /* Ignore unpritable entry types. */ | |
165 | break; | |
166 | } | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | static int aa_fs_seq_open(struct inode *inode, struct file *file) | |
172 | { | |
173 | return single_open(file, aa_fs_seq_show, inode->i_private); | |
174 | } | |
175 | ||
176 | const struct file_operations aa_fs_seq_file_ops = { | |
177 | .owner = THIS_MODULE, | |
178 | .open = aa_fs_seq_open, | |
179 | .read = seq_read, | |
180 | .llseek = seq_lseek, | |
181 | .release = single_release, | |
182 | }; | |
183 | ||
63e2b423 JJ |
184 | /** Base file system setup **/ |
185 | ||
a9bf8e9f KC |
186 | static struct aa_fs_entry aa_fs_entry_file[] = { |
187 | AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \ | |
188 | "link lock"), | |
189 | { } | |
190 | }; | |
191 | ||
e74abcf3 KC |
192 | static struct aa_fs_entry aa_fs_entry_domain[] = { |
193 | AA_FS_FILE_BOOLEAN("change_hat", 1), | |
194 | AA_FS_FILE_BOOLEAN("change_hatv", 1), | |
195 | AA_FS_FILE_BOOLEAN("change_onexec", 1), | |
196 | AA_FS_FILE_BOOLEAN("change_profile", 1), | |
197 | { } | |
198 | }; | |
199 | ||
200 | static struct aa_fs_entry aa_fs_entry_features[] = { | |
201 | AA_FS_DIR("domain", aa_fs_entry_domain), | |
a9bf8e9f | 202 | AA_FS_DIR("file", aa_fs_entry_file), |
e74abcf3 KC |
203 | AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), |
204 | { } | |
205 | }; | |
206 | ||
9acd494b KC |
207 | static struct aa_fs_entry aa_fs_entry_apparmor[] = { |
208 | AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load), | |
209 | AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace), | |
210 | AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove), | |
e74abcf3 | 211 | AA_FS_DIR("features", aa_fs_entry_features), |
9acd494b KC |
212 | { } |
213 | }; | |
63e2b423 | 214 | |
9acd494b KC |
215 | static struct aa_fs_entry aa_fs_entry = |
216 | AA_FS_DIR("apparmor", aa_fs_entry_apparmor); | |
63e2b423 | 217 | |
9acd494b KC |
218 | /** |
219 | * aafs_create_file - create a file entry in the apparmor securityfs | |
220 | * @fs_file: aa_fs_entry to build an entry for (NOT NULL) | |
221 | * @parent: the parent dentry in the securityfs | |
222 | * | |
223 | * Use aafs_remove_file to remove entries created with this fn. | |
224 | */ | |
225 | static int __init aafs_create_file(struct aa_fs_entry *fs_file, | |
226 | struct dentry *parent) | |
227 | { | |
228 | int error = 0; | |
229 | ||
230 | fs_file->dentry = securityfs_create_file(fs_file->name, | |
231 | S_IFREG | fs_file->mode, | |
232 | parent, fs_file, | |
233 | fs_file->file_ops); | |
234 | if (IS_ERR(fs_file->dentry)) { | |
235 | error = PTR_ERR(fs_file->dentry); | |
236 | fs_file->dentry = NULL; | |
63e2b423 | 237 | } |
9acd494b | 238 | return error; |
63e2b423 JJ |
239 | } |
240 | ||
241 | /** | |
9acd494b KC |
242 | * aafs_create_dir - recursively create a directory entry in the securityfs |
243 | * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL) | |
244 | * @parent: the parent dentry in the securityfs | |
63e2b423 | 245 | * |
9acd494b | 246 | * Use aafs_remove_dir to remove entries created with this fn. |
63e2b423 | 247 | */ |
9acd494b KC |
248 | static int __init aafs_create_dir(struct aa_fs_entry *fs_dir, |
249 | struct dentry *parent) | |
63e2b423 | 250 | { |
9acd494b KC |
251 | int error; |
252 | struct aa_fs_entry *fs_file; | |
63e2b423 | 253 | |
9acd494b KC |
254 | fs_dir->dentry = securityfs_create_dir(fs_dir->name, parent); |
255 | if (IS_ERR(fs_dir->dentry)) { | |
256 | error = PTR_ERR(fs_dir->dentry); | |
257 | fs_dir->dentry = NULL; | |
258 | goto failed; | |
259 | } | |
63e2b423 | 260 | |
9acd494b KC |
261 | for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) { |
262 | if (fs_file->v_type == AA_FS_TYPE_DIR) | |
263 | error = aafs_create_dir(fs_file, fs_dir->dentry); | |
264 | else | |
265 | error = aafs_create_file(fs_file, fs_dir->dentry); | |
266 | if (error) | |
267 | goto failed; | |
268 | } | |
269 | ||
270 | return 0; | |
271 | ||
272 | failed: | |
273 | return error; | |
274 | } | |
275 | ||
276 | /** | |
277 | * aafs_remove_file - drop a single file entry in the apparmor securityfs | |
278 | * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL) | |
279 | */ | |
280 | static void __init aafs_remove_file(struct aa_fs_entry *fs_file) | |
281 | { | |
282 | if (!fs_file->dentry) | |
283 | return; | |
284 | ||
285 | securityfs_remove(fs_file->dentry); | |
286 | fs_file->dentry = NULL; | |
287 | } | |
288 | ||
289 | /** | |
290 | * aafs_remove_dir - recursively drop a directory entry from the securityfs | |
291 | * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL) | |
292 | */ | |
293 | static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir) | |
294 | { | |
295 | struct aa_fs_entry *fs_file; | |
296 | ||
297 | for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) { | |
298 | if (fs_file->v_type == AA_FS_TYPE_DIR) | |
299 | aafs_remove_dir(fs_file); | |
300 | else | |
301 | aafs_remove_file(fs_file); | |
302 | } | |
303 | ||
304 | aafs_remove_file(fs_dir); | |
63e2b423 JJ |
305 | } |
306 | ||
307 | /** | |
308 | * aa_destroy_aafs - cleanup and free aafs | |
309 | * | |
310 | * releases dentries allocated by aa_create_aafs | |
311 | */ | |
312 | void __init aa_destroy_aafs(void) | |
313 | { | |
9acd494b | 314 | aafs_remove_dir(&aa_fs_entry); |
63e2b423 JJ |
315 | } |
316 | ||
317 | /** | |
318 | * aa_create_aafs - create the apparmor security filesystem | |
319 | * | |
320 | * dentries created here are released by aa_destroy_aafs | |
321 | * | |
322 | * Returns: error on failure | |
323 | */ | |
3417d8d5 | 324 | static int __init aa_create_aafs(void) |
63e2b423 JJ |
325 | { |
326 | int error; | |
327 | ||
328 | if (!apparmor_initialized) | |
329 | return 0; | |
330 | ||
9acd494b | 331 | if (aa_fs_entry.dentry) { |
63e2b423 JJ |
332 | AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); |
333 | return -EEXIST; | |
334 | } | |
335 | ||
9acd494b KC |
336 | /* Populate fs tree. */ |
337 | error = aafs_create_dir(&aa_fs_entry, NULL); | |
63e2b423 JJ |
338 | if (error) |
339 | goto error; | |
340 | ||
341 | /* TODO: add support for apparmorfs_null and apparmorfs_mnt */ | |
342 | ||
343 | /* Report that AppArmor fs is enabled */ | |
344 | aa_info_message("AppArmor Filesystem Enabled"); | |
345 | return 0; | |
346 | ||
347 | error: | |
348 | aa_destroy_aafs(); | |
349 | AA_ERROR("Error creating AppArmor securityfs\n"); | |
350 | return error; | |
351 | } | |
352 | ||
353 | fs_initcall(aa_create_aafs); |