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> 26a481f4d9SJohn Johansen #include <uapi/linux/major.h> 27a481f4d9SJohn Johansen #include <uapi/linux/magic.h> 2863e2b423SJohn Johansen 2963e2b423SJohn Johansen #include "include/apparmor.h" 3063e2b423SJohn Johansen #include "include/apparmorfs.h" 3163e2b423SJohn Johansen #include "include/audit.h" 3263e2b423SJohn Johansen #include "include/context.h" 33f8eb8a13SJohn Johansen #include "include/crypto.h" 3463e2b423SJohn Johansen #include "include/policy.h" 35cff281f6SJohn Johansen #include "include/policy_ns.h" 36d384b0a1SKees Cook #include "include/resource.h" 375ac8c355SJohn Johansen #include "include/policy_unpack.h" 3863e2b423SJohn Johansen 39c97204baSJohn Johansen /* 40c97204baSJohn Johansen * The apparmor filesystem interface used for policy load and introspection 41c97204baSJohn Johansen * The interface is split into two main components based on their function 42c97204baSJohn Johansen * a securityfs component: 43c97204baSJohn Johansen * used for static files that are always available, and which allows 44c97204baSJohn Johansen * userspace to specificy the location of the security filesystem. 45c97204baSJohn Johansen * 46c97204baSJohn Johansen * fns and data are prefixed with 47c97204baSJohn Johansen * aa_sfs_ 48c97204baSJohn Johansen * 49c97204baSJohn Johansen * an apparmorfs component: 50c97204baSJohn Johansen * used loaded policy content and introspection. It is not part of a 51c97204baSJohn Johansen * regular mounted filesystem and is available only through the magic 52c97204baSJohn Johansen * policy symlink in the root of the securityfs apparmor/ directory. 53c97204baSJohn Johansen * Tasks queries will be magically redirected to the correct portion 54c97204baSJohn Johansen * of the policy tree based on their confinement. 55c97204baSJohn Johansen * 56c97204baSJohn Johansen * fns and data are prefixed with 57c97204baSJohn Johansen * aafs_ 58c97204baSJohn Johansen * 59c97204baSJohn Johansen * The aa_fs_ prefix is used to indicate the fn is used by both the 60c97204baSJohn Johansen * securityfs and apparmorfs filesystems. 61c97204baSJohn Johansen */ 62c97204baSJohn Johansen 63c97204baSJohn Johansen 64c97204baSJohn Johansen /* 65c97204baSJohn Johansen * support fns 66c97204baSJohn Johansen */ 67c97204baSJohn Johansen 6863e2b423SJohn Johansen /** 690d259f04SJohn Johansen * aa_mangle_name - mangle a profile name to std profile layout form 700d259f04SJohn Johansen * @name: profile name to mangle (NOT NULL) 710d259f04SJohn Johansen * @target: buffer to store mangled name, same length as @name (MAYBE NULL) 720d259f04SJohn Johansen * 730d259f04SJohn Johansen * Returns: length of mangled name 740d259f04SJohn Johansen */ 75bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target) 760d259f04SJohn Johansen { 770d259f04SJohn Johansen char *t = target; 780d259f04SJohn Johansen 790d259f04SJohn Johansen while (*name == '/' || *name == '.') 800d259f04SJohn Johansen name++; 810d259f04SJohn Johansen 820d259f04SJohn Johansen if (target) { 830d259f04SJohn Johansen for (; *name; name++) { 840d259f04SJohn Johansen if (*name == '/') 850d259f04SJohn Johansen *(t)++ = '.'; 860d259f04SJohn Johansen else if (isspace(*name)) 870d259f04SJohn Johansen *(t)++ = '_'; 880d259f04SJohn Johansen else if (isalnum(*name) || strchr("._-", *name)) 890d259f04SJohn Johansen *(t)++ = *name; 900d259f04SJohn Johansen } 910d259f04SJohn Johansen 920d259f04SJohn Johansen *t = 0; 930d259f04SJohn Johansen } else { 940d259f04SJohn Johansen int len = 0; 950d259f04SJohn Johansen for (; *name; name++) { 960d259f04SJohn Johansen if (isalnum(*name) || isspace(*name) || 970d259f04SJohn Johansen strchr("/._-", *name)) 980d259f04SJohn Johansen len++; 990d259f04SJohn Johansen } 1000d259f04SJohn Johansen 1010d259f04SJohn Johansen return len; 1020d259f04SJohn Johansen } 1030d259f04SJohn Johansen 1040d259f04SJohn Johansen return t - target; 1050d259f04SJohn Johansen } 1060d259f04SJohn Johansen 107a481f4d9SJohn Johansen 108a481f4d9SJohn Johansen /* 109a481f4d9SJohn Johansen * aafs - core fns and data for the policy tree 110a481f4d9SJohn Johansen */ 111a481f4d9SJohn Johansen 112a481f4d9SJohn Johansen #define AAFS_NAME "apparmorfs" 113a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt; 114a481f4d9SJohn Johansen static int aafs_count; 115a481f4d9SJohn Johansen 116a481f4d9SJohn Johansen 117a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry) 118a481f4d9SJohn Johansen { 119a481f4d9SJohn Johansen struct inode *inode = d_inode(dentry); 120a481f4d9SJohn Johansen 121a481f4d9SJohn Johansen seq_printf(seq, "%s:[%lu]", AAFS_NAME, inode->i_ino); 122a481f4d9SJohn Johansen return 0; 123a481f4d9SJohn Johansen } 124a481f4d9SJohn Johansen 125a481f4d9SJohn Johansen static void aafs_evict_inode(struct inode *inode) 126a481f4d9SJohn Johansen { 127a481f4d9SJohn Johansen truncate_inode_pages_final(&inode->i_data); 128a481f4d9SJohn Johansen clear_inode(inode); 129a481f4d9SJohn Johansen if (S_ISLNK(inode->i_mode)) 130a481f4d9SJohn Johansen kfree(inode->i_link); 131a481f4d9SJohn Johansen } 132a481f4d9SJohn Johansen 133a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = { 134a481f4d9SJohn Johansen .statfs = simple_statfs, 135a481f4d9SJohn Johansen .evict_inode = aafs_evict_inode, 136a481f4d9SJohn Johansen .show_path = aafs_show_path, 137a481f4d9SJohn Johansen }; 138a481f4d9SJohn Johansen 139a481f4d9SJohn Johansen static int fill_super(struct super_block *sb, void *data, int silent) 140a481f4d9SJohn Johansen { 141a481f4d9SJohn Johansen static struct tree_descr files[] = { {""} }; 142a481f4d9SJohn Johansen int error; 143a481f4d9SJohn Johansen 144a481f4d9SJohn Johansen error = simple_fill_super(sb, AAFS_MAGIC, files); 145a481f4d9SJohn Johansen if (error) 146a481f4d9SJohn Johansen return error; 147a481f4d9SJohn Johansen sb->s_op = &aafs_super_ops; 148a481f4d9SJohn Johansen 149a481f4d9SJohn Johansen return 0; 150a481f4d9SJohn Johansen } 151a481f4d9SJohn Johansen 152a481f4d9SJohn Johansen static struct dentry *aafs_mount(struct file_system_type *fs_type, 153a481f4d9SJohn Johansen int flags, const char *dev_name, void *data) 154a481f4d9SJohn Johansen { 155a481f4d9SJohn Johansen return mount_single(fs_type, flags, data, fill_super); 156a481f4d9SJohn Johansen } 157a481f4d9SJohn Johansen 158a481f4d9SJohn Johansen static struct file_system_type aafs_ops = { 159a481f4d9SJohn Johansen .owner = THIS_MODULE, 160a481f4d9SJohn Johansen .name = AAFS_NAME, 161a481f4d9SJohn Johansen .mount = aafs_mount, 162a481f4d9SJohn Johansen .kill_sb = kill_anon_super, 163a481f4d9SJohn Johansen }; 164a481f4d9SJohn Johansen 165a481f4d9SJohn Johansen /** 166a481f4d9SJohn Johansen * __aafs_setup_d_inode - basic inode setup for apparmorfs 167a481f4d9SJohn Johansen * @dir: parent directory for the dentry 168a481f4d9SJohn Johansen * @dentry: dentry we are seting the inode up for 169a481f4d9SJohn Johansen * @mode: permissions the file should have 170a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 171a481f4d9SJohn Johansen * @link: if symlink, symlink target string 172a481f4d9SJohn Johansen * @fops: struct file_operations that should be used 173a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 174a481f4d9SJohn Johansen */ 175a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry, 176a481f4d9SJohn Johansen umode_t mode, void *data, char *link, 177a481f4d9SJohn Johansen const struct file_operations *fops, 178a481f4d9SJohn Johansen const struct inode_operations *iops) 179a481f4d9SJohn Johansen { 180a481f4d9SJohn Johansen struct inode *inode = new_inode(dir->i_sb); 181a481f4d9SJohn Johansen 182a481f4d9SJohn Johansen AA_BUG(!dir); 183a481f4d9SJohn Johansen AA_BUG(!dentry); 184a481f4d9SJohn Johansen 185a481f4d9SJohn Johansen if (!inode) 186a481f4d9SJohn Johansen return -ENOMEM; 187a481f4d9SJohn Johansen 188a481f4d9SJohn Johansen inode->i_ino = get_next_ino(); 189a481f4d9SJohn Johansen inode->i_mode = mode; 190a481f4d9SJohn Johansen inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 191a481f4d9SJohn Johansen inode->i_private = data; 192a481f4d9SJohn Johansen if (S_ISDIR(mode)) { 193a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_dir_inode_operations; 194a481f4d9SJohn Johansen inode->i_fop = &simple_dir_operations; 195a481f4d9SJohn Johansen inc_nlink(inode); 196a481f4d9SJohn Johansen inc_nlink(dir); 197a481f4d9SJohn Johansen } else if (S_ISLNK(mode)) { 198a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_symlink_inode_operations; 199a481f4d9SJohn Johansen inode->i_link = link; 200a481f4d9SJohn Johansen } else { 201a481f4d9SJohn Johansen inode->i_fop = fops; 202a481f4d9SJohn Johansen } 203a481f4d9SJohn Johansen d_instantiate(dentry, inode); 204a481f4d9SJohn Johansen dget(dentry); 205a481f4d9SJohn Johansen 206a481f4d9SJohn Johansen return 0; 207a481f4d9SJohn Johansen } 208a481f4d9SJohn Johansen 209a481f4d9SJohn Johansen /** 210a481f4d9SJohn Johansen * aafs_create - create a dentry in the apparmorfs filesystem 211a481f4d9SJohn Johansen * 212a481f4d9SJohn Johansen * @name: name of dentry to create 213a481f4d9SJohn Johansen * @mode: permissions the file should have 214a481f4d9SJohn Johansen * @parent: parent directory for this dentry 215a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 216a481f4d9SJohn Johansen * @link: if symlink, symlink target string 217a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 218a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 219a481f4d9SJohn Johansen * 220a481f4d9SJohn Johansen * This is the basic "create a xxx" function for apparmorfs. 221a481f4d9SJohn Johansen * 222a481f4d9SJohn Johansen * Returns a pointer to a dentry if it succeeds, that must be free with 223a481f4d9SJohn Johansen * aafs_remove(). Will return ERR_PTR on failure. 224a481f4d9SJohn Johansen */ 225a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode, 226a481f4d9SJohn Johansen struct dentry *parent, void *data, void *link, 227a481f4d9SJohn Johansen const struct file_operations *fops, 228a481f4d9SJohn Johansen const struct inode_operations *iops) 229a481f4d9SJohn Johansen { 230a481f4d9SJohn Johansen struct dentry *dentry; 231a481f4d9SJohn Johansen struct inode *dir; 232a481f4d9SJohn Johansen int error; 233a481f4d9SJohn Johansen 234a481f4d9SJohn Johansen AA_BUG(!name); 235a481f4d9SJohn Johansen AA_BUG(!parent); 236a481f4d9SJohn Johansen 237a481f4d9SJohn Johansen if (!(mode & S_IFMT)) 238a481f4d9SJohn Johansen mode = (mode & S_IALLUGO) | S_IFREG; 239a481f4d9SJohn Johansen 240a481f4d9SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 241a481f4d9SJohn Johansen if (error) 242a481f4d9SJohn Johansen return ERR_PTR(error); 243a481f4d9SJohn Johansen 244a481f4d9SJohn Johansen dir = d_inode(parent); 245a481f4d9SJohn Johansen 246a481f4d9SJohn Johansen inode_lock(dir); 247a481f4d9SJohn Johansen dentry = lookup_one_len(name, parent, strlen(name)); 248a481f4d9SJohn Johansen if (IS_ERR(dentry)) 249a481f4d9SJohn Johansen goto fail_lock; 250a481f4d9SJohn Johansen 251a481f4d9SJohn Johansen if (d_really_is_positive(dentry)) { 252a481f4d9SJohn Johansen error = -EEXIST; 253a481f4d9SJohn Johansen goto fail_dentry; 254a481f4d9SJohn Johansen } 255a481f4d9SJohn Johansen 256a481f4d9SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops); 257a481f4d9SJohn Johansen if (error) 258a481f4d9SJohn Johansen goto fail_dentry; 259a481f4d9SJohn Johansen inode_unlock(dir); 260a481f4d9SJohn Johansen 261a481f4d9SJohn Johansen return dentry; 262a481f4d9SJohn Johansen 263a481f4d9SJohn Johansen fail_dentry: 264a481f4d9SJohn Johansen dput(dentry); 265a481f4d9SJohn Johansen 266a481f4d9SJohn Johansen fail_lock: 267a481f4d9SJohn Johansen inode_unlock(dir); 268a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 269a481f4d9SJohn Johansen 270a481f4d9SJohn Johansen return ERR_PTR(error); 271a481f4d9SJohn Johansen } 272a481f4d9SJohn Johansen 273a481f4d9SJohn Johansen /** 274a481f4d9SJohn Johansen * aafs_create_file - create a file in the apparmorfs filesystem 275a481f4d9SJohn Johansen * 276a481f4d9SJohn Johansen * @name: name of dentry to create 277a481f4d9SJohn Johansen * @mode: permissions the file should have 278a481f4d9SJohn Johansen * @parent: parent directory for this dentry 279a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 280a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 281a481f4d9SJohn Johansen * 282a481f4d9SJohn Johansen * see aafs_create 283a481f4d9SJohn Johansen */ 284a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode, 285a481f4d9SJohn Johansen struct dentry *parent, void *data, 286a481f4d9SJohn Johansen const struct file_operations *fops) 287a481f4d9SJohn Johansen { 288a481f4d9SJohn Johansen return aafs_create(name, mode, parent, data, NULL, fops, NULL); 289a481f4d9SJohn Johansen } 290a481f4d9SJohn Johansen 291a481f4d9SJohn Johansen /** 292a481f4d9SJohn Johansen * aafs_create_dir - create a directory in the apparmorfs filesystem 293a481f4d9SJohn Johansen * 294a481f4d9SJohn Johansen * @name: name of dentry to create 295a481f4d9SJohn Johansen * @parent: parent directory for this dentry 296a481f4d9SJohn Johansen * 297a481f4d9SJohn Johansen * see aafs_create 298a481f4d9SJohn Johansen */ 299a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent) 300a481f4d9SJohn Johansen { 301a481f4d9SJohn Johansen return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL, 302a481f4d9SJohn Johansen NULL); 303a481f4d9SJohn Johansen } 304a481f4d9SJohn Johansen 305a481f4d9SJohn Johansen /** 306a481f4d9SJohn Johansen * aafs_create_symlink - create a symlink in the apparmorfs filesystem 307a481f4d9SJohn Johansen * @name: name of dentry to create 308a481f4d9SJohn Johansen * @parent: parent directory for this dentry 309a481f4d9SJohn Johansen * @target: if symlink, symlink target string 310a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 311a481f4d9SJohn Johansen * 312a481f4d9SJohn Johansen * If @target parameter is %NULL, then the @iops parameter needs to be 313a481f4d9SJohn Johansen * setup to handle .readlink and .get_link inode_operations. 314a481f4d9SJohn Johansen */ 315a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name, 316a481f4d9SJohn Johansen struct dentry *parent, 317a481f4d9SJohn Johansen const char *target, 318a481f4d9SJohn Johansen const struct inode_operations *iops) 319a481f4d9SJohn Johansen { 320a481f4d9SJohn Johansen struct dentry *dent; 321a481f4d9SJohn Johansen char *link = NULL; 322a481f4d9SJohn Johansen 323a481f4d9SJohn Johansen if (target) { 324a481f4d9SJohn Johansen link = kstrdup(target, GFP_KERNEL); 325a481f4d9SJohn Johansen if (!link) 326a481f4d9SJohn Johansen return ERR_PTR(-ENOMEM); 327a481f4d9SJohn Johansen } 328a481f4d9SJohn Johansen dent = aafs_create(name, S_IFLNK | 0444, parent, NULL, link, NULL, 329a481f4d9SJohn Johansen iops); 330a481f4d9SJohn Johansen if (IS_ERR(dent)) 331a481f4d9SJohn Johansen kfree(link); 332a481f4d9SJohn Johansen 333a481f4d9SJohn Johansen return dent; 334a481f4d9SJohn Johansen } 335a481f4d9SJohn Johansen 336a481f4d9SJohn Johansen /** 337a481f4d9SJohn Johansen * aafs_remove - removes a file or directory from the apparmorfs filesystem 338a481f4d9SJohn Johansen * 339a481f4d9SJohn Johansen * @dentry: dentry of the file/directory/symlink to removed. 340a481f4d9SJohn Johansen */ 341a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry) 342a481f4d9SJohn Johansen { 343a481f4d9SJohn Johansen struct inode *dir; 344a481f4d9SJohn Johansen 345a481f4d9SJohn Johansen if (!dentry || IS_ERR(dentry)) 346a481f4d9SJohn Johansen return; 347a481f4d9SJohn Johansen 348a481f4d9SJohn Johansen dir = d_inode(dentry->d_parent); 349a481f4d9SJohn Johansen inode_lock(dir); 350a481f4d9SJohn Johansen if (simple_positive(dentry)) { 351a481f4d9SJohn Johansen if (d_is_dir(dentry)) 352a481f4d9SJohn Johansen simple_rmdir(dir, dentry); 353a481f4d9SJohn Johansen else 354a481f4d9SJohn Johansen simple_unlink(dir, dentry); 355a481f4d9SJohn Johansen dput(dentry); 356a481f4d9SJohn Johansen } 357a481f4d9SJohn Johansen inode_unlock(dir); 358a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 359a481f4d9SJohn Johansen } 360a481f4d9SJohn Johansen 361a481f4d9SJohn Johansen 362a481f4d9SJohn Johansen /* 363a481f4d9SJohn Johansen * aa_fs - policy load/replace/remove 364a481f4d9SJohn Johansen */ 365a481f4d9SJohn Johansen 3660d259f04SJohn Johansen /** 36763e2b423SJohn Johansen * aa_simple_write_to_buffer - common routine for getting policy from user 36863e2b423SJohn Johansen * @userbuf: user buffer to copy data from (NOT NULL) 3693ed02adaSJohn Johansen * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 37063e2b423SJohn Johansen * @copy_size: size of data to copy from user buffer 37163e2b423SJohn Johansen * @pos: position write is at in the file (NOT NULL) 37263e2b423SJohn Johansen * 37363e2b423SJohn Johansen * Returns: kernel buffer containing copy of user buffer data or an 37463e2b423SJohn Johansen * ERR_PTR on failure. 37563e2b423SJohn Johansen */ 3765ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, 3775ac8c355SJohn Johansen size_t alloc_size, 3785ac8c355SJohn Johansen size_t copy_size, 37963e2b423SJohn Johansen loff_t *pos) 38063e2b423SJohn Johansen { 3815ac8c355SJohn Johansen struct aa_loaddata *data; 38263e2b423SJohn Johansen 383e6bfa25dSJohn Johansen AA_BUG(copy_size > alloc_size); 3843ed02adaSJohn Johansen 38563e2b423SJohn Johansen if (*pos != 0) 38663e2b423SJohn Johansen /* only writes from pos 0, that is complete writes */ 38763e2b423SJohn Johansen return ERR_PTR(-ESPIPE); 38863e2b423SJohn Johansen 38963e2b423SJohn Johansen /* freed by caller to simple_write_to_buffer */ 3905d5182caSJohn Johansen data = aa_loaddata_alloc(alloc_size); 3915d5182caSJohn Johansen if (IS_ERR(data)) 3925d5182caSJohn Johansen return data; 39363e2b423SJohn Johansen 3945d5182caSJohn Johansen data->size = copy_size; 3955ac8c355SJohn Johansen if (copy_from_user(data->data, userbuf, copy_size)) { 39663e2b423SJohn Johansen kvfree(data); 39763e2b423SJohn Johansen return ERR_PTR(-EFAULT); 39863e2b423SJohn Johansen } 39963e2b423SJohn Johansen 40063e2b423SJohn Johansen return data; 40163e2b423SJohn Johansen } 40263e2b423SJohn Johansen 4035ac8c355SJohn Johansen static ssize_t policy_update(int binop, const char __user *buf, size_t size, 404b7fd2c03SJohn Johansen loff_t *pos, struct aa_ns *ns) 4055ac8c355SJohn Johansen { 4065ac8c355SJohn Johansen ssize_t error; 4075ac8c355SJohn Johansen struct aa_loaddata *data; 4085ac8c355SJohn Johansen struct aa_profile *profile = aa_current_profile(); 40947f6e5ccSJohn Johansen const char *op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL; 4105ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4115ac8c355SJohn Johansen * below after unpack 4125ac8c355SJohn Johansen */ 413b7fd2c03SJohn Johansen error = aa_may_manage_policy(profile, ns, op); 4145ac8c355SJohn Johansen if (error) 4155ac8c355SJohn Johansen return error; 41663e2b423SJohn Johansen 4175ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size, size, pos); 4185ac8c355SJohn Johansen error = PTR_ERR(data); 4195ac8c355SJohn Johansen if (!IS_ERR(data)) { 420b7fd2c03SJohn Johansen error = aa_replace_profiles(ns ? ns : profile->ns, profile, 421b7fd2c03SJohn Johansen binop, data); 4225ac8c355SJohn Johansen aa_put_loaddata(data); 4235ac8c355SJohn Johansen } 4245ac8c355SJohn Johansen 4255ac8c355SJohn Johansen return error; 4265ac8c355SJohn Johansen } 4275ac8c355SJohn Johansen 428b7fd2c03SJohn Johansen /* .load file hook fn to load policy */ 42963e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 43063e2b423SJohn Johansen loff_t *pos) 43163e2b423SJohn Johansen { 432b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 433b7fd2c03SJohn Johansen int error = policy_update(PROF_ADD, buf, size, pos, ns); 434b7fd2c03SJohn Johansen 435b7fd2c03SJohn Johansen aa_put_ns(ns); 43663e2b423SJohn Johansen 43763e2b423SJohn Johansen return error; 43863e2b423SJohn Johansen } 43963e2b423SJohn Johansen 44063e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = { 4416038f373SArnd Bergmann .write = profile_load, 4426038f373SArnd Bergmann .llseek = default_llseek, 44363e2b423SJohn Johansen }; 44463e2b423SJohn Johansen 44563e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */ 44663e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf, 44763e2b423SJohn Johansen size_t size, loff_t *pos) 44863e2b423SJohn Johansen { 449b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 450b7fd2c03SJohn Johansen int error = policy_update(PROF_REPLACE, buf, size, pos, ns); 451b7fd2c03SJohn Johansen 452b7fd2c03SJohn Johansen aa_put_ns(ns); 45363e2b423SJohn Johansen 45463e2b423SJohn Johansen return error; 45563e2b423SJohn Johansen } 45663e2b423SJohn Johansen 45763e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = { 4586038f373SArnd Bergmann .write = profile_replace, 4596038f373SArnd Bergmann .llseek = default_llseek, 46063e2b423SJohn Johansen }; 46163e2b423SJohn Johansen 462b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */ 46363e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf, 46463e2b423SJohn Johansen size_t size, loff_t *pos) 46563e2b423SJohn Johansen { 4665ac8c355SJohn Johansen struct aa_loaddata *data; 4675ac8c355SJohn Johansen struct aa_profile *profile; 46863e2b423SJohn Johansen ssize_t error; 469b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 47063e2b423SJohn Johansen 4715ac8c355SJohn Johansen profile = aa_current_profile(); 4725ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4735ac8c355SJohn Johansen * below after unpack 4745ac8c355SJohn Johansen */ 475b7fd2c03SJohn Johansen error = aa_may_manage_policy(profile, ns, OP_PROF_RM); 4765ac8c355SJohn Johansen if (error) 4775ac8c355SJohn Johansen goto out; 4785ac8c355SJohn Johansen 47963e2b423SJohn Johansen /* 48063e2b423SJohn Johansen * aa_remove_profile needs a null terminated string so 1 extra 48163e2b423SJohn Johansen * byte is allocated and the copied data is null terminated. 48263e2b423SJohn Johansen */ 4835ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size + 1, size, pos); 48463e2b423SJohn Johansen 48563e2b423SJohn Johansen error = PTR_ERR(data); 48663e2b423SJohn Johansen if (!IS_ERR(data)) { 4875ac8c355SJohn Johansen data->data[size] = 0; 488b7fd2c03SJohn Johansen error = aa_remove_profiles(ns ? ns : profile->ns, profile, 489b7fd2c03SJohn Johansen data->data, size); 4905ac8c355SJohn Johansen aa_put_loaddata(data); 49163e2b423SJohn Johansen } 4925ac8c355SJohn Johansen out: 493b7fd2c03SJohn Johansen aa_put_ns(ns); 49463e2b423SJohn Johansen return error; 49563e2b423SJohn Johansen } 49663e2b423SJohn Johansen 49763e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = { 4986038f373SArnd Bergmann .write = profile_remove, 4996038f373SArnd Bergmann .llseek = default_llseek, 50063e2b423SJohn Johansen }; 50163e2b423SJohn Johansen 5025d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns) 5035d5182caSJohn Johansen { 5045d5182caSJohn Johansen ns->revision++; 5055d5182caSJohn Johansen } 5065d5182caSJohn Johansen 507e025be0fSWilliam Hua /** 508e025be0fSWilliam Hua * query_data - queries a policy and writes its data to buf 509e025be0fSWilliam Hua * @buf: the resulting data is stored here (NOT NULL) 510e025be0fSWilliam Hua * @buf_len: size of buf 511e025be0fSWilliam Hua * @query: query string used to retrieve data 512e025be0fSWilliam Hua * @query_len: size of query including second NUL byte 513e025be0fSWilliam Hua * 514e025be0fSWilliam Hua * The buffers pointed to by buf and query may overlap. The query buffer is 515e025be0fSWilliam Hua * parsed before buf is written to. 516e025be0fSWilliam Hua * 517e025be0fSWilliam Hua * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of 518e025be0fSWilliam Hua * the security confinement context and <KEY> is the name of the data to 519e025be0fSWilliam Hua * retrieve. <LABEL> and <KEY> must not be NUL-terminated. 520e025be0fSWilliam Hua * 521e025be0fSWilliam Hua * Don't expect the contents of buf to be preserved on failure. 522e025be0fSWilliam Hua * 523e025be0fSWilliam Hua * Returns: number of characters written to buf or -errno on failure 524e025be0fSWilliam Hua */ 525e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len, 526e025be0fSWilliam Hua char *query, size_t query_len) 527e025be0fSWilliam Hua { 528e025be0fSWilliam Hua char *out; 529e025be0fSWilliam Hua const char *key; 530e025be0fSWilliam Hua struct aa_profile *profile; 531e025be0fSWilliam Hua struct aa_data *data; 532e025be0fSWilliam Hua u32 bytes, blocks; 533e025be0fSWilliam Hua __le32 outle32; 534e025be0fSWilliam Hua 535e025be0fSWilliam Hua if (!query_len) 536e025be0fSWilliam Hua return -EINVAL; /* need a query */ 537e025be0fSWilliam Hua 538e025be0fSWilliam Hua key = query + strnlen(query, query_len) + 1; 539e025be0fSWilliam Hua if (key + 1 >= query + query_len) 540e025be0fSWilliam Hua return -EINVAL; /* not enough space for a non-empty key */ 541e025be0fSWilliam Hua if (key + strnlen(key, query + query_len - key) >= query + query_len) 542e025be0fSWilliam Hua return -EINVAL; /* must end with NUL */ 543e025be0fSWilliam Hua 544e025be0fSWilliam Hua if (buf_len < sizeof(bytes) + sizeof(blocks)) 545e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 546e025be0fSWilliam Hua 547e025be0fSWilliam Hua profile = aa_current_profile(); 548e025be0fSWilliam Hua 549e025be0fSWilliam Hua /* We are going to leave space for two numbers. The first is the total 550e025be0fSWilliam Hua * number of bytes we are writing after the first number. This is so 551e025be0fSWilliam Hua * users can read the full output without reallocation. 552e025be0fSWilliam Hua * 553e025be0fSWilliam Hua * The second number is the number of data blocks we're writing. An 554e025be0fSWilliam Hua * application might be confined by multiple policies having data in 555e025be0fSWilliam Hua * the same key. 556e025be0fSWilliam Hua */ 557e025be0fSWilliam Hua memset(buf, 0, sizeof(bytes) + sizeof(blocks)); 558e025be0fSWilliam Hua out = buf + sizeof(bytes) + sizeof(blocks); 559e025be0fSWilliam Hua 560e025be0fSWilliam Hua blocks = 0; 561e025be0fSWilliam Hua if (profile->data) { 562e025be0fSWilliam Hua data = rhashtable_lookup_fast(profile->data, &key, 563e025be0fSWilliam Hua profile->data->p); 564e025be0fSWilliam Hua 565e025be0fSWilliam Hua if (data) { 566e025be0fSWilliam Hua if (out + sizeof(outle32) + data->size > buf + buf_len) 567e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 568e025be0fSWilliam Hua outle32 = __cpu_to_le32(data->size); 569e025be0fSWilliam Hua memcpy(out, &outle32, sizeof(outle32)); 570e025be0fSWilliam Hua out += sizeof(outle32); 571e025be0fSWilliam Hua memcpy(out, data->data, data->size); 572e025be0fSWilliam Hua out += data->size; 573e025be0fSWilliam Hua blocks++; 574e025be0fSWilliam Hua } 575e025be0fSWilliam Hua } 576e025be0fSWilliam Hua 577e025be0fSWilliam Hua outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); 578e025be0fSWilliam Hua memcpy(buf, &outle32, sizeof(outle32)); 579e025be0fSWilliam Hua outle32 = __cpu_to_le32(blocks); 580e025be0fSWilliam Hua memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32)); 581e025be0fSWilliam Hua 582e025be0fSWilliam Hua return out - buf; 583e025be0fSWilliam Hua } 584e025be0fSWilliam Hua 585e025be0fSWilliam Hua #define QUERY_CMD_DATA "data\0" 586e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN 5 587e025be0fSWilliam Hua 588e025be0fSWilliam Hua /** 589e025be0fSWilliam Hua * aa_write_access - generic permissions and data query 590e025be0fSWilliam Hua * @file: pointer to open apparmorfs/access file 591e025be0fSWilliam Hua * @ubuf: user buffer containing the complete query string (NOT NULL) 592e025be0fSWilliam Hua * @count: size of ubuf 593e025be0fSWilliam Hua * @ppos: position in the file (MUST BE ZERO) 594e025be0fSWilliam Hua * 595e025be0fSWilliam Hua * Allows for one permissions or data query per open(), write(), and read() 596e025be0fSWilliam Hua * sequence. The only queries currently supported are label-based queries for 597e025be0fSWilliam Hua * permissions or data. 598e025be0fSWilliam Hua * 599e025be0fSWilliam Hua * For permissions queries, ubuf must begin with "label\0", followed by the 600e025be0fSWilliam Hua * profile query specific format described in the query_label() function 601e025be0fSWilliam Hua * documentation. 602e025be0fSWilliam Hua * 603e025be0fSWilliam Hua * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where 604e025be0fSWilliam Hua * <LABEL> is the name of the security confinement context and <KEY> is the 605e025be0fSWilliam Hua * name of the data to retrieve. 606e025be0fSWilliam Hua * 607e025be0fSWilliam Hua * Returns: number of bytes written or -errno on failure 608e025be0fSWilliam Hua */ 609e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf, 610e025be0fSWilliam Hua size_t count, loff_t *ppos) 611e025be0fSWilliam Hua { 612e025be0fSWilliam Hua char *buf; 613e025be0fSWilliam Hua ssize_t len; 614e025be0fSWilliam Hua 615e025be0fSWilliam Hua if (*ppos) 616e025be0fSWilliam Hua return -ESPIPE; 617e025be0fSWilliam Hua 618e025be0fSWilliam Hua buf = simple_transaction_get(file, ubuf, count); 619e025be0fSWilliam Hua if (IS_ERR(buf)) 620e025be0fSWilliam Hua return PTR_ERR(buf); 621e025be0fSWilliam Hua 622e025be0fSWilliam Hua if (count > QUERY_CMD_DATA_LEN && 623e025be0fSWilliam Hua !memcmp(buf, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { 624e025be0fSWilliam Hua len = query_data(buf, SIMPLE_TRANSACTION_LIMIT, 625e025be0fSWilliam Hua buf + QUERY_CMD_DATA_LEN, 626e025be0fSWilliam Hua count - QUERY_CMD_DATA_LEN); 627e025be0fSWilliam Hua } else 628e025be0fSWilliam Hua len = -EINVAL; 629e025be0fSWilliam Hua 630e025be0fSWilliam Hua if (len < 0) 631e025be0fSWilliam Hua return len; 632e025be0fSWilliam Hua 633e025be0fSWilliam Hua simple_transaction_set(file, len); 634e025be0fSWilliam Hua 635e025be0fSWilliam Hua return count; 636e025be0fSWilliam Hua } 637e025be0fSWilliam Hua 638c97204baSJohn Johansen static const struct file_operations aa_sfs_access = { 639e025be0fSWilliam Hua .write = aa_write_access, 640e025be0fSWilliam Hua .read = simple_transaction_read, 641e025be0fSWilliam Hua .release = simple_transaction_release, 642e025be0fSWilliam Hua .llseek = generic_file_llseek, 643e025be0fSWilliam Hua }; 644e025be0fSWilliam Hua 645c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v) 646e74abcf3SKees Cook { 647c97204baSJohn Johansen struct aa_sfs_entry *fs_file = seq->private; 648e74abcf3SKees Cook 649e74abcf3SKees Cook if (!fs_file) 650e74abcf3SKees Cook return 0; 651e74abcf3SKees Cook 652e74abcf3SKees Cook switch (fs_file->v_type) { 653c97204baSJohn Johansen case AA_SFS_TYPE_BOOLEAN: 654e74abcf3SKees Cook seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); 655e74abcf3SKees Cook break; 656c97204baSJohn Johansen case AA_SFS_TYPE_STRING: 657a9bf8e9fSKees Cook seq_printf(seq, "%s\n", fs_file->v.string); 658a9bf8e9fSKees Cook break; 659c97204baSJohn Johansen case AA_SFS_TYPE_U64: 660e74abcf3SKees Cook seq_printf(seq, "%#08lx\n", fs_file->v.u64); 661e74abcf3SKees Cook break; 662e74abcf3SKees Cook default: 663e74abcf3SKees Cook /* Ignore unpritable entry types. */ 664e74abcf3SKees Cook break; 665e74abcf3SKees Cook } 666e74abcf3SKees Cook 667e74abcf3SKees Cook return 0; 668e74abcf3SKees Cook } 669e74abcf3SKees Cook 670c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file) 671e74abcf3SKees Cook { 672c97204baSJohn Johansen return single_open(file, aa_sfs_seq_show, inode->i_private); 673e74abcf3SKees Cook } 674e74abcf3SKees Cook 675c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = { 676e74abcf3SKees Cook .owner = THIS_MODULE, 677c97204baSJohn Johansen .open = aa_sfs_seq_open, 678e74abcf3SKees Cook .read = seq_read, 679e74abcf3SKees Cook .llseek = seq_lseek, 680e74abcf3SKees Cook .release = single_release, 681e74abcf3SKees Cook }; 682e74abcf3SKees Cook 68352b97de3SJohn Johansen /* 68452b97de3SJohn Johansen * profile based file operations 68552b97de3SJohn Johansen * policy/profiles/XXXX/profiles/ * 68652b97de3SJohn Johansen */ 68752b97de3SJohn Johansen 68852b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME) \ 68952b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\ 69052b97de3SJohn Johansen { \ 69152b97de3SJohn Johansen return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \ 69252b97de3SJohn Johansen } \ 69352b97de3SJohn Johansen \ 69452b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = { \ 69552b97de3SJohn Johansen .owner = THIS_MODULE, \ 69652b97de3SJohn Johansen .open = seq_profile_ ##NAME ##_open, \ 69752b97de3SJohn Johansen .read = seq_read, \ 69852b97de3SJohn Johansen .llseek = seq_lseek, \ 69952b97de3SJohn Johansen .release = seq_profile_release, \ 70052b97de3SJohn Johansen } \ 70152b97de3SJohn Johansen 70252b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file, 7030d259f04SJohn Johansen int (*show)(struct seq_file *, void *)) 7040d259f04SJohn Johansen { 7058399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(inode->i_private); 7068399588aSJohn Johansen int error = single_open(file, show, proxy); 70763e2b423SJohn Johansen 7080d259f04SJohn Johansen if (error) { 7090d259f04SJohn Johansen file->private_data = NULL; 7108399588aSJohn Johansen aa_put_proxy(proxy); 7110d259f04SJohn Johansen } 7120d259f04SJohn Johansen 7130d259f04SJohn Johansen return error; 7140d259f04SJohn Johansen } 7150d259f04SJohn Johansen 71652b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file) 7170d259f04SJohn Johansen { 7180d259f04SJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 7190d259f04SJohn Johansen if (seq) 7208399588aSJohn Johansen aa_put_proxy(seq->private); 7210d259f04SJohn Johansen return single_release(inode, file); 7220d259f04SJohn Johansen } 7230d259f04SJohn Johansen 72452b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v) 7250d259f04SJohn Johansen { 7268399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 7278399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 7280d259f04SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 7290d259f04SJohn Johansen aa_put_profile(profile); 7300d259f04SJohn Johansen 7310d259f04SJohn Johansen return 0; 7320d259f04SJohn Johansen } 7330d259f04SJohn Johansen 73452b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v) 7350d259f04SJohn Johansen { 7368399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 7378399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 7380d259f04SJohn Johansen seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); 7390d259f04SJohn Johansen aa_put_profile(profile); 7400d259f04SJohn Johansen 7410d259f04SJohn Johansen return 0; 7420d259f04SJohn Johansen } 7430d259f04SJohn Johansen 74452b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v) 745556d0be7SJohn Johansen { 7468399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 7478399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 748556d0be7SJohn Johansen if (profile->attach) 749556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->attach); 750556d0be7SJohn Johansen else if (profile->xmatch) 751556d0be7SJohn Johansen seq_puts(seq, "<unknown>\n"); 752556d0be7SJohn Johansen else 753556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 754556d0be7SJohn Johansen aa_put_profile(profile); 755556d0be7SJohn Johansen 756556d0be7SJohn Johansen return 0; 757556d0be7SJohn Johansen } 758556d0be7SJohn Johansen 75952b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v) 760f8eb8a13SJohn Johansen { 7618399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 7628399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 763f8eb8a13SJohn Johansen unsigned int i, size = aa_hash_size(); 764f8eb8a13SJohn Johansen 765f8eb8a13SJohn Johansen if (profile->hash) { 766f8eb8a13SJohn Johansen for (i = 0; i < size; i++) 767f8eb8a13SJohn Johansen seq_printf(seq, "%.2x", profile->hash[i]); 76847dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 769f8eb8a13SJohn Johansen } 7700b938a2eSJohn Johansen aa_put_profile(profile); 771f8eb8a13SJohn Johansen 772f8eb8a13SJohn Johansen return 0; 773f8eb8a13SJohn Johansen } 774f8eb8a13SJohn Johansen 77552b97de3SJohn Johansen SEQ_PROFILE_FOPS(name); 77652b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode); 77752b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach); 77852b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash); 779f8eb8a13SJohn Johansen 78064c86970SJohn Johansen /* 78164c86970SJohn Johansen * namespace based files 78264c86970SJohn Johansen * several root files and 78364c86970SJohn Johansen * policy/ * 78464c86970SJohn Johansen */ 785f8eb8a13SJohn Johansen 78664c86970SJohn Johansen #define SEQ_NS_FOPS(NAME) \ 78764c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \ 78864c86970SJohn Johansen { \ 78964c86970SJohn Johansen return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \ 79064c86970SJohn Johansen } \ 79164c86970SJohn Johansen \ 79264c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = { \ 79364c86970SJohn Johansen .owner = THIS_MODULE, \ 79464c86970SJohn Johansen .open = seq_ns_ ##NAME ##_open, \ 79564c86970SJohn Johansen .read = seq_read, \ 79664c86970SJohn Johansen .llseek = seq_lseek, \ 79764c86970SJohn Johansen .release = single_release, \ 79864c86970SJohn Johansen } \ 7993e3e5695SJohn Johansen 80064c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v) 801a71ada30SJohn Johansen { 802a71ada30SJohn Johansen struct aa_ns *ns = aa_current_profile()->ns; 803a71ada30SJohn Johansen 804a71ada30SJohn Johansen seq_printf(seq, "%d\n", ns->level); 805a71ada30SJohn Johansen 806a71ada30SJohn Johansen return 0; 807a71ada30SJohn Johansen } 808a71ada30SJohn Johansen 80964c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v) 8103e3e5695SJohn Johansen { 8113e3e5695SJohn Johansen struct aa_ns *ns = aa_current_profile()->ns; 8123e3e5695SJohn Johansen 8133e3e5695SJohn Johansen seq_printf(seq, "%s\n", ns->base.name); 8143e3e5695SJohn Johansen 8153e3e5695SJohn Johansen return 0; 8163e3e5695SJohn Johansen } 8173e3e5695SJohn Johansen 81864c86970SJohn Johansen SEQ_NS_FOPS(level); 81964c86970SJohn Johansen SEQ_NS_FOPS(name); 8203e3e5695SJohn Johansen 8215d5182caSJohn Johansen 8225d5182caSJohn Johansen /* policy/raw_data/ * file ops */ 8235d5182caSJohn Johansen 8245d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME) \ 8255d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\ 8265d5182caSJohn Johansen { \ 8275d5182caSJohn Johansen return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \ 8285d5182caSJohn Johansen } \ 8295d5182caSJohn Johansen \ 8305d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \ 8315d5182caSJohn Johansen .owner = THIS_MODULE, \ 8325d5182caSJohn Johansen .open = seq_rawdata_ ##NAME ##_open, \ 8335d5182caSJohn Johansen .read = seq_read, \ 8345d5182caSJohn Johansen .llseek = seq_lseek, \ 8355d5182caSJohn Johansen .release = seq_rawdata_release, \ 8365d5182caSJohn Johansen } \ 8375d5182caSJohn Johansen 8385d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file, 8395d5182caSJohn Johansen int (*show)(struct seq_file *, void *)) 8405ac8c355SJohn Johansen { 8415d5182caSJohn Johansen struct aa_loaddata *data = __aa_get_loaddata(inode->i_private); 8425d5182caSJohn Johansen int error; 8435d5182caSJohn Johansen 8445d5182caSJohn Johansen if (!data) 8455d5182caSJohn Johansen /* lost race this ent is being reaped */ 8465d5182caSJohn Johansen return -ENOENT; 8475d5182caSJohn Johansen 8485d5182caSJohn Johansen error = single_open(file, show, data); 8495d5182caSJohn Johansen if (error) { 8505d5182caSJohn Johansen AA_BUG(file->private_data && 8515d5182caSJohn Johansen ((struct seq_file *)file->private_data)->private); 8525d5182caSJohn Johansen aa_put_loaddata(data); 8535d5182caSJohn Johansen } 8545d5182caSJohn Johansen 8555d5182caSJohn Johansen return error; 8565d5182caSJohn Johansen } 8575d5182caSJohn Johansen 8585d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file) 8595d5182caSJohn Johansen { 8605d5182caSJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 8615d5182caSJohn Johansen 8625d5182caSJohn Johansen if (seq) 8635d5182caSJohn Johansen aa_put_loaddata(seq->private); 8645d5182caSJohn Johansen 8655d5182caSJohn Johansen return single_release(inode, file); 8665d5182caSJohn Johansen } 8675d5182caSJohn Johansen 8685d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v) 8695d5182caSJohn Johansen { 8705d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 8715d5182caSJohn Johansen 8725d5182caSJohn Johansen seq_printf(seq, "v%d\n", data->abi); 8735ac8c355SJohn Johansen 8745ac8c355SJohn Johansen return 0; 8755ac8c355SJohn Johansen } 8765ac8c355SJohn Johansen 8775d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v) 8785ac8c355SJohn Johansen { 8795d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 8805ac8c355SJohn Johansen 8815d5182caSJohn Johansen seq_printf(seq, "%ld\n", data->revision); 8825ac8c355SJohn Johansen 8835ac8c355SJohn Johansen return 0; 8845ac8c355SJohn Johansen } 8855ac8c355SJohn Johansen 8865d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v) 8875ac8c355SJohn Johansen { 8885d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 8895ac8c355SJohn Johansen unsigned int i, size = aa_hash_size(); 8905ac8c355SJohn Johansen 8915d5182caSJohn Johansen if (data->hash) { 8925ac8c355SJohn Johansen for (i = 0; i < size; i++) 8935d5182caSJohn Johansen seq_printf(seq, "%.2x", data->hash[i]); 89447dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 8955ac8c355SJohn Johansen } 8965ac8c355SJohn Johansen 8975ac8c355SJohn Johansen return 0; 8985ac8c355SJohn Johansen } 8995ac8c355SJohn Johansen 9005d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi); 9015d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision); 9025d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash); 9035ac8c355SJohn Johansen 9045ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, 9055ac8c355SJohn Johansen loff_t *ppos) 9065ac8c355SJohn Johansen { 9075ac8c355SJohn Johansen struct aa_loaddata *rawdata = file->private_data; 9085ac8c355SJohn Johansen 9095ac8c355SJohn Johansen return simple_read_from_buffer(buf, size, ppos, rawdata->data, 9105ac8c355SJohn Johansen rawdata->size); 9115ac8c355SJohn Johansen } 9125ac8c355SJohn Johansen 9135d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file) 9145ac8c355SJohn Johansen { 9155d5182caSJohn Johansen aa_put_loaddata(file->private_data); 9165ac8c355SJohn Johansen 9175ac8c355SJohn Johansen return 0; 9185ac8c355SJohn Johansen } 9195ac8c355SJohn Johansen 9205d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file) 9215d5182caSJohn Johansen { 9225d5182caSJohn Johansen if (!policy_view_capable(NULL)) 9235d5182caSJohn Johansen return -EACCES; 9245d5182caSJohn Johansen file->private_data = __aa_get_loaddata(inode->i_private); 9255d5182caSJohn Johansen if (!file->private_data) 9265d5182caSJohn Johansen /* lost race: this entry is being reaped */ 9275d5182caSJohn Johansen return -ENOENT; 9285d5182caSJohn Johansen 9295d5182caSJohn Johansen return 0; 9305d5182caSJohn Johansen } 9315d5182caSJohn Johansen 9325d5182caSJohn Johansen static const struct file_operations rawdata_fops = { 9335ac8c355SJohn Johansen .open = rawdata_open, 9345ac8c355SJohn Johansen .read = rawdata_read, 9355ac8c355SJohn Johansen .llseek = generic_file_llseek, 9365ac8c355SJohn Johansen .release = rawdata_release, 9375ac8c355SJohn Johansen }; 9385ac8c355SJohn Johansen 9395d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata) 9405d5182caSJohn Johansen { 9415d5182caSJohn Johansen int i; 9425d5182caSJohn Johansen 9435d5182caSJohn Johansen for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) { 9445d5182caSJohn Johansen if (!IS_ERR_OR_NULL(rawdata->dents[i])) { 9455d5182caSJohn Johansen /* no refcounts on i_private */ 946*c961ee5fSJohn Johansen aafs_remove(rawdata->dents[i]); 9475d5182caSJohn Johansen rawdata->dents[i] = NULL; 9485d5182caSJohn Johansen } 9495d5182caSJohn Johansen } 9505d5182caSJohn Johansen } 9515d5182caSJohn Johansen 9525d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata) 9535d5182caSJohn Johansen { 9545d5182caSJohn Johansen AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock)); 9555d5182caSJohn Johansen 9565d5182caSJohn Johansen if (rawdata->ns) { 9575d5182caSJohn Johansen remove_rawdata_dents(rawdata); 9585d5182caSJohn Johansen list_del_init(&rawdata->list); 9595d5182caSJohn Johansen aa_put_ns(rawdata->ns); 9605d5182caSJohn Johansen rawdata->ns = NULL; 9615d5182caSJohn Johansen } 9625d5182caSJohn Johansen } 9635d5182caSJohn Johansen 9645d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata) 9655d5182caSJohn Johansen { 9665d5182caSJohn Johansen struct dentry *dent, *dir; 9675d5182caSJohn Johansen 9685d5182caSJohn Johansen AA_BUG(!ns); 9695d5182caSJohn Johansen AA_BUG(!rawdata); 9705d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 9715d5182caSJohn Johansen AA_BUG(!ns_subdata_dir(ns)); 9725d5182caSJohn Johansen 9735d5182caSJohn Johansen /* 9745d5182caSJohn Johansen * just use ns revision dir was originally created at. This is 9755d5182caSJohn Johansen * under ns->lock and if load is successful revision will be 9765d5182caSJohn Johansen * bumped and is guaranteed to be unique 9775d5182caSJohn Johansen */ 9785d5182caSJohn Johansen rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision); 9795d5182caSJohn Johansen if (!rawdata->name) 9805d5182caSJohn Johansen return -ENOMEM; 9815d5182caSJohn Johansen 982*c961ee5fSJohn Johansen dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns)); 9835d5182caSJohn Johansen if (IS_ERR(dir)) 9845d5182caSJohn Johansen /* ->name freed when rawdata freed */ 9855d5182caSJohn Johansen return PTR_ERR(dir); 9865d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DIR] = dir; 9875d5182caSJohn Johansen 988*c961ee5fSJohn Johansen dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata, 9895d5182caSJohn Johansen &seq_rawdata_abi_fops); 9905d5182caSJohn Johansen if (IS_ERR(dent)) 9915d5182caSJohn Johansen goto fail; 9925d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_ABI] = dent; 9935d5182caSJohn Johansen 994*c961ee5fSJohn Johansen dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata, 9955d5182caSJohn Johansen &seq_rawdata_revision_fops); 9965d5182caSJohn Johansen if (IS_ERR(dent)) 9975d5182caSJohn Johansen goto fail; 9985d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_REVISION] = dent; 9995d5182caSJohn Johansen 10005d5182caSJohn Johansen if (aa_g_hash_policy) { 1001*c961ee5fSJohn Johansen dent = aafs_create_file("sha1", S_IFREG | 0444, dir, 10025d5182caSJohn Johansen rawdata, &seq_rawdata_hash_fops); 10035d5182caSJohn Johansen if (IS_ERR(dent)) 10045d5182caSJohn Johansen goto fail; 10055d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_HASH] = dent; 10065d5182caSJohn Johansen } 10075d5182caSJohn Johansen 1008*c961ee5fSJohn Johansen dent = aafs_create_file("raw_data", S_IFREG | 0444, 10095d5182caSJohn Johansen dir, rawdata, &rawdata_fops); 10105d5182caSJohn Johansen if (IS_ERR(dent)) 10115d5182caSJohn Johansen goto fail; 10125d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DATA] = dent; 10135d5182caSJohn Johansen d_inode(dent)->i_size = rawdata->size; 10145d5182caSJohn Johansen 10155d5182caSJohn Johansen rawdata->ns = aa_get_ns(ns); 10165d5182caSJohn Johansen list_add(&rawdata->list, &ns->rawdata_list); 10175d5182caSJohn Johansen /* no refcount on inode rawdata */ 10185d5182caSJohn Johansen 10195d5182caSJohn Johansen return 0; 10205d5182caSJohn Johansen 10215d5182caSJohn Johansen fail: 10225d5182caSJohn Johansen remove_rawdata_dents(rawdata); 10235d5182caSJohn Johansen 10245d5182caSJohn Johansen return PTR_ERR(dent); 10255d5182caSJohn Johansen } 10265d5182caSJohn Johansen 10270d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/ 1028c97204baSJohn Johansen 1029c97204baSJohn Johansen /** 1030c97204baSJohn Johansen * 1031c97204baSJohn Johansen * Requires: @profile->ns->lock held 1032c97204baSJohn Johansen */ 1033c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile) 10340d259f04SJohn Johansen { 10350d259f04SJohn Johansen struct aa_profile *child; 10360d259f04SJohn Johansen int i; 10370d259f04SJohn Johansen 10380d259f04SJohn Johansen if (!profile) 10390d259f04SJohn Johansen return; 10400d259f04SJohn Johansen 10410d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) 1042c97204baSJohn Johansen __aafs_profile_rmdir(child); 10430d259f04SJohn Johansen 10440d259f04SJohn Johansen for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { 10458399588aSJohn Johansen struct aa_proxy *proxy; 10460d259f04SJohn Johansen if (!profile->dents[i]) 10470d259f04SJohn Johansen continue; 10480d259f04SJohn Johansen 10498399588aSJohn Johansen proxy = d_inode(profile->dents[i])->i_private; 1050*c961ee5fSJohn Johansen aafs_remove(profile->dents[i]); 10518399588aSJohn Johansen aa_put_proxy(proxy); 10520d259f04SJohn Johansen profile->dents[i] = NULL; 10530d259f04SJohn Johansen } 10540d259f04SJohn Johansen } 10550d259f04SJohn Johansen 1056c97204baSJohn Johansen /** 1057c97204baSJohn Johansen * 1058c97204baSJohn Johansen * Requires: @old->ns->lock held 1059c97204baSJohn Johansen */ 1060c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old, 10610d259f04SJohn Johansen struct aa_profile *new) 10620d259f04SJohn Johansen { 10630d259f04SJohn Johansen int i; 10640d259f04SJohn Johansen 10650d259f04SJohn Johansen for (i = 0; i < AAFS_PROF_SIZEOF; i++) { 10660d259f04SJohn Johansen new->dents[i] = old->dents[i]; 1067d671e890SJohn Johansen if (new->dents[i]) 1068078cd827SDeepa Dinamani new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); 10690d259f04SJohn Johansen old->dents[i] = NULL; 10700d259f04SJohn Johansen } 10710d259f04SJohn Johansen } 10720d259f04SJohn Johansen 10730d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name, 10740d259f04SJohn Johansen struct aa_profile *profile, 10750d259f04SJohn Johansen const struct file_operations *fops) 10760d259f04SJohn Johansen { 10778399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(profile->proxy); 10780d259f04SJohn Johansen struct dentry *dent; 10790d259f04SJohn Johansen 1080*c961ee5fSJohn Johansen dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops); 10810d259f04SJohn Johansen if (IS_ERR(dent)) 10828399588aSJohn Johansen aa_put_proxy(proxy); 10830d259f04SJohn Johansen 10840d259f04SJohn Johansen return dent; 10850d259f04SJohn Johansen } 10860d259f04SJohn Johansen 10875d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile) 10885d5182caSJohn Johansen { 10895d5182caSJohn Johansen int depth = 0; 10905d5182caSJohn Johansen 10915d5182caSJohn Johansen rcu_read_lock(); 10925d5182caSJohn Johansen for (depth = 0; profile; profile = rcu_access_pointer(profile->parent)) 10935d5182caSJohn Johansen depth++; 10945d5182caSJohn Johansen rcu_read_unlock(); 10955d5182caSJohn Johansen 10965d5182caSJohn Johansen return depth; 10975d5182caSJohn Johansen } 10985d5182caSJohn Johansen 10995d5182caSJohn Johansen static int gen_symlink_name(char *buffer, size_t bsize, int depth, 11005d5182caSJohn Johansen const char *dirname, const char *fname) 11015d5182caSJohn Johansen { 11025d5182caSJohn Johansen int error; 11035d5182caSJohn Johansen 11045d5182caSJohn Johansen for (; depth > 0; depth--) { 11055d5182caSJohn Johansen if (bsize < 7) 11065d5182caSJohn Johansen return -ENAMETOOLONG; 11075d5182caSJohn Johansen strcpy(buffer, "../../"); 11085d5182caSJohn Johansen buffer += 6; 11095d5182caSJohn Johansen bsize -= 6; 11105d5182caSJohn Johansen } 11115d5182caSJohn Johansen 11125d5182caSJohn Johansen error = snprintf(buffer, bsize, "raw_data/%s/%s", dirname, fname); 11135d5182caSJohn Johansen if (error >= bsize || error < 0) 11145d5182caSJohn Johansen return -ENAMETOOLONG; 11155d5182caSJohn Johansen 11165d5182caSJohn Johansen return 0; 11175d5182caSJohn Johansen } 11185d5182caSJohn Johansen 11195d5182caSJohn Johansen /* 11205d5182caSJohn Johansen * Requires: @profile->ns->lock held 11215d5182caSJohn Johansen */ 1122c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) 11230d259f04SJohn Johansen { 11240d259f04SJohn Johansen struct aa_profile *child; 11250d259f04SJohn Johansen struct dentry *dent = NULL, *dir; 11260d259f04SJohn Johansen int error; 11270d259f04SJohn Johansen 11280d259f04SJohn Johansen if (!parent) { 11290d259f04SJohn Johansen struct aa_profile *p; 11300d259f04SJohn Johansen p = aa_deref_parent(profile); 11310d259f04SJohn Johansen dent = prof_dir(p); 11320d259f04SJohn Johansen /* adding to parent that previously didn't have children */ 1133*c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dent); 11340d259f04SJohn Johansen if (IS_ERR(dent)) 11350d259f04SJohn Johansen goto fail; 11360d259f04SJohn Johansen prof_child_dir(p) = parent = dent; 11370d259f04SJohn Johansen } 11380d259f04SJohn Johansen 11390d259f04SJohn Johansen if (!profile->dirname) { 11400d259f04SJohn Johansen int len, id_len; 11410d259f04SJohn Johansen len = mangle_name(profile->base.name, NULL); 11420d259f04SJohn Johansen id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); 11430d259f04SJohn Johansen 11440d259f04SJohn Johansen profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); 1145ffac1de6SDan Carpenter if (!profile->dirname) { 1146ffac1de6SDan Carpenter error = -ENOMEM; 1147ffac1de6SDan Carpenter goto fail2; 1148ffac1de6SDan Carpenter } 11490d259f04SJohn Johansen 11500d259f04SJohn Johansen mangle_name(profile->base.name, profile->dirname); 11510d259f04SJohn Johansen sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); 11520d259f04SJohn Johansen } 11530d259f04SJohn Johansen 1154*c961ee5fSJohn Johansen dent = aafs_create_dir(profile->dirname, parent); 11550d259f04SJohn Johansen if (IS_ERR(dent)) 11560d259f04SJohn Johansen goto fail; 11570d259f04SJohn Johansen prof_dir(profile) = dir = dent; 11580d259f04SJohn Johansen 115952b97de3SJohn Johansen dent = create_profile_file(dir, "name", profile, 116052b97de3SJohn Johansen &seq_profile_name_fops); 11610d259f04SJohn Johansen if (IS_ERR(dent)) 11620d259f04SJohn Johansen goto fail; 11630d259f04SJohn Johansen profile->dents[AAFS_PROF_NAME] = dent; 11640d259f04SJohn Johansen 116552b97de3SJohn Johansen dent = create_profile_file(dir, "mode", profile, 116652b97de3SJohn Johansen &seq_profile_mode_fops); 11670d259f04SJohn Johansen if (IS_ERR(dent)) 11680d259f04SJohn Johansen goto fail; 11690d259f04SJohn Johansen profile->dents[AAFS_PROF_MODE] = dent; 11700d259f04SJohn Johansen 1171556d0be7SJohn Johansen dent = create_profile_file(dir, "attach", profile, 117252b97de3SJohn Johansen &seq_profile_attach_fops); 1173556d0be7SJohn Johansen if (IS_ERR(dent)) 1174556d0be7SJohn Johansen goto fail; 1175556d0be7SJohn Johansen profile->dents[AAFS_PROF_ATTACH] = dent; 1176556d0be7SJohn Johansen 1177f8eb8a13SJohn Johansen if (profile->hash) { 1178f8eb8a13SJohn Johansen dent = create_profile_file(dir, "sha1", profile, 117952b97de3SJohn Johansen &seq_profile_hash_fops); 1180f8eb8a13SJohn Johansen if (IS_ERR(dent)) 1181f8eb8a13SJohn Johansen goto fail; 1182f8eb8a13SJohn Johansen profile->dents[AAFS_PROF_HASH] = dent; 1183f8eb8a13SJohn Johansen } 1184f8eb8a13SJohn Johansen 11855ac8c355SJohn Johansen if (profile->rawdata) { 11865d5182caSJohn Johansen char target[64]; 11875d5182caSJohn Johansen int depth = profile_depth(profile); 11885d5182caSJohn Johansen 11895d5182caSJohn Johansen error = gen_symlink_name(target, sizeof(target), depth, 11905d5182caSJohn Johansen profile->rawdata->name, "sha1"); 11915d5182caSJohn Johansen if (error < 0) 11925d5182caSJohn Johansen goto fail2; 1193*c961ee5fSJohn Johansen dent = aafs_create_symlink("raw_sha1", dir, target, NULL); 11945ac8c355SJohn Johansen if (IS_ERR(dent)) 11955ac8c355SJohn Johansen goto fail; 11965ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_HASH] = dent; 11975ac8c355SJohn Johansen 11985d5182caSJohn Johansen error = gen_symlink_name(target, sizeof(target), depth, 11995d5182caSJohn Johansen profile->rawdata->name, "abi"); 12005d5182caSJohn Johansen if (error < 0) 12015d5182caSJohn Johansen goto fail2; 1202*c961ee5fSJohn Johansen dent = aafs_create_symlink("raw_abi", dir, target, NULL); 12035ac8c355SJohn Johansen if (IS_ERR(dent)) 12045ac8c355SJohn Johansen goto fail; 12055ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_ABI] = dent; 12065ac8c355SJohn Johansen 12075d5182caSJohn Johansen error = gen_symlink_name(target, sizeof(target), depth, 12085d5182caSJohn Johansen profile->rawdata->name, "raw_data"); 12095d5182caSJohn Johansen if (error < 0) 12105d5182caSJohn Johansen goto fail2; 1211*c961ee5fSJohn Johansen dent = aafs_create_symlink("raw_data", dir, target, NULL); 12125ac8c355SJohn Johansen if (IS_ERR(dent)) 12135ac8c355SJohn Johansen goto fail; 12145ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_DATA] = dent; 12155ac8c355SJohn Johansen } 12165ac8c355SJohn Johansen 12170d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) { 1218c97204baSJohn Johansen error = __aafs_profile_mkdir(child, prof_child_dir(profile)); 12190d259f04SJohn Johansen if (error) 12200d259f04SJohn Johansen goto fail2; 12210d259f04SJohn Johansen } 12220d259f04SJohn Johansen 12230d259f04SJohn Johansen return 0; 12240d259f04SJohn Johansen 12250d259f04SJohn Johansen fail: 12260d259f04SJohn Johansen error = PTR_ERR(dent); 12270d259f04SJohn Johansen 12280d259f04SJohn Johansen fail2: 1229c97204baSJohn Johansen __aafs_profile_rmdir(profile); 12300d259f04SJohn Johansen 12310d259f04SJohn Johansen return error; 12320d259f04SJohn Johansen } 12330d259f04SJohn Johansen 12345d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns) 12355d5182caSJohn Johansen { 12365d5182caSJohn Johansen struct aa_loaddata *ent, *tmp; 12375d5182caSJohn Johansen 12385d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 12395d5182caSJohn Johansen 12405d5182caSJohn Johansen list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list) 12415d5182caSJohn Johansen __aa_fs_remove_rawdata(ent); 12425d5182caSJohn Johansen } 12435d5182caSJohn Johansen 1244c97204baSJohn Johansen /** 1245c97204baSJohn Johansen * 1246c97204baSJohn Johansen * Requires: @ns->lock held 1247c97204baSJohn Johansen */ 1248c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns) 12490d259f04SJohn Johansen { 125098849dffSJohn Johansen struct aa_ns *sub; 12510d259f04SJohn Johansen struct aa_profile *child; 12520d259f04SJohn Johansen int i; 12530d259f04SJohn Johansen 12540d259f04SJohn Johansen if (!ns) 12550d259f04SJohn Johansen return; 12560d259f04SJohn Johansen 12570d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) 1258c97204baSJohn Johansen __aafs_profile_rmdir(child); 12590d259f04SJohn Johansen 12600d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 12610d259f04SJohn Johansen mutex_lock(&sub->lock); 1262c97204baSJohn Johansen __aafs_ns_rmdir(sub); 12630d259f04SJohn Johansen mutex_unlock(&sub->lock); 12640d259f04SJohn Johansen } 12650d259f04SJohn Johansen 12665d5182caSJohn Johansen __aa_fs_list_remove_rawdata(ns); 12675d5182caSJohn Johansen 1268b7fd2c03SJohn Johansen if (ns_subns_dir(ns)) { 1269b7fd2c03SJohn Johansen sub = d_inode(ns_subns_dir(ns))->i_private; 1270b7fd2c03SJohn Johansen aa_put_ns(sub); 1271b7fd2c03SJohn Johansen } 1272b7fd2c03SJohn Johansen if (ns_subload(ns)) { 1273b7fd2c03SJohn Johansen sub = d_inode(ns_subload(ns))->i_private; 1274b7fd2c03SJohn Johansen aa_put_ns(sub); 1275b7fd2c03SJohn Johansen } 1276b7fd2c03SJohn Johansen if (ns_subreplace(ns)) { 1277b7fd2c03SJohn Johansen sub = d_inode(ns_subreplace(ns))->i_private; 1278b7fd2c03SJohn Johansen aa_put_ns(sub); 1279b7fd2c03SJohn Johansen } 1280b7fd2c03SJohn Johansen if (ns_subremove(ns)) { 1281b7fd2c03SJohn Johansen sub = d_inode(ns_subremove(ns))->i_private; 1282b7fd2c03SJohn Johansen aa_put_ns(sub); 1283b7fd2c03SJohn Johansen } 1284b7fd2c03SJohn Johansen 12850d259f04SJohn Johansen for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { 1286*c961ee5fSJohn Johansen aafs_remove(ns->dents[i]); 12870d259f04SJohn Johansen ns->dents[i] = NULL; 12880d259f04SJohn Johansen } 12890d259f04SJohn Johansen } 12900d259f04SJohn Johansen 1291b7fd2c03SJohn Johansen /* assumes cleanup in caller */ 1292c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) 1293b7fd2c03SJohn Johansen { 1294b7fd2c03SJohn Johansen struct dentry *dent; 1295b7fd2c03SJohn Johansen 1296b7fd2c03SJohn Johansen AA_BUG(!ns); 1297b7fd2c03SJohn Johansen AA_BUG(!dir); 1298b7fd2c03SJohn Johansen 1299*c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dir); 1300b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1301b7fd2c03SJohn Johansen return PTR_ERR(dent); 1302b7fd2c03SJohn Johansen ns_subprofs_dir(ns) = dent; 1303b7fd2c03SJohn Johansen 1304*c961ee5fSJohn Johansen dent = aafs_create_dir("raw_data", dir); 1305b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1306b7fd2c03SJohn Johansen return PTR_ERR(dent); 1307b7fd2c03SJohn Johansen ns_subdata_dir(ns) = dent; 1308b7fd2c03SJohn Johansen 1309*c961ee5fSJohn Johansen dent = aafs_create_file(".load", 0640, dir, ns, 1310b7fd2c03SJohn Johansen &aa_fs_profile_load); 1311b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1312b7fd2c03SJohn Johansen return PTR_ERR(dent); 1313b7fd2c03SJohn Johansen aa_get_ns(ns); 1314b7fd2c03SJohn Johansen ns_subload(ns) = dent; 1315b7fd2c03SJohn Johansen 1316*c961ee5fSJohn Johansen dent = aafs_create_file(".replace", 0640, dir, ns, 1317b7fd2c03SJohn Johansen &aa_fs_profile_replace); 1318b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1319b7fd2c03SJohn Johansen return PTR_ERR(dent); 1320b7fd2c03SJohn Johansen aa_get_ns(ns); 1321b7fd2c03SJohn Johansen ns_subreplace(ns) = dent; 1322b7fd2c03SJohn Johansen 1323*c961ee5fSJohn Johansen dent = aafs_create_file(".remove", 0640, dir, ns, 1324b7fd2c03SJohn Johansen &aa_fs_profile_remove); 1325b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1326b7fd2c03SJohn Johansen return PTR_ERR(dent); 1327b7fd2c03SJohn Johansen aa_get_ns(ns); 1328b7fd2c03SJohn Johansen ns_subremove(ns) = dent; 1329b7fd2c03SJohn Johansen 1330*c961ee5fSJohn Johansen dent = aafs_create_dir("namespaces", dir); 1331b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1332b7fd2c03SJohn Johansen return PTR_ERR(dent); 1333b7fd2c03SJohn Johansen aa_get_ns(ns); 1334b7fd2c03SJohn Johansen ns_subns_dir(ns) = dent; 1335b7fd2c03SJohn Johansen 1336b7fd2c03SJohn Johansen return 0; 1337b7fd2c03SJohn Johansen } 1338b7fd2c03SJohn Johansen 1339c97204baSJohn Johansen /* 1340c97204baSJohn Johansen * Requires: @ns->lock held 1341c97204baSJohn Johansen */ 134298407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name, 134398407f0aSJohn Johansen struct dentry *dent) 13440d259f04SJohn Johansen { 134598849dffSJohn Johansen struct aa_ns *sub; 13460d259f04SJohn Johansen struct aa_profile *child; 134798407f0aSJohn Johansen struct dentry *dir; 13480d259f04SJohn Johansen int error; 13490d259f04SJohn Johansen 1350b7fd2c03SJohn Johansen AA_BUG(!ns); 1351b7fd2c03SJohn Johansen AA_BUG(!parent); 1352b7fd2c03SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 1353b7fd2c03SJohn Johansen 13540d259f04SJohn Johansen if (!name) 13550d259f04SJohn Johansen name = ns->base.name; 13560d259f04SJohn Johansen 1357*c961ee5fSJohn Johansen if (!dent) { 1358b7fd2c03SJohn Johansen /* create ns dir if it doesn't already exist */ 1359*c961ee5fSJohn Johansen dent = aafs_create_dir(name, parent); 13600d259f04SJohn Johansen if (IS_ERR(dent)) 13610d259f04SJohn Johansen goto fail; 1362*c961ee5fSJohn Johansen } else 1363*c961ee5fSJohn Johansen dget(dent); 13640d259f04SJohn Johansen ns_dir(ns) = dir = dent; 1365c97204baSJohn Johansen error = __aafs_ns_mkdir_entries(ns, dir); 1366b7fd2c03SJohn Johansen if (error) 1367b7fd2c03SJohn Johansen goto fail2; 13680d259f04SJohn Johansen 1369b7fd2c03SJohn Johansen /* profiles */ 13700d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) { 1371c97204baSJohn Johansen error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns)); 13720d259f04SJohn Johansen if (error) 13730d259f04SJohn Johansen goto fail2; 13740d259f04SJohn Johansen } 13750d259f04SJohn Johansen 1376b7fd2c03SJohn Johansen /* subnamespaces */ 13770d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 13780d259f04SJohn Johansen mutex_lock(&sub->lock); 137998407f0aSJohn Johansen error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL); 13800d259f04SJohn Johansen mutex_unlock(&sub->lock); 13810d259f04SJohn Johansen if (error) 13820d259f04SJohn Johansen goto fail2; 13830d259f04SJohn Johansen } 13840d259f04SJohn Johansen 13850d259f04SJohn Johansen return 0; 13860d259f04SJohn Johansen 13870d259f04SJohn Johansen fail: 13880d259f04SJohn Johansen error = PTR_ERR(dent); 13890d259f04SJohn Johansen 13900d259f04SJohn Johansen fail2: 1391c97204baSJohn Johansen __aafs_ns_rmdir(ns); 13920d259f04SJohn Johansen 13930d259f04SJohn Johansen return error; 13940d259f04SJohn Johansen } 13950d259f04SJohn Johansen 13960d259f04SJohn Johansen 139729b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head)) 139829b3822fSJohn Johansen 139929b3822fSJohn Johansen /** 140098849dffSJohn Johansen * __next_ns - find the next namespace to list 140129b3822fSJohn Johansen * @root: root namespace to stop search at (NOT NULL) 140229b3822fSJohn Johansen * @ns: current ns position (NOT NULL) 140329b3822fSJohn Johansen * 140429b3822fSJohn Johansen * Find the next namespace from @ns under @root and handle all locking needed 140529b3822fSJohn Johansen * while switching current namespace. 140629b3822fSJohn Johansen * 140729b3822fSJohn Johansen * Returns: next namespace or NULL if at last namespace under @root 140829b3822fSJohn Johansen * Requires: ns->parent->lock to be held 140929b3822fSJohn Johansen * NOTE: will not unlock root->lock 141029b3822fSJohn Johansen */ 141198849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) 141229b3822fSJohn Johansen { 141398849dffSJohn Johansen struct aa_ns *parent, *next; 141429b3822fSJohn Johansen 141529b3822fSJohn Johansen /* is next namespace a child */ 141629b3822fSJohn Johansen if (!list_empty(&ns->sub_ns)) { 141729b3822fSJohn Johansen next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); 141829b3822fSJohn Johansen mutex_lock(&next->lock); 141929b3822fSJohn Johansen return next; 142029b3822fSJohn Johansen } 142129b3822fSJohn Johansen 142229b3822fSJohn Johansen /* check if the next ns is a sibling, parent, gp, .. */ 142329b3822fSJohn Johansen parent = ns->parent; 1424ed2c7da3SJohn Johansen while (ns != root) { 142529b3822fSJohn Johansen mutex_unlock(&ns->lock); 142638dbd7d8SGeliang Tang next = list_next_entry(ns, base.list); 142729b3822fSJohn Johansen if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { 142829b3822fSJohn Johansen mutex_lock(&next->lock); 142929b3822fSJohn Johansen return next; 143029b3822fSJohn Johansen } 143129b3822fSJohn Johansen ns = parent; 143229b3822fSJohn Johansen parent = parent->parent; 143329b3822fSJohn Johansen } 143429b3822fSJohn Johansen 143529b3822fSJohn Johansen return NULL; 143629b3822fSJohn Johansen } 143729b3822fSJohn Johansen 143829b3822fSJohn Johansen /** 143929b3822fSJohn Johansen * __first_profile - find the first profile in a namespace 144029b3822fSJohn Johansen * @root: namespace that is root of profiles being displayed (NOT NULL) 144129b3822fSJohn Johansen * @ns: namespace to start in (NOT NULL) 144229b3822fSJohn Johansen * 144329b3822fSJohn Johansen * Returns: unrefcounted profile or NULL if no profile 144429b3822fSJohn Johansen * Requires: profile->ns.lock to be held 144529b3822fSJohn Johansen */ 144698849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root, 144798849dffSJohn Johansen struct aa_ns *ns) 144829b3822fSJohn Johansen { 144998849dffSJohn Johansen for (; ns; ns = __next_ns(root, ns)) { 145029b3822fSJohn Johansen if (!list_empty(&ns->base.profiles)) 145129b3822fSJohn Johansen return list_first_entry(&ns->base.profiles, 145229b3822fSJohn Johansen struct aa_profile, base.list); 145329b3822fSJohn Johansen } 145429b3822fSJohn Johansen return NULL; 145529b3822fSJohn Johansen } 145629b3822fSJohn Johansen 145729b3822fSJohn Johansen /** 145829b3822fSJohn Johansen * __next_profile - step to the next profile in a profile tree 145929b3822fSJohn Johansen * @profile: current profile in tree (NOT NULL) 146029b3822fSJohn Johansen * 146129b3822fSJohn Johansen * Perform a depth first traversal on the profile tree in a namespace 146229b3822fSJohn Johansen * 146329b3822fSJohn Johansen * Returns: next profile or NULL if done 146429b3822fSJohn Johansen * Requires: profile->ns.lock to be held 146529b3822fSJohn Johansen */ 146629b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p) 146729b3822fSJohn Johansen { 146829b3822fSJohn Johansen struct aa_profile *parent; 146998849dffSJohn Johansen struct aa_ns *ns = p->ns; 147029b3822fSJohn Johansen 147129b3822fSJohn Johansen /* is next profile a child */ 147229b3822fSJohn Johansen if (!list_empty(&p->base.profiles)) 147329b3822fSJohn Johansen return list_first_entry(&p->base.profiles, typeof(*p), 147429b3822fSJohn Johansen base.list); 147529b3822fSJohn Johansen 147629b3822fSJohn Johansen /* is next profile a sibling, parent sibling, gp, sibling, .. */ 147729b3822fSJohn Johansen parent = rcu_dereference_protected(p->parent, 147829b3822fSJohn Johansen mutex_is_locked(&p->ns->lock)); 147929b3822fSJohn Johansen while (parent) { 148038dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 148129b3822fSJohn Johansen if (!list_entry_is_head(p, &parent->base.profiles, base.list)) 148229b3822fSJohn Johansen return p; 148329b3822fSJohn Johansen p = parent; 148429b3822fSJohn Johansen parent = rcu_dereference_protected(parent->parent, 148529b3822fSJohn Johansen mutex_is_locked(&parent->ns->lock)); 148629b3822fSJohn Johansen } 148729b3822fSJohn Johansen 148829b3822fSJohn Johansen /* is next another profile in the namespace */ 148938dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 149029b3822fSJohn Johansen if (!list_entry_is_head(p, &ns->base.profiles, base.list)) 149129b3822fSJohn Johansen return p; 149229b3822fSJohn Johansen 149329b3822fSJohn Johansen return NULL; 149429b3822fSJohn Johansen } 149529b3822fSJohn Johansen 149629b3822fSJohn Johansen /** 149729b3822fSJohn Johansen * next_profile - step to the next profile in where ever it may be 149829b3822fSJohn Johansen * @root: root namespace (NOT NULL) 149929b3822fSJohn Johansen * @profile: current profile (NOT NULL) 150029b3822fSJohn Johansen * 150129b3822fSJohn Johansen * Returns: next profile or NULL if there isn't one 150229b3822fSJohn Johansen */ 150398849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root, 150429b3822fSJohn Johansen struct aa_profile *profile) 150529b3822fSJohn Johansen { 150629b3822fSJohn Johansen struct aa_profile *next = __next_profile(profile); 150729b3822fSJohn Johansen if (next) 150829b3822fSJohn Johansen return next; 150929b3822fSJohn Johansen 151029b3822fSJohn Johansen /* finished all profiles in namespace move to next namespace */ 151198849dffSJohn Johansen return __first_profile(root, __next_ns(root, profile->ns)); 151229b3822fSJohn Johansen } 151329b3822fSJohn Johansen 151429b3822fSJohn Johansen /** 151529b3822fSJohn Johansen * p_start - start a depth first traversal of profile tree 151629b3822fSJohn Johansen * @f: seq_file to fill 151729b3822fSJohn Johansen * @pos: current position 151829b3822fSJohn Johansen * 151929b3822fSJohn Johansen * Returns: first profile under current namespace or NULL if none found 152029b3822fSJohn Johansen * 152129b3822fSJohn Johansen * acquires first ns->lock 152229b3822fSJohn Johansen */ 152329b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos) 152429b3822fSJohn Johansen { 152529b3822fSJohn Johansen struct aa_profile *profile = NULL; 152698849dffSJohn Johansen struct aa_ns *root = aa_current_profile()->ns; 152729b3822fSJohn Johansen loff_t l = *pos; 152898849dffSJohn Johansen f->private = aa_get_ns(root); 152929b3822fSJohn Johansen 153029b3822fSJohn Johansen 153129b3822fSJohn Johansen /* find the first profile */ 153229b3822fSJohn Johansen mutex_lock(&root->lock); 153329b3822fSJohn Johansen profile = __first_profile(root, root); 153429b3822fSJohn Johansen 153529b3822fSJohn Johansen /* skip to position */ 153629b3822fSJohn Johansen for (; profile && l > 0; l--) 153729b3822fSJohn Johansen profile = next_profile(root, profile); 153829b3822fSJohn Johansen 153929b3822fSJohn Johansen return profile; 154029b3822fSJohn Johansen } 154129b3822fSJohn Johansen 154229b3822fSJohn Johansen /** 154329b3822fSJohn Johansen * p_next - read the next profile entry 154429b3822fSJohn Johansen * @f: seq_file to fill 154529b3822fSJohn Johansen * @p: profile previously returned 154629b3822fSJohn Johansen * @pos: current position 154729b3822fSJohn Johansen * 154829b3822fSJohn Johansen * Returns: next profile after @p or NULL if none 154929b3822fSJohn Johansen * 155029b3822fSJohn Johansen * may acquire/release locks in namespace tree as necessary 155129b3822fSJohn Johansen */ 155229b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos) 155329b3822fSJohn Johansen { 155429b3822fSJohn Johansen struct aa_profile *profile = p; 155598849dffSJohn Johansen struct aa_ns *ns = f->private; 155629b3822fSJohn Johansen (*pos)++; 155729b3822fSJohn Johansen 155829b3822fSJohn Johansen return next_profile(ns, profile); 155929b3822fSJohn Johansen } 156029b3822fSJohn Johansen 156129b3822fSJohn Johansen /** 156229b3822fSJohn Johansen * p_stop - stop depth first traversal 156329b3822fSJohn Johansen * @f: seq_file we are filling 156429b3822fSJohn Johansen * @p: the last profile writen 156529b3822fSJohn Johansen * 156629b3822fSJohn Johansen * Release all locking done by p_start/p_next on namespace tree 156729b3822fSJohn Johansen */ 156829b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p) 156929b3822fSJohn Johansen { 157029b3822fSJohn Johansen struct aa_profile *profile = p; 157198849dffSJohn Johansen struct aa_ns *root = f->private, *ns; 157229b3822fSJohn Johansen 157329b3822fSJohn Johansen if (profile) { 157429b3822fSJohn Johansen for (ns = profile->ns; ns && ns != root; ns = ns->parent) 157529b3822fSJohn Johansen mutex_unlock(&ns->lock); 157629b3822fSJohn Johansen } 157729b3822fSJohn Johansen mutex_unlock(&root->lock); 157898849dffSJohn Johansen aa_put_ns(root); 157929b3822fSJohn Johansen } 158029b3822fSJohn Johansen 158129b3822fSJohn Johansen /** 158229b3822fSJohn Johansen * seq_show_profile - show a profile entry 158329b3822fSJohn Johansen * @f: seq_file to file 158429b3822fSJohn Johansen * @p: current position (profile) (NOT NULL) 158529b3822fSJohn Johansen * 158629b3822fSJohn Johansen * Returns: error on failure 158729b3822fSJohn Johansen */ 158829b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p) 158929b3822fSJohn Johansen { 159029b3822fSJohn Johansen struct aa_profile *profile = (struct aa_profile *)p; 159198849dffSJohn Johansen struct aa_ns *root = f->private; 159229b3822fSJohn Johansen 159329b3822fSJohn Johansen if (profile->ns != root) 159492b6d8efSJohn Johansen seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true)); 159529b3822fSJohn Johansen seq_printf(f, "%s (%s)\n", profile->base.hname, 159629b3822fSJohn Johansen aa_profile_mode_names[profile->mode]); 159729b3822fSJohn Johansen 159829b3822fSJohn Johansen return 0; 159929b3822fSJohn Johansen } 160029b3822fSJohn Johansen 1601c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = { 160229b3822fSJohn Johansen .start = p_start, 160329b3822fSJohn Johansen .next = p_next, 160429b3822fSJohn Johansen .stop = p_stop, 160529b3822fSJohn Johansen .show = seq_show_profile, 160629b3822fSJohn Johansen }; 160729b3822fSJohn Johansen 160829b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file) 160929b3822fSJohn Johansen { 16105ac8c355SJohn Johansen if (!policy_view_capable(NULL)) 16115ac8c355SJohn Johansen return -EACCES; 16125ac8c355SJohn Johansen 1613c97204baSJohn Johansen return seq_open(file, &aa_sfs_profiles_op); 161429b3822fSJohn Johansen } 161529b3822fSJohn Johansen 161629b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file) 161729b3822fSJohn Johansen { 161829b3822fSJohn Johansen return seq_release(inode, file); 161929b3822fSJohn Johansen } 162029b3822fSJohn Johansen 1621c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = { 162229b3822fSJohn Johansen .open = profiles_open, 162329b3822fSJohn Johansen .read = seq_read, 162429b3822fSJohn Johansen .llseek = seq_lseek, 162529b3822fSJohn Johansen .release = profiles_release, 162629b3822fSJohn Johansen }; 162729b3822fSJohn Johansen 162829b3822fSJohn Johansen 16290d259f04SJohn Johansen /** Base file system setup **/ 1630c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = { 1631c97204baSJohn Johansen AA_SFS_FILE_STRING("mask", 1632c97204baSJohn Johansen "create read write exec append mmap_exec link lock"), 1633a9bf8e9fSKees Cook { } 1634a9bf8e9fSKees Cook }; 1635a9bf8e9fSKees Cook 1636c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = { 1637c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hat", 1), 1638c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hatv", 1), 1639c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_onexec", 1), 1640c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_profile", 1), 1641c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), 1642c97204baSJohn Johansen AA_SFS_FILE_STRING("version", "1.2"), 1643e74abcf3SKees Cook { } 1644e74abcf3SKees Cook }; 1645e74abcf3SKees Cook 1646c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = { 1647c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("v5", 1), 1648474d6b75SJohn Johansen { } 1649474d6b75SJohn Johansen }; 1650474d6b75SJohn Johansen 1651c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = { 1652c97204baSJohn Johansen AA_SFS_DIR("versions", aa_sfs_entry_versions), 1653c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("set_load", 1), 16549d910a3bSJohn Johansen { } 16559d910a3bSJohn Johansen }; 16569d910a3bSJohn Johansen 1657c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = { 1658c97204baSJohn Johansen AA_SFS_DIR("policy", aa_sfs_entry_policy), 1659c97204baSJohn Johansen AA_SFS_DIR("domain", aa_sfs_entry_domain), 1660c97204baSJohn Johansen AA_SFS_DIR("file", aa_sfs_entry_file), 1661c97204baSJohn Johansen AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 1662c97204baSJohn Johansen AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit), 1663c97204baSJohn Johansen AA_SFS_DIR("caps", aa_sfs_entry_caps), 1664e74abcf3SKees Cook { } 1665e74abcf3SKees Cook }; 1666e74abcf3SKees Cook 1667c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = { 1668c97204baSJohn Johansen AA_SFS_FILE_FOPS(".access", 0640, &aa_sfs_access), 1669c97204baSJohn Johansen AA_SFS_FILE_FOPS(".ns_level", 0666, &seq_ns_level_fops), 1670c97204baSJohn Johansen AA_SFS_FILE_FOPS(".ns_name", 0640, &seq_ns_name_fops), 1671c97204baSJohn Johansen AA_SFS_FILE_FOPS("profiles", 0440, &aa_sfs_profiles_fops), 1672c97204baSJohn Johansen AA_SFS_DIR("features", aa_sfs_entry_features), 16739acd494bSKees Cook { } 16749acd494bSKees Cook }; 167563e2b423SJohn Johansen 1676c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry = 1677c97204baSJohn Johansen AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor); 16789acd494bSKees Cook 16799acd494bSKees Cook /** 1680a481f4d9SJohn Johansen * entry_create_file - create a file entry in the apparmor securityfs 1681c97204baSJohn Johansen * @fs_file: aa_sfs_entry to build an entry for (NOT NULL) 16829acd494bSKees Cook * @parent: the parent dentry in the securityfs 16839acd494bSKees Cook * 1684a481f4d9SJohn Johansen * Use entry_remove_file to remove entries created with this fn. 16859acd494bSKees Cook */ 1686c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file, 16879acd494bSKees Cook struct dentry *parent) 168863e2b423SJohn Johansen { 16899acd494bSKees Cook int error = 0; 169063e2b423SJohn Johansen 16919acd494bSKees Cook fs_file->dentry = securityfs_create_file(fs_file->name, 16929acd494bSKees Cook S_IFREG | fs_file->mode, 16939acd494bSKees Cook parent, fs_file, 16949acd494bSKees Cook fs_file->file_ops); 16959acd494bSKees Cook if (IS_ERR(fs_file->dentry)) { 16969acd494bSKees Cook error = PTR_ERR(fs_file->dentry); 16979acd494bSKees Cook fs_file->dentry = NULL; 169863e2b423SJohn Johansen } 16999acd494bSKees Cook return error; 170063e2b423SJohn Johansen } 170163e2b423SJohn Johansen 1702c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir); 170363e2b423SJohn Johansen /** 1704a481f4d9SJohn Johansen * entry_create_dir - recursively create a directory entry in the securityfs 1705c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL) 17069acd494bSKees Cook * @parent: the parent dentry in the securityfs 170763e2b423SJohn Johansen * 1708a481f4d9SJohn Johansen * Use entry_remove_dir to remove entries created with this fn. 170963e2b423SJohn Johansen */ 1710c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir, 17119acd494bSKees Cook struct dentry *parent) 171263e2b423SJohn Johansen { 1713c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 17140d259f04SJohn Johansen struct dentry *dir; 17150d259f04SJohn Johansen int error; 171663e2b423SJohn Johansen 17170d259f04SJohn Johansen dir = securityfs_create_dir(fs_dir->name, parent); 17180d259f04SJohn Johansen if (IS_ERR(dir)) 17190d259f04SJohn Johansen return PTR_ERR(dir); 17200d259f04SJohn Johansen fs_dir->dentry = dir; 172163e2b423SJohn Johansen 17220d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 1723c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 1724a481f4d9SJohn Johansen error = entry_create_dir(fs_file, fs_dir->dentry); 17259acd494bSKees Cook else 1726a481f4d9SJohn Johansen error = entry_create_file(fs_file, fs_dir->dentry); 17279acd494bSKees Cook if (error) 17289acd494bSKees Cook goto failed; 17299acd494bSKees Cook } 17309acd494bSKees Cook 17319acd494bSKees Cook return 0; 17329acd494bSKees Cook 17339acd494bSKees Cook failed: 1734a481f4d9SJohn Johansen entry_remove_dir(fs_dir); 17350d259f04SJohn Johansen 17369acd494bSKees Cook return error; 17379acd494bSKees Cook } 17389acd494bSKees Cook 17399acd494bSKees Cook /** 1740c97204baSJohn Johansen * entry_remove_file - drop a single file entry in the apparmor securityfs 1741c97204baSJohn Johansen * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL) 17429acd494bSKees Cook */ 1743c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file) 17449acd494bSKees Cook { 17459acd494bSKees Cook if (!fs_file->dentry) 17469acd494bSKees Cook return; 17479acd494bSKees Cook 17489acd494bSKees Cook securityfs_remove(fs_file->dentry); 17499acd494bSKees Cook fs_file->dentry = NULL; 17509acd494bSKees Cook } 17519acd494bSKees Cook 17529acd494bSKees Cook /** 1753a481f4d9SJohn Johansen * entry_remove_dir - recursively drop a directory entry from the securityfs 1754c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL) 17559acd494bSKees Cook */ 1756c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir) 17579acd494bSKees Cook { 1758c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 17599acd494bSKees Cook 17600d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 1761c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 1762a481f4d9SJohn Johansen entry_remove_dir(fs_file); 17639acd494bSKees Cook else 1764c97204baSJohn Johansen entry_remove_file(fs_file); 17659acd494bSKees Cook } 17669acd494bSKees Cook 1767c97204baSJohn Johansen entry_remove_file(fs_dir); 176863e2b423SJohn Johansen } 176963e2b423SJohn Johansen 177063e2b423SJohn Johansen /** 177163e2b423SJohn Johansen * aa_destroy_aafs - cleanup and free aafs 177263e2b423SJohn Johansen * 177363e2b423SJohn Johansen * releases dentries allocated by aa_create_aafs 177463e2b423SJohn Johansen */ 177563e2b423SJohn Johansen void __init aa_destroy_aafs(void) 177663e2b423SJohn Johansen { 1777c97204baSJohn Johansen entry_remove_dir(&aa_sfs_entry); 177863e2b423SJohn Johansen } 177963e2b423SJohn Johansen 1780a71ada30SJohn Johansen 1781a71ada30SJohn Johansen #define NULL_FILE_NAME ".null" 1782a71ada30SJohn Johansen struct path aa_null; 1783a71ada30SJohn Johansen 1784a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent) 1785a71ada30SJohn Johansen { 1786a71ada30SJohn Johansen struct vfsmount *mount = NULL; 1787a71ada30SJohn Johansen struct dentry *dentry; 1788a71ada30SJohn Johansen struct inode *inode; 1789a71ada30SJohn Johansen int count = 0; 1790a71ada30SJohn Johansen int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); 1791a71ada30SJohn Johansen 1792a71ada30SJohn Johansen if (error) 1793a71ada30SJohn Johansen return error; 1794a71ada30SJohn Johansen 1795a71ada30SJohn Johansen inode_lock(d_inode(parent)); 1796a71ada30SJohn Johansen dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); 1797a71ada30SJohn Johansen if (IS_ERR(dentry)) { 1798a71ada30SJohn Johansen error = PTR_ERR(dentry); 1799a71ada30SJohn Johansen goto out; 1800a71ada30SJohn Johansen } 1801a71ada30SJohn Johansen inode = new_inode(parent->d_inode->i_sb); 1802a71ada30SJohn Johansen if (!inode) { 1803a71ada30SJohn Johansen error = -ENOMEM; 1804a71ada30SJohn Johansen goto out1; 1805a71ada30SJohn Johansen } 1806a71ada30SJohn Johansen 1807a71ada30SJohn Johansen inode->i_ino = get_next_ino(); 1808a71ada30SJohn Johansen inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; 180924d0d03cSDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 1810a71ada30SJohn Johansen init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, 1811a71ada30SJohn Johansen MKDEV(MEM_MAJOR, 3)); 1812a71ada30SJohn Johansen d_instantiate(dentry, inode); 1813a71ada30SJohn Johansen aa_null.dentry = dget(dentry); 1814a71ada30SJohn Johansen aa_null.mnt = mntget(mount); 1815a71ada30SJohn Johansen 1816a71ada30SJohn Johansen error = 0; 1817a71ada30SJohn Johansen 1818a71ada30SJohn Johansen out1: 1819a71ada30SJohn Johansen dput(dentry); 1820a71ada30SJohn Johansen out: 1821a71ada30SJohn Johansen inode_unlock(d_inode(parent)); 1822a71ada30SJohn Johansen simple_release_fs(&mount, &count); 1823a71ada30SJohn Johansen return error; 1824a71ada30SJohn Johansen } 1825a71ada30SJohn Johansen 1826a481f4d9SJohn Johansen 1827a481f4d9SJohn Johansen 1828a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry, 1829a481f4d9SJohn Johansen struct inode *inode, 1830a481f4d9SJohn Johansen struct delayed_call *done) 1831a481f4d9SJohn Johansen { 1832a481f4d9SJohn Johansen struct aa_ns *ns; 1833a481f4d9SJohn Johansen struct path path; 1834a481f4d9SJohn Johansen 1835a481f4d9SJohn Johansen if (!dentry) 1836a481f4d9SJohn Johansen return ERR_PTR(-ECHILD); 1837a481f4d9SJohn Johansen ns = aa_get_current_ns(); 1838a481f4d9SJohn Johansen path.mnt = mntget(aafs_mnt); 1839a481f4d9SJohn Johansen path.dentry = dget(ns_dir(ns)); 1840a481f4d9SJohn Johansen nd_jump_link(&path); 1841a481f4d9SJohn Johansen aa_put_ns(ns); 1842a481f4d9SJohn Johansen 1843a481f4d9SJohn Johansen return NULL; 1844a481f4d9SJohn Johansen } 1845a481f4d9SJohn Johansen 1846a481f4d9SJohn Johansen static int ns_get_name(char *buf, size_t size, struct aa_ns *ns, 1847a481f4d9SJohn Johansen struct inode *inode) 1848a481f4d9SJohn Johansen { 1849a481f4d9SJohn Johansen int res = snprintf(buf, size, "%s:[%lu]", AAFS_NAME, inode->i_ino); 1850a481f4d9SJohn Johansen 1851a481f4d9SJohn Johansen if (res < 0 || res >= size) 1852a481f4d9SJohn Johansen res = -ENOENT; 1853a481f4d9SJohn Johansen 1854a481f4d9SJohn Johansen return res; 1855a481f4d9SJohn Johansen } 1856a481f4d9SJohn Johansen 1857a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer, 1858a481f4d9SJohn Johansen int buflen) 1859a481f4d9SJohn Johansen { 1860a481f4d9SJohn Johansen struct aa_ns *ns; 1861a481f4d9SJohn Johansen char name[32]; 1862a481f4d9SJohn Johansen int res; 1863a481f4d9SJohn Johansen 1864a481f4d9SJohn Johansen ns = aa_get_current_ns(); 1865a481f4d9SJohn Johansen res = ns_get_name(name, sizeof(name), ns, d_inode(dentry)); 1866a481f4d9SJohn Johansen if (res >= 0) 1867a481f4d9SJohn Johansen res = readlink_copy(buffer, buflen, name); 1868a481f4d9SJohn Johansen aa_put_ns(ns); 1869a481f4d9SJohn Johansen 1870a481f4d9SJohn Johansen return res; 1871a481f4d9SJohn Johansen } 1872a481f4d9SJohn Johansen 1873a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = { 1874a481f4d9SJohn Johansen .readlink = policy_readlink, 1875a481f4d9SJohn Johansen .get_link = policy_get_link, 1876a481f4d9SJohn Johansen }; 1877a481f4d9SJohn Johansen 1878a481f4d9SJohn Johansen 187963e2b423SJohn Johansen /** 188063e2b423SJohn Johansen * aa_create_aafs - create the apparmor security filesystem 188163e2b423SJohn Johansen * 188263e2b423SJohn Johansen * dentries created here are released by aa_destroy_aafs 188363e2b423SJohn Johansen * 188463e2b423SJohn Johansen * Returns: error on failure 188563e2b423SJohn Johansen */ 18863417d8d5SJames Morris static int __init aa_create_aafs(void) 188763e2b423SJohn Johansen { 1888b7fd2c03SJohn Johansen struct dentry *dent; 188963e2b423SJohn Johansen int error; 189063e2b423SJohn Johansen 189163e2b423SJohn Johansen if (!apparmor_initialized) 189263e2b423SJohn Johansen return 0; 189363e2b423SJohn Johansen 1894c97204baSJohn Johansen if (aa_sfs_entry.dentry) { 189563e2b423SJohn Johansen AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 189663e2b423SJohn Johansen return -EEXIST; 189763e2b423SJohn Johansen } 189863e2b423SJohn Johansen 1899a481f4d9SJohn Johansen /* setup apparmorfs used to virtualize policy/ */ 1900a481f4d9SJohn Johansen aafs_mnt = kern_mount(&aafs_ops); 1901a481f4d9SJohn Johansen if (IS_ERR(aafs_mnt)) 1902a481f4d9SJohn Johansen panic("can't set apparmorfs up\n"); 1903a481f4d9SJohn Johansen aafs_mnt->mnt_sb->s_flags &= ~MS_NOUSER; 1904a481f4d9SJohn Johansen 19059acd494bSKees Cook /* Populate fs tree. */ 1906c97204baSJohn Johansen error = entry_create_dir(&aa_sfs_entry, NULL); 190763e2b423SJohn Johansen if (error) 190863e2b423SJohn Johansen goto error; 190963e2b423SJohn Johansen 1910c97204baSJohn Johansen dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry, 1911b7fd2c03SJohn Johansen NULL, &aa_fs_profile_load); 1912b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 1913b7fd2c03SJohn Johansen error = PTR_ERR(dent); 1914b7fd2c03SJohn Johansen goto error; 1915b7fd2c03SJohn Johansen } 1916b7fd2c03SJohn Johansen ns_subload(root_ns) = dent; 1917b7fd2c03SJohn Johansen 1918c97204baSJohn Johansen dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry, 1919b7fd2c03SJohn Johansen NULL, &aa_fs_profile_replace); 1920b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 1921b7fd2c03SJohn Johansen error = PTR_ERR(dent); 1922b7fd2c03SJohn Johansen goto error; 1923b7fd2c03SJohn Johansen } 1924b7fd2c03SJohn Johansen ns_subreplace(root_ns) = dent; 1925b7fd2c03SJohn Johansen 1926c97204baSJohn Johansen dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry, 1927b7fd2c03SJohn Johansen NULL, &aa_fs_profile_remove); 1928b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 1929b7fd2c03SJohn Johansen error = PTR_ERR(dent); 1930b7fd2c03SJohn Johansen goto error; 1931b7fd2c03SJohn Johansen } 1932b7fd2c03SJohn Johansen ns_subremove(root_ns) = dent; 1933b7fd2c03SJohn Johansen 1934b7fd2c03SJohn Johansen mutex_lock(&root_ns->lock); 1935*c961ee5fSJohn Johansen error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy", 1936*c961ee5fSJohn Johansen aafs_mnt->mnt_root); 1937b7fd2c03SJohn Johansen mutex_unlock(&root_ns->lock); 1938b7fd2c03SJohn Johansen 19390d259f04SJohn Johansen if (error) 19400d259f04SJohn Johansen goto error; 19410d259f04SJohn Johansen 1942*c961ee5fSJohn Johansen /* magic symlink similar to nsfs redirects based on task policy */ 1943*c961ee5fSJohn Johansen dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry, 1944*c961ee5fSJohn Johansen NULL, &policy_link_iops); 1945*c961ee5fSJohn Johansen if (IS_ERR(dent)) { 1946*c961ee5fSJohn Johansen error = PTR_ERR(dent); 1947*c961ee5fSJohn Johansen goto error; 1948*c961ee5fSJohn Johansen } 1949*c961ee5fSJohn Johansen 1950c97204baSJohn Johansen error = aa_mk_null_file(aa_sfs_entry.dentry); 1951a71ada30SJohn Johansen if (error) 1952a71ada30SJohn Johansen goto error; 1953a71ada30SJohn Johansen 1954a71ada30SJohn Johansen /* TODO: add default profile to apparmorfs */ 195563e2b423SJohn Johansen 195663e2b423SJohn Johansen /* Report that AppArmor fs is enabled */ 195763e2b423SJohn Johansen aa_info_message("AppArmor Filesystem Enabled"); 195863e2b423SJohn Johansen return 0; 195963e2b423SJohn Johansen 196063e2b423SJohn Johansen error: 196163e2b423SJohn Johansen aa_destroy_aafs(); 196263e2b423SJohn Johansen AA_ERROR("Error creating AppArmor securityfs\n"); 196363e2b423SJohn Johansen return error; 196463e2b423SJohn Johansen } 196563e2b423SJohn Johansen 196663e2b423SJohn Johansen fs_initcall(aa_create_aafs); 1967