163e2b423SJohn Johansen /* 263e2b423SJohn Johansen * AppArmor security module 363e2b423SJohn Johansen * 463e2b423SJohn Johansen * This file contains AppArmor /sys/kernel/security/apparmor interface functions 563e2b423SJohn Johansen * 663e2b423SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 763e2b423SJohn Johansen * Copyright 2009-2010 Canonical Ltd. 863e2b423SJohn Johansen * 963e2b423SJohn Johansen * This program is free software; you can redistribute it and/or 1063e2b423SJohn Johansen * modify it under the terms of the GNU General Public License as 1163e2b423SJohn Johansen * published by the Free Software Foundation, version 2 of the 1263e2b423SJohn Johansen * License. 1363e2b423SJohn Johansen */ 1463e2b423SJohn Johansen 150d259f04SJohn Johansen #include <linux/ctype.h> 1663e2b423SJohn Johansen #include <linux/security.h> 1763e2b423SJohn Johansen #include <linux/vmalloc.h> 1863e2b423SJohn Johansen #include <linux/module.h> 1963e2b423SJohn Johansen #include <linux/seq_file.h> 2063e2b423SJohn Johansen #include <linux/uaccess.h> 21a71ada30SJohn Johansen #include <linux/mount.h> 2263e2b423SJohn Johansen #include <linux/namei.h> 23e74abcf3SKees Cook #include <linux/capability.h> 2429b3822fSJohn Johansen #include <linux/rcupdate.h> 25a71ada30SJohn Johansen #include <linux/fs.h> 26d9bf2c26SJohn Johansen #include <linux/poll.h> 27a481f4d9SJohn Johansen #include <uapi/linux/major.h> 28a481f4d9SJohn Johansen #include <uapi/linux/magic.h> 2963e2b423SJohn Johansen 3063e2b423SJohn Johansen #include "include/apparmor.h" 3163e2b423SJohn Johansen #include "include/apparmorfs.h" 3263e2b423SJohn Johansen #include "include/audit.h" 33d8889d49SJohn Johansen #include "include/cred.h" 34f8eb8a13SJohn Johansen #include "include/crypto.h" 35cd1dbf76SJohn Johansen #include "include/ipc.h" 36317d9a05SJohn Johansen #include "include/label.h" 3763e2b423SJohn Johansen #include "include/policy.h" 38cff281f6SJohn Johansen #include "include/policy_ns.h" 39d384b0a1SKees Cook #include "include/resource.h" 405ac8c355SJohn Johansen #include "include/policy_unpack.h" 4163e2b423SJohn Johansen 42c97204baSJohn Johansen /* 43c97204baSJohn Johansen * The apparmor filesystem interface used for policy load and introspection 44c97204baSJohn Johansen * The interface is split into two main components based on their function 45c97204baSJohn Johansen * a securityfs component: 46c97204baSJohn Johansen * used for static files that are always available, and which allows 47c97204baSJohn Johansen * userspace to specificy the location of the security filesystem. 48c97204baSJohn Johansen * 49c97204baSJohn Johansen * fns and data are prefixed with 50c97204baSJohn Johansen * aa_sfs_ 51c97204baSJohn Johansen * 52c97204baSJohn Johansen * an apparmorfs component: 53c97204baSJohn Johansen * used loaded policy content and introspection. It is not part of a 54c97204baSJohn Johansen * regular mounted filesystem and is available only through the magic 55c97204baSJohn Johansen * policy symlink in the root of the securityfs apparmor/ directory. 56c97204baSJohn Johansen * Tasks queries will be magically redirected to the correct portion 57c97204baSJohn Johansen * of the policy tree based on their confinement. 58c97204baSJohn Johansen * 59c97204baSJohn Johansen * fns and data are prefixed with 60c97204baSJohn Johansen * aafs_ 61c97204baSJohn Johansen * 62c97204baSJohn Johansen * The aa_fs_ prefix is used to indicate the fn is used by both the 63c97204baSJohn Johansen * securityfs and apparmorfs filesystems. 64c97204baSJohn Johansen */ 65c97204baSJohn Johansen 66c97204baSJohn Johansen 67c97204baSJohn Johansen /* 68c97204baSJohn Johansen * support fns 69c97204baSJohn Johansen */ 70c97204baSJohn Johansen 7163e2b423SJohn Johansen /** 720d259f04SJohn Johansen * aa_mangle_name - mangle a profile name to std profile layout form 730d259f04SJohn Johansen * @name: profile name to mangle (NOT NULL) 740d259f04SJohn Johansen * @target: buffer to store mangled name, same length as @name (MAYBE NULL) 750d259f04SJohn Johansen * 760d259f04SJohn Johansen * Returns: length of mangled name 770d259f04SJohn Johansen */ 78bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target) 790d259f04SJohn Johansen { 800d259f04SJohn Johansen char *t = target; 810d259f04SJohn Johansen 820d259f04SJohn Johansen while (*name == '/' || *name == '.') 830d259f04SJohn Johansen name++; 840d259f04SJohn Johansen 850d259f04SJohn Johansen if (target) { 860d259f04SJohn Johansen for (; *name; name++) { 870d259f04SJohn Johansen if (*name == '/') 880d259f04SJohn Johansen *(t)++ = '.'; 890d259f04SJohn Johansen else if (isspace(*name)) 900d259f04SJohn Johansen *(t)++ = '_'; 910d259f04SJohn Johansen else if (isalnum(*name) || strchr("._-", *name)) 920d259f04SJohn Johansen *(t)++ = *name; 930d259f04SJohn Johansen } 940d259f04SJohn Johansen 950d259f04SJohn Johansen *t = 0; 960d259f04SJohn Johansen } else { 970d259f04SJohn Johansen int len = 0; 980d259f04SJohn Johansen for (; *name; name++) { 990d259f04SJohn Johansen if (isalnum(*name) || isspace(*name) || 1000d259f04SJohn Johansen strchr("/._-", *name)) 1010d259f04SJohn Johansen len++; 1020d259f04SJohn Johansen } 1030d259f04SJohn Johansen 1040d259f04SJohn Johansen return len; 1050d259f04SJohn Johansen } 1060d259f04SJohn Johansen 1070d259f04SJohn Johansen return t - target; 1080d259f04SJohn Johansen } 1090d259f04SJohn Johansen 110a481f4d9SJohn Johansen 111a481f4d9SJohn Johansen /* 112a481f4d9SJohn Johansen * aafs - core fns and data for the policy tree 113a481f4d9SJohn Johansen */ 114a481f4d9SJohn Johansen 115a481f4d9SJohn Johansen #define AAFS_NAME "apparmorfs" 116a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt; 117a481f4d9SJohn Johansen static int aafs_count; 118a481f4d9SJohn Johansen 119a481f4d9SJohn Johansen 120a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry) 121a481f4d9SJohn Johansen { 122a0781209SJohn Johansen seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino); 123a481f4d9SJohn Johansen return 0; 124a481f4d9SJohn Johansen } 125a481f4d9SJohn Johansen 126a481f4d9SJohn Johansen static void aafs_evict_inode(struct inode *inode) 127a481f4d9SJohn Johansen { 128a481f4d9SJohn Johansen truncate_inode_pages_final(&inode->i_data); 129a481f4d9SJohn Johansen clear_inode(inode); 130a481f4d9SJohn Johansen if (S_ISLNK(inode->i_mode)) 131a481f4d9SJohn Johansen kfree(inode->i_link); 132a481f4d9SJohn Johansen } 133a481f4d9SJohn Johansen 134a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = { 135a481f4d9SJohn Johansen .statfs = simple_statfs, 136a481f4d9SJohn Johansen .evict_inode = aafs_evict_inode, 137a481f4d9SJohn Johansen .show_path = aafs_show_path, 138a481f4d9SJohn Johansen }; 139a481f4d9SJohn Johansen 140a481f4d9SJohn Johansen static int fill_super(struct super_block *sb, void *data, int silent) 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 153a481f4d9SJohn Johansen static struct dentry *aafs_mount(struct file_system_type *fs_type, 154a481f4d9SJohn Johansen int flags, const char *dev_name, void *data) 155a481f4d9SJohn Johansen { 156a481f4d9SJohn Johansen return mount_single(fs_type, flags, data, fill_super); 157a481f4d9SJohn Johansen } 158a481f4d9SJohn Johansen 159a481f4d9SJohn Johansen static struct file_system_type aafs_ops = { 160a481f4d9SJohn Johansen .owner = THIS_MODULE, 161a481f4d9SJohn Johansen .name = AAFS_NAME, 162a481f4d9SJohn Johansen .mount = aafs_mount, 163a481f4d9SJohn Johansen .kill_sb = kill_anon_super, 164a481f4d9SJohn Johansen }; 165a481f4d9SJohn Johansen 166a481f4d9SJohn Johansen /** 167a481f4d9SJohn Johansen * __aafs_setup_d_inode - basic inode setup for apparmorfs 168a481f4d9SJohn Johansen * @dir: parent directory for the dentry 169a481f4d9SJohn Johansen * @dentry: dentry we are seting the inode up for 170a481f4d9SJohn Johansen * @mode: permissions the file should have 171a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 172a481f4d9SJohn Johansen * @link: if symlink, symlink target string 173a481f4d9SJohn Johansen * @fops: struct file_operations that should be used 174a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 175a481f4d9SJohn Johansen */ 176a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry, 177a481f4d9SJohn Johansen umode_t mode, void *data, char *link, 178a481f4d9SJohn Johansen const struct file_operations *fops, 179a481f4d9SJohn Johansen const struct inode_operations *iops) 180a481f4d9SJohn Johansen { 181a481f4d9SJohn Johansen struct inode *inode = new_inode(dir->i_sb); 182a481f4d9SJohn Johansen 183a481f4d9SJohn Johansen AA_BUG(!dir); 184a481f4d9SJohn Johansen AA_BUG(!dentry); 185a481f4d9SJohn Johansen 186a481f4d9SJohn Johansen if (!inode) 187a481f4d9SJohn Johansen return -ENOMEM; 188a481f4d9SJohn Johansen 189a481f4d9SJohn Johansen inode->i_ino = get_next_ino(); 190a481f4d9SJohn Johansen inode->i_mode = mode; 191a481f4d9SJohn Johansen inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 192a481f4d9SJohn Johansen inode->i_private = data; 193a481f4d9SJohn Johansen if (S_ISDIR(mode)) { 194a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_dir_inode_operations; 195a481f4d9SJohn Johansen inode->i_fop = &simple_dir_operations; 196a481f4d9SJohn Johansen inc_nlink(inode); 197a481f4d9SJohn Johansen inc_nlink(dir); 198a481f4d9SJohn Johansen } else if (S_ISLNK(mode)) { 199a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_symlink_inode_operations; 200a481f4d9SJohn Johansen inode->i_link = link; 201a481f4d9SJohn Johansen } else { 202a481f4d9SJohn Johansen inode->i_fop = fops; 203a481f4d9SJohn Johansen } 204a481f4d9SJohn Johansen d_instantiate(dentry, inode); 205a481f4d9SJohn Johansen dget(dentry); 206a481f4d9SJohn Johansen 207a481f4d9SJohn Johansen return 0; 208a481f4d9SJohn Johansen } 209a481f4d9SJohn Johansen 210a481f4d9SJohn Johansen /** 211a481f4d9SJohn Johansen * aafs_create - create a dentry in the apparmorfs filesystem 212a481f4d9SJohn Johansen * 213a481f4d9SJohn Johansen * @name: name of dentry to create 214a481f4d9SJohn Johansen * @mode: permissions the file should have 215a481f4d9SJohn Johansen * @parent: parent directory for this dentry 216a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 217a481f4d9SJohn Johansen * @link: if symlink, symlink target string 218a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 219a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 220a481f4d9SJohn Johansen * 221a481f4d9SJohn Johansen * This is the basic "create a xxx" function for apparmorfs. 222a481f4d9SJohn Johansen * 223a481f4d9SJohn Johansen * Returns a pointer to a dentry if it succeeds, that must be free with 224a481f4d9SJohn Johansen * aafs_remove(). Will return ERR_PTR on failure. 225a481f4d9SJohn Johansen */ 226a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode, 227a481f4d9SJohn Johansen struct dentry *parent, void *data, void *link, 228a481f4d9SJohn Johansen const struct file_operations *fops, 229a481f4d9SJohn Johansen const struct inode_operations *iops) 230a481f4d9SJohn Johansen { 231a481f4d9SJohn Johansen struct dentry *dentry; 232a481f4d9SJohn Johansen struct inode *dir; 233a481f4d9SJohn Johansen int error; 234a481f4d9SJohn Johansen 235a481f4d9SJohn Johansen AA_BUG(!name); 236a481f4d9SJohn Johansen AA_BUG(!parent); 237a481f4d9SJohn Johansen 238a481f4d9SJohn Johansen if (!(mode & S_IFMT)) 239a481f4d9SJohn Johansen mode = (mode & S_IALLUGO) | S_IFREG; 240a481f4d9SJohn Johansen 241a481f4d9SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 242a481f4d9SJohn Johansen if (error) 243a481f4d9SJohn Johansen return ERR_PTR(error); 244a481f4d9SJohn Johansen 245a481f4d9SJohn Johansen dir = d_inode(parent); 246a481f4d9SJohn Johansen 247a481f4d9SJohn Johansen inode_lock(dir); 248a481f4d9SJohn Johansen dentry = lookup_one_len(name, parent, strlen(name)); 2495d314a81SDan Carpenter if (IS_ERR(dentry)) { 2505d314a81SDan Carpenter error = PTR_ERR(dentry); 251a481f4d9SJohn Johansen goto fail_lock; 2525d314a81SDan Carpenter } 253a481f4d9SJohn Johansen 254a481f4d9SJohn Johansen if (d_really_is_positive(dentry)) { 255a481f4d9SJohn Johansen error = -EEXIST; 256a481f4d9SJohn Johansen goto fail_dentry; 257a481f4d9SJohn Johansen } 258a481f4d9SJohn Johansen 259a481f4d9SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops); 260a481f4d9SJohn Johansen if (error) 261a481f4d9SJohn Johansen goto fail_dentry; 262a481f4d9SJohn Johansen inode_unlock(dir); 263a481f4d9SJohn Johansen 264a481f4d9SJohn Johansen return dentry; 265a481f4d9SJohn Johansen 266a481f4d9SJohn Johansen fail_dentry: 267a481f4d9SJohn Johansen dput(dentry); 268a481f4d9SJohn Johansen 269a481f4d9SJohn Johansen fail_lock: 270a481f4d9SJohn Johansen inode_unlock(dir); 271a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 272a481f4d9SJohn Johansen 273a481f4d9SJohn Johansen return ERR_PTR(error); 274a481f4d9SJohn Johansen } 275a481f4d9SJohn Johansen 276a481f4d9SJohn Johansen /** 277a481f4d9SJohn Johansen * aafs_create_file - create a file in the apparmorfs filesystem 278a481f4d9SJohn Johansen * 279a481f4d9SJohn Johansen * @name: name of dentry to create 280a481f4d9SJohn Johansen * @mode: permissions the file should have 281a481f4d9SJohn Johansen * @parent: parent directory for this dentry 282a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 283a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 284a481f4d9SJohn Johansen * 285a481f4d9SJohn Johansen * see aafs_create 286a481f4d9SJohn Johansen */ 287a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode, 288a481f4d9SJohn Johansen struct dentry *parent, void *data, 289a481f4d9SJohn Johansen const struct file_operations *fops) 290a481f4d9SJohn Johansen { 291a481f4d9SJohn Johansen return aafs_create(name, mode, parent, data, NULL, fops, NULL); 292a481f4d9SJohn Johansen } 293a481f4d9SJohn Johansen 294a481f4d9SJohn Johansen /** 295a481f4d9SJohn Johansen * aafs_create_dir - create a directory in the apparmorfs filesystem 296a481f4d9SJohn Johansen * 297a481f4d9SJohn Johansen * @name: name of dentry to create 298a481f4d9SJohn Johansen * @parent: parent directory for this dentry 299a481f4d9SJohn Johansen * 300a481f4d9SJohn Johansen * see aafs_create 301a481f4d9SJohn Johansen */ 302a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent) 303a481f4d9SJohn Johansen { 304a481f4d9SJohn Johansen return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL, 305a481f4d9SJohn Johansen NULL); 306a481f4d9SJohn Johansen } 307a481f4d9SJohn Johansen 308a481f4d9SJohn Johansen /** 309a481f4d9SJohn Johansen * aafs_create_symlink - create a symlink in the apparmorfs filesystem 310a481f4d9SJohn Johansen * @name: name of dentry to create 311a481f4d9SJohn Johansen * @parent: parent directory for this dentry 312a481f4d9SJohn Johansen * @target: if symlink, symlink target string 3131180b4c7SJohn Johansen * @private: private data 314a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 315a481f4d9SJohn Johansen * 316a481f4d9SJohn Johansen * If @target parameter is %NULL, then the @iops parameter needs to be 317a481f4d9SJohn Johansen * setup to handle .readlink and .get_link inode_operations. 318a481f4d9SJohn Johansen */ 319a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name, 320a481f4d9SJohn Johansen struct dentry *parent, 321a481f4d9SJohn Johansen const char *target, 3221180b4c7SJohn Johansen void *private, 323a481f4d9SJohn Johansen const struct inode_operations *iops) 324a481f4d9SJohn Johansen { 325a481f4d9SJohn Johansen struct dentry *dent; 326a481f4d9SJohn Johansen char *link = NULL; 327a481f4d9SJohn Johansen 328a481f4d9SJohn Johansen if (target) { 329a481f4d9SJohn Johansen if (!link) 330a481f4d9SJohn Johansen return ERR_PTR(-ENOMEM); 331a481f4d9SJohn Johansen } 3321180b4c7SJohn Johansen dent = aafs_create(name, S_IFLNK | 0444, parent, private, link, NULL, 333a481f4d9SJohn Johansen iops); 334a481f4d9SJohn Johansen if (IS_ERR(dent)) 335a481f4d9SJohn Johansen kfree(link); 336a481f4d9SJohn Johansen 337a481f4d9SJohn Johansen return dent; 338a481f4d9SJohn Johansen } 339a481f4d9SJohn Johansen 340a481f4d9SJohn Johansen /** 341a481f4d9SJohn Johansen * aafs_remove - removes a file or directory from the apparmorfs filesystem 342a481f4d9SJohn Johansen * 343a481f4d9SJohn Johansen * @dentry: dentry of the file/directory/symlink to removed. 344a481f4d9SJohn Johansen */ 345a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry) 346a481f4d9SJohn Johansen { 347a481f4d9SJohn Johansen struct inode *dir; 348a481f4d9SJohn Johansen 349a481f4d9SJohn Johansen if (!dentry || IS_ERR(dentry)) 350a481f4d9SJohn Johansen return; 351a481f4d9SJohn Johansen 352a481f4d9SJohn Johansen dir = d_inode(dentry->d_parent); 353a481f4d9SJohn Johansen inode_lock(dir); 354a481f4d9SJohn Johansen if (simple_positive(dentry)) { 355a481f4d9SJohn Johansen if (d_is_dir(dentry)) 356a481f4d9SJohn Johansen simple_rmdir(dir, dentry); 357a481f4d9SJohn Johansen else 358a481f4d9SJohn Johansen simple_unlink(dir, dentry); 359a481f4d9SJohn Johansen dput(dentry); 360a481f4d9SJohn Johansen } 361a481f4d9SJohn Johansen inode_unlock(dir); 362a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 363a481f4d9SJohn Johansen } 364a481f4d9SJohn Johansen 365a481f4d9SJohn Johansen 366a481f4d9SJohn Johansen /* 367a481f4d9SJohn Johansen * aa_fs - policy load/replace/remove 368a481f4d9SJohn Johansen */ 369a481f4d9SJohn Johansen 3700d259f04SJohn Johansen /** 37163e2b423SJohn Johansen * aa_simple_write_to_buffer - common routine for getting policy from user 37263e2b423SJohn Johansen * @userbuf: user buffer to copy data from (NOT NULL) 3733ed02adaSJohn Johansen * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 37463e2b423SJohn Johansen * @copy_size: size of data to copy from user buffer 37563e2b423SJohn Johansen * @pos: position write is at in the file (NOT NULL) 37663e2b423SJohn Johansen * 37763e2b423SJohn Johansen * Returns: kernel buffer containing copy of user buffer data or an 37863e2b423SJohn Johansen * ERR_PTR on failure. 37963e2b423SJohn Johansen */ 3805ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, 3815ac8c355SJohn Johansen size_t alloc_size, 3825ac8c355SJohn Johansen size_t copy_size, 38363e2b423SJohn Johansen loff_t *pos) 38463e2b423SJohn Johansen { 3855ac8c355SJohn Johansen struct aa_loaddata *data; 38663e2b423SJohn Johansen 387e6bfa25dSJohn Johansen AA_BUG(copy_size > alloc_size); 3883ed02adaSJohn Johansen 38963e2b423SJohn Johansen if (*pos != 0) 39063e2b423SJohn Johansen /* only writes from pos 0, that is complete writes */ 39163e2b423SJohn Johansen return ERR_PTR(-ESPIPE); 39263e2b423SJohn Johansen 39363e2b423SJohn Johansen /* freed by caller to simple_write_to_buffer */ 3945d5182caSJohn Johansen data = aa_loaddata_alloc(alloc_size); 3955d5182caSJohn Johansen if (IS_ERR(data)) 3965d5182caSJohn Johansen return data; 39763e2b423SJohn Johansen 3985d5182caSJohn Johansen data->size = copy_size; 3995ac8c355SJohn Johansen if (copy_from_user(data->data, userbuf, copy_size)) { 40063e2b423SJohn Johansen kvfree(data); 40163e2b423SJohn Johansen return ERR_PTR(-EFAULT); 40263e2b423SJohn Johansen } 40363e2b423SJohn Johansen 40463e2b423SJohn Johansen return data; 40563e2b423SJohn Johansen } 40663e2b423SJohn Johansen 40718e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size, 408b7fd2c03SJohn Johansen loff_t *pos, struct aa_ns *ns) 4095ac8c355SJohn Johansen { 4105ac8c355SJohn Johansen struct aa_loaddata *data; 411637f688dSJohn Johansen struct aa_label *label; 412637f688dSJohn Johansen ssize_t error; 413cf797c0eSJohn Johansen 414637f688dSJohn Johansen label = begin_current_label_crit_section(); 415cf797c0eSJohn Johansen 4165ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4175ac8c355SJohn Johansen * below after unpack 4185ac8c355SJohn Johansen */ 419637f688dSJohn Johansen error = aa_may_manage_policy(label, ns, mask); 4205ac8c355SJohn Johansen if (error) 4215ac8c355SJohn Johansen return error; 42263e2b423SJohn Johansen 4235ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size, size, pos); 4245ac8c355SJohn Johansen error = PTR_ERR(data); 4255ac8c355SJohn Johansen if (!IS_ERR(data)) { 426637f688dSJohn Johansen error = aa_replace_profiles(ns, label, mask, data); 4275ac8c355SJohn Johansen aa_put_loaddata(data); 4285ac8c355SJohn Johansen } 429637f688dSJohn Johansen end_current_label_crit_section(label); 4305ac8c355SJohn Johansen 4315ac8c355SJohn Johansen return error; 4325ac8c355SJohn Johansen } 4335ac8c355SJohn Johansen 434b7fd2c03SJohn Johansen /* .load file hook fn to load policy */ 43563e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 43663e2b423SJohn Johansen loff_t *pos) 43763e2b423SJohn Johansen { 438b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 43918e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns); 440b7fd2c03SJohn Johansen 441b7fd2c03SJohn Johansen aa_put_ns(ns); 44263e2b423SJohn Johansen 44363e2b423SJohn Johansen return error; 44463e2b423SJohn Johansen } 44563e2b423SJohn Johansen 44663e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = { 4476038f373SArnd Bergmann .write = profile_load, 4486038f373SArnd Bergmann .llseek = default_llseek, 44963e2b423SJohn Johansen }; 45063e2b423SJohn Johansen 45163e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */ 45263e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf, 45363e2b423SJohn Johansen size_t size, loff_t *pos) 45463e2b423SJohn Johansen { 455b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 45618e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY, 45718e99f19SJohn Johansen buf, size, pos, ns); 458b7fd2c03SJohn Johansen aa_put_ns(ns); 45963e2b423SJohn Johansen 46063e2b423SJohn Johansen return error; 46163e2b423SJohn Johansen } 46263e2b423SJohn Johansen 46363e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = { 4646038f373SArnd Bergmann .write = profile_replace, 4656038f373SArnd Bergmann .llseek = default_llseek, 46663e2b423SJohn Johansen }; 46763e2b423SJohn Johansen 468b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */ 46963e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf, 47063e2b423SJohn Johansen size_t size, loff_t *pos) 47163e2b423SJohn Johansen { 4725ac8c355SJohn Johansen struct aa_loaddata *data; 473637f688dSJohn Johansen struct aa_label *label; 47463e2b423SJohn Johansen ssize_t error; 475b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 47663e2b423SJohn Johansen 477637f688dSJohn Johansen label = begin_current_label_crit_section(); 4785ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4795ac8c355SJohn Johansen * below after unpack 4805ac8c355SJohn Johansen */ 481637f688dSJohn Johansen error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY); 4825ac8c355SJohn Johansen if (error) 4835ac8c355SJohn Johansen goto out; 4845ac8c355SJohn Johansen 48563e2b423SJohn Johansen /* 48663e2b423SJohn Johansen * aa_remove_profile needs a null terminated string so 1 extra 48763e2b423SJohn Johansen * byte is allocated and the copied data is null terminated. 48863e2b423SJohn Johansen */ 4895ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size + 1, size, pos); 49063e2b423SJohn Johansen 49163e2b423SJohn Johansen error = PTR_ERR(data); 49263e2b423SJohn Johansen if (!IS_ERR(data)) { 4935ac8c355SJohn Johansen data->data[size] = 0; 494637f688dSJohn Johansen error = aa_remove_profiles(ns, label, data->data, size); 4955ac8c355SJohn Johansen aa_put_loaddata(data); 49663e2b423SJohn Johansen } 4975ac8c355SJohn Johansen out: 498637f688dSJohn Johansen end_current_label_crit_section(label); 499b7fd2c03SJohn Johansen aa_put_ns(ns); 50063e2b423SJohn Johansen return error; 50163e2b423SJohn Johansen } 50263e2b423SJohn Johansen 50363e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = { 5046038f373SArnd Bergmann .write = profile_remove, 5056038f373SArnd Bergmann .llseek = default_llseek, 50663e2b423SJohn Johansen }; 50763e2b423SJohn Johansen 508d9bf2c26SJohn Johansen struct aa_revision { 509d9bf2c26SJohn Johansen struct aa_ns *ns; 510d9bf2c26SJohn Johansen long last_read; 511d9bf2c26SJohn Johansen }; 512d9bf2c26SJohn Johansen 513d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */ 514d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file) 515d9bf2c26SJohn Johansen { 516d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 517d9bf2c26SJohn Johansen 518d9bf2c26SJohn Johansen if (rev) { 519d9bf2c26SJohn Johansen aa_put_ns(rev->ns); 520d9bf2c26SJohn Johansen kfree(rev); 521d9bf2c26SJohn Johansen } 522d9bf2c26SJohn Johansen 523d9bf2c26SJohn Johansen return 0; 524d9bf2c26SJohn Johansen } 525d9bf2c26SJohn Johansen 526d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf, 527d9bf2c26SJohn Johansen size_t size, loff_t *ppos) 528d9bf2c26SJohn Johansen { 529d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 530d9bf2c26SJohn Johansen char buffer[32]; 531d9bf2c26SJohn Johansen long last_read; 532d9bf2c26SJohn Johansen int avail; 533d9bf2c26SJohn Johansen 534feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 535d9bf2c26SJohn Johansen last_read = rev->last_read; 536d9bf2c26SJohn Johansen if (last_read == rev->ns->revision) { 537d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 538d9bf2c26SJohn Johansen if (file->f_flags & O_NONBLOCK) 539d9bf2c26SJohn Johansen return -EAGAIN; 540d9bf2c26SJohn Johansen if (wait_event_interruptible(rev->ns->wait, 541d9bf2c26SJohn Johansen last_read != 542d9bf2c26SJohn Johansen READ_ONCE(rev->ns->revision))) 543d9bf2c26SJohn Johansen return -ERESTARTSYS; 544feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 545d9bf2c26SJohn Johansen } 546d9bf2c26SJohn Johansen 547d9bf2c26SJohn Johansen avail = sprintf(buffer, "%ld\n", rev->ns->revision); 548d9bf2c26SJohn Johansen if (*ppos + size > avail) { 549d9bf2c26SJohn Johansen rev->last_read = rev->ns->revision; 550d9bf2c26SJohn Johansen *ppos = 0; 551d9bf2c26SJohn Johansen } 552d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 553d9bf2c26SJohn Johansen 554d9bf2c26SJohn Johansen return simple_read_from_buffer(buf, size, ppos, buffer, avail); 555d9bf2c26SJohn Johansen } 556d9bf2c26SJohn Johansen 557d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file) 558d9bf2c26SJohn Johansen { 559d9bf2c26SJohn Johansen struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL); 560d9bf2c26SJohn Johansen 561d9bf2c26SJohn Johansen if (!rev) 562d9bf2c26SJohn Johansen return -ENOMEM; 563d9bf2c26SJohn Johansen 564d9bf2c26SJohn Johansen rev->ns = aa_get_ns(inode->i_private); 565d9bf2c26SJohn Johansen if (!rev->ns) 566d9bf2c26SJohn Johansen rev->ns = aa_get_current_ns(); 567d9bf2c26SJohn Johansen file->private_data = rev; 568d9bf2c26SJohn Johansen 569d9bf2c26SJohn Johansen return 0; 570d9bf2c26SJohn Johansen } 571d9bf2c26SJohn Johansen 572d9bf2c26SJohn Johansen static unsigned int ns_revision_poll(struct file *file, poll_table *pt) 573d9bf2c26SJohn Johansen { 574d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 575d9bf2c26SJohn Johansen unsigned int mask = 0; 576d9bf2c26SJohn Johansen 577d9bf2c26SJohn Johansen if (rev) { 578feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 579d9bf2c26SJohn Johansen poll_wait(file, &rev->ns->wait, pt); 580d9bf2c26SJohn Johansen if (rev->last_read < rev->ns->revision) 581d9bf2c26SJohn Johansen mask |= POLLIN | POLLRDNORM; 582d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 583d9bf2c26SJohn Johansen } 584d9bf2c26SJohn Johansen 585d9bf2c26SJohn Johansen return mask; 586d9bf2c26SJohn Johansen } 587d9bf2c26SJohn Johansen 5885d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns) 5895d5182caSJohn Johansen { 5905d5182caSJohn Johansen ns->revision++; 591d9bf2c26SJohn Johansen wake_up_interruptible(&ns->wait); 5925d5182caSJohn Johansen } 5935d5182caSJohn Johansen 594d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = { 595d9bf2c26SJohn Johansen .owner = THIS_MODULE, 596d9bf2c26SJohn Johansen .open = ns_revision_open, 597d9bf2c26SJohn Johansen .poll = ns_revision_poll, 598d9bf2c26SJohn Johansen .read = ns_revision_read, 599d9bf2c26SJohn Johansen .llseek = generic_file_llseek, 600d9bf2c26SJohn Johansen .release = ns_revision_release, 601d9bf2c26SJohn Johansen }; 602d9bf2c26SJohn Johansen 6034f3b3f2dSJohn Johansen static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms, 6044f3b3f2dSJohn Johansen const char *match_str, size_t match_len) 6054f3b3f2dSJohn Johansen { 6064f3b3f2dSJohn Johansen struct aa_perms tmp; 6074f3b3f2dSJohn Johansen struct aa_dfa *dfa; 6084f3b3f2dSJohn Johansen unsigned int state = 0; 6094f3b3f2dSJohn Johansen 610637f688dSJohn Johansen if (profile_unconfined(profile)) 6114f3b3f2dSJohn Johansen return; 6124f3b3f2dSJohn Johansen if (profile->file.dfa && *match_str == AA_CLASS_FILE) { 6134f3b3f2dSJohn Johansen dfa = profile->file.dfa; 6144f3b3f2dSJohn Johansen state = aa_dfa_match_len(dfa, profile->file.start, 6154f3b3f2dSJohn Johansen match_str + 1, match_len - 1); 6164f3b3f2dSJohn Johansen tmp = nullperms; 6174f3b3f2dSJohn Johansen if (state) { 6184f3b3f2dSJohn Johansen struct path_cond cond = { }; 6194f3b3f2dSJohn Johansen 6204f3b3f2dSJohn Johansen tmp = aa_compute_fperms(dfa, state, &cond); 6214f3b3f2dSJohn Johansen } 6224f3b3f2dSJohn Johansen } else if (profile->policy.dfa) { 623b9590ad4SJohn Johansen if (!PROFILE_MEDIATES(profile, *match_str)) 6244f3b3f2dSJohn Johansen return; /* no change to current perms */ 6254f3b3f2dSJohn Johansen dfa = profile->policy.dfa; 6264f3b3f2dSJohn Johansen state = aa_dfa_match_len(dfa, profile->policy.start[0], 6274f3b3f2dSJohn Johansen match_str, match_len); 6284f3b3f2dSJohn Johansen if (state) 6294f3b3f2dSJohn Johansen aa_compute_perms(dfa, state, &tmp); 6304f3b3f2dSJohn Johansen else 6314f3b3f2dSJohn Johansen tmp = nullperms; 6324f3b3f2dSJohn Johansen } 6334f3b3f2dSJohn Johansen aa_apply_modes_to_perms(profile, &tmp); 634317d9a05SJohn Johansen aa_perms_accum_raw(perms, &tmp); 6354f3b3f2dSJohn Johansen } 6364f3b3f2dSJohn Johansen 6374f3b3f2dSJohn Johansen 638e025be0fSWilliam Hua /** 639e025be0fSWilliam Hua * query_data - queries a policy and writes its data to buf 640e025be0fSWilliam Hua * @buf: the resulting data is stored here (NOT NULL) 641e025be0fSWilliam Hua * @buf_len: size of buf 642e025be0fSWilliam Hua * @query: query string used to retrieve data 643e025be0fSWilliam Hua * @query_len: size of query including second NUL byte 644e025be0fSWilliam Hua * 645e025be0fSWilliam Hua * The buffers pointed to by buf and query may overlap. The query buffer is 646e025be0fSWilliam Hua * parsed before buf is written to. 647e025be0fSWilliam Hua * 648e025be0fSWilliam Hua * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of 649e025be0fSWilliam Hua * the security confinement context and <KEY> is the name of the data to 650e025be0fSWilliam Hua * retrieve. <LABEL> and <KEY> must not be NUL-terminated. 651e025be0fSWilliam Hua * 652e025be0fSWilliam Hua * Don't expect the contents of buf to be preserved on failure. 653e025be0fSWilliam Hua * 654e025be0fSWilliam Hua * Returns: number of characters written to buf or -errno on failure 655e025be0fSWilliam Hua */ 656e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len, 657e025be0fSWilliam Hua char *query, size_t query_len) 658e025be0fSWilliam Hua { 659e025be0fSWilliam Hua char *out; 660e025be0fSWilliam Hua const char *key; 661317d9a05SJohn Johansen struct label_it i; 662637f688dSJohn Johansen struct aa_label *label, *curr; 663317d9a05SJohn Johansen struct aa_profile *profile; 664e025be0fSWilliam Hua struct aa_data *data; 665e025be0fSWilliam Hua u32 bytes, blocks; 666e025be0fSWilliam Hua __le32 outle32; 667e025be0fSWilliam Hua 668e025be0fSWilliam Hua if (!query_len) 669e025be0fSWilliam Hua return -EINVAL; /* need a query */ 670e025be0fSWilliam Hua 671e025be0fSWilliam Hua key = query + strnlen(query, query_len) + 1; 672e025be0fSWilliam Hua if (key + 1 >= query + query_len) 673e025be0fSWilliam Hua return -EINVAL; /* not enough space for a non-empty key */ 674e025be0fSWilliam Hua if (key + strnlen(key, query + query_len - key) >= query + query_len) 675e025be0fSWilliam Hua return -EINVAL; /* must end with NUL */ 676e025be0fSWilliam Hua 677e025be0fSWilliam Hua if (buf_len < sizeof(bytes) + sizeof(blocks)) 678e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 679e025be0fSWilliam Hua 680637f688dSJohn Johansen curr = begin_current_label_crit_section(); 681637f688dSJohn Johansen label = aa_label_parse(curr, query, GFP_KERNEL, false, false); 682637f688dSJohn Johansen end_current_label_crit_section(curr); 683637f688dSJohn Johansen if (IS_ERR(label)) 684637f688dSJohn Johansen return PTR_ERR(label); 685e025be0fSWilliam Hua 686e025be0fSWilliam Hua /* We are going to leave space for two numbers. The first is the total 687e025be0fSWilliam Hua * number of bytes we are writing after the first number. This is so 688e025be0fSWilliam Hua * users can read the full output without reallocation. 689e025be0fSWilliam Hua * 690e025be0fSWilliam Hua * The second number is the number of data blocks we're writing. An 691e025be0fSWilliam Hua * application might be confined by multiple policies having data in 692e025be0fSWilliam Hua * the same key. 693e025be0fSWilliam Hua */ 694e025be0fSWilliam Hua memset(buf, 0, sizeof(bytes) + sizeof(blocks)); 695e025be0fSWilliam Hua out = buf + sizeof(bytes) + sizeof(blocks); 696e025be0fSWilliam Hua 697e025be0fSWilliam Hua blocks = 0; 698317d9a05SJohn Johansen label_for_each_confined(i, label, profile) { 699317d9a05SJohn Johansen if (!profile->data) 700317d9a05SJohn Johansen continue; 701317d9a05SJohn Johansen 702317d9a05SJohn Johansen data = rhashtable_lookup_fast(profile->data, &key, 703317d9a05SJohn Johansen profile->data->p); 704e025be0fSWilliam Hua 705e025be0fSWilliam Hua if (data) { 706317d9a05SJohn Johansen if (out + sizeof(outle32) + data->size > buf + 707317d9a05SJohn Johansen buf_len) { 708637f688dSJohn Johansen aa_put_label(label); 709e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 710637f688dSJohn Johansen } 711e025be0fSWilliam Hua outle32 = __cpu_to_le32(data->size); 712e025be0fSWilliam Hua memcpy(out, &outle32, sizeof(outle32)); 713e025be0fSWilliam Hua out += sizeof(outle32); 714e025be0fSWilliam Hua memcpy(out, data->data, data->size); 715e025be0fSWilliam Hua out += data->size; 716e025be0fSWilliam Hua blocks++; 717e025be0fSWilliam Hua } 718e025be0fSWilliam Hua } 719637f688dSJohn Johansen aa_put_label(label); 720e025be0fSWilliam Hua 721e025be0fSWilliam Hua outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); 722e025be0fSWilliam Hua memcpy(buf, &outle32, sizeof(outle32)); 723e025be0fSWilliam Hua outle32 = __cpu_to_le32(blocks); 724e025be0fSWilliam Hua memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32)); 725e025be0fSWilliam Hua 726e025be0fSWilliam Hua return out - buf; 727e025be0fSWilliam Hua } 728e025be0fSWilliam Hua 7294f3b3f2dSJohn Johansen /** 7304f3b3f2dSJohn Johansen * query_label - queries a label and writes permissions to buf 7314f3b3f2dSJohn Johansen * @buf: the resulting permissions string is stored here (NOT NULL) 7324f3b3f2dSJohn Johansen * @buf_len: size of buf 7334f3b3f2dSJohn Johansen * @query: binary query string to match against the dfa 7344f3b3f2dSJohn Johansen * @query_len: size of query 7354f3b3f2dSJohn Johansen * @view_only: only compute for querier's view 7364f3b3f2dSJohn Johansen * 7374f3b3f2dSJohn Johansen * The buffers pointed to by buf and query may overlap. The query buffer is 7384f3b3f2dSJohn Johansen * parsed before buf is written to. 7394f3b3f2dSJohn Johansen * 7404f3b3f2dSJohn Johansen * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is 7414f3b3f2dSJohn Johansen * the name of the label, in the current namespace, that is to be queried and 7424f3b3f2dSJohn Johansen * DFA_STRING is a binary string to match against the label(s)'s DFA. 7434f3b3f2dSJohn Johansen * 7444f3b3f2dSJohn Johansen * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters 7454f3b3f2dSJohn Johansen * but must *not* be NUL terminated. 7464f3b3f2dSJohn Johansen * 7474f3b3f2dSJohn Johansen * Returns: number of characters written to buf or -errno on failure 7484f3b3f2dSJohn Johansen */ 7494f3b3f2dSJohn Johansen static ssize_t query_label(char *buf, size_t buf_len, 7504f3b3f2dSJohn Johansen char *query, size_t query_len, bool view_only) 7514f3b3f2dSJohn Johansen { 752317d9a05SJohn Johansen struct aa_profile *profile; 753637f688dSJohn Johansen struct aa_label *label, *curr; 7544f3b3f2dSJohn Johansen char *label_name, *match_str; 7554f3b3f2dSJohn Johansen size_t label_name_len, match_len; 7564f3b3f2dSJohn Johansen struct aa_perms perms; 757317d9a05SJohn Johansen struct label_it i; 7584f3b3f2dSJohn Johansen 7594f3b3f2dSJohn Johansen if (!query_len) 7604f3b3f2dSJohn Johansen return -EINVAL; 7614f3b3f2dSJohn Johansen 7624f3b3f2dSJohn Johansen label_name = query; 7634f3b3f2dSJohn Johansen label_name_len = strnlen(query, query_len); 7644f3b3f2dSJohn Johansen if (!label_name_len || label_name_len == query_len) 7654f3b3f2dSJohn Johansen return -EINVAL; 7664f3b3f2dSJohn Johansen 7674f3b3f2dSJohn Johansen /** 7684f3b3f2dSJohn Johansen * The extra byte is to account for the null byte between the 7694f3b3f2dSJohn Johansen * profile name and dfa string. profile_name_len is greater 7704f3b3f2dSJohn Johansen * than zero and less than query_len, so a byte can be safely 7714f3b3f2dSJohn Johansen * added or subtracted. 7724f3b3f2dSJohn Johansen */ 7734f3b3f2dSJohn Johansen match_str = label_name + label_name_len + 1; 7744f3b3f2dSJohn Johansen match_len = query_len - label_name_len - 1; 7754f3b3f2dSJohn Johansen 776637f688dSJohn Johansen curr = begin_current_label_crit_section(); 777637f688dSJohn Johansen label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false); 778637f688dSJohn Johansen end_current_label_crit_section(curr); 779637f688dSJohn Johansen if (IS_ERR(label)) 780637f688dSJohn Johansen return PTR_ERR(label); 7814f3b3f2dSJohn Johansen 7824f3b3f2dSJohn Johansen perms = allperms; 783317d9a05SJohn Johansen if (view_only) { 784317d9a05SJohn Johansen label_for_each_in_ns(i, labels_ns(label), label, profile) { 785317d9a05SJohn Johansen profile_query_cb(profile, &perms, match_str, match_len); 786317d9a05SJohn Johansen } 787317d9a05SJohn Johansen } else { 788317d9a05SJohn Johansen label_for_each(i, label, profile) { 789317d9a05SJohn Johansen profile_query_cb(profile, &perms, match_str, match_len); 790317d9a05SJohn Johansen } 791317d9a05SJohn Johansen } 792317d9a05SJohn Johansen aa_put_label(label); 7934f3b3f2dSJohn Johansen 7944f3b3f2dSJohn Johansen return scnprintf(buf, buf_len, 7954f3b3f2dSJohn Johansen "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n", 7964f3b3f2dSJohn Johansen perms.allow, perms.deny, perms.audit, perms.quiet); 7974f3b3f2dSJohn Johansen } 7984f3b3f2dSJohn Johansen 7991dea3b41SJohn Johansen /* 8001dea3b41SJohn Johansen * Transaction based IO. 8011dea3b41SJohn Johansen * The file expects a write which triggers the transaction, and then 8021dea3b41SJohn Johansen * possibly a read(s) which collects the result - which is stored in a 8031dea3b41SJohn Johansen * file-local buffer. Once a new write is performed, a new set of results 8041dea3b41SJohn Johansen * are stored in the file-local buffer. 8051dea3b41SJohn Johansen */ 8061dea3b41SJohn Johansen struct multi_transaction { 8071dea3b41SJohn Johansen struct kref count; 8081dea3b41SJohn Johansen ssize_t size; 8091dea3b41SJohn Johansen char data[0]; 8101dea3b41SJohn Johansen }; 8111dea3b41SJohn Johansen 8121dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction)) 8131dea3b41SJohn Johansen /* TODO: replace with per file lock */ 8141dea3b41SJohn Johansen static DEFINE_SPINLOCK(multi_transaction_lock); 8151dea3b41SJohn Johansen 8161dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref) 8171dea3b41SJohn Johansen { 8181dea3b41SJohn Johansen struct multi_transaction *t; 8191dea3b41SJohn Johansen 8201dea3b41SJohn Johansen t = container_of(kref, struct multi_transaction, count); 8211dea3b41SJohn Johansen free_page((unsigned long) t); 8221dea3b41SJohn Johansen } 8231dea3b41SJohn Johansen 8241dea3b41SJohn Johansen static struct multi_transaction * 8251dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t) 8261dea3b41SJohn Johansen { 8271dea3b41SJohn Johansen if (t) 8281dea3b41SJohn Johansen kref_get(&(t->count)); 8291dea3b41SJohn Johansen 8301dea3b41SJohn Johansen return t; 8311dea3b41SJohn Johansen } 8321dea3b41SJohn Johansen 8331dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t) 8341dea3b41SJohn Johansen { 8351dea3b41SJohn Johansen if (t) 8361dea3b41SJohn Johansen kref_put(&(t->count), multi_transaction_kref); 8371dea3b41SJohn Johansen } 8381dea3b41SJohn Johansen 8391dea3b41SJohn Johansen /* does not increment @new's count */ 8401dea3b41SJohn Johansen static void multi_transaction_set(struct file *file, 8411dea3b41SJohn Johansen struct multi_transaction *new, size_t n) 8421dea3b41SJohn Johansen { 8431dea3b41SJohn Johansen struct multi_transaction *old; 8441dea3b41SJohn Johansen 8451dea3b41SJohn Johansen AA_BUG(n > MULTI_TRANSACTION_LIMIT); 8461dea3b41SJohn Johansen 8471dea3b41SJohn Johansen new->size = n; 8481dea3b41SJohn Johansen spin_lock(&multi_transaction_lock); 8491dea3b41SJohn Johansen old = (struct multi_transaction *) file->private_data; 8501dea3b41SJohn Johansen file->private_data = new; 8511dea3b41SJohn Johansen spin_unlock(&multi_transaction_lock); 8521dea3b41SJohn Johansen put_multi_transaction(old); 8531dea3b41SJohn Johansen } 8541dea3b41SJohn Johansen 8551dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file, 8561dea3b41SJohn Johansen const char __user *buf, 8571dea3b41SJohn Johansen size_t size) 8581dea3b41SJohn Johansen { 8591dea3b41SJohn Johansen struct multi_transaction *t; 8601dea3b41SJohn Johansen 8611dea3b41SJohn Johansen if (size > MULTI_TRANSACTION_LIMIT - 1) 8621dea3b41SJohn Johansen return ERR_PTR(-EFBIG); 8631dea3b41SJohn Johansen 8641dea3b41SJohn Johansen t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL); 8651dea3b41SJohn Johansen if (!t) 8661dea3b41SJohn Johansen return ERR_PTR(-ENOMEM); 8671dea3b41SJohn Johansen kref_init(&t->count); 8681dea3b41SJohn Johansen if (copy_from_user(t->data, buf, size)) 8691dea3b41SJohn Johansen return ERR_PTR(-EFAULT); 8701dea3b41SJohn Johansen 8711dea3b41SJohn Johansen return t; 8721dea3b41SJohn Johansen } 8731dea3b41SJohn Johansen 8741dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf, 8751dea3b41SJohn Johansen size_t size, loff_t *pos) 8761dea3b41SJohn Johansen { 8771dea3b41SJohn Johansen struct multi_transaction *t; 8781dea3b41SJohn Johansen ssize_t ret; 8791dea3b41SJohn Johansen 8801dea3b41SJohn Johansen spin_lock(&multi_transaction_lock); 8811dea3b41SJohn Johansen t = get_multi_transaction(file->private_data); 8821dea3b41SJohn Johansen spin_unlock(&multi_transaction_lock); 8831dea3b41SJohn Johansen if (!t) 8841dea3b41SJohn Johansen return 0; 8851dea3b41SJohn Johansen 8861dea3b41SJohn Johansen ret = simple_read_from_buffer(buf, size, pos, t->data, t->size); 8871dea3b41SJohn Johansen put_multi_transaction(t); 8881dea3b41SJohn Johansen 8891dea3b41SJohn Johansen return ret; 8901dea3b41SJohn Johansen } 8911dea3b41SJohn Johansen 8921dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file) 8931dea3b41SJohn Johansen { 8941dea3b41SJohn Johansen put_multi_transaction(file->private_data); 8951dea3b41SJohn Johansen 8961dea3b41SJohn Johansen return 0; 8971dea3b41SJohn Johansen } 8981dea3b41SJohn Johansen 899317d9a05SJohn Johansen #define QUERY_CMD_LABEL "label\0" 900317d9a05SJohn Johansen #define QUERY_CMD_LABEL_LEN 6 9014f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE "profile\0" 9024f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE_LEN 8 903317d9a05SJohn Johansen #define QUERY_CMD_LABELALL "labelall\0" 904317d9a05SJohn Johansen #define QUERY_CMD_LABELALL_LEN 9 905e025be0fSWilliam Hua #define QUERY_CMD_DATA "data\0" 906e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN 5 907e025be0fSWilliam Hua 908e025be0fSWilliam Hua /** 909e025be0fSWilliam Hua * aa_write_access - generic permissions and data query 910e025be0fSWilliam Hua * @file: pointer to open apparmorfs/access file 911e025be0fSWilliam Hua * @ubuf: user buffer containing the complete query string (NOT NULL) 912e025be0fSWilliam Hua * @count: size of ubuf 913e025be0fSWilliam Hua * @ppos: position in the file (MUST BE ZERO) 914e025be0fSWilliam Hua * 915e025be0fSWilliam Hua * Allows for one permissions or data query per open(), write(), and read() 916e025be0fSWilliam Hua * sequence. The only queries currently supported are label-based queries for 917e025be0fSWilliam Hua * permissions or data. 918e025be0fSWilliam Hua * 919e025be0fSWilliam Hua * For permissions queries, ubuf must begin with "label\0", followed by the 920e025be0fSWilliam Hua * profile query specific format described in the query_label() function 921e025be0fSWilliam Hua * documentation. 922e025be0fSWilliam Hua * 923e025be0fSWilliam Hua * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where 924e025be0fSWilliam Hua * <LABEL> is the name of the security confinement context and <KEY> is the 925e025be0fSWilliam Hua * name of the data to retrieve. 926e025be0fSWilliam Hua * 927e025be0fSWilliam Hua * Returns: number of bytes written or -errno on failure 928e025be0fSWilliam Hua */ 929e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf, 930e025be0fSWilliam Hua size_t count, loff_t *ppos) 931e025be0fSWilliam Hua { 9321dea3b41SJohn Johansen struct multi_transaction *t; 933e025be0fSWilliam Hua ssize_t len; 934e025be0fSWilliam Hua 935e025be0fSWilliam Hua if (*ppos) 936e025be0fSWilliam Hua return -ESPIPE; 937e025be0fSWilliam Hua 9381dea3b41SJohn Johansen t = multi_transaction_new(file, ubuf, count); 9391dea3b41SJohn Johansen if (IS_ERR(t)) 9401dea3b41SJohn Johansen return PTR_ERR(t); 941e025be0fSWilliam Hua 9424f3b3f2dSJohn Johansen if (count > QUERY_CMD_PROFILE_LEN && 9434f3b3f2dSJohn Johansen !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) { 9444f3b3f2dSJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 9454f3b3f2dSJohn Johansen t->data + QUERY_CMD_PROFILE_LEN, 9464f3b3f2dSJohn Johansen count - QUERY_CMD_PROFILE_LEN, true); 947317d9a05SJohn Johansen } else if (count > QUERY_CMD_LABEL_LEN && 948317d9a05SJohn Johansen !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) { 949317d9a05SJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 950317d9a05SJohn Johansen t->data + QUERY_CMD_LABEL_LEN, 951317d9a05SJohn Johansen count - QUERY_CMD_LABEL_LEN, true); 952317d9a05SJohn Johansen } else if (count > QUERY_CMD_LABELALL_LEN && 953317d9a05SJohn Johansen !memcmp(t->data, QUERY_CMD_LABELALL, 954317d9a05SJohn Johansen QUERY_CMD_LABELALL_LEN)) { 955317d9a05SJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 956317d9a05SJohn Johansen t->data + QUERY_CMD_LABELALL_LEN, 957317d9a05SJohn Johansen count - QUERY_CMD_LABELALL_LEN, false); 9584f3b3f2dSJohn Johansen } else if (count > QUERY_CMD_DATA_LEN && 9591dea3b41SJohn Johansen !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { 9601dea3b41SJohn Johansen len = query_data(t->data, MULTI_TRANSACTION_LIMIT, 9611dea3b41SJohn Johansen t->data + QUERY_CMD_DATA_LEN, 962e025be0fSWilliam Hua count - QUERY_CMD_DATA_LEN); 963e025be0fSWilliam Hua } else 964e025be0fSWilliam Hua len = -EINVAL; 965e025be0fSWilliam Hua 9661dea3b41SJohn Johansen if (len < 0) { 9671dea3b41SJohn Johansen put_multi_transaction(t); 968e025be0fSWilliam Hua return len; 9691dea3b41SJohn Johansen } 970e025be0fSWilliam Hua 9711dea3b41SJohn Johansen multi_transaction_set(file, t, len); 972e025be0fSWilliam Hua 973e025be0fSWilliam Hua return count; 974e025be0fSWilliam Hua } 975e025be0fSWilliam Hua 976c97204baSJohn Johansen static const struct file_operations aa_sfs_access = { 977e025be0fSWilliam Hua .write = aa_write_access, 9781dea3b41SJohn Johansen .read = multi_transaction_read, 9791dea3b41SJohn Johansen .release = multi_transaction_release, 980e025be0fSWilliam Hua .llseek = generic_file_llseek, 981e025be0fSWilliam Hua }; 982e025be0fSWilliam Hua 983c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v) 984e74abcf3SKees Cook { 985c97204baSJohn Johansen struct aa_sfs_entry *fs_file = seq->private; 986e74abcf3SKees Cook 987e74abcf3SKees Cook if (!fs_file) 988e74abcf3SKees Cook return 0; 989e74abcf3SKees Cook 990e74abcf3SKees Cook switch (fs_file->v_type) { 991c97204baSJohn Johansen case AA_SFS_TYPE_BOOLEAN: 992e74abcf3SKees Cook seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); 993e74abcf3SKees Cook break; 994c97204baSJohn Johansen case AA_SFS_TYPE_STRING: 995a9bf8e9fSKees Cook seq_printf(seq, "%s\n", fs_file->v.string); 996a9bf8e9fSKees Cook break; 997c97204baSJohn Johansen case AA_SFS_TYPE_U64: 998e74abcf3SKees Cook seq_printf(seq, "%#08lx\n", fs_file->v.u64); 999e74abcf3SKees Cook break; 1000e74abcf3SKees Cook default: 1001e74abcf3SKees Cook /* Ignore unpritable entry types. */ 1002e74abcf3SKees Cook break; 1003e74abcf3SKees Cook } 1004e74abcf3SKees Cook 1005e74abcf3SKees Cook return 0; 1006e74abcf3SKees Cook } 1007e74abcf3SKees Cook 1008c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file) 1009e74abcf3SKees Cook { 1010c97204baSJohn Johansen return single_open(file, aa_sfs_seq_show, inode->i_private); 1011e74abcf3SKees Cook } 1012e74abcf3SKees Cook 1013c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = { 1014e74abcf3SKees Cook .owner = THIS_MODULE, 1015c97204baSJohn Johansen .open = aa_sfs_seq_open, 1016e74abcf3SKees Cook .read = seq_read, 1017e74abcf3SKees Cook .llseek = seq_lseek, 1018e74abcf3SKees Cook .release = single_release, 1019e74abcf3SKees Cook }; 1020e74abcf3SKees Cook 102152b97de3SJohn Johansen /* 102252b97de3SJohn Johansen * profile based file operations 102352b97de3SJohn Johansen * policy/profiles/XXXX/profiles/ * 102452b97de3SJohn Johansen */ 102552b97de3SJohn Johansen 102652b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME) \ 102752b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\ 102852b97de3SJohn Johansen { \ 102952b97de3SJohn Johansen return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \ 103052b97de3SJohn Johansen } \ 103152b97de3SJohn Johansen \ 103252b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = { \ 103352b97de3SJohn Johansen .owner = THIS_MODULE, \ 103452b97de3SJohn Johansen .open = seq_profile_ ##NAME ##_open, \ 103552b97de3SJohn Johansen .read = seq_read, \ 103652b97de3SJohn Johansen .llseek = seq_lseek, \ 103752b97de3SJohn Johansen .release = seq_profile_release, \ 103852b97de3SJohn Johansen } \ 103952b97de3SJohn Johansen 104052b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file, 10410d259f04SJohn Johansen int (*show)(struct seq_file *, void *)) 10420d259f04SJohn Johansen { 10438399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(inode->i_private); 10448399588aSJohn Johansen int error = single_open(file, show, proxy); 104563e2b423SJohn Johansen 10460d259f04SJohn Johansen if (error) { 10470d259f04SJohn Johansen file->private_data = NULL; 10488399588aSJohn Johansen aa_put_proxy(proxy); 10490d259f04SJohn Johansen } 10500d259f04SJohn Johansen 10510d259f04SJohn Johansen return error; 10520d259f04SJohn Johansen } 10530d259f04SJohn Johansen 105452b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file) 10550d259f04SJohn Johansen { 10560d259f04SJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 10570d259f04SJohn Johansen if (seq) 10588399588aSJohn Johansen aa_put_proxy(seq->private); 10590d259f04SJohn Johansen return single_release(inode, file); 10600d259f04SJohn Johansen } 10610d259f04SJohn Johansen 106252b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v) 10630d259f04SJohn Johansen { 10648399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1065637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1066637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 10670d259f04SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 1068637f688dSJohn Johansen aa_put_label(label); 10690d259f04SJohn Johansen 10700d259f04SJohn Johansen return 0; 10710d259f04SJohn Johansen } 10720d259f04SJohn Johansen 107352b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v) 10740d259f04SJohn Johansen { 10758399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1076637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1077637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 10780d259f04SJohn Johansen seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); 1079637f688dSJohn Johansen aa_put_label(label); 10800d259f04SJohn Johansen 10810d259f04SJohn Johansen return 0; 10820d259f04SJohn Johansen } 10830d259f04SJohn Johansen 108452b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v) 1085556d0be7SJohn Johansen { 10868399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1087637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1088637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 1089556d0be7SJohn Johansen if (profile->attach) 1090556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->attach); 1091556d0be7SJohn Johansen else if (profile->xmatch) 1092556d0be7SJohn Johansen seq_puts(seq, "<unknown>\n"); 1093556d0be7SJohn Johansen else 1094556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 1095637f688dSJohn Johansen aa_put_label(label); 1096556d0be7SJohn Johansen 1097556d0be7SJohn Johansen return 0; 1098556d0be7SJohn Johansen } 1099556d0be7SJohn Johansen 110052b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v) 1101f8eb8a13SJohn Johansen { 11028399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1103637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1104637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 1105f8eb8a13SJohn Johansen unsigned int i, size = aa_hash_size(); 1106f8eb8a13SJohn Johansen 1107f8eb8a13SJohn Johansen if (profile->hash) { 1108f8eb8a13SJohn Johansen for (i = 0; i < size; i++) 1109f8eb8a13SJohn Johansen seq_printf(seq, "%.2x", profile->hash[i]); 111047dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 1111f8eb8a13SJohn Johansen } 1112637f688dSJohn Johansen aa_put_label(label); 1113f8eb8a13SJohn Johansen 1114f8eb8a13SJohn Johansen return 0; 1115f8eb8a13SJohn Johansen } 1116f8eb8a13SJohn Johansen 111752b97de3SJohn Johansen SEQ_PROFILE_FOPS(name); 111852b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode); 111952b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach); 112052b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash); 1121f8eb8a13SJohn Johansen 112264c86970SJohn Johansen /* 112364c86970SJohn Johansen * namespace based files 112464c86970SJohn Johansen * several root files and 112564c86970SJohn Johansen * policy/ * 112664c86970SJohn Johansen */ 1127f8eb8a13SJohn Johansen 112864c86970SJohn Johansen #define SEQ_NS_FOPS(NAME) \ 112964c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \ 113064c86970SJohn Johansen { \ 113164c86970SJohn Johansen return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \ 113264c86970SJohn Johansen } \ 113364c86970SJohn Johansen \ 113464c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = { \ 113564c86970SJohn Johansen .owner = THIS_MODULE, \ 113664c86970SJohn Johansen .open = seq_ns_ ##NAME ##_open, \ 113764c86970SJohn Johansen .read = seq_read, \ 113864c86970SJohn Johansen .llseek = seq_lseek, \ 113964c86970SJohn Johansen .release = single_release, \ 114064c86970SJohn Johansen } \ 11413e3e5695SJohn Johansen 114240cde7fcSJohn Johansen static int seq_ns_stacked_show(struct seq_file *seq, void *v) 114340cde7fcSJohn Johansen { 114440cde7fcSJohn Johansen struct aa_label *label; 114540cde7fcSJohn Johansen 114640cde7fcSJohn Johansen label = begin_current_label_crit_section(); 114740cde7fcSJohn Johansen seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no"); 114840cde7fcSJohn Johansen end_current_label_crit_section(label); 114940cde7fcSJohn Johansen 115040cde7fcSJohn Johansen return 0; 115140cde7fcSJohn Johansen } 115240cde7fcSJohn Johansen 115340cde7fcSJohn Johansen static int seq_ns_nsstacked_show(struct seq_file *seq, void *v) 115440cde7fcSJohn Johansen { 115540cde7fcSJohn Johansen struct aa_label *label; 115640cde7fcSJohn Johansen struct aa_profile *profile; 115740cde7fcSJohn Johansen struct label_it it; 115840cde7fcSJohn Johansen int count = 1; 115940cde7fcSJohn Johansen 116040cde7fcSJohn Johansen label = begin_current_label_crit_section(); 116140cde7fcSJohn Johansen 116240cde7fcSJohn Johansen if (label->size > 1) { 116340cde7fcSJohn Johansen label_for_each(it, label, profile) 116440cde7fcSJohn Johansen if (profile->ns != labels_ns(label)) { 116540cde7fcSJohn Johansen count++; 116640cde7fcSJohn Johansen break; 116740cde7fcSJohn Johansen } 116840cde7fcSJohn Johansen } 116940cde7fcSJohn Johansen 117040cde7fcSJohn Johansen seq_printf(seq, "%s\n", count > 1 ? "yes" : "no"); 117140cde7fcSJohn Johansen end_current_label_crit_section(label); 117240cde7fcSJohn Johansen 117340cde7fcSJohn Johansen return 0; 117440cde7fcSJohn Johansen } 117540cde7fcSJohn Johansen 117664c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v) 1177a71ada30SJohn Johansen { 1178637f688dSJohn Johansen struct aa_label *label; 1179a71ada30SJohn Johansen 1180637f688dSJohn Johansen label = begin_current_label_crit_section(); 1181637f688dSJohn Johansen seq_printf(seq, "%d\n", labels_ns(label)->level); 1182637f688dSJohn Johansen end_current_label_crit_section(label); 1183a71ada30SJohn Johansen 1184a71ada30SJohn Johansen return 0; 1185a71ada30SJohn Johansen } 1186a71ada30SJohn Johansen 118764c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v) 11883e3e5695SJohn Johansen { 1189637f688dSJohn Johansen struct aa_label *label = begin_current_label_crit_section(); 1190040d9e2bSJohn Johansen seq_printf(seq, "%s\n", labels_ns(label)->base.name); 1191637f688dSJohn Johansen end_current_label_crit_section(label); 11923e3e5695SJohn Johansen 11933e3e5695SJohn Johansen return 0; 11943e3e5695SJohn Johansen } 11953e3e5695SJohn Johansen 119640cde7fcSJohn Johansen SEQ_NS_FOPS(stacked); 119740cde7fcSJohn Johansen SEQ_NS_FOPS(nsstacked); 119864c86970SJohn Johansen SEQ_NS_FOPS(level); 119964c86970SJohn Johansen SEQ_NS_FOPS(name); 12003e3e5695SJohn Johansen 12015d5182caSJohn Johansen 12025d5182caSJohn Johansen /* policy/raw_data/ * file ops */ 12035d5182caSJohn Johansen 12045d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME) \ 12055d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\ 12065d5182caSJohn Johansen { \ 12075d5182caSJohn Johansen return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \ 12085d5182caSJohn Johansen } \ 12095d5182caSJohn Johansen \ 12105d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \ 12115d5182caSJohn Johansen .owner = THIS_MODULE, \ 12125d5182caSJohn Johansen .open = seq_rawdata_ ##NAME ##_open, \ 12135d5182caSJohn Johansen .read = seq_read, \ 12145d5182caSJohn Johansen .llseek = seq_lseek, \ 12155d5182caSJohn Johansen .release = seq_rawdata_release, \ 12165d5182caSJohn Johansen } \ 12175d5182caSJohn Johansen 12185d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file, 12195d5182caSJohn Johansen int (*show)(struct seq_file *, void *)) 12205ac8c355SJohn Johansen { 12215d5182caSJohn Johansen struct aa_loaddata *data = __aa_get_loaddata(inode->i_private); 12225d5182caSJohn Johansen int error; 12235d5182caSJohn Johansen 12245d5182caSJohn Johansen if (!data) 12255d5182caSJohn Johansen /* lost race this ent is being reaped */ 12265d5182caSJohn Johansen return -ENOENT; 12275d5182caSJohn Johansen 12285d5182caSJohn Johansen error = single_open(file, show, data); 12295d5182caSJohn Johansen if (error) { 12305d5182caSJohn Johansen AA_BUG(file->private_data && 12315d5182caSJohn Johansen ((struct seq_file *)file->private_data)->private); 12325d5182caSJohn Johansen aa_put_loaddata(data); 12335d5182caSJohn Johansen } 12345d5182caSJohn Johansen 12355d5182caSJohn Johansen return error; 12365d5182caSJohn Johansen } 12375d5182caSJohn Johansen 12385d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file) 12395d5182caSJohn Johansen { 12405d5182caSJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 12415d5182caSJohn Johansen 12425d5182caSJohn Johansen if (seq) 12435d5182caSJohn Johansen aa_put_loaddata(seq->private); 12445d5182caSJohn Johansen 12455d5182caSJohn Johansen return single_release(inode, file); 12465d5182caSJohn Johansen } 12475d5182caSJohn Johansen 12485d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v) 12495d5182caSJohn Johansen { 12505d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12515d5182caSJohn Johansen 12525d5182caSJohn Johansen seq_printf(seq, "v%d\n", data->abi); 12535ac8c355SJohn Johansen 12545ac8c355SJohn Johansen return 0; 12555ac8c355SJohn Johansen } 12565ac8c355SJohn Johansen 12575d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v) 12585ac8c355SJohn Johansen { 12595d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12605ac8c355SJohn Johansen 12615d5182caSJohn Johansen seq_printf(seq, "%ld\n", data->revision); 12625ac8c355SJohn Johansen 12635ac8c355SJohn Johansen return 0; 12645ac8c355SJohn Johansen } 12655ac8c355SJohn Johansen 12665d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v) 12675ac8c355SJohn Johansen { 12685d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12695ac8c355SJohn Johansen unsigned int i, size = aa_hash_size(); 12705ac8c355SJohn Johansen 12715d5182caSJohn Johansen if (data->hash) { 12725ac8c355SJohn Johansen for (i = 0; i < size; i++) 12735d5182caSJohn Johansen seq_printf(seq, "%.2x", data->hash[i]); 127447dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 12755ac8c355SJohn Johansen } 12765ac8c355SJohn Johansen 12775ac8c355SJohn Johansen return 0; 12785ac8c355SJohn Johansen } 12795ac8c355SJohn Johansen 12805d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi); 12815d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision); 12825d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash); 12835ac8c355SJohn Johansen 12845ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, 12855ac8c355SJohn Johansen loff_t *ppos) 12865ac8c355SJohn Johansen { 12875ac8c355SJohn Johansen struct aa_loaddata *rawdata = file->private_data; 12885ac8c355SJohn Johansen 12895ac8c355SJohn Johansen return simple_read_from_buffer(buf, size, ppos, rawdata->data, 12905ac8c355SJohn Johansen rawdata->size); 12915ac8c355SJohn Johansen } 12925ac8c355SJohn Johansen 12935d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file) 12945ac8c355SJohn Johansen { 12955d5182caSJohn Johansen aa_put_loaddata(file->private_data); 12965ac8c355SJohn Johansen 12975ac8c355SJohn Johansen return 0; 12985ac8c355SJohn Johansen } 12995ac8c355SJohn Johansen 13005d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file) 13015d5182caSJohn Johansen { 13025d5182caSJohn Johansen if (!policy_view_capable(NULL)) 13035d5182caSJohn Johansen return -EACCES; 13045d5182caSJohn Johansen file->private_data = __aa_get_loaddata(inode->i_private); 13055d5182caSJohn Johansen if (!file->private_data) 13065d5182caSJohn Johansen /* lost race: this entry is being reaped */ 13075d5182caSJohn Johansen return -ENOENT; 13085d5182caSJohn Johansen 13095d5182caSJohn Johansen return 0; 13105d5182caSJohn Johansen } 13115d5182caSJohn Johansen 13125d5182caSJohn Johansen static const struct file_operations rawdata_fops = { 13135ac8c355SJohn Johansen .open = rawdata_open, 13145ac8c355SJohn Johansen .read = rawdata_read, 13155ac8c355SJohn Johansen .llseek = generic_file_llseek, 13165ac8c355SJohn Johansen .release = rawdata_release, 13175ac8c355SJohn Johansen }; 13185ac8c355SJohn Johansen 13195d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata) 13205d5182caSJohn Johansen { 13215d5182caSJohn Johansen int i; 13225d5182caSJohn Johansen 13235d5182caSJohn Johansen for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) { 13245d5182caSJohn Johansen if (!IS_ERR_OR_NULL(rawdata->dents[i])) { 13255d5182caSJohn Johansen /* no refcounts on i_private */ 1326c961ee5fSJohn Johansen aafs_remove(rawdata->dents[i]); 13275d5182caSJohn Johansen rawdata->dents[i] = NULL; 13285d5182caSJohn Johansen } 13295d5182caSJohn Johansen } 13305d5182caSJohn Johansen } 13315d5182caSJohn Johansen 13325d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata) 13335d5182caSJohn Johansen { 13345d5182caSJohn Johansen AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock)); 13355d5182caSJohn Johansen 13365d5182caSJohn Johansen if (rawdata->ns) { 13375d5182caSJohn Johansen remove_rawdata_dents(rawdata); 13385d5182caSJohn Johansen list_del_init(&rawdata->list); 13395d5182caSJohn Johansen aa_put_ns(rawdata->ns); 13405d5182caSJohn Johansen rawdata->ns = NULL; 13415d5182caSJohn Johansen } 13425d5182caSJohn Johansen } 13435d5182caSJohn Johansen 13445d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata) 13455d5182caSJohn Johansen { 13465d5182caSJohn Johansen struct dentry *dent, *dir; 13475d5182caSJohn Johansen 13485d5182caSJohn Johansen AA_BUG(!ns); 13495d5182caSJohn Johansen AA_BUG(!rawdata); 13505d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 13515d5182caSJohn Johansen AA_BUG(!ns_subdata_dir(ns)); 13525d5182caSJohn Johansen 13535d5182caSJohn Johansen /* 13545d5182caSJohn Johansen * just use ns revision dir was originally created at. This is 13555d5182caSJohn Johansen * under ns->lock and if load is successful revision will be 13565d5182caSJohn Johansen * bumped and is guaranteed to be unique 13575d5182caSJohn Johansen */ 13585d5182caSJohn Johansen rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision); 13595d5182caSJohn Johansen if (!rawdata->name) 13605d5182caSJohn Johansen return -ENOMEM; 13615d5182caSJohn Johansen 1362c961ee5fSJohn Johansen dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns)); 13635d5182caSJohn Johansen if (IS_ERR(dir)) 13645d5182caSJohn Johansen /* ->name freed when rawdata freed */ 13655d5182caSJohn Johansen return PTR_ERR(dir); 13665d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DIR] = dir; 13675d5182caSJohn Johansen 1368c961ee5fSJohn Johansen dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata, 13695d5182caSJohn Johansen &seq_rawdata_abi_fops); 13705d5182caSJohn Johansen if (IS_ERR(dent)) 13715d5182caSJohn Johansen goto fail; 13725d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_ABI] = dent; 13735d5182caSJohn Johansen 1374c961ee5fSJohn Johansen dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata, 13755d5182caSJohn Johansen &seq_rawdata_revision_fops); 13765d5182caSJohn Johansen if (IS_ERR(dent)) 13775d5182caSJohn Johansen goto fail; 13785d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_REVISION] = dent; 13795d5182caSJohn Johansen 13805d5182caSJohn Johansen if (aa_g_hash_policy) { 1381c961ee5fSJohn Johansen dent = aafs_create_file("sha1", S_IFREG | 0444, dir, 13825d5182caSJohn Johansen rawdata, &seq_rawdata_hash_fops); 13835d5182caSJohn Johansen if (IS_ERR(dent)) 13845d5182caSJohn Johansen goto fail; 13855d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_HASH] = dent; 13865d5182caSJohn Johansen } 13875d5182caSJohn Johansen 1388c961ee5fSJohn Johansen dent = aafs_create_file("raw_data", S_IFREG | 0444, 13895d5182caSJohn Johansen dir, rawdata, &rawdata_fops); 13905d5182caSJohn Johansen if (IS_ERR(dent)) 13915d5182caSJohn Johansen goto fail; 13925d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DATA] = dent; 13935d5182caSJohn Johansen d_inode(dent)->i_size = rawdata->size; 13945d5182caSJohn Johansen 13955d5182caSJohn Johansen rawdata->ns = aa_get_ns(ns); 13965d5182caSJohn Johansen list_add(&rawdata->list, &ns->rawdata_list); 13975d5182caSJohn Johansen /* no refcount on inode rawdata */ 13985d5182caSJohn Johansen 13995d5182caSJohn Johansen return 0; 14005d5182caSJohn Johansen 14015d5182caSJohn Johansen fail: 14025d5182caSJohn Johansen remove_rawdata_dents(rawdata); 14035d5182caSJohn Johansen 14045d5182caSJohn Johansen return PTR_ERR(dent); 14055d5182caSJohn Johansen } 14065d5182caSJohn Johansen 14070d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/ 1408c97204baSJohn Johansen 1409c97204baSJohn Johansen /** 1410c97204baSJohn Johansen * 1411c97204baSJohn Johansen * Requires: @profile->ns->lock held 1412c97204baSJohn Johansen */ 1413c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile) 14140d259f04SJohn Johansen { 14150d259f04SJohn Johansen struct aa_profile *child; 14160d259f04SJohn Johansen int i; 14170d259f04SJohn Johansen 14180d259f04SJohn Johansen if (!profile) 14190d259f04SJohn Johansen return; 14200d259f04SJohn Johansen 14210d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) 1422c97204baSJohn Johansen __aafs_profile_rmdir(child); 14230d259f04SJohn Johansen 14240d259f04SJohn Johansen for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { 14258399588aSJohn Johansen struct aa_proxy *proxy; 14260d259f04SJohn Johansen if (!profile->dents[i]) 14270d259f04SJohn Johansen continue; 14280d259f04SJohn Johansen 14298399588aSJohn Johansen proxy = d_inode(profile->dents[i])->i_private; 1430c961ee5fSJohn Johansen aafs_remove(profile->dents[i]); 14318399588aSJohn Johansen aa_put_proxy(proxy); 14320d259f04SJohn Johansen profile->dents[i] = NULL; 14330d259f04SJohn Johansen } 14340d259f04SJohn Johansen } 14350d259f04SJohn Johansen 1436c97204baSJohn Johansen /** 1437c97204baSJohn Johansen * 1438c97204baSJohn Johansen * Requires: @old->ns->lock held 1439c97204baSJohn Johansen */ 1440c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old, 14410d259f04SJohn Johansen struct aa_profile *new) 14420d259f04SJohn Johansen { 14430d259f04SJohn Johansen int i; 14440d259f04SJohn Johansen 1445cbf2d0e1SJohn Johansen AA_BUG(!old); 1446cbf2d0e1SJohn Johansen AA_BUG(!new); 1447cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock)); 1448cbf2d0e1SJohn Johansen 14490d259f04SJohn Johansen for (i = 0; i < AAFS_PROF_SIZEOF; i++) { 14500d259f04SJohn Johansen new->dents[i] = old->dents[i]; 1451d671e890SJohn Johansen if (new->dents[i]) 1452078cd827SDeepa Dinamani new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); 14530d259f04SJohn Johansen old->dents[i] = NULL; 14540d259f04SJohn Johansen } 14550d259f04SJohn Johansen } 14560d259f04SJohn Johansen 14570d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name, 14580d259f04SJohn Johansen struct aa_profile *profile, 14590d259f04SJohn Johansen const struct file_operations *fops) 14600d259f04SJohn Johansen { 1461637f688dSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy); 14620d259f04SJohn Johansen struct dentry *dent; 14630d259f04SJohn Johansen 1464c961ee5fSJohn Johansen dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops); 14650d259f04SJohn Johansen if (IS_ERR(dent)) 14668399588aSJohn Johansen aa_put_proxy(proxy); 14670d259f04SJohn Johansen 14680d259f04SJohn Johansen return dent; 14690d259f04SJohn Johansen } 14700d259f04SJohn Johansen 14715d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile) 14725d5182caSJohn Johansen { 14735d5182caSJohn Johansen int depth = 0; 14745d5182caSJohn Johansen 14755d5182caSJohn Johansen rcu_read_lock(); 14765d5182caSJohn Johansen for (depth = 0; profile; profile = rcu_access_pointer(profile->parent)) 14775d5182caSJohn Johansen depth++; 14785d5182caSJohn Johansen rcu_read_unlock(); 14795d5182caSJohn Johansen 14805d5182caSJohn Johansen return depth; 14815d5182caSJohn Johansen } 14825d5182caSJohn Johansen 14831180b4c7SJohn Johansen static char *gen_symlink_name(int depth, const char *dirname, const char *fname) 14845d5182caSJohn Johansen { 14851180b4c7SJohn Johansen char *buffer, *s; 14865d5182caSJohn Johansen int error; 14871180b4c7SJohn Johansen int size = depth * 6 + strlen(dirname) + strlen(fname) + 11; 14881180b4c7SJohn Johansen 14891180b4c7SJohn Johansen s = buffer = kmalloc(size, GFP_KERNEL); 14901180b4c7SJohn Johansen if (!buffer) 14911180b4c7SJohn Johansen return ERR_PTR(-ENOMEM); 14925d5182caSJohn Johansen 14935d5182caSJohn Johansen for (; depth > 0; depth--) { 14941180b4c7SJohn Johansen strcpy(s, "../../"); 14951180b4c7SJohn Johansen s += 6; 14961180b4c7SJohn Johansen size -= 6; 14975d5182caSJohn Johansen } 14985d5182caSJohn Johansen 14991180b4c7SJohn Johansen error = snprintf(s, size, "raw_data/%s/%s", dirname, fname); 1500*588558ebSColin Ian King if (error >= size || error < 0) { 1501*588558ebSColin Ian King kfree(buffer); 15021180b4c7SJohn Johansen return ERR_PTR(-ENAMETOOLONG); 1503*588558ebSColin Ian King } 15045d5182caSJohn Johansen 15051180b4c7SJohn Johansen return buffer; 15065d5182caSJohn Johansen } 15075d5182caSJohn Johansen 15081180b4c7SJohn Johansen static void rawdata_link_cb(void *arg) 15091180b4c7SJohn Johansen { 15101180b4c7SJohn Johansen kfree(arg); 15111180b4c7SJohn Johansen } 15121180b4c7SJohn Johansen 15131180b4c7SJohn Johansen static const char *rawdata_get_link_base(struct dentry *dentry, 15141180b4c7SJohn Johansen struct inode *inode, 15151180b4c7SJohn Johansen struct delayed_call *done, 15161180b4c7SJohn Johansen const char *name) 15171180b4c7SJohn Johansen { 15181180b4c7SJohn Johansen struct aa_proxy *proxy = inode->i_private; 15191180b4c7SJohn Johansen struct aa_label *label; 15201180b4c7SJohn Johansen struct aa_profile *profile; 15211180b4c7SJohn Johansen char *target; 15221180b4c7SJohn Johansen int depth; 15231180b4c7SJohn Johansen 15241180b4c7SJohn Johansen if (!dentry) 15251180b4c7SJohn Johansen return ERR_PTR(-ECHILD); 15261180b4c7SJohn Johansen 15271180b4c7SJohn Johansen label = aa_get_label_rcu(&proxy->label); 15281180b4c7SJohn Johansen profile = labels_profile(label); 15291180b4c7SJohn Johansen depth = profile_depth(profile); 15301180b4c7SJohn Johansen target = gen_symlink_name(depth, profile->rawdata->name, name); 15311180b4c7SJohn Johansen aa_put_label(label); 15321180b4c7SJohn Johansen 15331180b4c7SJohn Johansen if (IS_ERR(target)) 15341180b4c7SJohn Johansen return target; 15351180b4c7SJohn Johansen 15361180b4c7SJohn Johansen set_delayed_call(done, rawdata_link_cb, target); 15371180b4c7SJohn Johansen 15381180b4c7SJohn Johansen return target; 15391180b4c7SJohn Johansen } 15401180b4c7SJohn Johansen 15411180b4c7SJohn Johansen static const char *rawdata_get_link_sha1(struct dentry *dentry, 15421180b4c7SJohn Johansen struct inode *inode, 15431180b4c7SJohn Johansen struct delayed_call *done) 15441180b4c7SJohn Johansen { 15451180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "sha1"); 15461180b4c7SJohn Johansen } 15471180b4c7SJohn Johansen 15481180b4c7SJohn Johansen static const char *rawdata_get_link_abi(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, "abi"); 15531180b4c7SJohn Johansen } 15541180b4c7SJohn Johansen 15551180b4c7SJohn Johansen static const char *rawdata_get_link_data(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, "raw_data"); 15601180b4c7SJohn Johansen } 15611180b4c7SJohn Johansen 15621180b4c7SJohn Johansen static const struct inode_operations rawdata_link_sha1_iops = { 15631180b4c7SJohn Johansen .get_link = rawdata_get_link_sha1, 15641180b4c7SJohn Johansen }; 15651180b4c7SJohn Johansen 15661180b4c7SJohn Johansen static const struct inode_operations rawdata_link_abi_iops = { 15671180b4c7SJohn Johansen .get_link = rawdata_get_link_abi, 15681180b4c7SJohn Johansen }; 15691180b4c7SJohn Johansen static const struct inode_operations rawdata_link_data_iops = { 15701180b4c7SJohn Johansen .get_link = rawdata_get_link_data, 15711180b4c7SJohn Johansen }; 15721180b4c7SJohn Johansen 15731180b4c7SJohn Johansen 15745d5182caSJohn Johansen /* 15755d5182caSJohn Johansen * Requires: @profile->ns->lock held 15765d5182caSJohn Johansen */ 1577c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) 15780d259f04SJohn Johansen { 15790d259f04SJohn Johansen struct aa_profile *child; 15800d259f04SJohn Johansen struct dentry *dent = NULL, *dir; 15810d259f04SJohn Johansen int error; 15820d259f04SJohn Johansen 1583cbf2d0e1SJohn Johansen AA_BUG(!profile); 1584cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock)); 1585cbf2d0e1SJohn Johansen 15860d259f04SJohn Johansen if (!parent) { 15870d259f04SJohn Johansen struct aa_profile *p; 15880d259f04SJohn Johansen p = aa_deref_parent(profile); 15890d259f04SJohn Johansen dent = prof_dir(p); 15900d259f04SJohn Johansen /* adding to parent that previously didn't have children */ 1591c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dent); 15920d259f04SJohn Johansen if (IS_ERR(dent)) 15930d259f04SJohn Johansen goto fail; 15940d259f04SJohn Johansen prof_child_dir(p) = parent = dent; 15950d259f04SJohn Johansen } 15960d259f04SJohn Johansen 15970d259f04SJohn Johansen if (!profile->dirname) { 15980d259f04SJohn Johansen int len, id_len; 15990d259f04SJohn Johansen len = mangle_name(profile->base.name, NULL); 16000d259f04SJohn Johansen id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); 16010d259f04SJohn Johansen 16020d259f04SJohn Johansen profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); 1603ffac1de6SDan Carpenter if (!profile->dirname) { 1604ffac1de6SDan Carpenter error = -ENOMEM; 1605ffac1de6SDan Carpenter goto fail2; 1606ffac1de6SDan Carpenter } 16070d259f04SJohn Johansen 16080d259f04SJohn Johansen mangle_name(profile->base.name, profile->dirname); 16090d259f04SJohn Johansen sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); 16100d259f04SJohn Johansen } 16110d259f04SJohn Johansen 1612c961ee5fSJohn Johansen dent = aafs_create_dir(profile->dirname, parent); 16130d259f04SJohn Johansen if (IS_ERR(dent)) 16140d259f04SJohn Johansen goto fail; 16150d259f04SJohn Johansen prof_dir(profile) = dir = dent; 16160d259f04SJohn Johansen 161752b97de3SJohn Johansen dent = create_profile_file(dir, "name", profile, 161852b97de3SJohn Johansen &seq_profile_name_fops); 16190d259f04SJohn Johansen if (IS_ERR(dent)) 16200d259f04SJohn Johansen goto fail; 16210d259f04SJohn Johansen profile->dents[AAFS_PROF_NAME] = dent; 16220d259f04SJohn Johansen 162352b97de3SJohn Johansen dent = create_profile_file(dir, "mode", profile, 162452b97de3SJohn Johansen &seq_profile_mode_fops); 16250d259f04SJohn Johansen if (IS_ERR(dent)) 16260d259f04SJohn Johansen goto fail; 16270d259f04SJohn Johansen profile->dents[AAFS_PROF_MODE] = dent; 16280d259f04SJohn Johansen 1629556d0be7SJohn Johansen dent = create_profile_file(dir, "attach", profile, 163052b97de3SJohn Johansen &seq_profile_attach_fops); 1631556d0be7SJohn Johansen if (IS_ERR(dent)) 1632556d0be7SJohn Johansen goto fail; 1633556d0be7SJohn Johansen profile->dents[AAFS_PROF_ATTACH] = dent; 1634556d0be7SJohn Johansen 1635f8eb8a13SJohn Johansen if (profile->hash) { 1636f8eb8a13SJohn Johansen dent = create_profile_file(dir, "sha1", profile, 163752b97de3SJohn Johansen &seq_profile_hash_fops); 1638f8eb8a13SJohn Johansen if (IS_ERR(dent)) 1639f8eb8a13SJohn Johansen goto fail; 1640f8eb8a13SJohn Johansen profile->dents[AAFS_PROF_HASH] = dent; 1641f8eb8a13SJohn Johansen } 1642f8eb8a13SJohn Johansen 16435ac8c355SJohn Johansen if (profile->rawdata) { 16441180b4c7SJohn Johansen dent = aafs_create_symlink("raw_sha1", dir, NULL, 16451180b4c7SJohn Johansen profile->label.proxy, 16461180b4c7SJohn Johansen &rawdata_link_sha1_iops); 16475ac8c355SJohn Johansen if (IS_ERR(dent)) 16485ac8c355SJohn Johansen goto fail; 16491180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 16505ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_HASH] = dent; 16515ac8c355SJohn Johansen 16521180b4c7SJohn Johansen dent = aafs_create_symlink("raw_abi", dir, NULL, 16531180b4c7SJohn Johansen profile->label.proxy, 16541180b4c7SJohn Johansen &rawdata_link_abi_iops); 16555ac8c355SJohn Johansen if (IS_ERR(dent)) 16565ac8c355SJohn Johansen goto fail; 16571180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 16585ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_ABI] = dent; 16595ac8c355SJohn Johansen 16601180b4c7SJohn Johansen dent = aafs_create_symlink("raw_data", dir, NULL, 16611180b4c7SJohn Johansen profile->label.proxy, 16621180b4c7SJohn Johansen &rawdata_link_data_iops); 16635ac8c355SJohn Johansen if (IS_ERR(dent)) 16645ac8c355SJohn Johansen goto fail; 16651180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 16665ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_DATA] = dent; 16675ac8c355SJohn Johansen } 16685ac8c355SJohn Johansen 16690d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) { 1670c97204baSJohn Johansen error = __aafs_profile_mkdir(child, prof_child_dir(profile)); 16710d259f04SJohn Johansen if (error) 16720d259f04SJohn Johansen goto fail2; 16730d259f04SJohn Johansen } 16740d259f04SJohn Johansen 16750d259f04SJohn Johansen return 0; 16760d259f04SJohn Johansen 16770d259f04SJohn Johansen fail: 16780d259f04SJohn Johansen error = PTR_ERR(dent); 16790d259f04SJohn Johansen 16800d259f04SJohn Johansen fail2: 1681c97204baSJohn Johansen __aafs_profile_rmdir(profile); 16820d259f04SJohn Johansen 16830d259f04SJohn Johansen return error; 16840d259f04SJohn Johansen } 16850d259f04SJohn Johansen 16864ae47f33SJohn Johansen static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode) 16874ae47f33SJohn Johansen { 16884ae47f33SJohn Johansen struct aa_ns *ns, *parent; 16894ae47f33SJohn Johansen /* TODO: improve permission check */ 1690637f688dSJohn Johansen struct aa_label *label; 1691637f688dSJohn Johansen int error; 1692637f688dSJohn Johansen 1693637f688dSJohn Johansen label = begin_current_label_crit_section(); 1694637f688dSJohn Johansen error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); 1695637f688dSJohn Johansen end_current_label_crit_section(label); 16964ae47f33SJohn Johansen if (error) 16974ae47f33SJohn Johansen return error; 16984ae47f33SJohn Johansen 16994ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 17004ae47f33SJohn Johansen AA_BUG(d_inode(ns_subns_dir(parent)) != dir); 17014ae47f33SJohn Johansen 17024ae47f33SJohn Johansen /* we have to unlock and then relock to get locking order right 17034ae47f33SJohn Johansen * for pin_fs 17044ae47f33SJohn Johansen */ 17054ae47f33SJohn Johansen inode_unlock(dir); 17064ae47f33SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 1707feb3c766SJohn Johansen mutex_lock_nested(&parent->lock, parent->level); 17084ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 17094ae47f33SJohn Johansen if (error) 17104ae47f33SJohn Johansen goto out; 17114ae47f33SJohn Johansen 17124ae47f33SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL, 17134ae47f33SJohn Johansen NULL, NULL, NULL); 17144ae47f33SJohn Johansen if (error) 17154ae47f33SJohn Johansen goto out_pin; 17164ae47f33SJohn Johansen 17174ae47f33SJohn Johansen ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name), 17184ae47f33SJohn Johansen dentry); 17194ae47f33SJohn Johansen if (IS_ERR(ns)) { 17204ae47f33SJohn Johansen error = PTR_ERR(ns); 17214ae47f33SJohn Johansen ns = NULL; 17224ae47f33SJohn Johansen } 17234ae47f33SJohn Johansen 17244ae47f33SJohn Johansen aa_put_ns(ns); /* list ref remains */ 17254ae47f33SJohn Johansen out_pin: 17264ae47f33SJohn Johansen if (error) 17274ae47f33SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 17284ae47f33SJohn Johansen out: 17294ae47f33SJohn Johansen mutex_unlock(&parent->lock); 17304ae47f33SJohn Johansen aa_put_ns(parent); 17314ae47f33SJohn Johansen 17324ae47f33SJohn Johansen return error; 17334ae47f33SJohn Johansen } 17344ae47f33SJohn Johansen 17354ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry) 17364ae47f33SJohn Johansen { 17374ae47f33SJohn Johansen struct aa_ns *ns, *parent; 17384ae47f33SJohn Johansen /* TODO: improve permission check */ 1739637f688dSJohn Johansen struct aa_label *label; 1740637f688dSJohn Johansen int error; 1741637f688dSJohn Johansen 1742637f688dSJohn Johansen label = begin_current_label_crit_section(); 1743637f688dSJohn Johansen error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); 1744637f688dSJohn Johansen end_current_label_crit_section(label); 17454ae47f33SJohn Johansen if (error) 17464ae47f33SJohn Johansen return error; 17474ae47f33SJohn Johansen 17484ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 17494ae47f33SJohn Johansen /* rmdir calls the generic securityfs functions to remove files 17504ae47f33SJohn Johansen * from the apparmor dir. It is up to the apparmor ns locking 17514ae47f33SJohn Johansen * to avoid races. 17524ae47f33SJohn Johansen */ 17534ae47f33SJohn Johansen inode_unlock(dir); 17544ae47f33SJohn Johansen inode_unlock(dentry->d_inode); 17554ae47f33SJohn Johansen 1756feb3c766SJohn Johansen mutex_lock_nested(&parent->lock, parent->level); 17574ae47f33SJohn Johansen ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name, 17584ae47f33SJohn Johansen dentry->d_name.len)); 17594ae47f33SJohn Johansen if (!ns) { 17604ae47f33SJohn Johansen error = -ENOENT; 17614ae47f33SJohn Johansen goto out; 17624ae47f33SJohn Johansen } 17634ae47f33SJohn Johansen AA_BUG(ns_dir(ns) != dentry); 17644ae47f33SJohn Johansen 17654ae47f33SJohn Johansen __aa_remove_ns(ns); 17664ae47f33SJohn Johansen aa_put_ns(ns); 17674ae47f33SJohn Johansen 17684ae47f33SJohn Johansen out: 17694ae47f33SJohn Johansen mutex_unlock(&parent->lock); 17704ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 17714ae47f33SJohn Johansen inode_lock(dentry->d_inode); 17724ae47f33SJohn Johansen aa_put_ns(parent); 17734ae47f33SJohn Johansen 17744ae47f33SJohn Johansen return error; 17754ae47f33SJohn Johansen } 17764ae47f33SJohn Johansen 17774ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = { 17784ae47f33SJohn Johansen .lookup = simple_lookup, 17794ae47f33SJohn Johansen .mkdir = ns_mkdir_op, 17804ae47f33SJohn Johansen .rmdir = ns_rmdir_op, 17814ae47f33SJohn Johansen }; 17824ae47f33SJohn Johansen 17835d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns) 17845d5182caSJohn Johansen { 17855d5182caSJohn Johansen struct aa_loaddata *ent, *tmp; 17865d5182caSJohn Johansen 17875d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 17885d5182caSJohn Johansen 17895d5182caSJohn Johansen list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list) 17905d5182caSJohn Johansen __aa_fs_remove_rawdata(ent); 17915d5182caSJohn Johansen } 17925d5182caSJohn Johansen 1793c97204baSJohn Johansen /** 1794c97204baSJohn Johansen * 1795c97204baSJohn Johansen * Requires: @ns->lock held 1796c97204baSJohn Johansen */ 1797c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns) 17980d259f04SJohn Johansen { 179998849dffSJohn Johansen struct aa_ns *sub; 18000d259f04SJohn Johansen struct aa_profile *child; 18010d259f04SJohn Johansen int i; 18020d259f04SJohn Johansen 18030d259f04SJohn Johansen if (!ns) 18040d259f04SJohn Johansen return; 1805cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 18060d259f04SJohn Johansen 18070d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) 1808c97204baSJohn Johansen __aafs_profile_rmdir(child); 18090d259f04SJohn Johansen 18100d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 1811feb3c766SJohn Johansen mutex_lock_nested(&sub->lock, sub->level); 1812c97204baSJohn Johansen __aafs_ns_rmdir(sub); 18130d259f04SJohn Johansen mutex_unlock(&sub->lock); 18140d259f04SJohn Johansen } 18150d259f04SJohn Johansen 18165d5182caSJohn Johansen __aa_fs_list_remove_rawdata(ns); 18175d5182caSJohn Johansen 1818b7fd2c03SJohn Johansen if (ns_subns_dir(ns)) { 1819b7fd2c03SJohn Johansen sub = d_inode(ns_subns_dir(ns))->i_private; 1820b7fd2c03SJohn Johansen aa_put_ns(sub); 1821b7fd2c03SJohn Johansen } 1822b7fd2c03SJohn Johansen if (ns_subload(ns)) { 1823b7fd2c03SJohn Johansen sub = d_inode(ns_subload(ns))->i_private; 1824b7fd2c03SJohn Johansen aa_put_ns(sub); 1825b7fd2c03SJohn Johansen } 1826b7fd2c03SJohn Johansen if (ns_subreplace(ns)) { 1827b7fd2c03SJohn Johansen sub = d_inode(ns_subreplace(ns))->i_private; 1828b7fd2c03SJohn Johansen aa_put_ns(sub); 1829b7fd2c03SJohn Johansen } 1830b7fd2c03SJohn Johansen if (ns_subremove(ns)) { 1831b7fd2c03SJohn Johansen sub = d_inode(ns_subremove(ns))->i_private; 1832b7fd2c03SJohn Johansen aa_put_ns(sub); 1833b7fd2c03SJohn Johansen } 1834d9bf2c26SJohn Johansen if (ns_subrevision(ns)) { 1835d9bf2c26SJohn Johansen sub = d_inode(ns_subrevision(ns))->i_private; 1836d9bf2c26SJohn Johansen aa_put_ns(sub); 1837d9bf2c26SJohn Johansen } 1838b7fd2c03SJohn Johansen 18390d259f04SJohn Johansen for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { 1840c961ee5fSJohn Johansen aafs_remove(ns->dents[i]); 18410d259f04SJohn Johansen ns->dents[i] = NULL; 18420d259f04SJohn Johansen } 18430d259f04SJohn Johansen } 18440d259f04SJohn Johansen 1845b7fd2c03SJohn Johansen /* assumes cleanup in caller */ 1846c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) 1847b7fd2c03SJohn Johansen { 1848b7fd2c03SJohn Johansen struct dentry *dent; 1849b7fd2c03SJohn Johansen 1850b7fd2c03SJohn Johansen AA_BUG(!ns); 1851b7fd2c03SJohn Johansen AA_BUG(!dir); 1852b7fd2c03SJohn Johansen 1853c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dir); 1854b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1855b7fd2c03SJohn Johansen return PTR_ERR(dent); 1856b7fd2c03SJohn Johansen ns_subprofs_dir(ns) = dent; 1857b7fd2c03SJohn Johansen 1858c961ee5fSJohn Johansen dent = aafs_create_dir("raw_data", dir); 1859b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1860b7fd2c03SJohn Johansen return PTR_ERR(dent); 1861b7fd2c03SJohn Johansen ns_subdata_dir(ns) = dent; 1862b7fd2c03SJohn Johansen 1863d9bf2c26SJohn Johansen dent = aafs_create_file("revision", 0444, dir, ns, 1864d9bf2c26SJohn Johansen &aa_fs_ns_revision_fops); 1865d9bf2c26SJohn Johansen if (IS_ERR(dent)) 1866d9bf2c26SJohn Johansen return PTR_ERR(dent); 1867d9bf2c26SJohn Johansen aa_get_ns(ns); 1868d9bf2c26SJohn Johansen ns_subrevision(ns) = dent; 1869d9bf2c26SJohn Johansen 1870c961ee5fSJohn Johansen dent = aafs_create_file(".load", 0640, dir, ns, 1871b7fd2c03SJohn Johansen &aa_fs_profile_load); 1872b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1873b7fd2c03SJohn Johansen return PTR_ERR(dent); 1874b7fd2c03SJohn Johansen aa_get_ns(ns); 1875b7fd2c03SJohn Johansen ns_subload(ns) = dent; 1876b7fd2c03SJohn Johansen 1877c961ee5fSJohn Johansen dent = aafs_create_file(".replace", 0640, dir, ns, 1878b7fd2c03SJohn Johansen &aa_fs_profile_replace); 1879b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1880b7fd2c03SJohn Johansen return PTR_ERR(dent); 1881b7fd2c03SJohn Johansen aa_get_ns(ns); 1882b7fd2c03SJohn Johansen ns_subreplace(ns) = dent; 1883b7fd2c03SJohn Johansen 1884c961ee5fSJohn Johansen dent = aafs_create_file(".remove", 0640, dir, ns, 1885b7fd2c03SJohn Johansen &aa_fs_profile_remove); 1886b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1887b7fd2c03SJohn Johansen return PTR_ERR(dent); 1888b7fd2c03SJohn Johansen aa_get_ns(ns); 1889b7fd2c03SJohn Johansen ns_subremove(ns) = dent; 1890b7fd2c03SJohn Johansen 18914ae47f33SJohn Johansen /* use create_dentry so we can supply private data */ 18924ae47f33SJohn Johansen dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL, 18934ae47f33SJohn Johansen &ns_dir_inode_operations); 1894b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1895b7fd2c03SJohn Johansen return PTR_ERR(dent); 1896b7fd2c03SJohn Johansen aa_get_ns(ns); 1897b7fd2c03SJohn Johansen ns_subns_dir(ns) = dent; 1898b7fd2c03SJohn Johansen 1899b7fd2c03SJohn Johansen return 0; 1900b7fd2c03SJohn Johansen } 1901b7fd2c03SJohn Johansen 1902c97204baSJohn Johansen /* 1903c97204baSJohn Johansen * Requires: @ns->lock held 1904c97204baSJohn Johansen */ 190598407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name, 190698407f0aSJohn Johansen struct dentry *dent) 19070d259f04SJohn Johansen { 190898849dffSJohn Johansen struct aa_ns *sub; 19090d259f04SJohn Johansen struct aa_profile *child; 191098407f0aSJohn Johansen struct dentry *dir; 19110d259f04SJohn Johansen int error; 19120d259f04SJohn Johansen 1913b7fd2c03SJohn Johansen AA_BUG(!ns); 1914b7fd2c03SJohn Johansen AA_BUG(!parent); 1915b7fd2c03SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 1916b7fd2c03SJohn Johansen 19170d259f04SJohn Johansen if (!name) 19180d259f04SJohn Johansen name = ns->base.name; 19190d259f04SJohn Johansen 1920c961ee5fSJohn Johansen if (!dent) { 1921b7fd2c03SJohn Johansen /* create ns dir if it doesn't already exist */ 1922c961ee5fSJohn Johansen dent = aafs_create_dir(name, parent); 19230d259f04SJohn Johansen if (IS_ERR(dent)) 19240d259f04SJohn Johansen goto fail; 1925c961ee5fSJohn Johansen } else 1926c961ee5fSJohn Johansen dget(dent); 19270d259f04SJohn Johansen ns_dir(ns) = dir = dent; 1928c97204baSJohn Johansen error = __aafs_ns_mkdir_entries(ns, dir); 1929b7fd2c03SJohn Johansen if (error) 1930b7fd2c03SJohn Johansen goto fail2; 19310d259f04SJohn Johansen 1932b7fd2c03SJohn Johansen /* profiles */ 19330d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) { 1934c97204baSJohn Johansen error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns)); 19350d259f04SJohn Johansen if (error) 19360d259f04SJohn Johansen goto fail2; 19370d259f04SJohn Johansen } 19380d259f04SJohn Johansen 1939b7fd2c03SJohn Johansen /* subnamespaces */ 19400d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 1941feb3c766SJohn Johansen mutex_lock_nested(&sub->lock, sub->level); 194298407f0aSJohn Johansen error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL); 19430d259f04SJohn Johansen mutex_unlock(&sub->lock); 19440d259f04SJohn Johansen if (error) 19450d259f04SJohn Johansen goto fail2; 19460d259f04SJohn Johansen } 19470d259f04SJohn Johansen 19480d259f04SJohn Johansen return 0; 19490d259f04SJohn Johansen 19500d259f04SJohn Johansen fail: 19510d259f04SJohn Johansen error = PTR_ERR(dent); 19520d259f04SJohn Johansen 19530d259f04SJohn Johansen fail2: 1954c97204baSJohn Johansen __aafs_ns_rmdir(ns); 19550d259f04SJohn Johansen 19560d259f04SJohn Johansen return error; 19570d259f04SJohn Johansen } 19580d259f04SJohn Johansen 19590d259f04SJohn Johansen 196029b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head)) 196129b3822fSJohn Johansen 196229b3822fSJohn Johansen /** 196398849dffSJohn Johansen * __next_ns - find the next namespace to list 196429b3822fSJohn Johansen * @root: root namespace to stop search at (NOT NULL) 196529b3822fSJohn Johansen * @ns: current ns position (NOT NULL) 196629b3822fSJohn Johansen * 196729b3822fSJohn Johansen * Find the next namespace from @ns under @root and handle all locking needed 196829b3822fSJohn Johansen * while switching current namespace. 196929b3822fSJohn Johansen * 197029b3822fSJohn Johansen * Returns: next namespace or NULL if at last namespace under @root 197129b3822fSJohn Johansen * Requires: ns->parent->lock to be held 197229b3822fSJohn Johansen * NOTE: will not unlock root->lock 197329b3822fSJohn Johansen */ 197498849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) 197529b3822fSJohn Johansen { 197698849dffSJohn Johansen struct aa_ns *parent, *next; 197729b3822fSJohn Johansen 1978cbf2d0e1SJohn Johansen AA_BUG(!root); 1979cbf2d0e1SJohn Johansen AA_BUG(!ns); 1980cbf2d0e1SJohn Johansen AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock)); 1981cbf2d0e1SJohn Johansen 198229b3822fSJohn Johansen /* is next namespace a child */ 198329b3822fSJohn Johansen if (!list_empty(&ns->sub_ns)) { 198429b3822fSJohn Johansen next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); 1985feb3c766SJohn Johansen mutex_lock_nested(&next->lock, next->level); 198629b3822fSJohn Johansen return next; 198729b3822fSJohn Johansen } 198829b3822fSJohn Johansen 198929b3822fSJohn Johansen /* check if the next ns is a sibling, parent, gp, .. */ 199029b3822fSJohn Johansen parent = ns->parent; 1991ed2c7da3SJohn Johansen while (ns != root) { 199229b3822fSJohn Johansen mutex_unlock(&ns->lock); 199338dbd7d8SGeliang Tang next = list_next_entry(ns, base.list); 199429b3822fSJohn Johansen if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { 1995feb3c766SJohn Johansen mutex_lock_nested(&next->lock, next->level); 199629b3822fSJohn Johansen return next; 199729b3822fSJohn Johansen } 199829b3822fSJohn Johansen ns = parent; 199929b3822fSJohn Johansen parent = parent->parent; 200029b3822fSJohn Johansen } 200129b3822fSJohn Johansen 200229b3822fSJohn Johansen return NULL; 200329b3822fSJohn Johansen } 200429b3822fSJohn Johansen 200529b3822fSJohn Johansen /** 200629b3822fSJohn Johansen * __first_profile - find the first profile in a namespace 200729b3822fSJohn Johansen * @root: namespace that is root of profiles being displayed (NOT NULL) 200829b3822fSJohn Johansen * @ns: namespace to start in (NOT NULL) 200929b3822fSJohn Johansen * 201029b3822fSJohn Johansen * Returns: unrefcounted profile or NULL if no profile 201129b3822fSJohn Johansen * Requires: profile->ns.lock to be held 201229b3822fSJohn Johansen */ 201398849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root, 201498849dffSJohn Johansen struct aa_ns *ns) 201529b3822fSJohn Johansen { 2016cbf2d0e1SJohn Johansen AA_BUG(!root); 2017cbf2d0e1SJohn Johansen AA_BUG(ns && !mutex_is_locked(&ns->lock)); 2018cbf2d0e1SJohn Johansen 201998849dffSJohn Johansen for (; ns; ns = __next_ns(root, ns)) { 202029b3822fSJohn Johansen if (!list_empty(&ns->base.profiles)) 202129b3822fSJohn Johansen return list_first_entry(&ns->base.profiles, 202229b3822fSJohn Johansen struct aa_profile, base.list); 202329b3822fSJohn Johansen } 202429b3822fSJohn Johansen return NULL; 202529b3822fSJohn Johansen } 202629b3822fSJohn Johansen 202729b3822fSJohn Johansen /** 202829b3822fSJohn Johansen * __next_profile - step to the next profile in a profile tree 202929b3822fSJohn Johansen * @profile: current profile in tree (NOT NULL) 203029b3822fSJohn Johansen * 203129b3822fSJohn Johansen * Perform a depth first traversal on the profile tree in a namespace 203229b3822fSJohn Johansen * 203329b3822fSJohn Johansen * Returns: next profile or NULL if done 203429b3822fSJohn Johansen * Requires: profile->ns.lock to be held 203529b3822fSJohn Johansen */ 203629b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p) 203729b3822fSJohn Johansen { 203829b3822fSJohn Johansen struct aa_profile *parent; 203998849dffSJohn Johansen struct aa_ns *ns = p->ns; 204029b3822fSJohn Johansen 2041cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock)); 2042cbf2d0e1SJohn Johansen 204329b3822fSJohn Johansen /* is next profile a child */ 204429b3822fSJohn Johansen if (!list_empty(&p->base.profiles)) 204529b3822fSJohn Johansen return list_first_entry(&p->base.profiles, typeof(*p), 204629b3822fSJohn Johansen base.list); 204729b3822fSJohn Johansen 204829b3822fSJohn Johansen /* is next profile a sibling, parent sibling, gp, sibling, .. */ 204929b3822fSJohn Johansen parent = rcu_dereference_protected(p->parent, 205029b3822fSJohn Johansen mutex_is_locked(&p->ns->lock)); 205129b3822fSJohn Johansen while (parent) { 205238dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 205329b3822fSJohn Johansen if (!list_entry_is_head(p, &parent->base.profiles, base.list)) 205429b3822fSJohn Johansen return p; 205529b3822fSJohn Johansen p = parent; 205629b3822fSJohn Johansen parent = rcu_dereference_protected(parent->parent, 205729b3822fSJohn Johansen mutex_is_locked(&parent->ns->lock)); 205829b3822fSJohn Johansen } 205929b3822fSJohn Johansen 206029b3822fSJohn Johansen /* is next another profile in the namespace */ 206138dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 206229b3822fSJohn Johansen if (!list_entry_is_head(p, &ns->base.profiles, base.list)) 206329b3822fSJohn Johansen return p; 206429b3822fSJohn Johansen 206529b3822fSJohn Johansen return NULL; 206629b3822fSJohn Johansen } 206729b3822fSJohn Johansen 206829b3822fSJohn Johansen /** 206929b3822fSJohn Johansen * next_profile - step to the next profile in where ever it may be 207029b3822fSJohn Johansen * @root: root namespace (NOT NULL) 207129b3822fSJohn Johansen * @profile: current profile (NOT NULL) 207229b3822fSJohn Johansen * 207329b3822fSJohn Johansen * Returns: next profile or NULL if there isn't one 207429b3822fSJohn Johansen */ 207598849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root, 207629b3822fSJohn Johansen struct aa_profile *profile) 207729b3822fSJohn Johansen { 207829b3822fSJohn Johansen struct aa_profile *next = __next_profile(profile); 207929b3822fSJohn Johansen if (next) 208029b3822fSJohn Johansen return next; 208129b3822fSJohn Johansen 208229b3822fSJohn Johansen /* finished all profiles in namespace move to next namespace */ 208398849dffSJohn Johansen return __first_profile(root, __next_ns(root, profile->ns)); 208429b3822fSJohn Johansen } 208529b3822fSJohn Johansen 208629b3822fSJohn Johansen /** 208729b3822fSJohn Johansen * p_start - start a depth first traversal of profile tree 208829b3822fSJohn Johansen * @f: seq_file to fill 208929b3822fSJohn Johansen * @pos: current position 209029b3822fSJohn Johansen * 209129b3822fSJohn Johansen * Returns: first profile under current namespace or NULL if none found 209229b3822fSJohn Johansen * 209329b3822fSJohn Johansen * acquires first ns->lock 209429b3822fSJohn Johansen */ 209529b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos) 209629b3822fSJohn Johansen { 209729b3822fSJohn Johansen struct aa_profile *profile = NULL; 2098cf797c0eSJohn Johansen struct aa_ns *root = aa_get_current_ns(); 209929b3822fSJohn Johansen loff_t l = *pos; 2100cf797c0eSJohn Johansen f->private = root; 210129b3822fSJohn Johansen 210229b3822fSJohn Johansen /* find the first profile */ 2103feb3c766SJohn Johansen mutex_lock_nested(&root->lock, root->level); 210429b3822fSJohn Johansen profile = __first_profile(root, root); 210529b3822fSJohn Johansen 210629b3822fSJohn Johansen /* skip to position */ 210729b3822fSJohn Johansen for (; profile && l > 0; l--) 210829b3822fSJohn Johansen profile = next_profile(root, profile); 210929b3822fSJohn Johansen 211029b3822fSJohn Johansen return profile; 211129b3822fSJohn Johansen } 211229b3822fSJohn Johansen 211329b3822fSJohn Johansen /** 211429b3822fSJohn Johansen * p_next - read the next profile entry 211529b3822fSJohn Johansen * @f: seq_file to fill 211629b3822fSJohn Johansen * @p: profile previously returned 211729b3822fSJohn Johansen * @pos: current position 211829b3822fSJohn Johansen * 211929b3822fSJohn Johansen * Returns: next profile after @p or NULL if none 212029b3822fSJohn Johansen * 212129b3822fSJohn Johansen * may acquire/release locks in namespace tree as necessary 212229b3822fSJohn Johansen */ 212329b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos) 212429b3822fSJohn Johansen { 212529b3822fSJohn Johansen struct aa_profile *profile = p; 212698849dffSJohn Johansen struct aa_ns *ns = f->private; 212729b3822fSJohn Johansen (*pos)++; 212829b3822fSJohn Johansen 212929b3822fSJohn Johansen return next_profile(ns, profile); 213029b3822fSJohn Johansen } 213129b3822fSJohn Johansen 213229b3822fSJohn Johansen /** 213329b3822fSJohn Johansen * p_stop - stop depth first traversal 213429b3822fSJohn Johansen * @f: seq_file we are filling 213529b3822fSJohn Johansen * @p: the last profile writen 213629b3822fSJohn Johansen * 213729b3822fSJohn Johansen * Release all locking done by p_start/p_next on namespace tree 213829b3822fSJohn Johansen */ 213929b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p) 214029b3822fSJohn Johansen { 214129b3822fSJohn Johansen struct aa_profile *profile = p; 214298849dffSJohn Johansen struct aa_ns *root = f->private, *ns; 214329b3822fSJohn Johansen 214429b3822fSJohn Johansen if (profile) { 214529b3822fSJohn Johansen for (ns = profile->ns; ns && ns != root; ns = ns->parent) 214629b3822fSJohn Johansen mutex_unlock(&ns->lock); 214729b3822fSJohn Johansen } 214829b3822fSJohn Johansen mutex_unlock(&root->lock); 214998849dffSJohn Johansen aa_put_ns(root); 215029b3822fSJohn Johansen } 215129b3822fSJohn Johansen 215229b3822fSJohn Johansen /** 215329b3822fSJohn Johansen * seq_show_profile - show a profile entry 215429b3822fSJohn Johansen * @f: seq_file to file 215529b3822fSJohn Johansen * @p: current position (profile) (NOT NULL) 215629b3822fSJohn Johansen * 215729b3822fSJohn Johansen * Returns: error on failure 215829b3822fSJohn Johansen */ 215929b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p) 216029b3822fSJohn Johansen { 216129b3822fSJohn Johansen struct aa_profile *profile = (struct aa_profile *)p; 216298849dffSJohn Johansen struct aa_ns *root = f->private; 216329b3822fSJohn Johansen 2164637f688dSJohn Johansen aa_label_seq_xprint(f, root, &profile->label, 2165637f688dSJohn Johansen FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL); 2166637f688dSJohn Johansen seq_putc(f, '\n'); 216729b3822fSJohn Johansen 216829b3822fSJohn Johansen return 0; 216929b3822fSJohn Johansen } 217029b3822fSJohn Johansen 2171c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = { 217229b3822fSJohn Johansen .start = p_start, 217329b3822fSJohn Johansen .next = p_next, 217429b3822fSJohn Johansen .stop = p_stop, 217529b3822fSJohn Johansen .show = seq_show_profile, 217629b3822fSJohn Johansen }; 217729b3822fSJohn Johansen 217829b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file) 217929b3822fSJohn Johansen { 21805ac8c355SJohn Johansen if (!policy_view_capable(NULL)) 21815ac8c355SJohn Johansen return -EACCES; 21825ac8c355SJohn Johansen 2183c97204baSJohn Johansen return seq_open(file, &aa_sfs_profiles_op); 218429b3822fSJohn Johansen } 218529b3822fSJohn Johansen 218629b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file) 218729b3822fSJohn Johansen { 218829b3822fSJohn Johansen return seq_release(inode, file); 218929b3822fSJohn Johansen } 219029b3822fSJohn Johansen 2191c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = { 219229b3822fSJohn Johansen .open = profiles_open, 219329b3822fSJohn Johansen .read = seq_read, 219429b3822fSJohn Johansen .llseek = seq_lseek, 219529b3822fSJohn Johansen .release = profiles_release, 219629b3822fSJohn Johansen }; 219729b3822fSJohn Johansen 219829b3822fSJohn Johansen 21990d259f04SJohn Johansen /** Base file system setup **/ 2200c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = { 2201c97204baSJohn Johansen AA_SFS_FILE_STRING("mask", 2202c97204baSJohn Johansen "create read write exec append mmap_exec link lock"), 2203a9bf8e9fSKees Cook { } 2204a9bf8e9fSKees Cook }; 2205a9bf8e9fSKees Cook 2206290f458aSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ptrace[] = { 2207290f458aSJohn Johansen AA_SFS_FILE_STRING("mask", "read trace"), 2208290f458aSJohn Johansen { } 2209290f458aSJohn Johansen }; 2210290f458aSJohn Johansen 2211cd1dbf76SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_signal[] = { 2212cd1dbf76SJohn Johansen AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK), 2213cd1dbf76SJohn Johansen { } 2214cd1dbf76SJohn Johansen }; 2215cd1dbf76SJohn Johansen 221673f488cdSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_attach[] = { 221773f488cdSJohn Johansen AA_SFS_FILE_BOOLEAN("xattr", 1), 221873f488cdSJohn Johansen { } 221973f488cdSJohn Johansen }; 2220c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = { 2221c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hat", 1), 2222c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hatv", 1), 2223c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_onexec", 1), 2224c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_profile", 1), 22256c5fc8f1SJohn Johansen AA_SFS_FILE_BOOLEAN("stack", 1), 2226c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), 22279fcf78ccSJohn Johansen AA_SFS_FILE_BOOLEAN("post_nnp_subset", 1), 222821f60661SJohn Johansen AA_SFS_FILE_BOOLEAN("computed_longest_left", 1), 222973f488cdSJohn Johansen AA_SFS_DIR("attach_conditions", aa_sfs_entry_attach), 2230c97204baSJohn Johansen AA_SFS_FILE_STRING("version", "1.2"), 2231e74abcf3SKees Cook { } 2232e74abcf3SKees Cook }; 2233e74abcf3SKees Cook 2234c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = { 2235c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("v5", 1), 22365379a331SJohn Johansen AA_SFS_FILE_BOOLEAN("v6", 1), 22375379a331SJohn Johansen AA_SFS_FILE_BOOLEAN("v7", 1), 223856974a6fSJohn Johansen AA_SFS_FILE_BOOLEAN("v8", 1), 2239474d6b75SJohn Johansen { } 2240474d6b75SJohn Johansen }; 2241474d6b75SJohn Johansen 2242c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = { 2243c97204baSJohn Johansen AA_SFS_DIR("versions", aa_sfs_entry_versions), 2244c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("set_load", 1), 22459d910a3bSJohn Johansen { } 22469d910a3bSJohn Johansen }; 22479d910a3bSJohn Johansen 22482ea3ffb7SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_mount[] = { 22492ea3ffb7SJohn Johansen AA_SFS_FILE_STRING("mask", "mount umount pivot_root"), 22502ea3ffb7SJohn Johansen { } 22512ea3ffb7SJohn Johansen }; 22522ea3ffb7SJohn Johansen 225333f2eadaSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ns[] = { 225433f2eadaSJohn Johansen AA_SFS_FILE_BOOLEAN("profile", 1), 22552ea3ffb7SJohn Johansen AA_SFS_FILE_BOOLEAN("pivot_root", 0), 225633f2eadaSJohn Johansen { } 225733f2eadaSJohn Johansen }; 225833f2eadaSJohn Johansen 2259a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = { 22604f3b3f2dSJohn Johansen AA_SFS_FILE_STRING("perms", "allow deny audit quiet"), 2261a83bd86eSJohn Johansen AA_SFS_FILE_BOOLEAN("data", 1), 22621dea3b41SJohn Johansen AA_SFS_FILE_BOOLEAN("multi_transaction", 1), 2263a83bd86eSJohn Johansen { } 2264a83bd86eSJohn Johansen }; 2265a83bd86eSJohn Johansen 2266a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = { 2267a83bd86eSJohn Johansen AA_SFS_DIR("label", aa_sfs_entry_query_label), 2268a83bd86eSJohn Johansen { } 2269a83bd86eSJohn Johansen }; 2270c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = { 2271c97204baSJohn Johansen AA_SFS_DIR("policy", aa_sfs_entry_policy), 2272c97204baSJohn Johansen AA_SFS_DIR("domain", aa_sfs_entry_domain), 2273c97204baSJohn Johansen AA_SFS_DIR("file", aa_sfs_entry_file), 227456974a6fSJohn Johansen AA_SFS_DIR("network_v8", aa_sfs_entry_network), 22752ea3ffb7SJohn Johansen AA_SFS_DIR("mount", aa_sfs_entry_mount), 227633f2eadaSJohn Johansen AA_SFS_DIR("namespaces", aa_sfs_entry_ns), 2277c97204baSJohn Johansen AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 2278c97204baSJohn Johansen AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit), 2279c97204baSJohn Johansen AA_SFS_DIR("caps", aa_sfs_entry_caps), 2280290f458aSJohn Johansen AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace), 2281cd1dbf76SJohn Johansen AA_SFS_DIR("signal", aa_sfs_entry_signal), 2282a83bd86eSJohn Johansen AA_SFS_DIR("query", aa_sfs_entry_query), 2283e74abcf3SKees Cook { } 2284e74abcf3SKees Cook }; 2285e74abcf3SKees Cook 2286c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = { 2287bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access), 22886c5fc8f1SJohn Johansen AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops), 22896c5fc8f1SJohn Johansen AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops), 2290bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops), 2291bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops), 2292bf81100fSJohn Johansen AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops), 2293c97204baSJohn Johansen AA_SFS_DIR("features", aa_sfs_entry_features), 22949acd494bSKees Cook { } 22959acd494bSKees Cook }; 229663e2b423SJohn Johansen 2297c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry = 2298c97204baSJohn Johansen AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor); 22999acd494bSKees Cook 23009acd494bSKees Cook /** 2301a481f4d9SJohn Johansen * entry_create_file - create a file entry in the apparmor securityfs 2302c97204baSJohn Johansen * @fs_file: aa_sfs_entry to build an entry for (NOT NULL) 23039acd494bSKees Cook * @parent: the parent dentry in the securityfs 23049acd494bSKees Cook * 2305a481f4d9SJohn Johansen * Use entry_remove_file to remove entries created with this fn. 23069acd494bSKees Cook */ 2307c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file, 23089acd494bSKees Cook struct dentry *parent) 230963e2b423SJohn Johansen { 23109acd494bSKees Cook int error = 0; 231163e2b423SJohn Johansen 23129acd494bSKees Cook fs_file->dentry = securityfs_create_file(fs_file->name, 23139acd494bSKees Cook S_IFREG | fs_file->mode, 23149acd494bSKees Cook parent, fs_file, 23159acd494bSKees Cook fs_file->file_ops); 23169acd494bSKees Cook if (IS_ERR(fs_file->dentry)) { 23179acd494bSKees Cook error = PTR_ERR(fs_file->dentry); 23189acd494bSKees Cook fs_file->dentry = NULL; 231963e2b423SJohn Johansen } 23209acd494bSKees Cook return error; 232163e2b423SJohn Johansen } 232263e2b423SJohn Johansen 2323c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir); 232463e2b423SJohn Johansen /** 2325a481f4d9SJohn Johansen * entry_create_dir - recursively create a directory entry in the securityfs 2326c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL) 23279acd494bSKees Cook * @parent: the parent dentry in the securityfs 232863e2b423SJohn Johansen * 2329a481f4d9SJohn Johansen * Use entry_remove_dir to remove entries created with this fn. 233063e2b423SJohn Johansen */ 2331c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir, 23329acd494bSKees Cook struct dentry *parent) 233363e2b423SJohn Johansen { 2334c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 23350d259f04SJohn Johansen struct dentry *dir; 23360d259f04SJohn Johansen int error; 233763e2b423SJohn Johansen 23380d259f04SJohn Johansen dir = securityfs_create_dir(fs_dir->name, parent); 23390d259f04SJohn Johansen if (IS_ERR(dir)) 23400d259f04SJohn Johansen return PTR_ERR(dir); 23410d259f04SJohn Johansen fs_dir->dentry = dir; 234263e2b423SJohn Johansen 23430d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2344c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2345a481f4d9SJohn Johansen error = entry_create_dir(fs_file, fs_dir->dentry); 23469acd494bSKees Cook else 2347a481f4d9SJohn Johansen error = entry_create_file(fs_file, fs_dir->dentry); 23489acd494bSKees Cook if (error) 23499acd494bSKees Cook goto failed; 23509acd494bSKees Cook } 23519acd494bSKees Cook 23529acd494bSKees Cook return 0; 23539acd494bSKees Cook 23549acd494bSKees Cook failed: 2355a481f4d9SJohn Johansen entry_remove_dir(fs_dir); 23560d259f04SJohn Johansen 23579acd494bSKees Cook return error; 23589acd494bSKees Cook } 23599acd494bSKees Cook 23609acd494bSKees Cook /** 2361c97204baSJohn Johansen * entry_remove_file - drop a single file entry in the apparmor securityfs 2362c97204baSJohn Johansen * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL) 23639acd494bSKees Cook */ 2364c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file) 23659acd494bSKees Cook { 23669acd494bSKees Cook if (!fs_file->dentry) 23679acd494bSKees Cook return; 23689acd494bSKees Cook 23699acd494bSKees Cook securityfs_remove(fs_file->dentry); 23709acd494bSKees Cook fs_file->dentry = NULL; 23719acd494bSKees Cook } 23729acd494bSKees Cook 23739acd494bSKees Cook /** 2374a481f4d9SJohn Johansen * entry_remove_dir - recursively drop a directory entry from the securityfs 2375c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL) 23769acd494bSKees Cook */ 2377c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir) 23789acd494bSKees Cook { 2379c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 23809acd494bSKees Cook 23810d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2382c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2383a481f4d9SJohn Johansen entry_remove_dir(fs_file); 23849acd494bSKees Cook else 2385c97204baSJohn Johansen entry_remove_file(fs_file); 23869acd494bSKees Cook } 23879acd494bSKees Cook 2388c97204baSJohn Johansen entry_remove_file(fs_dir); 238963e2b423SJohn Johansen } 239063e2b423SJohn Johansen 239163e2b423SJohn Johansen /** 239263e2b423SJohn Johansen * aa_destroy_aafs - cleanup and free aafs 239363e2b423SJohn Johansen * 239463e2b423SJohn Johansen * releases dentries allocated by aa_create_aafs 239563e2b423SJohn Johansen */ 239663e2b423SJohn Johansen void __init aa_destroy_aafs(void) 239763e2b423SJohn Johansen { 2398c97204baSJohn Johansen entry_remove_dir(&aa_sfs_entry); 239963e2b423SJohn Johansen } 240063e2b423SJohn Johansen 2401a71ada30SJohn Johansen 2402a71ada30SJohn Johansen #define NULL_FILE_NAME ".null" 2403a71ada30SJohn Johansen struct path aa_null; 2404a71ada30SJohn Johansen 2405a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent) 2406a71ada30SJohn Johansen { 2407a71ada30SJohn Johansen struct vfsmount *mount = NULL; 2408a71ada30SJohn Johansen struct dentry *dentry; 2409a71ada30SJohn Johansen struct inode *inode; 2410a71ada30SJohn Johansen int count = 0; 2411a71ada30SJohn Johansen int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); 2412a71ada30SJohn Johansen 2413a71ada30SJohn Johansen if (error) 2414a71ada30SJohn Johansen return error; 2415a71ada30SJohn Johansen 2416a71ada30SJohn Johansen inode_lock(d_inode(parent)); 2417a71ada30SJohn Johansen dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); 2418a71ada30SJohn Johansen if (IS_ERR(dentry)) { 2419a71ada30SJohn Johansen error = PTR_ERR(dentry); 2420a71ada30SJohn Johansen goto out; 2421a71ada30SJohn Johansen } 2422a71ada30SJohn Johansen inode = new_inode(parent->d_inode->i_sb); 2423a71ada30SJohn Johansen if (!inode) { 2424a71ada30SJohn Johansen error = -ENOMEM; 2425a71ada30SJohn Johansen goto out1; 2426a71ada30SJohn Johansen } 2427a71ada30SJohn Johansen 2428a71ada30SJohn Johansen inode->i_ino = get_next_ino(); 2429a71ada30SJohn Johansen inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; 243024d0d03cSDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2431a71ada30SJohn Johansen init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, 2432a71ada30SJohn Johansen MKDEV(MEM_MAJOR, 3)); 2433a71ada30SJohn Johansen d_instantiate(dentry, inode); 2434a71ada30SJohn Johansen aa_null.dentry = dget(dentry); 2435a71ada30SJohn Johansen aa_null.mnt = mntget(mount); 2436a71ada30SJohn Johansen 2437a71ada30SJohn Johansen error = 0; 2438a71ada30SJohn Johansen 2439a71ada30SJohn Johansen out1: 2440a71ada30SJohn Johansen dput(dentry); 2441a71ada30SJohn Johansen out: 2442a71ada30SJohn Johansen inode_unlock(d_inode(parent)); 2443a71ada30SJohn Johansen simple_release_fs(&mount, &count); 2444a71ada30SJohn Johansen return error; 2445a71ada30SJohn Johansen } 2446a71ada30SJohn Johansen 2447a481f4d9SJohn Johansen 2448a481f4d9SJohn Johansen 2449a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry, 2450a481f4d9SJohn Johansen struct inode *inode, 2451a481f4d9SJohn Johansen struct delayed_call *done) 2452a481f4d9SJohn Johansen { 2453a481f4d9SJohn Johansen struct aa_ns *ns; 2454a481f4d9SJohn Johansen struct path path; 2455a481f4d9SJohn Johansen 2456a481f4d9SJohn Johansen if (!dentry) 2457a481f4d9SJohn Johansen return ERR_PTR(-ECHILD); 2458a481f4d9SJohn Johansen ns = aa_get_current_ns(); 2459a481f4d9SJohn Johansen path.mnt = mntget(aafs_mnt); 2460a481f4d9SJohn Johansen path.dentry = dget(ns_dir(ns)); 2461a481f4d9SJohn Johansen nd_jump_link(&path); 2462a481f4d9SJohn Johansen aa_put_ns(ns); 2463a481f4d9SJohn Johansen 2464a481f4d9SJohn Johansen return NULL; 2465a481f4d9SJohn Johansen } 2466a481f4d9SJohn Johansen 2467a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer, 2468a481f4d9SJohn Johansen int buflen) 2469a481f4d9SJohn Johansen { 2470a481f4d9SJohn Johansen char name[32]; 2471a481f4d9SJohn Johansen int res; 2472a481f4d9SJohn Johansen 2473a0781209SJohn Johansen res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME, 2474a0781209SJohn Johansen d_inode(dentry)->i_ino); 2475a0781209SJohn Johansen if (res > 0 && res < sizeof(name)) 2476a481f4d9SJohn Johansen res = readlink_copy(buffer, buflen, name); 2477a0781209SJohn Johansen else 2478a0781209SJohn Johansen res = -ENOENT; 2479a481f4d9SJohn Johansen 2480a481f4d9SJohn Johansen return res; 2481a481f4d9SJohn Johansen } 2482a481f4d9SJohn Johansen 2483a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = { 2484a481f4d9SJohn Johansen .readlink = policy_readlink, 2485a481f4d9SJohn Johansen .get_link = policy_get_link, 2486a481f4d9SJohn Johansen }; 2487a481f4d9SJohn Johansen 2488a481f4d9SJohn Johansen 248963e2b423SJohn Johansen /** 249063e2b423SJohn Johansen * aa_create_aafs - create the apparmor security filesystem 249163e2b423SJohn Johansen * 249263e2b423SJohn Johansen * dentries created here are released by aa_destroy_aafs 249363e2b423SJohn Johansen * 249463e2b423SJohn Johansen * Returns: error on failure 249563e2b423SJohn Johansen */ 24963417d8d5SJames Morris static int __init aa_create_aafs(void) 249763e2b423SJohn Johansen { 2498b7fd2c03SJohn Johansen struct dentry *dent; 249963e2b423SJohn Johansen int error; 250063e2b423SJohn Johansen 250163e2b423SJohn Johansen if (!apparmor_initialized) 250263e2b423SJohn Johansen return 0; 250363e2b423SJohn Johansen 2504c97204baSJohn Johansen if (aa_sfs_entry.dentry) { 250563e2b423SJohn Johansen AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 250663e2b423SJohn Johansen return -EEXIST; 250763e2b423SJohn Johansen } 250863e2b423SJohn Johansen 2509a481f4d9SJohn Johansen /* setup apparmorfs used to virtualize policy/ */ 2510a481f4d9SJohn Johansen aafs_mnt = kern_mount(&aafs_ops); 2511a481f4d9SJohn Johansen if (IS_ERR(aafs_mnt)) 2512a481f4d9SJohn Johansen panic("can't set apparmorfs up\n"); 25131751e8a6SLinus Torvalds aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER; 2514a481f4d9SJohn Johansen 25159acd494bSKees Cook /* Populate fs tree. */ 2516c97204baSJohn Johansen error = entry_create_dir(&aa_sfs_entry, NULL); 251763e2b423SJohn Johansen if (error) 251863e2b423SJohn Johansen goto error; 251963e2b423SJohn Johansen 2520c97204baSJohn Johansen dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry, 2521b7fd2c03SJohn Johansen NULL, &aa_fs_profile_load); 2522cf916000SJohn Johansen if (IS_ERR(dent)) 2523cf916000SJohn Johansen goto dent_error; 2524b7fd2c03SJohn Johansen ns_subload(root_ns) = dent; 2525b7fd2c03SJohn Johansen 2526c97204baSJohn Johansen dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry, 2527b7fd2c03SJohn Johansen NULL, &aa_fs_profile_replace); 2528cf916000SJohn Johansen if (IS_ERR(dent)) 2529cf916000SJohn Johansen goto dent_error; 2530b7fd2c03SJohn Johansen ns_subreplace(root_ns) = dent; 2531b7fd2c03SJohn Johansen 2532c97204baSJohn Johansen dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry, 2533b7fd2c03SJohn Johansen NULL, &aa_fs_profile_remove); 2534cf916000SJohn Johansen if (IS_ERR(dent)) 2535cf916000SJohn Johansen goto dent_error; 2536b7fd2c03SJohn Johansen ns_subremove(root_ns) = dent; 2537b7fd2c03SJohn Johansen 2538d9bf2c26SJohn Johansen dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry, 2539d9bf2c26SJohn Johansen NULL, &aa_fs_ns_revision_fops); 2540cf916000SJohn Johansen if (IS_ERR(dent)) 2541cf916000SJohn Johansen goto dent_error; 2542d9bf2c26SJohn Johansen ns_subrevision(root_ns) = dent; 2543d9bf2c26SJohn Johansen 2544d9bf2c26SJohn Johansen /* policy tree referenced by magic policy symlink */ 2545feb3c766SJohn Johansen mutex_lock_nested(&root_ns->lock, root_ns->level); 2546c961ee5fSJohn Johansen error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy", 2547c961ee5fSJohn Johansen aafs_mnt->mnt_root); 2548b7fd2c03SJohn Johansen mutex_unlock(&root_ns->lock); 25490d259f04SJohn Johansen if (error) 25500d259f04SJohn Johansen goto error; 25510d259f04SJohn Johansen 2552c961ee5fSJohn Johansen /* magic symlink similar to nsfs redirects based on task policy */ 2553c961ee5fSJohn Johansen dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry, 2554c961ee5fSJohn Johansen NULL, &policy_link_iops); 2555cf916000SJohn Johansen if (IS_ERR(dent)) 2556cf916000SJohn Johansen goto dent_error; 2557c961ee5fSJohn Johansen 2558c97204baSJohn Johansen error = aa_mk_null_file(aa_sfs_entry.dentry); 2559a71ada30SJohn Johansen if (error) 2560a71ada30SJohn Johansen goto error; 2561a71ada30SJohn Johansen 2562a71ada30SJohn Johansen /* TODO: add default profile to apparmorfs */ 256363e2b423SJohn Johansen 256463e2b423SJohn Johansen /* Report that AppArmor fs is enabled */ 256563e2b423SJohn Johansen aa_info_message("AppArmor Filesystem Enabled"); 256663e2b423SJohn Johansen return 0; 256763e2b423SJohn Johansen 2568cf916000SJohn Johansen dent_error: 2569cf916000SJohn Johansen error = PTR_ERR(dent); 257063e2b423SJohn Johansen error: 257163e2b423SJohn Johansen aa_destroy_aafs(); 257263e2b423SJohn Johansen AA_ERROR("Error creating AppArmor securityfs\n"); 257363e2b423SJohn Johansen return error; 257463e2b423SJohn Johansen } 257563e2b423SJohn Johansen 257663e2b423SJohn Johansen fs_initcall(aa_create_aafs); 2577