163e2b423SJohn Johansen /* 263e2b423SJohn Johansen * AppArmor security module 363e2b423SJohn Johansen * 463e2b423SJohn Johansen * This file contains AppArmor /sys/kernel/security/apparmor interface functions 563e2b423SJohn Johansen * 663e2b423SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 763e2b423SJohn Johansen * Copyright 2009-2010 Canonical Ltd. 863e2b423SJohn Johansen * 963e2b423SJohn Johansen * This program is free software; you can redistribute it and/or 1063e2b423SJohn Johansen * modify it under the terms of the GNU General Public License as 1163e2b423SJohn Johansen * published by the Free Software Foundation, version 2 of the 1263e2b423SJohn Johansen * License. 1363e2b423SJohn Johansen */ 1463e2b423SJohn Johansen 150d259f04SJohn Johansen #include <linux/ctype.h> 1663e2b423SJohn Johansen #include <linux/security.h> 1763e2b423SJohn Johansen #include <linux/vmalloc.h> 1863e2b423SJohn Johansen #include <linux/module.h> 1963e2b423SJohn Johansen #include <linux/seq_file.h> 2063e2b423SJohn Johansen #include <linux/uaccess.h> 21a71ada30SJohn Johansen #include <linux/mount.h> 2263e2b423SJohn Johansen #include <linux/namei.h> 23e74abcf3SKees Cook #include <linux/capability.h> 2429b3822fSJohn Johansen #include <linux/rcupdate.h> 25a71ada30SJohn Johansen #include <uapi/linux/major.h> 26a71ada30SJohn Johansen #include <linux/fs.h> 2763e2b423SJohn Johansen 2863e2b423SJohn Johansen #include "include/apparmor.h" 2963e2b423SJohn Johansen #include "include/apparmorfs.h" 3063e2b423SJohn Johansen #include "include/audit.h" 3163e2b423SJohn Johansen #include "include/context.h" 32f8eb8a13SJohn Johansen #include "include/crypto.h" 3363e2b423SJohn Johansen #include "include/policy.h" 34cff281f6SJohn Johansen #include "include/policy_ns.h" 35d384b0a1SKees Cook #include "include/resource.h" 365ac8c355SJohn Johansen #include "include/policy_unpack.h" 3763e2b423SJohn Johansen 3863e2b423SJohn Johansen /** 390d259f04SJohn Johansen * aa_mangle_name - mangle a profile name to std profile layout form 400d259f04SJohn Johansen * @name: profile name to mangle (NOT NULL) 410d259f04SJohn Johansen * @target: buffer to store mangled name, same length as @name (MAYBE NULL) 420d259f04SJohn Johansen * 430d259f04SJohn Johansen * Returns: length of mangled name 440d259f04SJohn Johansen */ 45bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target) 460d259f04SJohn Johansen { 470d259f04SJohn Johansen char *t = target; 480d259f04SJohn Johansen 490d259f04SJohn Johansen while (*name == '/' || *name == '.') 500d259f04SJohn Johansen name++; 510d259f04SJohn Johansen 520d259f04SJohn Johansen if (target) { 530d259f04SJohn Johansen for (; *name; name++) { 540d259f04SJohn Johansen if (*name == '/') 550d259f04SJohn Johansen *(t)++ = '.'; 560d259f04SJohn Johansen else if (isspace(*name)) 570d259f04SJohn Johansen *(t)++ = '_'; 580d259f04SJohn Johansen else if (isalnum(*name) || strchr("._-", *name)) 590d259f04SJohn Johansen *(t)++ = *name; 600d259f04SJohn Johansen } 610d259f04SJohn Johansen 620d259f04SJohn Johansen *t = 0; 630d259f04SJohn Johansen } else { 640d259f04SJohn Johansen int len = 0; 650d259f04SJohn Johansen for (; *name; name++) { 660d259f04SJohn Johansen if (isalnum(*name) || isspace(*name) || 670d259f04SJohn Johansen strchr("/._-", *name)) 680d259f04SJohn Johansen len++; 690d259f04SJohn Johansen } 700d259f04SJohn Johansen 710d259f04SJohn Johansen return len; 720d259f04SJohn Johansen } 730d259f04SJohn Johansen 740d259f04SJohn Johansen return t - target; 750d259f04SJohn Johansen } 760d259f04SJohn Johansen 770d259f04SJohn Johansen /** 7863e2b423SJohn Johansen * aa_simple_write_to_buffer - common routine for getting policy from user 7963e2b423SJohn Johansen * @op: operation doing the user buffer copy 8063e2b423SJohn Johansen * @userbuf: user buffer to copy data from (NOT NULL) 813ed02adaSJohn Johansen * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 8263e2b423SJohn Johansen * @copy_size: size of data to copy from user buffer 8363e2b423SJohn Johansen * @pos: position write is at in the file (NOT NULL) 8463e2b423SJohn Johansen * 8563e2b423SJohn Johansen * Returns: kernel buffer containing copy of user buffer data or an 8663e2b423SJohn Johansen * ERR_PTR on failure. 8763e2b423SJohn Johansen */ 885ac8c355SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(int op, 895ac8c355SJohn Johansen const char __user *userbuf, 905ac8c355SJohn Johansen size_t alloc_size, 915ac8c355SJohn Johansen size_t copy_size, 9263e2b423SJohn Johansen loff_t *pos) 9363e2b423SJohn Johansen { 945ac8c355SJohn Johansen struct aa_loaddata *data; 9563e2b423SJohn Johansen 963ed02adaSJohn Johansen BUG_ON(copy_size > alloc_size); 973ed02adaSJohn Johansen 9863e2b423SJohn Johansen if (*pos != 0) 9963e2b423SJohn Johansen /* only writes from pos 0, that is complete writes */ 10063e2b423SJohn Johansen return ERR_PTR(-ESPIPE); 10163e2b423SJohn Johansen 10263e2b423SJohn Johansen /* freed by caller to simple_write_to_buffer */ 1035ac8c355SJohn Johansen data = kvmalloc(sizeof(*data) + alloc_size); 10463e2b423SJohn Johansen if (data == NULL) 10563e2b423SJohn Johansen return ERR_PTR(-ENOMEM); 1065ac8c355SJohn Johansen kref_init(&data->count); 1075ac8c355SJohn Johansen data->size = copy_size; 1085ac8c355SJohn Johansen data->hash = NULL; 1095ac8c355SJohn Johansen data->abi = 0; 11063e2b423SJohn Johansen 1115ac8c355SJohn Johansen if (copy_from_user(data->data, userbuf, copy_size)) { 11263e2b423SJohn Johansen kvfree(data); 11363e2b423SJohn Johansen return ERR_PTR(-EFAULT); 11463e2b423SJohn Johansen } 11563e2b423SJohn Johansen 11663e2b423SJohn Johansen return data; 11763e2b423SJohn Johansen } 11863e2b423SJohn Johansen 1195ac8c355SJohn Johansen static ssize_t policy_update(int binop, const char __user *buf, size_t size, 120*b7fd2c03SJohn Johansen loff_t *pos, struct aa_ns *ns) 1215ac8c355SJohn Johansen { 1225ac8c355SJohn Johansen ssize_t error; 1235ac8c355SJohn Johansen struct aa_loaddata *data; 1245ac8c355SJohn Johansen struct aa_profile *profile = aa_current_profile(); 1255ac8c355SJohn Johansen int op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL; 1265ac8c355SJohn Johansen /* high level check about policy management - fine grained in 1275ac8c355SJohn Johansen * below after unpack 1285ac8c355SJohn Johansen */ 129*b7fd2c03SJohn Johansen error = aa_may_manage_policy(profile, ns, op); 1305ac8c355SJohn Johansen if (error) 1315ac8c355SJohn Johansen return error; 13263e2b423SJohn Johansen 1335ac8c355SJohn Johansen data = aa_simple_write_to_buffer(op, buf, size, size, pos); 1345ac8c355SJohn Johansen error = PTR_ERR(data); 1355ac8c355SJohn Johansen if (!IS_ERR(data)) { 136*b7fd2c03SJohn Johansen error = aa_replace_profiles(ns ? ns : profile->ns, profile, 137*b7fd2c03SJohn Johansen binop, data); 1385ac8c355SJohn Johansen aa_put_loaddata(data); 1395ac8c355SJohn Johansen } 1405ac8c355SJohn Johansen 1415ac8c355SJohn Johansen return error; 1425ac8c355SJohn Johansen } 1435ac8c355SJohn Johansen 144*b7fd2c03SJohn Johansen /* .load file hook fn to load policy */ 14563e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 14663e2b423SJohn Johansen loff_t *pos) 14763e2b423SJohn Johansen { 148*b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 149*b7fd2c03SJohn Johansen int error = policy_update(PROF_ADD, buf, size, pos, ns); 150*b7fd2c03SJohn Johansen 151*b7fd2c03SJohn Johansen aa_put_ns(ns); 15263e2b423SJohn Johansen 15363e2b423SJohn Johansen return error; 15463e2b423SJohn Johansen } 15563e2b423SJohn Johansen 15663e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = { 1576038f373SArnd Bergmann .write = profile_load, 1586038f373SArnd Bergmann .llseek = default_llseek, 15963e2b423SJohn Johansen }; 16063e2b423SJohn Johansen 16163e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */ 16263e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf, 16363e2b423SJohn Johansen size_t size, loff_t *pos) 16463e2b423SJohn Johansen { 165*b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 166*b7fd2c03SJohn Johansen int error = policy_update(PROF_REPLACE, buf, size, pos, ns); 167*b7fd2c03SJohn Johansen 168*b7fd2c03SJohn Johansen aa_put_ns(ns); 16963e2b423SJohn Johansen 17063e2b423SJohn Johansen return error; 17163e2b423SJohn Johansen } 17263e2b423SJohn Johansen 17363e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = { 1746038f373SArnd Bergmann .write = profile_replace, 1756038f373SArnd Bergmann .llseek = default_llseek, 17663e2b423SJohn Johansen }; 17763e2b423SJohn Johansen 178*b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */ 17963e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf, 18063e2b423SJohn Johansen size_t size, loff_t *pos) 18163e2b423SJohn Johansen { 1825ac8c355SJohn Johansen struct aa_loaddata *data; 1835ac8c355SJohn Johansen struct aa_profile *profile; 18463e2b423SJohn Johansen ssize_t error; 185*b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 18663e2b423SJohn Johansen 1875ac8c355SJohn Johansen profile = aa_current_profile(); 1885ac8c355SJohn Johansen /* high level check about policy management - fine grained in 1895ac8c355SJohn Johansen * below after unpack 1905ac8c355SJohn Johansen */ 191*b7fd2c03SJohn Johansen error = aa_may_manage_policy(profile, ns, OP_PROF_RM); 1925ac8c355SJohn Johansen if (error) 1935ac8c355SJohn Johansen goto out; 1945ac8c355SJohn Johansen 19563e2b423SJohn Johansen /* 19663e2b423SJohn Johansen * aa_remove_profile needs a null terminated string so 1 extra 19763e2b423SJohn Johansen * byte is allocated and the copied data is null terminated. 19863e2b423SJohn Johansen */ 1995ac8c355SJohn Johansen data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, 2005ac8c355SJohn Johansen pos); 20163e2b423SJohn Johansen 20263e2b423SJohn Johansen error = PTR_ERR(data); 20363e2b423SJohn Johansen if (!IS_ERR(data)) { 2045ac8c355SJohn Johansen data->data[size] = 0; 205*b7fd2c03SJohn Johansen error = aa_remove_profiles(ns ? ns : profile->ns, profile, 206*b7fd2c03SJohn Johansen data->data, size); 2075ac8c355SJohn Johansen aa_put_loaddata(data); 20863e2b423SJohn Johansen } 2095ac8c355SJohn Johansen out: 210*b7fd2c03SJohn Johansen aa_put_ns(ns); 21163e2b423SJohn Johansen return error; 21263e2b423SJohn Johansen } 21363e2b423SJohn Johansen 21463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = { 2156038f373SArnd Bergmann .write = profile_remove, 2166038f373SArnd Bergmann .llseek = default_llseek, 21763e2b423SJohn Johansen }; 21863e2b423SJohn Johansen 219e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v) 220e74abcf3SKees Cook { 221e74abcf3SKees Cook struct aa_fs_entry *fs_file = seq->private; 222e74abcf3SKees Cook 223e74abcf3SKees Cook if (!fs_file) 224e74abcf3SKees Cook return 0; 225e74abcf3SKees Cook 226e74abcf3SKees Cook switch (fs_file->v_type) { 227e74abcf3SKees Cook case AA_FS_TYPE_BOOLEAN: 228e74abcf3SKees Cook seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); 229e74abcf3SKees Cook break; 230a9bf8e9fSKees Cook case AA_FS_TYPE_STRING: 231a9bf8e9fSKees Cook seq_printf(seq, "%s\n", fs_file->v.string); 232a9bf8e9fSKees Cook break; 233e74abcf3SKees Cook case AA_FS_TYPE_U64: 234e74abcf3SKees Cook seq_printf(seq, "%#08lx\n", fs_file->v.u64); 235e74abcf3SKees Cook break; 236e74abcf3SKees Cook default: 237e74abcf3SKees Cook /* Ignore unpritable entry types. */ 238e74abcf3SKees Cook break; 239e74abcf3SKees Cook } 240e74abcf3SKees Cook 241e74abcf3SKees Cook return 0; 242e74abcf3SKees Cook } 243e74abcf3SKees Cook 244e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file) 245e74abcf3SKees Cook { 246e74abcf3SKees Cook return single_open(file, aa_fs_seq_show, inode->i_private); 247e74abcf3SKees Cook } 248e74abcf3SKees Cook 249e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = { 250e74abcf3SKees Cook .owner = THIS_MODULE, 251e74abcf3SKees Cook .open = aa_fs_seq_open, 252e74abcf3SKees Cook .read = seq_read, 253e74abcf3SKees Cook .llseek = seq_lseek, 254e74abcf3SKees Cook .release = single_release, 255e74abcf3SKees Cook }; 256e74abcf3SKees Cook 2570d259f04SJohn Johansen static int aa_fs_seq_profile_open(struct inode *inode, struct file *file, 2580d259f04SJohn Johansen int (*show)(struct seq_file *, void *)) 2590d259f04SJohn Johansen { 2608399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(inode->i_private); 2618399588aSJohn Johansen int error = single_open(file, show, proxy); 26263e2b423SJohn Johansen 2630d259f04SJohn Johansen if (error) { 2640d259f04SJohn Johansen file->private_data = NULL; 2658399588aSJohn Johansen aa_put_proxy(proxy); 2660d259f04SJohn Johansen } 2670d259f04SJohn Johansen 2680d259f04SJohn Johansen return error; 2690d259f04SJohn Johansen } 2700d259f04SJohn Johansen 2710d259f04SJohn Johansen static int aa_fs_seq_profile_release(struct inode *inode, struct file *file) 2720d259f04SJohn Johansen { 2730d259f04SJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 2740d259f04SJohn Johansen if (seq) 2758399588aSJohn Johansen aa_put_proxy(seq->private); 2760d259f04SJohn Johansen return single_release(inode, file); 2770d259f04SJohn Johansen } 2780d259f04SJohn Johansen 2790d259f04SJohn Johansen static int aa_fs_seq_profname_show(struct seq_file *seq, void *v) 2800d259f04SJohn Johansen { 2818399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 2828399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 2830d259f04SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 2840d259f04SJohn Johansen aa_put_profile(profile); 2850d259f04SJohn Johansen 2860d259f04SJohn Johansen return 0; 2870d259f04SJohn Johansen } 2880d259f04SJohn Johansen 2890d259f04SJohn Johansen static int aa_fs_seq_profname_open(struct inode *inode, struct file *file) 2900d259f04SJohn Johansen { 2910d259f04SJohn Johansen return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show); 2920d259f04SJohn Johansen } 2930d259f04SJohn Johansen 2940d259f04SJohn Johansen static const struct file_operations aa_fs_profname_fops = { 2950d259f04SJohn Johansen .owner = THIS_MODULE, 2960d259f04SJohn Johansen .open = aa_fs_seq_profname_open, 2970d259f04SJohn Johansen .read = seq_read, 2980d259f04SJohn Johansen .llseek = seq_lseek, 2990d259f04SJohn Johansen .release = aa_fs_seq_profile_release, 3000d259f04SJohn Johansen }; 3010d259f04SJohn Johansen 3020d259f04SJohn Johansen static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v) 3030d259f04SJohn Johansen { 3048399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 3058399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 3060d259f04SJohn Johansen seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); 3070d259f04SJohn Johansen aa_put_profile(profile); 3080d259f04SJohn Johansen 3090d259f04SJohn Johansen return 0; 3100d259f04SJohn Johansen } 3110d259f04SJohn Johansen 3120d259f04SJohn Johansen static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file) 3130d259f04SJohn Johansen { 3140d259f04SJohn Johansen return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show); 3150d259f04SJohn Johansen } 3160d259f04SJohn Johansen 3170d259f04SJohn Johansen static const struct file_operations aa_fs_profmode_fops = { 3180d259f04SJohn Johansen .owner = THIS_MODULE, 3190d259f04SJohn Johansen .open = aa_fs_seq_profmode_open, 3200d259f04SJohn Johansen .read = seq_read, 3210d259f04SJohn Johansen .llseek = seq_lseek, 3220d259f04SJohn Johansen .release = aa_fs_seq_profile_release, 3230d259f04SJohn Johansen }; 3240d259f04SJohn Johansen 325556d0be7SJohn Johansen static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v) 326556d0be7SJohn Johansen { 3278399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 3288399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 329556d0be7SJohn Johansen if (profile->attach) 330556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->attach); 331556d0be7SJohn Johansen else if (profile->xmatch) 332556d0be7SJohn Johansen seq_puts(seq, "<unknown>\n"); 333556d0be7SJohn Johansen else 334556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 335556d0be7SJohn Johansen aa_put_profile(profile); 336556d0be7SJohn Johansen 337556d0be7SJohn Johansen return 0; 338556d0be7SJohn Johansen } 339556d0be7SJohn Johansen 340556d0be7SJohn Johansen static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file) 341556d0be7SJohn Johansen { 342556d0be7SJohn Johansen return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show); 343556d0be7SJohn Johansen } 344556d0be7SJohn Johansen 345556d0be7SJohn Johansen static const struct file_operations aa_fs_profattach_fops = { 346556d0be7SJohn Johansen .owner = THIS_MODULE, 347556d0be7SJohn Johansen .open = aa_fs_seq_profattach_open, 348556d0be7SJohn Johansen .read = seq_read, 349556d0be7SJohn Johansen .llseek = seq_lseek, 350556d0be7SJohn Johansen .release = aa_fs_seq_profile_release, 351556d0be7SJohn Johansen }; 352556d0be7SJohn Johansen 353f8eb8a13SJohn Johansen static int aa_fs_seq_hash_show(struct seq_file *seq, void *v) 354f8eb8a13SJohn Johansen { 3558399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 3568399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 357f8eb8a13SJohn Johansen unsigned int i, size = aa_hash_size(); 358f8eb8a13SJohn Johansen 359f8eb8a13SJohn Johansen if (profile->hash) { 360f8eb8a13SJohn Johansen for (i = 0; i < size; i++) 361f8eb8a13SJohn Johansen seq_printf(seq, "%.2x", profile->hash[i]); 362f8eb8a13SJohn Johansen seq_puts(seq, "\n"); 363f8eb8a13SJohn Johansen } 3640b938a2eSJohn Johansen aa_put_profile(profile); 365f8eb8a13SJohn Johansen 366f8eb8a13SJohn Johansen return 0; 367f8eb8a13SJohn Johansen } 368f8eb8a13SJohn Johansen 369f8eb8a13SJohn Johansen static int aa_fs_seq_hash_open(struct inode *inode, struct file *file) 370f8eb8a13SJohn Johansen { 371f8eb8a13SJohn Johansen return single_open(file, aa_fs_seq_hash_show, inode->i_private); 372f8eb8a13SJohn Johansen } 373f8eb8a13SJohn Johansen 374f8eb8a13SJohn Johansen static const struct file_operations aa_fs_seq_hash_fops = { 375f8eb8a13SJohn Johansen .owner = THIS_MODULE, 376f8eb8a13SJohn Johansen .open = aa_fs_seq_hash_open, 377f8eb8a13SJohn Johansen .read = seq_read, 378f8eb8a13SJohn Johansen .llseek = seq_lseek, 379f8eb8a13SJohn Johansen .release = single_release, 380f8eb8a13SJohn Johansen }; 381f8eb8a13SJohn Johansen 3823e3e5695SJohn Johansen 383a71ada30SJohn Johansen static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v) 384a71ada30SJohn Johansen { 385a71ada30SJohn Johansen struct aa_ns *ns = aa_current_profile()->ns; 386a71ada30SJohn Johansen 387a71ada30SJohn Johansen seq_printf(seq, "%d\n", ns->level); 388a71ada30SJohn Johansen 389a71ada30SJohn Johansen return 0; 390a71ada30SJohn Johansen } 391a71ada30SJohn Johansen 392a71ada30SJohn Johansen static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file) 393a71ada30SJohn Johansen { 394a71ada30SJohn Johansen return single_open(file, aa_fs_seq_show_ns_level, inode->i_private); 395a71ada30SJohn Johansen } 396a71ada30SJohn Johansen 397a71ada30SJohn Johansen static const struct file_operations aa_fs_ns_level = { 398a71ada30SJohn Johansen .owner = THIS_MODULE, 399a71ada30SJohn Johansen .open = aa_fs_seq_open_ns_level, 400a71ada30SJohn Johansen .read = seq_read, 401a71ada30SJohn Johansen .llseek = seq_lseek, 402a71ada30SJohn Johansen .release = single_release, 403a71ada30SJohn Johansen }; 404a71ada30SJohn Johansen 4053e3e5695SJohn Johansen static int aa_fs_seq_show_ns_name(struct seq_file *seq, void *v) 4063e3e5695SJohn Johansen { 4073e3e5695SJohn Johansen struct aa_ns *ns = aa_current_profile()->ns; 4083e3e5695SJohn Johansen 4093e3e5695SJohn Johansen seq_printf(seq, "%s\n", ns->base.name); 4103e3e5695SJohn Johansen 4113e3e5695SJohn Johansen return 0; 4123e3e5695SJohn Johansen } 4133e3e5695SJohn Johansen 4143e3e5695SJohn Johansen static int aa_fs_seq_open_ns_name(struct inode *inode, struct file *file) 4153e3e5695SJohn Johansen { 4163e3e5695SJohn Johansen return single_open(file, aa_fs_seq_show_ns_name, inode->i_private); 4173e3e5695SJohn Johansen } 4183e3e5695SJohn Johansen 4193e3e5695SJohn Johansen static const struct file_operations aa_fs_ns_name = { 4203e3e5695SJohn Johansen .owner = THIS_MODULE, 4213e3e5695SJohn Johansen .open = aa_fs_seq_open_ns_name, 4223e3e5695SJohn Johansen .read = seq_read, 4233e3e5695SJohn Johansen .llseek = seq_lseek, 4243e3e5695SJohn Johansen .release = single_release, 4253e3e5695SJohn Johansen }; 4263e3e5695SJohn Johansen 4275ac8c355SJohn Johansen static int rawdata_release(struct inode *inode, struct file *file) 4285ac8c355SJohn Johansen { 4295ac8c355SJohn Johansen /* TODO: switch to loaddata when profile switched to symlink */ 4305ac8c355SJohn Johansen aa_put_loaddata(file->private_data); 4315ac8c355SJohn Johansen 4325ac8c355SJohn Johansen return 0; 4335ac8c355SJohn Johansen } 4345ac8c355SJohn Johansen 4355ac8c355SJohn Johansen static int aa_fs_seq_raw_abi_show(struct seq_file *seq, void *v) 4365ac8c355SJohn Johansen { 4375ac8c355SJohn Johansen struct aa_proxy *proxy = seq->private; 4385ac8c355SJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 4395ac8c355SJohn Johansen 4405ac8c355SJohn Johansen if (profile->rawdata->abi) { 4415ac8c355SJohn Johansen seq_printf(seq, "v%d", profile->rawdata->abi); 4425ac8c355SJohn Johansen seq_puts(seq, "\n"); 4435ac8c355SJohn Johansen } 4445ac8c355SJohn Johansen aa_put_profile(profile); 4455ac8c355SJohn Johansen 4465ac8c355SJohn Johansen return 0; 4475ac8c355SJohn Johansen } 4485ac8c355SJohn Johansen 4495ac8c355SJohn Johansen static int aa_fs_seq_raw_abi_open(struct inode *inode, struct file *file) 4505ac8c355SJohn Johansen { 4515ac8c355SJohn Johansen return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_abi_show); 4525ac8c355SJohn Johansen } 4535ac8c355SJohn Johansen 4545ac8c355SJohn Johansen static const struct file_operations aa_fs_seq_raw_abi_fops = { 4555ac8c355SJohn Johansen .owner = THIS_MODULE, 4565ac8c355SJohn Johansen .open = aa_fs_seq_raw_abi_open, 4575ac8c355SJohn Johansen .read = seq_read, 4585ac8c355SJohn Johansen .llseek = seq_lseek, 4595ac8c355SJohn Johansen .release = aa_fs_seq_profile_release, 4605ac8c355SJohn Johansen }; 4615ac8c355SJohn Johansen 4625ac8c355SJohn Johansen static int aa_fs_seq_raw_hash_show(struct seq_file *seq, void *v) 4635ac8c355SJohn Johansen { 4645ac8c355SJohn Johansen struct aa_proxy *proxy = seq->private; 4655ac8c355SJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 4665ac8c355SJohn Johansen unsigned int i, size = aa_hash_size(); 4675ac8c355SJohn Johansen 4685ac8c355SJohn Johansen if (profile->rawdata->hash) { 4695ac8c355SJohn Johansen for (i = 0; i < size; i++) 4705ac8c355SJohn Johansen seq_printf(seq, "%.2x", profile->rawdata->hash[i]); 4715ac8c355SJohn Johansen seq_puts(seq, "\n"); 4725ac8c355SJohn Johansen } 4735ac8c355SJohn Johansen aa_put_profile(profile); 4745ac8c355SJohn Johansen 4755ac8c355SJohn Johansen return 0; 4765ac8c355SJohn Johansen } 4775ac8c355SJohn Johansen 4785ac8c355SJohn Johansen static int aa_fs_seq_raw_hash_open(struct inode *inode, struct file *file) 4795ac8c355SJohn Johansen { 4805ac8c355SJohn Johansen return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_hash_show); 4815ac8c355SJohn Johansen } 4825ac8c355SJohn Johansen 4835ac8c355SJohn Johansen static const struct file_operations aa_fs_seq_raw_hash_fops = { 4845ac8c355SJohn Johansen .owner = THIS_MODULE, 4855ac8c355SJohn Johansen .open = aa_fs_seq_raw_hash_open, 4865ac8c355SJohn Johansen .read = seq_read, 4875ac8c355SJohn Johansen .llseek = seq_lseek, 4885ac8c355SJohn Johansen .release = aa_fs_seq_profile_release, 4895ac8c355SJohn Johansen }; 4905ac8c355SJohn Johansen 4915ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, 4925ac8c355SJohn Johansen loff_t *ppos) 4935ac8c355SJohn Johansen { 4945ac8c355SJohn Johansen struct aa_loaddata *rawdata = file->private_data; 4955ac8c355SJohn Johansen 4965ac8c355SJohn Johansen return simple_read_from_buffer(buf, size, ppos, rawdata->data, 4975ac8c355SJohn Johansen rawdata->size); 4985ac8c355SJohn Johansen } 4995ac8c355SJohn Johansen 5005ac8c355SJohn Johansen static int rawdata_open(struct inode *inode, struct file *file) 5015ac8c355SJohn Johansen { 5025ac8c355SJohn Johansen struct aa_proxy *proxy = inode->i_private; 5035ac8c355SJohn Johansen struct aa_profile *profile; 5045ac8c355SJohn Johansen 5055ac8c355SJohn Johansen if (!policy_view_capable(NULL)) 5065ac8c355SJohn Johansen return -EACCES; 5075ac8c355SJohn Johansen profile = aa_get_profile_rcu(&proxy->profile); 5085ac8c355SJohn Johansen file->private_data = aa_get_loaddata(profile->rawdata); 5095ac8c355SJohn Johansen aa_put_profile(profile); 5105ac8c355SJohn Johansen 5115ac8c355SJohn Johansen return 0; 5125ac8c355SJohn Johansen } 5135ac8c355SJohn Johansen 5145ac8c355SJohn Johansen static const struct file_operations aa_fs_rawdata_fops = { 5155ac8c355SJohn Johansen .open = rawdata_open, 5165ac8c355SJohn Johansen .read = rawdata_read, 5175ac8c355SJohn Johansen .llseek = generic_file_llseek, 5185ac8c355SJohn Johansen .release = rawdata_release, 5195ac8c355SJohn Johansen }; 5205ac8c355SJohn Johansen 5210d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/ 5220d259f04SJohn Johansen void __aa_fs_profile_rmdir(struct aa_profile *profile) 5230d259f04SJohn Johansen { 5240d259f04SJohn Johansen struct aa_profile *child; 5250d259f04SJohn Johansen int i; 5260d259f04SJohn Johansen 5270d259f04SJohn Johansen if (!profile) 5280d259f04SJohn Johansen return; 5290d259f04SJohn Johansen 5300d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) 5310d259f04SJohn Johansen __aa_fs_profile_rmdir(child); 5320d259f04SJohn Johansen 5330d259f04SJohn Johansen for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { 5348399588aSJohn Johansen struct aa_proxy *proxy; 5350d259f04SJohn Johansen if (!profile->dents[i]) 5360d259f04SJohn Johansen continue; 5370d259f04SJohn Johansen 5388399588aSJohn Johansen proxy = d_inode(profile->dents[i])->i_private; 5390d259f04SJohn Johansen securityfs_remove(profile->dents[i]); 5408399588aSJohn Johansen aa_put_proxy(proxy); 5410d259f04SJohn Johansen profile->dents[i] = NULL; 5420d259f04SJohn Johansen } 5430d259f04SJohn Johansen } 5440d259f04SJohn Johansen 5450d259f04SJohn Johansen void __aa_fs_profile_migrate_dents(struct aa_profile *old, 5460d259f04SJohn Johansen struct aa_profile *new) 5470d259f04SJohn Johansen { 5480d259f04SJohn Johansen int i; 5490d259f04SJohn Johansen 5500d259f04SJohn Johansen for (i = 0; i < AAFS_PROF_SIZEOF; i++) { 5510d259f04SJohn Johansen new->dents[i] = old->dents[i]; 552d671e890SJohn Johansen if (new->dents[i]) 553078cd827SDeepa Dinamani new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); 5540d259f04SJohn Johansen old->dents[i] = NULL; 5550d259f04SJohn Johansen } 5560d259f04SJohn Johansen } 5570d259f04SJohn Johansen 5580d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name, 5590d259f04SJohn Johansen struct aa_profile *profile, 5600d259f04SJohn Johansen const struct file_operations *fops) 5610d259f04SJohn Johansen { 5628399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(profile->proxy); 5630d259f04SJohn Johansen struct dentry *dent; 5640d259f04SJohn Johansen 5658399588aSJohn Johansen dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops); 5660d259f04SJohn Johansen if (IS_ERR(dent)) 5678399588aSJohn Johansen aa_put_proxy(proxy); 5680d259f04SJohn Johansen 5690d259f04SJohn Johansen return dent; 5700d259f04SJohn Johansen } 5710d259f04SJohn Johansen 5720d259f04SJohn Johansen /* requires lock be held */ 5730d259f04SJohn Johansen int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) 5740d259f04SJohn Johansen { 5750d259f04SJohn Johansen struct aa_profile *child; 5760d259f04SJohn Johansen struct dentry *dent = NULL, *dir; 5770d259f04SJohn Johansen int error; 5780d259f04SJohn Johansen 5790d259f04SJohn Johansen if (!parent) { 5800d259f04SJohn Johansen struct aa_profile *p; 5810d259f04SJohn Johansen p = aa_deref_parent(profile); 5820d259f04SJohn Johansen dent = prof_dir(p); 5830d259f04SJohn Johansen /* adding to parent that previously didn't have children */ 5840d259f04SJohn Johansen dent = securityfs_create_dir("profiles", dent); 5850d259f04SJohn Johansen if (IS_ERR(dent)) 5860d259f04SJohn Johansen goto fail; 5870d259f04SJohn Johansen prof_child_dir(p) = parent = dent; 5880d259f04SJohn Johansen } 5890d259f04SJohn Johansen 5900d259f04SJohn Johansen if (!profile->dirname) { 5910d259f04SJohn Johansen int len, id_len; 5920d259f04SJohn Johansen len = mangle_name(profile->base.name, NULL); 5930d259f04SJohn Johansen id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); 5940d259f04SJohn Johansen 5950d259f04SJohn Johansen profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); 5960d259f04SJohn Johansen if (!profile->dirname) 5970d259f04SJohn Johansen goto fail; 5980d259f04SJohn Johansen 5990d259f04SJohn Johansen mangle_name(profile->base.name, profile->dirname); 6000d259f04SJohn Johansen sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); 6010d259f04SJohn Johansen } 6020d259f04SJohn Johansen 6030d259f04SJohn Johansen dent = securityfs_create_dir(profile->dirname, parent); 6040d259f04SJohn Johansen if (IS_ERR(dent)) 6050d259f04SJohn Johansen goto fail; 6060d259f04SJohn Johansen prof_dir(profile) = dir = dent; 6070d259f04SJohn Johansen 6080d259f04SJohn Johansen dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops); 6090d259f04SJohn Johansen if (IS_ERR(dent)) 6100d259f04SJohn Johansen goto fail; 6110d259f04SJohn Johansen profile->dents[AAFS_PROF_NAME] = dent; 6120d259f04SJohn Johansen 6130d259f04SJohn Johansen dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops); 6140d259f04SJohn Johansen if (IS_ERR(dent)) 6150d259f04SJohn Johansen goto fail; 6160d259f04SJohn Johansen profile->dents[AAFS_PROF_MODE] = dent; 6170d259f04SJohn Johansen 618556d0be7SJohn Johansen dent = create_profile_file(dir, "attach", profile, 619556d0be7SJohn Johansen &aa_fs_profattach_fops); 620556d0be7SJohn Johansen if (IS_ERR(dent)) 621556d0be7SJohn Johansen goto fail; 622556d0be7SJohn Johansen profile->dents[AAFS_PROF_ATTACH] = dent; 623556d0be7SJohn Johansen 624f8eb8a13SJohn Johansen if (profile->hash) { 625f8eb8a13SJohn Johansen dent = create_profile_file(dir, "sha1", profile, 626f8eb8a13SJohn Johansen &aa_fs_seq_hash_fops); 627f8eb8a13SJohn Johansen if (IS_ERR(dent)) 628f8eb8a13SJohn Johansen goto fail; 629f8eb8a13SJohn Johansen profile->dents[AAFS_PROF_HASH] = dent; 630f8eb8a13SJohn Johansen } 631f8eb8a13SJohn Johansen 6325ac8c355SJohn Johansen if (profile->rawdata) { 6335ac8c355SJohn Johansen dent = create_profile_file(dir, "raw_sha1", profile, 6345ac8c355SJohn Johansen &aa_fs_seq_raw_hash_fops); 6355ac8c355SJohn Johansen if (IS_ERR(dent)) 6365ac8c355SJohn Johansen goto fail; 6375ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_HASH] = dent; 6385ac8c355SJohn Johansen 6395ac8c355SJohn Johansen dent = create_profile_file(dir, "raw_abi", profile, 6405ac8c355SJohn Johansen &aa_fs_seq_raw_abi_fops); 6415ac8c355SJohn Johansen if (IS_ERR(dent)) 6425ac8c355SJohn Johansen goto fail; 6435ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_ABI] = dent; 6445ac8c355SJohn Johansen 6455ac8c355SJohn Johansen dent = securityfs_create_file("raw_data", S_IFREG | 0444, dir, 6465ac8c355SJohn Johansen profile->proxy, 6475ac8c355SJohn Johansen &aa_fs_rawdata_fops); 6485ac8c355SJohn Johansen if (IS_ERR(dent)) 6495ac8c355SJohn Johansen goto fail; 6505ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_DATA] = dent; 6515ac8c355SJohn Johansen d_inode(dent)->i_size = profile->rawdata->size; 6525ac8c355SJohn Johansen aa_get_proxy(profile->proxy); 6535ac8c355SJohn Johansen } 6545ac8c355SJohn Johansen 6550d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) { 6560d259f04SJohn Johansen error = __aa_fs_profile_mkdir(child, prof_child_dir(profile)); 6570d259f04SJohn Johansen if (error) 6580d259f04SJohn Johansen goto fail2; 6590d259f04SJohn Johansen } 6600d259f04SJohn Johansen 6610d259f04SJohn Johansen return 0; 6620d259f04SJohn Johansen 6630d259f04SJohn Johansen fail: 6640d259f04SJohn Johansen error = PTR_ERR(dent); 6650d259f04SJohn Johansen 6660d259f04SJohn Johansen fail2: 6670d259f04SJohn Johansen __aa_fs_profile_rmdir(profile); 6680d259f04SJohn Johansen 6690d259f04SJohn Johansen return error; 6700d259f04SJohn Johansen } 6710d259f04SJohn Johansen 67298849dffSJohn Johansen void __aa_fs_ns_rmdir(struct aa_ns *ns) 6730d259f04SJohn Johansen { 67498849dffSJohn Johansen struct aa_ns *sub; 6750d259f04SJohn Johansen struct aa_profile *child; 6760d259f04SJohn Johansen int i; 6770d259f04SJohn Johansen 6780d259f04SJohn Johansen if (!ns) 6790d259f04SJohn Johansen return; 6800d259f04SJohn Johansen 6810d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) 6820d259f04SJohn Johansen __aa_fs_profile_rmdir(child); 6830d259f04SJohn Johansen 6840d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 6850d259f04SJohn Johansen mutex_lock(&sub->lock); 68698849dffSJohn Johansen __aa_fs_ns_rmdir(sub); 6870d259f04SJohn Johansen mutex_unlock(&sub->lock); 6880d259f04SJohn Johansen } 6890d259f04SJohn Johansen 690*b7fd2c03SJohn Johansen if (ns_subns_dir(ns)) { 691*b7fd2c03SJohn Johansen sub = d_inode(ns_subns_dir(ns))->i_private; 692*b7fd2c03SJohn Johansen aa_put_ns(sub); 693*b7fd2c03SJohn Johansen } 694*b7fd2c03SJohn Johansen if (ns_subload(ns)) { 695*b7fd2c03SJohn Johansen sub = d_inode(ns_subload(ns))->i_private; 696*b7fd2c03SJohn Johansen aa_put_ns(sub); 697*b7fd2c03SJohn Johansen } 698*b7fd2c03SJohn Johansen if (ns_subreplace(ns)) { 699*b7fd2c03SJohn Johansen sub = d_inode(ns_subreplace(ns))->i_private; 700*b7fd2c03SJohn Johansen aa_put_ns(sub); 701*b7fd2c03SJohn Johansen } 702*b7fd2c03SJohn Johansen if (ns_subremove(ns)) { 703*b7fd2c03SJohn Johansen sub = d_inode(ns_subremove(ns))->i_private; 704*b7fd2c03SJohn Johansen aa_put_ns(sub); 705*b7fd2c03SJohn Johansen } 706*b7fd2c03SJohn Johansen 7070d259f04SJohn Johansen for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { 7080d259f04SJohn Johansen securityfs_remove(ns->dents[i]); 7090d259f04SJohn Johansen ns->dents[i] = NULL; 7100d259f04SJohn Johansen } 7110d259f04SJohn Johansen } 7120d259f04SJohn Johansen 713*b7fd2c03SJohn Johansen /* assumes cleanup in caller */ 714*b7fd2c03SJohn Johansen static int __aa_fs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) 715*b7fd2c03SJohn Johansen { 716*b7fd2c03SJohn Johansen struct dentry *dent; 717*b7fd2c03SJohn Johansen 718*b7fd2c03SJohn Johansen AA_BUG(!ns); 719*b7fd2c03SJohn Johansen AA_BUG(!dir); 720*b7fd2c03SJohn Johansen 721*b7fd2c03SJohn Johansen dent = securityfs_create_dir("profiles", dir); 722*b7fd2c03SJohn Johansen if (IS_ERR(dent)) 723*b7fd2c03SJohn Johansen return PTR_ERR(dent); 724*b7fd2c03SJohn Johansen ns_subprofs_dir(ns) = dent; 725*b7fd2c03SJohn Johansen 726*b7fd2c03SJohn Johansen dent = securityfs_create_dir("raw_data", dir); 727*b7fd2c03SJohn Johansen if (IS_ERR(dent)) 728*b7fd2c03SJohn Johansen return PTR_ERR(dent); 729*b7fd2c03SJohn Johansen ns_subdata_dir(ns) = dent; 730*b7fd2c03SJohn Johansen 731*b7fd2c03SJohn Johansen dent = securityfs_create_file(".load", 0640, dir, ns, 732*b7fd2c03SJohn Johansen &aa_fs_profile_load); 733*b7fd2c03SJohn Johansen if (IS_ERR(dent)) 734*b7fd2c03SJohn Johansen return PTR_ERR(dent); 735*b7fd2c03SJohn Johansen aa_get_ns(ns); 736*b7fd2c03SJohn Johansen ns_subload(ns) = dent; 737*b7fd2c03SJohn Johansen 738*b7fd2c03SJohn Johansen dent = securityfs_create_file(".replace", 0640, dir, ns, 739*b7fd2c03SJohn Johansen &aa_fs_profile_replace); 740*b7fd2c03SJohn Johansen if (IS_ERR(dent)) 741*b7fd2c03SJohn Johansen return PTR_ERR(dent); 742*b7fd2c03SJohn Johansen aa_get_ns(ns); 743*b7fd2c03SJohn Johansen ns_subreplace(ns) = dent; 744*b7fd2c03SJohn Johansen 745*b7fd2c03SJohn Johansen dent = securityfs_create_file(".remove", 0640, dir, ns, 746*b7fd2c03SJohn Johansen &aa_fs_profile_remove); 747*b7fd2c03SJohn Johansen if (IS_ERR(dent)) 748*b7fd2c03SJohn Johansen return PTR_ERR(dent); 749*b7fd2c03SJohn Johansen aa_get_ns(ns); 750*b7fd2c03SJohn Johansen ns_subremove(ns) = dent; 751*b7fd2c03SJohn Johansen 752*b7fd2c03SJohn Johansen dent = securityfs_create_dir("namespaces", dir); 753*b7fd2c03SJohn Johansen if (IS_ERR(dent)) 754*b7fd2c03SJohn Johansen return PTR_ERR(dent); 755*b7fd2c03SJohn Johansen aa_get_ns(ns); 756*b7fd2c03SJohn Johansen ns_subns_dir(ns) = dent; 757*b7fd2c03SJohn Johansen 758*b7fd2c03SJohn Johansen return 0; 759*b7fd2c03SJohn Johansen } 760*b7fd2c03SJohn Johansen 76198849dffSJohn Johansen int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name) 7620d259f04SJohn Johansen { 76398849dffSJohn Johansen struct aa_ns *sub; 7640d259f04SJohn Johansen struct aa_profile *child; 7650d259f04SJohn Johansen struct dentry *dent, *dir; 7660d259f04SJohn Johansen int error; 7670d259f04SJohn Johansen 768*b7fd2c03SJohn Johansen AA_BUG(!ns); 769*b7fd2c03SJohn Johansen AA_BUG(!parent); 770*b7fd2c03SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 771*b7fd2c03SJohn Johansen 7720d259f04SJohn Johansen if (!name) 7730d259f04SJohn Johansen name = ns->base.name; 7740d259f04SJohn Johansen 775*b7fd2c03SJohn Johansen /* create ns dir if it doesn't already exist */ 7760d259f04SJohn Johansen dent = securityfs_create_dir(name, parent); 7770d259f04SJohn Johansen if (IS_ERR(dent)) 7780d259f04SJohn Johansen goto fail; 779*b7fd2c03SJohn Johansen 7800d259f04SJohn Johansen ns_dir(ns) = dir = dent; 781*b7fd2c03SJohn Johansen error = __aa_fs_ns_mkdir_entries(ns, dir); 782*b7fd2c03SJohn Johansen if (error) 783*b7fd2c03SJohn Johansen goto fail2; 7840d259f04SJohn Johansen 785*b7fd2c03SJohn Johansen /* profiles */ 7860d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) { 7870d259f04SJohn Johansen error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns)); 7880d259f04SJohn Johansen if (error) 7890d259f04SJohn Johansen goto fail2; 7900d259f04SJohn Johansen } 7910d259f04SJohn Johansen 792*b7fd2c03SJohn Johansen /* subnamespaces */ 7930d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 7940d259f04SJohn Johansen mutex_lock(&sub->lock); 79598849dffSJohn Johansen error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL); 7960d259f04SJohn Johansen mutex_unlock(&sub->lock); 7970d259f04SJohn Johansen if (error) 7980d259f04SJohn Johansen goto fail2; 7990d259f04SJohn Johansen } 8000d259f04SJohn Johansen 8010d259f04SJohn Johansen return 0; 8020d259f04SJohn Johansen 8030d259f04SJohn Johansen fail: 8040d259f04SJohn Johansen error = PTR_ERR(dent); 8050d259f04SJohn Johansen 8060d259f04SJohn Johansen fail2: 80798849dffSJohn Johansen __aa_fs_ns_rmdir(ns); 8080d259f04SJohn Johansen 8090d259f04SJohn Johansen return error; 8100d259f04SJohn Johansen } 8110d259f04SJohn Johansen 8120d259f04SJohn Johansen 81329b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head)) 81429b3822fSJohn Johansen 81529b3822fSJohn Johansen /** 81698849dffSJohn Johansen * __next_ns - find the next namespace to list 81729b3822fSJohn Johansen * @root: root namespace to stop search at (NOT NULL) 81829b3822fSJohn Johansen * @ns: current ns position (NOT NULL) 81929b3822fSJohn Johansen * 82029b3822fSJohn Johansen * Find the next namespace from @ns under @root and handle all locking needed 82129b3822fSJohn Johansen * while switching current namespace. 82229b3822fSJohn Johansen * 82329b3822fSJohn Johansen * Returns: next namespace or NULL if at last namespace under @root 82429b3822fSJohn Johansen * Requires: ns->parent->lock to be held 82529b3822fSJohn Johansen * NOTE: will not unlock root->lock 82629b3822fSJohn Johansen */ 82798849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) 82829b3822fSJohn Johansen { 82998849dffSJohn Johansen struct aa_ns *parent, *next; 83029b3822fSJohn Johansen 83129b3822fSJohn Johansen /* is next namespace a child */ 83229b3822fSJohn Johansen if (!list_empty(&ns->sub_ns)) { 83329b3822fSJohn Johansen next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); 83429b3822fSJohn Johansen mutex_lock(&next->lock); 83529b3822fSJohn Johansen return next; 83629b3822fSJohn Johansen } 83729b3822fSJohn Johansen 83829b3822fSJohn Johansen /* check if the next ns is a sibling, parent, gp, .. */ 83929b3822fSJohn Johansen parent = ns->parent; 840ed2c7da3SJohn Johansen while (ns != root) { 84129b3822fSJohn Johansen mutex_unlock(&ns->lock); 84238dbd7d8SGeliang Tang next = list_next_entry(ns, base.list); 84329b3822fSJohn Johansen if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { 84429b3822fSJohn Johansen mutex_lock(&next->lock); 84529b3822fSJohn Johansen return next; 84629b3822fSJohn Johansen } 84729b3822fSJohn Johansen ns = parent; 84829b3822fSJohn Johansen parent = parent->parent; 84929b3822fSJohn Johansen } 85029b3822fSJohn Johansen 85129b3822fSJohn Johansen return NULL; 85229b3822fSJohn Johansen } 85329b3822fSJohn Johansen 85429b3822fSJohn Johansen /** 85529b3822fSJohn Johansen * __first_profile - find the first profile in a namespace 85629b3822fSJohn Johansen * @root: namespace that is root of profiles being displayed (NOT NULL) 85729b3822fSJohn Johansen * @ns: namespace to start in (NOT NULL) 85829b3822fSJohn Johansen * 85929b3822fSJohn Johansen * Returns: unrefcounted profile or NULL if no profile 86029b3822fSJohn Johansen * Requires: profile->ns.lock to be held 86129b3822fSJohn Johansen */ 86298849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root, 86398849dffSJohn Johansen struct aa_ns *ns) 86429b3822fSJohn Johansen { 86598849dffSJohn Johansen for (; ns; ns = __next_ns(root, ns)) { 86629b3822fSJohn Johansen if (!list_empty(&ns->base.profiles)) 86729b3822fSJohn Johansen return list_first_entry(&ns->base.profiles, 86829b3822fSJohn Johansen struct aa_profile, base.list); 86929b3822fSJohn Johansen } 87029b3822fSJohn Johansen return NULL; 87129b3822fSJohn Johansen } 87229b3822fSJohn Johansen 87329b3822fSJohn Johansen /** 87429b3822fSJohn Johansen * __next_profile - step to the next profile in a profile tree 87529b3822fSJohn Johansen * @profile: current profile in tree (NOT NULL) 87629b3822fSJohn Johansen * 87729b3822fSJohn Johansen * Perform a depth first traversal on the profile tree in a namespace 87829b3822fSJohn Johansen * 87929b3822fSJohn Johansen * Returns: next profile or NULL if done 88029b3822fSJohn Johansen * Requires: profile->ns.lock to be held 88129b3822fSJohn Johansen */ 88229b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p) 88329b3822fSJohn Johansen { 88429b3822fSJohn Johansen struct aa_profile *parent; 88598849dffSJohn Johansen struct aa_ns *ns = p->ns; 88629b3822fSJohn Johansen 88729b3822fSJohn Johansen /* is next profile a child */ 88829b3822fSJohn Johansen if (!list_empty(&p->base.profiles)) 88929b3822fSJohn Johansen return list_first_entry(&p->base.profiles, typeof(*p), 89029b3822fSJohn Johansen base.list); 89129b3822fSJohn Johansen 89229b3822fSJohn Johansen /* is next profile a sibling, parent sibling, gp, sibling, .. */ 89329b3822fSJohn Johansen parent = rcu_dereference_protected(p->parent, 89429b3822fSJohn Johansen mutex_is_locked(&p->ns->lock)); 89529b3822fSJohn Johansen while (parent) { 89638dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 89729b3822fSJohn Johansen if (!list_entry_is_head(p, &parent->base.profiles, base.list)) 89829b3822fSJohn Johansen return p; 89929b3822fSJohn Johansen p = parent; 90029b3822fSJohn Johansen parent = rcu_dereference_protected(parent->parent, 90129b3822fSJohn Johansen mutex_is_locked(&parent->ns->lock)); 90229b3822fSJohn Johansen } 90329b3822fSJohn Johansen 90429b3822fSJohn Johansen /* is next another profile in the namespace */ 90538dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 90629b3822fSJohn Johansen if (!list_entry_is_head(p, &ns->base.profiles, base.list)) 90729b3822fSJohn Johansen return p; 90829b3822fSJohn Johansen 90929b3822fSJohn Johansen return NULL; 91029b3822fSJohn Johansen } 91129b3822fSJohn Johansen 91229b3822fSJohn Johansen /** 91329b3822fSJohn Johansen * next_profile - step to the next profile in where ever it may be 91429b3822fSJohn Johansen * @root: root namespace (NOT NULL) 91529b3822fSJohn Johansen * @profile: current profile (NOT NULL) 91629b3822fSJohn Johansen * 91729b3822fSJohn Johansen * Returns: next profile or NULL if there isn't one 91829b3822fSJohn Johansen */ 91998849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root, 92029b3822fSJohn Johansen struct aa_profile *profile) 92129b3822fSJohn Johansen { 92229b3822fSJohn Johansen struct aa_profile *next = __next_profile(profile); 92329b3822fSJohn Johansen if (next) 92429b3822fSJohn Johansen return next; 92529b3822fSJohn Johansen 92629b3822fSJohn Johansen /* finished all profiles in namespace move to next namespace */ 92798849dffSJohn Johansen return __first_profile(root, __next_ns(root, profile->ns)); 92829b3822fSJohn Johansen } 92929b3822fSJohn Johansen 93029b3822fSJohn Johansen /** 93129b3822fSJohn Johansen * p_start - start a depth first traversal of profile tree 93229b3822fSJohn Johansen * @f: seq_file to fill 93329b3822fSJohn Johansen * @pos: current position 93429b3822fSJohn Johansen * 93529b3822fSJohn Johansen * Returns: first profile under current namespace or NULL if none found 93629b3822fSJohn Johansen * 93729b3822fSJohn Johansen * acquires first ns->lock 93829b3822fSJohn Johansen */ 93929b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos) 94029b3822fSJohn Johansen { 94129b3822fSJohn Johansen struct aa_profile *profile = NULL; 94298849dffSJohn Johansen struct aa_ns *root = aa_current_profile()->ns; 94329b3822fSJohn Johansen loff_t l = *pos; 94498849dffSJohn Johansen f->private = aa_get_ns(root); 94529b3822fSJohn Johansen 94629b3822fSJohn Johansen 94729b3822fSJohn Johansen /* find the first profile */ 94829b3822fSJohn Johansen mutex_lock(&root->lock); 94929b3822fSJohn Johansen profile = __first_profile(root, root); 95029b3822fSJohn Johansen 95129b3822fSJohn Johansen /* skip to position */ 95229b3822fSJohn Johansen for (; profile && l > 0; l--) 95329b3822fSJohn Johansen profile = next_profile(root, profile); 95429b3822fSJohn Johansen 95529b3822fSJohn Johansen return profile; 95629b3822fSJohn Johansen } 95729b3822fSJohn Johansen 95829b3822fSJohn Johansen /** 95929b3822fSJohn Johansen * p_next - read the next profile entry 96029b3822fSJohn Johansen * @f: seq_file to fill 96129b3822fSJohn Johansen * @p: profile previously returned 96229b3822fSJohn Johansen * @pos: current position 96329b3822fSJohn Johansen * 96429b3822fSJohn Johansen * Returns: next profile after @p or NULL if none 96529b3822fSJohn Johansen * 96629b3822fSJohn Johansen * may acquire/release locks in namespace tree as necessary 96729b3822fSJohn Johansen */ 96829b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos) 96929b3822fSJohn Johansen { 97029b3822fSJohn Johansen struct aa_profile *profile = p; 97198849dffSJohn Johansen struct aa_ns *ns = f->private; 97229b3822fSJohn Johansen (*pos)++; 97329b3822fSJohn Johansen 97429b3822fSJohn Johansen return next_profile(ns, profile); 97529b3822fSJohn Johansen } 97629b3822fSJohn Johansen 97729b3822fSJohn Johansen /** 97829b3822fSJohn Johansen * p_stop - stop depth first traversal 97929b3822fSJohn Johansen * @f: seq_file we are filling 98029b3822fSJohn Johansen * @p: the last profile writen 98129b3822fSJohn Johansen * 98229b3822fSJohn Johansen * Release all locking done by p_start/p_next on namespace tree 98329b3822fSJohn Johansen */ 98429b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p) 98529b3822fSJohn Johansen { 98629b3822fSJohn Johansen struct aa_profile *profile = p; 98798849dffSJohn Johansen struct aa_ns *root = f->private, *ns; 98829b3822fSJohn Johansen 98929b3822fSJohn Johansen if (profile) { 99029b3822fSJohn Johansen for (ns = profile->ns; ns && ns != root; ns = ns->parent) 99129b3822fSJohn Johansen mutex_unlock(&ns->lock); 99229b3822fSJohn Johansen } 99329b3822fSJohn Johansen mutex_unlock(&root->lock); 99498849dffSJohn Johansen aa_put_ns(root); 99529b3822fSJohn Johansen } 99629b3822fSJohn Johansen 99729b3822fSJohn Johansen /** 99829b3822fSJohn Johansen * seq_show_profile - show a profile entry 99929b3822fSJohn Johansen * @f: seq_file to file 100029b3822fSJohn Johansen * @p: current position (profile) (NOT NULL) 100129b3822fSJohn Johansen * 100229b3822fSJohn Johansen * Returns: error on failure 100329b3822fSJohn Johansen */ 100429b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p) 100529b3822fSJohn Johansen { 100629b3822fSJohn Johansen struct aa_profile *profile = (struct aa_profile *)p; 100798849dffSJohn Johansen struct aa_ns *root = f->private; 100829b3822fSJohn Johansen 100929b3822fSJohn Johansen if (profile->ns != root) 101092b6d8efSJohn Johansen seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true)); 101129b3822fSJohn Johansen seq_printf(f, "%s (%s)\n", profile->base.hname, 101229b3822fSJohn Johansen aa_profile_mode_names[profile->mode]); 101329b3822fSJohn Johansen 101429b3822fSJohn Johansen return 0; 101529b3822fSJohn Johansen } 101629b3822fSJohn Johansen 101729b3822fSJohn Johansen static const struct seq_operations aa_fs_profiles_op = { 101829b3822fSJohn Johansen .start = p_start, 101929b3822fSJohn Johansen .next = p_next, 102029b3822fSJohn Johansen .stop = p_stop, 102129b3822fSJohn Johansen .show = seq_show_profile, 102229b3822fSJohn Johansen }; 102329b3822fSJohn Johansen 102429b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file) 102529b3822fSJohn Johansen { 10265ac8c355SJohn Johansen if (!policy_view_capable(NULL)) 10275ac8c355SJohn Johansen return -EACCES; 10285ac8c355SJohn Johansen 102929b3822fSJohn Johansen return seq_open(file, &aa_fs_profiles_op); 103029b3822fSJohn Johansen } 103129b3822fSJohn Johansen 103229b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file) 103329b3822fSJohn Johansen { 103429b3822fSJohn Johansen return seq_release(inode, file); 103529b3822fSJohn Johansen } 103629b3822fSJohn Johansen 103729b3822fSJohn Johansen static const struct file_operations aa_fs_profiles_fops = { 103829b3822fSJohn Johansen .open = profiles_open, 103929b3822fSJohn Johansen .read = seq_read, 104029b3822fSJohn Johansen .llseek = seq_lseek, 104129b3822fSJohn Johansen .release = profiles_release, 104229b3822fSJohn Johansen }; 104329b3822fSJohn Johansen 104429b3822fSJohn Johansen 10450d259f04SJohn Johansen /** Base file system setup **/ 1046a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = { 1047a9bf8e9fSKees Cook AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \ 1048a9bf8e9fSKees Cook "link lock"), 1049a9bf8e9fSKees Cook { } 1050a9bf8e9fSKees Cook }; 1051a9bf8e9fSKees Cook 1052e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = { 1053e74abcf3SKees Cook AA_FS_FILE_BOOLEAN("change_hat", 1), 1054e74abcf3SKees Cook AA_FS_FILE_BOOLEAN("change_hatv", 1), 1055e74abcf3SKees Cook AA_FS_FILE_BOOLEAN("change_onexec", 1), 1056e74abcf3SKees Cook AA_FS_FILE_BOOLEAN("change_profile", 1), 105734c426acSJohn Johansen AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), 1058e74abcf3SKees Cook { } 1059e74abcf3SKees Cook }; 1060e74abcf3SKees Cook 1061474d6b75SJohn Johansen static struct aa_fs_entry aa_fs_entry_versions[] = { 1062474d6b75SJohn Johansen AA_FS_FILE_BOOLEAN("v5", 1), 1063474d6b75SJohn Johansen { } 1064474d6b75SJohn Johansen }; 1065474d6b75SJohn Johansen 10669d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = { 1067474d6b75SJohn Johansen AA_FS_DIR("versions", aa_fs_entry_versions), 1068dd51c848SJohn Johansen AA_FS_FILE_BOOLEAN("set_load", 1), 10699d910a3bSJohn Johansen { } 10709d910a3bSJohn Johansen }; 10719d910a3bSJohn Johansen 1072e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = { 10739d910a3bSJohn Johansen AA_FS_DIR("policy", aa_fs_entry_policy), 1074e74abcf3SKees Cook AA_FS_DIR("domain", aa_fs_entry_domain), 1075a9bf8e9fSKees Cook AA_FS_DIR("file", aa_fs_entry_file), 1076e74abcf3SKees Cook AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 1077d384b0a1SKees Cook AA_FS_DIR("rlimit", aa_fs_entry_rlimit), 107884f1f787SJohn Johansen AA_FS_DIR("caps", aa_fs_entry_caps), 1079e74abcf3SKees Cook { } 1080e74abcf3SKees Cook }; 1081e74abcf3SKees Cook 10829acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = { 1083a71ada30SJohn Johansen AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level), 10843e3e5695SJohn Johansen AA_FS_FILE_FOPS(".ns_name", 0640, &aa_fs_ns_name), 1085*b7fd2c03SJohn Johansen AA_FS_FILE_FOPS("profiles", 0440, &aa_fs_profiles_fops), 1086e74abcf3SKees Cook AA_FS_DIR("features", aa_fs_entry_features), 10879acd494bSKees Cook { } 10889acd494bSKees Cook }; 108963e2b423SJohn Johansen 10909acd494bSKees Cook static struct aa_fs_entry aa_fs_entry = 10919acd494bSKees Cook AA_FS_DIR("apparmor", aa_fs_entry_apparmor); 10929acd494bSKees Cook 10939acd494bSKees Cook /** 10949acd494bSKees Cook * aafs_create_file - create a file entry in the apparmor securityfs 10959acd494bSKees Cook * @fs_file: aa_fs_entry to build an entry for (NOT NULL) 10969acd494bSKees Cook * @parent: the parent dentry in the securityfs 10979acd494bSKees Cook * 10989acd494bSKees Cook * Use aafs_remove_file to remove entries created with this fn. 10999acd494bSKees Cook */ 11009acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file, 11019acd494bSKees Cook struct dentry *parent) 110263e2b423SJohn Johansen { 11039acd494bSKees Cook int error = 0; 110463e2b423SJohn Johansen 11059acd494bSKees Cook fs_file->dentry = securityfs_create_file(fs_file->name, 11069acd494bSKees Cook S_IFREG | fs_file->mode, 11079acd494bSKees Cook parent, fs_file, 11089acd494bSKees Cook fs_file->file_ops); 11099acd494bSKees Cook if (IS_ERR(fs_file->dentry)) { 11109acd494bSKees Cook error = PTR_ERR(fs_file->dentry); 11119acd494bSKees Cook fs_file->dentry = NULL; 111263e2b423SJohn Johansen } 11139acd494bSKees Cook return error; 111463e2b423SJohn Johansen } 111563e2b423SJohn Johansen 11160d259f04SJohn Johansen static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir); 111763e2b423SJohn Johansen /** 11189acd494bSKees Cook * aafs_create_dir - recursively create a directory entry in the securityfs 11199acd494bSKees Cook * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL) 11209acd494bSKees Cook * @parent: the parent dentry in the securityfs 112163e2b423SJohn Johansen * 11229acd494bSKees Cook * Use aafs_remove_dir to remove entries created with this fn. 112363e2b423SJohn Johansen */ 11249acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir, 11259acd494bSKees Cook struct dentry *parent) 112663e2b423SJohn Johansen { 11279acd494bSKees Cook struct aa_fs_entry *fs_file; 11280d259f04SJohn Johansen struct dentry *dir; 11290d259f04SJohn Johansen int error; 113063e2b423SJohn Johansen 11310d259f04SJohn Johansen dir = securityfs_create_dir(fs_dir->name, parent); 11320d259f04SJohn Johansen if (IS_ERR(dir)) 11330d259f04SJohn Johansen return PTR_ERR(dir); 11340d259f04SJohn Johansen fs_dir->dentry = dir; 113563e2b423SJohn Johansen 11360d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 11379acd494bSKees Cook if (fs_file->v_type == AA_FS_TYPE_DIR) 11389acd494bSKees Cook error = aafs_create_dir(fs_file, fs_dir->dentry); 11399acd494bSKees Cook else 11409acd494bSKees Cook error = aafs_create_file(fs_file, fs_dir->dentry); 11419acd494bSKees Cook if (error) 11429acd494bSKees Cook goto failed; 11439acd494bSKees Cook } 11449acd494bSKees Cook 11459acd494bSKees Cook return 0; 11469acd494bSKees Cook 11479acd494bSKees Cook failed: 11480d259f04SJohn Johansen aafs_remove_dir(fs_dir); 11490d259f04SJohn Johansen 11509acd494bSKees Cook return error; 11519acd494bSKees Cook } 11529acd494bSKees Cook 11539acd494bSKees Cook /** 11549acd494bSKees Cook * aafs_remove_file - drop a single file entry in the apparmor securityfs 11559acd494bSKees Cook * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL) 11569acd494bSKees Cook */ 11579acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file) 11589acd494bSKees Cook { 11599acd494bSKees Cook if (!fs_file->dentry) 11609acd494bSKees Cook return; 11619acd494bSKees Cook 11629acd494bSKees Cook securityfs_remove(fs_file->dentry); 11639acd494bSKees Cook fs_file->dentry = NULL; 11649acd494bSKees Cook } 11659acd494bSKees Cook 11669acd494bSKees Cook /** 11679acd494bSKees Cook * aafs_remove_dir - recursively drop a directory entry from the securityfs 11689acd494bSKees Cook * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL) 11699acd494bSKees Cook */ 11709acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir) 11719acd494bSKees Cook { 11729acd494bSKees Cook struct aa_fs_entry *fs_file; 11739acd494bSKees Cook 11740d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 11759acd494bSKees Cook if (fs_file->v_type == AA_FS_TYPE_DIR) 11769acd494bSKees Cook aafs_remove_dir(fs_file); 11779acd494bSKees Cook else 11789acd494bSKees Cook aafs_remove_file(fs_file); 11799acd494bSKees Cook } 11809acd494bSKees Cook 11819acd494bSKees Cook aafs_remove_file(fs_dir); 118263e2b423SJohn Johansen } 118363e2b423SJohn Johansen 118463e2b423SJohn Johansen /** 118563e2b423SJohn Johansen * aa_destroy_aafs - cleanup and free aafs 118663e2b423SJohn Johansen * 118763e2b423SJohn Johansen * releases dentries allocated by aa_create_aafs 118863e2b423SJohn Johansen */ 118963e2b423SJohn Johansen void __init aa_destroy_aafs(void) 119063e2b423SJohn Johansen { 11919acd494bSKees Cook aafs_remove_dir(&aa_fs_entry); 119263e2b423SJohn Johansen } 119363e2b423SJohn Johansen 1194a71ada30SJohn Johansen 1195a71ada30SJohn Johansen #define NULL_FILE_NAME ".null" 1196a71ada30SJohn Johansen struct path aa_null; 1197a71ada30SJohn Johansen 1198a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent) 1199a71ada30SJohn Johansen { 1200a71ada30SJohn Johansen struct vfsmount *mount = NULL; 1201a71ada30SJohn Johansen struct dentry *dentry; 1202a71ada30SJohn Johansen struct inode *inode; 1203a71ada30SJohn Johansen int count = 0; 1204a71ada30SJohn Johansen int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); 1205a71ada30SJohn Johansen 1206a71ada30SJohn Johansen if (error) 1207a71ada30SJohn Johansen return error; 1208a71ada30SJohn Johansen 1209a71ada30SJohn Johansen inode_lock(d_inode(parent)); 1210a71ada30SJohn Johansen dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); 1211a71ada30SJohn Johansen if (IS_ERR(dentry)) { 1212a71ada30SJohn Johansen error = PTR_ERR(dentry); 1213a71ada30SJohn Johansen goto out; 1214a71ada30SJohn Johansen } 1215a71ada30SJohn Johansen inode = new_inode(parent->d_inode->i_sb); 1216a71ada30SJohn Johansen if (!inode) { 1217a71ada30SJohn Johansen error = -ENOMEM; 1218a71ada30SJohn Johansen goto out1; 1219a71ada30SJohn Johansen } 1220a71ada30SJohn Johansen 1221a71ada30SJohn Johansen inode->i_ino = get_next_ino(); 1222a71ada30SJohn Johansen inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; 1223a71ada30SJohn Johansen inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 1224a71ada30SJohn Johansen init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, 1225a71ada30SJohn Johansen MKDEV(MEM_MAJOR, 3)); 1226a71ada30SJohn Johansen d_instantiate(dentry, inode); 1227a71ada30SJohn Johansen aa_null.dentry = dget(dentry); 1228a71ada30SJohn Johansen aa_null.mnt = mntget(mount); 1229a71ada30SJohn Johansen 1230a71ada30SJohn Johansen error = 0; 1231a71ada30SJohn Johansen 1232a71ada30SJohn Johansen out1: 1233a71ada30SJohn Johansen dput(dentry); 1234a71ada30SJohn Johansen out: 1235a71ada30SJohn Johansen inode_unlock(d_inode(parent)); 1236a71ada30SJohn Johansen simple_release_fs(&mount, &count); 1237a71ada30SJohn Johansen return error; 1238a71ada30SJohn Johansen } 1239a71ada30SJohn Johansen 124063e2b423SJohn Johansen /** 124163e2b423SJohn Johansen * aa_create_aafs - create the apparmor security filesystem 124263e2b423SJohn Johansen * 124363e2b423SJohn Johansen * dentries created here are released by aa_destroy_aafs 124463e2b423SJohn Johansen * 124563e2b423SJohn Johansen * Returns: error on failure 124663e2b423SJohn Johansen */ 12473417d8d5SJames Morris static int __init aa_create_aafs(void) 124863e2b423SJohn Johansen { 1249*b7fd2c03SJohn Johansen struct dentry *dent; 125063e2b423SJohn Johansen int error; 125163e2b423SJohn Johansen 125263e2b423SJohn Johansen if (!apparmor_initialized) 125363e2b423SJohn Johansen return 0; 125463e2b423SJohn Johansen 12559acd494bSKees Cook if (aa_fs_entry.dentry) { 125663e2b423SJohn Johansen AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 125763e2b423SJohn Johansen return -EEXIST; 125863e2b423SJohn Johansen } 125963e2b423SJohn Johansen 12609acd494bSKees Cook /* Populate fs tree. */ 12619acd494bSKees Cook error = aafs_create_dir(&aa_fs_entry, NULL); 126263e2b423SJohn Johansen if (error) 126363e2b423SJohn Johansen goto error; 126463e2b423SJohn Johansen 1265*b7fd2c03SJohn Johansen dent = securityfs_create_file(".load", 0666, aa_fs_entry.dentry, 1266*b7fd2c03SJohn Johansen NULL, &aa_fs_profile_load); 1267*b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 1268*b7fd2c03SJohn Johansen error = PTR_ERR(dent); 1269*b7fd2c03SJohn Johansen goto error; 1270*b7fd2c03SJohn Johansen } 1271*b7fd2c03SJohn Johansen ns_subload(root_ns) = dent; 1272*b7fd2c03SJohn Johansen 1273*b7fd2c03SJohn Johansen dent = securityfs_create_file(".replace", 0666, aa_fs_entry.dentry, 1274*b7fd2c03SJohn Johansen NULL, &aa_fs_profile_replace); 1275*b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 1276*b7fd2c03SJohn Johansen error = PTR_ERR(dent); 1277*b7fd2c03SJohn Johansen goto error; 1278*b7fd2c03SJohn Johansen } 1279*b7fd2c03SJohn Johansen ns_subreplace(root_ns) = dent; 1280*b7fd2c03SJohn Johansen 1281*b7fd2c03SJohn Johansen dent = securityfs_create_file(".remove", 0666, aa_fs_entry.dentry, 1282*b7fd2c03SJohn Johansen NULL, &aa_fs_profile_remove); 1283*b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 1284*b7fd2c03SJohn Johansen error = PTR_ERR(dent); 1285*b7fd2c03SJohn Johansen goto error; 1286*b7fd2c03SJohn Johansen } 1287*b7fd2c03SJohn Johansen ns_subremove(root_ns) = dent; 1288*b7fd2c03SJohn Johansen 1289*b7fd2c03SJohn Johansen mutex_lock(&root_ns->lock); 129098849dffSJohn Johansen error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy"); 1291*b7fd2c03SJohn Johansen mutex_unlock(&root_ns->lock); 1292*b7fd2c03SJohn Johansen 12930d259f04SJohn Johansen if (error) 12940d259f04SJohn Johansen goto error; 12950d259f04SJohn Johansen 1296a71ada30SJohn Johansen error = aa_mk_null_file(aa_fs_entry.dentry); 1297a71ada30SJohn Johansen if (error) 1298a71ada30SJohn Johansen goto error; 1299a71ada30SJohn Johansen 1300a71ada30SJohn Johansen /* TODO: add default profile to apparmorfs */ 130163e2b423SJohn Johansen 130263e2b423SJohn Johansen /* Report that AppArmor fs is enabled */ 130363e2b423SJohn Johansen aa_info_message("AppArmor Filesystem Enabled"); 130463e2b423SJohn Johansen return 0; 130563e2b423SJohn Johansen 130663e2b423SJohn Johansen error: 130763e2b423SJohn Johansen aa_destroy_aafs(); 130863e2b423SJohn Johansen AA_ERROR("Error creating AppArmor securityfs\n"); 130963e2b423SJohn Johansen return error; 131063e2b423SJohn Johansen } 131163e2b423SJohn Johansen 131263e2b423SJohn Johansen fs_initcall(aa_create_aafs); 1313