163e2b423SJohn Johansen /* 263e2b423SJohn Johansen * AppArmor security module 363e2b423SJohn Johansen * 463e2b423SJohn Johansen * This file contains AppArmor /sys/kernel/security/apparmor interface functions 563e2b423SJohn Johansen * 663e2b423SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 763e2b423SJohn Johansen * Copyright 2009-2010 Canonical Ltd. 863e2b423SJohn Johansen * 963e2b423SJohn Johansen * This program is free software; you can redistribute it and/or 1063e2b423SJohn Johansen * modify it under the terms of the GNU General Public License as 1163e2b423SJohn Johansen * published by the Free Software Foundation, version 2 of the 1263e2b423SJohn Johansen * License. 1363e2b423SJohn Johansen */ 1463e2b423SJohn Johansen 150d259f04SJohn Johansen #include <linux/ctype.h> 1663e2b423SJohn Johansen #include <linux/security.h> 1763e2b423SJohn Johansen #include <linux/vmalloc.h> 18876979c9SPaul Gortmaker #include <linux/init.h> 1963e2b423SJohn Johansen #include <linux/seq_file.h> 2063e2b423SJohn Johansen #include <linux/uaccess.h> 21a71ada30SJohn Johansen #include <linux/mount.h> 2263e2b423SJohn Johansen #include <linux/namei.h> 23e74abcf3SKees Cook #include <linux/capability.h> 2429b3822fSJohn Johansen #include <linux/rcupdate.h> 25a71ada30SJohn Johansen #include <linux/fs.h> 26d9bf2c26SJohn Johansen #include <linux/poll.h> 27*63c16c3aSChris Coulson #include <linux/zlib.h> 28a481f4d9SJohn Johansen #include <uapi/linux/major.h> 29a481f4d9SJohn Johansen #include <uapi/linux/magic.h> 3063e2b423SJohn Johansen 3163e2b423SJohn Johansen #include "include/apparmor.h" 3263e2b423SJohn Johansen #include "include/apparmorfs.h" 3363e2b423SJohn Johansen #include "include/audit.h" 34d8889d49SJohn Johansen #include "include/cred.h" 35f8eb8a13SJohn Johansen #include "include/crypto.h" 36cd1dbf76SJohn Johansen #include "include/ipc.h" 37317d9a05SJohn Johansen #include "include/label.h" 3863e2b423SJohn Johansen #include "include/policy.h" 39cff281f6SJohn Johansen #include "include/policy_ns.h" 40d384b0a1SKees Cook #include "include/resource.h" 415ac8c355SJohn Johansen #include "include/policy_unpack.h" 4263e2b423SJohn Johansen 43c97204baSJohn Johansen /* 44c97204baSJohn Johansen * The apparmor filesystem interface used for policy load and introspection 45c97204baSJohn Johansen * The interface is split into two main components based on their function 46c97204baSJohn Johansen * a securityfs component: 47c97204baSJohn Johansen * used for static files that are always available, and which allows 48c97204baSJohn Johansen * userspace to specificy the location of the security filesystem. 49c97204baSJohn Johansen * 50c97204baSJohn Johansen * fns and data are prefixed with 51c97204baSJohn Johansen * aa_sfs_ 52c97204baSJohn Johansen * 53c97204baSJohn Johansen * an apparmorfs component: 54c97204baSJohn Johansen * used loaded policy content and introspection. It is not part of a 55c97204baSJohn Johansen * regular mounted filesystem and is available only through the magic 56c97204baSJohn Johansen * policy symlink in the root of the securityfs apparmor/ directory. 57c97204baSJohn Johansen * Tasks queries will be magically redirected to the correct portion 58c97204baSJohn Johansen * of the policy tree based on their confinement. 59c97204baSJohn Johansen * 60c97204baSJohn Johansen * fns and data are prefixed with 61c97204baSJohn Johansen * aafs_ 62c97204baSJohn Johansen * 63c97204baSJohn Johansen * The aa_fs_ prefix is used to indicate the fn is used by both the 64c97204baSJohn Johansen * securityfs and apparmorfs filesystems. 65c97204baSJohn Johansen */ 66c97204baSJohn Johansen 67c97204baSJohn Johansen 68c97204baSJohn Johansen /* 69c97204baSJohn Johansen * support fns 70c97204baSJohn Johansen */ 71c97204baSJohn Johansen 72*63c16c3aSChris Coulson struct rawdata_f_data { 73*63c16c3aSChris Coulson struct aa_loaddata *loaddata; 74*63c16c3aSChris Coulson }; 75*63c16c3aSChris Coulson 76*63c16c3aSChris Coulson #define RAWDATA_F_DATA_BUF(p) (char *)(p + 1) 77*63c16c3aSChris Coulson 78*63c16c3aSChris Coulson static void rawdata_f_data_free(struct rawdata_f_data *private) 79*63c16c3aSChris Coulson { 80*63c16c3aSChris Coulson if (!private) 81*63c16c3aSChris Coulson return; 82*63c16c3aSChris Coulson 83*63c16c3aSChris Coulson aa_put_loaddata(private->loaddata); 84*63c16c3aSChris Coulson kvfree(private); 85*63c16c3aSChris Coulson } 86*63c16c3aSChris Coulson 87*63c16c3aSChris Coulson static struct rawdata_f_data *rawdata_f_data_alloc(size_t size) 88*63c16c3aSChris Coulson { 89*63c16c3aSChris Coulson struct rawdata_f_data *ret; 90*63c16c3aSChris Coulson 91*63c16c3aSChris Coulson if (size > SIZE_MAX - sizeof(*ret)) 92*63c16c3aSChris Coulson return ERR_PTR(-EINVAL); 93*63c16c3aSChris Coulson 94*63c16c3aSChris Coulson ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL); 95*63c16c3aSChris Coulson if (!ret) 96*63c16c3aSChris Coulson return ERR_PTR(-ENOMEM); 97*63c16c3aSChris Coulson 98*63c16c3aSChris Coulson return ret; 99*63c16c3aSChris Coulson } 100*63c16c3aSChris Coulson 10163e2b423SJohn Johansen /** 1020d259f04SJohn Johansen * aa_mangle_name - mangle a profile name to std profile layout form 1030d259f04SJohn Johansen * @name: profile name to mangle (NOT NULL) 1040d259f04SJohn Johansen * @target: buffer to store mangled name, same length as @name (MAYBE NULL) 1050d259f04SJohn Johansen * 1060d259f04SJohn Johansen * Returns: length of mangled name 1070d259f04SJohn Johansen */ 108bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target) 1090d259f04SJohn Johansen { 1100d259f04SJohn Johansen char *t = target; 1110d259f04SJohn Johansen 1120d259f04SJohn Johansen while (*name == '/' || *name == '.') 1130d259f04SJohn Johansen name++; 1140d259f04SJohn Johansen 1150d259f04SJohn Johansen if (target) { 1160d259f04SJohn Johansen for (; *name; name++) { 1170d259f04SJohn Johansen if (*name == '/') 1180d259f04SJohn Johansen *(t)++ = '.'; 1190d259f04SJohn Johansen else if (isspace(*name)) 1200d259f04SJohn Johansen *(t)++ = '_'; 1210d259f04SJohn Johansen else if (isalnum(*name) || strchr("._-", *name)) 1220d259f04SJohn Johansen *(t)++ = *name; 1230d259f04SJohn Johansen } 1240d259f04SJohn Johansen 1250d259f04SJohn Johansen *t = 0; 1260d259f04SJohn Johansen } else { 1270d259f04SJohn Johansen int len = 0; 1280d259f04SJohn Johansen for (; *name; name++) { 1290d259f04SJohn Johansen if (isalnum(*name) || isspace(*name) || 1300d259f04SJohn Johansen strchr("/._-", *name)) 1310d259f04SJohn Johansen len++; 1320d259f04SJohn Johansen } 1330d259f04SJohn Johansen 1340d259f04SJohn Johansen return len; 1350d259f04SJohn Johansen } 1360d259f04SJohn Johansen 1370d259f04SJohn Johansen return t - target; 1380d259f04SJohn Johansen } 1390d259f04SJohn Johansen 140a481f4d9SJohn Johansen 141a481f4d9SJohn Johansen /* 142a481f4d9SJohn Johansen * aafs - core fns and data for the policy tree 143a481f4d9SJohn Johansen */ 144a481f4d9SJohn Johansen 145a481f4d9SJohn Johansen #define AAFS_NAME "apparmorfs" 146a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt; 147a481f4d9SJohn Johansen static int aafs_count; 148a481f4d9SJohn Johansen 149a481f4d9SJohn Johansen 150a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry) 151a481f4d9SJohn Johansen { 152a0781209SJohn Johansen seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino); 153a481f4d9SJohn Johansen return 0; 154a481f4d9SJohn Johansen } 155a481f4d9SJohn Johansen 156a481f4d9SJohn Johansen static void aafs_evict_inode(struct inode *inode) 157a481f4d9SJohn Johansen { 158a481f4d9SJohn Johansen truncate_inode_pages_final(&inode->i_data); 159a481f4d9SJohn Johansen clear_inode(inode); 160a481f4d9SJohn Johansen if (S_ISLNK(inode->i_mode)) 161a481f4d9SJohn Johansen kfree(inode->i_link); 162a481f4d9SJohn Johansen } 163a481f4d9SJohn Johansen 164a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = { 165a481f4d9SJohn Johansen .statfs = simple_statfs, 166a481f4d9SJohn Johansen .evict_inode = aafs_evict_inode, 167a481f4d9SJohn Johansen .show_path = aafs_show_path, 168a481f4d9SJohn Johansen }; 169a481f4d9SJohn Johansen 170a481f4d9SJohn Johansen static int fill_super(struct super_block *sb, void *data, int silent) 171a481f4d9SJohn Johansen { 172a481f4d9SJohn Johansen static struct tree_descr files[] = { {""} }; 173a481f4d9SJohn Johansen int error; 174a481f4d9SJohn Johansen 175a481f4d9SJohn Johansen error = simple_fill_super(sb, AAFS_MAGIC, files); 176a481f4d9SJohn Johansen if (error) 177a481f4d9SJohn Johansen return error; 178a481f4d9SJohn Johansen sb->s_op = &aafs_super_ops; 179a481f4d9SJohn Johansen 180a481f4d9SJohn Johansen return 0; 181a481f4d9SJohn Johansen } 182a481f4d9SJohn Johansen 183a481f4d9SJohn Johansen static struct dentry *aafs_mount(struct file_system_type *fs_type, 184a481f4d9SJohn Johansen int flags, const char *dev_name, void *data) 185a481f4d9SJohn Johansen { 186a481f4d9SJohn Johansen return mount_single(fs_type, flags, data, fill_super); 187a481f4d9SJohn Johansen } 188a481f4d9SJohn Johansen 189a481f4d9SJohn Johansen static struct file_system_type aafs_ops = { 190a481f4d9SJohn Johansen .owner = THIS_MODULE, 191a481f4d9SJohn Johansen .name = AAFS_NAME, 192a481f4d9SJohn Johansen .mount = aafs_mount, 193a481f4d9SJohn Johansen .kill_sb = kill_anon_super, 194a481f4d9SJohn Johansen }; 195a481f4d9SJohn Johansen 196a481f4d9SJohn Johansen /** 197a481f4d9SJohn Johansen * __aafs_setup_d_inode - basic inode setup for apparmorfs 198a481f4d9SJohn Johansen * @dir: parent directory for the dentry 199a481f4d9SJohn Johansen * @dentry: dentry we are seting the inode up for 200a481f4d9SJohn Johansen * @mode: permissions the file should have 201a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 202a481f4d9SJohn Johansen * @link: if symlink, symlink target string 203a481f4d9SJohn Johansen * @fops: struct file_operations that should be used 204a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 205a481f4d9SJohn Johansen */ 206a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry, 207a481f4d9SJohn Johansen umode_t mode, void *data, char *link, 208a481f4d9SJohn Johansen const struct file_operations *fops, 209a481f4d9SJohn Johansen const struct inode_operations *iops) 210a481f4d9SJohn Johansen { 211a481f4d9SJohn Johansen struct inode *inode = new_inode(dir->i_sb); 212a481f4d9SJohn Johansen 213a481f4d9SJohn Johansen AA_BUG(!dir); 214a481f4d9SJohn Johansen AA_BUG(!dentry); 215a481f4d9SJohn Johansen 216a481f4d9SJohn Johansen if (!inode) 217a481f4d9SJohn Johansen return -ENOMEM; 218a481f4d9SJohn Johansen 219a481f4d9SJohn Johansen inode->i_ino = get_next_ino(); 220a481f4d9SJohn Johansen inode->i_mode = mode; 221a481f4d9SJohn Johansen inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 222a481f4d9SJohn Johansen inode->i_private = data; 223a481f4d9SJohn Johansen if (S_ISDIR(mode)) { 224a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_dir_inode_operations; 225a481f4d9SJohn Johansen inode->i_fop = &simple_dir_operations; 226a481f4d9SJohn Johansen inc_nlink(inode); 227a481f4d9SJohn Johansen inc_nlink(dir); 228a481f4d9SJohn Johansen } else if (S_ISLNK(mode)) { 229a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_symlink_inode_operations; 230a481f4d9SJohn Johansen inode->i_link = link; 231a481f4d9SJohn Johansen } else { 232a481f4d9SJohn Johansen inode->i_fop = fops; 233a481f4d9SJohn Johansen } 234a481f4d9SJohn Johansen d_instantiate(dentry, inode); 235a481f4d9SJohn Johansen dget(dentry); 236a481f4d9SJohn Johansen 237a481f4d9SJohn Johansen return 0; 238a481f4d9SJohn Johansen } 239a481f4d9SJohn Johansen 240a481f4d9SJohn Johansen /** 241a481f4d9SJohn Johansen * aafs_create - create a dentry in the apparmorfs filesystem 242a481f4d9SJohn Johansen * 243a481f4d9SJohn Johansen * @name: name of dentry to create 244a481f4d9SJohn Johansen * @mode: permissions the file should have 245a481f4d9SJohn Johansen * @parent: parent directory for this dentry 246a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 247a481f4d9SJohn Johansen * @link: if symlink, symlink target string 248a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 249a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 250a481f4d9SJohn Johansen * 251a481f4d9SJohn Johansen * This is the basic "create a xxx" function for apparmorfs. 252a481f4d9SJohn Johansen * 253a481f4d9SJohn Johansen * Returns a pointer to a dentry if it succeeds, that must be free with 254a481f4d9SJohn Johansen * aafs_remove(). Will return ERR_PTR on failure. 255a481f4d9SJohn Johansen */ 256a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode, 257a481f4d9SJohn Johansen struct dentry *parent, void *data, void *link, 258a481f4d9SJohn Johansen const struct file_operations *fops, 259a481f4d9SJohn Johansen const struct inode_operations *iops) 260a481f4d9SJohn Johansen { 261a481f4d9SJohn Johansen struct dentry *dentry; 262a481f4d9SJohn Johansen struct inode *dir; 263a481f4d9SJohn Johansen int error; 264a481f4d9SJohn Johansen 265a481f4d9SJohn Johansen AA_BUG(!name); 266a481f4d9SJohn Johansen AA_BUG(!parent); 267a481f4d9SJohn Johansen 268a481f4d9SJohn Johansen if (!(mode & S_IFMT)) 269a481f4d9SJohn Johansen mode = (mode & S_IALLUGO) | S_IFREG; 270a481f4d9SJohn Johansen 271a481f4d9SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 272a481f4d9SJohn Johansen if (error) 273a481f4d9SJohn Johansen return ERR_PTR(error); 274a481f4d9SJohn Johansen 275a481f4d9SJohn Johansen dir = d_inode(parent); 276a481f4d9SJohn Johansen 277a481f4d9SJohn Johansen inode_lock(dir); 278a481f4d9SJohn Johansen dentry = lookup_one_len(name, parent, strlen(name)); 2795d314a81SDan Carpenter if (IS_ERR(dentry)) { 2805d314a81SDan Carpenter error = PTR_ERR(dentry); 281a481f4d9SJohn Johansen goto fail_lock; 2825d314a81SDan Carpenter } 283a481f4d9SJohn Johansen 284a481f4d9SJohn Johansen if (d_really_is_positive(dentry)) { 285a481f4d9SJohn Johansen error = -EEXIST; 286a481f4d9SJohn Johansen goto fail_dentry; 287a481f4d9SJohn Johansen } 288a481f4d9SJohn Johansen 289a481f4d9SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops); 290a481f4d9SJohn Johansen if (error) 291a481f4d9SJohn Johansen goto fail_dentry; 292a481f4d9SJohn Johansen inode_unlock(dir); 293a481f4d9SJohn Johansen 294a481f4d9SJohn Johansen return dentry; 295a481f4d9SJohn Johansen 296a481f4d9SJohn Johansen fail_dentry: 297a481f4d9SJohn Johansen dput(dentry); 298a481f4d9SJohn Johansen 299a481f4d9SJohn Johansen fail_lock: 300a481f4d9SJohn Johansen inode_unlock(dir); 301a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 302a481f4d9SJohn Johansen 303a481f4d9SJohn Johansen return ERR_PTR(error); 304a481f4d9SJohn Johansen } 305a481f4d9SJohn Johansen 306a481f4d9SJohn Johansen /** 307a481f4d9SJohn Johansen * aafs_create_file - create a file in the apparmorfs filesystem 308a481f4d9SJohn Johansen * 309a481f4d9SJohn Johansen * @name: name of dentry to create 310a481f4d9SJohn Johansen * @mode: permissions the file should have 311a481f4d9SJohn Johansen * @parent: parent directory for this dentry 312a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 313a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 314a481f4d9SJohn Johansen * 315a481f4d9SJohn Johansen * see aafs_create 316a481f4d9SJohn Johansen */ 317a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode, 318a481f4d9SJohn Johansen struct dentry *parent, void *data, 319a481f4d9SJohn Johansen const struct file_operations *fops) 320a481f4d9SJohn Johansen { 321a481f4d9SJohn Johansen return aafs_create(name, mode, parent, data, NULL, fops, NULL); 322a481f4d9SJohn Johansen } 323a481f4d9SJohn Johansen 324a481f4d9SJohn Johansen /** 325a481f4d9SJohn Johansen * aafs_create_dir - create a directory in the apparmorfs filesystem 326a481f4d9SJohn Johansen * 327a481f4d9SJohn Johansen * @name: name of dentry to create 328a481f4d9SJohn Johansen * @parent: parent directory for this dentry 329a481f4d9SJohn Johansen * 330a481f4d9SJohn Johansen * see aafs_create 331a481f4d9SJohn Johansen */ 332a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent) 333a481f4d9SJohn Johansen { 334a481f4d9SJohn Johansen return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL, 335a481f4d9SJohn Johansen NULL); 336a481f4d9SJohn Johansen } 337a481f4d9SJohn Johansen 338a481f4d9SJohn Johansen /** 339a481f4d9SJohn Johansen * aafs_create_symlink - create a symlink in the apparmorfs filesystem 340a481f4d9SJohn Johansen * @name: name of dentry to create 341a481f4d9SJohn Johansen * @parent: parent directory for this dentry 342a481f4d9SJohn Johansen * @target: if symlink, symlink target string 3431180b4c7SJohn Johansen * @private: private data 344a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 345a481f4d9SJohn Johansen * 346a481f4d9SJohn Johansen * If @target parameter is %NULL, then the @iops parameter needs to be 347a481f4d9SJohn Johansen * setup to handle .readlink and .get_link inode_operations. 348a481f4d9SJohn Johansen */ 349a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name, 350a481f4d9SJohn Johansen struct dentry *parent, 351a481f4d9SJohn Johansen const char *target, 3521180b4c7SJohn Johansen void *private, 353a481f4d9SJohn Johansen const struct inode_operations *iops) 354a481f4d9SJohn Johansen { 355a481f4d9SJohn Johansen struct dentry *dent; 356a481f4d9SJohn Johansen char *link = NULL; 357a481f4d9SJohn Johansen 358a481f4d9SJohn Johansen if (target) { 359a481f4d9SJohn Johansen if (!link) 360a481f4d9SJohn Johansen return ERR_PTR(-ENOMEM); 361a481f4d9SJohn Johansen } 3621180b4c7SJohn Johansen dent = aafs_create(name, S_IFLNK | 0444, parent, private, link, NULL, 363a481f4d9SJohn Johansen iops); 364a481f4d9SJohn Johansen if (IS_ERR(dent)) 365a481f4d9SJohn Johansen kfree(link); 366a481f4d9SJohn Johansen 367a481f4d9SJohn Johansen return dent; 368a481f4d9SJohn Johansen } 369a481f4d9SJohn Johansen 370a481f4d9SJohn Johansen /** 371a481f4d9SJohn Johansen * aafs_remove - removes a file or directory from the apparmorfs filesystem 372a481f4d9SJohn Johansen * 373a481f4d9SJohn Johansen * @dentry: dentry of the file/directory/symlink to removed. 374a481f4d9SJohn Johansen */ 375a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry) 376a481f4d9SJohn Johansen { 377a481f4d9SJohn Johansen struct inode *dir; 378a481f4d9SJohn Johansen 379a481f4d9SJohn Johansen if (!dentry || IS_ERR(dentry)) 380a481f4d9SJohn Johansen return; 381a481f4d9SJohn Johansen 382a481f4d9SJohn Johansen dir = d_inode(dentry->d_parent); 383a481f4d9SJohn Johansen inode_lock(dir); 384a481f4d9SJohn Johansen if (simple_positive(dentry)) { 385a481f4d9SJohn Johansen if (d_is_dir(dentry)) 386a481f4d9SJohn Johansen simple_rmdir(dir, dentry); 387a481f4d9SJohn Johansen else 388a481f4d9SJohn Johansen simple_unlink(dir, dentry); 389201218e4SChris Coulson d_delete(dentry); 390a481f4d9SJohn Johansen dput(dentry); 391a481f4d9SJohn Johansen } 392a481f4d9SJohn Johansen inode_unlock(dir); 393a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 394a481f4d9SJohn Johansen } 395a481f4d9SJohn Johansen 396a481f4d9SJohn Johansen 397a481f4d9SJohn Johansen /* 398a481f4d9SJohn Johansen * aa_fs - policy load/replace/remove 399a481f4d9SJohn Johansen */ 400a481f4d9SJohn Johansen 4010d259f04SJohn Johansen /** 40263e2b423SJohn Johansen * aa_simple_write_to_buffer - common routine for getting policy from user 40363e2b423SJohn Johansen * @userbuf: user buffer to copy data from (NOT NULL) 4043ed02adaSJohn Johansen * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 40563e2b423SJohn Johansen * @copy_size: size of data to copy from user buffer 40663e2b423SJohn Johansen * @pos: position write is at in the file (NOT NULL) 40763e2b423SJohn Johansen * 40863e2b423SJohn Johansen * Returns: kernel buffer containing copy of user buffer data or an 40963e2b423SJohn Johansen * ERR_PTR on failure. 41063e2b423SJohn Johansen */ 4115ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, 4125ac8c355SJohn Johansen size_t alloc_size, 4135ac8c355SJohn Johansen size_t copy_size, 41463e2b423SJohn Johansen loff_t *pos) 41563e2b423SJohn Johansen { 4165ac8c355SJohn Johansen struct aa_loaddata *data; 41763e2b423SJohn Johansen 418e6bfa25dSJohn Johansen AA_BUG(copy_size > alloc_size); 4193ed02adaSJohn Johansen 42063e2b423SJohn Johansen if (*pos != 0) 42163e2b423SJohn Johansen /* only writes from pos 0, that is complete writes */ 42263e2b423SJohn Johansen return ERR_PTR(-ESPIPE); 42363e2b423SJohn Johansen 42463e2b423SJohn Johansen /* freed by caller to simple_write_to_buffer */ 4255d5182caSJohn Johansen data = aa_loaddata_alloc(alloc_size); 4265d5182caSJohn Johansen if (IS_ERR(data)) 4275d5182caSJohn Johansen return data; 42863e2b423SJohn Johansen 4295d5182caSJohn Johansen data->size = copy_size; 4305ac8c355SJohn Johansen if (copy_from_user(data->data, userbuf, copy_size)) { 43163e2b423SJohn Johansen kvfree(data); 43263e2b423SJohn Johansen return ERR_PTR(-EFAULT); 43363e2b423SJohn Johansen } 43463e2b423SJohn Johansen 43563e2b423SJohn Johansen return data; 43663e2b423SJohn Johansen } 43763e2b423SJohn Johansen 43818e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size, 439b7fd2c03SJohn Johansen loff_t *pos, struct aa_ns *ns) 4405ac8c355SJohn Johansen { 4415ac8c355SJohn Johansen struct aa_loaddata *data; 442637f688dSJohn Johansen struct aa_label *label; 443637f688dSJohn Johansen ssize_t error; 444cf797c0eSJohn Johansen 445637f688dSJohn Johansen label = begin_current_label_crit_section(); 446cf797c0eSJohn Johansen 4475ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4485ac8c355SJohn Johansen * below after unpack 4495ac8c355SJohn Johansen */ 450637f688dSJohn Johansen error = aa_may_manage_policy(label, ns, mask); 4515ac8c355SJohn Johansen if (error) 4525ac8c355SJohn Johansen return error; 45363e2b423SJohn Johansen 4545ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size, size, pos); 4555ac8c355SJohn Johansen error = PTR_ERR(data); 4565ac8c355SJohn Johansen if (!IS_ERR(data)) { 457637f688dSJohn Johansen error = aa_replace_profiles(ns, label, mask, data); 4585ac8c355SJohn Johansen aa_put_loaddata(data); 4595ac8c355SJohn Johansen } 460637f688dSJohn Johansen end_current_label_crit_section(label); 4615ac8c355SJohn Johansen 4625ac8c355SJohn Johansen return error; 4635ac8c355SJohn Johansen } 4645ac8c355SJohn Johansen 465b7fd2c03SJohn Johansen /* .load file hook fn to load policy */ 46663e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 46763e2b423SJohn Johansen loff_t *pos) 46863e2b423SJohn Johansen { 469b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 47018e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns); 471b7fd2c03SJohn Johansen 472b7fd2c03SJohn Johansen aa_put_ns(ns); 47363e2b423SJohn Johansen 47463e2b423SJohn Johansen return error; 47563e2b423SJohn Johansen } 47663e2b423SJohn Johansen 47763e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = { 4786038f373SArnd Bergmann .write = profile_load, 4796038f373SArnd Bergmann .llseek = default_llseek, 48063e2b423SJohn Johansen }; 48163e2b423SJohn Johansen 48263e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */ 48363e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf, 48463e2b423SJohn Johansen size_t size, loff_t *pos) 48563e2b423SJohn Johansen { 486b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 48718e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY, 48818e99f19SJohn Johansen buf, size, pos, ns); 489b7fd2c03SJohn Johansen aa_put_ns(ns); 49063e2b423SJohn Johansen 49163e2b423SJohn Johansen return error; 49263e2b423SJohn Johansen } 49363e2b423SJohn Johansen 49463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = { 4956038f373SArnd Bergmann .write = profile_replace, 4966038f373SArnd Bergmann .llseek = default_llseek, 49763e2b423SJohn Johansen }; 49863e2b423SJohn Johansen 499b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */ 50063e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf, 50163e2b423SJohn Johansen size_t size, loff_t *pos) 50263e2b423SJohn Johansen { 5035ac8c355SJohn Johansen struct aa_loaddata *data; 504637f688dSJohn Johansen struct aa_label *label; 50563e2b423SJohn Johansen ssize_t error; 506b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 50763e2b423SJohn Johansen 508637f688dSJohn Johansen label = begin_current_label_crit_section(); 5095ac8c355SJohn Johansen /* high level check about policy management - fine grained in 5105ac8c355SJohn Johansen * below after unpack 5115ac8c355SJohn Johansen */ 512637f688dSJohn Johansen error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY); 5135ac8c355SJohn Johansen if (error) 5145ac8c355SJohn Johansen goto out; 5155ac8c355SJohn Johansen 51663e2b423SJohn Johansen /* 51763e2b423SJohn Johansen * aa_remove_profile needs a null terminated string so 1 extra 51863e2b423SJohn Johansen * byte is allocated and the copied data is null terminated. 51963e2b423SJohn Johansen */ 5205ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size + 1, size, pos); 52163e2b423SJohn Johansen 52263e2b423SJohn Johansen error = PTR_ERR(data); 52363e2b423SJohn Johansen if (!IS_ERR(data)) { 5245ac8c355SJohn Johansen data->data[size] = 0; 525637f688dSJohn Johansen error = aa_remove_profiles(ns, label, data->data, size); 5265ac8c355SJohn Johansen aa_put_loaddata(data); 52763e2b423SJohn Johansen } 5285ac8c355SJohn Johansen out: 529637f688dSJohn Johansen end_current_label_crit_section(label); 530b7fd2c03SJohn Johansen aa_put_ns(ns); 53163e2b423SJohn Johansen return error; 53263e2b423SJohn Johansen } 53363e2b423SJohn Johansen 53463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = { 5356038f373SArnd Bergmann .write = profile_remove, 5366038f373SArnd Bergmann .llseek = default_llseek, 53763e2b423SJohn Johansen }; 53863e2b423SJohn Johansen 539d9bf2c26SJohn Johansen struct aa_revision { 540d9bf2c26SJohn Johansen struct aa_ns *ns; 541d9bf2c26SJohn Johansen long last_read; 542d9bf2c26SJohn Johansen }; 543d9bf2c26SJohn Johansen 544d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */ 545d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file) 546d9bf2c26SJohn Johansen { 547d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 548d9bf2c26SJohn Johansen 549d9bf2c26SJohn Johansen if (rev) { 550d9bf2c26SJohn Johansen aa_put_ns(rev->ns); 551d9bf2c26SJohn Johansen kfree(rev); 552d9bf2c26SJohn Johansen } 553d9bf2c26SJohn Johansen 554d9bf2c26SJohn Johansen return 0; 555d9bf2c26SJohn Johansen } 556d9bf2c26SJohn Johansen 557d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf, 558d9bf2c26SJohn Johansen size_t size, loff_t *ppos) 559d9bf2c26SJohn Johansen { 560d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 561d9bf2c26SJohn Johansen char buffer[32]; 562d9bf2c26SJohn Johansen long last_read; 563d9bf2c26SJohn Johansen int avail; 564d9bf2c26SJohn Johansen 565feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 566d9bf2c26SJohn Johansen last_read = rev->last_read; 567d9bf2c26SJohn Johansen if (last_read == rev->ns->revision) { 568d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 569d9bf2c26SJohn Johansen if (file->f_flags & O_NONBLOCK) 570d9bf2c26SJohn Johansen return -EAGAIN; 571d9bf2c26SJohn Johansen if (wait_event_interruptible(rev->ns->wait, 572d9bf2c26SJohn Johansen last_read != 573d9bf2c26SJohn Johansen READ_ONCE(rev->ns->revision))) 574d9bf2c26SJohn Johansen return -ERESTARTSYS; 575feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 576d9bf2c26SJohn Johansen } 577d9bf2c26SJohn Johansen 578d9bf2c26SJohn Johansen avail = sprintf(buffer, "%ld\n", rev->ns->revision); 579d9bf2c26SJohn Johansen if (*ppos + size > avail) { 580d9bf2c26SJohn Johansen rev->last_read = rev->ns->revision; 581d9bf2c26SJohn Johansen *ppos = 0; 582d9bf2c26SJohn Johansen } 583d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 584d9bf2c26SJohn Johansen 585d9bf2c26SJohn Johansen return simple_read_from_buffer(buf, size, ppos, buffer, avail); 586d9bf2c26SJohn Johansen } 587d9bf2c26SJohn Johansen 588d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file) 589d9bf2c26SJohn Johansen { 590d9bf2c26SJohn Johansen struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL); 591d9bf2c26SJohn Johansen 592d9bf2c26SJohn Johansen if (!rev) 593d9bf2c26SJohn Johansen return -ENOMEM; 594d9bf2c26SJohn Johansen 595d9bf2c26SJohn Johansen rev->ns = aa_get_ns(inode->i_private); 596d9bf2c26SJohn Johansen if (!rev->ns) 597d9bf2c26SJohn Johansen rev->ns = aa_get_current_ns(); 598d9bf2c26SJohn Johansen file->private_data = rev; 599d9bf2c26SJohn Johansen 600d9bf2c26SJohn Johansen return 0; 601d9bf2c26SJohn Johansen } 602d9bf2c26SJohn Johansen 603e6c5a7d9SAl Viro static __poll_t ns_revision_poll(struct file *file, poll_table *pt) 604d9bf2c26SJohn Johansen { 605d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 606e6c5a7d9SAl Viro __poll_t mask = 0; 607d9bf2c26SJohn Johansen 608d9bf2c26SJohn Johansen if (rev) { 609feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 610d9bf2c26SJohn Johansen poll_wait(file, &rev->ns->wait, pt); 611d9bf2c26SJohn Johansen if (rev->last_read < rev->ns->revision) 612a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 613d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 614d9bf2c26SJohn Johansen } 615d9bf2c26SJohn Johansen 616d9bf2c26SJohn Johansen return mask; 617d9bf2c26SJohn Johansen } 618d9bf2c26SJohn Johansen 6195d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns) 6205d5182caSJohn Johansen { 6215d5182caSJohn Johansen ns->revision++; 622d9bf2c26SJohn Johansen wake_up_interruptible(&ns->wait); 6235d5182caSJohn Johansen } 6245d5182caSJohn Johansen 625d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = { 626d9bf2c26SJohn Johansen .owner = THIS_MODULE, 627d9bf2c26SJohn Johansen .open = ns_revision_open, 628d9bf2c26SJohn Johansen .poll = ns_revision_poll, 629d9bf2c26SJohn Johansen .read = ns_revision_read, 630d9bf2c26SJohn Johansen .llseek = generic_file_llseek, 631d9bf2c26SJohn Johansen .release = ns_revision_release, 632d9bf2c26SJohn Johansen }; 633d9bf2c26SJohn Johansen 6344f3b3f2dSJohn Johansen static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms, 6354f3b3f2dSJohn Johansen const char *match_str, size_t match_len) 6364f3b3f2dSJohn Johansen { 637f4585bc2STyler Hicks struct aa_perms tmp = { }; 6384f3b3f2dSJohn Johansen struct aa_dfa *dfa; 6394f3b3f2dSJohn Johansen unsigned int state = 0; 6404f3b3f2dSJohn Johansen 641637f688dSJohn Johansen if (profile_unconfined(profile)) 6424f3b3f2dSJohn Johansen return; 6434f3b3f2dSJohn Johansen if (profile->file.dfa && *match_str == AA_CLASS_FILE) { 6444f3b3f2dSJohn Johansen dfa = profile->file.dfa; 6454f3b3f2dSJohn Johansen state = aa_dfa_match_len(dfa, profile->file.start, 6464f3b3f2dSJohn Johansen match_str + 1, match_len - 1); 6474f3b3f2dSJohn Johansen if (state) { 6484f3b3f2dSJohn Johansen struct path_cond cond = { }; 6494f3b3f2dSJohn Johansen 6504f3b3f2dSJohn Johansen tmp = aa_compute_fperms(dfa, state, &cond); 6514f3b3f2dSJohn Johansen } 6524f3b3f2dSJohn Johansen } else if (profile->policy.dfa) { 653b9590ad4SJohn Johansen if (!PROFILE_MEDIATES(profile, *match_str)) 6544f3b3f2dSJohn Johansen return; /* no change to current perms */ 6554f3b3f2dSJohn Johansen dfa = profile->policy.dfa; 6564f3b3f2dSJohn Johansen state = aa_dfa_match_len(dfa, profile->policy.start[0], 6574f3b3f2dSJohn Johansen match_str, match_len); 6584f3b3f2dSJohn Johansen if (state) 6594f3b3f2dSJohn Johansen aa_compute_perms(dfa, state, &tmp); 6604f3b3f2dSJohn Johansen } 6614f3b3f2dSJohn Johansen aa_apply_modes_to_perms(profile, &tmp); 662317d9a05SJohn Johansen aa_perms_accum_raw(perms, &tmp); 6634f3b3f2dSJohn Johansen } 6644f3b3f2dSJohn Johansen 6654f3b3f2dSJohn Johansen 666e025be0fSWilliam Hua /** 667e025be0fSWilliam Hua * query_data - queries a policy and writes its data to buf 668e025be0fSWilliam Hua * @buf: the resulting data is stored here (NOT NULL) 669e025be0fSWilliam Hua * @buf_len: size of buf 670e025be0fSWilliam Hua * @query: query string used to retrieve data 671e025be0fSWilliam Hua * @query_len: size of query including second NUL byte 672e025be0fSWilliam Hua * 673e025be0fSWilliam Hua * The buffers pointed to by buf and query may overlap. The query buffer is 674e025be0fSWilliam Hua * parsed before buf is written to. 675e025be0fSWilliam Hua * 676e025be0fSWilliam Hua * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of 677e025be0fSWilliam Hua * the security confinement context and <KEY> is the name of the data to 678e025be0fSWilliam Hua * retrieve. <LABEL> and <KEY> must not be NUL-terminated. 679e025be0fSWilliam Hua * 680e025be0fSWilliam Hua * Don't expect the contents of buf to be preserved on failure. 681e025be0fSWilliam Hua * 682e025be0fSWilliam Hua * Returns: number of characters written to buf or -errno on failure 683e025be0fSWilliam Hua */ 684e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len, 685e025be0fSWilliam Hua char *query, size_t query_len) 686e025be0fSWilliam Hua { 687e025be0fSWilliam Hua char *out; 688e025be0fSWilliam Hua const char *key; 689317d9a05SJohn Johansen struct label_it i; 690637f688dSJohn Johansen struct aa_label *label, *curr; 691317d9a05SJohn Johansen struct aa_profile *profile; 692e025be0fSWilliam Hua struct aa_data *data; 693e025be0fSWilliam Hua u32 bytes, blocks; 694e025be0fSWilliam Hua __le32 outle32; 695e025be0fSWilliam Hua 696e025be0fSWilliam Hua if (!query_len) 697e025be0fSWilliam Hua return -EINVAL; /* need a query */ 698e025be0fSWilliam Hua 699e025be0fSWilliam Hua key = query + strnlen(query, query_len) + 1; 700e025be0fSWilliam Hua if (key + 1 >= query + query_len) 701e025be0fSWilliam Hua return -EINVAL; /* not enough space for a non-empty key */ 702e025be0fSWilliam Hua if (key + strnlen(key, query + query_len - key) >= query + query_len) 703e025be0fSWilliam Hua return -EINVAL; /* must end with NUL */ 704e025be0fSWilliam Hua 705e025be0fSWilliam Hua if (buf_len < sizeof(bytes) + sizeof(blocks)) 706e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 707e025be0fSWilliam Hua 708637f688dSJohn Johansen curr = begin_current_label_crit_section(); 709637f688dSJohn Johansen label = aa_label_parse(curr, query, GFP_KERNEL, false, false); 710637f688dSJohn Johansen end_current_label_crit_section(curr); 711637f688dSJohn Johansen if (IS_ERR(label)) 712637f688dSJohn Johansen return PTR_ERR(label); 713e025be0fSWilliam Hua 714e025be0fSWilliam Hua /* We are going to leave space for two numbers. The first is the total 715e025be0fSWilliam Hua * number of bytes we are writing after the first number. This is so 716e025be0fSWilliam Hua * users can read the full output without reallocation. 717e025be0fSWilliam Hua * 718e025be0fSWilliam Hua * The second number is the number of data blocks we're writing. An 719e025be0fSWilliam Hua * application might be confined by multiple policies having data in 720e025be0fSWilliam Hua * the same key. 721e025be0fSWilliam Hua */ 722e025be0fSWilliam Hua memset(buf, 0, sizeof(bytes) + sizeof(blocks)); 723e025be0fSWilliam Hua out = buf + sizeof(bytes) + sizeof(blocks); 724e025be0fSWilliam Hua 725e025be0fSWilliam Hua blocks = 0; 726317d9a05SJohn Johansen label_for_each_confined(i, label, profile) { 727317d9a05SJohn Johansen if (!profile->data) 728317d9a05SJohn Johansen continue; 729317d9a05SJohn Johansen 730317d9a05SJohn Johansen data = rhashtable_lookup_fast(profile->data, &key, 731317d9a05SJohn Johansen profile->data->p); 732e025be0fSWilliam Hua 733e025be0fSWilliam Hua if (data) { 734317d9a05SJohn Johansen if (out + sizeof(outle32) + data->size > buf + 735317d9a05SJohn Johansen buf_len) { 736637f688dSJohn Johansen aa_put_label(label); 737e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 738637f688dSJohn Johansen } 739e025be0fSWilliam Hua outle32 = __cpu_to_le32(data->size); 740e025be0fSWilliam Hua memcpy(out, &outle32, sizeof(outle32)); 741e025be0fSWilliam Hua out += sizeof(outle32); 742e025be0fSWilliam Hua memcpy(out, data->data, data->size); 743e025be0fSWilliam Hua out += data->size; 744e025be0fSWilliam Hua blocks++; 745e025be0fSWilliam Hua } 746e025be0fSWilliam Hua } 747637f688dSJohn Johansen aa_put_label(label); 748e025be0fSWilliam Hua 749e025be0fSWilliam Hua outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); 750e025be0fSWilliam Hua memcpy(buf, &outle32, sizeof(outle32)); 751e025be0fSWilliam Hua outle32 = __cpu_to_le32(blocks); 752e025be0fSWilliam Hua memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32)); 753e025be0fSWilliam Hua 754e025be0fSWilliam Hua return out - buf; 755e025be0fSWilliam Hua } 756e025be0fSWilliam Hua 7574f3b3f2dSJohn Johansen /** 7584f3b3f2dSJohn Johansen * query_label - queries a label and writes permissions to buf 7594f3b3f2dSJohn Johansen * @buf: the resulting permissions string is stored here (NOT NULL) 7604f3b3f2dSJohn Johansen * @buf_len: size of buf 7614f3b3f2dSJohn Johansen * @query: binary query string to match against the dfa 7624f3b3f2dSJohn Johansen * @query_len: size of query 7634f3b3f2dSJohn Johansen * @view_only: only compute for querier's view 7644f3b3f2dSJohn Johansen * 7654f3b3f2dSJohn Johansen * The buffers pointed to by buf and query may overlap. The query buffer is 7664f3b3f2dSJohn Johansen * parsed before buf is written to. 7674f3b3f2dSJohn Johansen * 7684f3b3f2dSJohn Johansen * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is 7694f3b3f2dSJohn Johansen * the name of the label, in the current namespace, that is to be queried and 7704f3b3f2dSJohn Johansen * DFA_STRING is a binary string to match against the label(s)'s DFA. 7714f3b3f2dSJohn Johansen * 7724f3b3f2dSJohn Johansen * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters 7734f3b3f2dSJohn Johansen * but must *not* be NUL terminated. 7744f3b3f2dSJohn Johansen * 7754f3b3f2dSJohn Johansen * Returns: number of characters written to buf or -errno on failure 7764f3b3f2dSJohn Johansen */ 7774f3b3f2dSJohn Johansen static ssize_t query_label(char *buf, size_t buf_len, 7784f3b3f2dSJohn Johansen char *query, size_t query_len, bool view_only) 7794f3b3f2dSJohn Johansen { 780317d9a05SJohn Johansen struct aa_profile *profile; 781637f688dSJohn Johansen struct aa_label *label, *curr; 7824f3b3f2dSJohn Johansen char *label_name, *match_str; 7834f3b3f2dSJohn Johansen size_t label_name_len, match_len; 7844f3b3f2dSJohn Johansen struct aa_perms perms; 785317d9a05SJohn Johansen struct label_it i; 7864f3b3f2dSJohn Johansen 7874f3b3f2dSJohn Johansen if (!query_len) 7884f3b3f2dSJohn Johansen return -EINVAL; 7894f3b3f2dSJohn Johansen 7904f3b3f2dSJohn Johansen label_name = query; 7914f3b3f2dSJohn Johansen label_name_len = strnlen(query, query_len); 7924f3b3f2dSJohn Johansen if (!label_name_len || label_name_len == query_len) 7934f3b3f2dSJohn Johansen return -EINVAL; 7944f3b3f2dSJohn Johansen 7954f3b3f2dSJohn Johansen /** 7964f3b3f2dSJohn Johansen * The extra byte is to account for the null byte between the 7974f3b3f2dSJohn Johansen * profile name and dfa string. profile_name_len is greater 7984f3b3f2dSJohn Johansen * than zero and less than query_len, so a byte can be safely 7994f3b3f2dSJohn Johansen * added or subtracted. 8004f3b3f2dSJohn Johansen */ 8014f3b3f2dSJohn Johansen match_str = label_name + label_name_len + 1; 8024f3b3f2dSJohn Johansen match_len = query_len - label_name_len - 1; 8034f3b3f2dSJohn Johansen 804637f688dSJohn Johansen curr = begin_current_label_crit_section(); 805637f688dSJohn Johansen label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false); 806637f688dSJohn Johansen end_current_label_crit_section(curr); 807637f688dSJohn Johansen if (IS_ERR(label)) 808637f688dSJohn Johansen return PTR_ERR(label); 8094f3b3f2dSJohn Johansen 8104f3b3f2dSJohn Johansen perms = allperms; 811317d9a05SJohn Johansen if (view_only) { 812317d9a05SJohn Johansen label_for_each_in_ns(i, labels_ns(label), label, profile) { 813317d9a05SJohn Johansen profile_query_cb(profile, &perms, match_str, match_len); 814317d9a05SJohn Johansen } 815317d9a05SJohn Johansen } else { 816317d9a05SJohn Johansen label_for_each(i, label, profile) { 817317d9a05SJohn Johansen profile_query_cb(profile, &perms, match_str, match_len); 818317d9a05SJohn Johansen } 819317d9a05SJohn Johansen } 820317d9a05SJohn Johansen aa_put_label(label); 8214f3b3f2dSJohn Johansen 8224f3b3f2dSJohn Johansen return scnprintf(buf, buf_len, 8234f3b3f2dSJohn Johansen "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n", 8244f3b3f2dSJohn Johansen perms.allow, perms.deny, perms.audit, perms.quiet); 8254f3b3f2dSJohn Johansen } 8264f3b3f2dSJohn Johansen 8271dea3b41SJohn Johansen /* 8281dea3b41SJohn Johansen * Transaction based IO. 8291dea3b41SJohn Johansen * The file expects a write which triggers the transaction, and then 8301dea3b41SJohn Johansen * possibly a read(s) which collects the result - which is stored in a 8311dea3b41SJohn Johansen * file-local buffer. Once a new write is performed, a new set of results 8321dea3b41SJohn Johansen * are stored in the file-local buffer. 8331dea3b41SJohn Johansen */ 8341dea3b41SJohn Johansen struct multi_transaction { 8351dea3b41SJohn Johansen struct kref count; 8361dea3b41SJohn Johansen ssize_t size; 8371dea3b41SJohn Johansen char data[0]; 8381dea3b41SJohn Johansen }; 8391dea3b41SJohn Johansen 8401dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction)) 8411dea3b41SJohn Johansen /* TODO: replace with per file lock */ 8421dea3b41SJohn Johansen static DEFINE_SPINLOCK(multi_transaction_lock); 8431dea3b41SJohn Johansen 8441dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref) 8451dea3b41SJohn Johansen { 8461dea3b41SJohn Johansen struct multi_transaction *t; 8471dea3b41SJohn Johansen 8481dea3b41SJohn Johansen t = container_of(kref, struct multi_transaction, count); 8491dea3b41SJohn Johansen free_page((unsigned long) t); 8501dea3b41SJohn Johansen } 8511dea3b41SJohn Johansen 8521dea3b41SJohn Johansen static struct multi_transaction * 8531dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t) 8541dea3b41SJohn Johansen { 8551dea3b41SJohn Johansen if (t) 8561dea3b41SJohn Johansen kref_get(&(t->count)); 8571dea3b41SJohn Johansen 8581dea3b41SJohn Johansen return t; 8591dea3b41SJohn Johansen } 8601dea3b41SJohn Johansen 8611dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t) 8621dea3b41SJohn Johansen { 8631dea3b41SJohn Johansen if (t) 8641dea3b41SJohn Johansen kref_put(&(t->count), multi_transaction_kref); 8651dea3b41SJohn Johansen } 8661dea3b41SJohn Johansen 8671dea3b41SJohn Johansen /* does not increment @new's count */ 8681dea3b41SJohn Johansen static void multi_transaction_set(struct file *file, 8691dea3b41SJohn Johansen struct multi_transaction *new, size_t n) 8701dea3b41SJohn Johansen { 8711dea3b41SJohn Johansen struct multi_transaction *old; 8721dea3b41SJohn Johansen 8731dea3b41SJohn Johansen AA_BUG(n > MULTI_TRANSACTION_LIMIT); 8741dea3b41SJohn Johansen 8751dea3b41SJohn Johansen new->size = n; 8761dea3b41SJohn Johansen spin_lock(&multi_transaction_lock); 8771dea3b41SJohn Johansen old = (struct multi_transaction *) file->private_data; 8781dea3b41SJohn Johansen file->private_data = new; 8791dea3b41SJohn Johansen spin_unlock(&multi_transaction_lock); 8801dea3b41SJohn Johansen put_multi_transaction(old); 8811dea3b41SJohn Johansen } 8821dea3b41SJohn Johansen 8831dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file, 8841dea3b41SJohn Johansen const char __user *buf, 8851dea3b41SJohn Johansen size_t size) 8861dea3b41SJohn Johansen { 8871dea3b41SJohn Johansen struct multi_transaction *t; 8881dea3b41SJohn Johansen 8891dea3b41SJohn Johansen if (size > MULTI_TRANSACTION_LIMIT - 1) 8901dea3b41SJohn Johansen return ERR_PTR(-EFBIG); 8911dea3b41SJohn Johansen 8921dea3b41SJohn Johansen t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL); 8931dea3b41SJohn Johansen if (!t) 8941dea3b41SJohn Johansen return ERR_PTR(-ENOMEM); 8951dea3b41SJohn Johansen kref_init(&t->count); 8961dea3b41SJohn Johansen if (copy_from_user(t->data, buf, size)) 8971dea3b41SJohn Johansen return ERR_PTR(-EFAULT); 8981dea3b41SJohn Johansen 8991dea3b41SJohn Johansen return t; 9001dea3b41SJohn Johansen } 9011dea3b41SJohn Johansen 9021dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf, 9031dea3b41SJohn Johansen size_t size, loff_t *pos) 9041dea3b41SJohn Johansen { 9051dea3b41SJohn Johansen struct multi_transaction *t; 9061dea3b41SJohn Johansen ssize_t ret; 9071dea3b41SJohn Johansen 9081dea3b41SJohn Johansen spin_lock(&multi_transaction_lock); 9091dea3b41SJohn Johansen t = get_multi_transaction(file->private_data); 9101dea3b41SJohn Johansen spin_unlock(&multi_transaction_lock); 9111dea3b41SJohn Johansen if (!t) 9121dea3b41SJohn Johansen return 0; 9131dea3b41SJohn Johansen 9141dea3b41SJohn Johansen ret = simple_read_from_buffer(buf, size, pos, t->data, t->size); 9151dea3b41SJohn Johansen put_multi_transaction(t); 9161dea3b41SJohn Johansen 9171dea3b41SJohn Johansen return ret; 9181dea3b41SJohn Johansen } 9191dea3b41SJohn Johansen 9201dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file) 9211dea3b41SJohn Johansen { 9221dea3b41SJohn Johansen put_multi_transaction(file->private_data); 9231dea3b41SJohn Johansen 9241dea3b41SJohn Johansen return 0; 9251dea3b41SJohn Johansen } 9261dea3b41SJohn Johansen 927317d9a05SJohn Johansen #define QUERY_CMD_LABEL "label\0" 928317d9a05SJohn Johansen #define QUERY_CMD_LABEL_LEN 6 9294f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE "profile\0" 9304f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE_LEN 8 931317d9a05SJohn Johansen #define QUERY_CMD_LABELALL "labelall\0" 932317d9a05SJohn Johansen #define QUERY_CMD_LABELALL_LEN 9 933e025be0fSWilliam Hua #define QUERY_CMD_DATA "data\0" 934e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN 5 935e025be0fSWilliam Hua 936e025be0fSWilliam Hua /** 937e025be0fSWilliam Hua * aa_write_access - generic permissions and data query 938e025be0fSWilliam Hua * @file: pointer to open apparmorfs/access file 939e025be0fSWilliam Hua * @ubuf: user buffer containing the complete query string (NOT NULL) 940e025be0fSWilliam Hua * @count: size of ubuf 941e025be0fSWilliam Hua * @ppos: position in the file (MUST BE ZERO) 942e025be0fSWilliam Hua * 943e025be0fSWilliam Hua * Allows for one permissions or data query per open(), write(), and read() 944e025be0fSWilliam Hua * sequence. The only queries currently supported are label-based queries for 945e025be0fSWilliam Hua * permissions or data. 946e025be0fSWilliam Hua * 947e025be0fSWilliam Hua * For permissions queries, ubuf must begin with "label\0", followed by the 948e025be0fSWilliam Hua * profile query specific format described in the query_label() function 949e025be0fSWilliam Hua * documentation. 950e025be0fSWilliam Hua * 951e025be0fSWilliam Hua * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where 952e025be0fSWilliam Hua * <LABEL> is the name of the security confinement context and <KEY> is the 953e025be0fSWilliam Hua * name of the data to retrieve. 954e025be0fSWilliam Hua * 955e025be0fSWilliam Hua * Returns: number of bytes written or -errno on failure 956e025be0fSWilliam Hua */ 957e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf, 958e025be0fSWilliam Hua size_t count, loff_t *ppos) 959e025be0fSWilliam Hua { 9601dea3b41SJohn Johansen struct multi_transaction *t; 961e025be0fSWilliam Hua ssize_t len; 962e025be0fSWilliam Hua 963e025be0fSWilliam Hua if (*ppos) 964e025be0fSWilliam Hua return -ESPIPE; 965e025be0fSWilliam Hua 9661dea3b41SJohn Johansen t = multi_transaction_new(file, ubuf, count); 9671dea3b41SJohn Johansen if (IS_ERR(t)) 9681dea3b41SJohn Johansen return PTR_ERR(t); 969e025be0fSWilliam Hua 9704f3b3f2dSJohn Johansen if (count > QUERY_CMD_PROFILE_LEN && 9714f3b3f2dSJohn Johansen !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) { 9724f3b3f2dSJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 9734f3b3f2dSJohn Johansen t->data + QUERY_CMD_PROFILE_LEN, 9744f3b3f2dSJohn Johansen count - QUERY_CMD_PROFILE_LEN, true); 975317d9a05SJohn Johansen } else if (count > QUERY_CMD_LABEL_LEN && 976317d9a05SJohn Johansen !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) { 977317d9a05SJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 978317d9a05SJohn Johansen t->data + QUERY_CMD_LABEL_LEN, 979317d9a05SJohn Johansen count - QUERY_CMD_LABEL_LEN, true); 980317d9a05SJohn Johansen } else if (count > QUERY_CMD_LABELALL_LEN && 981317d9a05SJohn Johansen !memcmp(t->data, QUERY_CMD_LABELALL, 982317d9a05SJohn Johansen QUERY_CMD_LABELALL_LEN)) { 983317d9a05SJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 984317d9a05SJohn Johansen t->data + QUERY_CMD_LABELALL_LEN, 985317d9a05SJohn Johansen count - QUERY_CMD_LABELALL_LEN, false); 9864f3b3f2dSJohn Johansen } else if (count > QUERY_CMD_DATA_LEN && 9871dea3b41SJohn Johansen !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { 9881dea3b41SJohn Johansen len = query_data(t->data, MULTI_TRANSACTION_LIMIT, 9891dea3b41SJohn Johansen t->data + QUERY_CMD_DATA_LEN, 990e025be0fSWilliam Hua count - QUERY_CMD_DATA_LEN); 991e025be0fSWilliam Hua } else 992e025be0fSWilliam Hua len = -EINVAL; 993e025be0fSWilliam Hua 9941dea3b41SJohn Johansen if (len < 0) { 9951dea3b41SJohn Johansen put_multi_transaction(t); 996e025be0fSWilliam Hua return len; 9971dea3b41SJohn Johansen } 998e025be0fSWilliam Hua 9991dea3b41SJohn Johansen multi_transaction_set(file, t, len); 1000e025be0fSWilliam Hua 1001e025be0fSWilliam Hua return count; 1002e025be0fSWilliam Hua } 1003e025be0fSWilliam Hua 1004c97204baSJohn Johansen static const struct file_operations aa_sfs_access = { 1005e025be0fSWilliam Hua .write = aa_write_access, 10061dea3b41SJohn Johansen .read = multi_transaction_read, 10071dea3b41SJohn Johansen .release = multi_transaction_release, 1008e025be0fSWilliam Hua .llseek = generic_file_llseek, 1009e025be0fSWilliam Hua }; 1010e025be0fSWilliam Hua 1011c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v) 1012e74abcf3SKees Cook { 1013c97204baSJohn Johansen struct aa_sfs_entry *fs_file = seq->private; 1014e74abcf3SKees Cook 1015e74abcf3SKees Cook if (!fs_file) 1016e74abcf3SKees Cook return 0; 1017e74abcf3SKees Cook 1018e74abcf3SKees Cook switch (fs_file->v_type) { 1019c97204baSJohn Johansen case AA_SFS_TYPE_BOOLEAN: 1020e74abcf3SKees Cook seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); 1021e74abcf3SKees Cook break; 1022c97204baSJohn Johansen case AA_SFS_TYPE_STRING: 1023a9bf8e9fSKees Cook seq_printf(seq, "%s\n", fs_file->v.string); 1024a9bf8e9fSKees Cook break; 1025c97204baSJohn Johansen case AA_SFS_TYPE_U64: 1026e74abcf3SKees Cook seq_printf(seq, "%#08lx\n", fs_file->v.u64); 1027e74abcf3SKees Cook break; 1028e74abcf3SKees Cook default: 1029e74abcf3SKees Cook /* Ignore unpritable entry types. */ 1030e74abcf3SKees Cook break; 1031e74abcf3SKees Cook } 1032e74abcf3SKees Cook 1033e74abcf3SKees Cook return 0; 1034e74abcf3SKees Cook } 1035e74abcf3SKees Cook 1036c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file) 1037e74abcf3SKees Cook { 1038c97204baSJohn Johansen return single_open(file, aa_sfs_seq_show, inode->i_private); 1039e74abcf3SKees Cook } 1040e74abcf3SKees Cook 1041c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = { 1042e74abcf3SKees Cook .owner = THIS_MODULE, 1043c97204baSJohn Johansen .open = aa_sfs_seq_open, 1044e74abcf3SKees Cook .read = seq_read, 1045e74abcf3SKees Cook .llseek = seq_lseek, 1046e74abcf3SKees Cook .release = single_release, 1047e74abcf3SKees Cook }; 1048e74abcf3SKees Cook 104952b97de3SJohn Johansen /* 105052b97de3SJohn Johansen * profile based file operations 105152b97de3SJohn Johansen * policy/profiles/XXXX/profiles/ * 105252b97de3SJohn Johansen */ 105352b97de3SJohn Johansen 105452b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME) \ 105552b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\ 105652b97de3SJohn Johansen { \ 105752b97de3SJohn Johansen return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \ 105852b97de3SJohn Johansen } \ 105952b97de3SJohn Johansen \ 106052b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = { \ 106152b97de3SJohn Johansen .owner = THIS_MODULE, \ 106252b97de3SJohn Johansen .open = seq_profile_ ##NAME ##_open, \ 106352b97de3SJohn Johansen .read = seq_read, \ 106452b97de3SJohn Johansen .llseek = seq_lseek, \ 106552b97de3SJohn Johansen .release = seq_profile_release, \ 106652b97de3SJohn Johansen } \ 106752b97de3SJohn Johansen 106852b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file, 10690d259f04SJohn Johansen int (*show)(struct seq_file *, void *)) 10700d259f04SJohn Johansen { 10718399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(inode->i_private); 10728399588aSJohn Johansen int error = single_open(file, show, proxy); 107363e2b423SJohn Johansen 10740d259f04SJohn Johansen if (error) { 10750d259f04SJohn Johansen file->private_data = NULL; 10768399588aSJohn Johansen aa_put_proxy(proxy); 10770d259f04SJohn Johansen } 10780d259f04SJohn Johansen 10790d259f04SJohn Johansen return error; 10800d259f04SJohn Johansen } 10810d259f04SJohn Johansen 108252b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file) 10830d259f04SJohn Johansen { 10840d259f04SJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 10850d259f04SJohn Johansen if (seq) 10868399588aSJohn Johansen aa_put_proxy(seq->private); 10870d259f04SJohn Johansen return single_release(inode, file); 10880d259f04SJohn Johansen } 10890d259f04SJohn Johansen 109052b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v) 10910d259f04SJohn Johansen { 10928399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1093637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1094637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 10950d259f04SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 1096637f688dSJohn Johansen aa_put_label(label); 10970d259f04SJohn Johansen 10980d259f04SJohn Johansen return 0; 10990d259f04SJohn Johansen } 11000d259f04SJohn Johansen 110152b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v) 11020d259f04SJohn Johansen { 11038399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1104637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1105637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 11060d259f04SJohn Johansen seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); 1107637f688dSJohn Johansen aa_put_label(label); 11080d259f04SJohn Johansen 11090d259f04SJohn Johansen return 0; 11100d259f04SJohn Johansen } 11110d259f04SJohn Johansen 111252b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v) 1113556d0be7SJohn Johansen { 11148399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1115637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1116637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 1117556d0be7SJohn Johansen if (profile->attach) 1118556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->attach); 1119556d0be7SJohn Johansen else if (profile->xmatch) 1120556d0be7SJohn Johansen seq_puts(seq, "<unknown>\n"); 1121556d0be7SJohn Johansen else 1122556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 1123637f688dSJohn Johansen aa_put_label(label); 1124556d0be7SJohn Johansen 1125556d0be7SJohn Johansen return 0; 1126556d0be7SJohn Johansen } 1127556d0be7SJohn Johansen 112852b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v) 1129f8eb8a13SJohn Johansen { 11308399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1131637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1132637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 1133f8eb8a13SJohn Johansen unsigned int i, size = aa_hash_size(); 1134f8eb8a13SJohn Johansen 1135f8eb8a13SJohn Johansen if (profile->hash) { 1136f8eb8a13SJohn Johansen for (i = 0; i < size; i++) 1137f8eb8a13SJohn Johansen seq_printf(seq, "%.2x", profile->hash[i]); 113847dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 1139f8eb8a13SJohn Johansen } 1140637f688dSJohn Johansen aa_put_label(label); 1141f8eb8a13SJohn Johansen 1142f8eb8a13SJohn Johansen return 0; 1143f8eb8a13SJohn Johansen } 1144f8eb8a13SJohn Johansen 114552b97de3SJohn Johansen SEQ_PROFILE_FOPS(name); 114652b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode); 114752b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach); 114852b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash); 1149f8eb8a13SJohn Johansen 115064c86970SJohn Johansen /* 115164c86970SJohn Johansen * namespace based files 115264c86970SJohn Johansen * several root files and 115364c86970SJohn Johansen * policy/ * 115464c86970SJohn Johansen */ 1155f8eb8a13SJohn Johansen 115664c86970SJohn Johansen #define SEQ_NS_FOPS(NAME) \ 115764c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \ 115864c86970SJohn Johansen { \ 115964c86970SJohn Johansen return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \ 116064c86970SJohn Johansen } \ 116164c86970SJohn Johansen \ 116264c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = { \ 116364c86970SJohn Johansen .owner = THIS_MODULE, \ 116464c86970SJohn Johansen .open = seq_ns_ ##NAME ##_open, \ 116564c86970SJohn Johansen .read = seq_read, \ 116664c86970SJohn Johansen .llseek = seq_lseek, \ 116764c86970SJohn Johansen .release = single_release, \ 116864c86970SJohn Johansen } \ 11693e3e5695SJohn Johansen 117040cde7fcSJohn Johansen static int seq_ns_stacked_show(struct seq_file *seq, void *v) 117140cde7fcSJohn Johansen { 117240cde7fcSJohn Johansen struct aa_label *label; 117340cde7fcSJohn Johansen 117440cde7fcSJohn Johansen label = begin_current_label_crit_section(); 117540cde7fcSJohn Johansen seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no"); 117640cde7fcSJohn Johansen end_current_label_crit_section(label); 117740cde7fcSJohn Johansen 117840cde7fcSJohn Johansen return 0; 117940cde7fcSJohn Johansen } 118040cde7fcSJohn Johansen 118140cde7fcSJohn Johansen static int seq_ns_nsstacked_show(struct seq_file *seq, void *v) 118240cde7fcSJohn Johansen { 118340cde7fcSJohn Johansen struct aa_label *label; 118440cde7fcSJohn Johansen struct aa_profile *profile; 118540cde7fcSJohn Johansen struct label_it it; 118640cde7fcSJohn Johansen int count = 1; 118740cde7fcSJohn Johansen 118840cde7fcSJohn Johansen label = begin_current_label_crit_section(); 118940cde7fcSJohn Johansen 119040cde7fcSJohn Johansen if (label->size > 1) { 119140cde7fcSJohn Johansen label_for_each(it, label, profile) 119240cde7fcSJohn Johansen if (profile->ns != labels_ns(label)) { 119340cde7fcSJohn Johansen count++; 119440cde7fcSJohn Johansen break; 119540cde7fcSJohn Johansen } 119640cde7fcSJohn Johansen } 119740cde7fcSJohn Johansen 119840cde7fcSJohn Johansen seq_printf(seq, "%s\n", count > 1 ? "yes" : "no"); 119940cde7fcSJohn Johansen end_current_label_crit_section(label); 120040cde7fcSJohn Johansen 120140cde7fcSJohn Johansen return 0; 120240cde7fcSJohn Johansen } 120340cde7fcSJohn Johansen 120464c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v) 1205a71ada30SJohn Johansen { 1206637f688dSJohn Johansen struct aa_label *label; 1207a71ada30SJohn Johansen 1208637f688dSJohn Johansen label = begin_current_label_crit_section(); 1209637f688dSJohn Johansen seq_printf(seq, "%d\n", labels_ns(label)->level); 1210637f688dSJohn Johansen end_current_label_crit_section(label); 1211a71ada30SJohn Johansen 1212a71ada30SJohn Johansen return 0; 1213a71ada30SJohn Johansen } 1214a71ada30SJohn Johansen 121564c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v) 12163e3e5695SJohn Johansen { 1217637f688dSJohn Johansen struct aa_label *label = begin_current_label_crit_section(); 1218040d9e2bSJohn Johansen seq_printf(seq, "%s\n", labels_ns(label)->base.name); 1219637f688dSJohn Johansen end_current_label_crit_section(label); 12203e3e5695SJohn Johansen 12213e3e5695SJohn Johansen return 0; 12223e3e5695SJohn Johansen } 12233e3e5695SJohn Johansen 122440cde7fcSJohn Johansen SEQ_NS_FOPS(stacked); 122540cde7fcSJohn Johansen SEQ_NS_FOPS(nsstacked); 122664c86970SJohn Johansen SEQ_NS_FOPS(level); 122764c86970SJohn Johansen SEQ_NS_FOPS(name); 12283e3e5695SJohn Johansen 12295d5182caSJohn Johansen 12305d5182caSJohn Johansen /* policy/raw_data/ * file ops */ 12315d5182caSJohn Johansen 12325d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME) \ 12335d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\ 12345d5182caSJohn Johansen { \ 12355d5182caSJohn Johansen return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \ 12365d5182caSJohn Johansen } \ 12375d5182caSJohn Johansen \ 12385d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \ 12395d5182caSJohn Johansen .owner = THIS_MODULE, \ 12405d5182caSJohn Johansen .open = seq_rawdata_ ##NAME ##_open, \ 12415d5182caSJohn Johansen .read = seq_read, \ 12425d5182caSJohn Johansen .llseek = seq_lseek, \ 12435d5182caSJohn Johansen .release = seq_rawdata_release, \ 12445d5182caSJohn Johansen } \ 12455d5182caSJohn Johansen 12465d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file, 12475d5182caSJohn Johansen int (*show)(struct seq_file *, void *)) 12485ac8c355SJohn Johansen { 12495d5182caSJohn Johansen struct aa_loaddata *data = __aa_get_loaddata(inode->i_private); 12505d5182caSJohn Johansen int error; 12515d5182caSJohn Johansen 12525d5182caSJohn Johansen if (!data) 12535d5182caSJohn Johansen /* lost race this ent is being reaped */ 12545d5182caSJohn Johansen return -ENOENT; 12555d5182caSJohn Johansen 12565d5182caSJohn Johansen error = single_open(file, show, data); 12575d5182caSJohn Johansen if (error) { 12585d5182caSJohn Johansen AA_BUG(file->private_data && 12595d5182caSJohn Johansen ((struct seq_file *)file->private_data)->private); 12605d5182caSJohn Johansen aa_put_loaddata(data); 12615d5182caSJohn Johansen } 12625d5182caSJohn Johansen 12635d5182caSJohn Johansen return error; 12645d5182caSJohn Johansen } 12655d5182caSJohn Johansen 12665d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file) 12675d5182caSJohn Johansen { 12685d5182caSJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 12695d5182caSJohn Johansen 12705d5182caSJohn Johansen if (seq) 12715d5182caSJohn Johansen aa_put_loaddata(seq->private); 12725d5182caSJohn Johansen 12735d5182caSJohn Johansen return single_release(inode, file); 12745d5182caSJohn Johansen } 12755d5182caSJohn Johansen 12765d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v) 12775d5182caSJohn Johansen { 12785d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12795d5182caSJohn Johansen 12805d5182caSJohn Johansen seq_printf(seq, "v%d\n", data->abi); 12815ac8c355SJohn Johansen 12825ac8c355SJohn Johansen return 0; 12835ac8c355SJohn Johansen } 12845ac8c355SJohn Johansen 12855d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v) 12865ac8c355SJohn Johansen { 12875d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12885ac8c355SJohn Johansen 12895d5182caSJohn Johansen seq_printf(seq, "%ld\n", data->revision); 12905ac8c355SJohn Johansen 12915ac8c355SJohn Johansen return 0; 12925ac8c355SJohn Johansen } 12935ac8c355SJohn Johansen 12945d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v) 12955ac8c355SJohn Johansen { 12965d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12975ac8c355SJohn Johansen unsigned int i, size = aa_hash_size(); 12985ac8c355SJohn Johansen 12995d5182caSJohn Johansen if (data->hash) { 13005ac8c355SJohn Johansen for (i = 0; i < size; i++) 13015d5182caSJohn Johansen seq_printf(seq, "%.2x", data->hash[i]); 130247dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 13035ac8c355SJohn Johansen } 13045ac8c355SJohn Johansen 13055ac8c355SJohn Johansen return 0; 13065ac8c355SJohn Johansen } 13075ac8c355SJohn Johansen 1308*63c16c3aSChris Coulson static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v) 1309*63c16c3aSChris Coulson { 1310*63c16c3aSChris Coulson struct aa_loaddata *data = seq->private; 1311*63c16c3aSChris Coulson 1312*63c16c3aSChris Coulson seq_printf(seq, "%zu\n", data->compressed_size); 1313*63c16c3aSChris Coulson 1314*63c16c3aSChris Coulson return 0; 1315*63c16c3aSChris Coulson } 1316*63c16c3aSChris Coulson 13175d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi); 13185d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision); 13195d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash); 1320*63c16c3aSChris Coulson SEQ_RAWDATA_FOPS(compressed_size); 1321*63c16c3aSChris Coulson 1322*63c16c3aSChris Coulson static int deflate_decompress(char *src, size_t slen, char *dst, size_t dlen) 1323*63c16c3aSChris Coulson { 1324*63c16c3aSChris Coulson int error; 1325*63c16c3aSChris Coulson struct z_stream_s strm; 1326*63c16c3aSChris Coulson 1327*63c16c3aSChris Coulson if (aa_g_rawdata_compression_level == 0) { 1328*63c16c3aSChris Coulson if (dlen < slen) 1329*63c16c3aSChris Coulson return -EINVAL; 1330*63c16c3aSChris Coulson memcpy(dst, src, slen); 1331*63c16c3aSChris Coulson return 0; 1332*63c16c3aSChris Coulson } 1333*63c16c3aSChris Coulson 1334*63c16c3aSChris Coulson memset(&strm, 0, sizeof(strm)); 1335*63c16c3aSChris Coulson 1336*63c16c3aSChris Coulson strm.workspace = kvzalloc(zlib_inflate_workspacesize(), GFP_KERNEL); 1337*63c16c3aSChris Coulson if (!strm.workspace) 1338*63c16c3aSChris Coulson return -ENOMEM; 1339*63c16c3aSChris Coulson 1340*63c16c3aSChris Coulson strm.next_in = src; 1341*63c16c3aSChris Coulson strm.avail_in = slen; 1342*63c16c3aSChris Coulson 1343*63c16c3aSChris Coulson error = zlib_inflateInit(&strm); 1344*63c16c3aSChris Coulson if (error != Z_OK) { 1345*63c16c3aSChris Coulson error = -ENOMEM; 1346*63c16c3aSChris Coulson goto fail_inflate_init; 1347*63c16c3aSChris Coulson } 1348*63c16c3aSChris Coulson 1349*63c16c3aSChris Coulson strm.next_out = dst; 1350*63c16c3aSChris Coulson strm.avail_out = dlen; 1351*63c16c3aSChris Coulson 1352*63c16c3aSChris Coulson error = zlib_inflate(&strm, Z_FINISH); 1353*63c16c3aSChris Coulson if (error != Z_STREAM_END) 1354*63c16c3aSChris Coulson error = -EINVAL; 1355*63c16c3aSChris Coulson else 1356*63c16c3aSChris Coulson error = 0; 1357*63c16c3aSChris Coulson 1358*63c16c3aSChris Coulson zlib_inflateEnd(&strm); 1359*63c16c3aSChris Coulson fail_inflate_init: 1360*63c16c3aSChris Coulson kvfree(strm.workspace); 1361*63c16c3aSChris Coulson return error; 1362*63c16c3aSChris Coulson } 13635ac8c355SJohn Johansen 13645ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, 13655ac8c355SJohn Johansen loff_t *ppos) 13665ac8c355SJohn Johansen { 1367*63c16c3aSChris Coulson struct rawdata_f_data *private = file->private_data; 13685ac8c355SJohn Johansen 1369*63c16c3aSChris Coulson return simple_read_from_buffer(buf, size, ppos, 1370*63c16c3aSChris Coulson RAWDATA_F_DATA_BUF(private), 1371*63c16c3aSChris Coulson private->loaddata->size); 13725ac8c355SJohn Johansen } 13735ac8c355SJohn Johansen 13745d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file) 13755ac8c355SJohn Johansen { 1376*63c16c3aSChris Coulson rawdata_f_data_free(file->private_data); 13775ac8c355SJohn Johansen 13785ac8c355SJohn Johansen return 0; 13795ac8c355SJohn Johansen } 13805ac8c355SJohn Johansen 13815d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file) 13825d5182caSJohn Johansen { 1383*63c16c3aSChris Coulson int error; 1384*63c16c3aSChris Coulson struct aa_loaddata *loaddata; 1385*63c16c3aSChris Coulson struct rawdata_f_data *private; 1386*63c16c3aSChris Coulson 13875d5182caSJohn Johansen if (!policy_view_capable(NULL)) 13885d5182caSJohn Johansen return -EACCES; 1389*63c16c3aSChris Coulson 1390*63c16c3aSChris Coulson loaddata = __aa_get_loaddata(inode->i_private); 1391*63c16c3aSChris Coulson if (!loaddata) 13925d5182caSJohn Johansen /* lost race: this entry is being reaped */ 13935d5182caSJohn Johansen return -ENOENT; 13945d5182caSJohn Johansen 1395*63c16c3aSChris Coulson private = rawdata_f_data_alloc(loaddata->size); 1396*63c16c3aSChris Coulson if (IS_ERR(private)) { 1397*63c16c3aSChris Coulson error = PTR_ERR(private); 1398*63c16c3aSChris Coulson goto fail_private_alloc; 1399*63c16c3aSChris Coulson } 1400*63c16c3aSChris Coulson 1401*63c16c3aSChris Coulson private->loaddata = loaddata; 1402*63c16c3aSChris Coulson 1403*63c16c3aSChris Coulson error = deflate_decompress(loaddata->data, loaddata->compressed_size, 1404*63c16c3aSChris Coulson RAWDATA_F_DATA_BUF(private), 1405*63c16c3aSChris Coulson loaddata->size); 1406*63c16c3aSChris Coulson if (error) 1407*63c16c3aSChris Coulson goto fail_decompress; 1408*63c16c3aSChris Coulson 1409*63c16c3aSChris Coulson file->private_data = private; 14105d5182caSJohn Johansen return 0; 1411*63c16c3aSChris Coulson 1412*63c16c3aSChris Coulson fail_decompress: 1413*63c16c3aSChris Coulson rawdata_f_data_free(private); 1414*63c16c3aSChris Coulson return error; 1415*63c16c3aSChris Coulson 1416*63c16c3aSChris Coulson fail_private_alloc: 1417*63c16c3aSChris Coulson aa_put_loaddata(loaddata); 1418*63c16c3aSChris Coulson return error; 14195d5182caSJohn Johansen } 14205d5182caSJohn Johansen 14215d5182caSJohn Johansen static const struct file_operations rawdata_fops = { 14225ac8c355SJohn Johansen .open = rawdata_open, 14235ac8c355SJohn Johansen .read = rawdata_read, 14245ac8c355SJohn Johansen .llseek = generic_file_llseek, 14255ac8c355SJohn Johansen .release = rawdata_release, 14265ac8c355SJohn Johansen }; 14275ac8c355SJohn Johansen 14285d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata) 14295d5182caSJohn Johansen { 14305d5182caSJohn Johansen int i; 14315d5182caSJohn Johansen 14325d5182caSJohn Johansen for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) { 14335d5182caSJohn Johansen if (!IS_ERR_OR_NULL(rawdata->dents[i])) { 14345d5182caSJohn Johansen /* no refcounts on i_private */ 1435c961ee5fSJohn Johansen aafs_remove(rawdata->dents[i]); 14365d5182caSJohn Johansen rawdata->dents[i] = NULL; 14375d5182caSJohn Johansen } 14385d5182caSJohn Johansen } 14395d5182caSJohn Johansen } 14405d5182caSJohn Johansen 14415d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata) 14425d5182caSJohn Johansen { 14435d5182caSJohn Johansen AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock)); 14445d5182caSJohn Johansen 14455d5182caSJohn Johansen if (rawdata->ns) { 14465d5182caSJohn Johansen remove_rawdata_dents(rawdata); 14475d5182caSJohn Johansen list_del_init(&rawdata->list); 14485d5182caSJohn Johansen aa_put_ns(rawdata->ns); 14495d5182caSJohn Johansen rawdata->ns = NULL; 14505d5182caSJohn Johansen } 14515d5182caSJohn Johansen } 14525d5182caSJohn Johansen 14535d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata) 14545d5182caSJohn Johansen { 14555d5182caSJohn Johansen struct dentry *dent, *dir; 14565d5182caSJohn Johansen 14575d5182caSJohn Johansen AA_BUG(!ns); 14585d5182caSJohn Johansen AA_BUG(!rawdata); 14595d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 14605d5182caSJohn Johansen AA_BUG(!ns_subdata_dir(ns)); 14615d5182caSJohn Johansen 14625d5182caSJohn Johansen /* 14635d5182caSJohn Johansen * just use ns revision dir was originally created at. This is 14645d5182caSJohn Johansen * under ns->lock and if load is successful revision will be 14655d5182caSJohn Johansen * bumped and is guaranteed to be unique 14665d5182caSJohn Johansen */ 14675d5182caSJohn Johansen rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision); 14685d5182caSJohn Johansen if (!rawdata->name) 14695d5182caSJohn Johansen return -ENOMEM; 14705d5182caSJohn Johansen 1471c961ee5fSJohn Johansen dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns)); 14725d5182caSJohn Johansen if (IS_ERR(dir)) 14735d5182caSJohn Johansen /* ->name freed when rawdata freed */ 14745d5182caSJohn Johansen return PTR_ERR(dir); 14755d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DIR] = dir; 14765d5182caSJohn Johansen 1477c961ee5fSJohn Johansen dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata, 14785d5182caSJohn Johansen &seq_rawdata_abi_fops); 14795d5182caSJohn Johansen if (IS_ERR(dent)) 14805d5182caSJohn Johansen goto fail; 14815d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_ABI] = dent; 14825d5182caSJohn Johansen 1483c961ee5fSJohn Johansen dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata, 14845d5182caSJohn Johansen &seq_rawdata_revision_fops); 14855d5182caSJohn Johansen if (IS_ERR(dent)) 14865d5182caSJohn Johansen goto fail; 14875d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_REVISION] = dent; 14885d5182caSJohn Johansen 14895d5182caSJohn Johansen if (aa_g_hash_policy) { 1490c961ee5fSJohn Johansen dent = aafs_create_file("sha1", S_IFREG | 0444, dir, 14915d5182caSJohn Johansen rawdata, &seq_rawdata_hash_fops); 14925d5182caSJohn Johansen if (IS_ERR(dent)) 14935d5182caSJohn Johansen goto fail; 14945d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_HASH] = dent; 14955d5182caSJohn Johansen } 14965d5182caSJohn Johansen 1497*63c16c3aSChris Coulson dent = aafs_create_file("compressed_size", S_IFREG | 0444, dir, 1498*63c16c3aSChris Coulson rawdata, 1499*63c16c3aSChris Coulson &seq_rawdata_compressed_size_fops); 1500*63c16c3aSChris Coulson if (IS_ERR(dent)) 1501*63c16c3aSChris Coulson goto fail; 1502*63c16c3aSChris Coulson rawdata->dents[AAFS_LOADDATA_COMPRESSED_SIZE] = dent; 1503*63c16c3aSChris Coulson 1504c961ee5fSJohn Johansen dent = aafs_create_file("raw_data", S_IFREG | 0444, 15055d5182caSJohn Johansen dir, rawdata, &rawdata_fops); 15065d5182caSJohn Johansen if (IS_ERR(dent)) 15075d5182caSJohn Johansen goto fail; 15085d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DATA] = dent; 15095d5182caSJohn Johansen d_inode(dent)->i_size = rawdata->size; 15105d5182caSJohn Johansen 15115d5182caSJohn Johansen rawdata->ns = aa_get_ns(ns); 15125d5182caSJohn Johansen list_add(&rawdata->list, &ns->rawdata_list); 15135d5182caSJohn Johansen /* no refcount on inode rawdata */ 15145d5182caSJohn Johansen 15155d5182caSJohn Johansen return 0; 15165d5182caSJohn Johansen 15175d5182caSJohn Johansen fail: 15185d5182caSJohn Johansen remove_rawdata_dents(rawdata); 15195d5182caSJohn Johansen 15205d5182caSJohn Johansen return PTR_ERR(dent); 15215d5182caSJohn Johansen } 15225d5182caSJohn Johansen 15230d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/ 1524c97204baSJohn Johansen 1525c97204baSJohn Johansen /** 1526c97204baSJohn Johansen * 1527c97204baSJohn Johansen * Requires: @profile->ns->lock held 1528c97204baSJohn Johansen */ 1529c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile) 15300d259f04SJohn Johansen { 15310d259f04SJohn Johansen struct aa_profile *child; 15320d259f04SJohn Johansen int i; 15330d259f04SJohn Johansen 15340d259f04SJohn Johansen if (!profile) 15350d259f04SJohn Johansen return; 15360d259f04SJohn Johansen 15370d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) 1538c97204baSJohn Johansen __aafs_profile_rmdir(child); 15390d259f04SJohn Johansen 15400d259f04SJohn Johansen for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { 15418399588aSJohn Johansen struct aa_proxy *proxy; 15420d259f04SJohn Johansen if (!profile->dents[i]) 15430d259f04SJohn Johansen continue; 15440d259f04SJohn Johansen 15458399588aSJohn Johansen proxy = d_inode(profile->dents[i])->i_private; 1546c961ee5fSJohn Johansen aafs_remove(profile->dents[i]); 15478399588aSJohn Johansen aa_put_proxy(proxy); 15480d259f04SJohn Johansen profile->dents[i] = NULL; 15490d259f04SJohn Johansen } 15500d259f04SJohn Johansen } 15510d259f04SJohn Johansen 1552c97204baSJohn Johansen /** 1553c97204baSJohn Johansen * 1554c97204baSJohn Johansen * Requires: @old->ns->lock held 1555c97204baSJohn Johansen */ 1556c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old, 15570d259f04SJohn Johansen struct aa_profile *new) 15580d259f04SJohn Johansen { 15590d259f04SJohn Johansen int i; 15600d259f04SJohn Johansen 1561cbf2d0e1SJohn Johansen AA_BUG(!old); 1562cbf2d0e1SJohn Johansen AA_BUG(!new); 1563cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock)); 1564cbf2d0e1SJohn Johansen 15650d259f04SJohn Johansen for (i = 0; i < AAFS_PROF_SIZEOF; i++) { 15660d259f04SJohn Johansen new->dents[i] = old->dents[i]; 1567d671e890SJohn Johansen if (new->dents[i]) 1568078cd827SDeepa Dinamani new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); 15690d259f04SJohn Johansen old->dents[i] = NULL; 15700d259f04SJohn Johansen } 15710d259f04SJohn Johansen } 15720d259f04SJohn Johansen 15730d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name, 15740d259f04SJohn Johansen struct aa_profile *profile, 15750d259f04SJohn Johansen const struct file_operations *fops) 15760d259f04SJohn Johansen { 1577637f688dSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy); 15780d259f04SJohn Johansen struct dentry *dent; 15790d259f04SJohn Johansen 1580c961ee5fSJohn Johansen dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops); 15810d259f04SJohn Johansen if (IS_ERR(dent)) 15828399588aSJohn Johansen aa_put_proxy(proxy); 15830d259f04SJohn Johansen 15840d259f04SJohn Johansen return dent; 15850d259f04SJohn Johansen } 15860d259f04SJohn Johansen 15875d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile) 15885d5182caSJohn Johansen { 15895d5182caSJohn Johansen int depth = 0; 15905d5182caSJohn Johansen 15915d5182caSJohn Johansen rcu_read_lock(); 15925d5182caSJohn Johansen for (depth = 0; profile; profile = rcu_access_pointer(profile->parent)) 15935d5182caSJohn Johansen depth++; 15945d5182caSJohn Johansen rcu_read_unlock(); 15955d5182caSJohn Johansen 15965d5182caSJohn Johansen return depth; 15975d5182caSJohn Johansen } 15985d5182caSJohn Johansen 15991180b4c7SJohn Johansen static char *gen_symlink_name(int depth, const char *dirname, const char *fname) 16005d5182caSJohn Johansen { 16011180b4c7SJohn Johansen char *buffer, *s; 16025d5182caSJohn Johansen int error; 16031180b4c7SJohn Johansen int size = depth * 6 + strlen(dirname) + strlen(fname) + 11; 16041180b4c7SJohn Johansen 16051180b4c7SJohn Johansen s = buffer = kmalloc(size, GFP_KERNEL); 16061180b4c7SJohn Johansen if (!buffer) 16071180b4c7SJohn Johansen return ERR_PTR(-ENOMEM); 16085d5182caSJohn Johansen 16095d5182caSJohn Johansen for (; depth > 0; depth--) { 16101180b4c7SJohn Johansen strcpy(s, "../../"); 16111180b4c7SJohn Johansen s += 6; 16121180b4c7SJohn Johansen size -= 6; 16135d5182caSJohn Johansen } 16145d5182caSJohn Johansen 16151180b4c7SJohn Johansen error = snprintf(s, size, "raw_data/%s/%s", dirname, fname); 1616588558ebSColin Ian King if (error >= size || error < 0) { 1617588558ebSColin Ian King kfree(buffer); 16181180b4c7SJohn Johansen return ERR_PTR(-ENAMETOOLONG); 16195d5182caSJohn Johansen } 16205d5182caSJohn Johansen 16211180b4c7SJohn Johansen return buffer; 16225d5182caSJohn Johansen } 16235d5182caSJohn Johansen 16241180b4c7SJohn Johansen static void rawdata_link_cb(void *arg) 16251180b4c7SJohn Johansen { 16261180b4c7SJohn Johansen kfree(arg); 16271180b4c7SJohn Johansen } 16281180b4c7SJohn Johansen 16291180b4c7SJohn Johansen static const char *rawdata_get_link_base(struct dentry *dentry, 16301180b4c7SJohn Johansen struct inode *inode, 16311180b4c7SJohn Johansen struct delayed_call *done, 16321180b4c7SJohn Johansen const char *name) 16331180b4c7SJohn Johansen { 16341180b4c7SJohn Johansen struct aa_proxy *proxy = inode->i_private; 16351180b4c7SJohn Johansen struct aa_label *label; 16361180b4c7SJohn Johansen struct aa_profile *profile; 16371180b4c7SJohn Johansen char *target; 16381180b4c7SJohn Johansen int depth; 16391180b4c7SJohn Johansen 16401180b4c7SJohn Johansen if (!dentry) 16411180b4c7SJohn Johansen return ERR_PTR(-ECHILD); 16421180b4c7SJohn Johansen 16431180b4c7SJohn Johansen label = aa_get_label_rcu(&proxy->label); 16441180b4c7SJohn Johansen profile = labels_profile(label); 16451180b4c7SJohn Johansen depth = profile_depth(profile); 16461180b4c7SJohn Johansen target = gen_symlink_name(depth, profile->rawdata->name, name); 16471180b4c7SJohn Johansen aa_put_label(label); 16481180b4c7SJohn Johansen 16491180b4c7SJohn Johansen if (IS_ERR(target)) 16501180b4c7SJohn Johansen return target; 16511180b4c7SJohn Johansen 16521180b4c7SJohn Johansen set_delayed_call(done, rawdata_link_cb, target); 16531180b4c7SJohn Johansen 16541180b4c7SJohn Johansen return target; 16551180b4c7SJohn Johansen } 16561180b4c7SJohn Johansen 16571180b4c7SJohn Johansen static const char *rawdata_get_link_sha1(struct dentry *dentry, 16581180b4c7SJohn Johansen struct inode *inode, 16591180b4c7SJohn Johansen struct delayed_call *done) 16601180b4c7SJohn Johansen { 16611180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "sha1"); 16621180b4c7SJohn Johansen } 16631180b4c7SJohn Johansen 16641180b4c7SJohn Johansen static const char *rawdata_get_link_abi(struct dentry *dentry, 16651180b4c7SJohn Johansen struct inode *inode, 16661180b4c7SJohn Johansen struct delayed_call *done) 16671180b4c7SJohn Johansen { 16681180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "abi"); 16691180b4c7SJohn Johansen } 16701180b4c7SJohn Johansen 16711180b4c7SJohn Johansen static const char *rawdata_get_link_data(struct dentry *dentry, 16721180b4c7SJohn Johansen struct inode *inode, 16731180b4c7SJohn Johansen struct delayed_call *done) 16741180b4c7SJohn Johansen { 16751180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "raw_data"); 16761180b4c7SJohn Johansen } 16771180b4c7SJohn Johansen 16781180b4c7SJohn Johansen static const struct inode_operations rawdata_link_sha1_iops = { 16791180b4c7SJohn Johansen .get_link = rawdata_get_link_sha1, 16801180b4c7SJohn Johansen }; 16811180b4c7SJohn Johansen 16821180b4c7SJohn Johansen static const struct inode_operations rawdata_link_abi_iops = { 16831180b4c7SJohn Johansen .get_link = rawdata_get_link_abi, 16841180b4c7SJohn Johansen }; 16851180b4c7SJohn Johansen static const struct inode_operations rawdata_link_data_iops = { 16861180b4c7SJohn Johansen .get_link = rawdata_get_link_data, 16871180b4c7SJohn Johansen }; 16881180b4c7SJohn Johansen 16891180b4c7SJohn Johansen 16905d5182caSJohn Johansen /* 16915d5182caSJohn Johansen * Requires: @profile->ns->lock held 16925d5182caSJohn Johansen */ 1693c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) 16940d259f04SJohn Johansen { 16950d259f04SJohn Johansen struct aa_profile *child; 16960d259f04SJohn Johansen struct dentry *dent = NULL, *dir; 16970d259f04SJohn Johansen int error; 16980d259f04SJohn Johansen 1699cbf2d0e1SJohn Johansen AA_BUG(!profile); 1700cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock)); 1701cbf2d0e1SJohn Johansen 17020d259f04SJohn Johansen if (!parent) { 17030d259f04SJohn Johansen struct aa_profile *p; 17040d259f04SJohn Johansen p = aa_deref_parent(profile); 17050d259f04SJohn Johansen dent = prof_dir(p); 17060d259f04SJohn Johansen /* adding to parent that previously didn't have children */ 1707c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dent); 17080d259f04SJohn Johansen if (IS_ERR(dent)) 17090d259f04SJohn Johansen goto fail; 17100d259f04SJohn Johansen prof_child_dir(p) = parent = dent; 17110d259f04SJohn Johansen } 17120d259f04SJohn Johansen 17130d259f04SJohn Johansen if (!profile->dirname) { 17140d259f04SJohn Johansen int len, id_len; 17150d259f04SJohn Johansen len = mangle_name(profile->base.name, NULL); 17160d259f04SJohn Johansen id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); 17170d259f04SJohn Johansen 17180d259f04SJohn Johansen profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); 1719ffac1de6SDan Carpenter if (!profile->dirname) { 1720ffac1de6SDan Carpenter error = -ENOMEM; 1721ffac1de6SDan Carpenter goto fail2; 1722ffac1de6SDan Carpenter } 17230d259f04SJohn Johansen 17240d259f04SJohn Johansen mangle_name(profile->base.name, profile->dirname); 17250d259f04SJohn Johansen sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); 17260d259f04SJohn Johansen } 17270d259f04SJohn Johansen 1728c961ee5fSJohn Johansen dent = aafs_create_dir(profile->dirname, parent); 17290d259f04SJohn Johansen if (IS_ERR(dent)) 17300d259f04SJohn Johansen goto fail; 17310d259f04SJohn Johansen prof_dir(profile) = dir = dent; 17320d259f04SJohn Johansen 173352b97de3SJohn Johansen dent = create_profile_file(dir, "name", profile, 173452b97de3SJohn Johansen &seq_profile_name_fops); 17350d259f04SJohn Johansen if (IS_ERR(dent)) 17360d259f04SJohn Johansen goto fail; 17370d259f04SJohn Johansen profile->dents[AAFS_PROF_NAME] = dent; 17380d259f04SJohn Johansen 173952b97de3SJohn Johansen dent = create_profile_file(dir, "mode", profile, 174052b97de3SJohn Johansen &seq_profile_mode_fops); 17410d259f04SJohn Johansen if (IS_ERR(dent)) 17420d259f04SJohn Johansen goto fail; 17430d259f04SJohn Johansen profile->dents[AAFS_PROF_MODE] = dent; 17440d259f04SJohn Johansen 1745556d0be7SJohn Johansen dent = create_profile_file(dir, "attach", profile, 174652b97de3SJohn Johansen &seq_profile_attach_fops); 1747556d0be7SJohn Johansen if (IS_ERR(dent)) 1748556d0be7SJohn Johansen goto fail; 1749556d0be7SJohn Johansen profile->dents[AAFS_PROF_ATTACH] = dent; 1750556d0be7SJohn Johansen 1751f8eb8a13SJohn Johansen if (profile->hash) { 1752f8eb8a13SJohn Johansen dent = create_profile_file(dir, "sha1", profile, 175352b97de3SJohn Johansen &seq_profile_hash_fops); 1754f8eb8a13SJohn Johansen if (IS_ERR(dent)) 1755f8eb8a13SJohn Johansen goto fail; 1756f8eb8a13SJohn Johansen profile->dents[AAFS_PROF_HASH] = dent; 1757f8eb8a13SJohn Johansen } 1758f8eb8a13SJohn Johansen 17595ac8c355SJohn Johansen if (profile->rawdata) { 17601180b4c7SJohn Johansen dent = aafs_create_symlink("raw_sha1", dir, NULL, 17611180b4c7SJohn Johansen profile->label.proxy, 17621180b4c7SJohn Johansen &rawdata_link_sha1_iops); 17635ac8c355SJohn Johansen if (IS_ERR(dent)) 17645ac8c355SJohn Johansen goto fail; 17651180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 17665ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_HASH] = dent; 17675ac8c355SJohn Johansen 17681180b4c7SJohn Johansen dent = aafs_create_symlink("raw_abi", dir, NULL, 17691180b4c7SJohn Johansen profile->label.proxy, 17701180b4c7SJohn Johansen &rawdata_link_abi_iops); 17715ac8c355SJohn Johansen if (IS_ERR(dent)) 17725ac8c355SJohn Johansen goto fail; 17731180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 17745ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_ABI] = dent; 17755ac8c355SJohn Johansen 17761180b4c7SJohn Johansen dent = aafs_create_symlink("raw_data", dir, NULL, 17771180b4c7SJohn Johansen profile->label.proxy, 17781180b4c7SJohn Johansen &rawdata_link_data_iops); 17795ac8c355SJohn Johansen if (IS_ERR(dent)) 17805ac8c355SJohn Johansen goto fail; 17811180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 17825ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_DATA] = dent; 17835ac8c355SJohn Johansen } 17845ac8c355SJohn Johansen 17850d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) { 1786c97204baSJohn Johansen error = __aafs_profile_mkdir(child, prof_child_dir(profile)); 17870d259f04SJohn Johansen if (error) 17880d259f04SJohn Johansen goto fail2; 17890d259f04SJohn Johansen } 17900d259f04SJohn Johansen 17910d259f04SJohn Johansen return 0; 17920d259f04SJohn Johansen 17930d259f04SJohn Johansen fail: 17940d259f04SJohn Johansen error = PTR_ERR(dent); 17950d259f04SJohn Johansen 17960d259f04SJohn Johansen fail2: 1797c97204baSJohn Johansen __aafs_profile_rmdir(profile); 17980d259f04SJohn Johansen 17990d259f04SJohn Johansen return error; 18000d259f04SJohn Johansen } 18010d259f04SJohn Johansen 18024ae47f33SJohn Johansen static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode) 18034ae47f33SJohn Johansen { 18044ae47f33SJohn Johansen struct aa_ns *ns, *parent; 18054ae47f33SJohn Johansen /* TODO: improve permission check */ 1806637f688dSJohn Johansen struct aa_label *label; 1807637f688dSJohn Johansen int error; 1808637f688dSJohn Johansen 1809637f688dSJohn Johansen label = begin_current_label_crit_section(); 1810637f688dSJohn Johansen error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); 1811637f688dSJohn Johansen end_current_label_crit_section(label); 18124ae47f33SJohn Johansen if (error) 18134ae47f33SJohn Johansen return error; 18144ae47f33SJohn Johansen 18154ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 18164ae47f33SJohn Johansen AA_BUG(d_inode(ns_subns_dir(parent)) != dir); 18174ae47f33SJohn Johansen 18184ae47f33SJohn Johansen /* we have to unlock and then relock to get locking order right 18194ae47f33SJohn Johansen * for pin_fs 18204ae47f33SJohn Johansen */ 18214ae47f33SJohn Johansen inode_unlock(dir); 18224ae47f33SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 1823feb3c766SJohn Johansen mutex_lock_nested(&parent->lock, parent->level); 18244ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 18254ae47f33SJohn Johansen if (error) 18264ae47f33SJohn Johansen goto out; 18274ae47f33SJohn Johansen 18284ae47f33SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL, 18294ae47f33SJohn Johansen NULL, NULL, NULL); 18304ae47f33SJohn Johansen if (error) 18314ae47f33SJohn Johansen goto out_pin; 18324ae47f33SJohn Johansen 18334ae47f33SJohn Johansen ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name), 18344ae47f33SJohn Johansen dentry); 18354ae47f33SJohn Johansen if (IS_ERR(ns)) { 18364ae47f33SJohn Johansen error = PTR_ERR(ns); 18374ae47f33SJohn Johansen ns = NULL; 18384ae47f33SJohn Johansen } 18394ae47f33SJohn Johansen 18404ae47f33SJohn Johansen aa_put_ns(ns); /* list ref remains */ 18414ae47f33SJohn Johansen out_pin: 18424ae47f33SJohn Johansen if (error) 18434ae47f33SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 18444ae47f33SJohn Johansen out: 18454ae47f33SJohn Johansen mutex_unlock(&parent->lock); 18464ae47f33SJohn Johansen aa_put_ns(parent); 18474ae47f33SJohn Johansen 18484ae47f33SJohn Johansen return error; 18494ae47f33SJohn Johansen } 18504ae47f33SJohn Johansen 18514ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry) 18524ae47f33SJohn Johansen { 18534ae47f33SJohn Johansen struct aa_ns *ns, *parent; 18544ae47f33SJohn Johansen /* TODO: improve permission check */ 1855637f688dSJohn Johansen struct aa_label *label; 1856637f688dSJohn Johansen int error; 1857637f688dSJohn Johansen 1858637f688dSJohn Johansen label = begin_current_label_crit_section(); 1859637f688dSJohn Johansen error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); 1860637f688dSJohn Johansen end_current_label_crit_section(label); 18614ae47f33SJohn Johansen if (error) 18624ae47f33SJohn Johansen return error; 18634ae47f33SJohn Johansen 18644ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 18654ae47f33SJohn Johansen /* rmdir calls the generic securityfs functions to remove files 18664ae47f33SJohn Johansen * from the apparmor dir. It is up to the apparmor ns locking 18674ae47f33SJohn Johansen * to avoid races. 18684ae47f33SJohn Johansen */ 18694ae47f33SJohn Johansen inode_unlock(dir); 18704ae47f33SJohn Johansen inode_unlock(dentry->d_inode); 18714ae47f33SJohn Johansen 1872feb3c766SJohn Johansen mutex_lock_nested(&parent->lock, parent->level); 18734ae47f33SJohn Johansen ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name, 18744ae47f33SJohn Johansen dentry->d_name.len)); 18754ae47f33SJohn Johansen if (!ns) { 18764ae47f33SJohn Johansen error = -ENOENT; 18774ae47f33SJohn Johansen goto out; 18784ae47f33SJohn Johansen } 18794ae47f33SJohn Johansen AA_BUG(ns_dir(ns) != dentry); 18804ae47f33SJohn Johansen 18814ae47f33SJohn Johansen __aa_remove_ns(ns); 18824ae47f33SJohn Johansen aa_put_ns(ns); 18834ae47f33SJohn Johansen 18844ae47f33SJohn Johansen out: 18854ae47f33SJohn Johansen mutex_unlock(&parent->lock); 18864ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 18874ae47f33SJohn Johansen inode_lock(dentry->d_inode); 18884ae47f33SJohn Johansen aa_put_ns(parent); 18894ae47f33SJohn Johansen 18904ae47f33SJohn Johansen return error; 18914ae47f33SJohn Johansen } 18924ae47f33SJohn Johansen 18934ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = { 18944ae47f33SJohn Johansen .lookup = simple_lookup, 18954ae47f33SJohn Johansen .mkdir = ns_mkdir_op, 18964ae47f33SJohn Johansen .rmdir = ns_rmdir_op, 18974ae47f33SJohn Johansen }; 18984ae47f33SJohn Johansen 18995d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns) 19005d5182caSJohn Johansen { 19015d5182caSJohn Johansen struct aa_loaddata *ent, *tmp; 19025d5182caSJohn Johansen 19035d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 19045d5182caSJohn Johansen 19055d5182caSJohn Johansen list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list) 19065d5182caSJohn Johansen __aa_fs_remove_rawdata(ent); 19075d5182caSJohn Johansen } 19085d5182caSJohn Johansen 1909c97204baSJohn Johansen /** 1910c97204baSJohn Johansen * 1911c97204baSJohn Johansen * Requires: @ns->lock held 1912c97204baSJohn Johansen */ 1913c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns) 19140d259f04SJohn Johansen { 191598849dffSJohn Johansen struct aa_ns *sub; 19160d259f04SJohn Johansen struct aa_profile *child; 19170d259f04SJohn Johansen int i; 19180d259f04SJohn Johansen 19190d259f04SJohn Johansen if (!ns) 19200d259f04SJohn Johansen return; 1921cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 19220d259f04SJohn Johansen 19230d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) 1924c97204baSJohn Johansen __aafs_profile_rmdir(child); 19250d259f04SJohn Johansen 19260d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 1927feb3c766SJohn Johansen mutex_lock_nested(&sub->lock, sub->level); 1928c97204baSJohn Johansen __aafs_ns_rmdir(sub); 19290d259f04SJohn Johansen mutex_unlock(&sub->lock); 19300d259f04SJohn Johansen } 19310d259f04SJohn Johansen 19325d5182caSJohn Johansen __aa_fs_list_remove_rawdata(ns); 19335d5182caSJohn Johansen 1934b7fd2c03SJohn Johansen if (ns_subns_dir(ns)) { 1935b7fd2c03SJohn Johansen sub = d_inode(ns_subns_dir(ns))->i_private; 1936b7fd2c03SJohn Johansen aa_put_ns(sub); 1937b7fd2c03SJohn Johansen } 1938b7fd2c03SJohn Johansen if (ns_subload(ns)) { 1939b7fd2c03SJohn Johansen sub = d_inode(ns_subload(ns))->i_private; 1940b7fd2c03SJohn Johansen aa_put_ns(sub); 1941b7fd2c03SJohn Johansen } 1942b7fd2c03SJohn Johansen if (ns_subreplace(ns)) { 1943b7fd2c03SJohn Johansen sub = d_inode(ns_subreplace(ns))->i_private; 1944b7fd2c03SJohn Johansen aa_put_ns(sub); 1945b7fd2c03SJohn Johansen } 1946b7fd2c03SJohn Johansen if (ns_subremove(ns)) { 1947b7fd2c03SJohn Johansen sub = d_inode(ns_subremove(ns))->i_private; 1948b7fd2c03SJohn Johansen aa_put_ns(sub); 1949b7fd2c03SJohn Johansen } 1950d9bf2c26SJohn Johansen if (ns_subrevision(ns)) { 1951d9bf2c26SJohn Johansen sub = d_inode(ns_subrevision(ns))->i_private; 1952d9bf2c26SJohn Johansen aa_put_ns(sub); 1953d9bf2c26SJohn Johansen } 1954b7fd2c03SJohn Johansen 19550d259f04SJohn Johansen for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { 1956c961ee5fSJohn Johansen aafs_remove(ns->dents[i]); 19570d259f04SJohn Johansen ns->dents[i] = NULL; 19580d259f04SJohn Johansen } 19590d259f04SJohn Johansen } 19600d259f04SJohn Johansen 1961b7fd2c03SJohn Johansen /* assumes cleanup in caller */ 1962c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) 1963b7fd2c03SJohn Johansen { 1964b7fd2c03SJohn Johansen struct dentry *dent; 1965b7fd2c03SJohn Johansen 1966b7fd2c03SJohn Johansen AA_BUG(!ns); 1967b7fd2c03SJohn Johansen AA_BUG(!dir); 1968b7fd2c03SJohn Johansen 1969c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dir); 1970b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1971b7fd2c03SJohn Johansen return PTR_ERR(dent); 1972b7fd2c03SJohn Johansen ns_subprofs_dir(ns) = dent; 1973b7fd2c03SJohn Johansen 1974c961ee5fSJohn Johansen dent = aafs_create_dir("raw_data", dir); 1975b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1976b7fd2c03SJohn Johansen return PTR_ERR(dent); 1977b7fd2c03SJohn Johansen ns_subdata_dir(ns) = dent; 1978b7fd2c03SJohn Johansen 1979d9bf2c26SJohn Johansen dent = aafs_create_file("revision", 0444, dir, ns, 1980d9bf2c26SJohn Johansen &aa_fs_ns_revision_fops); 1981d9bf2c26SJohn Johansen if (IS_ERR(dent)) 1982d9bf2c26SJohn Johansen return PTR_ERR(dent); 1983d9bf2c26SJohn Johansen aa_get_ns(ns); 1984d9bf2c26SJohn Johansen ns_subrevision(ns) = dent; 1985d9bf2c26SJohn Johansen 1986c961ee5fSJohn Johansen dent = aafs_create_file(".load", 0640, dir, ns, 1987b7fd2c03SJohn Johansen &aa_fs_profile_load); 1988b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1989b7fd2c03SJohn Johansen return PTR_ERR(dent); 1990b7fd2c03SJohn Johansen aa_get_ns(ns); 1991b7fd2c03SJohn Johansen ns_subload(ns) = dent; 1992b7fd2c03SJohn Johansen 1993c961ee5fSJohn Johansen dent = aafs_create_file(".replace", 0640, dir, ns, 1994b7fd2c03SJohn Johansen &aa_fs_profile_replace); 1995b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1996b7fd2c03SJohn Johansen return PTR_ERR(dent); 1997b7fd2c03SJohn Johansen aa_get_ns(ns); 1998b7fd2c03SJohn Johansen ns_subreplace(ns) = dent; 1999b7fd2c03SJohn Johansen 2000c961ee5fSJohn Johansen dent = aafs_create_file(".remove", 0640, dir, ns, 2001b7fd2c03SJohn Johansen &aa_fs_profile_remove); 2002b7fd2c03SJohn Johansen if (IS_ERR(dent)) 2003b7fd2c03SJohn Johansen return PTR_ERR(dent); 2004b7fd2c03SJohn Johansen aa_get_ns(ns); 2005b7fd2c03SJohn Johansen ns_subremove(ns) = dent; 2006b7fd2c03SJohn Johansen 20074ae47f33SJohn Johansen /* use create_dentry so we can supply private data */ 20084ae47f33SJohn Johansen dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL, 20094ae47f33SJohn Johansen &ns_dir_inode_operations); 2010b7fd2c03SJohn Johansen if (IS_ERR(dent)) 2011b7fd2c03SJohn Johansen return PTR_ERR(dent); 2012b7fd2c03SJohn Johansen aa_get_ns(ns); 2013b7fd2c03SJohn Johansen ns_subns_dir(ns) = dent; 2014b7fd2c03SJohn Johansen 2015b7fd2c03SJohn Johansen return 0; 2016b7fd2c03SJohn Johansen } 2017b7fd2c03SJohn Johansen 2018c97204baSJohn Johansen /* 2019c97204baSJohn Johansen * Requires: @ns->lock held 2020c97204baSJohn Johansen */ 202198407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name, 202298407f0aSJohn Johansen struct dentry *dent) 20230d259f04SJohn Johansen { 202498849dffSJohn Johansen struct aa_ns *sub; 20250d259f04SJohn Johansen struct aa_profile *child; 202698407f0aSJohn Johansen struct dentry *dir; 20270d259f04SJohn Johansen int error; 20280d259f04SJohn Johansen 2029b7fd2c03SJohn Johansen AA_BUG(!ns); 2030b7fd2c03SJohn Johansen AA_BUG(!parent); 2031b7fd2c03SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 2032b7fd2c03SJohn Johansen 20330d259f04SJohn Johansen if (!name) 20340d259f04SJohn Johansen name = ns->base.name; 20350d259f04SJohn Johansen 2036c961ee5fSJohn Johansen if (!dent) { 2037b7fd2c03SJohn Johansen /* create ns dir if it doesn't already exist */ 2038c961ee5fSJohn Johansen dent = aafs_create_dir(name, parent); 20390d259f04SJohn Johansen if (IS_ERR(dent)) 20400d259f04SJohn Johansen goto fail; 2041c961ee5fSJohn Johansen } else 2042c961ee5fSJohn Johansen dget(dent); 20430d259f04SJohn Johansen ns_dir(ns) = dir = dent; 2044c97204baSJohn Johansen error = __aafs_ns_mkdir_entries(ns, dir); 2045b7fd2c03SJohn Johansen if (error) 2046b7fd2c03SJohn Johansen goto fail2; 20470d259f04SJohn Johansen 2048b7fd2c03SJohn Johansen /* profiles */ 20490d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) { 2050c97204baSJohn Johansen error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns)); 20510d259f04SJohn Johansen if (error) 20520d259f04SJohn Johansen goto fail2; 20530d259f04SJohn Johansen } 20540d259f04SJohn Johansen 2055b7fd2c03SJohn Johansen /* subnamespaces */ 20560d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 2057feb3c766SJohn Johansen mutex_lock_nested(&sub->lock, sub->level); 205898407f0aSJohn Johansen error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL); 20590d259f04SJohn Johansen mutex_unlock(&sub->lock); 20600d259f04SJohn Johansen if (error) 20610d259f04SJohn Johansen goto fail2; 20620d259f04SJohn Johansen } 20630d259f04SJohn Johansen 20640d259f04SJohn Johansen return 0; 20650d259f04SJohn Johansen 20660d259f04SJohn Johansen fail: 20670d259f04SJohn Johansen error = PTR_ERR(dent); 20680d259f04SJohn Johansen 20690d259f04SJohn Johansen fail2: 2070c97204baSJohn Johansen __aafs_ns_rmdir(ns); 20710d259f04SJohn Johansen 20720d259f04SJohn Johansen return error; 20730d259f04SJohn Johansen } 20740d259f04SJohn Johansen 20750d259f04SJohn Johansen 207629b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head)) 207729b3822fSJohn Johansen 207829b3822fSJohn Johansen /** 207998849dffSJohn Johansen * __next_ns - find the next namespace to list 208029b3822fSJohn Johansen * @root: root namespace to stop search at (NOT NULL) 208129b3822fSJohn Johansen * @ns: current ns position (NOT NULL) 208229b3822fSJohn Johansen * 208329b3822fSJohn Johansen * Find the next namespace from @ns under @root and handle all locking needed 208429b3822fSJohn Johansen * while switching current namespace. 208529b3822fSJohn Johansen * 208629b3822fSJohn Johansen * Returns: next namespace or NULL if at last namespace under @root 208729b3822fSJohn Johansen * Requires: ns->parent->lock to be held 208829b3822fSJohn Johansen * NOTE: will not unlock root->lock 208929b3822fSJohn Johansen */ 209098849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) 209129b3822fSJohn Johansen { 209298849dffSJohn Johansen struct aa_ns *parent, *next; 209329b3822fSJohn Johansen 2094cbf2d0e1SJohn Johansen AA_BUG(!root); 2095cbf2d0e1SJohn Johansen AA_BUG(!ns); 2096cbf2d0e1SJohn Johansen AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock)); 2097cbf2d0e1SJohn Johansen 209829b3822fSJohn Johansen /* is next namespace a child */ 209929b3822fSJohn Johansen if (!list_empty(&ns->sub_ns)) { 210029b3822fSJohn Johansen next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); 2101feb3c766SJohn Johansen mutex_lock_nested(&next->lock, next->level); 210229b3822fSJohn Johansen return next; 210329b3822fSJohn Johansen } 210429b3822fSJohn Johansen 210529b3822fSJohn Johansen /* check if the next ns is a sibling, parent, gp, .. */ 210629b3822fSJohn Johansen parent = ns->parent; 2107ed2c7da3SJohn Johansen while (ns != root) { 210829b3822fSJohn Johansen mutex_unlock(&ns->lock); 210938dbd7d8SGeliang Tang next = list_next_entry(ns, base.list); 211029b3822fSJohn Johansen if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { 2111feb3c766SJohn Johansen mutex_lock_nested(&next->lock, next->level); 211229b3822fSJohn Johansen return next; 211329b3822fSJohn Johansen } 211429b3822fSJohn Johansen ns = parent; 211529b3822fSJohn Johansen parent = parent->parent; 211629b3822fSJohn Johansen } 211729b3822fSJohn Johansen 211829b3822fSJohn Johansen return NULL; 211929b3822fSJohn Johansen } 212029b3822fSJohn Johansen 212129b3822fSJohn Johansen /** 212229b3822fSJohn Johansen * __first_profile - find the first profile in a namespace 212329b3822fSJohn Johansen * @root: namespace that is root of profiles being displayed (NOT NULL) 212429b3822fSJohn Johansen * @ns: namespace to start in (NOT NULL) 212529b3822fSJohn Johansen * 212629b3822fSJohn Johansen * Returns: unrefcounted profile or NULL if no profile 212729b3822fSJohn Johansen * Requires: profile->ns.lock to be held 212829b3822fSJohn Johansen */ 212998849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root, 213098849dffSJohn Johansen struct aa_ns *ns) 213129b3822fSJohn Johansen { 2132cbf2d0e1SJohn Johansen AA_BUG(!root); 2133cbf2d0e1SJohn Johansen AA_BUG(ns && !mutex_is_locked(&ns->lock)); 2134cbf2d0e1SJohn Johansen 213598849dffSJohn Johansen for (; ns; ns = __next_ns(root, ns)) { 213629b3822fSJohn Johansen if (!list_empty(&ns->base.profiles)) 213729b3822fSJohn Johansen return list_first_entry(&ns->base.profiles, 213829b3822fSJohn Johansen struct aa_profile, base.list); 213929b3822fSJohn Johansen } 214029b3822fSJohn Johansen return NULL; 214129b3822fSJohn Johansen } 214229b3822fSJohn Johansen 214329b3822fSJohn Johansen /** 214429b3822fSJohn Johansen * __next_profile - step to the next profile in a profile tree 214529b3822fSJohn Johansen * @profile: current profile in tree (NOT NULL) 214629b3822fSJohn Johansen * 214729b3822fSJohn Johansen * Perform a depth first traversal on the profile tree in a namespace 214829b3822fSJohn Johansen * 214929b3822fSJohn Johansen * Returns: next profile or NULL if done 215029b3822fSJohn Johansen * Requires: profile->ns.lock to be held 215129b3822fSJohn Johansen */ 215229b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p) 215329b3822fSJohn Johansen { 215429b3822fSJohn Johansen struct aa_profile *parent; 215598849dffSJohn Johansen struct aa_ns *ns = p->ns; 215629b3822fSJohn Johansen 2157cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock)); 2158cbf2d0e1SJohn Johansen 215929b3822fSJohn Johansen /* is next profile a child */ 216029b3822fSJohn Johansen if (!list_empty(&p->base.profiles)) 216129b3822fSJohn Johansen return list_first_entry(&p->base.profiles, typeof(*p), 216229b3822fSJohn Johansen base.list); 216329b3822fSJohn Johansen 216429b3822fSJohn Johansen /* is next profile a sibling, parent sibling, gp, sibling, .. */ 216529b3822fSJohn Johansen parent = rcu_dereference_protected(p->parent, 216629b3822fSJohn Johansen mutex_is_locked(&p->ns->lock)); 216729b3822fSJohn Johansen while (parent) { 216838dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 216929b3822fSJohn Johansen if (!list_entry_is_head(p, &parent->base.profiles, base.list)) 217029b3822fSJohn Johansen return p; 217129b3822fSJohn Johansen p = parent; 217229b3822fSJohn Johansen parent = rcu_dereference_protected(parent->parent, 217329b3822fSJohn Johansen mutex_is_locked(&parent->ns->lock)); 217429b3822fSJohn Johansen } 217529b3822fSJohn Johansen 217629b3822fSJohn Johansen /* is next another profile in the namespace */ 217738dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 217829b3822fSJohn Johansen if (!list_entry_is_head(p, &ns->base.profiles, base.list)) 217929b3822fSJohn Johansen return p; 218029b3822fSJohn Johansen 218129b3822fSJohn Johansen return NULL; 218229b3822fSJohn Johansen } 218329b3822fSJohn Johansen 218429b3822fSJohn Johansen /** 218529b3822fSJohn Johansen * next_profile - step to the next profile in where ever it may be 218629b3822fSJohn Johansen * @root: root namespace (NOT NULL) 218729b3822fSJohn Johansen * @profile: current profile (NOT NULL) 218829b3822fSJohn Johansen * 218929b3822fSJohn Johansen * Returns: next profile or NULL if there isn't one 219029b3822fSJohn Johansen */ 219198849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root, 219229b3822fSJohn Johansen struct aa_profile *profile) 219329b3822fSJohn Johansen { 219429b3822fSJohn Johansen struct aa_profile *next = __next_profile(profile); 219529b3822fSJohn Johansen if (next) 219629b3822fSJohn Johansen return next; 219729b3822fSJohn Johansen 219829b3822fSJohn Johansen /* finished all profiles in namespace move to next namespace */ 219998849dffSJohn Johansen return __first_profile(root, __next_ns(root, profile->ns)); 220029b3822fSJohn Johansen } 220129b3822fSJohn Johansen 220229b3822fSJohn Johansen /** 220329b3822fSJohn Johansen * p_start - start a depth first traversal of profile tree 220429b3822fSJohn Johansen * @f: seq_file to fill 220529b3822fSJohn Johansen * @pos: current position 220629b3822fSJohn Johansen * 220729b3822fSJohn Johansen * Returns: first profile under current namespace or NULL if none found 220829b3822fSJohn Johansen * 220929b3822fSJohn Johansen * acquires first ns->lock 221029b3822fSJohn Johansen */ 221129b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos) 221229b3822fSJohn Johansen { 221329b3822fSJohn Johansen struct aa_profile *profile = NULL; 2214cf797c0eSJohn Johansen struct aa_ns *root = aa_get_current_ns(); 221529b3822fSJohn Johansen loff_t l = *pos; 2216cf797c0eSJohn Johansen f->private = root; 221729b3822fSJohn Johansen 221829b3822fSJohn Johansen /* find the first profile */ 2219feb3c766SJohn Johansen mutex_lock_nested(&root->lock, root->level); 222029b3822fSJohn Johansen profile = __first_profile(root, root); 222129b3822fSJohn Johansen 222229b3822fSJohn Johansen /* skip to position */ 222329b3822fSJohn Johansen for (; profile && l > 0; l--) 222429b3822fSJohn Johansen profile = next_profile(root, profile); 222529b3822fSJohn Johansen 222629b3822fSJohn Johansen return profile; 222729b3822fSJohn Johansen } 222829b3822fSJohn Johansen 222929b3822fSJohn Johansen /** 223029b3822fSJohn Johansen * p_next - read the next profile entry 223129b3822fSJohn Johansen * @f: seq_file to fill 223229b3822fSJohn Johansen * @p: profile previously returned 223329b3822fSJohn Johansen * @pos: current position 223429b3822fSJohn Johansen * 223529b3822fSJohn Johansen * Returns: next profile after @p or NULL if none 223629b3822fSJohn Johansen * 223729b3822fSJohn Johansen * may acquire/release locks in namespace tree as necessary 223829b3822fSJohn Johansen */ 223929b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos) 224029b3822fSJohn Johansen { 224129b3822fSJohn Johansen struct aa_profile *profile = p; 224298849dffSJohn Johansen struct aa_ns *ns = f->private; 224329b3822fSJohn Johansen (*pos)++; 224429b3822fSJohn Johansen 224529b3822fSJohn Johansen return next_profile(ns, profile); 224629b3822fSJohn Johansen } 224729b3822fSJohn Johansen 224829b3822fSJohn Johansen /** 224929b3822fSJohn Johansen * p_stop - stop depth first traversal 225029b3822fSJohn Johansen * @f: seq_file we are filling 225129b3822fSJohn Johansen * @p: the last profile writen 225229b3822fSJohn Johansen * 225329b3822fSJohn Johansen * Release all locking done by p_start/p_next on namespace tree 225429b3822fSJohn Johansen */ 225529b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p) 225629b3822fSJohn Johansen { 225729b3822fSJohn Johansen struct aa_profile *profile = p; 225898849dffSJohn Johansen struct aa_ns *root = f->private, *ns; 225929b3822fSJohn Johansen 226029b3822fSJohn Johansen if (profile) { 226129b3822fSJohn Johansen for (ns = profile->ns; ns && ns != root; ns = ns->parent) 226229b3822fSJohn Johansen mutex_unlock(&ns->lock); 226329b3822fSJohn Johansen } 226429b3822fSJohn Johansen mutex_unlock(&root->lock); 226598849dffSJohn Johansen aa_put_ns(root); 226629b3822fSJohn Johansen } 226729b3822fSJohn Johansen 226829b3822fSJohn Johansen /** 226929b3822fSJohn Johansen * seq_show_profile - show a profile entry 227029b3822fSJohn Johansen * @f: seq_file to file 227129b3822fSJohn Johansen * @p: current position (profile) (NOT NULL) 227229b3822fSJohn Johansen * 227329b3822fSJohn Johansen * Returns: error on failure 227429b3822fSJohn Johansen */ 227529b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p) 227629b3822fSJohn Johansen { 227729b3822fSJohn Johansen struct aa_profile *profile = (struct aa_profile *)p; 227898849dffSJohn Johansen struct aa_ns *root = f->private; 227929b3822fSJohn Johansen 2280637f688dSJohn Johansen aa_label_seq_xprint(f, root, &profile->label, 2281637f688dSJohn Johansen FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL); 2282637f688dSJohn Johansen seq_putc(f, '\n'); 228329b3822fSJohn Johansen 228429b3822fSJohn Johansen return 0; 228529b3822fSJohn Johansen } 228629b3822fSJohn Johansen 2287c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = { 228829b3822fSJohn Johansen .start = p_start, 228929b3822fSJohn Johansen .next = p_next, 229029b3822fSJohn Johansen .stop = p_stop, 229129b3822fSJohn Johansen .show = seq_show_profile, 229229b3822fSJohn Johansen }; 229329b3822fSJohn Johansen 229429b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file) 229529b3822fSJohn Johansen { 22965ac8c355SJohn Johansen if (!policy_view_capable(NULL)) 22975ac8c355SJohn Johansen return -EACCES; 22985ac8c355SJohn Johansen 2299c97204baSJohn Johansen return seq_open(file, &aa_sfs_profiles_op); 230029b3822fSJohn Johansen } 230129b3822fSJohn Johansen 230229b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file) 230329b3822fSJohn Johansen { 230429b3822fSJohn Johansen return seq_release(inode, file); 230529b3822fSJohn Johansen } 230629b3822fSJohn Johansen 2307c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = { 230829b3822fSJohn Johansen .open = profiles_open, 230929b3822fSJohn Johansen .read = seq_read, 231029b3822fSJohn Johansen .llseek = seq_lseek, 231129b3822fSJohn Johansen .release = profiles_release, 231229b3822fSJohn Johansen }; 231329b3822fSJohn Johansen 231429b3822fSJohn Johansen 23150d259f04SJohn Johansen /** Base file system setup **/ 2316c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = { 2317c97204baSJohn Johansen AA_SFS_FILE_STRING("mask", 2318c97204baSJohn Johansen "create read write exec append mmap_exec link lock"), 2319a9bf8e9fSKees Cook { } 2320a9bf8e9fSKees Cook }; 2321a9bf8e9fSKees Cook 2322290f458aSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ptrace[] = { 2323290f458aSJohn Johansen AA_SFS_FILE_STRING("mask", "read trace"), 2324290f458aSJohn Johansen { } 2325290f458aSJohn Johansen }; 2326290f458aSJohn Johansen 2327cd1dbf76SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_signal[] = { 2328cd1dbf76SJohn Johansen AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK), 2329cd1dbf76SJohn Johansen { } 2330cd1dbf76SJohn Johansen }; 2331cd1dbf76SJohn Johansen 233273f488cdSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_attach[] = { 233373f488cdSJohn Johansen AA_SFS_FILE_BOOLEAN("xattr", 1), 233473f488cdSJohn Johansen { } 233573f488cdSJohn Johansen }; 2336c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = { 2337c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hat", 1), 2338c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hatv", 1), 2339c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_onexec", 1), 2340c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_profile", 1), 23416c5fc8f1SJohn Johansen AA_SFS_FILE_BOOLEAN("stack", 1), 2342c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), 23439fcf78ccSJohn Johansen AA_SFS_FILE_BOOLEAN("post_nnp_subset", 1), 234421f60661SJohn Johansen AA_SFS_FILE_BOOLEAN("computed_longest_left", 1), 234573f488cdSJohn Johansen AA_SFS_DIR("attach_conditions", aa_sfs_entry_attach), 2346c97204baSJohn Johansen AA_SFS_FILE_STRING("version", "1.2"), 2347e74abcf3SKees Cook { } 2348e74abcf3SKees Cook }; 2349e74abcf3SKees Cook 2350c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = { 2351c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("v5", 1), 23525379a331SJohn Johansen AA_SFS_FILE_BOOLEAN("v6", 1), 23535379a331SJohn Johansen AA_SFS_FILE_BOOLEAN("v7", 1), 235456974a6fSJohn Johansen AA_SFS_FILE_BOOLEAN("v8", 1), 2355474d6b75SJohn Johansen { } 2356474d6b75SJohn Johansen }; 2357474d6b75SJohn Johansen 2358c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = { 2359c97204baSJohn Johansen AA_SFS_DIR("versions", aa_sfs_entry_versions), 2360c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("set_load", 1), 23619d910a3bSJohn Johansen { } 23629d910a3bSJohn Johansen }; 23639d910a3bSJohn Johansen 23642ea3ffb7SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_mount[] = { 23652ea3ffb7SJohn Johansen AA_SFS_FILE_STRING("mask", "mount umount pivot_root"), 23662ea3ffb7SJohn Johansen { } 23672ea3ffb7SJohn Johansen }; 23682ea3ffb7SJohn Johansen 236933f2eadaSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ns[] = { 237033f2eadaSJohn Johansen AA_SFS_FILE_BOOLEAN("profile", 1), 23712ea3ffb7SJohn Johansen AA_SFS_FILE_BOOLEAN("pivot_root", 0), 237233f2eadaSJohn Johansen { } 237333f2eadaSJohn Johansen }; 237433f2eadaSJohn Johansen 2375a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = { 23764f3b3f2dSJohn Johansen AA_SFS_FILE_STRING("perms", "allow deny audit quiet"), 2377a83bd86eSJohn Johansen AA_SFS_FILE_BOOLEAN("data", 1), 23781dea3b41SJohn Johansen AA_SFS_FILE_BOOLEAN("multi_transaction", 1), 2379a83bd86eSJohn Johansen { } 2380a83bd86eSJohn Johansen }; 2381a83bd86eSJohn Johansen 2382a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = { 2383a83bd86eSJohn Johansen AA_SFS_DIR("label", aa_sfs_entry_query_label), 2384a83bd86eSJohn Johansen { } 2385a83bd86eSJohn Johansen }; 2386c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = { 2387c97204baSJohn Johansen AA_SFS_DIR("policy", aa_sfs_entry_policy), 2388c97204baSJohn Johansen AA_SFS_DIR("domain", aa_sfs_entry_domain), 2389c97204baSJohn Johansen AA_SFS_DIR("file", aa_sfs_entry_file), 239056974a6fSJohn Johansen AA_SFS_DIR("network_v8", aa_sfs_entry_network), 23912ea3ffb7SJohn Johansen AA_SFS_DIR("mount", aa_sfs_entry_mount), 239233f2eadaSJohn Johansen AA_SFS_DIR("namespaces", aa_sfs_entry_ns), 2393c97204baSJohn Johansen AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 2394c97204baSJohn Johansen AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit), 2395c97204baSJohn Johansen AA_SFS_DIR("caps", aa_sfs_entry_caps), 2396290f458aSJohn Johansen AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace), 2397cd1dbf76SJohn Johansen AA_SFS_DIR("signal", aa_sfs_entry_signal), 2398a83bd86eSJohn Johansen AA_SFS_DIR("query", aa_sfs_entry_query), 2399e74abcf3SKees Cook { } 2400e74abcf3SKees Cook }; 2401e74abcf3SKees Cook 2402c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = { 2403bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access), 24046c5fc8f1SJohn Johansen AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops), 24056c5fc8f1SJohn Johansen AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops), 2406bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops), 2407bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops), 2408bf81100fSJohn Johansen AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops), 2409c97204baSJohn Johansen AA_SFS_DIR("features", aa_sfs_entry_features), 24109acd494bSKees Cook { } 24119acd494bSKees Cook }; 241263e2b423SJohn Johansen 2413c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry = 2414c97204baSJohn Johansen AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor); 24159acd494bSKees Cook 24169acd494bSKees Cook /** 2417a481f4d9SJohn Johansen * entry_create_file - create a file entry in the apparmor securityfs 2418c97204baSJohn Johansen * @fs_file: aa_sfs_entry to build an entry for (NOT NULL) 24199acd494bSKees Cook * @parent: the parent dentry in the securityfs 24209acd494bSKees Cook * 2421a481f4d9SJohn Johansen * Use entry_remove_file to remove entries created with this fn. 24229acd494bSKees Cook */ 2423c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file, 24249acd494bSKees Cook struct dentry *parent) 242563e2b423SJohn Johansen { 24269acd494bSKees Cook int error = 0; 242763e2b423SJohn Johansen 24289acd494bSKees Cook fs_file->dentry = securityfs_create_file(fs_file->name, 24299acd494bSKees Cook S_IFREG | fs_file->mode, 24309acd494bSKees Cook parent, fs_file, 24319acd494bSKees Cook fs_file->file_ops); 24329acd494bSKees Cook if (IS_ERR(fs_file->dentry)) { 24339acd494bSKees Cook error = PTR_ERR(fs_file->dentry); 24349acd494bSKees Cook fs_file->dentry = NULL; 243563e2b423SJohn Johansen } 24369acd494bSKees Cook return error; 243763e2b423SJohn Johansen } 243863e2b423SJohn Johansen 2439c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir); 244063e2b423SJohn Johansen /** 2441a481f4d9SJohn Johansen * entry_create_dir - recursively create a directory entry in the securityfs 2442c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL) 24439acd494bSKees Cook * @parent: the parent dentry in the securityfs 244463e2b423SJohn Johansen * 2445a481f4d9SJohn Johansen * Use entry_remove_dir to remove entries created with this fn. 244663e2b423SJohn Johansen */ 2447c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir, 24489acd494bSKees Cook struct dentry *parent) 244963e2b423SJohn Johansen { 2450c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 24510d259f04SJohn Johansen struct dentry *dir; 24520d259f04SJohn Johansen int error; 245363e2b423SJohn Johansen 24540d259f04SJohn Johansen dir = securityfs_create_dir(fs_dir->name, parent); 24550d259f04SJohn Johansen if (IS_ERR(dir)) 24560d259f04SJohn Johansen return PTR_ERR(dir); 24570d259f04SJohn Johansen fs_dir->dentry = dir; 245863e2b423SJohn Johansen 24590d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2460c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2461a481f4d9SJohn Johansen error = entry_create_dir(fs_file, fs_dir->dentry); 24629acd494bSKees Cook else 2463a481f4d9SJohn Johansen error = entry_create_file(fs_file, fs_dir->dentry); 24649acd494bSKees Cook if (error) 24659acd494bSKees Cook goto failed; 24669acd494bSKees Cook } 24679acd494bSKees Cook 24689acd494bSKees Cook return 0; 24699acd494bSKees Cook 24709acd494bSKees Cook failed: 2471a481f4d9SJohn Johansen entry_remove_dir(fs_dir); 24720d259f04SJohn Johansen 24739acd494bSKees Cook return error; 24749acd494bSKees Cook } 24759acd494bSKees Cook 24769acd494bSKees Cook /** 2477c97204baSJohn Johansen * entry_remove_file - drop a single file entry in the apparmor securityfs 2478c97204baSJohn Johansen * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL) 24799acd494bSKees Cook */ 2480c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file) 24819acd494bSKees Cook { 24829acd494bSKees Cook if (!fs_file->dentry) 24839acd494bSKees Cook return; 24849acd494bSKees Cook 24859acd494bSKees Cook securityfs_remove(fs_file->dentry); 24869acd494bSKees Cook fs_file->dentry = NULL; 24879acd494bSKees Cook } 24889acd494bSKees Cook 24899acd494bSKees Cook /** 2490a481f4d9SJohn Johansen * entry_remove_dir - recursively drop a directory entry from the securityfs 2491c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL) 24929acd494bSKees Cook */ 2493c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir) 24949acd494bSKees Cook { 2495c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 24969acd494bSKees Cook 24970d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2498c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2499a481f4d9SJohn Johansen entry_remove_dir(fs_file); 25009acd494bSKees Cook else 2501c97204baSJohn Johansen entry_remove_file(fs_file); 25029acd494bSKees Cook } 25039acd494bSKees Cook 2504c97204baSJohn Johansen entry_remove_file(fs_dir); 250563e2b423SJohn Johansen } 250663e2b423SJohn Johansen 250763e2b423SJohn Johansen /** 250863e2b423SJohn Johansen * aa_destroy_aafs - cleanup and free aafs 250963e2b423SJohn Johansen * 251063e2b423SJohn Johansen * releases dentries allocated by aa_create_aafs 251163e2b423SJohn Johansen */ 251263e2b423SJohn Johansen void __init aa_destroy_aafs(void) 251363e2b423SJohn Johansen { 2514c97204baSJohn Johansen entry_remove_dir(&aa_sfs_entry); 251563e2b423SJohn Johansen } 251663e2b423SJohn Johansen 2517a71ada30SJohn Johansen 2518a71ada30SJohn Johansen #define NULL_FILE_NAME ".null" 2519a71ada30SJohn Johansen struct path aa_null; 2520a71ada30SJohn Johansen 2521a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent) 2522a71ada30SJohn Johansen { 2523a71ada30SJohn Johansen struct vfsmount *mount = NULL; 2524a71ada30SJohn Johansen struct dentry *dentry; 2525a71ada30SJohn Johansen struct inode *inode; 2526a71ada30SJohn Johansen int count = 0; 2527a71ada30SJohn Johansen int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); 2528a71ada30SJohn Johansen 2529a71ada30SJohn Johansen if (error) 2530a71ada30SJohn Johansen return error; 2531a71ada30SJohn Johansen 2532a71ada30SJohn Johansen inode_lock(d_inode(parent)); 2533a71ada30SJohn Johansen dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); 2534a71ada30SJohn Johansen if (IS_ERR(dentry)) { 2535a71ada30SJohn Johansen error = PTR_ERR(dentry); 2536a71ada30SJohn Johansen goto out; 2537a71ada30SJohn Johansen } 2538a71ada30SJohn Johansen inode = new_inode(parent->d_inode->i_sb); 2539a71ada30SJohn Johansen if (!inode) { 2540a71ada30SJohn Johansen error = -ENOMEM; 2541a71ada30SJohn Johansen goto out1; 2542a71ada30SJohn Johansen } 2543a71ada30SJohn Johansen 2544a71ada30SJohn Johansen inode->i_ino = get_next_ino(); 2545a71ada30SJohn Johansen inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; 254624d0d03cSDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2547a71ada30SJohn Johansen init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, 2548a71ada30SJohn Johansen MKDEV(MEM_MAJOR, 3)); 2549a71ada30SJohn Johansen d_instantiate(dentry, inode); 2550a71ada30SJohn Johansen aa_null.dentry = dget(dentry); 2551a71ada30SJohn Johansen aa_null.mnt = mntget(mount); 2552a71ada30SJohn Johansen 2553a71ada30SJohn Johansen error = 0; 2554a71ada30SJohn Johansen 2555a71ada30SJohn Johansen out1: 2556a71ada30SJohn Johansen dput(dentry); 2557a71ada30SJohn Johansen out: 2558a71ada30SJohn Johansen inode_unlock(d_inode(parent)); 2559a71ada30SJohn Johansen simple_release_fs(&mount, &count); 2560a71ada30SJohn Johansen return error; 2561a71ada30SJohn Johansen } 2562a71ada30SJohn Johansen 2563a481f4d9SJohn Johansen 2564a481f4d9SJohn Johansen 2565a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry, 2566a481f4d9SJohn Johansen struct inode *inode, 2567a481f4d9SJohn Johansen struct delayed_call *done) 2568a481f4d9SJohn Johansen { 2569a481f4d9SJohn Johansen struct aa_ns *ns; 2570a481f4d9SJohn Johansen struct path path; 2571a481f4d9SJohn Johansen 2572a481f4d9SJohn Johansen if (!dentry) 2573a481f4d9SJohn Johansen return ERR_PTR(-ECHILD); 2574a481f4d9SJohn Johansen ns = aa_get_current_ns(); 2575a481f4d9SJohn Johansen path.mnt = mntget(aafs_mnt); 2576a481f4d9SJohn Johansen path.dentry = dget(ns_dir(ns)); 2577a481f4d9SJohn Johansen nd_jump_link(&path); 2578a481f4d9SJohn Johansen aa_put_ns(ns); 2579a481f4d9SJohn Johansen 2580a481f4d9SJohn Johansen return NULL; 2581a481f4d9SJohn Johansen } 2582a481f4d9SJohn Johansen 2583a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer, 2584a481f4d9SJohn Johansen int buflen) 2585a481f4d9SJohn Johansen { 2586a481f4d9SJohn Johansen char name[32]; 2587a481f4d9SJohn Johansen int res; 2588a481f4d9SJohn Johansen 2589a0781209SJohn Johansen res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME, 2590a0781209SJohn Johansen d_inode(dentry)->i_ino); 2591a0781209SJohn Johansen if (res > 0 && res < sizeof(name)) 2592a481f4d9SJohn Johansen res = readlink_copy(buffer, buflen, name); 2593a0781209SJohn Johansen else 2594a0781209SJohn Johansen res = -ENOENT; 2595a481f4d9SJohn Johansen 2596a481f4d9SJohn Johansen return res; 2597a481f4d9SJohn Johansen } 2598a481f4d9SJohn Johansen 2599a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = { 2600a481f4d9SJohn Johansen .readlink = policy_readlink, 2601a481f4d9SJohn Johansen .get_link = policy_get_link, 2602a481f4d9SJohn Johansen }; 2603a481f4d9SJohn Johansen 2604a481f4d9SJohn Johansen 260563e2b423SJohn Johansen /** 260663e2b423SJohn Johansen * aa_create_aafs - create the apparmor security filesystem 260763e2b423SJohn Johansen * 260863e2b423SJohn Johansen * dentries created here are released by aa_destroy_aafs 260963e2b423SJohn Johansen * 261063e2b423SJohn Johansen * Returns: error on failure 261163e2b423SJohn Johansen */ 26123417d8d5SJames Morris static int __init aa_create_aafs(void) 261363e2b423SJohn Johansen { 2614b7fd2c03SJohn Johansen struct dentry *dent; 261563e2b423SJohn Johansen int error; 261663e2b423SJohn Johansen 261763e2b423SJohn Johansen if (!apparmor_initialized) 261863e2b423SJohn Johansen return 0; 261963e2b423SJohn Johansen 2620c97204baSJohn Johansen if (aa_sfs_entry.dentry) { 262163e2b423SJohn Johansen AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 262263e2b423SJohn Johansen return -EEXIST; 262363e2b423SJohn Johansen } 262463e2b423SJohn Johansen 2625a481f4d9SJohn Johansen /* setup apparmorfs used to virtualize policy/ */ 2626a481f4d9SJohn Johansen aafs_mnt = kern_mount(&aafs_ops); 2627a481f4d9SJohn Johansen if (IS_ERR(aafs_mnt)) 2628a481f4d9SJohn Johansen panic("can't set apparmorfs up\n"); 26291751e8a6SLinus Torvalds aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER; 2630a481f4d9SJohn Johansen 26319acd494bSKees Cook /* Populate fs tree. */ 2632c97204baSJohn Johansen error = entry_create_dir(&aa_sfs_entry, NULL); 263363e2b423SJohn Johansen if (error) 263463e2b423SJohn Johansen goto error; 263563e2b423SJohn Johansen 2636c97204baSJohn Johansen dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry, 2637b7fd2c03SJohn Johansen NULL, &aa_fs_profile_load); 2638cf916000SJohn Johansen if (IS_ERR(dent)) 2639cf916000SJohn Johansen goto dent_error; 2640b7fd2c03SJohn Johansen ns_subload(root_ns) = dent; 2641b7fd2c03SJohn Johansen 2642c97204baSJohn Johansen dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry, 2643b7fd2c03SJohn Johansen NULL, &aa_fs_profile_replace); 2644cf916000SJohn Johansen if (IS_ERR(dent)) 2645cf916000SJohn Johansen goto dent_error; 2646b7fd2c03SJohn Johansen ns_subreplace(root_ns) = dent; 2647b7fd2c03SJohn Johansen 2648c97204baSJohn Johansen dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry, 2649b7fd2c03SJohn Johansen NULL, &aa_fs_profile_remove); 2650cf916000SJohn Johansen if (IS_ERR(dent)) 2651cf916000SJohn Johansen goto dent_error; 2652b7fd2c03SJohn Johansen ns_subremove(root_ns) = dent; 2653b7fd2c03SJohn Johansen 2654d9bf2c26SJohn Johansen dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry, 2655d9bf2c26SJohn Johansen NULL, &aa_fs_ns_revision_fops); 2656cf916000SJohn Johansen if (IS_ERR(dent)) 2657cf916000SJohn Johansen goto dent_error; 2658d9bf2c26SJohn Johansen ns_subrevision(root_ns) = dent; 2659d9bf2c26SJohn Johansen 2660d9bf2c26SJohn Johansen /* policy tree referenced by magic policy symlink */ 2661feb3c766SJohn Johansen mutex_lock_nested(&root_ns->lock, root_ns->level); 2662c961ee5fSJohn Johansen error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy", 2663c961ee5fSJohn Johansen aafs_mnt->mnt_root); 2664b7fd2c03SJohn Johansen mutex_unlock(&root_ns->lock); 26650d259f04SJohn Johansen if (error) 26660d259f04SJohn Johansen goto error; 26670d259f04SJohn Johansen 2668c961ee5fSJohn Johansen /* magic symlink similar to nsfs redirects based on task policy */ 2669c961ee5fSJohn Johansen dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry, 2670c961ee5fSJohn Johansen NULL, &policy_link_iops); 2671cf916000SJohn Johansen if (IS_ERR(dent)) 2672cf916000SJohn Johansen goto dent_error; 2673c961ee5fSJohn Johansen 2674c97204baSJohn Johansen error = aa_mk_null_file(aa_sfs_entry.dentry); 2675a71ada30SJohn Johansen if (error) 2676a71ada30SJohn Johansen goto error; 2677a71ada30SJohn Johansen 2678a71ada30SJohn Johansen /* TODO: add default profile to apparmorfs */ 267963e2b423SJohn Johansen 268063e2b423SJohn Johansen /* Report that AppArmor fs is enabled */ 268163e2b423SJohn Johansen aa_info_message("AppArmor Filesystem Enabled"); 268263e2b423SJohn Johansen return 0; 268363e2b423SJohn Johansen 2684cf916000SJohn Johansen dent_error: 2685cf916000SJohn Johansen error = PTR_ERR(dent); 268663e2b423SJohn Johansen error: 268763e2b423SJohn Johansen aa_destroy_aafs(); 268863e2b423SJohn Johansen AA_ERROR("Error creating AppArmor securityfs\n"); 268963e2b423SJohn Johansen return error; 269063e2b423SJohn Johansen } 269163e2b423SJohn Johansen 269263e2b423SJohn Johansen fs_initcall(aa_create_aafs); 2693