163e2b423SJohn Johansen /* 263e2b423SJohn Johansen * AppArmor security module 363e2b423SJohn Johansen * 463e2b423SJohn Johansen * This file contains AppArmor /sys/kernel/security/apparmor interface functions 563e2b423SJohn Johansen * 663e2b423SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 763e2b423SJohn Johansen * Copyright 2009-2010 Canonical Ltd. 863e2b423SJohn Johansen * 963e2b423SJohn Johansen * This program is free software; you can redistribute it and/or 1063e2b423SJohn Johansen * modify it under the terms of the GNU General Public License as 1163e2b423SJohn Johansen * published by the Free Software Foundation, version 2 of the 1263e2b423SJohn Johansen * License. 1363e2b423SJohn Johansen */ 1463e2b423SJohn Johansen 150d259f04SJohn Johansen #include <linux/ctype.h> 1663e2b423SJohn Johansen #include <linux/security.h> 1763e2b423SJohn Johansen #include <linux/vmalloc.h> 1863e2b423SJohn Johansen #include <linux/module.h> 1963e2b423SJohn Johansen #include <linux/seq_file.h> 2063e2b423SJohn Johansen #include <linux/uaccess.h> 21a71ada30SJohn Johansen #include <linux/mount.h> 2263e2b423SJohn Johansen #include <linux/namei.h> 23e74abcf3SKees Cook #include <linux/capability.h> 2429b3822fSJohn Johansen #include <linux/rcupdate.h> 25a71ada30SJohn Johansen #include <linux/fs.h> 26d9bf2c26SJohn Johansen #include <linux/poll.h> 27a481f4d9SJohn Johansen #include <uapi/linux/major.h> 28a481f4d9SJohn Johansen #include <uapi/linux/magic.h> 2963e2b423SJohn Johansen 3063e2b423SJohn Johansen #include "include/apparmor.h" 3163e2b423SJohn Johansen #include "include/apparmorfs.h" 3263e2b423SJohn Johansen #include "include/audit.h" 3363e2b423SJohn Johansen #include "include/context.h" 34f8eb8a13SJohn Johansen #include "include/crypto.h" 35d9bf2c26SJohn Johansen #include "include/policy_ns.h" 3663e2b423SJohn Johansen #include "include/policy.h" 37cff281f6SJohn Johansen #include "include/policy_ns.h" 38d384b0a1SKees Cook #include "include/resource.h" 395ac8c355SJohn Johansen #include "include/policy_unpack.h" 4063e2b423SJohn Johansen 41c97204baSJohn Johansen /* 42c97204baSJohn Johansen * The apparmor filesystem interface used for policy load and introspection 43c97204baSJohn Johansen * The interface is split into two main components based on their function 44c97204baSJohn Johansen * a securityfs component: 45c97204baSJohn Johansen * used for static files that are always available, and which allows 46c97204baSJohn Johansen * userspace to specificy the location of the security filesystem. 47c97204baSJohn Johansen * 48c97204baSJohn Johansen * fns and data are prefixed with 49c97204baSJohn Johansen * aa_sfs_ 50c97204baSJohn Johansen * 51c97204baSJohn Johansen * an apparmorfs component: 52c97204baSJohn Johansen * used loaded policy content and introspection. It is not part of a 53c97204baSJohn Johansen * regular mounted filesystem and is available only through the magic 54c97204baSJohn Johansen * policy symlink in the root of the securityfs apparmor/ directory. 55c97204baSJohn Johansen * Tasks queries will be magically redirected to the correct portion 56c97204baSJohn Johansen * of the policy tree based on their confinement. 57c97204baSJohn Johansen * 58c97204baSJohn Johansen * fns and data are prefixed with 59c97204baSJohn Johansen * aafs_ 60c97204baSJohn Johansen * 61c97204baSJohn Johansen * The aa_fs_ prefix is used to indicate the fn is used by both the 62c97204baSJohn Johansen * securityfs and apparmorfs filesystems. 63c97204baSJohn Johansen */ 64c97204baSJohn Johansen 65c97204baSJohn Johansen 66c97204baSJohn Johansen /* 67c97204baSJohn Johansen * support fns 68c97204baSJohn Johansen */ 69c97204baSJohn Johansen 7063e2b423SJohn Johansen /** 710d259f04SJohn Johansen * aa_mangle_name - mangle a profile name to std profile layout form 720d259f04SJohn Johansen * @name: profile name to mangle (NOT NULL) 730d259f04SJohn Johansen * @target: buffer to store mangled name, same length as @name (MAYBE NULL) 740d259f04SJohn Johansen * 750d259f04SJohn Johansen * Returns: length of mangled name 760d259f04SJohn Johansen */ 77bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target) 780d259f04SJohn Johansen { 790d259f04SJohn Johansen char *t = target; 800d259f04SJohn Johansen 810d259f04SJohn Johansen while (*name == '/' || *name == '.') 820d259f04SJohn Johansen name++; 830d259f04SJohn Johansen 840d259f04SJohn Johansen if (target) { 850d259f04SJohn Johansen for (; *name; name++) { 860d259f04SJohn Johansen if (*name == '/') 870d259f04SJohn Johansen *(t)++ = '.'; 880d259f04SJohn Johansen else if (isspace(*name)) 890d259f04SJohn Johansen *(t)++ = '_'; 900d259f04SJohn Johansen else if (isalnum(*name) || strchr("._-", *name)) 910d259f04SJohn Johansen *(t)++ = *name; 920d259f04SJohn Johansen } 930d259f04SJohn Johansen 940d259f04SJohn Johansen *t = 0; 950d259f04SJohn Johansen } else { 960d259f04SJohn Johansen int len = 0; 970d259f04SJohn Johansen for (; *name; name++) { 980d259f04SJohn Johansen if (isalnum(*name) || isspace(*name) || 990d259f04SJohn Johansen strchr("/._-", *name)) 1000d259f04SJohn Johansen len++; 1010d259f04SJohn Johansen } 1020d259f04SJohn Johansen 1030d259f04SJohn Johansen return len; 1040d259f04SJohn Johansen } 1050d259f04SJohn Johansen 1060d259f04SJohn Johansen return t - target; 1070d259f04SJohn Johansen } 1080d259f04SJohn Johansen 109a481f4d9SJohn Johansen 110a481f4d9SJohn Johansen /* 111a481f4d9SJohn Johansen * aafs - core fns and data for the policy tree 112a481f4d9SJohn Johansen */ 113a481f4d9SJohn Johansen 114a481f4d9SJohn Johansen #define AAFS_NAME "apparmorfs" 115a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt; 116a481f4d9SJohn Johansen static int aafs_count; 117a481f4d9SJohn Johansen 118a481f4d9SJohn Johansen 119a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry) 120a481f4d9SJohn Johansen { 121a481f4d9SJohn Johansen struct inode *inode = d_inode(dentry); 122a481f4d9SJohn Johansen 123a481f4d9SJohn Johansen seq_printf(seq, "%s:[%lu]", AAFS_NAME, inode->i_ino); 124a481f4d9SJohn Johansen return 0; 125a481f4d9SJohn Johansen } 126a481f4d9SJohn Johansen 127a481f4d9SJohn Johansen static void aafs_evict_inode(struct inode *inode) 128a481f4d9SJohn Johansen { 129a481f4d9SJohn Johansen truncate_inode_pages_final(&inode->i_data); 130a481f4d9SJohn Johansen clear_inode(inode); 131a481f4d9SJohn Johansen if (S_ISLNK(inode->i_mode)) 132a481f4d9SJohn Johansen kfree(inode->i_link); 133a481f4d9SJohn Johansen } 134a481f4d9SJohn Johansen 135a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = { 136a481f4d9SJohn Johansen .statfs = simple_statfs, 137a481f4d9SJohn Johansen .evict_inode = aafs_evict_inode, 138a481f4d9SJohn Johansen .show_path = aafs_show_path, 139a481f4d9SJohn Johansen }; 140a481f4d9SJohn Johansen 141a481f4d9SJohn Johansen static int fill_super(struct super_block *sb, void *data, int silent) 142a481f4d9SJohn Johansen { 143a481f4d9SJohn Johansen static struct tree_descr files[] = { {""} }; 144a481f4d9SJohn Johansen int error; 145a481f4d9SJohn Johansen 146a481f4d9SJohn Johansen error = simple_fill_super(sb, AAFS_MAGIC, files); 147a481f4d9SJohn Johansen if (error) 148a481f4d9SJohn Johansen return error; 149a481f4d9SJohn Johansen sb->s_op = &aafs_super_ops; 150a481f4d9SJohn Johansen 151a481f4d9SJohn Johansen return 0; 152a481f4d9SJohn Johansen } 153a481f4d9SJohn Johansen 154a481f4d9SJohn Johansen static struct dentry *aafs_mount(struct file_system_type *fs_type, 155a481f4d9SJohn Johansen int flags, const char *dev_name, void *data) 156a481f4d9SJohn Johansen { 157a481f4d9SJohn Johansen return mount_single(fs_type, flags, data, fill_super); 158a481f4d9SJohn Johansen } 159a481f4d9SJohn Johansen 160a481f4d9SJohn Johansen static struct file_system_type aafs_ops = { 161a481f4d9SJohn Johansen .owner = THIS_MODULE, 162a481f4d9SJohn Johansen .name = AAFS_NAME, 163a481f4d9SJohn Johansen .mount = aafs_mount, 164a481f4d9SJohn Johansen .kill_sb = kill_anon_super, 165a481f4d9SJohn Johansen }; 166a481f4d9SJohn Johansen 167a481f4d9SJohn Johansen /** 168a481f4d9SJohn Johansen * __aafs_setup_d_inode - basic inode setup for apparmorfs 169a481f4d9SJohn Johansen * @dir: parent directory for the dentry 170a481f4d9SJohn Johansen * @dentry: dentry we are seting the inode up for 171a481f4d9SJohn Johansen * @mode: permissions the file should have 172a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 173a481f4d9SJohn Johansen * @link: if symlink, symlink target string 174a481f4d9SJohn Johansen * @fops: struct file_operations that should be used 175a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 176a481f4d9SJohn Johansen */ 177a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry, 178a481f4d9SJohn Johansen umode_t mode, void *data, char *link, 179a481f4d9SJohn Johansen const struct file_operations *fops, 180a481f4d9SJohn Johansen const struct inode_operations *iops) 181a481f4d9SJohn Johansen { 182a481f4d9SJohn Johansen struct inode *inode = new_inode(dir->i_sb); 183a481f4d9SJohn Johansen 184a481f4d9SJohn Johansen AA_BUG(!dir); 185a481f4d9SJohn Johansen AA_BUG(!dentry); 186a481f4d9SJohn Johansen 187a481f4d9SJohn Johansen if (!inode) 188a481f4d9SJohn Johansen return -ENOMEM; 189a481f4d9SJohn Johansen 190a481f4d9SJohn Johansen inode->i_ino = get_next_ino(); 191a481f4d9SJohn Johansen inode->i_mode = mode; 192a481f4d9SJohn Johansen inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 193a481f4d9SJohn Johansen inode->i_private = data; 194a481f4d9SJohn Johansen if (S_ISDIR(mode)) { 195a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_dir_inode_operations; 196a481f4d9SJohn Johansen inode->i_fop = &simple_dir_operations; 197a481f4d9SJohn Johansen inc_nlink(inode); 198a481f4d9SJohn Johansen inc_nlink(dir); 199a481f4d9SJohn Johansen } else if (S_ISLNK(mode)) { 200a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_symlink_inode_operations; 201a481f4d9SJohn Johansen inode->i_link = link; 202a481f4d9SJohn Johansen } else { 203a481f4d9SJohn Johansen inode->i_fop = fops; 204a481f4d9SJohn Johansen } 205a481f4d9SJohn Johansen d_instantiate(dentry, inode); 206a481f4d9SJohn Johansen dget(dentry); 207a481f4d9SJohn Johansen 208a481f4d9SJohn Johansen return 0; 209a481f4d9SJohn Johansen } 210a481f4d9SJohn Johansen 211a481f4d9SJohn Johansen /** 212a481f4d9SJohn Johansen * aafs_create - create a dentry in the apparmorfs filesystem 213a481f4d9SJohn Johansen * 214a481f4d9SJohn Johansen * @name: name of dentry to create 215a481f4d9SJohn Johansen * @mode: permissions the file should have 216a481f4d9SJohn Johansen * @parent: parent directory for this dentry 217a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 218a481f4d9SJohn Johansen * @link: if symlink, symlink target string 219a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 220a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 221a481f4d9SJohn Johansen * 222a481f4d9SJohn Johansen * This is the basic "create a xxx" function for apparmorfs. 223a481f4d9SJohn Johansen * 224a481f4d9SJohn Johansen * Returns a pointer to a dentry if it succeeds, that must be free with 225a481f4d9SJohn Johansen * aafs_remove(). Will return ERR_PTR on failure. 226a481f4d9SJohn Johansen */ 227a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode, 228a481f4d9SJohn Johansen struct dentry *parent, void *data, void *link, 229a481f4d9SJohn Johansen const struct file_operations *fops, 230a481f4d9SJohn Johansen const struct inode_operations *iops) 231a481f4d9SJohn Johansen { 232a481f4d9SJohn Johansen struct dentry *dentry; 233a481f4d9SJohn Johansen struct inode *dir; 234a481f4d9SJohn Johansen int error; 235a481f4d9SJohn Johansen 236a481f4d9SJohn Johansen AA_BUG(!name); 237a481f4d9SJohn Johansen AA_BUG(!parent); 238a481f4d9SJohn Johansen 239a481f4d9SJohn Johansen if (!(mode & S_IFMT)) 240a481f4d9SJohn Johansen mode = (mode & S_IALLUGO) | S_IFREG; 241a481f4d9SJohn Johansen 242a481f4d9SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 243a481f4d9SJohn Johansen if (error) 244a481f4d9SJohn Johansen return ERR_PTR(error); 245a481f4d9SJohn Johansen 246a481f4d9SJohn Johansen dir = d_inode(parent); 247a481f4d9SJohn Johansen 248a481f4d9SJohn Johansen inode_lock(dir); 249a481f4d9SJohn Johansen dentry = lookup_one_len(name, parent, strlen(name)); 250a481f4d9SJohn Johansen if (IS_ERR(dentry)) 251a481f4d9SJohn Johansen goto fail_lock; 252a481f4d9SJohn Johansen 253a481f4d9SJohn Johansen if (d_really_is_positive(dentry)) { 254a481f4d9SJohn Johansen error = -EEXIST; 255a481f4d9SJohn Johansen goto fail_dentry; 256a481f4d9SJohn Johansen } 257a481f4d9SJohn Johansen 258a481f4d9SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops); 259a481f4d9SJohn Johansen if (error) 260a481f4d9SJohn Johansen goto fail_dentry; 261a481f4d9SJohn Johansen inode_unlock(dir); 262a481f4d9SJohn Johansen 263a481f4d9SJohn Johansen return dentry; 264a481f4d9SJohn Johansen 265a481f4d9SJohn Johansen fail_dentry: 266a481f4d9SJohn Johansen dput(dentry); 267a481f4d9SJohn Johansen 268a481f4d9SJohn Johansen fail_lock: 269a481f4d9SJohn Johansen inode_unlock(dir); 270a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 271a481f4d9SJohn Johansen 272a481f4d9SJohn Johansen return ERR_PTR(error); 273a481f4d9SJohn Johansen } 274a481f4d9SJohn Johansen 275a481f4d9SJohn Johansen /** 276a481f4d9SJohn Johansen * aafs_create_file - create a file in the apparmorfs filesystem 277a481f4d9SJohn Johansen * 278a481f4d9SJohn Johansen * @name: name of dentry to create 279a481f4d9SJohn Johansen * @mode: permissions the file should have 280a481f4d9SJohn Johansen * @parent: parent directory for this dentry 281a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 282a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 283a481f4d9SJohn Johansen * 284a481f4d9SJohn Johansen * see aafs_create 285a481f4d9SJohn Johansen */ 286a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode, 287a481f4d9SJohn Johansen struct dentry *parent, void *data, 288a481f4d9SJohn Johansen const struct file_operations *fops) 289a481f4d9SJohn Johansen { 290a481f4d9SJohn Johansen return aafs_create(name, mode, parent, data, NULL, fops, NULL); 291a481f4d9SJohn Johansen } 292a481f4d9SJohn Johansen 293a481f4d9SJohn Johansen /** 294a481f4d9SJohn Johansen * aafs_create_dir - create a directory in the apparmorfs filesystem 295a481f4d9SJohn Johansen * 296a481f4d9SJohn Johansen * @name: name of dentry to create 297a481f4d9SJohn Johansen * @parent: parent directory for this dentry 298a481f4d9SJohn Johansen * 299a481f4d9SJohn Johansen * see aafs_create 300a481f4d9SJohn Johansen */ 301a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent) 302a481f4d9SJohn Johansen { 303a481f4d9SJohn Johansen return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL, 304a481f4d9SJohn Johansen NULL); 305a481f4d9SJohn Johansen } 306a481f4d9SJohn Johansen 307a481f4d9SJohn Johansen /** 308a481f4d9SJohn Johansen * aafs_create_symlink - create a symlink in the apparmorfs filesystem 309a481f4d9SJohn Johansen * @name: name of dentry to create 310a481f4d9SJohn Johansen * @parent: parent directory for this dentry 311a481f4d9SJohn Johansen * @target: if symlink, symlink target string 312a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 313a481f4d9SJohn Johansen * 314a481f4d9SJohn Johansen * If @target parameter is %NULL, then the @iops parameter needs to be 315a481f4d9SJohn Johansen * setup to handle .readlink and .get_link inode_operations. 316a481f4d9SJohn Johansen */ 317a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name, 318a481f4d9SJohn Johansen struct dentry *parent, 319a481f4d9SJohn Johansen const char *target, 320a481f4d9SJohn Johansen const struct inode_operations *iops) 321a481f4d9SJohn Johansen { 322a481f4d9SJohn Johansen struct dentry *dent; 323a481f4d9SJohn Johansen char *link = NULL; 324a481f4d9SJohn Johansen 325a481f4d9SJohn Johansen if (target) { 326a481f4d9SJohn Johansen link = kstrdup(target, GFP_KERNEL); 327a481f4d9SJohn Johansen if (!link) 328a481f4d9SJohn Johansen return ERR_PTR(-ENOMEM); 329a481f4d9SJohn Johansen } 330a481f4d9SJohn Johansen dent = aafs_create(name, S_IFLNK | 0444, parent, NULL, link, NULL, 331a481f4d9SJohn Johansen iops); 332a481f4d9SJohn Johansen if (IS_ERR(dent)) 333a481f4d9SJohn Johansen kfree(link); 334a481f4d9SJohn Johansen 335a481f4d9SJohn Johansen return dent; 336a481f4d9SJohn Johansen } 337a481f4d9SJohn Johansen 338a481f4d9SJohn Johansen /** 339a481f4d9SJohn Johansen * aafs_remove - removes a file or directory from the apparmorfs filesystem 340a481f4d9SJohn Johansen * 341a481f4d9SJohn Johansen * @dentry: dentry of the file/directory/symlink to removed. 342a481f4d9SJohn Johansen */ 343a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry) 344a481f4d9SJohn Johansen { 345a481f4d9SJohn Johansen struct inode *dir; 346a481f4d9SJohn Johansen 347a481f4d9SJohn Johansen if (!dentry || IS_ERR(dentry)) 348a481f4d9SJohn Johansen return; 349a481f4d9SJohn Johansen 350a481f4d9SJohn Johansen dir = d_inode(dentry->d_parent); 351a481f4d9SJohn Johansen inode_lock(dir); 352a481f4d9SJohn Johansen if (simple_positive(dentry)) { 353a481f4d9SJohn Johansen if (d_is_dir(dentry)) 354a481f4d9SJohn Johansen simple_rmdir(dir, dentry); 355a481f4d9SJohn Johansen else 356a481f4d9SJohn Johansen simple_unlink(dir, dentry); 357a481f4d9SJohn Johansen dput(dentry); 358a481f4d9SJohn Johansen } 359a481f4d9SJohn Johansen inode_unlock(dir); 360a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 361a481f4d9SJohn Johansen } 362a481f4d9SJohn Johansen 363a481f4d9SJohn Johansen 364a481f4d9SJohn Johansen /* 365a481f4d9SJohn Johansen * aa_fs - policy load/replace/remove 366a481f4d9SJohn Johansen */ 367a481f4d9SJohn Johansen 3680d259f04SJohn Johansen /** 36963e2b423SJohn Johansen * aa_simple_write_to_buffer - common routine for getting policy from user 37063e2b423SJohn Johansen * @userbuf: user buffer to copy data from (NOT NULL) 3713ed02adaSJohn Johansen * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 37263e2b423SJohn Johansen * @copy_size: size of data to copy from user buffer 37363e2b423SJohn Johansen * @pos: position write is at in the file (NOT NULL) 37463e2b423SJohn Johansen * 37563e2b423SJohn Johansen * Returns: kernel buffer containing copy of user buffer data or an 37663e2b423SJohn Johansen * ERR_PTR on failure. 37763e2b423SJohn Johansen */ 3785ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, 3795ac8c355SJohn Johansen size_t alloc_size, 3805ac8c355SJohn Johansen size_t copy_size, 38163e2b423SJohn Johansen loff_t *pos) 38263e2b423SJohn Johansen { 3835ac8c355SJohn Johansen struct aa_loaddata *data; 38463e2b423SJohn Johansen 385e6bfa25dSJohn Johansen AA_BUG(copy_size > alloc_size); 3863ed02adaSJohn Johansen 38763e2b423SJohn Johansen if (*pos != 0) 38863e2b423SJohn Johansen /* only writes from pos 0, that is complete writes */ 38963e2b423SJohn Johansen return ERR_PTR(-ESPIPE); 39063e2b423SJohn Johansen 39163e2b423SJohn Johansen /* freed by caller to simple_write_to_buffer */ 3925d5182caSJohn Johansen data = aa_loaddata_alloc(alloc_size); 3935d5182caSJohn Johansen if (IS_ERR(data)) 3945d5182caSJohn Johansen return data; 39563e2b423SJohn Johansen 3965d5182caSJohn Johansen data->size = copy_size; 3975ac8c355SJohn Johansen if (copy_from_user(data->data, userbuf, copy_size)) { 39863e2b423SJohn Johansen kvfree(data); 39963e2b423SJohn Johansen return ERR_PTR(-EFAULT); 40063e2b423SJohn Johansen } 40163e2b423SJohn Johansen 40263e2b423SJohn Johansen return data; 40363e2b423SJohn Johansen } 40463e2b423SJohn Johansen 40518e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size, 406b7fd2c03SJohn Johansen loff_t *pos, struct aa_ns *ns) 4075ac8c355SJohn Johansen { 4085ac8c355SJohn Johansen ssize_t error; 4095ac8c355SJohn Johansen struct aa_loaddata *data; 4105ac8c355SJohn Johansen struct aa_profile *profile = aa_current_profile(); 4115ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4125ac8c355SJohn Johansen * below after unpack 4135ac8c355SJohn Johansen */ 41418e99f19SJohn Johansen error = aa_may_manage_policy(profile, ns, mask); 4155ac8c355SJohn Johansen if (error) 4165ac8c355SJohn Johansen return error; 41763e2b423SJohn Johansen 4185ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size, size, pos); 4195ac8c355SJohn Johansen error = PTR_ERR(data); 4205ac8c355SJohn Johansen if (!IS_ERR(data)) { 421b7fd2c03SJohn Johansen error = aa_replace_profiles(ns ? ns : profile->ns, profile, 42218e99f19SJohn Johansen mask, data); 4235ac8c355SJohn Johansen aa_put_loaddata(data); 4245ac8c355SJohn Johansen } 4255ac8c355SJohn Johansen 4265ac8c355SJohn Johansen return error; 4275ac8c355SJohn Johansen } 4285ac8c355SJohn Johansen 429b7fd2c03SJohn Johansen /* .load file hook fn to load policy */ 43063e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 43163e2b423SJohn Johansen loff_t *pos) 43263e2b423SJohn Johansen { 433b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 43418e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns); 435b7fd2c03SJohn Johansen 436b7fd2c03SJohn Johansen aa_put_ns(ns); 43763e2b423SJohn Johansen 43863e2b423SJohn Johansen return error; 43963e2b423SJohn Johansen } 44063e2b423SJohn Johansen 44163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = { 4426038f373SArnd Bergmann .write = profile_load, 4436038f373SArnd Bergmann .llseek = default_llseek, 44463e2b423SJohn Johansen }; 44563e2b423SJohn Johansen 44663e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */ 44763e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf, 44863e2b423SJohn Johansen size_t size, loff_t *pos) 44963e2b423SJohn Johansen { 450b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 45118e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY, 45218e99f19SJohn Johansen buf, size, pos, ns); 453b7fd2c03SJohn Johansen aa_put_ns(ns); 45463e2b423SJohn Johansen 45563e2b423SJohn Johansen return error; 45663e2b423SJohn Johansen } 45763e2b423SJohn Johansen 45863e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = { 4596038f373SArnd Bergmann .write = profile_replace, 4606038f373SArnd Bergmann .llseek = default_llseek, 46163e2b423SJohn Johansen }; 46263e2b423SJohn Johansen 463b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */ 46463e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf, 46563e2b423SJohn Johansen size_t size, loff_t *pos) 46663e2b423SJohn Johansen { 4675ac8c355SJohn Johansen struct aa_loaddata *data; 4685ac8c355SJohn Johansen struct aa_profile *profile; 46963e2b423SJohn Johansen ssize_t error; 470b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 47163e2b423SJohn Johansen 4725ac8c355SJohn Johansen profile = aa_current_profile(); 4735ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4745ac8c355SJohn Johansen * below after unpack 4755ac8c355SJohn Johansen */ 47618e99f19SJohn Johansen error = aa_may_manage_policy(profile, ns, AA_MAY_REMOVE_POLICY); 4775ac8c355SJohn Johansen if (error) 4785ac8c355SJohn Johansen goto out; 4795ac8c355SJohn Johansen 48063e2b423SJohn Johansen /* 48163e2b423SJohn Johansen * aa_remove_profile needs a null terminated string so 1 extra 48263e2b423SJohn Johansen * byte is allocated and the copied data is null terminated. 48363e2b423SJohn Johansen */ 4845ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size + 1, size, pos); 48563e2b423SJohn Johansen 48663e2b423SJohn Johansen error = PTR_ERR(data); 48763e2b423SJohn Johansen if (!IS_ERR(data)) { 4885ac8c355SJohn Johansen data->data[size] = 0; 489b7fd2c03SJohn Johansen error = aa_remove_profiles(ns ? ns : profile->ns, profile, 490b7fd2c03SJohn Johansen data->data, size); 4915ac8c355SJohn Johansen aa_put_loaddata(data); 49263e2b423SJohn Johansen } 4935ac8c355SJohn Johansen out: 494b7fd2c03SJohn Johansen aa_put_ns(ns); 49563e2b423SJohn Johansen return error; 49663e2b423SJohn Johansen } 49763e2b423SJohn Johansen 49863e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = { 4996038f373SArnd Bergmann .write = profile_remove, 5006038f373SArnd Bergmann .llseek = default_llseek, 50163e2b423SJohn Johansen }; 50263e2b423SJohn Johansen 503d9bf2c26SJohn Johansen struct aa_revision { 504d9bf2c26SJohn Johansen struct aa_ns *ns; 505d9bf2c26SJohn Johansen long last_read; 506d9bf2c26SJohn Johansen }; 507d9bf2c26SJohn Johansen 508d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */ 509d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file) 510d9bf2c26SJohn Johansen { 511d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 512d9bf2c26SJohn Johansen 513d9bf2c26SJohn Johansen if (rev) { 514d9bf2c26SJohn Johansen aa_put_ns(rev->ns); 515d9bf2c26SJohn Johansen kfree(rev); 516d9bf2c26SJohn Johansen } 517d9bf2c26SJohn Johansen 518d9bf2c26SJohn Johansen return 0; 519d9bf2c26SJohn Johansen } 520d9bf2c26SJohn Johansen 521d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf, 522d9bf2c26SJohn Johansen size_t size, loff_t *ppos) 523d9bf2c26SJohn Johansen { 524d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 525d9bf2c26SJohn Johansen char buffer[32]; 526d9bf2c26SJohn Johansen long last_read; 527d9bf2c26SJohn Johansen int avail; 528d9bf2c26SJohn Johansen 529d9bf2c26SJohn Johansen mutex_lock(&rev->ns->lock); 530d9bf2c26SJohn Johansen last_read = rev->last_read; 531d9bf2c26SJohn Johansen if (last_read == rev->ns->revision) { 532d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 533d9bf2c26SJohn Johansen if (file->f_flags & O_NONBLOCK) 534d9bf2c26SJohn Johansen return -EAGAIN; 535d9bf2c26SJohn Johansen if (wait_event_interruptible(rev->ns->wait, 536d9bf2c26SJohn Johansen last_read != 537d9bf2c26SJohn Johansen READ_ONCE(rev->ns->revision))) 538d9bf2c26SJohn Johansen return -ERESTARTSYS; 539d9bf2c26SJohn Johansen mutex_lock(&rev->ns->lock); 540d9bf2c26SJohn Johansen } 541d9bf2c26SJohn Johansen 542d9bf2c26SJohn Johansen avail = sprintf(buffer, "%ld\n", rev->ns->revision); 543d9bf2c26SJohn Johansen if (*ppos + size > avail) { 544d9bf2c26SJohn Johansen rev->last_read = rev->ns->revision; 545d9bf2c26SJohn Johansen *ppos = 0; 546d9bf2c26SJohn Johansen } 547d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 548d9bf2c26SJohn Johansen 549d9bf2c26SJohn Johansen return simple_read_from_buffer(buf, size, ppos, buffer, avail); 550d9bf2c26SJohn Johansen } 551d9bf2c26SJohn Johansen 552d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file) 553d9bf2c26SJohn Johansen { 554d9bf2c26SJohn Johansen struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL); 555d9bf2c26SJohn Johansen 556d9bf2c26SJohn Johansen if (!rev) 557d9bf2c26SJohn Johansen return -ENOMEM; 558d9bf2c26SJohn Johansen 559d9bf2c26SJohn Johansen rev->ns = aa_get_ns(inode->i_private); 560d9bf2c26SJohn Johansen if (!rev->ns) 561d9bf2c26SJohn Johansen rev->ns = aa_get_current_ns(); 562d9bf2c26SJohn Johansen file->private_data = rev; 563d9bf2c26SJohn Johansen 564d9bf2c26SJohn Johansen return 0; 565d9bf2c26SJohn Johansen } 566d9bf2c26SJohn Johansen 567d9bf2c26SJohn Johansen static unsigned int ns_revision_poll(struct file *file, poll_table *pt) 568d9bf2c26SJohn Johansen { 569d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 570d9bf2c26SJohn Johansen unsigned int mask = 0; 571d9bf2c26SJohn Johansen 572d9bf2c26SJohn Johansen if (rev) { 573d9bf2c26SJohn Johansen mutex_lock(&rev->ns->lock); 574d9bf2c26SJohn Johansen poll_wait(file, &rev->ns->wait, pt); 575d9bf2c26SJohn Johansen if (rev->last_read < rev->ns->revision) 576d9bf2c26SJohn Johansen mask |= POLLIN | POLLRDNORM; 577d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 578d9bf2c26SJohn Johansen } 579d9bf2c26SJohn Johansen 580d9bf2c26SJohn Johansen return mask; 581d9bf2c26SJohn Johansen } 582d9bf2c26SJohn Johansen 5835d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns) 5845d5182caSJohn Johansen { 5855d5182caSJohn Johansen ns->revision++; 586d9bf2c26SJohn Johansen wake_up_interruptible(&ns->wait); 5875d5182caSJohn Johansen } 5885d5182caSJohn Johansen 589d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = { 590d9bf2c26SJohn Johansen .owner = THIS_MODULE, 591d9bf2c26SJohn Johansen .open = ns_revision_open, 592d9bf2c26SJohn Johansen .poll = ns_revision_poll, 593d9bf2c26SJohn Johansen .read = ns_revision_read, 594d9bf2c26SJohn Johansen .llseek = generic_file_llseek, 595d9bf2c26SJohn Johansen .release = ns_revision_release, 596d9bf2c26SJohn Johansen }; 597d9bf2c26SJohn Johansen 598e025be0fSWilliam Hua /** 599e025be0fSWilliam Hua * query_data - queries a policy and writes its data to buf 600e025be0fSWilliam Hua * @buf: the resulting data is stored here (NOT NULL) 601e025be0fSWilliam Hua * @buf_len: size of buf 602e025be0fSWilliam Hua * @query: query string used to retrieve data 603e025be0fSWilliam Hua * @query_len: size of query including second NUL byte 604e025be0fSWilliam Hua * 605e025be0fSWilliam Hua * The buffers pointed to by buf and query may overlap. The query buffer is 606e025be0fSWilliam Hua * parsed before buf is written to. 607e025be0fSWilliam Hua * 608e025be0fSWilliam Hua * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of 609e025be0fSWilliam Hua * the security confinement context and <KEY> is the name of the data to 610e025be0fSWilliam Hua * retrieve. <LABEL> and <KEY> must not be NUL-terminated. 611e025be0fSWilliam Hua * 612e025be0fSWilliam Hua * Don't expect the contents of buf to be preserved on failure. 613e025be0fSWilliam Hua * 614e025be0fSWilliam Hua * Returns: number of characters written to buf or -errno on failure 615e025be0fSWilliam Hua */ 616e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len, 617e025be0fSWilliam Hua char *query, size_t query_len) 618e025be0fSWilliam Hua { 619e025be0fSWilliam Hua char *out; 620e025be0fSWilliam Hua const char *key; 621e025be0fSWilliam Hua struct aa_profile *profile; 622e025be0fSWilliam Hua struct aa_data *data; 623e025be0fSWilliam Hua u32 bytes, blocks; 624e025be0fSWilliam Hua __le32 outle32; 625e025be0fSWilliam Hua 626e025be0fSWilliam Hua if (!query_len) 627e025be0fSWilliam Hua return -EINVAL; /* need a query */ 628e025be0fSWilliam Hua 629e025be0fSWilliam Hua key = query + strnlen(query, query_len) + 1; 630e025be0fSWilliam Hua if (key + 1 >= query + query_len) 631e025be0fSWilliam Hua return -EINVAL; /* not enough space for a non-empty key */ 632e025be0fSWilliam Hua if (key + strnlen(key, query + query_len - key) >= query + query_len) 633e025be0fSWilliam Hua return -EINVAL; /* must end with NUL */ 634e025be0fSWilliam Hua 635e025be0fSWilliam Hua if (buf_len < sizeof(bytes) + sizeof(blocks)) 636e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 637e025be0fSWilliam Hua 638e025be0fSWilliam Hua profile = aa_current_profile(); 639e025be0fSWilliam Hua 640e025be0fSWilliam Hua /* We are going to leave space for two numbers. The first is the total 641e025be0fSWilliam Hua * number of bytes we are writing after the first number. This is so 642e025be0fSWilliam Hua * users can read the full output without reallocation. 643e025be0fSWilliam Hua * 644e025be0fSWilliam Hua * The second number is the number of data blocks we're writing. An 645e025be0fSWilliam Hua * application might be confined by multiple policies having data in 646e025be0fSWilliam Hua * the same key. 647e025be0fSWilliam Hua */ 648e025be0fSWilliam Hua memset(buf, 0, sizeof(bytes) + sizeof(blocks)); 649e025be0fSWilliam Hua out = buf + sizeof(bytes) + sizeof(blocks); 650e025be0fSWilliam Hua 651e025be0fSWilliam Hua blocks = 0; 652e025be0fSWilliam Hua if (profile->data) { 653e025be0fSWilliam Hua data = rhashtable_lookup_fast(profile->data, &key, 654e025be0fSWilliam Hua profile->data->p); 655e025be0fSWilliam Hua 656e025be0fSWilliam Hua if (data) { 657e025be0fSWilliam Hua if (out + sizeof(outle32) + data->size > buf + buf_len) 658e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 659e025be0fSWilliam Hua outle32 = __cpu_to_le32(data->size); 660e025be0fSWilliam Hua memcpy(out, &outle32, sizeof(outle32)); 661e025be0fSWilliam Hua out += sizeof(outle32); 662e025be0fSWilliam Hua memcpy(out, data->data, data->size); 663e025be0fSWilliam Hua out += data->size; 664e025be0fSWilliam Hua blocks++; 665e025be0fSWilliam Hua } 666e025be0fSWilliam Hua } 667e025be0fSWilliam Hua 668e025be0fSWilliam Hua outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); 669e025be0fSWilliam Hua memcpy(buf, &outle32, sizeof(outle32)); 670e025be0fSWilliam Hua outle32 = __cpu_to_le32(blocks); 671e025be0fSWilliam Hua memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32)); 672e025be0fSWilliam Hua 673e025be0fSWilliam Hua return out - buf; 674e025be0fSWilliam Hua } 675e025be0fSWilliam Hua 676*1dea3b41SJohn Johansen /* 677*1dea3b41SJohn Johansen * Transaction based IO. 678*1dea3b41SJohn Johansen * The file expects a write which triggers the transaction, and then 679*1dea3b41SJohn Johansen * possibly a read(s) which collects the result - which is stored in a 680*1dea3b41SJohn Johansen * file-local buffer. Once a new write is performed, a new set of results 681*1dea3b41SJohn Johansen * are stored in the file-local buffer. 682*1dea3b41SJohn Johansen */ 683*1dea3b41SJohn Johansen struct multi_transaction { 684*1dea3b41SJohn Johansen struct kref count; 685*1dea3b41SJohn Johansen ssize_t size; 686*1dea3b41SJohn Johansen char data[0]; 687*1dea3b41SJohn Johansen }; 688*1dea3b41SJohn Johansen 689*1dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction)) 690*1dea3b41SJohn Johansen /* TODO: replace with per file lock */ 691*1dea3b41SJohn Johansen static DEFINE_SPINLOCK(multi_transaction_lock); 692*1dea3b41SJohn Johansen 693*1dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref) 694*1dea3b41SJohn Johansen { 695*1dea3b41SJohn Johansen struct multi_transaction *t; 696*1dea3b41SJohn Johansen 697*1dea3b41SJohn Johansen t = container_of(kref, struct multi_transaction, count); 698*1dea3b41SJohn Johansen free_page((unsigned long) t); 699*1dea3b41SJohn Johansen } 700*1dea3b41SJohn Johansen 701*1dea3b41SJohn Johansen static struct multi_transaction * 702*1dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t) 703*1dea3b41SJohn Johansen { 704*1dea3b41SJohn Johansen if (t) 705*1dea3b41SJohn Johansen kref_get(&(t->count)); 706*1dea3b41SJohn Johansen 707*1dea3b41SJohn Johansen return t; 708*1dea3b41SJohn Johansen } 709*1dea3b41SJohn Johansen 710*1dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t) 711*1dea3b41SJohn Johansen { 712*1dea3b41SJohn Johansen if (t) 713*1dea3b41SJohn Johansen kref_put(&(t->count), multi_transaction_kref); 714*1dea3b41SJohn Johansen } 715*1dea3b41SJohn Johansen 716*1dea3b41SJohn Johansen /* does not increment @new's count */ 717*1dea3b41SJohn Johansen static void multi_transaction_set(struct file *file, 718*1dea3b41SJohn Johansen struct multi_transaction *new, size_t n) 719*1dea3b41SJohn Johansen { 720*1dea3b41SJohn Johansen struct multi_transaction *old; 721*1dea3b41SJohn Johansen 722*1dea3b41SJohn Johansen AA_BUG(n > MULTI_TRANSACTION_LIMIT); 723*1dea3b41SJohn Johansen 724*1dea3b41SJohn Johansen new->size = n; 725*1dea3b41SJohn Johansen spin_lock(&multi_transaction_lock); 726*1dea3b41SJohn Johansen old = (struct multi_transaction *) file->private_data; 727*1dea3b41SJohn Johansen file->private_data = new; 728*1dea3b41SJohn Johansen spin_unlock(&multi_transaction_lock); 729*1dea3b41SJohn Johansen put_multi_transaction(old); 730*1dea3b41SJohn Johansen } 731*1dea3b41SJohn Johansen 732*1dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file, 733*1dea3b41SJohn Johansen const char __user *buf, 734*1dea3b41SJohn Johansen size_t size) 735*1dea3b41SJohn Johansen { 736*1dea3b41SJohn Johansen struct multi_transaction *t; 737*1dea3b41SJohn Johansen 738*1dea3b41SJohn Johansen if (size > MULTI_TRANSACTION_LIMIT - 1) 739*1dea3b41SJohn Johansen return ERR_PTR(-EFBIG); 740*1dea3b41SJohn Johansen 741*1dea3b41SJohn Johansen t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL); 742*1dea3b41SJohn Johansen if (!t) 743*1dea3b41SJohn Johansen return ERR_PTR(-ENOMEM); 744*1dea3b41SJohn Johansen kref_init(&t->count); 745*1dea3b41SJohn Johansen if (copy_from_user(t->data, buf, size)) 746*1dea3b41SJohn Johansen return ERR_PTR(-EFAULT); 747*1dea3b41SJohn Johansen 748*1dea3b41SJohn Johansen return t; 749*1dea3b41SJohn Johansen } 750*1dea3b41SJohn Johansen 751*1dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf, 752*1dea3b41SJohn Johansen size_t size, loff_t *pos) 753*1dea3b41SJohn Johansen { 754*1dea3b41SJohn Johansen struct multi_transaction *t; 755*1dea3b41SJohn Johansen ssize_t ret; 756*1dea3b41SJohn Johansen 757*1dea3b41SJohn Johansen spin_lock(&multi_transaction_lock); 758*1dea3b41SJohn Johansen t = get_multi_transaction(file->private_data); 759*1dea3b41SJohn Johansen spin_unlock(&multi_transaction_lock); 760*1dea3b41SJohn Johansen if (!t) 761*1dea3b41SJohn Johansen return 0; 762*1dea3b41SJohn Johansen 763*1dea3b41SJohn Johansen ret = simple_read_from_buffer(buf, size, pos, t->data, t->size); 764*1dea3b41SJohn Johansen put_multi_transaction(t); 765*1dea3b41SJohn Johansen 766*1dea3b41SJohn Johansen return ret; 767*1dea3b41SJohn Johansen } 768*1dea3b41SJohn Johansen 769*1dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file) 770*1dea3b41SJohn Johansen { 771*1dea3b41SJohn Johansen put_multi_transaction(file->private_data); 772*1dea3b41SJohn Johansen 773*1dea3b41SJohn Johansen return 0; 774*1dea3b41SJohn Johansen } 775*1dea3b41SJohn Johansen 776e025be0fSWilliam Hua #define QUERY_CMD_DATA "data\0" 777e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN 5 778e025be0fSWilliam Hua 779e025be0fSWilliam Hua /** 780e025be0fSWilliam Hua * aa_write_access - generic permissions and data query 781e025be0fSWilliam Hua * @file: pointer to open apparmorfs/access file 782e025be0fSWilliam Hua * @ubuf: user buffer containing the complete query string (NOT NULL) 783e025be0fSWilliam Hua * @count: size of ubuf 784e025be0fSWilliam Hua * @ppos: position in the file (MUST BE ZERO) 785e025be0fSWilliam Hua * 786e025be0fSWilliam Hua * Allows for one permissions or data query per open(), write(), and read() 787e025be0fSWilliam Hua * sequence. The only queries currently supported are label-based queries for 788e025be0fSWilliam Hua * permissions or data. 789e025be0fSWilliam Hua * 790e025be0fSWilliam Hua * For permissions queries, ubuf must begin with "label\0", followed by the 791e025be0fSWilliam Hua * profile query specific format described in the query_label() function 792e025be0fSWilliam Hua * documentation. 793e025be0fSWilliam Hua * 794e025be0fSWilliam Hua * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where 795e025be0fSWilliam Hua * <LABEL> is the name of the security confinement context and <KEY> is the 796e025be0fSWilliam Hua * name of the data to retrieve. 797e025be0fSWilliam Hua * 798e025be0fSWilliam Hua * Returns: number of bytes written or -errno on failure 799e025be0fSWilliam Hua */ 800e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf, 801e025be0fSWilliam Hua size_t count, loff_t *ppos) 802e025be0fSWilliam Hua { 803*1dea3b41SJohn Johansen struct multi_transaction *t; 804e025be0fSWilliam Hua ssize_t len; 805e025be0fSWilliam Hua 806e025be0fSWilliam Hua if (*ppos) 807e025be0fSWilliam Hua return -ESPIPE; 808e025be0fSWilliam Hua 809*1dea3b41SJohn Johansen t = multi_transaction_new(file, ubuf, count); 810*1dea3b41SJohn Johansen if (IS_ERR(t)) 811*1dea3b41SJohn Johansen return PTR_ERR(t); 812e025be0fSWilliam Hua 813e025be0fSWilliam Hua if (count > QUERY_CMD_DATA_LEN && 814*1dea3b41SJohn Johansen !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { 815*1dea3b41SJohn Johansen len = query_data(t->data, MULTI_TRANSACTION_LIMIT, 816*1dea3b41SJohn Johansen t->data + QUERY_CMD_DATA_LEN, 817e025be0fSWilliam Hua count - QUERY_CMD_DATA_LEN); 818e025be0fSWilliam Hua } else 819e025be0fSWilliam Hua len = -EINVAL; 820e025be0fSWilliam Hua 821*1dea3b41SJohn Johansen if (len < 0) { 822*1dea3b41SJohn Johansen put_multi_transaction(t); 823e025be0fSWilliam Hua return len; 824*1dea3b41SJohn Johansen } 825e025be0fSWilliam Hua 826*1dea3b41SJohn Johansen multi_transaction_set(file, t, len); 827e025be0fSWilliam Hua 828e025be0fSWilliam Hua return count; 829e025be0fSWilliam Hua } 830e025be0fSWilliam Hua 831c97204baSJohn Johansen static const struct file_operations aa_sfs_access = { 832e025be0fSWilliam Hua .write = aa_write_access, 833*1dea3b41SJohn Johansen .read = multi_transaction_read, 834*1dea3b41SJohn Johansen .release = multi_transaction_release, 835e025be0fSWilliam Hua .llseek = generic_file_llseek, 836e025be0fSWilliam Hua }; 837e025be0fSWilliam Hua 838c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v) 839e74abcf3SKees Cook { 840c97204baSJohn Johansen struct aa_sfs_entry *fs_file = seq->private; 841e74abcf3SKees Cook 842e74abcf3SKees Cook if (!fs_file) 843e74abcf3SKees Cook return 0; 844e74abcf3SKees Cook 845e74abcf3SKees Cook switch (fs_file->v_type) { 846c97204baSJohn Johansen case AA_SFS_TYPE_BOOLEAN: 847e74abcf3SKees Cook seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); 848e74abcf3SKees Cook break; 849c97204baSJohn Johansen case AA_SFS_TYPE_STRING: 850a9bf8e9fSKees Cook seq_printf(seq, "%s\n", fs_file->v.string); 851a9bf8e9fSKees Cook break; 852c97204baSJohn Johansen case AA_SFS_TYPE_U64: 853e74abcf3SKees Cook seq_printf(seq, "%#08lx\n", fs_file->v.u64); 854e74abcf3SKees Cook break; 855e74abcf3SKees Cook default: 856e74abcf3SKees Cook /* Ignore unpritable entry types. */ 857e74abcf3SKees Cook break; 858e74abcf3SKees Cook } 859e74abcf3SKees Cook 860e74abcf3SKees Cook return 0; 861e74abcf3SKees Cook } 862e74abcf3SKees Cook 863c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file) 864e74abcf3SKees Cook { 865c97204baSJohn Johansen return single_open(file, aa_sfs_seq_show, inode->i_private); 866e74abcf3SKees Cook } 867e74abcf3SKees Cook 868c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = { 869e74abcf3SKees Cook .owner = THIS_MODULE, 870c97204baSJohn Johansen .open = aa_sfs_seq_open, 871e74abcf3SKees Cook .read = seq_read, 872e74abcf3SKees Cook .llseek = seq_lseek, 873e74abcf3SKees Cook .release = single_release, 874e74abcf3SKees Cook }; 875e74abcf3SKees Cook 87652b97de3SJohn Johansen /* 87752b97de3SJohn Johansen * profile based file operations 87852b97de3SJohn Johansen * policy/profiles/XXXX/profiles/ * 87952b97de3SJohn Johansen */ 88052b97de3SJohn Johansen 88152b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME) \ 88252b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\ 88352b97de3SJohn Johansen { \ 88452b97de3SJohn Johansen return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \ 88552b97de3SJohn Johansen } \ 88652b97de3SJohn Johansen \ 88752b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = { \ 88852b97de3SJohn Johansen .owner = THIS_MODULE, \ 88952b97de3SJohn Johansen .open = seq_profile_ ##NAME ##_open, \ 89052b97de3SJohn Johansen .read = seq_read, \ 89152b97de3SJohn Johansen .llseek = seq_lseek, \ 89252b97de3SJohn Johansen .release = seq_profile_release, \ 89352b97de3SJohn Johansen } \ 89452b97de3SJohn Johansen 89552b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file, 8960d259f04SJohn Johansen int (*show)(struct seq_file *, void *)) 8970d259f04SJohn Johansen { 8988399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(inode->i_private); 8998399588aSJohn Johansen int error = single_open(file, show, proxy); 90063e2b423SJohn Johansen 9010d259f04SJohn Johansen if (error) { 9020d259f04SJohn Johansen file->private_data = NULL; 9038399588aSJohn Johansen aa_put_proxy(proxy); 9040d259f04SJohn Johansen } 9050d259f04SJohn Johansen 9060d259f04SJohn Johansen return error; 9070d259f04SJohn Johansen } 9080d259f04SJohn Johansen 90952b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file) 9100d259f04SJohn Johansen { 9110d259f04SJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 9120d259f04SJohn Johansen if (seq) 9138399588aSJohn Johansen aa_put_proxy(seq->private); 9140d259f04SJohn Johansen return single_release(inode, file); 9150d259f04SJohn Johansen } 9160d259f04SJohn Johansen 91752b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v) 9180d259f04SJohn Johansen { 9198399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 9208399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 9210d259f04SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 9220d259f04SJohn Johansen aa_put_profile(profile); 9230d259f04SJohn Johansen 9240d259f04SJohn Johansen return 0; 9250d259f04SJohn Johansen } 9260d259f04SJohn Johansen 92752b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v) 9280d259f04SJohn Johansen { 9298399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 9308399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 9310d259f04SJohn Johansen seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); 9320d259f04SJohn Johansen aa_put_profile(profile); 9330d259f04SJohn Johansen 9340d259f04SJohn Johansen return 0; 9350d259f04SJohn Johansen } 9360d259f04SJohn Johansen 93752b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v) 938556d0be7SJohn Johansen { 9398399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 9408399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 941556d0be7SJohn Johansen if (profile->attach) 942556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->attach); 943556d0be7SJohn Johansen else if (profile->xmatch) 944556d0be7SJohn Johansen seq_puts(seq, "<unknown>\n"); 945556d0be7SJohn Johansen else 946556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 947556d0be7SJohn Johansen aa_put_profile(profile); 948556d0be7SJohn Johansen 949556d0be7SJohn Johansen return 0; 950556d0be7SJohn Johansen } 951556d0be7SJohn Johansen 95252b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v) 953f8eb8a13SJohn Johansen { 9548399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 9558399588aSJohn Johansen struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile); 956f8eb8a13SJohn Johansen unsigned int i, size = aa_hash_size(); 957f8eb8a13SJohn Johansen 958f8eb8a13SJohn Johansen if (profile->hash) { 959f8eb8a13SJohn Johansen for (i = 0; i < size; i++) 960f8eb8a13SJohn Johansen seq_printf(seq, "%.2x", profile->hash[i]); 96147dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 962f8eb8a13SJohn Johansen } 9630b938a2eSJohn Johansen aa_put_profile(profile); 964f8eb8a13SJohn Johansen 965f8eb8a13SJohn Johansen return 0; 966f8eb8a13SJohn Johansen } 967f8eb8a13SJohn Johansen 96852b97de3SJohn Johansen SEQ_PROFILE_FOPS(name); 96952b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode); 97052b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach); 97152b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash); 972f8eb8a13SJohn Johansen 97364c86970SJohn Johansen /* 97464c86970SJohn Johansen * namespace based files 97564c86970SJohn Johansen * several root files and 97664c86970SJohn Johansen * policy/ * 97764c86970SJohn Johansen */ 978f8eb8a13SJohn Johansen 97964c86970SJohn Johansen #define SEQ_NS_FOPS(NAME) \ 98064c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \ 98164c86970SJohn Johansen { \ 98264c86970SJohn Johansen return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \ 98364c86970SJohn Johansen } \ 98464c86970SJohn Johansen \ 98564c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = { \ 98664c86970SJohn Johansen .owner = THIS_MODULE, \ 98764c86970SJohn Johansen .open = seq_ns_ ##NAME ##_open, \ 98864c86970SJohn Johansen .read = seq_read, \ 98964c86970SJohn Johansen .llseek = seq_lseek, \ 99064c86970SJohn Johansen .release = single_release, \ 99164c86970SJohn Johansen } \ 9923e3e5695SJohn Johansen 99364c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v) 994a71ada30SJohn Johansen { 995a71ada30SJohn Johansen struct aa_ns *ns = aa_current_profile()->ns; 996a71ada30SJohn Johansen 997a71ada30SJohn Johansen seq_printf(seq, "%d\n", ns->level); 998a71ada30SJohn Johansen 999a71ada30SJohn Johansen return 0; 1000a71ada30SJohn Johansen } 1001a71ada30SJohn Johansen 100264c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v) 10033e3e5695SJohn Johansen { 10043e3e5695SJohn Johansen struct aa_ns *ns = aa_current_profile()->ns; 10053e3e5695SJohn Johansen 10063e3e5695SJohn Johansen seq_printf(seq, "%s\n", ns->base.name); 10073e3e5695SJohn Johansen 10083e3e5695SJohn Johansen return 0; 10093e3e5695SJohn Johansen } 10103e3e5695SJohn Johansen 101164c86970SJohn Johansen SEQ_NS_FOPS(level); 101264c86970SJohn Johansen SEQ_NS_FOPS(name); 10133e3e5695SJohn Johansen 10145d5182caSJohn Johansen 10155d5182caSJohn Johansen /* policy/raw_data/ * file ops */ 10165d5182caSJohn Johansen 10175d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME) \ 10185d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\ 10195d5182caSJohn Johansen { \ 10205d5182caSJohn Johansen return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \ 10215d5182caSJohn Johansen } \ 10225d5182caSJohn Johansen \ 10235d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \ 10245d5182caSJohn Johansen .owner = THIS_MODULE, \ 10255d5182caSJohn Johansen .open = seq_rawdata_ ##NAME ##_open, \ 10265d5182caSJohn Johansen .read = seq_read, \ 10275d5182caSJohn Johansen .llseek = seq_lseek, \ 10285d5182caSJohn Johansen .release = seq_rawdata_release, \ 10295d5182caSJohn Johansen } \ 10305d5182caSJohn Johansen 10315d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file, 10325d5182caSJohn Johansen int (*show)(struct seq_file *, void *)) 10335ac8c355SJohn Johansen { 10345d5182caSJohn Johansen struct aa_loaddata *data = __aa_get_loaddata(inode->i_private); 10355d5182caSJohn Johansen int error; 10365d5182caSJohn Johansen 10375d5182caSJohn Johansen if (!data) 10385d5182caSJohn Johansen /* lost race this ent is being reaped */ 10395d5182caSJohn Johansen return -ENOENT; 10405d5182caSJohn Johansen 10415d5182caSJohn Johansen error = single_open(file, show, data); 10425d5182caSJohn Johansen if (error) { 10435d5182caSJohn Johansen AA_BUG(file->private_data && 10445d5182caSJohn Johansen ((struct seq_file *)file->private_data)->private); 10455d5182caSJohn Johansen aa_put_loaddata(data); 10465d5182caSJohn Johansen } 10475d5182caSJohn Johansen 10485d5182caSJohn Johansen return error; 10495d5182caSJohn Johansen } 10505d5182caSJohn Johansen 10515d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file) 10525d5182caSJohn Johansen { 10535d5182caSJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 10545d5182caSJohn Johansen 10555d5182caSJohn Johansen if (seq) 10565d5182caSJohn Johansen aa_put_loaddata(seq->private); 10575d5182caSJohn Johansen 10585d5182caSJohn Johansen return single_release(inode, file); 10595d5182caSJohn Johansen } 10605d5182caSJohn Johansen 10615d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v) 10625d5182caSJohn Johansen { 10635d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 10645d5182caSJohn Johansen 10655d5182caSJohn Johansen seq_printf(seq, "v%d\n", data->abi); 10665ac8c355SJohn Johansen 10675ac8c355SJohn Johansen return 0; 10685ac8c355SJohn Johansen } 10695ac8c355SJohn Johansen 10705d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v) 10715ac8c355SJohn Johansen { 10725d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 10735ac8c355SJohn Johansen 10745d5182caSJohn Johansen seq_printf(seq, "%ld\n", data->revision); 10755ac8c355SJohn Johansen 10765ac8c355SJohn Johansen return 0; 10775ac8c355SJohn Johansen } 10785ac8c355SJohn Johansen 10795d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v) 10805ac8c355SJohn Johansen { 10815d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 10825ac8c355SJohn Johansen unsigned int i, size = aa_hash_size(); 10835ac8c355SJohn Johansen 10845d5182caSJohn Johansen if (data->hash) { 10855ac8c355SJohn Johansen for (i = 0; i < size; i++) 10865d5182caSJohn Johansen seq_printf(seq, "%.2x", data->hash[i]); 108747dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 10885ac8c355SJohn Johansen } 10895ac8c355SJohn Johansen 10905ac8c355SJohn Johansen return 0; 10915ac8c355SJohn Johansen } 10925ac8c355SJohn Johansen 10935d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi); 10945d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision); 10955d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash); 10965ac8c355SJohn Johansen 10975ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, 10985ac8c355SJohn Johansen loff_t *ppos) 10995ac8c355SJohn Johansen { 11005ac8c355SJohn Johansen struct aa_loaddata *rawdata = file->private_data; 11015ac8c355SJohn Johansen 11025ac8c355SJohn Johansen return simple_read_from_buffer(buf, size, ppos, rawdata->data, 11035ac8c355SJohn Johansen rawdata->size); 11045ac8c355SJohn Johansen } 11055ac8c355SJohn Johansen 11065d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file) 11075ac8c355SJohn Johansen { 11085d5182caSJohn Johansen aa_put_loaddata(file->private_data); 11095ac8c355SJohn Johansen 11105ac8c355SJohn Johansen return 0; 11115ac8c355SJohn Johansen } 11125ac8c355SJohn Johansen 11135d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file) 11145d5182caSJohn Johansen { 11155d5182caSJohn Johansen if (!policy_view_capable(NULL)) 11165d5182caSJohn Johansen return -EACCES; 11175d5182caSJohn Johansen file->private_data = __aa_get_loaddata(inode->i_private); 11185d5182caSJohn Johansen if (!file->private_data) 11195d5182caSJohn Johansen /* lost race: this entry is being reaped */ 11205d5182caSJohn Johansen return -ENOENT; 11215d5182caSJohn Johansen 11225d5182caSJohn Johansen return 0; 11235d5182caSJohn Johansen } 11245d5182caSJohn Johansen 11255d5182caSJohn Johansen static const struct file_operations rawdata_fops = { 11265ac8c355SJohn Johansen .open = rawdata_open, 11275ac8c355SJohn Johansen .read = rawdata_read, 11285ac8c355SJohn Johansen .llseek = generic_file_llseek, 11295ac8c355SJohn Johansen .release = rawdata_release, 11305ac8c355SJohn Johansen }; 11315ac8c355SJohn Johansen 11325d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata) 11335d5182caSJohn Johansen { 11345d5182caSJohn Johansen int i; 11355d5182caSJohn Johansen 11365d5182caSJohn Johansen for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) { 11375d5182caSJohn Johansen if (!IS_ERR_OR_NULL(rawdata->dents[i])) { 11385d5182caSJohn Johansen /* no refcounts on i_private */ 1139c961ee5fSJohn Johansen aafs_remove(rawdata->dents[i]); 11405d5182caSJohn Johansen rawdata->dents[i] = NULL; 11415d5182caSJohn Johansen } 11425d5182caSJohn Johansen } 11435d5182caSJohn Johansen } 11445d5182caSJohn Johansen 11455d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata) 11465d5182caSJohn Johansen { 11475d5182caSJohn Johansen AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock)); 11485d5182caSJohn Johansen 11495d5182caSJohn Johansen if (rawdata->ns) { 11505d5182caSJohn Johansen remove_rawdata_dents(rawdata); 11515d5182caSJohn Johansen list_del_init(&rawdata->list); 11525d5182caSJohn Johansen aa_put_ns(rawdata->ns); 11535d5182caSJohn Johansen rawdata->ns = NULL; 11545d5182caSJohn Johansen } 11555d5182caSJohn Johansen } 11565d5182caSJohn Johansen 11575d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata) 11585d5182caSJohn Johansen { 11595d5182caSJohn Johansen struct dentry *dent, *dir; 11605d5182caSJohn Johansen 11615d5182caSJohn Johansen AA_BUG(!ns); 11625d5182caSJohn Johansen AA_BUG(!rawdata); 11635d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 11645d5182caSJohn Johansen AA_BUG(!ns_subdata_dir(ns)); 11655d5182caSJohn Johansen 11665d5182caSJohn Johansen /* 11675d5182caSJohn Johansen * just use ns revision dir was originally created at. This is 11685d5182caSJohn Johansen * under ns->lock and if load is successful revision will be 11695d5182caSJohn Johansen * bumped and is guaranteed to be unique 11705d5182caSJohn Johansen */ 11715d5182caSJohn Johansen rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision); 11725d5182caSJohn Johansen if (!rawdata->name) 11735d5182caSJohn Johansen return -ENOMEM; 11745d5182caSJohn Johansen 1175c961ee5fSJohn Johansen dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns)); 11765d5182caSJohn Johansen if (IS_ERR(dir)) 11775d5182caSJohn Johansen /* ->name freed when rawdata freed */ 11785d5182caSJohn Johansen return PTR_ERR(dir); 11795d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DIR] = dir; 11805d5182caSJohn Johansen 1181c961ee5fSJohn Johansen dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata, 11825d5182caSJohn Johansen &seq_rawdata_abi_fops); 11835d5182caSJohn Johansen if (IS_ERR(dent)) 11845d5182caSJohn Johansen goto fail; 11855d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_ABI] = dent; 11865d5182caSJohn Johansen 1187c961ee5fSJohn Johansen dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata, 11885d5182caSJohn Johansen &seq_rawdata_revision_fops); 11895d5182caSJohn Johansen if (IS_ERR(dent)) 11905d5182caSJohn Johansen goto fail; 11915d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_REVISION] = dent; 11925d5182caSJohn Johansen 11935d5182caSJohn Johansen if (aa_g_hash_policy) { 1194c961ee5fSJohn Johansen dent = aafs_create_file("sha1", S_IFREG | 0444, dir, 11955d5182caSJohn Johansen rawdata, &seq_rawdata_hash_fops); 11965d5182caSJohn Johansen if (IS_ERR(dent)) 11975d5182caSJohn Johansen goto fail; 11985d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_HASH] = dent; 11995d5182caSJohn Johansen } 12005d5182caSJohn Johansen 1201c961ee5fSJohn Johansen dent = aafs_create_file("raw_data", S_IFREG | 0444, 12025d5182caSJohn Johansen dir, rawdata, &rawdata_fops); 12035d5182caSJohn Johansen if (IS_ERR(dent)) 12045d5182caSJohn Johansen goto fail; 12055d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DATA] = dent; 12065d5182caSJohn Johansen d_inode(dent)->i_size = rawdata->size; 12075d5182caSJohn Johansen 12085d5182caSJohn Johansen rawdata->ns = aa_get_ns(ns); 12095d5182caSJohn Johansen list_add(&rawdata->list, &ns->rawdata_list); 12105d5182caSJohn Johansen /* no refcount on inode rawdata */ 12115d5182caSJohn Johansen 12125d5182caSJohn Johansen return 0; 12135d5182caSJohn Johansen 12145d5182caSJohn Johansen fail: 12155d5182caSJohn Johansen remove_rawdata_dents(rawdata); 12165d5182caSJohn Johansen 12175d5182caSJohn Johansen return PTR_ERR(dent); 12185d5182caSJohn Johansen } 12195d5182caSJohn Johansen 12200d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/ 1221c97204baSJohn Johansen 1222c97204baSJohn Johansen /** 1223c97204baSJohn Johansen * 1224c97204baSJohn Johansen * Requires: @profile->ns->lock held 1225c97204baSJohn Johansen */ 1226c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile) 12270d259f04SJohn Johansen { 12280d259f04SJohn Johansen struct aa_profile *child; 12290d259f04SJohn Johansen int i; 12300d259f04SJohn Johansen 12310d259f04SJohn Johansen if (!profile) 12320d259f04SJohn Johansen return; 12330d259f04SJohn Johansen 12340d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) 1235c97204baSJohn Johansen __aafs_profile_rmdir(child); 12360d259f04SJohn Johansen 12370d259f04SJohn Johansen for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { 12388399588aSJohn Johansen struct aa_proxy *proxy; 12390d259f04SJohn Johansen if (!profile->dents[i]) 12400d259f04SJohn Johansen continue; 12410d259f04SJohn Johansen 12428399588aSJohn Johansen proxy = d_inode(profile->dents[i])->i_private; 1243c961ee5fSJohn Johansen aafs_remove(profile->dents[i]); 12448399588aSJohn Johansen aa_put_proxy(proxy); 12450d259f04SJohn Johansen profile->dents[i] = NULL; 12460d259f04SJohn Johansen } 12470d259f04SJohn Johansen } 12480d259f04SJohn Johansen 1249c97204baSJohn Johansen /** 1250c97204baSJohn Johansen * 1251c97204baSJohn Johansen * Requires: @old->ns->lock held 1252c97204baSJohn Johansen */ 1253c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old, 12540d259f04SJohn Johansen struct aa_profile *new) 12550d259f04SJohn Johansen { 12560d259f04SJohn Johansen int i; 12570d259f04SJohn Johansen 12580d259f04SJohn Johansen for (i = 0; i < AAFS_PROF_SIZEOF; i++) { 12590d259f04SJohn Johansen new->dents[i] = old->dents[i]; 1260d671e890SJohn Johansen if (new->dents[i]) 1261078cd827SDeepa Dinamani new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); 12620d259f04SJohn Johansen old->dents[i] = NULL; 12630d259f04SJohn Johansen } 12640d259f04SJohn Johansen } 12650d259f04SJohn Johansen 12660d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name, 12670d259f04SJohn Johansen struct aa_profile *profile, 12680d259f04SJohn Johansen const struct file_operations *fops) 12690d259f04SJohn Johansen { 12708399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(profile->proxy); 12710d259f04SJohn Johansen struct dentry *dent; 12720d259f04SJohn Johansen 1273c961ee5fSJohn Johansen dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops); 12740d259f04SJohn Johansen if (IS_ERR(dent)) 12758399588aSJohn Johansen aa_put_proxy(proxy); 12760d259f04SJohn Johansen 12770d259f04SJohn Johansen return dent; 12780d259f04SJohn Johansen } 12790d259f04SJohn Johansen 12805d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile) 12815d5182caSJohn Johansen { 12825d5182caSJohn Johansen int depth = 0; 12835d5182caSJohn Johansen 12845d5182caSJohn Johansen rcu_read_lock(); 12855d5182caSJohn Johansen for (depth = 0; profile; profile = rcu_access_pointer(profile->parent)) 12865d5182caSJohn Johansen depth++; 12875d5182caSJohn Johansen rcu_read_unlock(); 12885d5182caSJohn Johansen 12895d5182caSJohn Johansen return depth; 12905d5182caSJohn Johansen } 12915d5182caSJohn Johansen 12925d5182caSJohn Johansen static int gen_symlink_name(char *buffer, size_t bsize, int depth, 12935d5182caSJohn Johansen const char *dirname, const char *fname) 12945d5182caSJohn Johansen { 12955d5182caSJohn Johansen int error; 12965d5182caSJohn Johansen 12975d5182caSJohn Johansen for (; depth > 0; depth--) { 12985d5182caSJohn Johansen if (bsize < 7) 12995d5182caSJohn Johansen return -ENAMETOOLONG; 13005d5182caSJohn Johansen strcpy(buffer, "../../"); 13015d5182caSJohn Johansen buffer += 6; 13025d5182caSJohn Johansen bsize -= 6; 13035d5182caSJohn Johansen } 13045d5182caSJohn Johansen 13055d5182caSJohn Johansen error = snprintf(buffer, bsize, "raw_data/%s/%s", dirname, fname); 13065d5182caSJohn Johansen if (error >= bsize || error < 0) 13075d5182caSJohn Johansen return -ENAMETOOLONG; 13085d5182caSJohn Johansen 13095d5182caSJohn Johansen return 0; 13105d5182caSJohn Johansen } 13115d5182caSJohn Johansen 13125d5182caSJohn Johansen /* 13135d5182caSJohn Johansen * Requires: @profile->ns->lock held 13145d5182caSJohn Johansen */ 1315c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) 13160d259f04SJohn Johansen { 13170d259f04SJohn Johansen struct aa_profile *child; 13180d259f04SJohn Johansen struct dentry *dent = NULL, *dir; 13190d259f04SJohn Johansen int error; 13200d259f04SJohn Johansen 13210d259f04SJohn Johansen if (!parent) { 13220d259f04SJohn Johansen struct aa_profile *p; 13230d259f04SJohn Johansen p = aa_deref_parent(profile); 13240d259f04SJohn Johansen dent = prof_dir(p); 13250d259f04SJohn Johansen /* adding to parent that previously didn't have children */ 1326c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dent); 13270d259f04SJohn Johansen if (IS_ERR(dent)) 13280d259f04SJohn Johansen goto fail; 13290d259f04SJohn Johansen prof_child_dir(p) = parent = dent; 13300d259f04SJohn Johansen } 13310d259f04SJohn Johansen 13320d259f04SJohn Johansen if (!profile->dirname) { 13330d259f04SJohn Johansen int len, id_len; 13340d259f04SJohn Johansen len = mangle_name(profile->base.name, NULL); 13350d259f04SJohn Johansen id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); 13360d259f04SJohn Johansen 13370d259f04SJohn Johansen profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); 1338ffac1de6SDan Carpenter if (!profile->dirname) { 1339ffac1de6SDan Carpenter error = -ENOMEM; 1340ffac1de6SDan Carpenter goto fail2; 1341ffac1de6SDan Carpenter } 13420d259f04SJohn Johansen 13430d259f04SJohn Johansen mangle_name(profile->base.name, profile->dirname); 13440d259f04SJohn Johansen sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); 13450d259f04SJohn Johansen } 13460d259f04SJohn Johansen 1347c961ee5fSJohn Johansen dent = aafs_create_dir(profile->dirname, parent); 13480d259f04SJohn Johansen if (IS_ERR(dent)) 13490d259f04SJohn Johansen goto fail; 13500d259f04SJohn Johansen prof_dir(profile) = dir = dent; 13510d259f04SJohn Johansen 135252b97de3SJohn Johansen dent = create_profile_file(dir, "name", profile, 135352b97de3SJohn Johansen &seq_profile_name_fops); 13540d259f04SJohn Johansen if (IS_ERR(dent)) 13550d259f04SJohn Johansen goto fail; 13560d259f04SJohn Johansen profile->dents[AAFS_PROF_NAME] = dent; 13570d259f04SJohn Johansen 135852b97de3SJohn Johansen dent = create_profile_file(dir, "mode", profile, 135952b97de3SJohn Johansen &seq_profile_mode_fops); 13600d259f04SJohn Johansen if (IS_ERR(dent)) 13610d259f04SJohn Johansen goto fail; 13620d259f04SJohn Johansen profile->dents[AAFS_PROF_MODE] = dent; 13630d259f04SJohn Johansen 1364556d0be7SJohn Johansen dent = create_profile_file(dir, "attach", profile, 136552b97de3SJohn Johansen &seq_profile_attach_fops); 1366556d0be7SJohn Johansen if (IS_ERR(dent)) 1367556d0be7SJohn Johansen goto fail; 1368556d0be7SJohn Johansen profile->dents[AAFS_PROF_ATTACH] = dent; 1369556d0be7SJohn Johansen 1370f8eb8a13SJohn Johansen if (profile->hash) { 1371f8eb8a13SJohn Johansen dent = create_profile_file(dir, "sha1", profile, 137252b97de3SJohn Johansen &seq_profile_hash_fops); 1373f8eb8a13SJohn Johansen if (IS_ERR(dent)) 1374f8eb8a13SJohn Johansen goto fail; 1375f8eb8a13SJohn Johansen profile->dents[AAFS_PROF_HASH] = dent; 1376f8eb8a13SJohn Johansen } 1377f8eb8a13SJohn Johansen 13785ac8c355SJohn Johansen if (profile->rawdata) { 13795d5182caSJohn Johansen char target[64]; 13805d5182caSJohn Johansen int depth = profile_depth(profile); 13815d5182caSJohn Johansen 13825d5182caSJohn Johansen error = gen_symlink_name(target, sizeof(target), depth, 13835d5182caSJohn Johansen profile->rawdata->name, "sha1"); 13845d5182caSJohn Johansen if (error < 0) 13855d5182caSJohn Johansen goto fail2; 1386c961ee5fSJohn Johansen dent = aafs_create_symlink("raw_sha1", dir, target, NULL); 13875ac8c355SJohn Johansen if (IS_ERR(dent)) 13885ac8c355SJohn Johansen goto fail; 13895ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_HASH] = dent; 13905ac8c355SJohn Johansen 13915d5182caSJohn Johansen error = gen_symlink_name(target, sizeof(target), depth, 13925d5182caSJohn Johansen profile->rawdata->name, "abi"); 13935d5182caSJohn Johansen if (error < 0) 13945d5182caSJohn Johansen goto fail2; 1395c961ee5fSJohn Johansen dent = aafs_create_symlink("raw_abi", dir, target, NULL); 13965ac8c355SJohn Johansen if (IS_ERR(dent)) 13975ac8c355SJohn Johansen goto fail; 13985ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_ABI] = dent; 13995ac8c355SJohn Johansen 14005d5182caSJohn Johansen error = gen_symlink_name(target, sizeof(target), depth, 14015d5182caSJohn Johansen profile->rawdata->name, "raw_data"); 14025d5182caSJohn Johansen if (error < 0) 14035d5182caSJohn Johansen goto fail2; 1404c961ee5fSJohn Johansen dent = aafs_create_symlink("raw_data", dir, target, NULL); 14055ac8c355SJohn Johansen if (IS_ERR(dent)) 14065ac8c355SJohn Johansen goto fail; 14075ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_DATA] = dent; 14085ac8c355SJohn Johansen } 14095ac8c355SJohn Johansen 14100d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) { 1411c97204baSJohn Johansen error = __aafs_profile_mkdir(child, prof_child_dir(profile)); 14120d259f04SJohn Johansen if (error) 14130d259f04SJohn Johansen goto fail2; 14140d259f04SJohn Johansen } 14150d259f04SJohn Johansen 14160d259f04SJohn Johansen return 0; 14170d259f04SJohn Johansen 14180d259f04SJohn Johansen fail: 14190d259f04SJohn Johansen error = PTR_ERR(dent); 14200d259f04SJohn Johansen 14210d259f04SJohn Johansen fail2: 1422c97204baSJohn Johansen __aafs_profile_rmdir(profile); 14230d259f04SJohn Johansen 14240d259f04SJohn Johansen return error; 14250d259f04SJohn Johansen } 14260d259f04SJohn Johansen 14274ae47f33SJohn Johansen static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode) 14284ae47f33SJohn Johansen { 14294ae47f33SJohn Johansen struct aa_ns *ns, *parent; 14304ae47f33SJohn Johansen /* TODO: improve permission check */ 14314ae47f33SJohn Johansen struct aa_profile *profile = aa_current_profile(); 14324ae47f33SJohn Johansen int error = aa_may_manage_policy(profile, NULL, AA_MAY_LOAD_POLICY); 14334ae47f33SJohn Johansen 14344ae47f33SJohn Johansen if (error) 14354ae47f33SJohn Johansen return error; 14364ae47f33SJohn Johansen 14374ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 14384ae47f33SJohn Johansen AA_BUG(d_inode(ns_subns_dir(parent)) != dir); 14394ae47f33SJohn Johansen 14404ae47f33SJohn Johansen /* we have to unlock and then relock to get locking order right 14414ae47f33SJohn Johansen * for pin_fs 14424ae47f33SJohn Johansen */ 14434ae47f33SJohn Johansen inode_unlock(dir); 14444ae47f33SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 14454ae47f33SJohn Johansen mutex_lock(&parent->lock); 14464ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 14474ae47f33SJohn Johansen if (error) 14484ae47f33SJohn Johansen goto out; 14494ae47f33SJohn Johansen 14504ae47f33SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL, 14514ae47f33SJohn Johansen NULL, NULL, NULL); 14524ae47f33SJohn Johansen if (error) 14534ae47f33SJohn Johansen goto out_pin; 14544ae47f33SJohn Johansen 14554ae47f33SJohn Johansen ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name), 14564ae47f33SJohn Johansen dentry); 14574ae47f33SJohn Johansen if (IS_ERR(ns)) { 14584ae47f33SJohn Johansen error = PTR_ERR(ns); 14594ae47f33SJohn Johansen ns = NULL; 14604ae47f33SJohn Johansen } 14614ae47f33SJohn Johansen 14624ae47f33SJohn Johansen aa_put_ns(ns); /* list ref remains */ 14634ae47f33SJohn Johansen out_pin: 14644ae47f33SJohn Johansen if (error) 14654ae47f33SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 14664ae47f33SJohn Johansen out: 14674ae47f33SJohn Johansen mutex_unlock(&parent->lock); 14684ae47f33SJohn Johansen aa_put_ns(parent); 14694ae47f33SJohn Johansen 14704ae47f33SJohn Johansen return error; 14714ae47f33SJohn Johansen } 14724ae47f33SJohn Johansen 14734ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry) 14744ae47f33SJohn Johansen { 14754ae47f33SJohn Johansen struct aa_ns *ns, *parent; 14764ae47f33SJohn Johansen /* TODO: improve permission check */ 14774ae47f33SJohn Johansen struct aa_profile *profile = aa_current_profile(); 14784ae47f33SJohn Johansen int error = aa_may_manage_policy(profile, NULL, AA_MAY_LOAD_POLICY); 14794ae47f33SJohn Johansen 14804ae47f33SJohn Johansen if (error) 14814ae47f33SJohn Johansen return error; 14824ae47f33SJohn Johansen 14834ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 14844ae47f33SJohn Johansen /* rmdir calls the generic securityfs functions to remove files 14854ae47f33SJohn Johansen * from the apparmor dir. It is up to the apparmor ns locking 14864ae47f33SJohn Johansen * to avoid races. 14874ae47f33SJohn Johansen */ 14884ae47f33SJohn Johansen inode_unlock(dir); 14894ae47f33SJohn Johansen inode_unlock(dentry->d_inode); 14904ae47f33SJohn Johansen 14914ae47f33SJohn Johansen mutex_lock(&parent->lock); 14924ae47f33SJohn Johansen ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name, 14934ae47f33SJohn Johansen dentry->d_name.len)); 14944ae47f33SJohn Johansen if (!ns) { 14954ae47f33SJohn Johansen error = -ENOENT; 14964ae47f33SJohn Johansen goto out; 14974ae47f33SJohn Johansen } 14984ae47f33SJohn Johansen AA_BUG(ns_dir(ns) != dentry); 14994ae47f33SJohn Johansen 15004ae47f33SJohn Johansen __aa_remove_ns(ns); 15014ae47f33SJohn Johansen aa_put_ns(ns); 15024ae47f33SJohn Johansen 15034ae47f33SJohn Johansen out: 15044ae47f33SJohn Johansen mutex_unlock(&parent->lock); 15054ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 15064ae47f33SJohn Johansen inode_lock(dentry->d_inode); 15074ae47f33SJohn Johansen aa_put_ns(parent); 15084ae47f33SJohn Johansen 15094ae47f33SJohn Johansen return error; 15104ae47f33SJohn Johansen } 15114ae47f33SJohn Johansen 15124ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = { 15134ae47f33SJohn Johansen .lookup = simple_lookup, 15144ae47f33SJohn Johansen .mkdir = ns_mkdir_op, 15154ae47f33SJohn Johansen .rmdir = ns_rmdir_op, 15164ae47f33SJohn Johansen }; 15174ae47f33SJohn Johansen 15185d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns) 15195d5182caSJohn Johansen { 15205d5182caSJohn Johansen struct aa_loaddata *ent, *tmp; 15215d5182caSJohn Johansen 15225d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 15235d5182caSJohn Johansen 15245d5182caSJohn Johansen list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list) 15255d5182caSJohn Johansen __aa_fs_remove_rawdata(ent); 15265d5182caSJohn Johansen } 15275d5182caSJohn Johansen 1528c97204baSJohn Johansen /** 1529c97204baSJohn Johansen * 1530c97204baSJohn Johansen * Requires: @ns->lock held 1531c97204baSJohn Johansen */ 1532c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns) 15330d259f04SJohn Johansen { 153498849dffSJohn Johansen struct aa_ns *sub; 15350d259f04SJohn Johansen struct aa_profile *child; 15360d259f04SJohn Johansen int i; 15370d259f04SJohn Johansen 15380d259f04SJohn Johansen if (!ns) 15390d259f04SJohn Johansen return; 15400d259f04SJohn Johansen 15410d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) 1542c97204baSJohn Johansen __aafs_profile_rmdir(child); 15430d259f04SJohn Johansen 15440d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 15450d259f04SJohn Johansen mutex_lock(&sub->lock); 1546c97204baSJohn Johansen __aafs_ns_rmdir(sub); 15470d259f04SJohn Johansen mutex_unlock(&sub->lock); 15480d259f04SJohn Johansen } 15490d259f04SJohn Johansen 15505d5182caSJohn Johansen __aa_fs_list_remove_rawdata(ns); 15515d5182caSJohn Johansen 1552b7fd2c03SJohn Johansen if (ns_subns_dir(ns)) { 1553b7fd2c03SJohn Johansen sub = d_inode(ns_subns_dir(ns))->i_private; 1554b7fd2c03SJohn Johansen aa_put_ns(sub); 1555b7fd2c03SJohn Johansen } 1556b7fd2c03SJohn Johansen if (ns_subload(ns)) { 1557b7fd2c03SJohn Johansen sub = d_inode(ns_subload(ns))->i_private; 1558b7fd2c03SJohn Johansen aa_put_ns(sub); 1559b7fd2c03SJohn Johansen } 1560b7fd2c03SJohn Johansen if (ns_subreplace(ns)) { 1561b7fd2c03SJohn Johansen sub = d_inode(ns_subreplace(ns))->i_private; 1562b7fd2c03SJohn Johansen aa_put_ns(sub); 1563b7fd2c03SJohn Johansen } 1564b7fd2c03SJohn Johansen if (ns_subremove(ns)) { 1565b7fd2c03SJohn Johansen sub = d_inode(ns_subremove(ns))->i_private; 1566b7fd2c03SJohn Johansen aa_put_ns(sub); 1567b7fd2c03SJohn Johansen } 1568d9bf2c26SJohn Johansen if (ns_subrevision(ns)) { 1569d9bf2c26SJohn Johansen sub = d_inode(ns_subrevision(ns))->i_private; 1570d9bf2c26SJohn Johansen aa_put_ns(sub); 1571d9bf2c26SJohn Johansen } 1572b7fd2c03SJohn Johansen 15730d259f04SJohn Johansen for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { 1574c961ee5fSJohn Johansen aafs_remove(ns->dents[i]); 15750d259f04SJohn Johansen ns->dents[i] = NULL; 15760d259f04SJohn Johansen } 15770d259f04SJohn Johansen } 15780d259f04SJohn Johansen 1579b7fd2c03SJohn Johansen /* assumes cleanup in caller */ 1580c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) 1581b7fd2c03SJohn Johansen { 1582b7fd2c03SJohn Johansen struct dentry *dent; 1583b7fd2c03SJohn Johansen 1584b7fd2c03SJohn Johansen AA_BUG(!ns); 1585b7fd2c03SJohn Johansen AA_BUG(!dir); 1586b7fd2c03SJohn Johansen 1587c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dir); 1588b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1589b7fd2c03SJohn Johansen return PTR_ERR(dent); 1590b7fd2c03SJohn Johansen ns_subprofs_dir(ns) = dent; 1591b7fd2c03SJohn Johansen 1592c961ee5fSJohn Johansen dent = aafs_create_dir("raw_data", dir); 1593b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1594b7fd2c03SJohn Johansen return PTR_ERR(dent); 1595b7fd2c03SJohn Johansen ns_subdata_dir(ns) = dent; 1596b7fd2c03SJohn Johansen 1597d9bf2c26SJohn Johansen dent = aafs_create_file("revision", 0444, dir, ns, 1598d9bf2c26SJohn Johansen &aa_fs_ns_revision_fops); 1599d9bf2c26SJohn Johansen if (IS_ERR(dent)) 1600d9bf2c26SJohn Johansen return PTR_ERR(dent); 1601d9bf2c26SJohn Johansen aa_get_ns(ns); 1602d9bf2c26SJohn Johansen ns_subrevision(ns) = dent; 1603d9bf2c26SJohn Johansen 1604c961ee5fSJohn Johansen dent = aafs_create_file(".load", 0640, dir, ns, 1605b7fd2c03SJohn Johansen &aa_fs_profile_load); 1606b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1607b7fd2c03SJohn Johansen return PTR_ERR(dent); 1608b7fd2c03SJohn Johansen aa_get_ns(ns); 1609b7fd2c03SJohn Johansen ns_subload(ns) = dent; 1610b7fd2c03SJohn Johansen 1611c961ee5fSJohn Johansen dent = aafs_create_file(".replace", 0640, dir, ns, 1612b7fd2c03SJohn Johansen &aa_fs_profile_replace); 1613b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1614b7fd2c03SJohn Johansen return PTR_ERR(dent); 1615b7fd2c03SJohn Johansen aa_get_ns(ns); 1616b7fd2c03SJohn Johansen ns_subreplace(ns) = dent; 1617b7fd2c03SJohn Johansen 1618c961ee5fSJohn Johansen dent = aafs_create_file(".remove", 0640, dir, ns, 1619b7fd2c03SJohn Johansen &aa_fs_profile_remove); 1620b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1621b7fd2c03SJohn Johansen return PTR_ERR(dent); 1622b7fd2c03SJohn Johansen aa_get_ns(ns); 1623b7fd2c03SJohn Johansen ns_subremove(ns) = dent; 1624b7fd2c03SJohn Johansen 16254ae47f33SJohn Johansen /* use create_dentry so we can supply private data */ 16264ae47f33SJohn Johansen dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL, 16274ae47f33SJohn Johansen &ns_dir_inode_operations); 1628b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1629b7fd2c03SJohn Johansen return PTR_ERR(dent); 1630b7fd2c03SJohn Johansen aa_get_ns(ns); 1631b7fd2c03SJohn Johansen ns_subns_dir(ns) = dent; 1632b7fd2c03SJohn Johansen 1633b7fd2c03SJohn Johansen return 0; 1634b7fd2c03SJohn Johansen } 1635b7fd2c03SJohn Johansen 1636c97204baSJohn Johansen /* 1637c97204baSJohn Johansen * Requires: @ns->lock held 1638c97204baSJohn Johansen */ 163998407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name, 164098407f0aSJohn Johansen struct dentry *dent) 16410d259f04SJohn Johansen { 164298849dffSJohn Johansen struct aa_ns *sub; 16430d259f04SJohn Johansen struct aa_profile *child; 164498407f0aSJohn Johansen struct dentry *dir; 16450d259f04SJohn Johansen int error; 16460d259f04SJohn Johansen 1647b7fd2c03SJohn Johansen AA_BUG(!ns); 1648b7fd2c03SJohn Johansen AA_BUG(!parent); 1649b7fd2c03SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 1650b7fd2c03SJohn Johansen 16510d259f04SJohn Johansen if (!name) 16520d259f04SJohn Johansen name = ns->base.name; 16530d259f04SJohn Johansen 1654c961ee5fSJohn Johansen if (!dent) { 1655b7fd2c03SJohn Johansen /* create ns dir if it doesn't already exist */ 1656c961ee5fSJohn Johansen dent = aafs_create_dir(name, parent); 16570d259f04SJohn Johansen if (IS_ERR(dent)) 16580d259f04SJohn Johansen goto fail; 1659c961ee5fSJohn Johansen } else 1660c961ee5fSJohn Johansen dget(dent); 16610d259f04SJohn Johansen ns_dir(ns) = dir = dent; 1662c97204baSJohn Johansen error = __aafs_ns_mkdir_entries(ns, dir); 1663b7fd2c03SJohn Johansen if (error) 1664b7fd2c03SJohn Johansen goto fail2; 16650d259f04SJohn Johansen 1666b7fd2c03SJohn Johansen /* profiles */ 16670d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) { 1668c97204baSJohn Johansen error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns)); 16690d259f04SJohn Johansen if (error) 16700d259f04SJohn Johansen goto fail2; 16710d259f04SJohn Johansen } 16720d259f04SJohn Johansen 1673b7fd2c03SJohn Johansen /* subnamespaces */ 16740d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 16750d259f04SJohn Johansen mutex_lock(&sub->lock); 167698407f0aSJohn Johansen error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL); 16770d259f04SJohn Johansen mutex_unlock(&sub->lock); 16780d259f04SJohn Johansen if (error) 16790d259f04SJohn Johansen goto fail2; 16800d259f04SJohn Johansen } 16810d259f04SJohn Johansen 16820d259f04SJohn Johansen return 0; 16830d259f04SJohn Johansen 16840d259f04SJohn Johansen fail: 16850d259f04SJohn Johansen error = PTR_ERR(dent); 16860d259f04SJohn Johansen 16870d259f04SJohn Johansen fail2: 1688c97204baSJohn Johansen __aafs_ns_rmdir(ns); 16890d259f04SJohn Johansen 16900d259f04SJohn Johansen return error; 16910d259f04SJohn Johansen } 16920d259f04SJohn Johansen 16930d259f04SJohn Johansen 169429b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head)) 169529b3822fSJohn Johansen 169629b3822fSJohn Johansen /** 169798849dffSJohn Johansen * __next_ns - find the next namespace to list 169829b3822fSJohn Johansen * @root: root namespace to stop search at (NOT NULL) 169929b3822fSJohn Johansen * @ns: current ns position (NOT NULL) 170029b3822fSJohn Johansen * 170129b3822fSJohn Johansen * Find the next namespace from @ns under @root and handle all locking needed 170229b3822fSJohn Johansen * while switching current namespace. 170329b3822fSJohn Johansen * 170429b3822fSJohn Johansen * Returns: next namespace or NULL if at last namespace under @root 170529b3822fSJohn Johansen * Requires: ns->parent->lock to be held 170629b3822fSJohn Johansen * NOTE: will not unlock root->lock 170729b3822fSJohn Johansen */ 170898849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) 170929b3822fSJohn Johansen { 171098849dffSJohn Johansen struct aa_ns *parent, *next; 171129b3822fSJohn Johansen 171229b3822fSJohn Johansen /* is next namespace a child */ 171329b3822fSJohn Johansen if (!list_empty(&ns->sub_ns)) { 171429b3822fSJohn Johansen next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); 171529b3822fSJohn Johansen mutex_lock(&next->lock); 171629b3822fSJohn Johansen return next; 171729b3822fSJohn Johansen } 171829b3822fSJohn Johansen 171929b3822fSJohn Johansen /* check if the next ns is a sibling, parent, gp, .. */ 172029b3822fSJohn Johansen parent = ns->parent; 1721ed2c7da3SJohn Johansen while (ns != root) { 172229b3822fSJohn Johansen mutex_unlock(&ns->lock); 172338dbd7d8SGeliang Tang next = list_next_entry(ns, base.list); 172429b3822fSJohn Johansen if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { 172529b3822fSJohn Johansen mutex_lock(&next->lock); 172629b3822fSJohn Johansen return next; 172729b3822fSJohn Johansen } 172829b3822fSJohn Johansen ns = parent; 172929b3822fSJohn Johansen parent = parent->parent; 173029b3822fSJohn Johansen } 173129b3822fSJohn Johansen 173229b3822fSJohn Johansen return NULL; 173329b3822fSJohn Johansen } 173429b3822fSJohn Johansen 173529b3822fSJohn Johansen /** 173629b3822fSJohn Johansen * __first_profile - find the first profile in a namespace 173729b3822fSJohn Johansen * @root: namespace that is root of profiles being displayed (NOT NULL) 173829b3822fSJohn Johansen * @ns: namespace to start in (NOT NULL) 173929b3822fSJohn Johansen * 174029b3822fSJohn Johansen * Returns: unrefcounted profile or NULL if no profile 174129b3822fSJohn Johansen * Requires: profile->ns.lock to be held 174229b3822fSJohn Johansen */ 174398849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root, 174498849dffSJohn Johansen struct aa_ns *ns) 174529b3822fSJohn Johansen { 174698849dffSJohn Johansen for (; ns; ns = __next_ns(root, ns)) { 174729b3822fSJohn Johansen if (!list_empty(&ns->base.profiles)) 174829b3822fSJohn Johansen return list_first_entry(&ns->base.profiles, 174929b3822fSJohn Johansen struct aa_profile, base.list); 175029b3822fSJohn Johansen } 175129b3822fSJohn Johansen return NULL; 175229b3822fSJohn Johansen } 175329b3822fSJohn Johansen 175429b3822fSJohn Johansen /** 175529b3822fSJohn Johansen * __next_profile - step to the next profile in a profile tree 175629b3822fSJohn Johansen * @profile: current profile in tree (NOT NULL) 175729b3822fSJohn Johansen * 175829b3822fSJohn Johansen * Perform a depth first traversal on the profile tree in a namespace 175929b3822fSJohn Johansen * 176029b3822fSJohn Johansen * Returns: next profile or NULL if done 176129b3822fSJohn Johansen * Requires: profile->ns.lock to be held 176229b3822fSJohn Johansen */ 176329b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p) 176429b3822fSJohn Johansen { 176529b3822fSJohn Johansen struct aa_profile *parent; 176698849dffSJohn Johansen struct aa_ns *ns = p->ns; 176729b3822fSJohn Johansen 176829b3822fSJohn Johansen /* is next profile a child */ 176929b3822fSJohn Johansen if (!list_empty(&p->base.profiles)) 177029b3822fSJohn Johansen return list_first_entry(&p->base.profiles, typeof(*p), 177129b3822fSJohn Johansen base.list); 177229b3822fSJohn Johansen 177329b3822fSJohn Johansen /* is next profile a sibling, parent sibling, gp, sibling, .. */ 177429b3822fSJohn Johansen parent = rcu_dereference_protected(p->parent, 177529b3822fSJohn Johansen mutex_is_locked(&p->ns->lock)); 177629b3822fSJohn Johansen while (parent) { 177738dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 177829b3822fSJohn Johansen if (!list_entry_is_head(p, &parent->base.profiles, base.list)) 177929b3822fSJohn Johansen return p; 178029b3822fSJohn Johansen p = parent; 178129b3822fSJohn Johansen parent = rcu_dereference_protected(parent->parent, 178229b3822fSJohn Johansen mutex_is_locked(&parent->ns->lock)); 178329b3822fSJohn Johansen } 178429b3822fSJohn Johansen 178529b3822fSJohn Johansen /* is next another profile in the namespace */ 178638dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 178729b3822fSJohn Johansen if (!list_entry_is_head(p, &ns->base.profiles, base.list)) 178829b3822fSJohn Johansen return p; 178929b3822fSJohn Johansen 179029b3822fSJohn Johansen return NULL; 179129b3822fSJohn Johansen } 179229b3822fSJohn Johansen 179329b3822fSJohn Johansen /** 179429b3822fSJohn Johansen * next_profile - step to the next profile in where ever it may be 179529b3822fSJohn Johansen * @root: root namespace (NOT NULL) 179629b3822fSJohn Johansen * @profile: current profile (NOT NULL) 179729b3822fSJohn Johansen * 179829b3822fSJohn Johansen * Returns: next profile or NULL if there isn't one 179929b3822fSJohn Johansen */ 180098849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root, 180129b3822fSJohn Johansen struct aa_profile *profile) 180229b3822fSJohn Johansen { 180329b3822fSJohn Johansen struct aa_profile *next = __next_profile(profile); 180429b3822fSJohn Johansen if (next) 180529b3822fSJohn Johansen return next; 180629b3822fSJohn Johansen 180729b3822fSJohn Johansen /* finished all profiles in namespace move to next namespace */ 180898849dffSJohn Johansen return __first_profile(root, __next_ns(root, profile->ns)); 180929b3822fSJohn Johansen } 181029b3822fSJohn Johansen 181129b3822fSJohn Johansen /** 181229b3822fSJohn Johansen * p_start - start a depth first traversal of profile tree 181329b3822fSJohn Johansen * @f: seq_file to fill 181429b3822fSJohn Johansen * @pos: current position 181529b3822fSJohn Johansen * 181629b3822fSJohn Johansen * Returns: first profile under current namespace or NULL if none found 181729b3822fSJohn Johansen * 181829b3822fSJohn Johansen * acquires first ns->lock 181929b3822fSJohn Johansen */ 182029b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos) 182129b3822fSJohn Johansen { 182229b3822fSJohn Johansen struct aa_profile *profile = NULL; 182398849dffSJohn Johansen struct aa_ns *root = aa_current_profile()->ns; 182429b3822fSJohn Johansen loff_t l = *pos; 182598849dffSJohn Johansen f->private = aa_get_ns(root); 182629b3822fSJohn Johansen 182729b3822fSJohn Johansen 182829b3822fSJohn Johansen /* find the first profile */ 182929b3822fSJohn Johansen mutex_lock(&root->lock); 183029b3822fSJohn Johansen profile = __first_profile(root, root); 183129b3822fSJohn Johansen 183229b3822fSJohn Johansen /* skip to position */ 183329b3822fSJohn Johansen for (; profile && l > 0; l--) 183429b3822fSJohn Johansen profile = next_profile(root, profile); 183529b3822fSJohn Johansen 183629b3822fSJohn Johansen return profile; 183729b3822fSJohn Johansen } 183829b3822fSJohn Johansen 183929b3822fSJohn Johansen /** 184029b3822fSJohn Johansen * p_next - read the next profile entry 184129b3822fSJohn Johansen * @f: seq_file to fill 184229b3822fSJohn Johansen * @p: profile previously returned 184329b3822fSJohn Johansen * @pos: current position 184429b3822fSJohn Johansen * 184529b3822fSJohn Johansen * Returns: next profile after @p or NULL if none 184629b3822fSJohn Johansen * 184729b3822fSJohn Johansen * may acquire/release locks in namespace tree as necessary 184829b3822fSJohn Johansen */ 184929b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos) 185029b3822fSJohn Johansen { 185129b3822fSJohn Johansen struct aa_profile *profile = p; 185298849dffSJohn Johansen struct aa_ns *ns = f->private; 185329b3822fSJohn Johansen (*pos)++; 185429b3822fSJohn Johansen 185529b3822fSJohn Johansen return next_profile(ns, profile); 185629b3822fSJohn Johansen } 185729b3822fSJohn Johansen 185829b3822fSJohn Johansen /** 185929b3822fSJohn Johansen * p_stop - stop depth first traversal 186029b3822fSJohn Johansen * @f: seq_file we are filling 186129b3822fSJohn Johansen * @p: the last profile writen 186229b3822fSJohn Johansen * 186329b3822fSJohn Johansen * Release all locking done by p_start/p_next on namespace tree 186429b3822fSJohn Johansen */ 186529b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p) 186629b3822fSJohn Johansen { 186729b3822fSJohn Johansen struct aa_profile *profile = p; 186898849dffSJohn Johansen struct aa_ns *root = f->private, *ns; 186929b3822fSJohn Johansen 187029b3822fSJohn Johansen if (profile) { 187129b3822fSJohn Johansen for (ns = profile->ns; ns && ns != root; ns = ns->parent) 187229b3822fSJohn Johansen mutex_unlock(&ns->lock); 187329b3822fSJohn Johansen } 187429b3822fSJohn Johansen mutex_unlock(&root->lock); 187598849dffSJohn Johansen aa_put_ns(root); 187629b3822fSJohn Johansen } 187729b3822fSJohn Johansen 187829b3822fSJohn Johansen /** 187929b3822fSJohn Johansen * seq_show_profile - show a profile entry 188029b3822fSJohn Johansen * @f: seq_file to file 188129b3822fSJohn Johansen * @p: current position (profile) (NOT NULL) 188229b3822fSJohn Johansen * 188329b3822fSJohn Johansen * Returns: error on failure 188429b3822fSJohn Johansen */ 188529b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p) 188629b3822fSJohn Johansen { 188729b3822fSJohn Johansen struct aa_profile *profile = (struct aa_profile *)p; 188898849dffSJohn Johansen struct aa_ns *root = f->private; 188929b3822fSJohn Johansen 189029b3822fSJohn Johansen if (profile->ns != root) 189192b6d8efSJohn Johansen seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true)); 189229b3822fSJohn Johansen seq_printf(f, "%s (%s)\n", profile->base.hname, 189329b3822fSJohn Johansen aa_profile_mode_names[profile->mode]); 189429b3822fSJohn Johansen 189529b3822fSJohn Johansen return 0; 189629b3822fSJohn Johansen } 189729b3822fSJohn Johansen 1898c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = { 189929b3822fSJohn Johansen .start = p_start, 190029b3822fSJohn Johansen .next = p_next, 190129b3822fSJohn Johansen .stop = p_stop, 190229b3822fSJohn Johansen .show = seq_show_profile, 190329b3822fSJohn Johansen }; 190429b3822fSJohn Johansen 190529b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file) 190629b3822fSJohn Johansen { 19075ac8c355SJohn Johansen if (!policy_view_capable(NULL)) 19085ac8c355SJohn Johansen return -EACCES; 19095ac8c355SJohn Johansen 1910c97204baSJohn Johansen return seq_open(file, &aa_sfs_profiles_op); 191129b3822fSJohn Johansen } 191229b3822fSJohn Johansen 191329b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file) 191429b3822fSJohn Johansen { 191529b3822fSJohn Johansen return seq_release(inode, file); 191629b3822fSJohn Johansen } 191729b3822fSJohn Johansen 1918c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = { 191929b3822fSJohn Johansen .open = profiles_open, 192029b3822fSJohn Johansen .read = seq_read, 192129b3822fSJohn Johansen .llseek = seq_lseek, 192229b3822fSJohn Johansen .release = profiles_release, 192329b3822fSJohn Johansen }; 192429b3822fSJohn Johansen 192529b3822fSJohn Johansen 19260d259f04SJohn Johansen /** Base file system setup **/ 1927c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = { 1928c97204baSJohn Johansen AA_SFS_FILE_STRING("mask", 1929c97204baSJohn Johansen "create read write exec append mmap_exec link lock"), 1930a9bf8e9fSKees Cook { } 1931a9bf8e9fSKees Cook }; 1932a9bf8e9fSKees Cook 1933c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = { 1934c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hat", 1), 1935c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hatv", 1), 1936c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_onexec", 1), 1937c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_profile", 1), 1938c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), 1939c97204baSJohn Johansen AA_SFS_FILE_STRING("version", "1.2"), 1940e74abcf3SKees Cook { } 1941e74abcf3SKees Cook }; 1942e74abcf3SKees Cook 1943c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = { 1944c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("v5", 1), 1945474d6b75SJohn Johansen { } 1946474d6b75SJohn Johansen }; 1947474d6b75SJohn Johansen 1948c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = { 1949c97204baSJohn Johansen AA_SFS_DIR("versions", aa_sfs_entry_versions), 1950c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("set_load", 1), 19519d910a3bSJohn Johansen { } 19529d910a3bSJohn Johansen }; 19539d910a3bSJohn Johansen 1954a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = { 1955a83bd86eSJohn Johansen AA_SFS_FILE_BOOLEAN("data", 1), 1956*1dea3b41SJohn Johansen AA_SFS_FILE_BOOLEAN("multi_transaction", 1), 1957a83bd86eSJohn Johansen { } 1958a83bd86eSJohn Johansen }; 1959a83bd86eSJohn Johansen 1960a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = { 1961a83bd86eSJohn Johansen AA_SFS_DIR("label", aa_sfs_entry_query_label), 1962a83bd86eSJohn Johansen { } 1963a83bd86eSJohn Johansen }; 1964c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = { 1965c97204baSJohn Johansen AA_SFS_DIR("policy", aa_sfs_entry_policy), 1966c97204baSJohn Johansen AA_SFS_DIR("domain", aa_sfs_entry_domain), 1967c97204baSJohn Johansen AA_SFS_DIR("file", aa_sfs_entry_file), 1968c97204baSJohn Johansen AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 1969c97204baSJohn Johansen AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit), 1970c97204baSJohn Johansen AA_SFS_DIR("caps", aa_sfs_entry_caps), 1971a83bd86eSJohn Johansen AA_SFS_DIR("query", aa_sfs_entry_query), 1972e74abcf3SKees Cook { } 1973e74abcf3SKees Cook }; 1974e74abcf3SKees Cook 1975c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = { 1976c97204baSJohn Johansen AA_SFS_FILE_FOPS(".access", 0640, &aa_sfs_access), 1977c97204baSJohn Johansen AA_SFS_FILE_FOPS(".ns_level", 0666, &seq_ns_level_fops), 1978c97204baSJohn Johansen AA_SFS_FILE_FOPS(".ns_name", 0640, &seq_ns_name_fops), 1979c97204baSJohn Johansen AA_SFS_FILE_FOPS("profiles", 0440, &aa_sfs_profiles_fops), 1980c97204baSJohn Johansen AA_SFS_DIR("features", aa_sfs_entry_features), 19819acd494bSKees Cook { } 19829acd494bSKees Cook }; 198363e2b423SJohn Johansen 1984c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry = 1985c97204baSJohn Johansen AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor); 19869acd494bSKees Cook 19879acd494bSKees Cook /** 1988a481f4d9SJohn Johansen * entry_create_file - create a file entry in the apparmor securityfs 1989c97204baSJohn Johansen * @fs_file: aa_sfs_entry to build an entry for (NOT NULL) 19909acd494bSKees Cook * @parent: the parent dentry in the securityfs 19919acd494bSKees Cook * 1992a481f4d9SJohn Johansen * Use entry_remove_file to remove entries created with this fn. 19939acd494bSKees Cook */ 1994c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file, 19959acd494bSKees Cook struct dentry *parent) 199663e2b423SJohn Johansen { 19979acd494bSKees Cook int error = 0; 199863e2b423SJohn Johansen 19999acd494bSKees Cook fs_file->dentry = securityfs_create_file(fs_file->name, 20009acd494bSKees Cook S_IFREG | fs_file->mode, 20019acd494bSKees Cook parent, fs_file, 20029acd494bSKees Cook fs_file->file_ops); 20039acd494bSKees Cook if (IS_ERR(fs_file->dentry)) { 20049acd494bSKees Cook error = PTR_ERR(fs_file->dentry); 20059acd494bSKees Cook fs_file->dentry = NULL; 200663e2b423SJohn Johansen } 20079acd494bSKees Cook return error; 200863e2b423SJohn Johansen } 200963e2b423SJohn Johansen 2010c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir); 201163e2b423SJohn Johansen /** 2012a481f4d9SJohn Johansen * entry_create_dir - recursively create a directory entry in the securityfs 2013c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL) 20149acd494bSKees Cook * @parent: the parent dentry in the securityfs 201563e2b423SJohn Johansen * 2016a481f4d9SJohn Johansen * Use entry_remove_dir to remove entries created with this fn. 201763e2b423SJohn Johansen */ 2018c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir, 20199acd494bSKees Cook struct dentry *parent) 202063e2b423SJohn Johansen { 2021c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 20220d259f04SJohn Johansen struct dentry *dir; 20230d259f04SJohn Johansen int error; 202463e2b423SJohn Johansen 20250d259f04SJohn Johansen dir = securityfs_create_dir(fs_dir->name, parent); 20260d259f04SJohn Johansen if (IS_ERR(dir)) 20270d259f04SJohn Johansen return PTR_ERR(dir); 20280d259f04SJohn Johansen fs_dir->dentry = dir; 202963e2b423SJohn Johansen 20300d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2031c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2032a481f4d9SJohn Johansen error = entry_create_dir(fs_file, fs_dir->dentry); 20339acd494bSKees Cook else 2034a481f4d9SJohn Johansen error = entry_create_file(fs_file, fs_dir->dentry); 20359acd494bSKees Cook if (error) 20369acd494bSKees Cook goto failed; 20379acd494bSKees Cook } 20389acd494bSKees Cook 20399acd494bSKees Cook return 0; 20409acd494bSKees Cook 20419acd494bSKees Cook failed: 2042a481f4d9SJohn Johansen entry_remove_dir(fs_dir); 20430d259f04SJohn Johansen 20449acd494bSKees Cook return error; 20459acd494bSKees Cook } 20469acd494bSKees Cook 20479acd494bSKees Cook /** 2048c97204baSJohn Johansen * entry_remove_file - drop a single file entry in the apparmor securityfs 2049c97204baSJohn Johansen * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL) 20509acd494bSKees Cook */ 2051c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file) 20529acd494bSKees Cook { 20539acd494bSKees Cook if (!fs_file->dentry) 20549acd494bSKees Cook return; 20559acd494bSKees Cook 20569acd494bSKees Cook securityfs_remove(fs_file->dentry); 20579acd494bSKees Cook fs_file->dentry = NULL; 20589acd494bSKees Cook } 20599acd494bSKees Cook 20609acd494bSKees Cook /** 2061a481f4d9SJohn Johansen * entry_remove_dir - recursively drop a directory entry from the securityfs 2062c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL) 20639acd494bSKees Cook */ 2064c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir) 20659acd494bSKees Cook { 2066c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 20679acd494bSKees Cook 20680d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2069c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2070a481f4d9SJohn Johansen entry_remove_dir(fs_file); 20719acd494bSKees Cook else 2072c97204baSJohn Johansen entry_remove_file(fs_file); 20739acd494bSKees Cook } 20749acd494bSKees Cook 2075c97204baSJohn Johansen entry_remove_file(fs_dir); 207663e2b423SJohn Johansen } 207763e2b423SJohn Johansen 207863e2b423SJohn Johansen /** 207963e2b423SJohn Johansen * aa_destroy_aafs - cleanup and free aafs 208063e2b423SJohn Johansen * 208163e2b423SJohn Johansen * releases dentries allocated by aa_create_aafs 208263e2b423SJohn Johansen */ 208363e2b423SJohn Johansen void __init aa_destroy_aafs(void) 208463e2b423SJohn Johansen { 2085c97204baSJohn Johansen entry_remove_dir(&aa_sfs_entry); 208663e2b423SJohn Johansen } 208763e2b423SJohn Johansen 2088a71ada30SJohn Johansen 2089a71ada30SJohn Johansen #define NULL_FILE_NAME ".null" 2090a71ada30SJohn Johansen struct path aa_null; 2091a71ada30SJohn Johansen 2092a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent) 2093a71ada30SJohn Johansen { 2094a71ada30SJohn Johansen struct vfsmount *mount = NULL; 2095a71ada30SJohn Johansen struct dentry *dentry; 2096a71ada30SJohn Johansen struct inode *inode; 2097a71ada30SJohn Johansen int count = 0; 2098a71ada30SJohn Johansen int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); 2099a71ada30SJohn Johansen 2100a71ada30SJohn Johansen if (error) 2101a71ada30SJohn Johansen return error; 2102a71ada30SJohn Johansen 2103a71ada30SJohn Johansen inode_lock(d_inode(parent)); 2104a71ada30SJohn Johansen dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); 2105a71ada30SJohn Johansen if (IS_ERR(dentry)) { 2106a71ada30SJohn Johansen error = PTR_ERR(dentry); 2107a71ada30SJohn Johansen goto out; 2108a71ada30SJohn Johansen } 2109a71ada30SJohn Johansen inode = new_inode(parent->d_inode->i_sb); 2110a71ada30SJohn Johansen if (!inode) { 2111a71ada30SJohn Johansen error = -ENOMEM; 2112a71ada30SJohn Johansen goto out1; 2113a71ada30SJohn Johansen } 2114a71ada30SJohn Johansen 2115a71ada30SJohn Johansen inode->i_ino = get_next_ino(); 2116a71ada30SJohn Johansen inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; 211724d0d03cSDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2118a71ada30SJohn Johansen init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, 2119a71ada30SJohn Johansen MKDEV(MEM_MAJOR, 3)); 2120a71ada30SJohn Johansen d_instantiate(dentry, inode); 2121a71ada30SJohn Johansen aa_null.dentry = dget(dentry); 2122a71ada30SJohn Johansen aa_null.mnt = mntget(mount); 2123a71ada30SJohn Johansen 2124a71ada30SJohn Johansen error = 0; 2125a71ada30SJohn Johansen 2126a71ada30SJohn Johansen out1: 2127a71ada30SJohn Johansen dput(dentry); 2128a71ada30SJohn Johansen out: 2129a71ada30SJohn Johansen inode_unlock(d_inode(parent)); 2130a71ada30SJohn Johansen simple_release_fs(&mount, &count); 2131a71ada30SJohn Johansen return error; 2132a71ada30SJohn Johansen } 2133a71ada30SJohn Johansen 2134a481f4d9SJohn Johansen 2135a481f4d9SJohn Johansen 2136a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry, 2137a481f4d9SJohn Johansen struct inode *inode, 2138a481f4d9SJohn Johansen struct delayed_call *done) 2139a481f4d9SJohn Johansen { 2140a481f4d9SJohn Johansen struct aa_ns *ns; 2141a481f4d9SJohn Johansen struct path path; 2142a481f4d9SJohn Johansen 2143a481f4d9SJohn Johansen if (!dentry) 2144a481f4d9SJohn Johansen return ERR_PTR(-ECHILD); 2145a481f4d9SJohn Johansen ns = aa_get_current_ns(); 2146a481f4d9SJohn Johansen path.mnt = mntget(aafs_mnt); 2147a481f4d9SJohn Johansen path.dentry = dget(ns_dir(ns)); 2148a481f4d9SJohn Johansen nd_jump_link(&path); 2149a481f4d9SJohn Johansen aa_put_ns(ns); 2150a481f4d9SJohn Johansen 2151a481f4d9SJohn Johansen return NULL; 2152a481f4d9SJohn Johansen } 2153a481f4d9SJohn Johansen 2154a481f4d9SJohn Johansen static int ns_get_name(char *buf, size_t size, struct aa_ns *ns, 2155a481f4d9SJohn Johansen struct inode *inode) 2156a481f4d9SJohn Johansen { 2157a481f4d9SJohn Johansen int res = snprintf(buf, size, "%s:[%lu]", AAFS_NAME, inode->i_ino); 2158a481f4d9SJohn Johansen 2159a481f4d9SJohn Johansen if (res < 0 || res >= size) 2160a481f4d9SJohn Johansen res = -ENOENT; 2161a481f4d9SJohn Johansen 2162a481f4d9SJohn Johansen return res; 2163a481f4d9SJohn Johansen } 2164a481f4d9SJohn Johansen 2165a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer, 2166a481f4d9SJohn Johansen int buflen) 2167a481f4d9SJohn Johansen { 2168a481f4d9SJohn Johansen struct aa_ns *ns; 2169a481f4d9SJohn Johansen char name[32]; 2170a481f4d9SJohn Johansen int res; 2171a481f4d9SJohn Johansen 2172a481f4d9SJohn Johansen ns = aa_get_current_ns(); 2173a481f4d9SJohn Johansen res = ns_get_name(name, sizeof(name), ns, d_inode(dentry)); 2174a481f4d9SJohn Johansen if (res >= 0) 2175a481f4d9SJohn Johansen res = readlink_copy(buffer, buflen, name); 2176a481f4d9SJohn Johansen aa_put_ns(ns); 2177a481f4d9SJohn Johansen 2178a481f4d9SJohn Johansen return res; 2179a481f4d9SJohn Johansen } 2180a481f4d9SJohn Johansen 2181a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = { 2182a481f4d9SJohn Johansen .readlink = policy_readlink, 2183a481f4d9SJohn Johansen .get_link = policy_get_link, 2184a481f4d9SJohn Johansen }; 2185a481f4d9SJohn Johansen 2186a481f4d9SJohn Johansen 218763e2b423SJohn Johansen /** 218863e2b423SJohn Johansen * aa_create_aafs - create the apparmor security filesystem 218963e2b423SJohn Johansen * 219063e2b423SJohn Johansen * dentries created here are released by aa_destroy_aafs 219163e2b423SJohn Johansen * 219263e2b423SJohn Johansen * Returns: error on failure 219363e2b423SJohn Johansen */ 21943417d8d5SJames Morris static int __init aa_create_aafs(void) 219563e2b423SJohn Johansen { 2196b7fd2c03SJohn Johansen struct dentry *dent; 219763e2b423SJohn Johansen int error; 219863e2b423SJohn Johansen 219963e2b423SJohn Johansen if (!apparmor_initialized) 220063e2b423SJohn Johansen return 0; 220163e2b423SJohn Johansen 2202c97204baSJohn Johansen if (aa_sfs_entry.dentry) { 220363e2b423SJohn Johansen AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 220463e2b423SJohn Johansen return -EEXIST; 220563e2b423SJohn Johansen } 220663e2b423SJohn Johansen 2207a481f4d9SJohn Johansen /* setup apparmorfs used to virtualize policy/ */ 2208a481f4d9SJohn Johansen aafs_mnt = kern_mount(&aafs_ops); 2209a481f4d9SJohn Johansen if (IS_ERR(aafs_mnt)) 2210a481f4d9SJohn Johansen panic("can't set apparmorfs up\n"); 2211a481f4d9SJohn Johansen aafs_mnt->mnt_sb->s_flags &= ~MS_NOUSER; 2212a481f4d9SJohn Johansen 22139acd494bSKees Cook /* Populate fs tree. */ 2214c97204baSJohn Johansen error = entry_create_dir(&aa_sfs_entry, NULL); 221563e2b423SJohn Johansen if (error) 221663e2b423SJohn Johansen goto error; 221763e2b423SJohn Johansen 2218c97204baSJohn Johansen dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry, 2219b7fd2c03SJohn Johansen NULL, &aa_fs_profile_load); 2220b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 2221b7fd2c03SJohn Johansen error = PTR_ERR(dent); 2222b7fd2c03SJohn Johansen goto error; 2223b7fd2c03SJohn Johansen } 2224b7fd2c03SJohn Johansen ns_subload(root_ns) = dent; 2225b7fd2c03SJohn Johansen 2226c97204baSJohn Johansen dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry, 2227b7fd2c03SJohn Johansen NULL, &aa_fs_profile_replace); 2228b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 2229b7fd2c03SJohn Johansen error = PTR_ERR(dent); 2230b7fd2c03SJohn Johansen goto error; 2231b7fd2c03SJohn Johansen } 2232b7fd2c03SJohn Johansen ns_subreplace(root_ns) = dent; 2233b7fd2c03SJohn Johansen 2234c97204baSJohn Johansen dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry, 2235b7fd2c03SJohn Johansen NULL, &aa_fs_profile_remove); 2236b7fd2c03SJohn Johansen if (IS_ERR(dent)) { 2237b7fd2c03SJohn Johansen error = PTR_ERR(dent); 2238b7fd2c03SJohn Johansen goto error; 2239b7fd2c03SJohn Johansen } 2240b7fd2c03SJohn Johansen ns_subremove(root_ns) = dent; 2241b7fd2c03SJohn Johansen 2242d9bf2c26SJohn Johansen dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry, 2243d9bf2c26SJohn Johansen NULL, &aa_fs_ns_revision_fops); 2244d9bf2c26SJohn Johansen if (IS_ERR(dent)) { 2245d9bf2c26SJohn Johansen error = PTR_ERR(dent); 2246d9bf2c26SJohn Johansen goto error; 2247d9bf2c26SJohn Johansen } 2248d9bf2c26SJohn Johansen ns_subrevision(root_ns) = dent; 2249d9bf2c26SJohn Johansen 2250d9bf2c26SJohn Johansen /* policy tree referenced by magic policy symlink */ 2251b7fd2c03SJohn Johansen mutex_lock(&root_ns->lock); 2252c961ee5fSJohn Johansen error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy", 2253c961ee5fSJohn Johansen aafs_mnt->mnt_root); 2254b7fd2c03SJohn Johansen mutex_unlock(&root_ns->lock); 22550d259f04SJohn Johansen if (error) 22560d259f04SJohn Johansen goto error; 22570d259f04SJohn Johansen 2258c961ee5fSJohn Johansen /* magic symlink similar to nsfs redirects based on task policy */ 2259c961ee5fSJohn Johansen dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry, 2260c961ee5fSJohn Johansen NULL, &policy_link_iops); 2261c961ee5fSJohn Johansen if (IS_ERR(dent)) { 2262c961ee5fSJohn Johansen error = PTR_ERR(dent); 2263c961ee5fSJohn Johansen goto error; 2264c961ee5fSJohn Johansen } 2265c961ee5fSJohn Johansen 2266c97204baSJohn Johansen error = aa_mk_null_file(aa_sfs_entry.dentry); 2267a71ada30SJohn Johansen if (error) 2268a71ada30SJohn Johansen goto error; 2269a71ada30SJohn Johansen 2270a71ada30SJohn Johansen /* TODO: add default profile to apparmorfs */ 227163e2b423SJohn Johansen 227263e2b423SJohn Johansen /* Report that AppArmor fs is enabled */ 227363e2b423SJohn Johansen aa_info_message("AppArmor Filesystem Enabled"); 227463e2b423SJohn Johansen return 0; 227563e2b423SJohn Johansen 227663e2b423SJohn Johansen error: 227763e2b423SJohn Johansen aa_destroy_aafs(); 227863e2b423SJohn Johansen AA_ERROR("Error creating AppArmor securityfs\n"); 227963e2b423SJohn Johansen return error; 228063e2b423SJohn Johansen } 228163e2b423SJohn Johansen 228263e2b423SJohn Johansen fs_initcall(aa_create_aafs); 2283