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> 18876979c9SPaul Gortmaker #include <linux/init.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 <linux/fs.h> 26*b0ecc9daSDavid Howells #include <linux/fs_context.h> 27d9bf2c26SJohn Johansen #include <linux/poll.h> 28a481f4d9SJohn Johansen #include <uapi/linux/major.h> 29a481f4d9SJohn Johansen #include <uapi/linux/magic.h> 3063e2b423SJohn Johansen 3163e2b423SJohn Johansen #include "include/apparmor.h" 3263e2b423SJohn Johansen #include "include/apparmorfs.h" 3363e2b423SJohn Johansen #include "include/audit.h" 34d8889d49SJohn Johansen #include "include/cred.h" 35f8eb8a13SJohn Johansen #include "include/crypto.h" 36cd1dbf76SJohn Johansen #include "include/ipc.h" 37317d9a05SJohn Johansen #include "include/label.h" 3863e2b423SJohn Johansen #include "include/policy.h" 39cff281f6SJohn Johansen #include "include/policy_ns.h" 40d384b0a1SKees Cook #include "include/resource.h" 415ac8c355SJohn Johansen #include "include/policy_unpack.h" 4263e2b423SJohn Johansen 43c97204baSJohn Johansen /* 44c97204baSJohn Johansen * The apparmor filesystem interface used for policy load and introspection 45c97204baSJohn Johansen * The interface is split into two main components based on their function 46c97204baSJohn Johansen * a securityfs component: 47c97204baSJohn Johansen * used for static files that are always available, and which allows 48c97204baSJohn Johansen * userspace to specificy the location of the security filesystem. 49c97204baSJohn Johansen * 50c97204baSJohn Johansen * fns and data are prefixed with 51c97204baSJohn Johansen * aa_sfs_ 52c97204baSJohn Johansen * 53c97204baSJohn Johansen * an apparmorfs component: 54c97204baSJohn Johansen * used loaded policy content and introspection. It is not part of a 55c97204baSJohn Johansen * regular mounted filesystem and is available only through the magic 56c97204baSJohn Johansen * policy symlink in the root of the securityfs apparmor/ directory. 57c97204baSJohn Johansen * Tasks queries will be magically redirected to the correct portion 58c97204baSJohn Johansen * of the policy tree based on their confinement. 59c97204baSJohn Johansen * 60c97204baSJohn Johansen * fns and data are prefixed with 61c97204baSJohn Johansen * aafs_ 62c97204baSJohn Johansen * 63c97204baSJohn Johansen * The aa_fs_ prefix is used to indicate the fn is used by both the 64c97204baSJohn Johansen * securityfs and apparmorfs filesystems. 65c97204baSJohn Johansen */ 66c97204baSJohn Johansen 67c97204baSJohn Johansen 68c97204baSJohn Johansen /* 69c97204baSJohn Johansen * support fns 70c97204baSJohn Johansen */ 71c97204baSJohn Johansen 7263e2b423SJohn Johansen /** 730d259f04SJohn Johansen * aa_mangle_name - mangle a profile name to std profile layout form 740d259f04SJohn Johansen * @name: profile name to mangle (NOT NULL) 750d259f04SJohn Johansen * @target: buffer to store mangled name, same length as @name (MAYBE NULL) 760d259f04SJohn Johansen * 770d259f04SJohn Johansen * Returns: length of mangled name 780d259f04SJohn Johansen */ 79bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target) 800d259f04SJohn Johansen { 810d259f04SJohn Johansen char *t = target; 820d259f04SJohn Johansen 830d259f04SJohn Johansen while (*name == '/' || *name == '.') 840d259f04SJohn Johansen name++; 850d259f04SJohn Johansen 860d259f04SJohn Johansen if (target) { 870d259f04SJohn Johansen for (; *name; name++) { 880d259f04SJohn Johansen if (*name == '/') 890d259f04SJohn Johansen *(t)++ = '.'; 900d259f04SJohn Johansen else if (isspace(*name)) 910d259f04SJohn Johansen *(t)++ = '_'; 920d259f04SJohn Johansen else if (isalnum(*name) || strchr("._-", *name)) 930d259f04SJohn Johansen *(t)++ = *name; 940d259f04SJohn Johansen } 950d259f04SJohn Johansen 960d259f04SJohn Johansen *t = 0; 970d259f04SJohn Johansen } else { 980d259f04SJohn Johansen int len = 0; 990d259f04SJohn Johansen for (; *name; name++) { 1000d259f04SJohn Johansen if (isalnum(*name) || isspace(*name) || 1010d259f04SJohn Johansen strchr("/._-", *name)) 1020d259f04SJohn Johansen len++; 1030d259f04SJohn Johansen } 1040d259f04SJohn Johansen 1050d259f04SJohn Johansen return len; 1060d259f04SJohn Johansen } 1070d259f04SJohn Johansen 1080d259f04SJohn Johansen return t - target; 1090d259f04SJohn Johansen } 1100d259f04SJohn Johansen 111a481f4d9SJohn Johansen 112a481f4d9SJohn Johansen /* 113a481f4d9SJohn Johansen * aafs - core fns and data for the policy tree 114a481f4d9SJohn Johansen */ 115a481f4d9SJohn Johansen 116a481f4d9SJohn Johansen #define AAFS_NAME "apparmorfs" 117a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt; 118a481f4d9SJohn Johansen static int aafs_count; 119a481f4d9SJohn Johansen 120a481f4d9SJohn Johansen 121a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry) 122a481f4d9SJohn Johansen { 123a0781209SJohn Johansen seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino); 124a481f4d9SJohn Johansen return 0; 125a481f4d9SJohn Johansen } 126a481f4d9SJohn Johansen 12727afa27dSAl Viro static void aafs_free_inode(struct inode *inode) 128a481f4d9SJohn Johansen { 129a481f4d9SJohn Johansen if (S_ISLNK(inode->i_mode)) 130a481f4d9SJohn Johansen kfree(inode->i_link); 131f51dcd0fSAl Viro free_inode_nonrcu(inode); 132f51dcd0fSAl Viro } 133f51dcd0fSAl Viro 134a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = { 135a481f4d9SJohn Johansen .statfs = simple_statfs, 13627afa27dSAl Viro .free_inode = aafs_free_inode, 137a481f4d9SJohn Johansen .show_path = aafs_show_path, 138a481f4d9SJohn Johansen }; 139a481f4d9SJohn Johansen 140*b0ecc9daSDavid Howells static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc) 141a481f4d9SJohn Johansen { 142a481f4d9SJohn Johansen static struct tree_descr files[] = { {""} }; 143a481f4d9SJohn Johansen int error; 144a481f4d9SJohn Johansen 145a481f4d9SJohn Johansen error = simple_fill_super(sb, AAFS_MAGIC, files); 146a481f4d9SJohn Johansen if (error) 147a481f4d9SJohn Johansen return error; 148a481f4d9SJohn Johansen sb->s_op = &aafs_super_ops; 149a481f4d9SJohn Johansen 150a481f4d9SJohn Johansen return 0; 151a481f4d9SJohn Johansen } 152a481f4d9SJohn Johansen 153*b0ecc9daSDavid Howells static int apparmorfs_get_tree(struct fs_context *fc) 154a481f4d9SJohn Johansen { 155*b0ecc9daSDavid Howells return get_tree_single(fc, apparmorfs_fill_super); 156*b0ecc9daSDavid Howells } 157*b0ecc9daSDavid Howells 158*b0ecc9daSDavid Howells static const struct fs_context_operations apparmorfs_context_ops = { 159*b0ecc9daSDavid Howells .get_tree = apparmorfs_get_tree, 160*b0ecc9daSDavid Howells }; 161*b0ecc9daSDavid Howells 162*b0ecc9daSDavid Howells static int apparmorfs_init_fs_context(struct fs_context *fc) 163*b0ecc9daSDavid Howells { 164*b0ecc9daSDavid Howells fc->ops = &apparmorfs_context_ops; 165*b0ecc9daSDavid Howells return 0; 166a481f4d9SJohn Johansen } 167a481f4d9SJohn Johansen 168a481f4d9SJohn Johansen static struct file_system_type aafs_ops = { 169a481f4d9SJohn Johansen .owner = THIS_MODULE, 170a481f4d9SJohn Johansen .name = AAFS_NAME, 171*b0ecc9daSDavid Howells .init_fs_context = apparmorfs_init_fs_context, 172a481f4d9SJohn Johansen .kill_sb = kill_anon_super, 173a481f4d9SJohn Johansen }; 174a481f4d9SJohn Johansen 175a481f4d9SJohn Johansen /** 176a481f4d9SJohn Johansen * __aafs_setup_d_inode - basic inode setup for apparmorfs 177a481f4d9SJohn Johansen * @dir: parent directory for the dentry 178a481f4d9SJohn Johansen * @dentry: dentry we are seting the inode up for 179a481f4d9SJohn Johansen * @mode: permissions the file should have 180a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 181a481f4d9SJohn Johansen * @link: if symlink, symlink target string 182a481f4d9SJohn Johansen * @fops: struct file_operations that should be used 183a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 184a481f4d9SJohn Johansen */ 185a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry, 186a481f4d9SJohn Johansen umode_t mode, void *data, char *link, 187a481f4d9SJohn Johansen const struct file_operations *fops, 188a481f4d9SJohn Johansen const struct inode_operations *iops) 189a481f4d9SJohn Johansen { 190a481f4d9SJohn Johansen struct inode *inode = new_inode(dir->i_sb); 191a481f4d9SJohn Johansen 192a481f4d9SJohn Johansen AA_BUG(!dir); 193a481f4d9SJohn Johansen AA_BUG(!dentry); 194a481f4d9SJohn Johansen 195a481f4d9SJohn Johansen if (!inode) 196a481f4d9SJohn Johansen return -ENOMEM; 197a481f4d9SJohn Johansen 198a481f4d9SJohn Johansen inode->i_ino = get_next_ino(); 199a481f4d9SJohn Johansen inode->i_mode = mode; 200a481f4d9SJohn Johansen inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 201a481f4d9SJohn Johansen inode->i_private = data; 202a481f4d9SJohn Johansen if (S_ISDIR(mode)) { 203a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_dir_inode_operations; 204a481f4d9SJohn Johansen inode->i_fop = &simple_dir_operations; 205a481f4d9SJohn Johansen inc_nlink(inode); 206a481f4d9SJohn Johansen inc_nlink(dir); 207a481f4d9SJohn Johansen } else if (S_ISLNK(mode)) { 208a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_symlink_inode_operations; 209a481f4d9SJohn Johansen inode->i_link = link; 210a481f4d9SJohn Johansen } else { 211a481f4d9SJohn Johansen inode->i_fop = fops; 212a481f4d9SJohn Johansen } 213a481f4d9SJohn Johansen d_instantiate(dentry, inode); 214a481f4d9SJohn Johansen dget(dentry); 215a481f4d9SJohn Johansen 216a481f4d9SJohn Johansen return 0; 217a481f4d9SJohn Johansen } 218a481f4d9SJohn Johansen 219a481f4d9SJohn Johansen /** 220a481f4d9SJohn Johansen * aafs_create - create a dentry in the apparmorfs filesystem 221a481f4d9SJohn Johansen * 222a481f4d9SJohn Johansen * @name: name of dentry to create 223a481f4d9SJohn Johansen * @mode: permissions the file should have 224a481f4d9SJohn Johansen * @parent: parent directory for this dentry 225a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 226a481f4d9SJohn Johansen * @link: if symlink, symlink target string 227a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 228a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 229a481f4d9SJohn Johansen * 230a481f4d9SJohn Johansen * This is the basic "create a xxx" function for apparmorfs. 231a481f4d9SJohn Johansen * 232a481f4d9SJohn Johansen * Returns a pointer to a dentry if it succeeds, that must be free with 233a481f4d9SJohn Johansen * aafs_remove(). Will return ERR_PTR on failure. 234a481f4d9SJohn Johansen */ 235a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode, 236a481f4d9SJohn Johansen struct dentry *parent, void *data, void *link, 237a481f4d9SJohn Johansen const struct file_operations *fops, 238a481f4d9SJohn Johansen const struct inode_operations *iops) 239a481f4d9SJohn Johansen { 240a481f4d9SJohn Johansen struct dentry *dentry; 241a481f4d9SJohn Johansen struct inode *dir; 242a481f4d9SJohn Johansen int error; 243a481f4d9SJohn Johansen 244a481f4d9SJohn Johansen AA_BUG(!name); 245a481f4d9SJohn Johansen AA_BUG(!parent); 246a481f4d9SJohn Johansen 247a481f4d9SJohn Johansen if (!(mode & S_IFMT)) 248a481f4d9SJohn Johansen mode = (mode & S_IALLUGO) | S_IFREG; 249a481f4d9SJohn Johansen 250a481f4d9SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 251a481f4d9SJohn Johansen if (error) 252a481f4d9SJohn Johansen return ERR_PTR(error); 253a481f4d9SJohn Johansen 254a481f4d9SJohn Johansen dir = d_inode(parent); 255a481f4d9SJohn Johansen 256a481f4d9SJohn Johansen inode_lock(dir); 257a481f4d9SJohn Johansen dentry = lookup_one_len(name, parent, strlen(name)); 2585d314a81SDan Carpenter if (IS_ERR(dentry)) { 2595d314a81SDan Carpenter error = PTR_ERR(dentry); 260a481f4d9SJohn Johansen goto fail_lock; 2615d314a81SDan Carpenter } 262a481f4d9SJohn Johansen 263a481f4d9SJohn Johansen if (d_really_is_positive(dentry)) { 264a481f4d9SJohn Johansen error = -EEXIST; 265a481f4d9SJohn Johansen goto fail_dentry; 266a481f4d9SJohn Johansen } 267a481f4d9SJohn Johansen 268a481f4d9SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops); 269a481f4d9SJohn Johansen if (error) 270a481f4d9SJohn Johansen goto fail_dentry; 271a481f4d9SJohn Johansen inode_unlock(dir); 272a481f4d9SJohn Johansen 273a481f4d9SJohn Johansen return dentry; 274a481f4d9SJohn Johansen 275a481f4d9SJohn Johansen fail_dentry: 276a481f4d9SJohn Johansen dput(dentry); 277a481f4d9SJohn Johansen 278a481f4d9SJohn Johansen fail_lock: 279a481f4d9SJohn Johansen inode_unlock(dir); 280a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 281a481f4d9SJohn Johansen 282a481f4d9SJohn Johansen return ERR_PTR(error); 283a481f4d9SJohn Johansen } 284a481f4d9SJohn Johansen 285a481f4d9SJohn Johansen /** 286a481f4d9SJohn Johansen * aafs_create_file - create a file in the apparmorfs filesystem 287a481f4d9SJohn Johansen * 288a481f4d9SJohn Johansen * @name: name of dentry to create 289a481f4d9SJohn Johansen * @mode: permissions the file should have 290a481f4d9SJohn Johansen * @parent: parent directory for this dentry 291a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 292a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 293a481f4d9SJohn Johansen * 294a481f4d9SJohn Johansen * see aafs_create 295a481f4d9SJohn Johansen */ 296a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode, 297a481f4d9SJohn Johansen struct dentry *parent, void *data, 298a481f4d9SJohn Johansen const struct file_operations *fops) 299a481f4d9SJohn Johansen { 300a481f4d9SJohn Johansen return aafs_create(name, mode, parent, data, NULL, fops, NULL); 301a481f4d9SJohn Johansen } 302a481f4d9SJohn Johansen 303a481f4d9SJohn Johansen /** 304a481f4d9SJohn Johansen * aafs_create_dir - create a directory in the apparmorfs filesystem 305a481f4d9SJohn Johansen * 306a481f4d9SJohn Johansen * @name: name of dentry to create 307a481f4d9SJohn Johansen * @parent: parent directory for this dentry 308a481f4d9SJohn Johansen * 309a481f4d9SJohn Johansen * see aafs_create 310a481f4d9SJohn Johansen */ 311a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent) 312a481f4d9SJohn Johansen { 313a481f4d9SJohn Johansen return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL, 314a481f4d9SJohn Johansen NULL); 315a481f4d9SJohn Johansen } 316a481f4d9SJohn Johansen 317a481f4d9SJohn Johansen /** 318a481f4d9SJohn Johansen * aafs_create_symlink - create a symlink in the apparmorfs filesystem 319a481f4d9SJohn Johansen * @name: name of dentry to create 320a481f4d9SJohn Johansen * @parent: parent directory for this dentry 321a481f4d9SJohn Johansen * @target: if symlink, symlink target string 3221180b4c7SJohn Johansen * @private: private data 323a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 324a481f4d9SJohn Johansen * 325a481f4d9SJohn Johansen * If @target parameter is %NULL, then the @iops parameter needs to be 326a481f4d9SJohn Johansen * setup to handle .readlink and .get_link inode_operations. 327a481f4d9SJohn Johansen */ 328a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name, 329a481f4d9SJohn Johansen struct dentry *parent, 330a481f4d9SJohn Johansen const char *target, 3311180b4c7SJohn Johansen void *private, 332a481f4d9SJohn Johansen const struct inode_operations *iops) 333a481f4d9SJohn Johansen { 334a481f4d9SJohn Johansen struct dentry *dent; 335a481f4d9SJohn Johansen char *link = NULL; 336a481f4d9SJohn Johansen 337a481f4d9SJohn Johansen if (target) { 338a481f4d9SJohn Johansen if (!link) 339a481f4d9SJohn Johansen return ERR_PTR(-ENOMEM); 340a481f4d9SJohn Johansen } 3411180b4c7SJohn Johansen dent = aafs_create(name, S_IFLNK | 0444, parent, private, link, NULL, 342a481f4d9SJohn Johansen iops); 343a481f4d9SJohn Johansen if (IS_ERR(dent)) 344a481f4d9SJohn Johansen kfree(link); 345a481f4d9SJohn Johansen 346a481f4d9SJohn Johansen return dent; 347a481f4d9SJohn Johansen } 348a481f4d9SJohn Johansen 349a481f4d9SJohn Johansen /** 350a481f4d9SJohn Johansen * aafs_remove - removes a file or directory from the apparmorfs filesystem 351a481f4d9SJohn Johansen * 352a481f4d9SJohn Johansen * @dentry: dentry of the file/directory/symlink to removed. 353a481f4d9SJohn Johansen */ 354a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry) 355a481f4d9SJohn Johansen { 356a481f4d9SJohn Johansen struct inode *dir; 357a481f4d9SJohn Johansen 358a481f4d9SJohn Johansen if (!dentry || IS_ERR(dentry)) 359a481f4d9SJohn Johansen return; 360a481f4d9SJohn Johansen 361a481f4d9SJohn Johansen dir = d_inode(dentry->d_parent); 362a481f4d9SJohn Johansen inode_lock(dir); 363a481f4d9SJohn Johansen if (simple_positive(dentry)) { 364a481f4d9SJohn Johansen if (d_is_dir(dentry)) 365a481f4d9SJohn Johansen simple_rmdir(dir, dentry); 366a481f4d9SJohn Johansen else 367a481f4d9SJohn Johansen simple_unlink(dir, dentry); 368201218e4SChris Coulson d_delete(dentry); 369a481f4d9SJohn Johansen dput(dentry); 370a481f4d9SJohn Johansen } 371a481f4d9SJohn Johansen inode_unlock(dir); 372a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 373a481f4d9SJohn Johansen } 374a481f4d9SJohn Johansen 375a481f4d9SJohn Johansen 376a481f4d9SJohn Johansen /* 377a481f4d9SJohn Johansen * aa_fs - policy load/replace/remove 378a481f4d9SJohn Johansen */ 379a481f4d9SJohn Johansen 3800d259f04SJohn Johansen /** 38163e2b423SJohn Johansen * aa_simple_write_to_buffer - common routine for getting policy from user 38263e2b423SJohn Johansen * @userbuf: user buffer to copy data from (NOT NULL) 3833ed02adaSJohn Johansen * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 38463e2b423SJohn Johansen * @copy_size: size of data to copy from user buffer 38563e2b423SJohn Johansen * @pos: position write is at in the file (NOT NULL) 38663e2b423SJohn Johansen * 38763e2b423SJohn Johansen * Returns: kernel buffer containing copy of user buffer data or an 38863e2b423SJohn Johansen * ERR_PTR on failure. 38963e2b423SJohn Johansen */ 3905ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, 3915ac8c355SJohn Johansen size_t alloc_size, 3925ac8c355SJohn Johansen size_t copy_size, 39363e2b423SJohn Johansen loff_t *pos) 39463e2b423SJohn Johansen { 3955ac8c355SJohn Johansen struct aa_loaddata *data; 39663e2b423SJohn Johansen 397e6bfa25dSJohn Johansen AA_BUG(copy_size > alloc_size); 3983ed02adaSJohn Johansen 39963e2b423SJohn Johansen if (*pos != 0) 40063e2b423SJohn Johansen /* only writes from pos 0, that is complete writes */ 40163e2b423SJohn Johansen return ERR_PTR(-ESPIPE); 40263e2b423SJohn Johansen 40363e2b423SJohn Johansen /* freed by caller to simple_write_to_buffer */ 4045d5182caSJohn Johansen data = aa_loaddata_alloc(alloc_size); 4055d5182caSJohn Johansen if (IS_ERR(data)) 4065d5182caSJohn Johansen return data; 40763e2b423SJohn Johansen 4085d5182caSJohn Johansen data->size = copy_size; 4095ac8c355SJohn Johansen if (copy_from_user(data->data, userbuf, copy_size)) { 41063e2b423SJohn Johansen kvfree(data); 41163e2b423SJohn Johansen return ERR_PTR(-EFAULT); 41263e2b423SJohn Johansen } 41363e2b423SJohn Johansen 41463e2b423SJohn Johansen return data; 41563e2b423SJohn Johansen } 41663e2b423SJohn Johansen 41718e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size, 418b7fd2c03SJohn Johansen loff_t *pos, struct aa_ns *ns) 4195ac8c355SJohn Johansen { 4205ac8c355SJohn Johansen struct aa_loaddata *data; 421637f688dSJohn Johansen struct aa_label *label; 422637f688dSJohn Johansen ssize_t error; 423cf797c0eSJohn Johansen 424637f688dSJohn Johansen label = begin_current_label_crit_section(); 425cf797c0eSJohn Johansen 4265ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4275ac8c355SJohn Johansen * below after unpack 4285ac8c355SJohn Johansen */ 429637f688dSJohn Johansen error = aa_may_manage_policy(label, ns, mask); 4305ac8c355SJohn Johansen if (error) 4315ac8c355SJohn Johansen return error; 43263e2b423SJohn Johansen 4335ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size, size, pos); 4345ac8c355SJohn Johansen error = PTR_ERR(data); 4355ac8c355SJohn Johansen if (!IS_ERR(data)) { 436637f688dSJohn Johansen error = aa_replace_profiles(ns, label, mask, data); 4375ac8c355SJohn Johansen aa_put_loaddata(data); 4385ac8c355SJohn Johansen } 439637f688dSJohn Johansen end_current_label_crit_section(label); 4405ac8c355SJohn Johansen 4415ac8c355SJohn Johansen return error; 4425ac8c355SJohn Johansen } 4435ac8c355SJohn Johansen 444b7fd2c03SJohn Johansen /* .load file hook fn to load policy */ 44563e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 44663e2b423SJohn Johansen loff_t *pos) 44763e2b423SJohn Johansen { 448b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 44918e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns); 450b7fd2c03SJohn Johansen 451b7fd2c03SJohn Johansen aa_put_ns(ns); 45263e2b423SJohn Johansen 45363e2b423SJohn Johansen return error; 45463e2b423SJohn Johansen } 45563e2b423SJohn Johansen 45663e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = { 4576038f373SArnd Bergmann .write = profile_load, 4586038f373SArnd Bergmann .llseek = default_llseek, 45963e2b423SJohn Johansen }; 46063e2b423SJohn Johansen 46163e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */ 46263e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf, 46363e2b423SJohn Johansen size_t size, loff_t *pos) 46463e2b423SJohn Johansen { 465b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 46618e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY, 46718e99f19SJohn Johansen buf, size, pos, ns); 468b7fd2c03SJohn Johansen aa_put_ns(ns); 46963e2b423SJohn Johansen 47063e2b423SJohn Johansen return error; 47163e2b423SJohn Johansen } 47263e2b423SJohn Johansen 47363e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = { 4746038f373SArnd Bergmann .write = profile_replace, 4756038f373SArnd Bergmann .llseek = default_llseek, 47663e2b423SJohn Johansen }; 47763e2b423SJohn Johansen 478b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */ 47963e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf, 48063e2b423SJohn Johansen size_t size, loff_t *pos) 48163e2b423SJohn Johansen { 4825ac8c355SJohn Johansen struct aa_loaddata *data; 483637f688dSJohn Johansen struct aa_label *label; 48463e2b423SJohn Johansen ssize_t error; 485b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 48663e2b423SJohn Johansen 487637f688dSJohn Johansen label = begin_current_label_crit_section(); 4885ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4895ac8c355SJohn Johansen * below after unpack 4905ac8c355SJohn Johansen */ 491637f688dSJohn Johansen error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY); 4925ac8c355SJohn Johansen if (error) 4935ac8c355SJohn Johansen goto out; 4945ac8c355SJohn Johansen 49563e2b423SJohn Johansen /* 49663e2b423SJohn Johansen * aa_remove_profile needs a null terminated string so 1 extra 49763e2b423SJohn Johansen * byte is allocated and the copied data is null terminated. 49863e2b423SJohn Johansen */ 4995ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size + 1, size, pos); 50063e2b423SJohn Johansen 50163e2b423SJohn Johansen error = PTR_ERR(data); 50263e2b423SJohn Johansen if (!IS_ERR(data)) { 5035ac8c355SJohn Johansen data->data[size] = 0; 504637f688dSJohn Johansen error = aa_remove_profiles(ns, label, data->data, size); 5055ac8c355SJohn Johansen aa_put_loaddata(data); 50663e2b423SJohn Johansen } 5075ac8c355SJohn Johansen out: 508637f688dSJohn Johansen end_current_label_crit_section(label); 509b7fd2c03SJohn Johansen aa_put_ns(ns); 51063e2b423SJohn Johansen return error; 51163e2b423SJohn Johansen } 51263e2b423SJohn Johansen 51363e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = { 5146038f373SArnd Bergmann .write = profile_remove, 5156038f373SArnd Bergmann .llseek = default_llseek, 51663e2b423SJohn Johansen }; 51763e2b423SJohn Johansen 518d9bf2c26SJohn Johansen struct aa_revision { 519d9bf2c26SJohn Johansen struct aa_ns *ns; 520d9bf2c26SJohn Johansen long last_read; 521d9bf2c26SJohn Johansen }; 522d9bf2c26SJohn Johansen 523d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */ 524d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file) 525d9bf2c26SJohn Johansen { 526d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 527d9bf2c26SJohn Johansen 528d9bf2c26SJohn Johansen if (rev) { 529d9bf2c26SJohn Johansen aa_put_ns(rev->ns); 530d9bf2c26SJohn Johansen kfree(rev); 531d9bf2c26SJohn Johansen } 532d9bf2c26SJohn Johansen 533d9bf2c26SJohn Johansen return 0; 534d9bf2c26SJohn Johansen } 535d9bf2c26SJohn Johansen 536d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf, 537d9bf2c26SJohn Johansen size_t size, loff_t *ppos) 538d9bf2c26SJohn Johansen { 539d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 540d9bf2c26SJohn Johansen char buffer[32]; 541d9bf2c26SJohn Johansen long last_read; 542d9bf2c26SJohn Johansen int avail; 543d9bf2c26SJohn Johansen 544feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 545d9bf2c26SJohn Johansen last_read = rev->last_read; 546d9bf2c26SJohn Johansen if (last_read == rev->ns->revision) { 547d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 548d9bf2c26SJohn Johansen if (file->f_flags & O_NONBLOCK) 549d9bf2c26SJohn Johansen return -EAGAIN; 550d9bf2c26SJohn Johansen if (wait_event_interruptible(rev->ns->wait, 551d9bf2c26SJohn Johansen last_read != 552d9bf2c26SJohn Johansen READ_ONCE(rev->ns->revision))) 553d9bf2c26SJohn Johansen return -ERESTARTSYS; 554feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 555d9bf2c26SJohn Johansen } 556d9bf2c26SJohn Johansen 557d9bf2c26SJohn Johansen avail = sprintf(buffer, "%ld\n", rev->ns->revision); 558d9bf2c26SJohn Johansen if (*ppos + size > avail) { 559d9bf2c26SJohn Johansen rev->last_read = rev->ns->revision; 560d9bf2c26SJohn Johansen *ppos = 0; 561d9bf2c26SJohn Johansen } 562d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 563d9bf2c26SJohn Johansen 564d9bf2c26SJohn Johansen return simple_read_from_buffer(buf, size, ppos, buffer, avail); 565d9bf2c26SJohn Johansen } 566d9bf2c26SJohn Johansen 567d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file) 568d9bf2c26SJohn Johansen { 569d9bf2c26SJohn Johansen struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL); 570d9bf2c26SJohn Johansen 571d9bf2c26SJohn Johansen if (!rev) 572d9bf2c26SJohn Johansen return -ENOMEM; 573d9bf2c26SJohn Johansen 574d9bf2c26SJohn Johansen rev->ns = aa_get_ns(inode->i_private); 575d9bf2c26SJohn Johansen if (!rev->ns) 576d9bf2c26SJohn Johansen rev->ns = aa_get_current_ns(); 577d9bf2c26SJohn Johansen file->private_data = rev; 578d9bf2c26SJohn Johansen 579d9bf2c26SJohn Johansen return 0; 580d9bf2c26SJohn Johansen } 581d9bf2c26SJohn Johansen 582e6c5a7d9SAl Viro static __poll_t ns_revision_poll(struct file *file, poll_table *pt) 583d9bf2c26SJohn Johansen { 584d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 585e6c5a7d9SAl Viro __poll_t mask = 0; 586d9bf2c26SJohn Johansen 587d9bf2c26SJohn Johansen if (rev) { 588feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 589d9bf2c26SJohn Johansen poll_wait(file, &rev->ns->wait, pt); 590d9bf2c26SJohn Johansen if (rev->last_read < rev->ns->revision) 591a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 592d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 593d9bf2c26SJohn Johansen } 594d9bf2c26SJohn Johansen 595d9bf2c26SJohn Johansen return mask; 596d9bf2c26SJohn Johansen } 597d9bf2c26SJohn Johansen 5985d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns) 5995d5182caSJohn Johansen { 6005d5182caSJohn Johansen ns->revision++; 601d9bf2c26SJohn Johansen wake_up_interruptible(&ns->wait); 6025d5182caSJohn Johansen } 6035d5182caSJohn Johansen 604d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = { 605d9bf2c26SJohn Johansen .owner = THIS_MODULE, 606d9bf2c26SJohn Johansen .open = ns_revision_open, 607d9bf2c26SJohn Johansen .poll = ns_revision_poll, 608d9bf2c26SJohn Johansen .read = ns_revision_read, 609d9bf2c26SJohn Johansen .llseek = generic_file_llseek, 610d9bf2c26SJohn Johansen .release = ns_revision_release, 611d9bf2c26SJohn Johansen }; 612d9bf2c26SJohn Johansen 6134f3b3f2dSJohn Johansen static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms, 6144f3b3f2dSJohn Johansen const char *match_str, size_t match_len) 6154f3b3f2dSJohn Johansen { 616f4585bc2STyler Hicks struct aa_perms tmp = { }; 6174f3b3f2dSJohn Johansen struct aa_dfa *dfa; 6184f3b3f2dSJohn Johansen unsigned int state = 0; 6194f3b3f2dSJohn Johansen 620637f688dSJohn Johansen if (profile_unconfined(profile)) 6214f3b3f2dSJohn Johansen return; 6224f3b3f2dSJohn Johansen if (profile->file.dfa && *match_str == AA_CLASS_FILE) { 6234f3b3f2dSJohn Johansen dfa = profile->file.dfa; 6244f3b3f2dSJohn Johansen state = aa_dfa_match_len(dfa, profile->file.start, 6254f3b3f2dSJohn Johansen match_str + 1, match_len - 1); 6264f3b3f2dSJohn Johansen if (state) { 6274f3b3f2dSJohn Johansen struct path_cond cond = { }; 6284f3b3f2dSJohn Johansen 6294f3b3f2dSJohn Johansen tmp = aa_compute_fperms(dfa, state, &cond); 6304f3b3f2dSJohn Johansen } 6314f3b3f2dSJohn Johansen } else if (profile->policy.dfa) { 632b9590ad4SJohn Johansen if (!PROFILE_MEDIATES(profile, *match_str)) 6334f3b3f2dSJohn Johansen return; /* no change to current perms */ 6344f3b3f2dSJohn Johansen dfa = profile->policy.dfa; 6354f3b3f2dSJohn Johansen state = aa_dfa_match_len(dfa, profile->policy.start[0], 6364f3b3f2dSJohn Johansen match_str, match_len); 6374f3b3f2dSJohn Johansen if (state) 6384f3b3f2dSJohn Johansen aa_compute_perms(dfa, state, &tmp); 6394f3b3f2dSJohn Johansen } 6404f3b3f2dSJohn Johansen aa_apply_modes_to_perms(profile, &tmp); 641317d9a05SJohn Johansen aa_perms_accum_raw(perms, &tmp); 6424f3b3f2dSJohn Johansen } 6434f3b3f2dSJohn Johansen 6444f3b3f2dSJohn Johansen 645e025be0fSWilliam Hua /** 646e025be0fSWilliam Hua * query_data - queries a policy and writes its data to buf 647e025be0fSWilliam Hua * @buf: the resulting data is stored here (NOT NULL) 648e025be0fSWilliam Hua * @buf_len: size of buf 649e025be0fSWilliam Hua * @query: query string used to retrieve data 650e025be0fSWilliam Hua * @query_len: size of query including second NUL byte 651e025be0fSWilliam Hua * 652e025be0fSWilliam Hua * The buffers pointed to by buf and query may overlap. The query buffer is 653e025be0fSWilliam Hua * parsed before buf is written to. 654e025be0fSWilliam Hua * 655e025be0fSWilliam Hua * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of 656e025be0fSWilliam Hua * the security confinement context and <KEY> is the name of the data to 657e025be0fSWilliam Hua * retrieve. <LABEL> and <KEY> must not be NUL-terminated. 658e025be0fSWilliam Hua * 659e025be0fSWilliam Hua * Don't expect the contents of buf to be preserved on failure. 660e025be0fSWilliam Hua * 661e025be0fSWilliam Hua * Returns: number of characters written to buf or -errno on failure 662e025be0fSWilliam Hua */ 663e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len, 664e025be0fSWilliam Hua char *query, size_t query_len) 665e025be0fSWilliam Hua { 666e025be0fSWilliam Hua char *out; 667e025be0fSWilliam Hua const char *key; 668317d9a05SJohn Johansen struct label_it i; 669637f688dSJohn Johansen struct aa_label *label, *curr; 670317d9a05SJohn Johansen struct aa_profile *profile; 671e025be0fSWilliam Hua struct aa_data *data; 672e025be0fSWilliam Hua u32 bytes, blocks; 673e025be0fSWilliam Hua __le32 outle32; 674e025be0fSWilliam Hua 675e025be0fSWilliam Hua if (!query_len) 676e025be0fSWilliam Hua return -EINVAL; /* need a query */ 677e025be0fSWilliam Hua 678e025be0fSWilliam Hua key = query + strnlen(query, query_len) + 1; 679e025be0fSWilliam Hua if (key + 1 >= query + query_len) 680e025be0fSWilliam Hua return -EINVAL; /* not enough space for a non-empty key */ 681e025be0fSWilliam Hua if (key + strnlen(key, query + query_len - key) >= query + query_len) 682e025be0fSWilliam Hua return -EINVAL; /* must end with NUL */ 683e025be0fSWilliam Hua 684e025be0fSWilliam Hua if (buf_len < sizeof(bytes) + sizeof(blocks)) 685e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 686e025be0fSWilliam Hua 687637f688dSJohn Johansen curr = begin_current_label_crit_section(); 688637f688dSJohn Johansen label = aa_label_parse(curr, query, GFP_KERNEL, false, false); 689637f688dSJohn Johansen end_current_label_crit_section(curr); 690637f688dSJohn Johansen if (IS_ERR(label)) 691637f688dSJohn Johansen return PTR_ERR(label); 692e025be0fSWilliam Hua 693e025be0fSWilliam Hua /* We are going to leave space for two numbers. The first is the total 694e025be0fSWilliam Hua * number of bytes we are writing after the first number. This is so 695e025be0fSWilliam Hua * users can read the full output without reallocation. 696e025be0fSWilliam Hua * 697e025be0fSWilliam Hua * The second number is the number of data blocks we're writing. An 698e025be0fSWilliam Hua * application might be confined by multiple policies having data in 699e025be0fSWilliam Hua * the same key. 700e025be0fSWilliam Hua */ 701e025be0fSWilliam Hua memset(buf, 0, sizeof(bytes) + sizeof(blocks)); 702e025be0fSWilliam Hua out = buf + sizeof(bytes) + sizeof(blocks); 703e025be0fSWilliam Hua 704e025be0fSWilliam Hua blocks = 0; 705317d9a05SJohn Johansen label_for_each_confined(i, label, profile) { 706317d9a05SJohn Johansen if (!profile->data) 707317d9a05SJohn Johansen continue; 708317d9a05SJohn Johansen 709317d9a05SJohn Johansen data = rhashtable_lookup_fast(profile->data, &key, 710317d9a05SJohn Johansen profile->data->p); 711e025be0fSWilliam Hua 712e025be0fSWilliam Hua if (data) { 713317d9a05SJohn Johansen if (out + sizeof(outle32) + data->size > buf + 714317d9a05SJohn Johansen buf_len) { 715637f688dSJohn Johansen aa_put_label(label); 716e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 717637f688dSJohn Johansen } 718e025be0fSWilliam Hua outle32 = __cpu_to_le32(data->size); 719e025be0fSWilliam Hua memcpy(out, &outle32, sizeof(outle32)); 720e025be0fSWilliam Hua out += sizeof(outle32); 721e025be0fSWilliam Hua memcpy(out, data->data, data->size); 722e025be0fSWilliam Hua out += data->size; 723e025be0fSWilliam Hua blocks++; 724e025be0fSWilliam Hua } 725e025be0fSWilliam Hua } 726637f688dSJohn Johansen aa_put_label(label); 727e025be0fSWilliam Hua 728e025be0fSWilliam Hua outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); 729e025be0fSWilliam Hua memcpy(buf, &outle32, sizeof(outle32)); 730e025be0fSWilliam Hua outle32 = __cpu_to_le32(blocks); 731e025be0fSWilliam Hua memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32)); 732e025be0fSWilliam Hua 733e025be0fSWilliam Hua return out - buf; 734e025be0fSWilliam Hua } 735e025be0fSWilliam Hua 7364f3b3f2dSJohn Johansen /** 7374f3b3f2dSJohn Johansen * query_label - queries a label and writes permissions to buf 7384f3b3f2dSJohn Johansen * @buf: the resulting permissions string is stored here (NOT NULL) 7394f3b3f2dSJohn Johansen * @buf_len: size of buf 7404f3b3f2dSJohn Johansen * @query: binary query string to match against the dfa 7414f3b3f2dSJohn Johansen * @query_len: size of query 7424f3b3f2dSJohn Johansen * @view_only: only compute for querier's view 7434f3b3f2dSJohn Johansen * 7444f3b3f2dSJohn Johansen * The buffers pointed to by buf and query may overlap. The query buffer is 7454f3b3f2dSJohn Johansen * parsed before buf is written to. 7464f3b3f2dSJohn Johansen * 7474f3b3f2dSJohn Johansen * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is 7484f3b3f2dSJohn Johansen * the name of the label, in the current namespace, that is to be queried and 7494f3b3f2dSJohn Johansen * DFA_STRING is a binary string to match against the label(s)'s DFA. 7504f3b3f2dSJohn Johansen * 7514f3b3f2dSJohn Johansen * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters 7524f3b3f2dSJohn Johansen * but must *not* be NUL terminated. 7534f3b3f2dSJohn Johansen * 7544f3b3f2dSJohn Johansen * Returns: number of characters written to buf or -errno on failure 7554f3b3f2dSJohn Johansen */ 7564f3b3f2dSJohn Johansen static ssize_t query_label(char *buf, size_t buf_len, 7574f3b3f2dSJohn Johansen char *query, size_t query_len, bool view_only) 7584f3b3f2dSJohn Johansen { 759317d9a05SJohn Johansen struct aa_profile *profile; 760637f688dSJohn Johansen struct aa_label *label, *curr; 7614f3b3f2dSJohn Johansen char *label_name, *match_str; 7624f3b3f2dSJohn Johansen size_t label_name_len, match_len; 7634f3b3f2dSJohn Johansen struct aa_perms perms; 764317d9a05SJohn Johansen struct label_it i; 7654f3b3f2dSJohn Johansen 7664f3b3f2dSJohn Johansen if (!query_len) 7674f3b3f2dSJohn Johansen return -EINVAL; 7684f3b3f2dSJohn Johansen 7694f3b3f2dSJohn Johansen label_name = query; 7704f3b3f2dSJohn Johansen label_name_len = strnlen(query, query_len); 7714f3b3f2dSJohn Johansen if (!label_name_len || label_name_len == query_len) 7724f3b3f2dSJohn Johansen return -EINVAL; 7734f3b3f2dSJohn Johansen 7744f3b3f2dSJohn Johansen /** 7754f3b3f2dSJohn Johansen * The extra byte is to account for the null byte between the 7764f3b3f2dSJohn Johansen * profile name and dfa string. profile_name_len is greater 7774f3b3f2dSJohn Johansen * than zero and less than query_len, so a byte can be safely 7784f3b3f2dSJohn Johansen * added or subtracted. 7794f3b3f2dSJohn Johansen */ 7804f3b3f2dSJohn Johansen match_str = label_name + label_name_len + 1; 7814f3b3f2dSJohn Johansen match_len = query_len - label_name_len - 1; 7824f3b3f2dSJohn Johansen 783637f688dSJohn Johansen curr = begin_current_label_crit_section(); 784637f688dSJohn Johansen label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false); 785637f688dSJohn Johansen end_current_label_crit_section(curr); 786637f688dSJohn Johansen if (IS_ERR(label)) 787637f688dSJohn Johansen return PTR_ERR(label); 7884f3b3f2dSJohn Johansen 7894f3b3f2dSJohn Johansen perms = allperms; 790317d9a05SJohn Johansen if (view_only) { 791317d9a05SJohn Johansen label_for_each_in_ns(i, labels_ns(label), label, profile) { 792317d9a05SJohn Johansen profile_query_cb(profile, &perms, match_str, match_len); 793317d9a05SJohn Johansen } 794317d9a05SJohn Johansen } else { 795317d9a05SJohn Johansen label_for_each(i, label, profile) { 796317d9a05SJohn Johansen profile_query_cb(profile, &perms, match_str, match_len); 797317d9a05SJohn Johansen } 798317d9a05SJohn Johansen } 799317d9a05SJohn Johansen aa_put_label(label); 8004f3b3f2dSJohn Johansen 8014f3b3f2dSJohn Johansen return scnprintf(buf, buf_len, 8024f3b3f2dSJohn Johansen "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n", 8034f3b3f2dSJohn Johansen perms.allow, perms.deny, perms.audit, perms.quiet); 8044f3b3f2dSJohn Johansen } 8054f3b3f2dSJohn Johansen 8061dea3b41SJohn Johansen /* 8071dea3b41SJohn Johansen * Transaction based IO. 8081dea3b41SJohn Johansen * The file expects a write which triggers the transaction, and then 8091dea3b41SJohn Johansen * possibly a read(s) which collects the result - which is stored in a 8101dea3b41SJohn Johansen * file-local buffer. Once a new write is performed, a new set of results 8111dea3b41SJohn Johansen * are stored in the file-local buffer. 8121dea3b41SJohn Johansen */ 8131dea3b41SJohn Johansen struct multi_transaction { 8141dea3b41SJohn Johansen struct kref count; 8151dea3b41SJohn Johansen ssize_t size; 8161dea3b41SJohn Johansen char data[0]; 8171dea3b41SJohn Johansen }; 8181dea3b41SJohn Johansen 8191dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction)) 8201dea3b41SJohn Johansen /* TODO: replace with per file lock */ 8211dea3b41SJohn Johansen static DEFINE_SPINLOCK(multi_transaction_lock); 8221dea3b41SJohn Johansen 8231dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref) 8241dea3b41SJohn Johansen { 8251dea3b41SJohn Johansen struct multi_transaction *t; 8261dea3b41SJohn Johansen 8271dea3b41SJohn Johansen t = container_of(kref, struct multi_transaction, count); 8281dea3b41SJohn Johansen free_page((unsigned long) t); 8291dea3b41SJohn Johansen } 8301dea3b41SJohn Johansen 8311dea3b41SJohn Johansen static struct multi_transaction * 8321dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t) 8331dea3b41SJohn Johansen { 8341dea3b41SJohn Johansen if (t) 8351dea3b41SJohn Johansen kref_get(&(t->count)); 8361dea3b41SJohn Johansen 8371dea3b41SJohn Johansen return t; 8381dea3b41SJohn Johansen } 8391dea3b41SJohn Johansen 8401dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t) 8411dea3b41SJohn Johansen { 8421dea3b41SJohn Johansen if (t) 8431dea3b41SJohn Johansen kref_put(&(t->count), multi_transaction_kref); 8441dea3b41SJohn Johansen } 8451dea3b41SJohn Johansen 8461dea3b41SJohn Johansen /* does not increment @new's count */ 8471dea3b41SJohn Johansen static void multi_transaction_set(struct file *file, 8481dea3b41SJohn Johansen struct multi_transaction *new, size_t n) 8491dea3b41SJohn Johansen { 8501dea3b41SJohn Johansen struct multi_transaction *old; 8511dea3b41SJohn Johansen 8521dea3b41SJohn Johansen AA_BUG(n > MULTI_TRANSACTION_LIMIT); 8531dea3b41SJohn Johansen 8541dea3b41SJohn Johansen new->size = n; 8551dea3b41SJohn Johansen spin_lock(&multi_transaction_lock); 8561dea3b41SJohn Johansen old = (struct multi_transaction *) file->private_data; 8571dea3b41SJohn Johansen file->private_data = new; 8581dea3b41SJohn Johansen spin_unlock(&multi_transaction_lock); 8591dea3b41SJohn Johansen put_multi_transaction(old); 8601dea3b41SJohn Johansen } 8611dea3b41SJohn Johansen 8621dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file, 8631dea3b41SJohn Johansen const char __user *buf, 8641dea3b41SJohn Johansen size_t size) 8651dea3b41SJohn Johansen { 8661dea3b41SJohn Johansen struct multi_transaction *t; 8671dea3b41SJohn Johansen 8681dea3b41SJohn Johansen if (size > MULTI_TRANSACTION_LIMIT - 1) 8691dea3b41SJohn Johansen return ERR_PTR(-EFBIG); 8701dea3b41SJohn Johansen 8711dea3b41SJohn Johansen t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL); 8721dea3b41SJohn Johansen if (!t) 8731dea3b41SJohn Johansen return ERR_PTR(-ENOMEM); 8741dea3b41SJohn Johansen kref_init(&t->count); 8751dea3b41SJohn Johansen if (copy_from_user(t->data, buf, size)) 8761dea3b41SJohn Johansen return ERR_PTR(-EFAULT); 8771dea3b41SJohn Johansen 8781dea3b41SJohn Johansen return t; 8791dea3b41SJohn Johansen } 8801dea3b41SJohn Johansen 8811dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf, 8821dea3b41SJohn Johansen size_t size, loff_t *pos) 8831dea3b41SJohn Johansen { 8841dea3b41SJohn Johansen struct multi_transaction *t; 8851dea3b41SJohn Johansen ssize_t ret; 8861dea3b41SJohn Johansen 8871dea3b41SJohn Johansen spin_lock(&multi_transaction_lock); 8881dea3b41SJohn Johansen t = get_multi_transaction(file->private_data); 8891dea3b41SJohn Johansen spin_unlock(&multi_transaction_lock); 8901dea3b41SJohn Johansen if (!t) 8911dea3b41SJohn Johansen return 0; 8921dea3b41SJohn Johansen 8931dea3b41SJohn Johansen ret = simple_read_from_buffer(buf, size, pos, t->data, t->size); 8941dea3b41SJohn Johansen put_multi_transaction(t); 8951dea3b41SJohn Johansen 8961dea3b41SJohn Johansen return ret; 8971dea3b41SJohn Johansen } 8981dea3b41SJohn Johansen 8991dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file) 9001dea3b41SJohn Johansen { 9011dea3b41SJohn Johansen put_multi_transaction(file->private_data); 9021dea3b41SJohn Johansen 9031dea3b41SJohn Johansen return 0; 9041dea3b41SJohn Johansen } 9051dea3b41SJohn Johansen 906317d9a05SJohn Johansen #define QUERY_CMD_LABEL "label\0" 907317d9a05SJohn Johansen #define QUERY_CMD_LABEL_LEN 6 9084f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE "profile\0" 9094f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE_LEN 8 910317d9a05SJohn Johansen #define QUERY_CMD_LABELALL "labelall\0" 911317d9a05SJohn Johansen #define QUERY_CMD_LABELALL_LEN 9 912e025be0fSWilliam Hua #define QUERY_CMD_DATA "data\0" 913e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN 5 914e025be0fSWilliam Hua 915e025be0fSWilliam Hua /** 916e025be0fSWilliam Hua * aa_write_access - generic permissions and data query 917e025be0fSWilliam Hua * @file: pointer to open apparmorfs/access file 918e025be0fSWilliam Hua * @ubuf: user buffer containing the complete query string (NOT NULL) 919e025be0fSWilliam Hua * @count: size of ubuf 920e025be0fSWilliam Hua * @ppos: position in the file (MUST BE ZERO) 921e025be0fSWilliam Hua * 922e025be0fSWilliam Hua * Allows for one permissions or data query per open(), write(), and read() 923e025be0fSWilliam Hua * sequence. The only queries currently supported are label-based queries for 924e025be0fSWilliam Hua * permissions or data. 925e025be0fSWilliam Hua * 926e025be0fSWilliam Hua * For permissions queries, ubuf must begin with "label\0", followed by the 927e025be0fSWilliam Hua * profile query specific format described in the query_label() function 928e025be0fSWilliam Hua * documentation. 929e025be0fSWilliam Hua * 930e025be0fSWilliam Hua * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where 931e025be0fSWilliam Hua * <LABEL> is the name of the security confinement context and <KEY> is the 932e025be0fSWilliam Hua * name of the data to retrieve. 933e025be0fSWilliam Hua * 934e025be0fSWilliam Hua * Returns: number of bytes written or -errno on failure 935e025be0fSWilliam Hua */ 936e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf, 937e025be0fSWilliam Hua size_t count, loff_t *ppos) 938e025be0fSWilliam Hua { 9391dea3b41SJohn Johansen struct multi_transaction *t; 940e025be0fSWilliam Hua ssize_t len; 941e025be0fSWilliam Hua 942e025be0fSWilliam Hua if (*ppos) 943e025be0fSWilliam Hua return -ESPIPE; 944e025be0fSWilliam Hua 9451dea3b41SJohn Johansen t = multi_transaction_new(file, ubuf, count); 9461dea3b41SJohn Johansen if (IS_ERR(t)) 9471dea3b41SJohn Johansen return PTR_ERR(t); 948e025be0fSWilliam Hua 9494f3b3f2dSJohn Johansen if (count > QUERY_CMD_PROFILE_LEN && 9504f3b3f2dSJohn Johansen !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) { 9514f3b3f2dSJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 9524f3b3f2dSJohn Johansen t->data + QUERY_CMD_PROFILE_LEN, 9534f3b3f2dSJohn Johansen count - QUERY_CMD_PROFILE_LEN, true); 954317d9a05SJohn Johansen } else if (count > QUERY_CMD_LABEL_LEN && 955317d9a05SJohn Johansen !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) { 956317d9a05SJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 957317d9a05SJohn Johansen t->data + QUERY_CMD_LABEL_LEN, 958317d9a05SJohn Johansen count - QUERY_CMD_LABEL_LEN, true); 959317d9a05SJohn Johansen } else if (count > QUERY_CMD_LABELALL_LEN && 960317d9a05SJohn Johansen !memcmp(t->data, QUERY_CMD_LABELALL, 961317d9a05SJohn Johansen QUERY_CMD_LABELALL_LEN)) { 962317d9a05SJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 963317d9a05SJohn Johansen t->data + QUERY_CMD_LABELALL_LEN, 964317d9a05SJohn Johansen count - QUERY_CMD_LABELALL_LEN, false); 9654f3b3f2dSJohn Johansen } else if (count > QUERY_CMD_DATA_LEN && 9661dea3b41SJohn Johansen !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { 9671dea3b41SJohn Johansen len = query_data(t->data, MULTI_TRANSACTION_LIMIT, 9681dea3b41SJohn Johansen t->data + QUERY_CMD_DATA_LEN, 969e025be0fSWilliam Hua count - QUERY_CMD_DATA_LEN); 970e025be0fSWilliam Hua } else 971e025be0fSWilliam Hua len = -EINVAL; 972e025be0fSWilliam Hua 9731dea3b41SJohn Johansen if (len < 0) { 9741dea3b41SJohn Johansen put_multi_transaction(t); 975e025be0fSWilliam Hua return len; 9761dea3b41SJohn Johansen } 977e025be0fSWilliam Hua 9781dea3b41SJohn Johansen multi_transaction_set(file, t, len); 979e025be0fSWilliam Hua 980e025be0fSWilliam Hua return count; 981e025be0fSWilliam Hua } 982e025be0fSWilliam Hua 983c97204baSJohn Johansen static const struct file_operations aa_sfs_access = { 984e025be0fSWilliam Hua .write = aa_write_access, 9851dea3b41SJohn Johansen .read = multi_transaction_read, 9861dea3b41SJohn Johansen .release = multi_transaction_release, 987e025be0fSWilliam Hua .llseek = generic_file_llseek, 988e025be0fSWilliam Hua }; 989e025be0fSWilliam Hua 990c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v) 991e74abcf3SKees Cook { 992c97204baSJohn Johansen struct aa_sfs_entry *fs_file = seq->private; 993e74abcf3SKees Cook 994e74abcf3SKees Cook if (!fs_file) 995e74abcf3SKees Cook return 0; 996e74abcf3SKees Cook 997e74abcf3SKees Cook switch (fs_file->v_type) { 998c97204baSJohn Johansen case AA_SFS_TYPE_BOOLEAN: 999e74abcf3SKees Cook seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); 1000e74abcf3SKees Cook break; 1001c97204baSJohn Johansen case AA_SFS_TYPE_STRING: 1002a9bf8e9fSKees Cook seq_printf(seq, "%s\n", fs_file->v.string); 1003a9bf8e9fSKees Cook break; 1004c97204baSJohn Johansen case AA_SFS_TYPE_U64: 1005e74abcf3SKees Cook seq_printf(seq, "%#08lx\n", fs_file->v.u64); 1006e74abcf3SKees Cook break; 1007e74abcf3SKees Cook default: 1008e74abcf3SKees Cook /* Ignore unpritable entry types. */ 1009e74abcf3SKees Cook break; 1010e74abcf3SKees Cook } 1011e74abcf3SKees Cook 1012e74abcf3SKees Cook return 0; 1013e74abcf3SKees Cook } 1014e74abcf3SKees Cook 1015c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file) 1016e74abcf3SKees Cook { 1017c97204baSJohn Johansen return single_open(file, aa_sfs_seq_show, inode->i_private); 1018e74abcf3SKees Cook } 1019e74abcf3SKees Cook 1020c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = { 1021e74abcf3SKees Cook .owner = THIS_MODULE, 1022c97204baSJohn Johansen .open = aa_sfs_seq_open, 1023e74abcf3SKees Cook .read = seq_read, 1024e74abcf3SKees Cook .llseek = seq_lseek, 1025e74abcf3SKees Cook .release = single_release, 1026e74abcf3SKees Cook }; 1027e74abcf3SKees Cook 102852b97de3SJohn Johansen /* 102952b97de3SJohn Johansen * profile based file operations 103052b97de3SJohn Johansen * policy/profiles/XXXX/profiles/ * 103152b97de3SJohn Johansen */ 103252b97de3SJohn Johansen 103352b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME) \ 103452b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\ 103552b97de3SJohn Johansen { \ 103652b97de3SJohn Johansen return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \ 103752b97de3SJohn Johansen } \ 103852b97de3SJohn Johansen \ 103952b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = { \ 104052b97de3SJohn Johansen .owner = THIS_MODULE, \ 104152b97de3SJohn Johansen .open = seq_profile_ ##NAME ##_open, \ 104252b97de3SJohn Johansen .read = seq_read, \ 104352b97de3SJohn Johansen .llseek = seq_lseek, \ 104452b97de3SJohn Johansen .release = seq_profile_release, \ 104552b97de3SJohn Johansen } \ 104652b97de3SJohn Johansen 104752b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file, 10480d259f04SJohn Johansen int (*show)(struct seq_file *, void *)) 10490d259f04SJohn Johansen { 10508399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(inode->i_private); 10518399588aSJohn Johansen int error = single_open(file, show, proxy); 105263e2b423SJohn Johansen 10530d259f04SJohn Johansen if (error) { 10540d259f04SJohn Johansen file->private_data = NULL; 10558399588aSJohn Johansen aa_put_proxy(proxy); 10560d259f04SJohn Johansen } 10570d259f04SJohn Johansen 10580d259f04SJohn Johansen return error; 10590d259f04SJohn Johansen } 10600d259f04SJohn Johansen 106152b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file) 10620d259f04SJohn Johansen { 10630d259f04SJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 10640d259f04SJohn Johansen if (seq) 10658399588aSJohn Johansen aa_put_proxy(seq->private); 10660d259f04SJohn Johansen return single_release(inode, file); 10670d259f04SJohn Johansen } 10680d259f04SJohn Johansen 106952b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v) 10700d259f04SJohn Johansen { 10718399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1072637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1073637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 10740d259f04SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 1075637f688dSJohn Johansen aa_put_label(label); 10760d259f04SJohn Johansen 10770d259f04SJohn Johansen return 0; 10780d259f04SJohn Johansen } 10790d259f04SJohn Johansen 108052b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v) 10810d259f04SJohn Johansen { 10828399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1083637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1084637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 10850d259f04SJohn Johansen seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); 1086637f688dSJohn Johansen aa_put_label(label); 10870d259f04SJohn Johansen 10880d259f04SJohn Johansen return 0; 10890d259f04SJohn Johansen } 10900d259f04SJohn Johansen 109152b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v) 1092556d0be7SJohn Johansen { 10938399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1094637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1095637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 1096556d0be7SJohn Johansen if (profile->attach) 1097556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->attach); 1098556d0be7SJohn Johansen else if (profile->xmatch) 1099556d0be7SJohn Johansen seq_puts(seq, "<unknown>\n"); 1100556d0be7SJohn Johansen else 1101556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 1102637f688dSJohn Johansen aa_put_label(label); 1103556d0be7SJohn Johansen 1104556d0be7SJohn Johansen return 0; 1105556d0be7SJohn Johansen } 1106556d0be7SJohn Johansen 110752b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v) 1108f8eb8a13SJohn Johansen { 11098399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1110637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1111637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 1112f8eb8a13SJohn Johansen unsigned int i, size = aa_hash_size(); 1113f8eb8a13SJohn Johansen 1114f8eb8a13SJohn Johansen if (profile->hash) { 1115f8eb8a13SJohn Johansen for (i = 0; i < size; i++) 1116f8eb8a13SJohn Johansen seq_printf(seq, "%.2x", profile->hash[i]); 111747dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 1118f8eb8a13SJohn Johansen } 1119637f688dSJohn Johansen aa_put_label(label); 1120f8eb8a13SJohn Johansen 1121f8eb8a13SJohn Johansen return 0; 1122f8eb8a13SJohn Johansen } 1123f8eb8a13SJohn Johansen 112452b97de3SJohn Johansen SEQ_PROFILE_FOPS(name); 112552b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode); 112652b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach); 112752b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash); 1128f8eb8a13SJohn Johansen 112964c86970SJohn Johansen /* 113064c86970SJohn Johansen * namespace based files 113164c86970SJohn Johansen * several root files and 113264c86970SJohn Johansen * policy/ * 113364c86970SJohn Johansen */ 1134f8eb8a13SJohn Johansen 113564c86970SJohn Johansen #define SEQ_NS_FOPS(NAME) \ 113664c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \ 113764c86970SJohn Johansen { \ 113864c86970SJohn Johansen return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \ 113964c86970SJohn Johansen } \ 114064c86970SJohn Johansen \ 114164c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = { \ 114264c86970SJohn Johansen .owner = THIS_MODULE, \ 114364c86970SJohn Johansen .open = seq_ns_ ##NAME ##_open, \ 114464c86970SJohn Johansen .read = seq_read, \ 114564c86970SJohn Johansen .llseek = seq_lseek, \ 114664c86970SJohn Johansen .release = single_release, \ 114764c86970SJohn Johansen } \ 11483e3e5695SJohn Johansen 114940cde7fcSJohn Johansen static int seq_ns_stacked_show(struct seq_file *seq, void *v) 115040cde7fcSJohn Johansen { 115140cde7fcSJohn Johansen struct aa_label *label; 115240cde7fcSJohn Johansen 115340cde7fcSJohn Johansen label = begin_current_label_crit_section(); 115440cde7fcSJohn Johansen seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no"); 115540cde7fcSJohn Johansen end_current_label_crit_section(label); 115640cde7fcSJohn Johansen 115740cde7fcSJohn Johansen return 0; 115840cde7fcSJohn Johansen } 115940cde7fcSJohn Johansen 116040cde7fcSJohn Johansen static int seq_ns_nsstacked_show(struct seq_file *seq, void *v) 116140cde7fcSJohn Johansen { 116240cde7fcSJohn Johansen struct aa_label *label; 116340cde7fcSJohn Johansen struct aa_profile *profile; 116440cde7fcSJohn Johansen struct label_it it; 116540cde7fcSJohn Johansen int count = 1; 116640cde7fcSJohn Johansen 116740cde7fcSJohn Johansen label = begin_current_label_crit_section(); 116840cde7fcSJohn Johansen 116940cde7fcSJohn Johansen if (label->size > 1) { 117040cde7fcSJohn Johansen label_for_each(it, label, profile) 117140cde7fcSJohn Johansen if (profile->ns != labels_ns(label)) { 117240cde7fcSJohn Johansen count++; 117340cde7fcSJohn Johansen break; 117440cde7fcSJohn Johansen } 117540cde7fcSJohn Johansen } 117640cde7fcSJohn Johansen 117740cde7fcSJohn Johansen seq_printf(seq, "%s\n", count > 1 ? "yes" : "no"); 117840cde7fcSJohn Johansen end_current_label_crit_section(label); 117940cde7fcSJohn Johansen 118040cde7fcSJohn Johansen return 0; 118140cde7fcSJohn Johansen } 118240cde7fcSJohn Johansen 118364c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v) 1184a71ada30SJohn Johansen { 1185637f688dSJohn Johansen struct aa_label *label; 1186a71ada30SJohn Johansen 1187637f688dSJohn Johansen label = begin_current_label_crit_section(); 1188637f688dSJohn Johansen seq_printf(seq, "%d\n", labels_ns(label)->level); 1189637f688dSJohn Johansen end_current_label_crit_section(label); 1190a71ada30SJohn Johansen 1191a71ada30SJohn Johansen return 0; 1192a71ada30SJohn Johansen } 1193a71ada30SJohn Johansen 119464c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v) 11953e3e5695SJohn Johansen { 1196637f688dSJohn Johansen struct aa_label *label = begin_current_label_crit_section(); 1197040d9e2bSJohn Johansen seq_printf(seq, "%s\n", labels_ns(label)->base.name); 1198637f688dSJohn Johansen end_current_label_crit_section(label); 11993e3e5695SJohn Johansen 12003e3e5695SJohn Johansen return 0; 12013e3e5695SJohn Johansen } 12023e3e5695SJohn Johansen 120340cde7fcSJohn Johansen SEQ_NS_FOPS(stacked); 120440cde7fcSJohn Johansen SEQ_NS_FOPS(nsstacked); 120564c86970SJohn Johansen SEQ_NS_FOPS(level); 120664c86970SJohn Johansen SEQ_NS_FOPS(name); 12073e3e5695SJohn Johansen 12085d5182caSJohn Johansen 12095d5182caSJohn Johansen /* policy/raw_data/ * file ops */ 12105d5182caSJohn Johansen 12115d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME) \ 12125d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\ 12135d5182caSJohn Johansen { \ 12145d5182caSJohn Johansen return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \ 12155d5182caSJohn Johansen } \ 12165d5182caSJohn Johansen \ 12175d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \ 12185d5182caSJohn Johansen .owner = THIS_MODULE, \ 12195d5182caSJohn Johansen .open = seq_rawdata_ ##NAME ##_open, \ 12205d5182caSJohn Johansen .read = seq_read, \ 12215d5182caSJohn Johansen .llseek = seq_lseek, \ 12225d5182caSJohn Johansen .release = seq_rawdata_release, \ 12235d5182caSJohn Johansen } \ 12245d5182caSJohn Johansen 12255d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file, 12265d5182caSJohn Johansen int (*show)(struct seq_file *, void *)) 12275ac8c355SJohn Johansen { 12285d5182caSJohn Johansen struct aa_loaddata *data = __aa_get_loaddata(inode->i_private); 12295d5182caSJohn Johansen int error; 12305d5182caSJohn Johansen 12315d5182caSJohn Johansen if (!data) 12325d5182caSJohn Johansen /* lost race this ent is being reaped */ 12335d5182caSJohn Johansen return -ENOENT; 12345d5182caSJohn Johansen 12355d5182caSJohn Johansen error = single_open(file, show, data); 12365d5182caSJohn Johansen if (error) { 12375d5182caSJohn Johansen AA_BUG(file->private_data && 12385d5182caSJohn Johansen ((struct seq_file *)file->private_data)->private); 12395d5182caSJohn Johansen aa_put_loaddata(data); 12405d5182caSJohn Johansen } 12415d5182caSJohn Johansen 12425d5182caSJohn Johansen return error; 12435d5182caSJohn Johansen } 12445d5182caSJohn Johansen 12455d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file) 12465d5182caSJohn Johansen { 12475d5182caSJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 12485d5182caSJohn Johansen 12495d5182caSJohn Johansen if (seq) 12505d5182caSJohn Johansen aa_put_loaddata(seq->private); 12515d5182caSJohn Johansen 12525d5182caSJohn Johansen return single_release(inode, file); 12535d5182caSJohn Johansen } 12545d5182caSJohn Johansen 12555d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v) 12565d5182caSJohn Johansen { 12575d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12585d5182caSJohn Johansen 12595d5182caSJohn Johansen seq_printf(seq, "v%d\n", data->abi); 12605ac8c355SJohn Johansen 12615ac8c355SJohn Johansen return 0; 12625ac8c355SJohn Johansen } 12635ac8c355SJohn Johansen 12645d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v) 12655ac8c355SJohn Johansen { 12665d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12675ac8c355SJohn Johansen 12685d5182caSJohn Johansen seq_printf(seq, "%ld\n", data->revision); 12695ac8c355SJohn Johansen 12705ac8c355SJohn Johansen return 0; 12715ac8c355SJohn Johansen } 12725ac8c355SJohn Johansen 12735d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v) 12745ac8c355SJohn Johansen { 12755d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12765ac8c355SJohn Johansen unsigned int i, size = aa_hash_size(); 12775ac8c355SJohn Johansen 12785d5182caSJohn Johansen if (data->hash) { 12795ac8c355SJohn Johansen for (i = 0; i < size; i++) 12805d5182caSJohn Johansen seq_printf(seq, "%.2x", data->hash[i]); 128147dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 12825ac8c355SJohn Johansen } 12835ac8c355SJohn Johansen 12845ac8c355SJohn Johansen return 0; 12855ac8c355SJohn Johansen } 12865ac8c355SJohn Johansen 12875d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi); 12885d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision); 12895d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash); 12905ac8c355SJohn Johansen 12915ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, 12925ac8c355SJohn Johansen loff_t *ppos) 12935ac8c355SJohn Johansen { 12945ac8c355SJohn Johansen struct aa_loaddata *rawdata = file->private_data; 12955ac8c355SJohn Johansen 12965ac8c355SJohn Johansen return simple_read_from_buffer(buf, size, ppos, rawdata->data, 12975ac8c355SJohn Johansen rawdata->size); 12985ac8c355SJohn Johansen } 12995ac8c355SJohn Johansen 13005d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file) 13015ac8c355SJohn Johansen { 13025d5182caSJohn Johansen aa_put_loaddata(file->private_data); 13035ac8c355SJohn Johansen 13045ac8c355SJohn Johansen return 0; 13055ac8c355SJohn Johansen } 13065ac8c355SJohn Johansen 13075d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file) 13085d5182caSJohn Johansen { 13095d5182caSJohn Johansen if (!policy_view_capable(NULL)) 13105d5182caSJohn Johansen return -EACCES; 13115d5182caSJohn Johansen file->private_data = __aa_get_loaddata(inode->i_private); 13125d5182caSJohn Johansen if (!file->private_data) 13135d5182caSJohn Johansen /* lost race: this entry is being reaped */ 13145d5182caSJohn Johansen return -ENOENT; 13155d5182caSJohn Johansen 13165d5182caSJohn Johansen return 0; 13175d5182caSJohn Johansen } 13185d5182caSJohn Johansen 13195d5182caSJohn Johansen static const struct file_operations rawdata_fops = { 13205ac8c355SJohn Johansen .open = rawdata_open, 13215ac8c355SJohn Johansen .read = rawdata_read, 13225ac8c355SJohn Johansen .llseek = generic_file_llseek, 13235ac8c355SJohn Johansen .release = rawdata_release, 13245ac8c355SJohn Johansen }; 13255ac8c355SJohn Johansen 13265d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata) 13275d5182caSJohn Johansen { 13285d5182caSJohn Johansen int i; 13295d5182caSJohn Johansen 13305d5182caSJohn Johansen for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) { 13315d5182caSJohn Johansen if (!IS_ERR_OR_NULL(rawdata->dents[i])) { 13325d5182caSJohn Johansen /* no refcounts on i_private */ 1333c961ee5fSJohn Johansen aafs_remove(rawdata->dents[i]); 13345d5182caSJohn Johansen rawdata->dents[i] = NULL; 13355d5182caSJohn Johansen } 13365d5182caSJohn Johansen } 13375d5182caSJohn Johansen } 13385d5182caSJohn Johansen 13395d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata) 13405d5182caSJohn Johansen { 13415d5182caSJohn Johansen AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock)); 13425d5182caSJohn Johansen 13435d5182caSJohn Johansen if (rawdata->ns) { 13445d5182caSJohn Johansen remove_rawdata_dents(rawdata); 13455d5182caSJohn Johansen list_del_init(&rawdata->list); 13465d5182caSJohn Johansen aa_put_ns(rawdata->ns); 13475d5182caSJohn Johansen rawdata->ns = NULL; 13485d5182caSJohn Johansen } 13495d5182caSJohn Johansen } 13505d5182caSJohn Johansen 13515d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata) 13525d5182caSJohn Johansen { 13535d5182caSJohn Johansen struct dentry *dent, *dir; 13545d5182caSJohn Johansen 13555d5182caSJohn Johansen AA_BUG(!ns); 13565d5182caSJohn Johansen AA_BUG(!rawdata); 13575d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 13585d5182caSJohn Johansen AA_BUG(!ns_subdata_dir(ns)); 13595d5182caSJohn Johansen 13605d5182caSJohn Johansen /* 13615d5182caSJohn Johansen * just use ns revision dir was originally created at. This is 13625d5182caSJohn Johansen * under ns->lock and if load is successful revision will be 13635d5182caSJohn Johansen * bumped and is guaranteed to be unique 13645d5182caSJohn Johansen */ 13655d5182caSJohn Johansen rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision); 13665d5182caSJohn Johansen if (!rawdata->name) 13675d5182caSJohn Johansen return -ENOMEM; 13685d5182caSJohn Johansen 1369c961ee5fSJohn Johansen dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns)); 13705d5182caSJohn Johansen if (IS_ERR(dir)) 13715d5182caSJohn Johansen /* ->name freed when rawdata freed */ 13725d5182caSJohn Johansen return PTR_ERR(dir); 13735d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DIR] = dir; 13745d5182caSJohn Johansen 1375c961ee5fSJohn Johansen dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata, 13765d5182caSJohn Johansen &seq_rawdata_abi_fops); 13775d5182caSJohn Johansen if (IS_ERR(dent)) 13785d5182caSJohn Johansen goto fail; 13795d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_ABI] = dent; 13805d5182caSJohn Johansen 1381c961ee5fSJohn Johansen dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata, 13825d5182caSJohn Johansen &seq_rawdata_revision_fops); 13835d5182caSJohn Johansen if (IS_ERR(dent)) 13845d5182caSJohn Johansen goto fail; 13855d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_REVISION] = dent; 13865d5182caSJohn Johansen 13875d5182caSJohn Johansen if (aa_g_hash_policy) { 1388c961ee5fSJohn Johansen dent = aafs_create_file("sha1", S_IFREG | 0444, dir, 13895d5182caSJohn Johansen rawdata, &seq_rawdata_hash_fops); 13905d5182caSJohn Johansen if (IS_ERR(dent)) 13915d5182caSJohn Johansen goto fail; 13925d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_HASH] = dent; 13935d5182caSJohn Johansen } 13945d5182caSJohn Johansen 1395c961ee5fSJohn Johansen dent = aafs_create_file("raw_data", S_IFREG | 0444, 13965d5182caSJohn Johansen dir, rawdata, &rawdata_fops); 13975d5182caSJohn Johansen if (IS_ERR(dent)) 13985d5182caSJohn Johansen goto fail; 13995d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DATA] = dent; 14005d5182caSJohn Johansen d_inode(dent)->i_size = rawdata->size; 14015d5182caSJohn Johansen 14025d5182caSJohn Johansen rawdata->ns = aa_get_ns(ns); 14035d5182caSJohn Johansen list_add(&rawdata->list, &ns->rawdata_list); 14045d5182caSJohn Johansen /* no refcount on inode rawdata */ 14055d5182caSJohn Johansen 14065d5182caSJohn Johansen return 0; 14075d5182caSJohn Johansen 14085d5182caSJohn Johansen fail: 14095d5182caSJohn Johansen remove_rawdata_dents(rawdata); 14105d5182caSJohn Johansen 14115d5182caSJohn Johansen return PTR_ERR(dent); 14125d5182caSJohn Johansen } 14135d5182caSJohn Johansen 14140d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/ 1415c97204baSJohn Johansen 1416c97204baSJohn Johansen /** 1417c97204baSJohn Johansen * 1418c97204baSJohn Johansen * Requires: @profile->ns->lock held 1419c97204baSJohn Johansen */ 1420c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile) 14210d259f04SJohn Johansen { 14220d259f04SJohn Johansen struct aa_profile *child; 14230d259f04SJohn Johansen int i; 14240d259f04SJohn Johansen 14250d259f04SJohn Johansen if (!profile) 14260d259f04SJohn Johansen return; 14270d259f04SJohn Johansen 14280d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) 1429c97204baSJohn Johansen __aafs_profile_rmdir(child); 14300d259f04SJohn Johansen 14310d259f04SJohn Johansen for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { 14328399588aSJohn Johansen struct aa_proxy *proxy; 14330d259f04SJohn Johansen if (!profile->dents[i]) 14340d259f04SJohn Johansen continue; 14350d259f04SJohn Johansen 14368399588aSJohn Johansen proxy = d_inode(profile->dents[i])->i_private; 1437c961ee5fSJohn Johansen aafs_remove(profile->dents[i]); 14388399588aSJohn Johansen aa_put_proxy(proxy); 14390d259f04SJohn Johansen profile->dents[i] = NULL; 14400d259f04SJohn Johansen } 14410d259f04SJohn Johansen } 14420d259f04SJohn Johansen 1443c97204baSJohn Johansen /** 1444c97204baSJohn Johansen * 1445c97204baSJohn Johansen * Requires: @old->ns->lock held 1446c97204baSJohn Johansen */ 1447c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old, 14480d259f04SJohn Johansen struct aa_profile *new) 14490d259f04SJohn Johansen { 14500d259f04SJohn Johansen int i; 14510d259f04SJohn Johansen 1452cbf2d0e1SJohn Johansen AA_BUG(!old); 1453cbf2d0e1SJohn Johansen AA_BUG(!new); 1454cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock)); 1455cbf2d0e1SJohn Johansen 14560d259f04SJohn Johansen for (i = 0; i < AAFS_PROF_SIZEOF; i++) { 14570d259f04SJohn Johansen new->dents[i] = old->dents[i]; 1458d671e890SJohn Johansen if (new->dents[i]) 1459078cd827SDeepa Dinamani new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); 14600d259f04SJohn Johansen old->dents[i] = NULL; 14610d259f04SJohn Johansen } 14620d259f04SJohn Johansen } 14630d259f04SJohn Johansen 14640d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name, 14650d259f04SJohn Johansen struct aa_profile *profile, 14660d259f04SJohn Johansen const struct file_operations *fops) 14670d259f04SJohn Johansen { 1468637f688dSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy); 14690d259f04SJohn Johansen struct dentry *dent; 14700d259f04SJohn Johansen 1471c961ee5fSJohn Johansen dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops); 14720d259f04SJohn Johansen if (IS_ERR(dent)) 14738399588aSJohn Johansen aa_put_proxy(proxy); 14740d259f04SJohn Johansen 14750d259f04SJohn Johansen return dent; 14760d259f04SJohn Johansen } 14770d259f04SJohn Johansen 14785d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile) 14795d5182caSJohn Johansen { 14805d5182caSJohn Johansen int depth = 0; 14815d5182caSJohn Johansen 14825d5182caSJohn Johansen rcu_read_lock(); 14835d5182caSJohn Johansen for (depth = 0; profile; profile = rcu_access_pointer(profile->parent)) 14845d5182caSJohn Johansen depth++; 14855d5182caSJohn Johansen rcu_read_unlock(); 14865d5182caSJohn Johansen 14875d5182caSJohn Johansen return depth; 14885d5182caSJohn Johansen } 14895d5182caSJohn Johansen 14901180b4c7SJohn Johansen static char *gen_symlink_name(int depth, const char *dirname, const char *fname) 14915d5182caSJohn Johansen { 14921180b4c7SJohn Johansen char *buffer, *s; 14935d5182caSJohn Johansen int error; 14941180b4c7SJohn Johansen int size = depth * 6 + strlen(dirname) + strlen(fname) + 11; 14951180b4c7SJohn Johansen 14961180b4c7SJohn Johansen s = buffer = kmalloc(size, GFP_KERNEL); 14971180b4c7SJohn Johansen if (!buffer) 14981180b4c7SJohn Johansen return ERR_PTR(-ENOMEM); 14995d5182caSJohn Johansen 15005d5182caSJohn Johansen for (; depth > 0; depth--) { 15011180b4c7SJohn Johansen strcpy(s, "../../"); 15021180b4c7SJohn Johansen s += 6; 15031180b4c7SJohn Johansen size -= 6; 15045d5182caSJohn Johansen } 15055d5182caSJohn Johansen 15061180b4c7SJohn Johansen error = snprintf(s, size, "raw_data/%s/%s", dirname, fname); 1507588558ebSColin Ian King if (error >= size || error < 0) { 1508588558ebSColin Ian King kfree(buffer); 15091180b4c7SJohn Johansen return ERR_PTR(-ENAMETOOLONG); 15105d5182caSJohn Johansen } 15115d5182caSJohn Johansen 15121180b4c7SJohn Johansen return buffer; 15135d5182caSJohn Johansen } 15145d5182caSJohn Johansen 15151180b4c7SJohn Johansen static void rawdata_link_cb(void *arg) 15161180b4c7SJohn Johansen { 15171180b4c7SJohn Johansen kfree(arg); 15181180b4c7SJohn Johansen } 15191180b4c7SJohn Johansen 15201180b4c7SJohn Johansen static const char *rawdata_get_link_base(struct dentry *dentry, 15211180b4c7SJohn Johansen struct inode *inode, 15221180b4c7SJohn Johansen struct delayed_call *done, 15231180b4c7SJohn Johansen const char *name) 15241180b4c7SJohn Johansen { 15251180b4c7SJohn Johansen struct aa_proxy *proxy = inode->i_private; 15261180b4c7SJohn Johansen struct aa_label *label; 15271180b4c7SJohn Johansen struct aa_profile *profile; 15281180b4c7SJohn Johansen char *target; 15291180b4c7SJohn Johansen int depth; 15301180b4c7SJohn Johansen 15311180b4c7SJohn Johansen if (!dentry) 15321180b4c7SJohn Johansen return ERR_PTR(-ECHILD); 15331180b4c7SJohn Johansen 15341180b4c7SJohn Johansen label = aa_get_label_rcu(&proxy->label); 15351180b4c7SJohn Johansen profile = labels_profile(label); 15361180b4c7SJohn Johansen depth = profile_depth(profile); 15371180b4c7SJohn Johansen target = gen_symlink_name(depth, profile->rawdata->name, name); 15381180b4c7SJohn Johansen aa_put_label(label); 15391180b4c7SJohn Johansen 15401180b4c7SJohn Johansen if (IS_ERR(target)) 15411180b4c7SJohn Johansen return target; 15421180b4c7SJohn Johansen 15431180b4c7SJohn Johansen set_delayed_call(done, rawdata_link_cb, target); 15441180b4c7SJohn Johansen 15451180b4c7SJohn Johansen return target; 15461180b4c7SJohn Johansen } 15471180b4c7SJohn Johansen 15481180b4c7SJohn Johansen static const char *rawdata_get_link_sha1(struct dentry *dentry, 15491180b4c7SJohn Johansen struct inode *inode, 15501180b4c7SJohn Johansen struct delayed_call *done) 15511180b4c7SJohn Johansen { 15521180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "sha1"); 15531180b4c7SJohn Johansen } 15541180b4c7SJohn Johansen 15551180b4c7SJohn Johansen static const char *rawdata_get_link_abi(struct dentry *dentry, 15561180b4c7SJohn Johansen struct inode *inode, 15571180b4c7SJohn Johansen struct delayed_call *done) 15581180b4c7SJohn Johansen { 15591180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "abi"); 15601180b4c7SJohn Johansen } 15611180b4c7SJohn Johansen 15621180b4c7SJohn Johansen static const char *rawdata_get_link_data(struct dentry *dentry, 15631180b4c7SJohn Johansen struct inode *inode, 15641180b4c7SJohn Johansen struct delayed_call *done) 15651180b4c7SJohn Johansen { 15661180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "raw_data"); 15671180b4c7SJohn Johansen } 15681180b4c7SJohn Johansen 15691180b4c7SJohn Johansen static const struct inode_operations rawdata_link_sha1_iops = { 15701180b4c7SJohn Johansen .get_link = rawdata_get_link_sha1, 15711180b4c7SJohn Johansen }; 15721180b4c7SJohn Johansen 15731180b4c7SJohn Johansen static const struct inode_operations rawdata_link_abi_iops = { 15741180b4c7SJohn Johansen .get_link = rawdata_get_link_abi, 15751180b4c7SJohn Johansen }; 15761180b4c7SJohn Johansen static const struct inode_operations rawdata_link_data_iops = { 15771180b4c7SJohn Johansen .get_link = rawdata_get_link_data, 15781180b4c7SJohn Johansen }; 15791180b4c7SJohn Johansen 15801180b4c7SJohn Johansen 15815d5182caSJohn Johansen /* 15825d5182caSJohn Johansen * Requires: @profile->ns->lock held 15835d5182caSJohn Johansen */ 1584c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) 15850d259f04SJohn Johansen { 15860d259f04SJohn Johansen struct aa_profile *child; 15870d259f04SJohn Johansen struct dentry *dent = NULL, *dir; 15880d259f04SJohn Johansen int error; 15890d259f04SJohn Johansen 1590cbf2d0e1SJohn Johansen AA_BUG(!profile); 1591cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock)); 1592cbf2d0e1SJohn Johansen 15930d259f04SJohn Johansen if (!parent) { 15940d259f04SJohn Johansen struct aa_profile *p; 15950d259f04SJohn Johansen p = aa_deref_parent(profile); 15960d259f04SJohn Johansen dent = prof_dir(p); 15970d259f04SJohn Johansen /* adding to parent that previously didn't have children */ 1598c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dent); 15990d259f04SJohn Johansen if (IS_ERR(dent)) 16000d259f04SJohn Johansen goto fail; 16010d259f04SJohn Johansen prof_child_dir(p) = parent = dent; 16020d259f04SJohn Johansen } 16030d259f04SJohn Johansen 16040d259f04SJohn Johansen if (!profile->dirname) { 16050d259f04SJohn Johansen int len, id_len; 16060d259f04SJohn Johansen len = mangle_name(profile->base.name, NULL); 16070d259f04SJohn Johansen id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); 16080d259f04SJohn Johansen 16090d259f04SJohn Johansen profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); 1610ffac1de6SDan Carpenter if (!profile->dirname) { 1611ffac1de6SDan Carpenter error = -ENOMEM; 1612ffac1de6SDan Carpenter goto fail2; 1613ffac1de6SDan Carpenter } 16140d259f04SJohn Johansen 16150d259f04SJohn Johansen mangle_name(profile->base.name, profile->dirname); 16160d259f04SJohn Johansen sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); 16170d259f04SJohn Johansen } 16180d259f04SJohn Johansen 1619c961ee5fSJohn Johansen dent = aafs_create_dir(profile->dirname, parent); 16200d259f04SJohn Johansen if (IS_ERR(dent)) 16210d259f04SJohn Johansen goto fail; 16220d259f04SJohn Johansen prof_dir(profile) = dir = dent; 16230d259f04SJohn Johansen 162452b97de3SJohn Johansen dent = create_profile_file(dir, "name", profile, 162552b97de3SJohn Johansen &seq_profile_name_fops); 16260d259f04SJohn Johansen if (IS_ERR(dent)) 16270d259f04SJohn Johansen goto fail; 16280d259f04SJohn Johansen profile->dents[AAFS_PROF_NAME] = dent; 16290d259f04SJohn Johansen 163052b97de3SJohn Johansen dent = create_profile_file(dir, "mode", profile, 163152b97de3SJohn Johansen &seq_profile_mode_fops); 16320d259f04SJohn Johansen if (IS_ERR(dent)) 16330d259f04SJohn Johansen goto fail; 16340d259f04SJohn Johansen profile->dents[AAFS_PROF_MODE] = dent; 16350d259f04SJohn Johansen 1636556d0be7SJohn Johansen dent = create_profile_file(dir, "attach", profile, 163752b97de3SJohn Johansen &seq_profile_attach_fops); 1638556d0be7SJohn Johansen if (IS_ERR(dent)) 1639556d0be7SJohn Johansen goto fail; 1640556d0be7SJohn Johansen profile->dents[AAFS_PROF_ATTACH] = dent; 1641556d0be7SJohn Johansen 1642f8eb8a13SJohn Johansen if (profile->hash) { 1643f8eb8a13SJohn Johansen dent = create_profile_file(dir, "sha1", profile, 164452b97de3SJohn Johansen &seq_profile_hash_fops); 1645f8eb8a13SJohn Johansen if (IS_ERR(dent)) 1646f8eb8a13SJohn Johansen goto fail; 1647f8eb8a13SJohn Johansen profile->dents[AAFS_PROF_HASH] = dent; 1648f8eb8a13SJohn Johansen } 1649f8eb8a13SJohn Johansen 16505ac8c355SJohn Johansen if (profile->rawdata) { 16511180b4c7SJohn Johansen dent = aafs_create_symlink("raw_sha1", dir, NULL, 16521180b4c7SJohn Johansen profile->label.proxy, 16531180b4c7SJohn Johansen &rawdata_link_sha1_iops); 16545ac8c355SJohn Johansen if (IS_ERR(dent)) 16555ac8c355SJohn Johansen goto fail; 16561180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 16575ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_HASH] = dent; 16585ac8c355SJohn Johansen 16591180b4c7SJohn Johansen dent = aafs_create_symlink("raw_abi", dir, NULL, 16601180b4c7SJohn Johansen profile->label.proxy, 16611180b4c7SJohn Johansen &rawdata_link_abi_iops); 16625ac8c355SJohn Johansen if (IS_ERR(dent)) 16635ac8c355SJohn Johansen goto fail; 16641180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 16655ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_ABI] = dent; 16665ac8c355SJohn Johansen 16671180b4c7SJohn Johansen dent = aafs_create_symlink("raw_data", dir, NULL, 16681180b4c7SJohn Johansen profile->label.proxy, 16691180b4c7SJohn Johansen &rawdata_link_data_iops); 16705ac8c355SJohn Johansen if (IS_ERR(dent)) 16715ac8c355SJohn Johansen goto fail; 16721180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 16735ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_DATA] = dent; 16745ac8c355SJohn Johansen } 16755ac8c355SJohn Johansen 16760d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) { 1677c97204baSJohn Johansen error = __aafs_profile_mkdir(child, prof_child_dir(profile)); 16780d259f04SJohn Johansen if (error) 16790d259f04SJohn Johansen goto fail2; 16800d259f04SJohn Johansen } 16810d259f04SJohn Johansen 16820d259f04SJohn Johansen return 0; 16830d259f04SJohn Johansen 16840d259f04SJohn Johansen fail: 16850d259f04SJohn Johansen error = PTR_ERR(dent); 16860d259f04SJohn Johansen 16870d259f04SJohn Johansen fail2: 1688c97204baSJohn Johansen __aafs_profile_rmdir(profile); 16890d259f04SJohn Johansen 16900d259f04SJohn Johansen return error; 16910d259f04SJohn Johansen } 16920d259f04SJohn Johansen 16934ae47f33SJohn Johansen static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode) 16944ae47f33SJohn Johansen { 16954ae47f33SJohn Johansen struct aa_ns *ns, *parent; 16964ae47f33SJohn Johansen /* TODO: improve permission check */ 1697637f688dSJohn Johansen struct aa_label *label; 1698637f688dSJohn Johansen int error; 1699637f688dSJohn Johansen 1700637f688dSJohn Johansen label = begin_current_label_crit_section(); 1701637f688dSJohn Johansen error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); 1702637f688dSJohn Johansen end_current_label_crit_section(label); 17034ae47f33SJohn Johansen if (error) 17044ae47f33SJohn Johansen return error; 17054ae47f33SJohn Johansen 17064ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 17074ae47f33SJohn Johansen AA_BUG(d_inode(ns_subns_dir(parent)) != dir); 17084ae47f33SJohn Johansen 17094ae47f33SJohn Johansen /* we have to unlock and then relock to get locking order right 17104ae47f33SJohn Johansen * for pin_fs 17114ae47f33SJohn Johansen */ 17124ae47f33SJohn Johansen inode_unlock(dir); 17134ae47f33SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 1714feb3c766SJohn Johansen mutex_lock_nested(&parent->lock, parent->level); 17154ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 17164ae47f33SJohn Johansen if (error) 17174ae47f33SJohn Johansen goto out; 17184ae47f33SJohn Johansen 17194ae47f33SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL, 17204ae47f33SJohn Johansen NULL, NULL, NULL); 17214ae47f33SJohn Johansen if (error) 17224ae47f33SJohn Johansen goto out_pin; 17234ae47f33SJohn Johansen 17244ae47f33SJohn Johansen ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name), 17254ae47f33SJohn Johansen dentry); 17264ae47f33SJohn Johansen if (IS_ERR(ns)) { 17274ae47f33SJohn Johansen error = PTR_ERR(ns); 17284ae47f33SJohn Johansen ns = NULL; 17294ae47f33SJohn Johansen } 17304ae47f33SJohn Johansen 17314ae47f33SJohn Johansen aa_put_ns(ns); /* list ref remains */ 17324ae47f33SJohn Johansen out_pin: 17334ae47f33SJohn Johansen if (error) 17344ae47f33SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 17354ae47f33SJohn Johansen out: 17364ae47f33SJohn Johansen mutex_unlock(&parent->lock); 17374ae47f33SJohn Johansen aa_put_ns(parent); 17384ae47f33SJohn Johansen 17394ae47f33SJohn Johansen return error; 17404ae47f33SJohn Johansen } 17414ae47f33SJohn Johansen 17424ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry) 17434ae47f33SJohn Johansen { 17444ae47f33SJohn Johansen struct aa_ns *ns, *parent; 17454ae47f33SJohn Johansen /* TODO: improve permission check */ 1746637f688dSJohn Johansen struct aa_label *label; 1747637f688dSJohn Johansen int error; 1748637f688dSJohn Johansen 1749637f688dSJohn Johansen label = begin_current_label_crit_section(); 1750637f688dSJohn Johansen error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); 1751637f688dSJohn Johansen end_current_label_crit_section(label); 17524ae47f33SJohn Johansen if (error) 17534ae47f33SJohn Johansen return error; 17544ae47f33SJohn Johansen 17554ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 17564ae47f33SJohn Johansen /* rmdir calls the generic securityfs functions to remove files 17574ae47f33SJohn Johansen * from the apparmor dir. It is up to the apparmor ns locking 17584ae47f33SJohn Johansen * to avoid races. 17594ae47f33SJohn Johansen */ 17604ae47f33SJohn Johansen inode_unlock(dir); 17614ae47f33SJohn Johansen inode_unlock(dentry->d_inode); 17624ae47f33SJohn Johansen 1763feb3c766SJohn Johansen mutex_lock_nested(&parent->lock, parent->level); 17644ae47f33SJohn Johansen ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name, 17654ae47f33SJohn Johansen dentry->d_name.len)); 17664ae47f33SJohn Johansen if (!ns) { 17674ae47f33SJohn Johansen error = -ENOENT; 17684ae47f33SJohn Johansen goto out; 17694ae47f33SJohn Johansen } 17704ae47f33SJohn Johansen AA_BUG(ns_dir(ns) != dentry); 17714ae47f33SJohn Johansen 17724ae47f33SJohn Johansen __aa_remove_ns(ns); 17734ae47f33SJohn Johansen aa_put_ns(ns); 17744ae47f33SJohn Johansen 17754ae47f33SJohn Johansen out: 17764ae47f33SJohn Johansen mutex_unlock(&parent->lock); 17774ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 17784ae47f33SJohn Johansen inode_lock(dentry->d_inode); 17794ae47f33SJohn Johansen aa_put_ns(parent); 17804ae47f33SJohn Johansen 17814ae47f33SJohn Johansen return error; 17824ae47f33SJohn Johansen } 17834ae47f33SJohn Johansen 17844ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = { 17854ae47f33SJohn Johansen .lookup = simple_lookup, 17864ae47f33SJohn Johansen .mkdir = ns_mkdir_op, 17874ae47f33SJohn Johansen .rmdir = ns_rmdir_op, 17884ae47f33SJohn Johansen }; 17894ae47f33SJohn Johansen 17905d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns) 17915d5182caSJohn Johansen { 17925d5182caSJohn Johansen struct aa_loaddata *ent, *tmp; 17935d5182caSJohn Johansen 17945d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 17955d5182caSJohn Johansen 17965d5182caSJohn Johansen list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list) 17975d5182caSJohn Johansen __aa_fs_remove_rawdata(ent); 17985d5182caSJohn Johansen } 17995d5182caSJohn Johansen 1800c97204baSJohn Johansen /** 1801c97204baSJohn Johansen * 1802c97204baSJohn Johansen * Requires: @ns->lock held 1803c97204baSJohn Johansen */ 1804c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns) 18050d259f04SJohn Johansen { 180698849dffSJohn Johansen struct aa_ns *sub; 18070d259f04SJohn Johansen struct aa_profile *child; 18080d259f04SJohn Johansen int i; 18090d259f04SJohn Johansen 18100d259f04SJohn Johansen if (!ns) 18110d259f04SJohn Johansen return; 1812cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 18130d259f04SJohn Johansen 18140d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) 1815c97204baSJohn Johansen __aafs_profile_rmdir(child); 18160d259f04SJohn Johansen 18170d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 1818feb3c766SJohn Johansen mutex_lock_nested(&sub->lock, sub->level); 1819c97204baSJohn Johansen __aafs_ns_rmdir(sub); 18200d259f04SJohn Johansen mutex_unlock(&sub->lock); 18210d259f04SJohn Johansen } 18220d259f04SJohn Johansen 18235d5182caSJohn Johansen __aa_fs_list_remove_rawdata(ns); 18245d5182caSJohn Johansen 1825b7fd2c03SJohn Johansen if (ns_subns_dir(ns)) { 1826b7fd2c03SJohn Johansen sub = d_inode(ns_subns_dir(ns))->i_private; 1827b7fd2c03SJohn Johansen aa_put_ns(sub); 1828b7fd2c03SJohn Johansen } 1829b7fd2c03SJohn Johansen if (ns_subload(ns)) { 1830b7fd2c03SJohn Johansen sub = d_inode(ns_subload(ns))->i_private; 1831b7fd2c03SJohn Johansen aa_put_ns(sub); 1832b7fd2c03SJohn Johansen } 1833b7fd2c03SJohn Johansen if (ns_subreplace(ns)) { 1834b7fd2c03SJohn Johansen sub = d_inode(ns_subreplace(ns))->i_private; 1835b7fd2c03SJohn Johansen aa_put_ns(sub); 1836b7fd2c03SJohn Johansen } 1837b7fd2c03SJohn Johansen if (ns_subremove(ns)) { 1838b7fd2c03SJohn Johansen sub = d_inode(ns_subremove(ns))->i_private; 1839b7fd2c03SJohn Johansen aa_put_ns(sub); 1840b7fd2c03SJohn Johansen } 1841d9bf2c26SJohn Johansen if (ns_subrevision(ns)) { 1842d9bf2c26SJohn Johansen sub = d_inode(ns_subrevision(ns))->i_private; 1843d9bf2c26SJohn Johansen aa_put_ns(sub); 1844d9bf2c26SJohn Johansen } 1845b7fd2c03SJohn Johansen 18460d259f04SJohn Johansen for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { 1847c961ee5fSJohn Johansen aafs_remove(ns->dents[i]); 18480d259f04SJohn Johansen ns->dents[i] = NULL; 18490d259f04SJohn Johansen } 18500d259f04SJohn Johansen } 18510d259f04SJohn Johansen 1852b7fd2c03SJohn Johansen /* assumes cleanup in caller */ 1853c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) 1854b7fd2c03SJohn Johansen { 1855b7fd2c03SJohn Johansen struct dentry *dent; 1856b7fd2c03SJohn Johansen 1857b7fd2c03SJohn Johansen AA_BUG(!ns); 1858b7fd2c03SJohn Johansen AA_BUG(!dir); 1859b7fd2c03SJohn Johansen 1860c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dir); 1861b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1862b7fd2c03SJohn Johansen return PTR_ERR(dent); 1863b7fd2c03SJohn Johansen ns_subprofs_dir(ns) = dent; 1864b7fd2c03SJohn Johansen 1865c961ee5fSJohn Johansen dent = aafs_create_dir("raw_data", dir); 1866b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1867b7fd2c03SJohn Johansen return PTR_ERR(dent); 1868b7fd2c03SJohn Johansen ns_subdata_dir(ns) = dent; 1869b7fd2c03SJohn Johansen 1870d9bf2c26SJohn Johansen dent = aafs_create_file("revision", 0444, dir, ns, 1871d9bf2c26SJohn Johansen &aa_fs_ns_revision_fops); 1872d9bf2c26SJohn Johansen if (IS_ERR(dent)) 1873d9bf2c26SJohn Johansen return PTR_ERR(dent); 1874d9bf2c26SJohn Johansen aa_get_ns(ns); 1875d9bf2c26SJohn Johansen ns_subrevision(ns) = dent; 1876d9bf2c26SJohn Johansen 1877c961ee5fSJohn Johansen dent = aafs_create_file(".load", 0640, dir, ns, 1878b7fd2c03SJohn Johansen &aa_fs_profile_load); 1879b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1880b7fd2c03SJohn Johansen return PTR_ERR(dent); 1881b7fd2c03SJohn Johansen aa_get_ns(ns); 1882b7fd2c03SJohn Johansen ns_subload(ns) = dent; 1883b7fd2c03SJohn Johansen 1884c961ee5fSJohn Johansen dent = aafs_create_file(".replace", 0640, dir, ns, 1885b7fd2c03SJohn Johansen &aa_fs_profile_replace); 1886b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1887b7fd2c03SJohn Johansen return PTR_ERR(dent); 1888b7fd2c03SJohn Johansen aa_get_ns(ns); 1889b7fd2c03SJohn Johansen ns_subreplace(ns) = dent; 1890b7fd2c03SJohn Johansen 1891c961ee5fSJohn Johansen dent = aafs_create_file(".remove", 0640, dir, ns, 1892b7fd2c03SJohn Johansen &aa_fs_profile_remove); 1893b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1894b7fd2c03SJohn Johansen return PTR_ERR(dent); 1895b7fd2c03SJohn Johansen aa_get_ns(ns); 1896b7fd2c03SJohn Johansen ns_subremove(ns) = dent; 1897b7fd2c03SJohn Johansen 18984ae47f33SJohn Johansen /* use create_dentry so we can supply private data */ 18994ae47f33SJohn Johansen dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL, 19004ae47f33SJohn Johansen &ns_dir_inode_operations); 1901b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1902b7fd2c03SJohn Johansen return PTR_ERR(dent); 1903b7fd2c03SJohn Johansen aa_get_ns(ns); 1904b7fd2c03SJohn Johansen ns_subns_dir(ns) = dent; 1905b7fd2c03SJohn Johansen 1906b7fd2c03SJohn Johansen return 0; 1907b7fd2c03SJohn Johansen } 1908b7fd2c03SJohn Johansen 1909c97204baSJohn Johansen /* 1910c97204baSJohn Johansen * Requires: @ns->lock held 1911c97204baSJohn Johansen */ 191298407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name, 191398407f0aSJohn Johansen struct dentry *dent) 19140d259f04SJohn Johansen { 191598849dffSJohn Johansen struct aa_ns *sub; 19160d259f04SJohn Johansen struct aa_profile *child; 191798407f0aSJohn Johansen struct dentry *dir; 19180d259f04SJohn Johansen int error; 19190d259f04SJohn Johansen 1920b7fd2c03SJohn Johansen AA_BUG(!ns); 1921b7fd2c03SJohn Johansen AA_BUG(!parent); 1922b7fd2c03SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 1923b7fd2c03SJohn Johansen 19240d259f04SJohn Johansen if (!name) 19250d259f04SJohn Johansen name = ns->base.name; 19260d259f04SJohn Johansen 1927c961ee5fSJohn Johansen if (!dent) { 1928b7fd2c03SJohn Johansen /* create ns dir if it doesn't already exist */ 1929c961ee5fSJohn Johansen dent = aafs_create_dir(name, parent); 19300d259f04SJohn Johansen if (IS_ERR(dent)) 19310d259f04SJohn Johansen goto fail; 1932c961ee5fSJohn Johansen } else 1933c961ee5fSJohn Johansen dget(dent); 19340d259f04SJohn Johansen ns_dir(ns) = dir = dent; 1935c97204baSJohn Johansen error = __aafs_ns_mkdir_entries(ns, dir); 1936b7fd2c03SJohn Johansen if (error) 1937b7fd2c03SJohn Johansen goto fail2; 19380d259f04SJohn Johansen 1939b7fd2c03SJohn Johansen /* profiles */ 19400d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) { 1941c97204baSJohn Johansen error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns)); 19420d259f04SJohn Johansen if (error) 19430d259f04SJohn Johansen goto fail2; 19440d259f04SJohn Johansen } 19450d259f04SJohn Johansen 1946b7fd2c03SJohn Johansen /* subnamespaces */ 19470d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 1948feb3c766SJohn Johansen mutex_lock_nested(&sub->lock, sub->level); 194998407f0aSJohn Johansen error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL); 19500d259f04SJohn Johansen mutex_unlock(&sub->lock); 19510d259f04SJohn Johansen if (error) 19520d259f04SJohn Johansen goto fail2; 19530d259f04SJohn Johansen } 19540d259f04SJohn Johansen 19550d259f04SJohn Johansen return 0; 19560d259f04SJohn Johansen 19570d259f04SJohn Johansen fail: 19580d259f04SJohn Johansen error = PTR_ERR(dent); 19590d259f04SJohn Johansen 19600d259f04SJohn Johansen fail2: 1961c97204baSJohn Johansen __aafs_ns_rmdir(ns); 19620d259f04SJohn Johansen 19630d259f04SJohn Johansen return error; 19640d259f04SJohn Johansen } 19650d259f04SJohn Johansen 19660d259f04SJohn Johansen 196729b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head)) 196829b3822fSJohn Johansen 196929b3822fSJohn Johansen /** 197098849dffSJohn Johansen * __next_ns - find the next namespace to list 197129b3822fSJohn Johansen * @root: root namespace to stop search at (NOT NULL) 197229b3822fSJohn Johansen * @ns: current ns position (NOT NULL) 197329b3822fSJohn Johansen * 197429b3822fSJohn Johansen * Find the next namespace from @ns under @root and handle all locking needed 197529b3822fSJohn Johansen * while switching current namespace. 197629b3822fSJohn Johansen * 197729b3822fSJohn Johansen * Returns: next namespace or NULL if at last namespace under @root 197829b3822fSJohn Johansen * Requires: ns->parent->lock to be held 197929b3822fSJohn Johansen * NOTE: will not unlock root->lock 198029b3822fSJohn Johansen */ 198198849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) 198229b3822fSJohn Johansen { 198398849dffSJohn Johansen struct aa_ns *parent, *next; 198429b3822fSJohn Johansen 1985cbf2d0e1SJohn Johansen AA_BUG(!root); 1986cbf2d0e1SJohn Johansen AA_BUG(!ns); 1987cbf2d0e1SJohn Johansen AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock)); 1988cbf2d0e1SJohn Johansen 198929b3822fSJohn Johansen /* is next namespace a child */ 199029b3822fSJohn Johansen if (!list_empty(&ns->sub_ns)) { 199129b3822fSJohn Johansen next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); 1992feb3c766SJohn Johansen mutex_lock_nested(&next->lock, next->level); 199329b3822fSJohn Johansen return next; 199429b3822fSJohn Johansen } 199529b3822fSJohn Johansen 199629b3822fSJohn Johansen /* check if the next ns is a sibling, parent, gp, .. */ 199729b3822fSJohn Johansen parent = ns->parent; 1998ed2c7da3SJohn Johansen while (ns != root) { 199929b3822fSJohn Johansen mutex_unlock(&ns->lock); 200038dbd7d8SGeliang Tang next = list_next_entry(ns, base.list); 200129b3822fSJohn Johansen if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { 2002feb3c766SJohn Johansen mutex_lock_nested(&next->lock, next->level); 200329b3822fSJohn Johansen return next; 200429b3822fSJohn Johansen } 200529b3822fSJohn Johansen ns = parent; 200629b3822fSJohn Johansen parent = parent->parent; 200729b3822fSJohn Johansen } 200829b3822fSJohn Johansen 200929b3822fSJohn Johansen return NULL; 201029b3822fSJohn Johansen } 201129b3822fSJohn Johansen 201229b3822fSJohn Johansen /** 201329b3822fSJohn Johansen * __first_profile - find the first profile in a namespace 201429b3822fSJohn Johansen * @root: namespace that is root of profiles being displayed (NOT NULL) 201529b3822fSJohn Johansen * @ns: namespace to start in (NOT NULL) 201629b3822fSJohn Johansen * 201729b3822fSJohn Johansen * Returns: unrefcounted profile or NULL if no profile 201829b3822fSJohn Johansen * Requires: profile->ns.lock to be held 201929b3822fSJohn Johansen */ 202098849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root, 202198849dffSJohn Johansen struct aa_ns *ns) 202229b3822fSJohn Johansen { 2023cbf2d0e1SJohn Johansen AA_BUG(!root); 2024cbf2d0e1SJohn Johansen AA_BUG(ns && !mutex_is_locked(&ns->lock)); 2025cbf2d0e1SJohn Johansen 202698849dffSJohn Johansen for (; ns; ns = __next_ns(root, ns)) { 202729b3822fSJohn Johansen if (!list_empty(&ns->base.profiles)) 202829b3822fSJohn Johansen return list_first_entry(&ns->base.profiles, 202929b3822fSJohn Johansen struct aa_profile, base.list); 203029b3822fSJohn Johansen } 203129b3822fSJohn Johansen return NULL; 203229b3822fSJohn Johansen } 203329b3822fSJohn Johansen 203429b3822fSJohn Johansen /** 203529b3822fSJohn Johansen * __next_profile - step to the next profile in a profile tree 203629b3822fSJohn Johansen * @profile: current profile in tree (NOT NULL) 203729b3822fSJohn Johansen * 203829b3822fSJohn Johansen * Perform a depth first traversal on the profile tree in a namespace 203929b3822fSJohn Johansen * 204029b3822fSJohn Johansen * Returns: next profile or NULL if done 204129b3822fSJohn Johansen * Requires: profile->ns.lock to be held 204229b3822fSJohn Johansen */ 204329b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p) 204429b3822fSJohn Johansen { 204529b3822fSJohn Johansen struct aa_profile *parent; 204698849dffSJohn Johansen struct aa_ns *ns = p->ns; 204729b3822fSJohn Johansen 2048cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock)); 2049cbf2d0e1SJohn Johansen 205029b3822fSJohn Johansen /* is next profile a child */ 205129b3822fSJohn Johansen if (!list_empty(&p->base.profiles)) 205229b3822fSJohn Johansen return list_first_entry(&p->base.profiles, typeof(*p), 205329b3822fSJohn Johansen base.list); 205429b3822fSJohn Johansen 205529b3822fSJohn Johansen /* is next profile a sibling, parent sibling, gp, sibling, .. */ 205629b3822fSJohn Johansen parent = rcu_dereference_protected(p->parent, 205729b3822fSJohn Johansen mutex_is_locked(&p->ns->lock)); 205829b3822fSJohn Johansen while (parent) { 205938dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 206029b3822fSJohn Johansen if (!list_entry_is_head(p, &parent->base.profiles, base.list)) 206129b3822fSJohn Johansen return p; 206229b3822fSJohn Johansen p = parent; 206329b3822fSJohn Johansen parent = rcu_dereference_protected(parent->parent, 206429b3822fSJohn Johansen mutex_is_locked(&parent->ns->lock)); 206529b3822fSJohn Johansen } 206629b3822fSJohn Johansen 206729b3822fSJohn Johansen /* is next another profile in the namespace */ 206838dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 206929b3822fSJohn Johansen if (!list_entry_is_head(p, &ns->base.profiles, base.list)) 207029b3822fSJohn Johansen return p; 207129b3822fSJohn Johansen 207229b3822fSJohn Johansen return NULL; 207329b3822fSJohn Johansen } 207429b3822fSJohn Johansen 207529b3822fSJohn Johansen /** 207629b3822fSJohn Johansen * next_profile - step to the next profile in where ever it may be 207729b3822fSJohn Johansen * @root: root namespace (NOT NULL) 207829b3822fSJohn Johansen * @profile: current profile (NOT NULL) 207929b3822fSJohn Johansen * 208029b3822fSJohn Johansen * Returns: next profile or NULL if there isn't one 208129b3822fSJohn Johansen */ 208298849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root, 208329b3822fSJohn Johansen struct aa_profile *profile) 208429b3822fSJohn Johansen { 208529b3822fSJohn Johansen struct aa_profile *next = __next_profile(profile); 208629b3822fSJohn Johansen if (next) 208729b3822fSJohn Johansen return next; 208829b3822fSJohn Johansen 208929b3822fSJohn Johansen /* finished all profiles in namespace move to next namespace */ 209098849dffSJohn Johansen return __first_profile(root, __next_ns(root, profile->ns)); 209129b3822fSJohn Johansen } 209229b3822fSJohn Johansen 209329b3822fSJohn Johansen /** 209429b3822fSJohn Johansen * p_start - start a depth first traversal of profile tree 209529b3822fSJohn Johansen * @f: seq_file to fill 209629b3822fSJohn Johansen * @pos: current position 209729b3822fSJohn Johansen * 209829b3822fSJohn Johansen * Returns: first profile under current namespace or NULL if none found 209929b3822fSJohn Johansen * 210029b3822fSJohn Johansen * acquires first ns->lock 210129b3822fSJohn Johansen */ 210229b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos) 210329b3822fSJohn Johansen { 210429b3822fSJohn Johansen struct aa_profile *profile = NULL; 2105cf797c0eSJohn Johansen struct aa_ns *root = aa_get_current_ns(); 210629b3822fSJohn Johansen loff_t l = *pos; 2107cf797c0eSJohn Johansen f->private = root; 210829b3822fSJohn Johansen 210929b3822fSJohn Johansen /* find the first profile */ 2110feb3c766SJohn Johansen mutex_lock_nested(&root->lock, root->level); 211129b3822fSJohn Johansen profile = __first_profile(root, root); 211229b3822fSJohn Johansen 211329b3822fSJohn Johansen /* skip to position */ 211429b3822fSJohn Johansen for (; profile && l > 0; l--) 211529b3822fSJohn Johansen profile = next_profile(root, profile); 211629b3822fSJohn Johansen 211729b3822fSJohn Johansen return profile; 211829b3822fSJohn Johansen } 211929b3822fSJohn Johansen 212029b3822fSJohn Johansen /** 212129b3822fSJohn Johansen * p_next - read the next profile entry 212229b3822fSJohn Johansen * @f: seq_file to fill 212329b3822fSJohn Johansen * @p: profile previously returned 212429b3822fSJohn Johansen * @pos: current position 212529b3822fSJohn Johansen * 212629b3822fSJohn Johansen * Returns: next profile after @p or NULL if none 212729b3822fSJohn Johansen * 212829b3822fSJohn Johansen * may acquire/release locks in namespace tree as necessary 212929b3822fSJohn Johansen */ 213029b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos) 213129b3822fSJohn Johansen { 213229b3822fSJohn Johansen struct aa_profile *profile = p; 213398849dffSJohn Johansen struct aa_ns *ns = f->private; 213429b3822fSJohn Johansen (*pos)++; 213529b3822fSJohn Johansen 213629b3822fSJohn Johansen return next_profile(ns, profile); 213729b3822fSJohn Johansen } 213829b3822fSJohn Johansen 213929b3822fSJohn Johansen /** 214029b3822fSJohn Johansen * p_stop - stop depth first traversal 214129b3822fSJohn Johansen * @f: seq_file we are filling 214229b3822fSJohn Johansen * @p: the last profile writen 214329b3822fSJohn Johansen * 214429b3822fSJohn Johansen * Release all locking done by p_start/p_next on namespace tree 214529b3822fSJohn Johansen */ 214629b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p) 214729b3822fSJohn Johansen { 214829b3822fSJohn Johansen struct aa_profile *profile = p; 214998849dffSJohn Johansen struct aa_ns *root = f->private, *ns; 215029b3822fSJohn Johansen 215129b3822fSJohn Johansen if (profile) { 215229b3822fSJohn Johansen for (ns = profile->ns; ns && ns != root; ns = ns->parent) 215329b3822fSJohn Johansen mutex_unlock(&ns->lock); 215429b3822fSJohn Johansen } 215529b3822fSJohn Johansen mutex_unlock(&root->lock); 215698849dffSJohn Johansen aa_put_ns(root); 215729b3822fSJohn Johansen } 215829b3822fSJohn Johansen 215929b3822fSJohn Johansen /** 216029b3822fSJohn Johansen * seq_show_profile - show a profile entry 216129b3822fSJohn Johansen * @f: seq_file to file 216229b3822fSJohn Johansen * @p: current position (profile) (NOT NULL) 216329b3822fSJohn Johansen * 216429b3822fSJohn Johansen * Returns: error on failure 216529b3822fSJohn Johansen */ 216629b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p) 216729b3822fSJohn Johansen { 216829b3822fSJohn Johansen struct aa_profile *profile = (struct aa_profile *)p; 216998849dffSJohn Johansen struct aa_ns *root = f->private; 217029b3822fSJohn Johansen 2171637f688dSJohn Johansen aa_label_seq_xprint(f, root, &profile->label, 2172637f688dSJohn Johansen FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL); 2173637f688dSJohn Johansen seq_putc(f, '\n'); 217429b3822fSJohn Johansen 217529b3822fSJohn Johansen return 0; 217629b3822fSJohn Johansen } 217729b3822fSJohn Johansen 2178c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = { 217929b3822fSJohn Johansen .start = p_start, 218029b3822fSJohn Johansen .next = p_next, 218129b3822fSJohn Johansen .stop = p_stop, 218229b3822fSJohn Johansen .show = seq_show_profile, 218329b3822fSJohn Johansen }; 218429b3822fSJohn Johansen 218529b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file) 218629b3822fSJohn Johansen { 21875ac8c355SJohn Johansen if (!policy_view_capable(NULL)) 21885ac8c355SJohn Johansen return -EACCES; 21895ac8c355SJohn Johansen 2190c97204baSJohn Johansen return seq_open(file, &aa_sfs_profiles_op); 219129b3822fSJohn Johansen } 219229b3822fSJohn Johansen 219329b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file) 219429b3822fSJohn Johansen { 219529b3822fSJohn Johansen return seq_release(inode, file); 219629b3822fSJohn Johansen } 219729b3822fSJohn Johansen 2198c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = { 219929b3822fSJohn Johansen .open = profiles_open, 220029b3822fSJohn Johansen .read = seq_read, 220129b3822fSJohn Johansen .llseek = seq_lseek, 220229b3822fSJohn Johansen .release = profiles_release, 220329b3822fSJohn Johansen }; 220429b3822fSJohn Johansen 220529b3822fSJohn Johansen 22060d259f04SJohn Johansen /** Base file system setup **/ 2207c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = { 2208c97204baSJohn Johansen AA_SFS_FILE_STRING("mask", 2209c97204baSJohn Johansen "create read write exec append mmap_exec link lock"), 2210a9bf8e9fSKees Cook { } 2211a9bf8e9fSKees Cook }; 2212a9bf8e9fSKees Cook 2213290f458aSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ptrace[] = { 2214290f458aSJohn Johansen AA_SFS_FILE_STRING("mask", "read trace"), 2215290f458aSJohn Johansen { } 2216290f458aSJohn Johansen }; 2217290f458aSJohn Johansen 2218cd1dbf76SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_signal[] = { 2219cd1dbf76SJohn Johansen AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK), 2220cd1dbf76SJohn Johansen { } 2221cd1dbf76SJohn Johansen }; 2222cd1dbf76SJohn Johansen 222373f488cdSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_attach[] = { 222473f488cdSJohn Johansen AA_SFS_FILE_BOOLEAN("xattr", 1), 222573f488cdSJohn Johansen { } 222673f488cdSJohn Johansen }; 2227c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = { 2228c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hat", 1), 2229c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hatv", 1), 2230c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_onexec", 1), 2231c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_profile", 1), 22326c5fc8f1SJohn Johansen AA_SFS_FILE_BOOLEAN("stack", 1), 2233c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), 22349fcf78ccSJohn Johansen AA_SFS_FILE_BOOLEAN("post_nnp_subset", 1), 223521f60661SJohn Johansen AA_SFS_FILE_BOOLEAN("computed_longest_left", 1), 223673f488cdSJohn Johansen AA_SFS_DIR("attach_conditions", aa_sfs_entry_attach), 2237c97204baSJohn Johansen AA_SFS_FILE_STRING("version", "1.2"), 2238e74abcf3SKees Cook { } 2239e74abcf3SKees Cook }; 2240e74abcf3SKees Cook 2241c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = { 2242c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("v5", 1), 22435379a331SJohn Johansen AA_SFS_FILE_BOOLEAN("v6", 1), 22445379a331SJohn Johansen AA_SFS_FILE_BOOLEAN("v7", 1), 224556974a6fSJohn Johansen AA_SFS_FILE_BOOLEAN("v8", 1), 2246474d6b75SJohn Johansen { } 2247474d6b75SJohn Johansen }; 2248474d6b75SJohn Johansen 2249c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = { 2250c97204baSJohn Johansen AA_SFS_DIR("versions", aa_sfs_entry_versions), 2251c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("set_load", 1), 22529d910a3bSJohn Johansen { } 22539d910a3bSJohn Johansen }; 22549d910a3bSJohn Johansen 22552ea3ffb7SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_mount[] = { 22562ea3ffb7SJohn Johansen AA_SFS_FILE_STRING("mask", "mount umount pivot_root"), 22572ea3ffb7SJohn Johansen { } 22582ea3ffb7SJohn Johansen }; 22592ea3ffb7SJohn Johansen 226033f2eadaSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ns[] = { 226133f2eadaSJohn Johansen AA_SFS_FILE_BOOLEAN("profile", 1), 22622ea3ffb7SJohn Johansen AA_SFS_FILE_BOOLEAN("pivot_root", 0), 226333f2eadaSJohn Johansen { } 226433f2eadaSJohn Johansen }; 226533f2eadaSJohn Johansen 2266a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = { 22674f3b3f2dSJohn Johansen AA_SFS_FILE_STRING("perms", "allow deny audit quiet"), 2268a83bd86eSJohn Johansen AA_SFS_FILE_BOOLEAN("data", 1), 22691dea3b41SJohn Johansen AA_SFS_FILE_BOOLEAN("multi_transaction", 1), 2270a83bd86eSJohn Johansen { } 2271a83bd86eSJohn Johansen }; 2272a83bd86eSJohn Johansen 2273a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = { 2274a83bd86eSJohn Johansen AA_SFS_DIR("label", aa_sfs_entry_query_label), 2275a83bd86eSJohn Johansen { } 2276a83bd86eSJohn Johansen }; 2277c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = { 2278c97204baSJohn Johansen AA_SFS_DIR("policy", aa_sfs_entry_policy), 2279c97204baSJohn Johansen AA_SFS_DIR("domain", aa_sfs_entry_domain), 2280c97204baSJohn Johansen AA_SFS_DIR("file", aa_sfs_entry_file), 228156974a6fSJohn Johansen AA_SFS_DIR("network_v8", aa_sfs_entry_network), 22822ea3ffb7SJohn Johansen AA_SFS_DIR("mount", aa_sfs_entry_mount), 228333f2eadaSJohn Johansen AA_SFS_DIR("namespaces", aa_sfs_entry_ns), 2284c97204baSJohn Johansen AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 2285c97204baSJohn Johansen AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit), 2286c97204baSJohn Johansen AA_SFS_DIR("caps", aa_sfs_entry_caps), 2287290f458aSJohn Johansen AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace), 2288cd1dbf76SJohn Johansen AA_SFS_DIR("signal", aa_sfs_entry_signal), 2289a83bd86eSJohn Johansen AA_SFS_DIR("query", aa_sfs_entry_query), 2290e74abcf3SKees Cook { } 2291e74abcf3SKees Cook }; 2292e74abcf3SKees Cook 2293c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = { 2294bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access), 22956c5fc8f1SJohn Johansen AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops), 22966c5fc8f1SJohn Johansen AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops), 2297bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops), 2298bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops), 2299bf81100fSJohn Johansen AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops), 2300c97204baSJohn Johansen AA_SFS_DIR("features", aa_sfs_entry_features), 23019acd494bSKees Cook { } 23029acd494bSKees Cook }; 230363e2b423SJohn Johansen 2304c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry = 2305c97204baSJohn Johansen AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor); 23069acd494bSKees Cook 23079acd494bSKees Cook /** 2308a481f4d9SJohn Johansen * entry_create_file - create a file entry in the apparmor securityfs 2309c97204baSJohn Johansen * @fs_file: aa_sfs_entry to build an entry for (NOT NULL) 23109acd494bSKees Cook * @parent: the parent dentry in the securityfs 23119acd494bSKees Cook * 2312a481f4d9SJohn Johansen * Use entry_remove_file to remove entries created with this fn. 23139acd494bSKees Cook */ 2314c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file, 23159acd494bSKees Cook struct dentry *parent) 231663e2b423SJohn Johansen { 23179acd494bSKees Cook int error = 0; 231863e2b423SJohn Johansen 23199acd494bSKees Cook fs_file->dentry = securityfs_create_file(fs_file->name, 23209acd494bSKees Cook S_IFREG | fs_file->mode, 23219acd494bSKees Cook parent, fs_file, 23229acd494bSKees Cook fs_file->file_ops); 23239acd494bSKees Cook if (IS_ERR(fs_file->dentry)) { 23249acd494bSKees Cook error = PTR_ERR(fs_file->dentry); 23259acd494bSKees Cook fs_file->dentry = NULL; 232663e2b423SJohn Johansen } 23279acd494bSKees Cook return error; 232863e2b423SJohn Johansen } 232963e2b423SJohn Johansen 2330c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir); 233163e2b423SJohn Johansen /** 2332a481f4d9SJohn Johansen * entry_create_dir - recursively create a directory entry in the securityfs 2333c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL) 23349acd494bSKees Cook * @parent: the parent dentry in the securityfs 233563e2b423SJohn Johansen * 2336a481f4d9SJohn Johansen * Use entry_remove_dir to remove entries created with this fn. 233763e2b423SJohn Johansen */ 2338c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir, 23399acd494bSKees Cook struct dentry *parent) 234063e2b423SJohn Johansen { 2341c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 23420d259f04SJohn Johansen struct dentry *dir; 23430d259f04SJohn Johansen int error; 234463e2b423SJohn Johansen 23450d259f04SJohn Johansen dir = securityfs_create_dir(fs_dir->name, parent); 23460d259f04SJohn Johansen if (IS_ERR(dir)) 23470d259f04SJohn Johansen return PTR_ERR(dir); 23480d259f04SJohn Johansen fs_dir->dentry = dir; 234963e2b423SJohn Johansen 23500d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2351c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2352a481f4d9SJohn Johansen error = entry_create_dir(fs_file, fs_dir->dentry); 23539acd494bSKees Cook else 2354a481f4d9SJohn Johansen error = entry_create_file(fs_file, fs_dir->dentry); 23559acd494bSKees Cook if (error) 23569acd494bSKees Cook goto failed; 23579acd494bSKees Cook } 23589acd494bSKees Cook 23599acd494bSKees Cook return 0; 23609acd494bSKees Cook 23619acd494bSKees Cook failed: 2362a481f4d9SJohn Johansen entry_remove_dir(fs_dir); 23630d259f04SJohn Johansen 23649acd494bSKees Cook return error; 23659acd494bSKees Cook } 23669acd494bSKees Cook 23679acd494bSKees Cook /** 2368c97204baSJohn Johansen * entry_remove_file - drop a single file entry in the apparmor securityfs 2369c97204baSJohn Johansen * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL) 23709acd494bSKees Cook */ 2371c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file) 23729acd494bSKees Cook { 23739acd494bSKees Cook if (!fs_file->dentry) 23749acd494bSKees Cook return; 23759acd494bSKees Cook 23769acd494bSKees Cook securityfs_remove(fs_file->dentry); 23779acd494bSKees Cook fs_file->dentry = NULL; 23789acd494bSKees Cook } 23799acd494bSKees Cook 23809acd494bSKees Cook /** 2381a481f4d9SJohn Johansen * entry_remove_dir - recursively drop a directory entry from the securityfs 2382c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL) 23839acd494bSKees Cook */ 2384c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir) 23859acd494bSKees Cook { 2386c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 23879acd494bSKees Cook 23880d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2389c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2390a481f4d9SJohn Johansen entry_remove_dir(fs_file); 23919acd494bSKees Cook else 2392c97204baSJohn Johansen entry_remove_file(fs_file); 23939acd494bSKees Cook } 23949acd494bSKees Cook 2395c97204baSJohn Johansen entry_remove_file(fs_dir); 239663e2b423SJohn Johansen } 239763e2b423SJohn Johansen 239863e2b423SJohn Johansen /** 239963e2b423SJohn Johansen * aa_destroy_aafs - cleanup and free aafs 240063e2b423SJohn Johansen * 240163e2b423SJohn Johansen * releases dentries allocated by aa_create_aafs 240263e2b423SJohn Johansen */ 240363e2b423SJohn Johansen void __init aa_destroy_aafs(void) 240463e2b423SJohn Johansen { 2405c97204baSJohn Johansen entry_remove_dir(&aa_sfs_entry); 240663e2b423SJohn Johansen } 240763e2b423SJohn Johansen 2408a71ada30SJohn Johansen 2409a71ada30SJohn Johansen #define NULL_FILE_NAME ".null" 2410a71ada30SJohn Johansen struct path aa_null; 2411a71ada30SJohn Johansen 2412a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent) 2413a71ada30SJohn Johansen { 2414a71ada30SJohn Johansen struct vfsmount *mount = NULL; 2415a71ada30SJohn Johansen struct dentry *dentry; 2416a71ada30SJohn Johansen struct inode *inode; 2417a71ada30SJohn Johansen int count = 0; 2418a71ada30SJohn Johansen int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); 2419a71ada30SJohn Johansen 2420a71ada30SJohn Johansen if (error) 2421a71ada30SJohn Johansen return error; 2422a71ada30SJohn Johansen 2423a71ada30SJohn Johansen inode_lock(d_inode(parent)); 2424a71ada30SJohn Johansen dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); 2425a71ada30SJohn Johansen if (IS_ERR(dentry)) { 2426a71ada30SJohn Johansen error = PTR_ERR(dentry); 2427a71ada30SJohn Johansen goto out; 2428a71ada30SJohn Johansen } 2429a71ada30SJohn Johansen inode = new_inode(parent->d_inode->i_sb); 2430a71ada30SJohn Johansen if (!inode) { 2431a71ada30SJohn Johansen error = -ENOMEM; 2432a71ada30SJohn Johansen goto out1; 2433a71ada30SJohn Johansen } 2434a71ada30SJohn Johansen 2435a71ada30SJohn Johansen inode->i_ino = get_next_ino(); 2436a71ada30SJohn Johansen inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; 243724d0d03cSDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2438a71ada30SJohn Johansen init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, 2439a71ada30SJohn Johansen MKDEV(MEM_MAJOR, 3)); 2440a71ada30SJohn Johansen d_instantiate(dentry, inode); 2441a71ada30SJohn Johansen aa_null.dentry = dget(dentry); 2442a71ada30SJohn Johansen aa_null.mnt = mntget(mount); 2443a71ada30SJohn Johansen 2444a71ada30SJohn Johansen error = 0; 2445a71ada30SJohn Johansen 2446a71ada30SJohn Johansen out1: 2447a71ada30SJohn Johansen dput(dentry); 2448a71ada30SJohn Johansen out: 2449a71ada30SJohn Johansen inode_unlock(d_inode(parent)); 2450a71ada30SJohn Johansen simple_release_fs(&mount, &count); 2451a71ada30SJohn Johansen return error; 2452a71ada30SJohn Johansen } 2453a71ada30SJohn Johansen 2454a481f4d9SJohn Johansen 2455a481f4d9SJohn Johansen 2456a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry, 2457a481f4d9SJohn Johansen struct inode *inode, 2458a481f4d9SJohn Johansen struct delayed_call *done) 2459a481f4d9SJohn Johansen { 2460a481f4d9SJohn Johansen struct aa_ns *ns; 2461a481f4d9SJohn Johansen struct path path; 2462a481f4d9SJohn Johansen 2463a481f4d9SJohn Johansen if (!dentry) 2464a481f4d9SJohn Johansen return ERR_PTR(-ECHILD); 2465a481f4d9SJohn Johansen ns = aa_get_current_ns(); 2466a481f4d9SJohn Johansen path.mnt = mntget(aafs_mnt); 2467a481f4d9SJohn Johansen path.dentry = dget(ns_dir(ns)); 2468a481f4d9SJohn Johansen nd_jump_link(&path); 2469a481f4d9SJohn Johansen aa_put_ns(ns); 2470a481f4d9SJohn Johansen 2471a481f4d9SJohn Johansen return NULL; 2472a481f4d9SJohn Johansen } 2473a481f4d9SJohn Johansen 2474a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer, 2475a481f4d9SJohn Johansen int buflen) 2476a481f4d9SJohn Johansen { 2477a481f4d9SJohn Johansen char name[32]; 2478a481f4d9SJohn Johansen int res; 2479a481f4d9SJohn Johansen 2480a0781209SJohn Johansen res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME, 2481a0781209SJohn Johansen d_inode(dentry)->i_ino); 2482a0781209SJohn Johansen if (res > 0 && res < sizeof(name)) 2483a481f4d9SJohn Johansen res = readlink_copy(buffer, buflen, name); 2484a0781209SJohn Johansen else 2485a0781209SJohn Johansen res = -ENOENT; 2486a481f4d9SJohn Johansen 2487a481f4d9SJohn Johansen return res; 2488a481f4d9SJohn Johansen } 2489a481f4d9SJohn Johansen 2490a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = { 2491a481f4d9SJohn Johansen .readlink = policy_readlink, 2492a481f4d9SJohn Johansen .get_link = policy_get_link, 2493a481f4d9SJohn Johansen }; 2494a481f4d9SJohn Johansen 2495a481f4d9SJohn Johansen 249663e2b423SJohn Johansen /** 249763e2b423SJohn Johansen * aa_create_aafs - create the apparmor security filesystem 249863e2b423SJohn Johansen * 249963e2b423SJohn Johansen * dentries created here are released by aa_destroy_aafs 250063e2b423SJohn Johansen * 250163e2b423SJohn Johansen * Returns: error on failure 250263e2b423SJohn Johansen */ 25033417d8d5SJames Morris static int __init aa_create_aafs(void) 250463e2b423SJohn Johansen { 2505b7fd2c03SJohn Johansen struct dentry *dent; 250663e2b423SJohn Johansen int error; 250763e2b423SJohn Johansen 250863e2b423SJohn Johansen if (!apparmor_initialized) 250963e2b423SJohn Johansen return 0; 251063e2b423SJohn Johansen 2511c97204baSJohn Johansen if (aa_sfs_entry.dentry) { 251263e2b423SJohn Johansen AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 251363e2b423SJohn Johansen return -EEXIST; 251463e2b423SJohn Johansen } 251563e2b423SJohn Johansen 2516a481f4d9SJohn Johansen /* setup apparmorfs used to virtualize policy/ */ 2517a481f4d9SJohn Johansen aafs_mnt = kern_mount(&aafs_ops); 2518a481f4d9SJohn Johansen if (IS_ERR(aafs_mnt)) 2519a481f4d9SJohn Johansen panic("can't set apparmorfs up\n"); 25201751e8a6SLinus Torvalds aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER; 2521a481f4d9SJohn Johansen 25229acd494bSKees Cook /* Populate fs tree. */ 2523c97204baSJohn Johansen error = entry_create_dir(&aa_sfs_entry, NULL); 252463e2b423SJohn Johansen if (error) 252563e2b423SJohn Johansen goto error; 252663e2b423SJohn Johansen 2527c97204baSJohn Johansen dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry, 2528b7fd2c03SJohn Johansen NULL, &aa_fs_profile_load); 2529cf916000SJohn Johansen if (IS_ERR(dent)) 2530cf916000SJohn Johansen goto dent_error; 2531b7fd2c03SJohn Johansen ns_subload(root_ns) = dent; 2532b7fd2c03SJohn Johansen 2533c97204baSJohn Johansen dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry, 2534b7fd2c03SJohn Johansen NULL, &aa_fs_profile_replace); 2535cf916000SJohn Johansen if (IS_ERR(dent)) 2536cf916000SJohn Johansen goto dent_error; 2537b7fd2c03SJohn Johansen ns_subreplace(root_ns) = dent; 2538b7fd2c03SJohn Johansen 2539c97204baSJohn Johansen dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry, 2540b7fd2c03SJohn Johansen NULL, &aa_fs_profile_remove); 2541cf916000SJohn Johansen if (IS_ERR(dent)) 2542cf916000SJohn Johansen goto dent_error; 2543b7fd2c03SJohn Johansen ns_subremove(root_ns) = dent; 2544b7fd2c03SJohn Johansen 2545d9bf2c26SJohn Johansen dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry, 2546d9bf2c26SJohn Johansen NULL, &aa_fs_ns_revision_fops); 2547cf916000SJohn Johansen if (IS_ERR(dent)) 2548cf916000SJohn Johansen goto dent_error; 2549d9bf2c26SJohn Johansen ns_subrevision(root_ns) = dent; 2550d9bf2c26SJohn Johansen 2551d9bf2c26SJohn Johansen /* policy tree referenced by magic policy symlink */ 2552feb3c766SJohn Johansen mutex_lock_nested(&root_ns->lock, root_ns->level); 2553c961ee5fSJohn Johansen error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy", 2554c961ee5fSJohn Johansen aafs_mnt->mnt_root); 2555b7fd2c03SJohn Johansen mutex_unlock(&root_ns->lock); 25560d259f04SJohn Johansen if (error) 25570d259f04SJohn Johansen goto error; 25580d259f04SJohn Johansen 2559c961ee5fSJohn Johansen /* magic symlink similar to nsfs redirects based on task policy */ 2560c961ee5fSJohn Johansen dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry, 2561c961ee5fSJohn Johansen NULL, &policy_link_iops); 2562cf916000SJohn Johansen if (IS_ERR(dent)) 2563cf916000SJohn Johansen goto dent_error; 2564c961ee5fSJohn Johansen 2565c97204baSJohn Johansen error = aa_mk_null_file(aa_sfs_entry.dentry); 2566a71ada30SJohn Johansen if (error) 2567a71ada30SJohn Johansen goto error; 2568a71ada30SJohn Johansen 2569a71ada30SJohn Johansen /* TODO: add default profile to apparmorfs */ 257063e2b423SJohn Johansen 257163e2b423SJohn Johansen /* Report that AppArmor fs is enabled */ 257263e2b423SJohn Johansen aa_info_message("AppArmor Filesystem Enabled"); 257363e2b423SJohn Johansen return 0; 257463e2b423SJohn Johansen 2575cf916000SJohn Johansen dent_error: 2576cf916000SJohn Johansen error = PTR_ERR(dent); 257763e2b423SJohn Johansen error: 257863e2b423SJohn Johansen aa_destroy_aafs(); 257963e2b423SJohn Johansen AA_ERROR("Error creating AppArmor securityfs\n"); 258063e2b423SJohn Johansen return error; 258163e2b423SJohn Johansen } 258263e2b423SJohn Johansen 258363e2b423SJohn Johansen fs_initcall(aa_create_aafs); 2584