]>
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 | ||
0d259f04 | 15 | #include <linux/ctype.h> |
63e2b423 JJ |
16 | #include <linux/security.h> |
17 | #include <linux/vmalloc.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/seq_file.h> | |
20 | #include <linux/uaccess.h> | |
a71ada30 | 21 | #include <linux/mount.h> |
63e2b423 | 22 | #include <linux/namei.h> |
e74abcf3 | 23 | #include <linux/capability.h> |
29b3822f | 24 | #include <linux/rcupdate.h> |
a71ada30 JJ |
25 | #include <uapi/linux/major.h> |
26 | #include <linux/fs.h> | |
63e2b423 JJ |
27 | |
28 | #include "include/apparmor.h" | |
29 | #include "include/apparmorfs.h" | |
30 | #include "include/audit.h" | |
31 | #include "include/context.h" | |
f8eb8a13 | 32 | #include "include/crypto.h" |
63e2b423 | 33 | #include "include/policy.h" |
cff281f6 | 34 | #include "include/policy_ns.h" |
d384b0a1 | 35 | #include "include/resource.h" |
5ac8c355 | 36 | #include "include/policy_unpack.h" |
63e2b423 | 37 | |
0d259f04 JJ |
38 | /** |
39 | * aa_mangle_name - mangle a profile name to std profile layout form | |
40 | * @name: profile name to mangle (NOT NULL) | |
41 | * @target: buffer to store mangled name, same length as @name (MAYBE NULL) | |
42 | * | |
43 | * Returns: length of mangled name | |
44 | */ | |
bbe4a7c8 | 45 | static int mangle_name(const char *name, char *target) |
0d259f04 JJ |
46 | { |
47 | char *t = target; | |
48 | ||
49 | while (*name == '/' || *name == '.') | |
50 | name++; | |
51 | ||
52 | if (target) { | |
53 | for (; *name; name++) { | |
54 | if (*name == '/') | |
55 | *(t)++ = '.'; | |
56 | else if (isspace(*name)) | |
57 | *(t)++ = '_'; | |
58 | else if (isalnum(*name) || strchr("._-", *name)) | |
59 | *(t)++ = *name; | |
60 | } | |
61 | ||
62 | *t = 0; | |
63 | } else { | |
64 | int len = 0; | |
65 | for (; *name; name++) { | |
66 | if (isalnum(*name) || isspace(*name) || | |
67 | strchr("/._-", *name)) | |
68 | len++; | |
69 | } | |
70 | ||
71 | return len; | |
72 | } | |
73 | ||
74 | return t - target; | |
75 | } | |
76 | ||
63e2b423 JJ |
77 | /** |
78 | * aa_simple_write_to_buffer - common routine for getting policy from user | |
63e2b423 | 79 | * @userbuf: user buffer to copy data from (NOT NULL) |
3ed02ada | 80 | * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) |
63e2b423 JJ |
81 | * @copy_size: size of data to copy from user buffer |
82 | * @pos: position write is at in the file (NOT NULL) | |
83 | * | |
84 | * Returns: kernel buffer containing copy of user buffer data or an | |
85 | * ERR_PTR on failure. | |
86 | */ | |
5ef50d01 | 87 | static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, |
5ac8c355 JJ |
88 | size_t alloc_size, |
89 | size_t copy_size, | |
90 | loff_t *pos) | |
63e2b423 | 91 | { |
5ac8c355 | 92 | struct aa_loaddata *data; |
63e2b423 | 93 | |
e6bfa25d | 94 | AA_BUG(copy_size > alloc_size); |
3ed02ada | 95 | |
63e2b423 JJ |
96 | if (*pos != 0) |
97 | /* only writes from pos 0, that is complete writes */ | |
98 | return ERR_PTR(-ESPIPE); | |
99 | ||
63e2b423 | 100 | /* freed by caller to simple_write_to_buffer */ |
a7c3e901 | 101 | data = kvmalloc(sizeof(*data) + alloc_size, GFP_KERNEL); |
63e2b423 JJ |
102 | if (data == NULL) |
103 | return ERR_PTR(-ENOMEM); | |
5ac8c355 JJ |
104 | kref_init(&data->count); |
105 | data->size = copy_size; | |
106 | data->hash = NULL; | |
107 | data->abi = 0; | |
63e2b423 | 108 | |
5ac8c355 | 109 | if (copy_from_user(data->data, userbuf, copy_size)) { |
63e2b423 JJ |
110 | kvfree(data); |
111 | return ERR_PTR(-EFAULT); | |
112 | } | |
113 | ||
114 | return data; | |
115 | } | |
116 | ||
5ac8c355 | 117 | static ssize_t policy_update(int binop, const char __user *buf, size_t size, |
b7fd2c03 | 118 | loff_t *pos, struct aa_ns *ns) |
63e2b423 | 119 | { |
63e2b423 | 120 | ssize_t error; |
5ac8c355 JJ |
121 | struct aa_loaddata *data; |
122 | struct aa_profile *profile = aa_current_profile(); | |
47f6e5cc | 123 | const char *op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL; |
5ac8c355 JJ |
124 | /* high level check about policy management - fine grained in |
125 | * below after unpack | |
126 | */ | |
b7fd2c03 | 127 | error = aa_may_manage_policy(profile, ns, op); |
5ac8c355 JJ |
128 | if (error) |
129 | return error; | |
63e2b423 | 130 | |
5ef50d01 | 131 | data = aa_simple_write_to_buffer(buf, size, size, pos); |
63e2b423 JJ |
132 | error = PTR_ERR(data); |
133 | if (!IS_ERR(data)) { | |
b7fd2c03 JJ |
134 | error = aa_replace_profiles(ns ? ns : profile->ns, profile, |
135 | binop, data); | |
5ac8c355 | 136 | aa_put_loaddata(data); |
63e2b423 JJ |
137 | } |
138 | ||
139 | return error; | |
140 | } | |
141 | ||
b7fd2c03 | 142 | /* .load file hook fn to load policy */ |
5ac8c355 JJ |
143 | static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, |
144 | loff_t *pos) | |
145 | { | |
b7fd2c03 JJ |
146 | struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); |
147 | int error = policy_update(PROF_ADD, buf, size, pos, ns); | |
148 | ||
149 | aa_put_ns(ns); | |
5ac8c355 JJ |
150 | |
151 | return error; | |
152 | } | |
153 | ||
63e2b423 | 154 | static const struct file_operations aa_fs_profile_load = { |
6038f373 AB |
155 | .write = profile_load, |
156 | .llseek = default_llseek, | |
63e2b423 JJ |
157 | }; |
158 | ||
159 | /* .replace file hook fn to load and/or replace policy */ | |
160 | static ssize_t profile_replace(struct file *f, const char __user *buf, | |
161 | size_t size, loff_t *pos) | |
162 | { | |
b7fd2c03 JJ |
163 | struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); |
164 | int error = policy_update(PROF_REPLACE, buf, size, pos, ns); | |
165 | ||
166 | aa_put_ns(ns); | |
63e2b423 JJ |
167 | |
168 | return error; | |
169 | } | |
170 | ||
171 | static const struct file_operations aa_fs_profile_replace = { | |
6038f373 AB |
172 | .write = profile_replace, |
173 | .llseek = default_llseek, | |
63e2b423 JJ |
174 | }; |
175 | ||
b7fd2c03 | 176 | /* .remove file hook fn to remove loaded policy */ |
63e2b423 JJ |
177 | static ssize_t profile_remove(struct file *f, const char __user *buf, |
178 | size_t size, loff_t *pos) | |
179 | { | |
5ac8c355 JJ |
180 | struct aa_loaddata *data; |
181 | struct aa_profile *profile; | |
63e2b423 | 182 | ssize_t error; |
b7fd2c03 | 183 | struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); |
63e2b423 | 184 | |
5ac8c355 JJ |
185 | profile = aa_current_profile(); |
186 | /* high level check about policy management - fine grained in | |
187 | * below after unpack | |
188 | */ | |
b7fd2c03 | 189 | error = aa_may_manage_policy(profile, ns, OP_PROF_RM); |
5ac8c355 JJ |
190 | if (error) |
191 | goto out; | |
192 | ||
63e2b423 JJ |
193 | /* |
194 | * aa_remove_profile needs a null terminated string so 1 extra | |
195 | * byte is allocated and the copied data is null terminated. | |
196 | */ | |
5ef50d01 | 197 | data = aa_simple_write_to_buffer(buf, size + 1, size, pos); |
63e2b423 JJ |
198 | |
199 | error = PTR_ERR(data); | |
200 | if (!IS_ERR(data)) { | |
5ac8c355 | 201 | data->data[size] = 0; |
b7fd2c03 JJ |
202 | error = aa_remove_profiles(ns ? ns : profile->ns, profile, |
203 | data->data, size); | |
5ac8c355 | 204 | aa_put_loaddata(data); |
63e2b423 | 205 | } |
5ac8c355 | 206 | out: |
b7fd2c03 | 207 | aa_put_ns(ns); |
63e2b423 JJ |
208 | return error; |
209 | } | |
210 | ||
211 | static const struct file_operations aa_fs_profile_remove = { | |
6038f373 AB |
212 | .write = profile_remove, |
213 | .llseek = default_llseek, | |
63e2b423 JJ |
214 | }; |
215 | ||
e025be0f WH |
216 | /** |
217 | * query_data - queries a policy and writes its data to buf | |
218 | * @buf: the resulting data is stored here (NOT NULL) | |
219 | * @buf_len: size of buf | |
220 | * @query: query string used to retrieve data | |
221 | * @query_len: size of query including second NUL byte | |
222 | * | |
223 | * The buffers pointed to by buf and query may overlap. The query buffer is | |
224 | * parsed before buf is written to. | |
225 | * | |
226 | * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of | |
227 | * the security confinement context and <KEY> is the name of the data to | |
228 | * retrieve. <LABEL> and <KEY> must not be NUL-terminated. | |
229 | * | |
230 | * Don't expect the contents of buf to be preserved on failure. | |
231 | * | |
232 | * Returns: number of characters written to buf or -errno on failure | |
233 | */ | |
234 | static ssize_t query_data(char *buf, size_t buf_len, | |
235 | char *query, size_t query_len) | |
236 | { | |
237 | char *out; | |
238 | const char *key; | |
239 | struct aa_profile *profile; | |
240 | struct aa_data *data; | |
241 | u32 bytes, blocks; | |
242 | __le32 outle32; | |
243 | ||
244 | if (!query_len) | |
245 | return -EINVAL; /* need a query */ | |
246 | ||
247 | key = query + strnlen(query, query_len) + 1; | |
248 | if (key + 1 >= query + query_len) | |
249 | return -EINVAL; /* not enough space for a non-empty key */ | |
250 | if (key + strnlen(key, query + query_len - key) >= query + query_len) | |
251 | return -EINVAL; /* must end with NUL */ | |
252 | ||
253 | if (buf_len < sizeof(bytes) + sizeof(blocks)) | |
254 | return -EINVAL; /* not enough space */ | |
255 | ||
256 | profile = aa_current_profile(); | |
257 | ||
258 | /* We are going to leave space for two numbers. The first is the total | |
259 | * number of bytes we are writing after the first number. This is so | |
260 | * users can read the full output without reallocation. | |
261 | * | |
262 | * The second number is the number of data blocks we're writing. An | |
263 | * application might be confined by multiple policies having data in | |
264 | * the same key. | |
265 | */ | |
266 | memset(buf, 0, sizeof(bytes) + sizeof(blocks)); | |
267 | out = buf + sizeof(bytes) + sizeof(blocks); | |
268 | ||
269 | blocks = 0; | |
270 | if (profile->data) { | |
271 | data = rhashtable_lookup_fast(profile->data, &key, | |
272 | profile->data->p); | |
273 | ||
274 | if (data) { | |
275 | if (out + sizeof(outle32) + data->size > buf + buf_len) | |
276 | return -EINVAL; /* not enough space */ | |
277 | outle32 = __cpu_to_le32(data->size); | |
278 | memcpy(out, &outle32, sizeof(outle32)); | |
279 | out += sizeof(outle32); | |
280 | memcpy(out, data->data, data->size); | |
281 | out += data->size; | |
282 | blocks++; | |
283 | } | |
284 | } | |
285 | ||
286 | outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); | |
287 | memcpy(buf, &outle32, sizeof(outle32)); | |
288 | outle32 = __cpu_to_le32(blocks); | |
289 | memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32)); | |
290 | ||
291 | return out - buf; | |
292 | } | |
293 | ||
294 | #define QUERY_CMD_DATA "data\0" | |
295 | #define QUERY_CMD_DATA_LEN 5 | |
296 | ||
297 | /** | |
298 | * aa_write_access - generic permissions and data query | |
299 | * @file: pointer to open apparmorfs/access file | |
300 | * @ubuf: user buffer containing the complete query string (NOT NULL) | |
301 | * @count: size of ubuf | |
302 | * @ppos: position in the file (MUST BE ZERO) | |
303 | * | |
304 | * Allows for one permissions or data query per open(), write(), and read() | |
305 | * sequence. The only queries currently supported are label-based queries for | |
306 | * permissions or data. | |
307 | * | |
308 | * For permissions queries, ubuf must begin with "label\0", followed by the | |
309 | * profile query specific format described in the query_label() function | |
310 | * documentation. | |
311 | * | |
312 | * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where | |
313 | * <LABEL> is the name of the security confinement context and <KEY> is the | |
314 | * name of the data to retrieve. | |
315 | * | |
316 | * Returns: number of bytes written or -errno on failure | |
317 | */ | |
318 | static ssize_t aa_write_access(struct file *file, const char __user *ubuf, | |
319 | size_t count, loff_t *ppos) | |
320 | { | |
321 | char *buf; | |
322 | ssize_t len; | |
323 | ||
324 | if (*ppos) | |
325 | return -ESPIPE; | |
326 | ||
327 | buf = simple_transaction_get(file, ubuf, count); | |
328 | if (IS_ERR(buf)) | |
329 | return PTR_ERR(buf); | |
330 | ||
331 | if (count > QUERY_CMD_DATA_LEN && | |
332 | !memcmp(buf, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { | |
333 | len = query_data(buf, SIMPLE_TRANSACTION_LIMIT, | |
334 | buf + QUERY_CMD_DATA_LEN, | |
335 | count - QUERY_CMD_DATA_LEN); | |
336 | } else | |
337 | len = -EINVAL; | |
338 | ||
339 | if (len < 0) | |
340 | return len; | |
341 | ||
342 | simple_transaction_set(file, len); | |
343 | ||
344 | return count; | |
345 | } | |
346 | ||
347 | static const struct file_operations aa_fs_access = { | |
348 | .write = aa_write_access, | |
349 | .read = simple_transaction_read, | |
350 | .release = simple_transaction_release, | |
351 | .llseek = generic_file_llseek, | |
352 | }; | |
353 | ||
e74abcf3 KC |
354 | static int aa_fs_seq_show(struct seq_file *seq, void *v) |
355 | { | |
356 | struct aa_fs_entry *fs_file = seq->private; | |
357 | ||
358 | if (!fs_file) | |
359 | return 0; | |
360 | ||
361 | switch (fs_file->v_type) { | |
362 | case AA_FS_TYPE_BOOLEAN: | |
363 | seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); | |
364 | break; | |
a9bf8e9f KC |
365 | case AA_FS_TYPE_STRING: |
366 | seq_printf(seq, "%s\n", fs_file->v.string); | |
367 | break; | |
e74abcf3 KC |
368 | case AA_FS_TYPE_U64: |
369 | seq_printf(seq, "%#08lx\n", fs_file->v.u64); | |
370 | break; | |
371 | default: | |
372 | /* Ignore unpritable entry types. */ | |
373 | break; | |
374 | } | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
379 | static int aa_fs_seq_open(struct inode *inode, struct file *file) | |
380 | { | |
381 | return single_open(file, aa_fs_seq_show, inode->i_private); | |
382 | } | |
383 | ||
384 | const struct file_operations aa_fs_seq_file_ops = { | |
385 | .owner = THIS_MODULE, | |
386 | .open = aa_fs_seq_open, | |
387 | .read = seq_read, | |
388 | .llseek = seq_lseek, | |
389 | .release = single_release, | |
390 | }; | |
391 | ||
0d259f04 JJ |
392 | static int aa_fs_seq_profile_open(struct inode *inode, struct file *file, |
393 | int (*show)(struct seq_file *, void *)) | |
394 | { | |
8399588a JJ |
395 | struct aa_proxy *proxy = aa_get_proxy(inode->i_private); |
396 | int error = single_open(file, show, proxy); | |
0d259f04 JJ |
397 | |
398 | if (error) { | |
399 | file->private_data = NULL; | |
8399588a | 400 | aa_put_proxy(proxy); |
0d259f04 JJ |
401 | } |
402 | ||
403 | return error; | |
404 | } | |
405 | ||
406 | static int aa_fs_seq_profile_release(struct inode *inode, struct file *file) | |
407 | { | |
408 | struct seq_file *seq = (struct seq_file *) file->private_data; | |
409 | if (seq) | |
8399588a | 410 | aa_put_proxy(seq->private); |
0d259f04 JJ |
411 | return single_release(inode, file); |
412 | } | |
413 | ||
414 | static int aa_fs_seq_profname_show(struct seq_file *seq, void *v) | |
415 | { | |
8399588a JJ |
416 | struct aa_proxy *proxy = seq->private; |
417 | struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); | |
0d259f04 JJ |
418 | seq_printf(seq, "%s\n", profile->base.name); |
419 | aa_put_profile(profile); | |
420 | ||
421 | return 0; | |
422 | } | |
423 | ||
424 | static int aa_fs_seq_profname_open(struct inode *inode, struct file *file) | |
425 | { | |
426 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show); | |
427 | } | |
428 | ||
429 | static const struct file_operations aa_fs_profname_fops = { | |
430 | .owner = THIS_MODULE, | |
431 | .open = aa_fs_seq_profname_open, | |
432 | .read = seq_read, | |
433 | .llseek = seq_lseek, | |
434 | .release = aa_fs_seq_profile_release, | |
435 | }; | |
436 | ||
437 | static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v) | |
438 | { | |
8399588a JJ |
439 | struct aa_proxy *proxy = seq->private; |
440 | struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); | |
0d259f04 JJ |
441 | seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); |
442 | aa_put_profile(profile); | |
443 | ||
444 | return 0; | |
445 | } | |
446 | ||
447 | static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file) | |
448 | { | |
449 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show); | |
450 | } | |
451 | ||
452 | static const struct file_operations aa_fs_profmode_fops = { | |
453 | .owner = THIS_MODULE, | |
454 | .open = aa_fs_seq_profmode_open, | |
455 | .read = seq_read, | |
456 | .llseek = seq_lseek, | |
457 | .release = aa_fs_seq_profile_release, | |
458 | }; | |
459 | ||
556d0be7 JJ |
460 | static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v) |
461 | { | |
8399588a JJ |
462 | struct aa_proxy *proxy = seq->private; |
463 | struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); | |
556d0be7 JJ |
464 | if (profile->attach) |
465 | seq_printf(seq, "%s\n", profile->attach); | |
466 | else if (profile->xmatch) | |
467 | seq_puts(seq, "<unknown>\n"); | |
468 | else | |
469 | seq_printf(seq, "%s\n", profile->base.name); | |
470 | aa_put_profile(profile); | |
471 | ||
472 | return 0; | |
473 | } | |
474 | ||
475 | static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file) | |
476 | { | |
477 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show); | |
478 | } | |
479 | ||
480 | static const struct file_operations aa_fs_profattach_fops = { | |
481 | .owner = THIS_MODULE, | |
482 | .open = aa_fs_seq_profattach_open, | |
483 | .read = seq_read, | |
484 | .llseek = seq_lseek, | |
485 | .release = aa_fs_seq_profile_release, | |
486 | }; | |
487 | ||
f8eb8a13 JJ |
488 | static int aa_fs_seq_hash_show(struct seq_file *seq, void *v) |
489 | { | |
8399588a JJ |
490 | struct aa_proxy *proxy = seq->private; |
491 | struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); | |
f8eb8a13 JJ |
492 | unsigned int i, size = aa_hash_size(); |
493 | ||
494 | if (profile->hash) { | |
495 | for (i = 0; i < size; i++) | |
496 | seq_printf(seq, "%.2x", profile->hash[i]); | |
497 | seq_puts(seq, "\n"); | |
498 | } | |
0b938a2e | 499 | aa_put_profile(profile); |
f8eb8a13 JJ |
500 | |
501 | return 0; | |
502 | } | |
503 | ||
504 | static int aa_fs_seq_hash_open(struct inode *inode, struct file *file) | |
505 | { | |
506 | return single_open(file, aa_fs_seq_hash_show, inode->i_private); | |
507 | } | |
508 | ||
509 | static const struct file_operations aa_fs_seq_hash_fops = { | |
510 | .owner = THIS_MODULE, | |
511 | .open = aa_fs_seq_hash_open, | |
512 | .read = seq_read, | |
513 | .llseek = seq_lseek, | |
514 | .release = single_release, | |
515 | }; | |
516 | ||
3e3e5695 | 517 | |
a71ada30 JJ |
518 | static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v) |
519 | { | |
520 | struct aa_ns *ns = aa_current_profile()->ns; | |
521 | ||
522 | seq_printf(seq, "%d\n", ns->level); | |
523 | ||
524 | return 0; | |
525 | } | |
526 | ||
527 | static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file) | |
528 | { | |
529 | return single_open(file, aa_fs_seq_show_ns_level, inode->i_private); | |
530 | } | |
531 | ||
532 | static const struct file_operations aa_fs_ns_level = { | |
533 | .owner = THIS_MODULE, | |
534 | .open = aa_fs_seq_open_ns_level, | |
535 | .read = seq_read, | |
536 | .llseek = seq_lseek, | |
537 | .release = single_release, | |
538 | }; | |
539 | ||
3e3e5695 JJ |
540 | static int aa_fs_seq_show_ns_name(struct seq_file *seq, void *v) |
541 | { | |
542 | struct aa_ns *ns = aa_current_profile()->ns; | |
543 | ||
544 | seq_printf(seq, "%s\n", ns->base.name); | |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
549 | static int aa_fs_seq_open_ns_name(struct inode *inode, struct file *file) | |
550 | { | |
551 | return single_open(file, aa_fs_seq_show_ns_name, inode->i_private); | |
552 | } | |
553 | ||
554 | static const struct file_operations aa_fs_ns_name = { | |
555 | .owner = THIS_MODULE, | |
556 | .open = aa_fs_seq_open_ns_name, | |
557 | .read = seq_read, | |
558 | .llseek = seq_lseek, | |
559 | .release = single_release, | |
560 | }; | |
561 | ||
5ac8c355 JJ |
562 | static int rawdata_release(struct inode *inode, struct file *file) |
563 | { | |
564 | /* TODO: switch to loaddata when profile switched to symlink */ | |
565 | aa_put_loaddata(file->private_data); | |
566 | ||
567 | return 0; | |
568 | } | |
569 | ||
570 | static int aa_fs_seq_raw_abi_show(struct seq_file *seq, void *v) | |
571 | { | |
572 | struct aa_proxy *proxy = seq->private; | |
573 | struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); | |
574 | ||
0ff3d97f ME |
575 | if (profile->rawdata->abi) |
576 | seq_printf(seq, "v%d\n", profile->rawdata->abi); | |
577 | ||
5ac8c355 JJ |
578 | aa_put_profile(profile); |
579 | ||
580 | return 0; | |
581 | } | |
582 | ||
583 | static int aa_fs_seq_raw_abi_open(struct inode *inode, struct file *file) | |
584 | { | |
585 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_abi_show); | |
586 | } | |
587 | ||
588 | static const struct file_operations aa_fs_seq_raw_abi_fops = { | |
589 | .owner = THIS_MODULE, | |
590 | .open = aa_fs_seq_raw_abi_open, | |
591 | .read = seq_read, | |
592 | .llseek = seq_lseek, | |
593 | .release = aa_fs_seq_profile_release, | |
594 | }; | |
595 | ||
596 | static int aa_fs_seq_raw_hash_show(struct seq_file *seq, void *v) | |
597 | { | |
598 | struct aa_proxy *proxy = seq->private; | |
599 | struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); | |
600 | unsigned int i, size = aa_hash_size(); | |
601 | ||
602 | if (profile->rawdata->hash) { | |
603 | for (i = 0; i < size; i++) | |
604 | seq_printf(seq, "%.2x", profile->rawdata->hash[i]); | |
605 | seq_puts(seq, "\n"); | |
606 | } | |
607 | aa_put_profile(profile); | |
608 | ||
609 | return 0; | |
610 | } | |
611 | ||
612 | static int aa_fs_seq_raw_hash_open(struct inode *inode, struct file *file) | |
613 | { | |
614 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_hash_show); | |
615 | } | |
616 | ||
617 | static const struct file_operations aa_fs_seq_raw_hash_fops = { | |
618 | .owner = THIS_MODULE, | |
619 | .open = aa_fs_seq_raw_hash_open, | |
620 | .read = seq_read, | |
621 | .llseek = seq_lseek, | |
622 | .release = aa_fs_seq_profile_release, | |
623 | }; | |
624 | ||
625 | static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, | |
626 | loff_t *ppos) | |
627 | { | |
628 | struct aa_loaddata *rawdata = file->private_data; | |
629 | ||
630 | return simple_read_from_buffer(buf, size, ppos, rawdata->data, | |
631 | rawdata->size); | |
632 | } | |
633 | ||
634 | static int rawdata_open(struct inode *inode, struct file *file) | |
635 | { | |
636 | struct aa_proxy *proxy = inode->i_private; | |
637 | struct aa_profile *profile; | |
638 | ||
639 | if (!policy_view_capable(NULL)) | |
640 | return -EACCES; | |
641 | profile = aa_get_profile_rcu(&proxy->profile); | |
642 | file->private_data = aa_get_loaddata(profile->rawdata); | |
643 | aa_put_profile(profile); | |
644 | ||
645 | return 0; | |
646 | } | |
647 | ||
648 | static const struct file_operations aa_fs_rawdata_fops = { | |
649 | .open = rawdata_open, | |
650 | .read = rawdata_read, | |
651 | .llseek = generic_file_llseek, | |
652 | .release = rawdata_release, | |
653 | }; | |
654 | ||
0d259f04 JJ |
655 | /** fns to setup dynamic per profile/namespace files **/ |
656 | void __aa_fs_profile_rmdir(struct aa_profile *profile) | |
657 | { | |
658 | struct aa_profile *child; | |
659 | int i; | |
660 | ||
661 | if (!profile) | |
662 | return; | |
663 | ||
664 | list_for_each_entry(child, &profile->base.profiles, base.list) | |
665 | __aa_fs_profile_rmdir(child); | |
666 | ||
667 | for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { | |
8399588a | 668 | struct aa_proxy *proxy; |
0d259f04 JJ |
669 | if (!profile->dents[i]) |
670 | continue; | |
671 | ||
8399588a | 672 | proxy = d_inode(profile->dents[i])->i_private; |
0d259f04 | 673 | securityfs_remove(profile->dents[i]); |
8399588a | 674 | aa_put_proxy(proxy); |
0d259f04 JJ |
675 | profile->dents[i] = NULL; |
676 | } | |
677 | } | |
678 | ||
679 | void __aa_fs_profile_migrate_dents(struct aa_profile *old, | |
680 | struct aa_profile *new) | |
681 | { | |
682 | int i; | |
683 | ||
684 | for (i = 0; i < AAFS_PROF_SIZEOF; i++) { | |
685 | new->dents[i] = old->dents[i]; | |
d671e890 | 686 | if (new->dents[i]) |
078cd827 | 687 | new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); |
0d259f04 JJ |
688 | old->dents[i] = NULL; |
689 | } | |
690 | } | |
691 | ||
692 | static struct dentry *create_profile_file(struct dentry *dir, const char *name, | |
693 | struct aa_profile *profile, | |
694 | const struct file_operations *fops) | |
695 | { | |
8399588a | 696 | struct aa_proxy *proxy = aa_get_proxy(profile->proxy); |
0d259f04 JJ |
697 | struct dentry *dent; |
698 | ||
8399588a | 699 | dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops); |
0d259f04 | 700 | if (IS_ERR(dent)) |
8399588a | 701 | aa_put_proxy(proxy); |
0d259f04 JJ |
702 | |
703 | return dent; | |
704 | } | |
705 | ||
706 | /* requires lock be held */ | |
707 | int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) | |
708 | { | |
709 | struct aa_profile *child; | |
710 | struct dentry *dent = NULL, *dir; | |
711 | int error; | |
712 | ||
713 | if (!parent) { | |
714 | struct aa_profile *p; | |
715 | p = aa_deref_parent(profile); | |
716 | dent = prof_dir(p); | |
717 | /* adding to parent that previously didn't have children */ | |
718 | dent = securityfs_create_dir("profiles", dent); | |
719 | if (IS_ERR(dent)) | |
720 | goto fail; | |
721 | prof_child_dir(p) = parent = dent; | |
722 | } | |
723 | ||
724 | if (!profile->dirname) { | |
725 | int len, id_len; | |
726 | len = mangle_name(profile->base.name, NULL); | |
727 | id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); | |
728 | ||
729 | profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); | |
730 | if (!profile->dirname) | |
731 | goto fail; | |
732 | ||
733 | mangle_name(profile->base.name, profile->dirname); | |
734 | sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); | |
735 | } | |
736 | ||
737 | dent = securityfs_create_dir(profile->dirname, parent); | |
738 | if (IS_ERR(dent)) | |
739 | goto fail; | |
740 | prof_dir(profile) = dir = dent; | |
741 | ||
742 | dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops); | |
743 | if (IS_ERR(dent)) | |
744 | goto fail; | |
745 | profile->dents[AAFS_PROF_NAME] = dent; | |
746 | ||
747 | dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops); | |
748 | if (IS_ERR(dent)) | |
749 | goto fail; | |
750 | profile->dents[AAFS_PROF_MODE] = dent; | |
751 | ||
556d0be7 JJ |
752 | dent = create_profile_file(dir, "attach", profile, |
753 | &aa_fs_profattach_fops); | |
754 | if (IS_ERR(dent)) | |
755 | goto fail; | |
756 | profile->dents[AAFS_PROF_ATTACH] = dent; | |
757 | ||
f8eb8a13 JJ |
758 | if (profile->hash) { |
759 | dent = create_profile_file(dir, "sha1", profile, | |
760 | &aa_fs_seq_hash_fops); | |
761 | if (IS_ERR(dent)) | |
762 | goto fail; | |
763 | profile->dents[AAFS_PROF_HASH] = dent; | |
764 | } | |
765 | ||
5ac8c355 JJ |
766 | if (profile->rawdata) { |
767 | dent = create_profile_file(dir, "raw_sha1", profile, | |
768 | &aa_fs_seq_raw_hash_fops); | |
769 | if (IS_ERR(dent)) | |
770 | goto fail; | |
771 | profile->dents[AAFS_PROF_RAW_HASH] = dent; | |
772 | ||
773 | dent = create_profile_file(dir, "raw_abi", profile, | |
774 | &aa_fs_seq_raw_abi_fops); | |
775 | if (IS_ERR(dent)) | |
776 | goto fail; | |
777 | profile->dents[AAFS_PROF_RAW_ABI] = dent; | |
778 | ||
779 | dent = securityfs_create_file("raw_data", S_IFREG | 0444, dir, | |
780 | profile->proxy, | |
781 | &aa_fs_rawdata_fops); | |
782 | if (IS_ERR(dent)) | |
783 | goto fail; | |
784 | profile->dents[AAFS_PROF_RAW_DATA] = dent; | |
785 | d_inode(dent)->i_size = profile->rawdata->size; | |
786 | aa_get_proxy(profile->proxy); | |
787 | } | |
788 | ||
0d259f04 JJ |
789 | list_for_each_entry(child, &profile->base.profiles, base.list) { |
790 | error = __aa_fs_profile_mkdir(child, prof_child_dir(profile)); | |
791 | if (error) | |
792 | goto fail2; | |
793 | } | |
794 | ||
795 | return 0; | |
796 | ||
797 | fail: | |
798 | error = PTR_ERR(dent); | |
799 | ||
800 | fail2: | |
801 | __aa_fs_profile_rmdir(profile); | |
802 | ||
803 | return error; | |
804 | } | |
805 | ||
98849dff | 806 | void __aa_fs_ns_rmdir(struct aa_ns *ns) |
0d259f04 | 807 | { |
98849dff | 808 | struct aa_ns *sub; |
0d259f04 JJ |
809 | struct aa_profile *child; |
810 | int i; | |
811 | ||
812 | if (!ns) | |
813 | return; | |
814 | ||
815 | list_for_each_entry(child, &ns->base.profiles, base.list) | |
816 | __aa_fs_profile_rmdir(child); | |
817 | ||
818 | list_for_each_entry(sub, &ns->sub_ns, base.list) { | |
819 | mutex_lock(&sub->lock); | |
98849dff | 820 | __aa_fs_ns_rmdir(sub); |
0d259f04 JJ |
821 | mutex_unlock(&sub->lock); |
822 | } | |
823 | ||
b7fd2c03 JJ |
824 | if (ns_subns_dir(ns)) { |
825 | sub = d_inode(ns_subns_dir(ns))->i_private; | |
826 | aa_put_ns(sub); | |
827 | } | |
828 | if (ns_subload(ns)) { | |
829 | sub = d_inode(ns_subload(ns))->i_private; | |
830 | aa_put_ns(sub); | |
831 | } | |
832 | if (ns_subreplace(ns)) { | |
833 | sub = d_inode(ns_subreplace(ns))->i_private; | |
834 | aa_put_ns(sub); | |
835 | } | |
836 | if (ns_subremove(ns)) { | |
837 | sub = d_inode(ns_subremove(ns))->i_private; | |
838 | aa_put_ns(sub); | |
839 | } | |
840 | ||
0d259f04 JJ |
841 | for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { |
842 | securityfs_remove(ns->dents[i]); | |
843 | ns->dents[i] = NULL; | |
844 | } | |
845 | } | |
846 | ||
b7fd2c03 JJ |
847 | /* assumes cleanup in caller */ |
848 | static int __aa_fs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) | |
849 | { | |
850 | struct dentry *dent; | |
851 | ||
852 | AA_BUG(!ns); | |
853 | AA_BUG(!dir); | |
854 | ||
855 | dent = securityfs_create_dir("profiles", dir); | |
856 | if (IS_ERR(dent)) | |
857 | return PTR_ERR(dent); | |
858 | ns_subprofs_dir(ns) = dent; | |
859 | ||
860 | dent = securityfs_create_dir("raw_data", dir); | |
861 | if (IS_ERR(dent)) | |
862 | return PTR_ERR(dent); | |
863 | ns_subdata_dir(ns) = dent; | |
864 | ||
865 | dent = securityfs_create_file(".load", 0640, dir, ns, | |
866 | &aa_fs_profile_load); | |
867 | if (IS_ERR(dent)) | |
868 | return PTR_ERR(dent); | |
869 | aa_get_ns(ns); | |
870 | ns_subload(ns) = dent; | |
871 | ||
872 | dent = securityfs_create_file(".replace", 0640, dir, ns, | |
873 | &aa_fs_profile_replace); | |
874 | if (IS_ERR(dent)) | |
875 | return PTR_ERR(dent); | |
876 | aa_get_ns(ns); | |
877 | ns_subreplace(ns) = dent; | |
878 | ||
879 | dent = securityfs_create_file(".remove", 0640, dir, ns, | |
880 | &aa_fs_profile_remove); | |
881 | if (IS_ERR(dent)) | |
882 | return PTR_ERR(dent); | |
883 | aa_get_ns(ns); | |
884 | ns_subremove(ns) = dent; | |
885 | ||
886 | dent = securityfs_create_dir("namespaces", dir); | |
887 | if (IS_ERR(dent)) | |
888 | return PTR_ERR(dent); | |
889 | aa_get_ns(ns); | |
890 | ns_subns_dir(ns) = dent; | |
891 | ||
892 | return 0; | |
893 | } | |
894 | ||
98849dff | 895 | int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name) |
0d259f04 | 896 | { |
98849dff | 897 | struct aa_ns *sub; |
0d259f04 JJ |
898 | struct aa_profile *child; |
899 | struct dentry *dent, *dir; | |
900 | int error; | |
901 | ||
b7fd2c03 JJ |
902 | AA_BUG(!ns); |
903 | AA_BUG(!parent); | |
904 | AA_BUG(!mutex_is_locked(&ns->lock)); | |
905 | ||
0d259f04 JJ |
906 | if (!name) |
907 | name = ns->base.name; | |
908 | ||
b7fd2c03 | 909 | /* create ns dir if it doesn't already exist */ |
0d259f04 JJ |
910 | dent = securityfs_create_dir(name, parent); |
911 | if (IS_ERR(dent)) | |
912 | goto fail; | |
63e2b423 | 913 | |
b7fd2c03 JJ |
914 | ns_dir(ns) = dir = dent; |
915 | error = __aa_fs_ns_mkdir_entries(ns, dir); | |
916 | if (error) | |
917 | goto fail2; | |
0d259f04 | 918 | |
b7fd2c03 | 919 | /* profiles */ |
0d259f04 JJ |
920 | list_for_each_entry(child, &ns->base.profiles, base.list) { |
921 | error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns)); | |
922 | if (error) | |
923 | goto fail2; | |
924 | } | |
925 | ||
b7fd2c03 | 926 | /* subnamespaces */ |
0d259f04 JJ |
927 | list_for_each_entry(sub, &ns->sub_ns, base.list) { |
928 | mutex_lock(&sub->lock); | |
98849dff | 929 | error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL); |
0d259f04 JJ |
930 | mutex_unlock(&sub->lock); |
931 | if (error) | |
932 | goto fail2; | |
933 | } | |
934 | ||
935 | return 0; | |
936 | ||
937 | fail: | |
938 | error = PTR_ERR(dent); | |
939 | ||
940 | fail2: | |
98849dff | 941 | __aa_fs_ns_rmdir(ns); |
0d259f04 JJ |
942 | |
943 | return error; | |
944 | } | |
945 | ||
946 | ||
29b3822f JJ |
947 | #define list_entry_is_head(pos, head, member) (&pos->member == (head)) |
948 | ||
949 | /** | |
98849dff | 950 | * __next_ns - find the next namespace to list |
29b3822f JJ |
951 | * @root: root namespace to stop search at (NOT NULL) |
952 | * @ns: current ns position (NOT NULL) | |
953 | * | |
954 | * Find the next namespace from @ns under @root and handle all locking needed | |
955 | * while switching current namespace. | |
956 | * | |
957 | * Returns: next namespace or NULL if at last namespace under @root | |
958 | * Requires: ns->parent->lock to be held | |
959 | * NOTE: will not unlock root->lock | |
960 | */ | |
98849dff | 961 | static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) |
29b3822f | 962 | { |
98849dff | 963 | struct aa_ns *parent, *next; |
29b3822f JJ |
964 | |
965 | /* is next namespace a child */ | |
966 | if (!list_empty(&ns->sub_ns)) { | |
967 | next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); | |
968 | mutex_lock(&next->lock); | |
969 | return next; | |
970 | } | |
971 | ||
972 | /* check if the next ns is a sibling, parent, gp, .. */ | |
973 | parent = ns->parent; | |
ed2c7da3 | 974 | while (ns != root) { |
29b3822f | 975 | mutex_unlock(&ns->lock); |
38dbd7d8 | 976 | next = list_next_entry(ns, base.list); |
29b3822f JJ |
977 | if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { |
978 | mutex_lock(&next->lock); | |
979 | return next; | |
980 | } | |
29b3822f JJ |
981 | ns = parent; |
982 | parent = parent->parent; | |
983 | } | |
984 | ||
985 | return NULL; | |
986 | } | |
987 | ||
988 | /** | |
989 | * __first_profile - find the first profile in a namespace | |
990 | * @root: namespace that is root of profiles being displayed (NOT NULL) | |
991 | * @ns: namespace to start in (NOT NULL) | |
992 | * | |
993 | * Returns: unrefcounted profile or NULL if no profile | |
994 | * Requires: profile->ns.lock to be held | |
995 | */ | |
98849dff JJ |
996 | static struct aa_profile *__first_profile(struct aa_ns *root, |
997 | struct aa_ns *ns) | |
29b3822f | 998 | { |
98849dff | 999 | for (; ns; ns = __next_ns(root, ns)) { |
29b3822f JJ |
1000 | if (!list_empty(&ns->base.profiles)) |
1001 | return list_first_entry(&ns->base.profiles, | |
1002 | struct aa_profile, base.list); | |
1003 | } | |
1004 | return NULL; | |
1005 | } | |
1006 | ||
1007 | /** | |
1008 | * __next_profile - step to the next profile in a profile tree | |
1009 | * @profile: current profile in tree (NOT NULL) | |
1010 | * | |
1011 | * Perform a depth first traversal on the profile tree in a namespace | |
1012 | * | |
1013 | * Returns: next profile or NULL if done | |
1014 | * Requires: profile->ns.lock to be held | |
1015 | */ | |
1016 | static struct aa_profile *__next_profile(struct aa_profile *p) | |
1017 | { | |
1018 | struct aa_profile *parent; | |
98849dff | 1019 | struct aa_ns *ns = p->ns; |
29b3822f JJ |
1020 | |
1021 | /* is next profile a child */ | |
1022 | if (!list_empty(&p->base.profiles)) | |
1023 | return list_first_entry(&p->base.profiles, typeof(*p), | |
1024 | base.list); | |
1025 | ||
1026 | /* is next profile a sibling, parent sibling, gp, sibling, .. */ | |
1027 | parent = rcu_dereference_protected(p->parent, | |
1028 | mutex_is_locked(&p->ns->lock)); | |
1029 | while (parent) { | |
38dbd7d8 | 1030 | p = list_next_entry(p, base.list); |
29b3822f JJ |
1031 | if (!list_entry_is_head(p, &parent->base.profiles, base.list)) |
1032 | return p; | |
1033 | p = parent; | |
1034 | parent = rcu_dereference_protected(parent->parent, | |
1035 | mutex_is_locked(&parent->ns->lock)); | |
1036 | } | |
1037 | ||
1038 | /* is next another profile in the namespace */ | |
38dbd7d8 | 1039 | p = list_next_entry(p, base.list); |
29b3822f JJ |
1040 | if (!list_entry_is_head(p, &ns->base.profiles, base.list)) |
1041 | return p; | |
1042 | ||
1043 | return NULL; | |
1044 | } | |
1045 | ||
1046 | /** | |
1047 | * next_profile - step to the next profile in where ever it may be | |
1048 | * @root: root namespace (NOT NULL) | |
1049 | * @profile: current profile (NOT NULL) | |
1050 | * | |
1051 | * Returns: next profile or NULL if there isn't one | |
1052 | */ | |
98849dff | 1053 | static struct aa_profile *next_profile(struct aa_ns *root, |
29b3822f JJ |
1054 | struct aa_profile *profile) |
1055 | { | |
1056 | struct aa_profile *next = __next_profile(profile); | |
1057 | if (next) | |
1058 | return next; | |
1059 | ||
1060 | /* finished all profiles in namespace move to next namespace */ | |
98849dff | 1061 | return __first_profile(root, __next_ns(root, profile->ns)); |
29b3822f JJ |
1062 | } |
1063 | ||
1064 | /** | |
1065 | * p_start - start a depth first traversal of profile tree | |
1066 | * @f: seq_file to fill | |
1067 | * @pos: current position | |
1068 | * | |
1069 | * Returns: first profile under current namespace or NULL if none found | |
1070 | * | |
1071 | * acquires first ns->lock | |
1072 | */ | |
1073 | static void *p_start(struct seq_file *f, loff_t *pos) | |
1074 | { | |
1075 | struct aa_profile *profile = NULL; | |
98849dff | 1076 | struct aa_ns *root = aa_current_profile()->ns; |
29b3822f | 1077 | loff_t l = *pos; |
98849dff | 1078 | f->private = aa_get_ns(root); |
29b3822f JJ |
1079 | |
1080 | ||
1081 | /* find the first profile */ | |
1082 | mutex_lock(&root->lock); | |
1083 | profile = __first_profile(root, root); | |
1084 | ||
1085 | /* skip to position */ | |
1086 | for (; profile && l > 0; l--) | |
1087 | profile = next_profile(root, profile); | |
1088 | ||
1089 | return profile; | |
1090 | } | |
1091 | ||
1092 | /** | |
1093 | * p_next - read the next profile entry | |
1094 | * @f: seq_file to fill | |
1095 | * @p: profile previously returned | |
1096 | * @pos: current position | |
1097 | * | |
1098 | * Returns: next profile after @p or NULL if none | |
1099 | * | |
1100 | * may acquire/release locks in namespace tree as necessary | |
1101 | */ | |
1102 | static void *p_next(struct seq_file *f, void *p, loff_t *pos) | |
1103 | { | |
1104 | struct aa_profile *profile = p; | |
98849dff | 1105 | struct aa_ns *ns = f->private; |
29b3822f JJ |
1106 | (*pos)++; |
1107 | ||
1108 | return next_profile(ns, profile); | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * p_stop - stop depth first traversal | |
1113 | * @f: seq_file we are filling | |
1114 | * @p: the last profile writen | |
1115 | * | |
1116 | * Release all locking done by p_start/p_next on namespace tree | |
1117 | */ | |
1118 | static void p_stop(struct seq_file *f, void *p) | |
1119 | { | |
1120 | struct aa_profile *profile = p; | |
98849dff | 1121 | struct aa_ns *root = f->private, *ns; |
29b3822f JJ |
1122 | |
1123 | if (profile) { | |
1124 | for (ns = profile->ns; ns && ns != root; ns = ns->parent) | |
1125 | mutex_unlock(&ns->lock); | |
1126 | } | |
1127 | mutex_unlock(&root->lock); | |
98849dff | 1128 | aa_put_ns(root); |
29b3822f JJ |
1129 | } |
1130 | ||
1131 | /** | |
1132 | * seq_show_profile - show a profile entry | |
1133 | * @f: seq_file to file | |
1134 | * @p: current position (profile) (NOT NULL) | |
1135 | * | |
1136 | * Returns: error on failure | |
1137 | */ | |
1138 | static int seq_show_profile(struct seq_file *f, void *p) | |
1139 | { | |
1140 | struct aa_profile *profile = (struct aa_profile *)p; | |
98849dff | 1141 | struct aa_ns *root = f->private; |
29b3822f JJ |
1142 | |
1143 | if (profile->ns != root) | |
92b6d8ef | 1144 | seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true)); |
29b3822f JJ |
1145 | seq_printf(f, "%s (%s)\n", profile->base.hname, |
1146 | aa_profile_mode_names[profile->mode]); | |
1147 | ||
1148 | return 0; | |
1149 | } | |
1150 | ||
1151 | static const struct seq_operations aa_fs_profiles_op = { | |
1152 | .start = p_start, | |
1153 | .next = p_next, | |
1154 | .stop = p_stop, | |
1155 | .show = seq_show_profile, | |
1156 | }; | |
1157 | ||
1158 | static int profiles_open(struct inode *inode, struct file *file) | |
1159 | { | |
5ac8c355 JJ |
1160 | if (!policy_view_capable(NULL)) |
1161 | return -EACCES; | |
1162 | ||
29b3822f JJ |
1163 | return seq_open(file, &aa_fs_profiles_op); |
1164 | } | |
1165 | ||
1166 | static int profiles_release(struct inode *inode, struct file *file) | |
1167 | { | |
1168 | return seq_release(inode, file); | |
1169 | } | |
1170 | ||
1171 | static const struct file_operations aa_fs_profiles_fops = { | |
1172 | .open = profiles_open, | |
1173 | .read = seq_read, | |
1174 | .llseek = seq_lseek, | |
1175 | .release = profiles_release, | |
1176 | }; | |
1177 | ||
1178 | ||
0d259f04 | 1179 | /** Base file system setup **/ |
a9bf8e9f KC |
1180 | static struct aa_fs_entry aa_fs_entry_file[] = { |
1181 | AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \ | |
1182 | "link lock"), | |
1183 | { } | |
1184 | }; | |
1185 | ||
e74abcf3 KC |
1186 | static struct aa_fs_entry aa_fs_entry_domain[] = { |
1187 | AA_FS_FILE_BOOLEAN("change_hat", 1), | |
1188 | AA_FS_FILE_BOOLEAN("change_hatv", 1), | |
1189 | AA_FS_FILE_BOOLEAN("change_onexec", 1), | |
1190 | AA_FS_FILE_BOOLEAN("change_profile", 1), | |
34c426ac | 1191 | AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), |
aa9a39ad | 1192 | AA_FS_FILE_STRING("version", "1.2"), |
e74abcf3 KC |
1193 | { } |
1194 | }; | |
1195 | ||
474d6b75 JJ |
1196 | static struct aa_fs_entry aa_fs_entry_versions[] = { |
1197 | AA_FS_FILE_BOOLEAN("v5", 1), | |
1198 | { } | |
1199 | }; | |
1200 | ||
9d910a3b | 1201 | static struct aa_fs_entry aa_fs_entry_policy[] = { |
474d6b75 JJ |
1202 | AA_FS_DIR("versions", aa_fs_entry_versions), |
1203 | AA_FS_FILE_BOOLEAN("set_load", 1), | |
1204 | { } | |
9d910a3b JJ |
1205 | }; |
1206 | ||
e74abcf3 | 1207 | static struct aa_fs_entry aa_fs_entry_features[] = { |
9d910a3b | 1208 | AA_FS_DIR("policy", aa_fs_entry_policy), |
e74abcf3 | 1209 | AA_FS_DIR("domain", aa_fs_entry_domain), |
a9bf8e9f | 1210 | AA_FS_DIR("file", aa_fs_entry_file), |
e74abcf3 | 1211 | AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), |
d384b0a1 | 1212 | AA_FS_DIR("rlimit", aa_fs_entry_rlimit), |
84f1f787 | 1213 | AA_FS_DIR("caps", aa_fs_entry_caps), |
e74abcf3 KC |
1214 | { } |
1215 | }; | |
1216 | ||
9acd494b | 1217 | static struct aa_fs_entry aa_fs_entry_apparmor[] = { |
e025be0f | 1218 | AA_FS_FILE_FOPS(".access", 0640, &aa_fs_access), |
a71ada30 | 1219 | AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level), |
3e3e5695 | 1220 | AA_FS_FILE_FOPS(".ns_name", 0640, &aa_fs_ns_name), |
b7fd2c03 | 1221 | AA_FS_FILE_FOPS("profiles", 0440, &aa_fs_profiles_fops), |
e74abcf3 | 1222 | AA_FS_DIR("features", aa_fs_entry_features), |
9acd494b KC |
1223 | { } |
1224 | }; | |
63e2b423 | 1225 | |
9acd494b KC |
1226 | static struct aa_fs_entry aa_fs_entry = |
1227 | AA_FS_DIR("apparmor", aa_fs_entry_apparmor); | |
63e2b423 | 1228 | |
9acd494b KC |
1229 | /** |
1230 | * aafs_create_file - create a file entry in the apparmor securityfs | |
1231 | * @fs_file: aa_fs_entry to build an entry for (NOT NULL) | |
1232 | * @parent: the parent dentry in the securityfs | |
1233 | * | |
1234 | * Use aafs_remove_file to remove entries created with this fn. | |
1235 | */ | |
1236 | static int __init aafs_create_file(struct aa_fs_entry *fs_file, | |
1237 | struct dentry *parent) | |
1238 | { | |
1239 | int error = 0; | |
1240 | ||
1241 | fs_file->dentry = securityfs_create_file(fs_file->name, | |
1242 | S_IFREG | fs_file->mode, | |
1243 | parent, fs_file, | |
1244 | fs_file->file_ops); | |
1245 | if (IS_ERR(fs_file->dentry)) { | |
1246 | error = PTR_ERR(fs_file->dentry); | |
1247 | fs_file->dentry = NULL; | |
63e2b423 | 1248 | } |
9acd494b | 1249 | return error; |
63e2b423 JJ |
1250 | } |
1251 | ||
0d259f04 | 1252 | static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir); |
63e2b423 | 1253 | /** |
9acd494b KC |
1254 | * aafs_create_dir - recursively create a directory entry in the securityfs |
1255 | * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL) | |
1256 | * @parent: the parent dentry in the securityfs | |
63e2b423 | 1257 | * |
9acd494b | 1258 | * Use aafs_remove_dir to remove entries created with this fn. |
63e2b423 | 1259 | */ |
9acd494b KC |
1260 | static int __init aafs_create_dir(struct aa_fs_entry *fs_dir, |
1261 | struct dentry *parent) | |
63e2b423 | 1262 | { |
9acd494b | 1263 | struct aa_fs_entry *fs_file; |
0d259f04 JJ |
1264 | struct dentry *dir; |
1265 | int error; | |
63e2b423 | 1266 | |
0d259f04 JJ |
1267 | dir = securityfs_create_dir(fs_dir->name, parent); |
1268 | if (IS_ERR(dir)) | |
1269 | return PTR_ERR(dir); | |
1270 | fs_dir->dentry = dir; | |
63e2b423 | 1271 | |
0d259f04 | 1272 | for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { |
9acd494b KC |
1273 | if (fs_file->v_type == AA_FS_TYPE_DIR) |
1274 | error = aafs_create_dir(fs_file, fs_dir->dentry); | |
1275 | else | |
1276 | error = aafs_create_file(fs_file, fs_dir->dentry); | |
1277 | if (error) | |
1278 | goto failed; | |
1279 | } | |
1280 | ||
1281 | return 0; | |
1282 | ||
1283 | failed: | |
0d259f04 JJ |
1284 | aafs_remove_dir(fs_dir); |
1285 | ||
9acd494b KC |
1286 | return error; |
1287 | } | |
1288 | ||
1289 | /** | |
1290 | * aafs_remove_file - drop a single file entry in the apparmor securityfs | |
1291 | * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL) | |
1292 | */ | |
1293 | static void __init aafs_remove_file(struct aa_fs_entry *fs_file) | |
1294 | { | |
1295 | if (!fs_file->dentry) | |
1296 | return; | |
1297 | ||
1298 | securityfs_remove(fs_file->dentry); | |
1299 | fs_file->dentry = NULL; | |
1300 | } | |
1301 | ||
1302 | /** | |
1303 | * aafs_remove_dir - recursively drop a directory entry from the securityfs | |
1304 | * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL) | |
1305 | */ | |
1306 | static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir) | |
1307 | { | |
1308 | struct aa_fs_entry *fs_file; | |
1309 | ||
0d259f04 | 1310 | for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { |
9acd494b KC |
1311 | if (fs_file->v_type == AA_FS_TYPE_DIR) |
1312 | aafs_remove_dir(fs_file); | |
1313 | else | |
1314 | aafs_remove_file(fs_file); | |
1315 | } | |
1316 | ||
1317 | aafs_remove_file(fs_dir); | |
63e2b423 JJ |
1318 | } |
1319 | ||
1320 | /** | |
1321 | * aa_destroy_aafs - cleanup and free aafs | |
1322 | * | |
1323 | * releases dentries allocated by aa_create_aafs | |
1324 | */ | |
1325 | void __init aa_destroy_aafs(void) | |
1326 | { | |
9acd494b | 1327 | aafs_remove_dir(&aa_fs_entry); |
63e2b423 JJ |
1328 | } |
1329 | ||
a71ada30 JJ |
1330 | |
1331 | #define NULL_FILE_NAME ".null" | |
1332 | struct path aa_null; | |
1333 | ||
1334 | static int aa_mk_null_file(struct dentry *parent) | |
1335 | { | |
1336 | struct vfsmount *mount = NULL; | |
1337 | struct dentry *dentry; | |
1338 | struct inode *inode; | |
1339 | int count = 0; | |
1340 | int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); | |
1341 | ||
1342 | if (error) | |
1343 | return error; | |
1344 | ||
1345 | inode_lock(d_inode(parent)); | |
1346 | dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); | |
1347 | if (IS_ERR(dentry)) { | |
1348 | error = PTR_ERR(dentry); | |
1349 | goto out; | |
1350 | } | |
1351 | inode = new_inode(parent->d_inode->i_sb); | |
1352 | if (!inode) { | |
1353 | error = -ENOMEM; | |
1354 | goto out1; | |
1355 | } | |
1356 | ||
1357 | inode->i_ino = get_next_ino(); | |
1358 | inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; | |
24d0d03c | 1359 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
a71ada30 JJ |
1360 | init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, |
1361 | MKDEV(MEM_MAJOR, 3)); | |
1362 | d_instantiate(dentry, inode); | |
1363 | aa_null.dentry = dget(dentry); | |
1364 | aa_null.mnt = mntget(mount); | |
1365 | ||
1366 | error = 0; | |
1367 | ||
1368 | out1: | |
1369 | dput(dentry); | |
1370 | out: | |
1371 | inode_unlock(d_inode(parent)); | |
1372 | simple_release_fs(&mount, &count); | |
1373 | return error; | |
1374 | } | |
1375 | ||
63e2b423 JJ |
1376 | /** |
1377 | * aa_create_aafs - create the apparmor security filesystem | |
1378 | * | |
1379 | * dentries created here are released by aa_destroy_aafs | |
1380 | * | |
1381 | * Returns: error on failure | |
1382 | */ | |
3417d8d5 | 1383 | static int __init aa_create_aafs(void) |
63e2b423 | 1384 | { |
b7fd2c03 | 1385 | struct dentry *dent; |
63e2b423 JJ |
1386 | int error; |
1387 | ||
1388 | if (!apparmor_initialized) | |
1389 | return 0; | |
1390 | ||
9acd494b | 1391 | if (aa_fs_entry.dentry) { |
63e2b423 JJ |
1392 | AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); |
1393 | return -EEXIST; | |
1394 | } | |
1395 | ||
9acd494b KC |
1396 | /* Populate fs tree. */ |
1397 | error = aafs_create_dir(&aa_fs_entry, NULL); | |
63e2b423 JJ |
1398 | if (error) |
1399 | goto error; | |
1400 | ||
b7fd2c03 JJ |
1401 | dent = securityfs_create_file(".load", 0666, aa_fs_entry.dentry, |
1402 | NULL, &aa_fs_profile_load); | |
1403 | if (IS_ERR(dent)) { | |
1404 | error = PTR_ERR(dent); | |
1405 | goto error; | |
1406 | } | |
1407 | ns_subload(root_ns) = dent; | |
1408 | ||
1409 | dent = securityfs_create_file(".replace", 0666, aa_fs_entry.dentry, | |
1410 | NULL, &aa_fs_profile_replace); | |
1411 | if (IS_ERR(dent)) { | |
1412 | error = PTR_ERR(dent); | |
1413 | goto error; | |
1414 | } | |
1415 | ns_subreplace(root_ns) = dent; | |
1416 | ||
1417 | dent = securityfs_create_file(".remove", 0666, aa_fs_entry.dentry, | |
1418 | NULL, &aa_fs_profile_remove); | |
1419 | if (IS_ERR(dent)) { | |
1420 | error = PTR_ERR(dent); | |
1421 | goto error; | |
1422 | } | |
1423 | ns_subremove(root_ns) = dent; | |
1424 | ||
1425 | mutex_lock(&root_ns->lock); | |
98849dff | 1426 | error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy"); |
b7fd2c03 JJ |
1427 | mutex_unlock(&root_ns->lock); |
1428 | ||
0d259f04 JJ |
1429 | if (error) |
1430 | goto error; | |
1431 | ||
a71ada30 JJ |
1432 | error = aa_mk_null_file(aa_fs_entry.dentry); |
1433 | if (error) | |
1434 | goto error; | |
1435 | ||
1436 | /* TODO: add default profile to apparmorfs */ | |
63e2b423 JJ |
1437 | |
1438 | /* Report that AppArmor fs is enabled */ | |
1439 | aa_info_message("AppArmor Filesystem Enabled"); | |
1440 | return 0; | |
1441 | ||
1442 | error: | |
1443 | aa_destroy_aafs(); | |
1444 | AA_ERROR("Error creating AppArmor securityfs\n"); | |
1445 | return error; | |
1446 | } | |
1447 | ||
1448 | fs_initcall(aa_create_aafs); |