1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 263e2b423SJohn Johansen /* 363e2b423SJohn Johansen * AppArmor security module 463e2b423SJohn Johansen * 563e2b423SJohn Johansen * This file contains AppArmor /sys/kernel/security/apparmor interface functions 663e2b423SJohn Johansen * 763e2b423SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 863e2b423SJohn Johansen * Copyright 2009-2010 Canonical Ltd. 963e2b423SJohn Johansen */ 1063e2b423SJohn Johansen 110d259f04SJohn Johansen #include <linux/ctype.h> 1263e2b423SJohn Johansen #include <linux/security.h> 1363e2b423SJohn Johansen #include <linux/vmalloc.h> 14876979c9SPaul Gortmaker #include <linux/init.h> 1563e2b423SJohn Johansen #include <linux/seq_file.h> 1663e2b423SJohn Johansen #include <linux/uaccess.h> 17a71ada30SJohn Johansen #include <linux/mount.h> 1863e2b423SJohn Johansen #include <linux/namei.h> 19e74abcf3SKees Cook #include <linux/capability.h> 2029b3822fSJohn Johansen #include <linux/rcupdate.h> 21a71ada30SJohn Johansen #include <linux/fs.h> 22b0ecc9daSDavid Howells #include <linux/fs_context.h> 23d9bf2c26SJohn Johansen #include <linux/poll.h> 2463c16c3aSChris Coulson #include <linux/zlib.h> 25a481f4d9SJohn Johansen #include <uapi/linux/major.h> 26a481f4d9SJohn Johansen #include <uapi/linux/magic.h> 2763e2b423SJohn Johansen 2863e2b423SJohn Johansen #include "include/apparmor.h" 2963e2b423SJohn Johansen #include "include/apparmorfs.h" 3063e2b423SJohn Johansen #include "include/audit.h" 31d8889d49SJohn Johansen #include "include/cred.h" 32f8eb8a13SJohn Johansen #include "include/crypto.h" 33cd1dbf76SJohn Johansen #include "include/ipc.h" 34317d9a05SJohn Johansen #include "include/label.h" 3563e2b423SJohn Johansen #include "include/policy.h" 36cff281f6SJohn Johansen #include "include/policy_ns.h" 37d384b0a1SKees Cook #include "include/resource.h" 385ac8c355SJohn Johansen #include "include/policy_unpack.h" 3963e2b423SJohn Johansen 40c97204baSJohn Johansen /* 41c97204baSJohn Johansen * The apparmor filesystem interface used for policy load and introspection 42c97204baSJohn Johansen * The interface is split into two main components based on their function 43c97204baSJohn Johansen * a securityfs component: 44c97204baSJohn Johansen * used for static files that are always available, and which allows 45c97204baSJohn Johansen * userspace to specificy the location of the security filesystem. 46c97204baSJohn Johansen * 47c97204baSJohn Johansen * fns and data are prefixed with 48c97204baSJohn Johansen * aa_sfs_ 49c97204baSJohn Johansen * 50c97204baSJohn Johansen * an apparmorfs component: 51c97204baSJohn Johansen * used loaded policy content and introspection. It is not part of a 52c97204baSJohn Johansen * regular mounted filesystem and is available only through the magic 53c97204baSJohn Johansen * policy symlink in the root of the securityfs apparmor/ directory. 54c97204baSJohn Johansen * Tasks queries will be magically redirected to the correct portion 55c97204baSJohn Johansen * of the policy tree based on their confinement. 56c97204baSJohn Johansen * 57c97204baSJohn Johansen * fns and data are prefixed with 58c97204baSJohn Johansen * aafs_ 59c97204baSJohn Johansen * 60c97204baSJohn Johansen * The aa_fs_ prefix is used to indicate the fn is used by both the 61c97204baSJohn Johansen * securityfs and apparmorfs filesystems. 62c97204baSJohn Johansen */ 63c97204baSJohn Johansen 64c97204baSJohn Johansen 65c97204baSJohn Johansen /* 66c97204baSJohn Johansen * support fns 67c97204baSJohn Johansen */ 68c97204baSJohn Johansen 6963c16c3aSChris Coulson struct rawdata_f_data { 7063c16c3aSChris Coulson struct aa_loaddata *loaddata; 7163c16c3aSChris Coulson }; 7263c16c3aSChris Coulson 73d61c57fdSJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 7463c16c3aSChris Coulson #define RAWDATA_F_DATA_BUF(p) (char *)(p + 1) 7563c16c3aSChris Coulson 7663c16c3aSChris Coulson static void rawdata_f_data_free(struct rawdata_f_data *private) 7763c16c3aSChris Coulson { 7863c16c3aSChris Coulson if (!private) 7963c16c3aSChris Coulson return; 8063c16c3aSChris Coulson 8163c16c3aSChris Coulson aa_put_loaddata(private->loaddata); 8263c16c3aSChris Coulson kvfree(private); 8363c16c3aSChris Coulson } 8463c16c3aSChris Coulson 8563c16c3aSChris Coulson static struct rawdata_f_data *rawdata_f_data_alloc(size_t size) 8663c16c3aSChris Coulson { 8763c16c3aSChris Coulson struct rawdata_f_data *ret; 8863c16c3aSChris Coulson 8963c16c3aSChris Coulson if (size > SIZE_MAX - sizeof(*ret)) 9063c16c3aSChris Coulson return ERR_PTR(-EINVAL); 9163c16c3aSChris Coulson 9263c16c3aSChris Coulson ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL); 9363c16c3aSChris Coulson if (!ret) 9463c16c3aSChris Coulson return ERR_PTR(-ENOMEM); 9563c16c3aSChris Coulson 9663c16c3aSChris Coulson return ret; 9763c16c3aSChris Coulson } 98d61c57fdSJohn Johansen #endif 9963c16c3aSChris Coulson 10063e2b423SJohn Johansen /** 1010d259f04SJohn Johansen * aa_mangle_name - mangle a profile name to std profile layout form 1020d259f04SJohn Johansen * @name: profile name to mangle (NOT NULL) 1030d259f04SJohn Johansen * @target: buffer to store mangled name, same length as @name (MAYBE NULL) 1040d259f04SJohn Johansen * 1050d259f04SJohn Johansen * Returns: length of mangled name 1060d259f04SJohn Johansen */ 107bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target) 1080d259f04SJohn Johansen { 1090d259f04SJohn Johansen char *t = target; 1100d259f04SJohn Johansen 1110d259f04SJohn Johansen while (*name == '/' || *name == '.') 1120d259f04SJohn Johansen name++; 1130d259f04SJohn Johansen 1140d259f04SJohn Johansen if (target) { 1150d259f04SJohn Johansen for (; *name; name++) { 1160d259f04SJohn Johansen if (*name == '/') 1170d259f04SJohn Johansen *(t)++ = '.'; 1180d259f04SJohn Johansen else if (isspace(*name)) 1190d259f04SJohn Johansen *(t)++ = '_'; 1200d259f04SJohn Johansen else if (isalnum(*name) || strchr("._-", *name)) 1210d259f04SJohn Johansen *(t)++ = *name; 1220d259f04SJohn Johansen } 1230d259f04SJohn Johansen 1240d259f04SJohn Johansen *t = 0; 1250d259f04SJohn Johansen } else { 1260d259f04SJohn Johansen int len = 0; 1270d259f04SJohn Johansen for (; *name; name++) { 1280d259f04SJohn Johansen if (isalnum(*name) || isspace(*name) || 1290d259f04SJohn Johansen strchr("/._-", *name)) 1300d259f04SJohn Johansen len++; 1310d259f04SJohn Johansen } 1320d259f04SJohn Johansen 1330d259f04SJohn Johansen return len; 1340d259f04SJohn Johansen } 1350d259f04SJohn Johansen 1360d259f04SJohn Johansen return t - target; 1370d259f04SJohn Johansen } 1380d259f04SJohn Johansen 139a481f4d9SJohn Johansen 140a481f4d9SJohn Johansen /* 141a481f4d9SJohn Johansen * aafs - core fns and data for the policy tree 142a481f4d9SJohn Johansen */ 143a481f4d9SJohn Johansen 144a481f4d9SJohn Johansen #define AAFS_NAME "apparmorfs" 145a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt; 146a481f4d9SJohn Johansen static int aafs_count; 147a481f4d9SJohn Johansen 148a481f4d9SJohn Johansen 149a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry) 150a481f4d9SJohn Johansen { 151a0781209SJohn Johansen seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino); 152a481f4d9SJohn Johansen return 0; 153a481f4d9SJohn Johansen } 154a481f4d9SJohn Johansen 15527afa27dSAl Viro static void aafs_free_inode(struct inode *inode) 156a481f4d9SJohn Johansen { 157a481f4d9SJohn Johansen if (S_ISLNK(inode->i_mode)) 158a481f4d9SJohn Johansen kfree(inode->i_link); 159f51dcd0fSAl Viro free_inode_nonrcu(inode); 160f51dcd0fSAl Viro } 161f51dcd0fSAl Viro 162a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = { 163a481f4d9SJohn Johansen .statfs = simple_statfs, 16427afa27dSAl Viro .free_inode = aafs_free_inode, 165a481f4d9SJohn Johansen .show_path = aafs_show_path, 166a481f4d9SJohn Johansen }; 167a481f4d9SJohn Johansen 168b0ecc9daSDavid Howells static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc) 169a481f4d9SJohn Johansen { 170a481f4d9SJohn Johansen static struct tree_descr files[] = { {""} }; 171a481f4d9SJohn Johansen int error; 172a481f4d9SJohn Johansen 173a481f4d9SJohn Johansen error = simple_fill_super(sb, AAFS_MAGIC, files); 174a481f4d9SJohn Johansen if (error) 175a481f4d9SJohn Johansen return error; 176a481f4d9SJohn Johansen sb->s_op = &aafs_super_ops; 177a481f4d9SJohn Johansen 178a481f4d9SJohn Johansen return 0; 179a481f4d9SJohn Johansen } 180a481f4d9SJohn Johansen 181b0ecc9daSDavid Howells static int apparmorfs_get_tree(struct fs_context *fc) 182a481f4d9SJohn Johansen { 183b0ecc9daSDavid Howells return get_tree_single(fc, apparmorfs_fill_super); 184b0ecc9daSDavid Howells } 185b0ecc9daSDavid Howells 186b0ecc9daSDavid Howells static const struct fs_context_operations apparmorfs_context_ops = { 187b0ecc9daSDavid Howells .get_tree = apparmorfs_get_tree, 188b0ecc9daSDavid Howells }; 189b0ecc9daSDavid Howells 190b0ecc9daSDavid Howells static int apparmorfs_init_fs_context(struct fs_context *fc) 191b0ecc9daSDavid Howells { 192b0ecc9daSDavid Howells fc->ops = &apparmorfs_context_ops; 193b0ecc9daSDavid Howells return 0; 194a481f4d9SJohn Johansen } 195a481f4d9SJohn Johansen 196a481f4d9SJohn Johansen static struct file_system_type aafs_ops = { 197a481f4d9SJohn Johansen .owner = THIS_MODULE, 198a481f4d9SJohn Johansen .name = AAFS_NAME, 199b0ecc9daSDavid Howells .init_fs_context = apparmorfs_init_fs_context, 200a481f4d9SJohn Johansen .kill_sb = kill_anon_super, 201a481f4d9SJohn Johansen }; 202a481f4d9SJohn Johansen 203a481f4d9SJohn Johansen /** 204a481f4d9SJohn Johansen * __aafs_setup_d_inode - basic inode setup for apparmorfs 205a481f4d9SJohn Johansen * @dir: parent directory for the dentry 206a481f4d9SJohn Johansen * @dentry: dentry we are seting the inode up for 207a481f4d9SJohn Johansen * @mode: permissions the file should have 208a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 209a481f4d9SJohn Johansen * @link: if symlink, symlink target string 210a481f4d9SJohn Johansen * @fops: struct file_operations that should be used 211a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 212a481f4d9SJohn Johansen */ 213a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry, 214a481f4d9SJohn Johansen umode_t mode, void *data, char *link, 215a481f4d9SJohn Johansen const struct file_operations *fops, 216a481f4d9SJohn Johansen const struct inode_operations *iops) 217a481f4d9SJohn Johansen { 218a481f4d9SJohn Johansen struct inode *inode = new_inode(dir->i_sb); 219a481f4d9SJohn Johansen 220a481f4d9SJohn Johansen AA_BUG(!dir); 221a481f4d9SJohn Johansen AA_BUG(!dentry); 222a481f4d9SJohn Johansen 223a481f4d9SJohn Johansen if (!inode) 224a481f4d9SJohn Johansen return -ENOMEM; 225a481f4d9SJohn Johansen 226a481f4d9SJohn Johansen inode->i_ino = get_next_ino(); 227a481f4d9SJohn Johansen inode->i_mode = mode; 228a481f4d9SJohn Johansen inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 229a481f4d9SJohn Johansen inode->i_private = data; 230a481f4d9SJohn Johansen if (S_ISDIR(mode)) { 231a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_dir_inode_operations; 232a481f4d9SJohn Johansen inode->i_fop = &simple_dir_operations; 233a481f4d9SJohn Johansen inc_nlink(inode); 234a481f4d9SJohn Johansen inc_nlink(dir); 235a481f4d9SJohn Johansen } else if (S_ISLNK(mode)) { 236a481f4d9SJohn Johansen inode->i_op = iops ? iops : &simple_symlink_inode_operations; 237a481f4d9SJohn Johansen inode->i_link = link; 238a481f4d9SJohn Johansen } else { 239a481f4d9SJohn Johansen inode->i_fop = fops; 240a481f4d9SJohn Johansen } 241a481f4d9SJohn Johansen d_instantiate(dentry, inode); 242a481f4d9SJohn Johansen dget(dentry); 243a481f4d9SJohn Johansen 244a481f4d9SJohn Johansen return 0; 245a481f4d9SJohn Johansen } 246a481f4d9SJohn Johansen 247a481f4d9SJohn Johansen /** 248a481f4d9SJohn Johansen * aafs_create - create a dentry in the apparmorfs filesystem 249a481f4d9SJohn Johansen * 250a481f4d9SJohn Johansen * @name: name of dentry to create 251a481f4d9SJohn Johansen * @mode: permissions the file should have 252a481f4d9SJohn Johansen * @parent: parent directory for this dentry 253a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 254a481f4d9SJohn Johansen * @link: if symlink, symlink target string 255a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 256a481f4d9SJohn Johansen * @iops: struct of inode_operations that should be used 257a481f4d9SJohn Johansen * 258a481f4d9SJohn Johansen * This is the basic "create a xxx" function for apparmorfs. 259a481f4d9SJohn Johansen * 260a481f4d9SJohn Johansen * Returns a pointer to a dentry if it succeeds, that must be free with 261a481f4d9SJohn Johansen * aafs_remove(). Will return ERR_PTR on failure. 262a481f4d9SJohn Johansen */ 263a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode, 264a481f4d9SJohn Johansen struct dentry *parent, void *data, void *link, 265a481f4d9SJohn Johansen const struct file_operations *fops, 266a481f4d9SJohn Johansen const struct inode_operations *iops) 267a481f4d9SJohn Johansen { 268a481f4d9SJohn Johansen struct dentry *dentry; 269a481f4d9SJohn Johansen struct inode *dir; 270a481f4d9SJohn Johansen int error; 271a481f4d9SJohn Johansen 272a481f4d9SJohn Johansen AA_BUG(!name); 273a481f4d9SJohn Johansen AA_BUG(!parent); 274a481f4d9SJohn Johansen 275a481f4d9SJohn Johansen if (!(mode & S_IFMT)) 276a481f4d9SJohn Johansen mode = (mode & S_IALLUGO) | S_IFREG; 277a481f4d9SJohn Johansen 278a481f4d9SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 279a481f4d9SJohn Johansen if (error) 280a481f4d9SJohn Johansen return ERR_PTR(error); 281a481f4d9SJohn Johansen 282a481f4d9SJohn Johansen dir = d_inode(parent); 283a481f4d9SJohn Johansen 284a481f4d9SJohn Johansen inode_lock(dir); 285a481f4d9SJohn Johansen dentry = lookup_one_len(name, parent, strlen(name)); 2865d314a81SDan Carpenter if (IS_ERR(dentry)) { 2875d314a81SDan Carpenter error = PTR_ERR(dentry); 288a481f4d9SJohn Johansen goto fail_lock; 2895d314a81SDan Carpenter } 290a481f4d9SJohn Johansen 291a481f4d9SJohn Johansen if (d_really_is_positive(dentry)) { 292a481f4d9SJohn Johansen error = -EEXIST; 293a481f4d9SJohn Johansen goto fail_dentry; 294a481f4d9SJohn Johansen } 295a481f4d9SJohn Johansen 296a481f4d9SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops); 297a481f4d9SJohn Johansen if (error) 298a481f4d9SJohn Johansen goto fail_dentry; 299a481f4d9SJohn Johansen inode_unlock(dir); 300a481f4d9SJohn Johansen 301a481f4d9SJohn Johansen return dentry; 302a481f4d9SJohn Johansen 303a481f4d9SJohn Johansen fail_dentry: 304a481f4d9SJohn Johansen dput(dentry); 305a481f4d9SJohn Johansen 306a481f4d9SJohn Johansen fail_lock: 307a481f4d9SJohn Johansen inode_unlock(dir); 308a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 309a481f4d9SJohn Johansen 310a481f4d9SJohn Johansen return ERR_PTR(error); 311a481f4d9SJohn Johansen } 312a481f4d9SJohn Johansen 313a481f4d9SJohn Johansen /** 314a481f4d9SJohn Johansen * aafs_create_file - create a file in the apparmorfs filesystem 315a481f4d9SJohn Johansen * 316a481f4d9SJohn Johansen * @name: name of dentry to create 317a481f4d9SJohn Johansen * @mode: permissions the file should have 318a481f4d9SJohn Johansen * @parent: parent directory for this dentry 319a481f4d9SJohn Johansen * @data: data to store on inode.i_private, available in open() 320a481f4d9SJohn Johansen * @fops: struct file_operations that should be used for 321a481f4d9SJohn Johansen * 322a481f4d9SJohn Johansen * see aafs_create 323a481f4d9SJohn Johansen */ 324a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode, 325a481f4d9SJohn Johansen struct dentry *parent, void *data, 326a481f4d9SJohn Johansen const struct file_operations *fops) 327a481f4d9SJohn Johansen { 328a481f4d9SJohn Johansen return aafs_create(name, mode, parent, data, NULL, fops, NULL); 329a481f4d9SJohn Johansen } 330a481f4d9SJohn Johansen 331a481f4d9SJohn Johansen /** 332a481f4d9SJohn Johansen * aafs_create_dir - create a directory in the apparmorfs filesystem 333a481f4d9SJohn Johansen * 334a481f4d9SJohn Johansen * @name: name of dentry to create 335a481f4d9SJohn Johansen * @parent: parent directory for this dentry 336a481f4d9SJohn Johansen * 337a481f4d9SJohn Johansen * see aafs_create 338a481f4d9SJohn Johansen */ 339a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent) 340a481f4d9SJohn Johansen { 341a481f4d9SJohn Johansen return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL, 342a481f4d9SJohn Johansen NULL); 343a481f4d9SJohn Johansen } 344a481f4d9SJohn Johansen 345a481f4d9SJohn Johansen /** 346a481f4d9SJohn Johansen * aafs_remove - removes a file or directory from the apparmorfs filesystem 347a481f4d9SJohn Johansen * 348a481f4d9SJohn Johansen * @dentry: dentry of the file/directory/symlink to removed. 349a481f4d9SJohn Johansen */ 350a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry) 351a481f4d9SJohn Johansen { 352a481f4d9SJohn Johansen struct inode *dir; 353a481f4d9SJohn Johansen 354a481f4d9SJohn Johansen if (!dentry || IS_ERR(dentry)) 355a481f4d9SJohn Johansen return; 356a481f4d9SJohn Johansen 357a481f4d9SJohn Johansen dir = d_inode(dentry->d_parent); 358a481f4d9SJohn Johansen inode_lock(dir); 359a481f4d9SJohn Johansen if (simple_positive(dentry)) { 360a481f4d9SJohn Johansen if (d_is_dir(dentry)) 361a481f4d9SJohn Johansen simple_rmdir(dir, dentry); 362a481f4d9SJohn Johansen else 363a481f4d9SJohn Johansen simple_unlink(dir, dentry); 364201218e4SChris Coulson d_delete(dentry); 365a481f4d9SJohn Johansen dput(dentry); 366a481f4d9SJohn Johansen } 367a481f4d9SJohn Johansen inode_unlock(dir); 368a481f4d9SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 369a481f4d9SJohn Johansen } 370a481f4d9SJohn Johansen 371a481f4d9SJohn Johansen 372a481f4d9SJohn Johansen /* 373a481f4d9SJohn Johansen * aa_fs - policy load/replace/remove 374a481f4d9SJohn Johansen */ 375a481f4d9SJohn Johansen 3760d259f04SJohn Johansen /** 37763e2b423SJohn Johansen * aa_simple_write_to_buffer - common routine for getting policy from user 37863e2b423SJohn Johansen * @userbuf: user buffer to copy data from (NOT NULL) 3793ed02adaSJohn Johansen * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 38063e2b423SJohn Johansen * @copy_size: size of data to copy from user buffer 38163e2b423SJohn Johansen * @pos: position write is at in the file (NOT NULL) 38263e2b423SJohn Johansen * 38363e2b423SJohn Johansen * Returns: kernel buffer containing copy of user buffer data or an 38463e2b423SJohn Johansen * ERR_PTR on failure. 38563e2b423SJohn Johansen */ 3865ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, 3875ac8c355SJohn Johansen size_t alloc_size, 3885ac8c355SJohn Johansen size_t copy_size, 38963e2b423SJohn Johansen loff_t *pos) 39063e2b423SJohn Johansen { 3915ac8c355SJohn Johansen struct aa_loaddata *data; 39263e2b423SJohn Johansen 393e6bfa25dSJohn Johansen AA_BUG(copy_size > alloc_size); 3943ed02adaSJohn Johansen 39563e2b423SJohn Johansen if (*pos != 0) 39663e2b423SJohn Johansen /* only writes from pos 0, that is complete writes */ 39763e2b423SJohn Johansen return ERR_PTR(-ESPIPE); 39863e2b423SJohn Johansen 39963e2b423SJohn Johansen /* freed by caller to simple_write_to_buffer */ 4005d5182caSJohn Johansen data = aa_loaddata_alloc(alloc_size); 4015d5182caSJohn Johansen if (IS_ERR(data)) 4025d5182caSJohn Johansen return data; 40363e2b423SJohn Johansen 4045d5182caSJohn Johansen data->size = copy_size; 4055ac8c355SJohn Johansen if (copy_from_user(data->data, userbuf, copy_size)) { 40663e2b423SJohn Johansen kvfree(data); 40763e2b423SJohn Johansen return ERR_PTR(-EFAULT); 40863e2b423SJohn Johansen } 40963e2b423SJohn Johansen 41063e2b423SJohn Johansen return data; 41163e2b423SJohn Johansen } 41263e2b423SJohn Johansen 41318e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size, 414b7fd2c03SJohn Johansen loff_t *pos, struct aa_ns *ns) 4155ac8c355SJohn Johansen { 4165ac8c355SJohn Johansen struct aa_loaddata *data; 417637f688dSJohn Johansen struct aa_label *label; 418637f688dSJohn Johansen ssize_t error; 419cf797c0eSJohn Johansen 420637f688dSJohn Johansen label = begin_current_label_crit_section(); 421cf797c0eSJohn Johansen 4225ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4235ac8c355SJohn Johansen * below after unpack 4245ac8c355SJohn Johansen */ 425637f688dSJohn Johansen error = aa_may_manage_policy(label, ns, mask); 4265ac8c355SJohn Johansen if (error) 427c6b39f07SXiyu Yang goto end_section; 42863e2b423SJohn Johansen 4295ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size, size, pos); 4305ac8c355SJohn Johansen error = PTR_ERR(data); 4315ac8c355SJohn Johansen if (!IS_ERR(data)) { 432637f688dSJohn Johansen error = aa_replace_profiles(ns, label, mask, data); 4335ac8c355SJohn Johansen aa_put_loaddata(data); 4345ac8c355SJohn Johansen } 435c6b39f07SXiyu Yang end_section: 436637f688dSJohn Johansen end_current_label_crit_section(label); 4375ac8c355SJohn Johansen 4385ac8c355SJohn Johansen return error; 4395ac8c355SJohn Johansen } 4405ac8c355SJohn Johansen 441b7fd2c03SJohn Johansen /* .load file hook fn to load policy */ 44263e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 44363e2b423SJohn Johansen loff_t *pos) 44463e2b423SJohn Johansen { 445b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 44618e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns); 447b7fd2c03SJohn Johansen 448b7fd2c03SJohn Johansen aa_put_ns(ns); 44963e2b423SJohn Johansen 45063e2b423SJohn Johansen return error; 45163e2b423SJohn Johansen } 45263e2b423SJohn Johansen 45363e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = { 4546038f373SArnd Bergmann .write = profile_load, 4556038f373SArnd Bergmann .llseek = default_llseek, 45663e2b423SJohn Johansen }; 45763e2b423SJohn Johansen 45863e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */ 45963e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf, 46063e2b423SJohn Johansen size_t size, loff_t *pos) 46163e2b423SJohn Johansen { 462b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 46318e99f19SJohn Johansen int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY, 46418e99f19SJohn Johansen buf, size, pos, ns); 465b7fd2c03SJohn Johansen aa_put_ns(ns); 46663e2b423SJohn Johansen 46763e2b423SJohn Johansen return error; 46863e2b423SJohn Johansen } 46963e2b423SJohn Johansen 47063e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = { 4716038f373SArnd Bergmann .write = profile_replace, 4726038f373SArnd Bergmann .llseek = default_llseek, 47363e2b423SJohn Johansen }; 47463e2b423SJohn Johansen 475b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */ 47663e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf, 47763e2b423SJohn Johansen size_t size, loff_t *pos) 47863e2b423SJohn Johansen { 4795ac8c355SJohn Johansen struct aa_loaddata *data; 480637f688dSJohn Johansen struct aa_label *label; 48163e2b423SJohn Johansen ssize_t error; 482b7fd2c03SJohn Johansen struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); 48363e2b423SJohn Johansen 484637f688dSJohn Johansen label = begin_current_label_crit_section(); 4855ac8c355SJohn Johansen /* high level check about policy management - fine grained in 4865ac8c355SJohn Johansen * below after unpack 4875ac8c355SJohn Johansen */ 488637f688dSJohn Johansen error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY); 4895ac8c355SJohn Johansen if (error) 4905ac8c355SJohn Johansen goto out; 4915ac8c355SJohn Johansen 49263e2b423SJohn Johansen /* 49363e2b423SJohn Johansen * aa_remove_profile needs a null terminated string so 1 extra 49463e2b423SJohn Johansen * byte is allocated and the copied data is null terminated. 49563e2b423SJohn Johansen */ 4965ef50d01SJohn Johansen data = aa_simple_write_to_buffer(buf, size + 1, size, pos); 49763e2b423SJohn Johansen 49863e2b423SJohn Johansen error = PTR_ERR(data); 49963e2b423SJohn Johansen if (!IS_ERR(data)) { 5005ac8c355SJohn Johansen data->data[size] = 0; 501637f688dSJohn Johansen error = aa_remove_profiles(ns, label, data->data, size); 5025ac8c355SJohn Johansen aa_put_loaddata(data); 50363e2b423SJohn Johansen } 5045ac8c355SJohn Johansen out: 505637f688dSJohn Johansen end_current_label_crit_section(label); 506b7fd2c03SJohn Johansen aa_put_ns(ns); 50763e2b423SJohn Johansen return error; 50863e2b423SJohn Johansen } 50963e2b423SJohn Johansen 51063e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = { 5116038f373SArnd Bergmann .write = profile_remove, 5126038f373SArnd Bergmann .llseek = default_llseek, 51363e2b423SJohn Johansen }; 51463e2b423SJohn Johansen 515d9bf2c26SJohn Johansen struct aa_revision { 516d9bf2c26SJohn Johansen struct aa_ns *ns; 517d9bf2c26SJohn Johansen long last_read; 518d9bf2c26SJohn Johansen }; 519d9bf2c26SJohn Johansen 520d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */ 521d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file) 522d9bf2c26SJohn Johansen { 523d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 524d9bf2c26SJohn Johansen 525d9bf2c26SJohn Johansen if (rev) { 526d9bf2c26SJohn Johansen aa_put_ns(rev->ns); 527d9bf2c26SJohn Johansen kfree(rev); 528d9bf2c26SJohn Johansen } 529d9bf2c26SJohn Johansen 530d9bf2c26SJohn Johansen return 0; 531d9bf2c26SJohn Johansen } 532d9bf2c26SJohn Johansen 533d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf, 534d9bf2c26SJohn Johansen size_t size, loff_t *ppos) 535d9bf2c26SJohn Johansen { 536d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 537d9bf2c26SJohn Johansen char buffer[32]; 538d9bf2c26SJohn Johansen long last_read; 539d9bf2c26SJohn Johansen int avail; 540d9bf2c26SJohn Johansen 541feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 542d9bf2c26SJohn Johansen last_read = rev->last_read; 543d9bf2c26SJohn Johansen if (last_read == rev->ns->revision) { 544d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 545d9bf2c26SJohn Johansen if (file->f_flags & O_NONBLOCK) 546d9bf2c26SJohn Johansen return -EAGAIN; 547d9bf2c26SJohn Johansen if (wait_event_interruptible(rev->ns->wait, 548d9bf2c26SJohn Johansen last_read != 549d9bf2c26SJohn Johansen READ_ONCE(rev->ns->revision))) 550d9bf2c26SJohn Johansen return -ERESTARTSYS; 551feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 552d9bf2c26SJohn Johansen } 553d9bf2c26SJohn Johansen 554d9bf2c26SJohn Johansen avail = sprintf(buffer, "%ld\n", rev->ns->revision); 555d9bf2c26SJohn Johansen if (*ppos + size > avail) { 556d9bf2c26SJohn Johansen rev->last_read = rev->ns->revision; 557d9bf2c26SJohn Johansen *ppos = 0; 558d9bf2c26SJohn Johansen } 559d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 560d9bf2c26SJohn Johansen 561d9bf2c26SJohn Johansen return simple_read_from_buffer(buf, size, ppos, buffer, avail); 562d9bf2c26SJohn Johansen } 563d9bf2c26SJohn Johansen 564d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file) 565d9bf2c26SJohn Johansen { 566d9bf2c26SJohn Johansen struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL); 567d9bf2c26SJohn Johansen 568d9bf2c26SJohn Johansen if (!rev) 569d9bf2c26SJohn Johansen return -ENOMEM; 570d9bf2c26SJohn Johansen 571d9bf2c26SJohn Johansen rev->ns = aa_get_ns(inode->i_private); 572d9bf2c26SJohn Johansen if (!rev->ns) 573d9bf2c26SJohn Johansen rev->ns = aa_get_current_ns(); 574d9bf2c26SJohn Johansen file->private_data = rev; 575d9bf2c26SJohn Johansen 576d9bf2c26SJohn Johansen return 0; 577d9bf2c26SJohn Johansen } 578d9bf2c26SJohn Johansen 579e6c5a7d9SAl Viro static __poll_t ns_revision_poll(struct file *file, poll_table *pt) 580d9bf2c26SJohn Johansen { 581d9bf2c26SJohn Johansen struct aa_revision *rev = file->private_data; 582e6c5a7d9SAl Viro __poll_t mask = 0; 583d9bf2c26SJohn Johansen 584d9bf2c26SJohn Johansen if (rev) { 585feb3c766SJohn Johansen mutex_lock_nested(&rev->ns->lock, rev->ns->level); 586d9bf2c26SJohn Johansen poll_wait(file, &rev->ns->wait, pt); 587d9bf2c26SJohn Johansen if (rev->last_read < rev->ns->revision) 588a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 589d9bf2c26SJohn Johansen mutex_unlock(&rev->ns->lock); 590d9bf2c26SJohn Johansen } 591d9bf2c26SJohn Johansen 592d9bf2c26SJohn Johansen return mask; 593d9bf2c26SJohn Johansen } 594d9bf2c26SJohn Johansen 5955d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns) 5965d5182caSJohn Johansen { 5970df34a64SJohn Johansen WRITE_ONCE(ns->revision, READ_ONCE(ns->revision) + 1); 598d9bf2c26SJohn Johansen wake_up_interruptible(&ns->wait); 5995d5182caSJohn Johansen } 6005d5182caSJohn Johansen 601d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = { 602d9bf2c26SJohn Johansen .owner = THIS_MODULE, 603d9bf2c26SJohn Johansen .open = ns_revision_open, 604d9bf2c26SJohn Johansen .poll = ns_revision_poll, 605d9bf2c26SJohn Johansen .read = ns_revision_read, 606d9bf2c26SJohn Johansen .llseek = generic_file_llseek, 607d9bf2c26SJohn Johansen .release = ns_revision_release, 608d9bf2c26SJohn Johansen }; 609d9bf2c26SJohn Johansen 6104f3b3f2dSJohn Johansen static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms, 6114f3b3f2dSJohn Johansen const char *match_str, size_t match_len) 6124f3b3f2dSJohn Johansen { 613f4585bc2STyler Hicks struct aa_perms tmp = { }; 6144f3b3f2dSJohn Johansen struct aa_dfa *dfa; 6154f3b3f2dSJohn Johansen unsigned int state = 0; 6164f3b3f2dSJohn Johansen 617637f688dSJohn Johansen if (profile_unconfined(profile)) 6184f3b3f2dSJohn Johansen return; 6194f3b3f2dSJohn Johansen if (profile->file.dfa && *match_str == AA_CLASS_FILE) { 6204f3b3f2dSJohn Johansen dfa = profile->file.dfa; 6214f3b3f2dSJohn Johansen state = aa_dfa_match_len(dfa, profile->file.start, 6224f3b3f2dSJohn Johansen match_str + 1, match_len - 1); 6234f3b3f2dSJohn Johansen if (state) { 6244f3b3f2dSJohn Johansen struct path_cond cond = { }; 6254f3b3f2dSJohn Johansen 6264f3b3f2dSJohn Johansen tmp = aa_compute_fperms(dfa, state, &cond); 6274f3b3f2dSJohn Johansen } 6284f3b3f2dSJohn Johansen } else if (profile->policy.dfa) { 629b9590ad4SJohn Johansen if (!PROFILE_MEDIATES(profile, *match_str)) 6304f3b3f2dSJohn Johansen return; /* no change to current perms */ 6314f3b3f2dSJohn Johansen dfa = profile->policy.dfa; 6324f3b3f2dSJohn Johansen state = aa_dfa_match_len(dfa, profile->policy.start[0], 6334f3b3f2dSJohn Johansen match_str, match_len); 6344f3b3f2dSJohn Johansen if (state) 6354f3b3f2dSJohn Johansen aa_compute_perms(dfa, state, &tmp); 6364f3b3f2dSJohn Johansen } 6374f3b3f2dSJohn Johansen aa_apply_modes_to_perms(profile, &tmp); 638317d9a05SJohn Johansen aa_perms_accum_raw(perms, &tmp); 6394f3b3f2dSJohn Johansen } 6404f3b3f2dSJohn Johansen 6414f3b3f2dSJohn Johansen 642e025be0fSWilliam Hua /** 643e025be0fSWilliam Hua * query_data - queries a policy and writes its data to buf 644e025be0fSWilliam Hua * @buf: the resulting data is stored here (NOT NULL) 645e025be0fSWilliam Hua * @buf_len: size of buf 646e025be0fSWilliam Hua * @query: query string used to retrieve data 647e025be0fSWilliam Hua * @query_len: size of query including second NUL byte 648e025be0fSWilliam Hua * 649e025be0fSWilliam Hua * The buffers pointed to by buf and query may overlap. The query buffer is 650e025be0fSWilliam Hua * parsed before buf is written to. 651e025be0fSWilliam Hua * 652e025be0fSWilliam Hua * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of 653e025be0fSWilliam Hua * the security confinement context and <KEY> is the name of the data to 654e025be0fSWilliam Hua * retrieve. <LABEL> and <KEY> must not be NUL-terminated. 655e025be0fSWilliam Hua * 656e025be0fSWilliam Hua * Don't expect the contents of buf to be preserved on failure. 657e025be0fSWilliam Hua * 658e025be0fSWilliam Hua * Returns: number of characters written to buf or -errno on failure 659e025be0fSWilliam Hua */ 660e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len, 661e025be0fSWilliam Hua char *query, size_t query_len) 662e025be0fSWilliam Hua { 663e025be0fSWilliam Hua char *out; 664e025be0fSWilliam Hua const char *key; 665317d9a05SJohn Johansen struct label_it i; 666637f688dSJohn Johansen struct aa_label *label, *curr; 667317d9a05SJohn Johansen struct aa_profile *profile; 668e025be0fSWilliam Hua struct aa_data *data; 669e025be0fSWilliam Hua u32 bytes, blocks; 670e025be0fSWilliam Hua __le32 outle32; 671e025be0fSWilliam Hua 672e025be0fSWilliam Hua if (!query_len) 673e025be0fSWilliam Hua return -EINVAL; /* need a query */ 674e025be0fSWilliam Hua 675e025be0fSWilliam Hua key = query + strnlen(query, query_len) + 1; 676e025be0fSWilliam Hua if (key + 1 >= query + query_len) 677e025be0fSWilliam Hua return -EINVAL; /* not enough space for a non-empty key */ 678e025be0fSWilliam Hua if (key + strnlen(key, query + query_len - key) >= query + query_len) 679e025be0fSWilliam Hua return -EINVAL; /* must end with NUL */ 680e025be0fSWilliam Hua 681e025be0fSWilliam Hua if (buf_len < sizeof(bytes) + sizeof(blocks)) 682e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 683e025be0fSWilliam Hua 684637f688dSJohn Johansen curr = begin_current_label_crit_section(); 685637f688dSJohn Johansen label = aa_label_parse(curr, query, GFP_KERNEL, false, false); 686637f688dSJohn Johansen end_current_label_crit_section(curr); 687637f688dSJohn Johansen if (IS_ERR(label)) 688637f688dSJohn Johansen return PTR_ERR(label); 689e025be0fSWilliam Hua 690e025be0fSWilliam Hua /* We are going to leave space for two numbers. The first is the total 691e025be0fSWilliam Hua * number of bytes we are writing after the first number. This is so 692e025be0fSWilliam Hua * users can read the full output without reallocation. 693e025be0fSWilliam Hua * 694e025be0fSWilliam Hua * The second number is the number of data blocks we're writing. An 695e025be0fSWilliam Hua * application might be confined by multiple policies having data in 696e025be0fSWilliam Hua * the same key. 697e025be0fSWilliam Hua */ 698e025be0fSWilliam Hua memset(buf, 0, sizeof(bytes) + sizeof(blocks)); 699e025be0fSWilliam Hua out = buf + sizeof(bytes) + sizeof(blocks); 700e025be0fSWilliam Hua 701e025be0fSWilliam Hua blocks = 0; 702317d9a05SJohn Johansen label_for_each_confined(i, label, profile) { 703317d9a05SJohn Johansen if (!profile->data) 704317d9a05SJohn Johansen continue; 705317d9a05SJohn Johansen 706317d9a05SJohn Johansen data = rhashtable_lookup_fast(profile->data, &key, 707317d9a05SJohn Johansen profile->data->p); 708e025be0fSWilliam Hua 709e025be0fSWilliam Hua if (data) { 710317d9a05SJohn Johansen if (out + sizeof(outle32) + data->size > buf + 711317d9a05SJohn Johansen buf_len) { 712637f688dSJohn Johansen aa_put_label(label); 713e025be0fSWilliam Hua return -EINVAL; /* not enough space */ 714637f688dSJohn Johansen } 715e025be0fSWilliam Hua outle32 = __cpu_to_le32(data->size); 716e025be0fSWilliam Hua memcpy(out, &outle32, sizeof(outle32)); 717e025be0fSWilliam Hua out += sizeof(outle32); 718e025be0fSWilliam Hua memcpy(out, data->data, data->size); 719e025be0fSWilliam Hua out += data->size; 720e025be0fSWilliam Hua blocks++; 721e025be0fSWilliam Hua } 722e025be0fSWilliam Hua } 723637f688dSJohn Johansen aa_put_label(label); 724e025be0fSWilliam Hua 725e025be0fSWilliam Hua outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); 726e025be0fSWilliam Hua memcpy(buf, &outle32, sizeof(outle32)); 727e025be0fSWilliam Hua outle32 = __cpu_to_le32(blocks); 728e025be0fSWilliam Hua memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32)); 729e025be0fSWilliam Hua 730e025be0fSWilliam Hua return out - buf; 731e025be0fSWilliam Hua } 732e025be0fSWilliam Hua 7334f3b3f2dSJohn Johansen /** 7344f3b3f2dSJohn Johansen * query_label - queries a label and writes permissions to buf 7354f3b3f2dSJohn Johansen * @buf: the resulting permissions string is stored here (NOT NULL) 7364f3b3f2dSJohn Johansen * @buf_len: size of buf 7374f3b3f2dSJohn Johansen * @query: binary query string to match against the dfa 7384f3b3f2dSJohn Johansen * @query_len: size of query 7394f3b3f2dSJohn Johansen * @view_only: only compute for querier's view 7404f3b3f2dSJohn Johansen * 7414f3b3f2dSJohn Johansen * The buffers pointed to by buf and query may overlap. The query buffer is 7424f3b3f2dSJohn Johansen * parsed before buf is written to. 7434f3b3f2dSJohn Johansen * 7444f3b3f2dSJohn Johansen * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is 7454f3b3f2dSJohn Johansen * the name of the label, in the current namespace, that is to be queried and 7464f3b3f2dSJohn Johansen * DFA_STRING is a binary string to match against the label(s)'s DFA. 7474f3b3f2dSJohn Johansen * 7484f3b3f2dSJohn Johansen * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters 7494f3b3f2dSJohn Johansen * but must *not* be NUL terminated. 7504f3b3f2dSJohn Johansen * 7514f3b3f2dSJohn Johansen * Returns: number of characters written to buf or -errno on failure 7524f3b3f2dSJohn Johansen */ 7534f3b3f2dSJohn Johansen static ssize_t query_label(char *buf, size_t buf_len, 7544f3b3f2dSJohn Johansen char *query, size_t query_len, bool view_only) 7554f3b3f2dSJohn Johansen { 756317d9a05SJohn Johansen struct aa_profile *profile; 757637f688dSJohn Johansen struct aa_label *label, *curr; 7584f3b3f2dSJohn Johansen char *label_name, *match_str; 7594f3b3f2dSJohn Johansen size_t label_name_len, match_len; 7604f3b3f2dSJohn Johansen struct aa_perms perms; 761317d9a05SJohn Johansen struct label_it i; 7624f3b3f2dSJohn Johansen 7634f3b3f2dSJohn Johansen if (!query_len) 7644f3b3f2dSJohn Johansen return -EINVAL; 7654f3b3f2dSJohn Johansen 7664f3b3f2dSJohn Johansen label_name = query; 7674f3b3f2dSJohn Johansen label_name_len = strnlen(query, query_len); 7684f3b3f2dSJohn Johansen if (!label_name_len || label_name_len == query_len) 7694f3b3f2dSJohn Johansen return -EINVAL; 7704f3b3f2dSJohn Johansen 7714f3b3f2dSJohn Johansen /** 7724f3b3f2dSJohn Johansen * The extra byte is to account for the null byte between the 7734f3b3f2dSJohn Johansen * profile name and dfa string. profile_name_len is greater 7744f3b3f2dSJohn Johansen * than zero and less than query_len, so a byte can be safely 7754f3b3f2dSJohn Johansen * added or subtracted. 7764f3b3f2dSJohn Johansen */ 7774f3b3f2dSJohn Johansen match_str = label_name + label_name_len + 1; 7784f3b3f2dSJohn Johansen match_len = query_len - label_name_len - 1; 7794f3b3f2dSJohn Johansen 780637f688dSJohn Johansen curr = begin_current_label_crit_section(); 781637f688dSJohn Johansen label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false); 782637f688dSJohn Johansen end_current_label_crit_section(curr); 783637f688dSJohn Johansen if (IS_ERR(label)) 784637f688dSJohn Johansen return PTR_ERR(label); 7854f3b3f2dSJohn Johansen 7864f3b3f2dSJohn Johansen perms = allperms; 787317d9a05SJohn Johansen if (view_only) { 788317d9a05SJohn Johansen label_for_each_in_ns(i, labels_ns(label), label, profile) { 789317d9a05SJohn Johansen profile_query_cb(profile, &perms, match_str, match_len); 790317d9a05SJohn Johansen } 791317d9a05SJohn Johansen } else { 792317d9a05SJohn Johansen label_for_each(i, label, profile) { 793317d9a05SJohn Johansen profile_query_cb(profile, &perms, match_str, match_len); 794317d9a05SJohn Johansen } 795317d9a05SJohn Johansen } 796317d9a05SJohn Johansen aa_put_label(label); 7974f3b3f2dSJohn Johansen 7984f3b3f2dSJohn Johansen return scnprintf(buf, buf_len, 7994f3b3f2dSJohn Johansen "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n", 8004f3b3f2dSJohn Johansen perms.allow, perms.deny, perms.audit, perms.quiet); 8014f3b3f2dSJohn Johansen } 8024f3b3f2dSJohn Johansen 8031dea3b41SJohn Johansen /* 8041dea3b41SJohn Johansen * Transaction based IO. 8051dea3b41SJohn Johansen * The file expects a write which triggers the transaction, and then 8061dea3b41SJohn Johansen * possibly a read(s) which collects the result - which is stored in a 8071dea3b41SJohn Johansen * file-local buffer. Once a new write is performed, a new set of results 8081dea3b41SJohn Johansen * are stored in the file-local buffer. 8091dea3b41SJohn Johansen */ 8101dea3b41SJohn Johansen struct multi_transaction { 8111dea3b41SJohn Johansen struct kref count; 8121dea3b41SJohn Johansen ssize_t size; 813fe9fd23eSGustavo A. R. Silva char data[]; 8141dea3b41SJohn Johansen }; 8151dea3b41SJohn Johansen 8161dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction)) 8171dea3b41SJohn Johansen 8181dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref) 8191dea3b41SJohn Johansen { 8201dea3b41SJohn Johansen struct multi_transaction *t; 8211dea3b41SJohn Johansen 8221dea3b41SJohn Johansen t = container_of(kref, struct multi_transaction, count); 8231dea3b41SJohn Johansen free_page((unsigned long) t); 8241dea3b41SJohn Johansen } 8251dea3b41SJohn Johansen 8261dea3b41SJohn Johansen static struct multi_transaction * 8271dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t) 8281dea3b41SJohn Johansen { 8291dea3b41SJohn Johansen if (t) 8301dea3b41SJohn Johansen kref_get(&(t->count)); 8311dea3b41SJohn Johansen 8321dea3b41SJohn Johansen return t; 8331dea3b41SJohn Johansen } 8341dea3b41SJohn Johansen 8351dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t) 8361dea3b41SJohn Johansen { 8371dea3b41SJohn Johansen if (t) 8381dea3b41SJohn Johansen kref_put(&(t->count), multi_transaction_kref); 8391dea3b41SJohn Johansen } 8401dea3b41SJohn Johansen 8411dea3b41SJohn Johansen /* does not increment @new's count */ 8421dea3b41SJohn Johansen static void multi_transaction_set(struct file *file, 8431dea3b41SJohn Johansen struct multi_transaction *new, size_t n) 8441dea3b41SJohn Johansen { 8451dea3b41SJohn Johansen struct multi_transaction *old; 8461dea3b41SJohn Johansen 8471dea3b41SJohn Johansen AA_BUG(n > MULTI_TRANSACTION_LIMIT); 8481dea3b41SJohn Johansen 8491dea3b41SJohn Johansen new->size = n; 850d0d845a7SHamza Mahfooz spin_lock(&file->f_lock); 8511dea3b41SJohn Johansen old = (struct multi_transaction *) file->private_data; 8521dea3b41SJohn Johansen file->private_data = new; 853d0d845a7SHamza Mahfooz spin_unlock(&file->f_lock); 8541dea3b41SJohn Johansen put_multi_transaction(old); 8551dea3b41SJohn Johansen } 8561dea3b41SJohn Johansen 8571dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file, 8581dea3b41SJohn Johansen const char __user *buf, 8591dea3b41SJohn Johansen size_t size) 8601dea3b41SJohn Johansen { 8611dea3b41SJohn Johansen struct multi_transaction *t; 8621dea3b41SJohn Johansen 8631dea3b41SJohn Johansen if (size > MULTI_TRANSACTION_LIMIT - 1) 8641dea3b41SJohn Johansen return ERR_PTR(-EFBIG); 8651dea3b41SJohn Johansen 8661dea3b41SJohn Johansen t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL); 8671dea3b41SJohn Johansen if (!t) 8681dea3b41SJohn Johansen return ERR_PTR(-ENOMEM); 8691dea3b41SJohn Johansen kref_init(&t->count); 8701dea3b41SJohn Johansen if (copy_from_user(t->data, buf, size)) 8711dea3b41SJohn Johansen return ERR_PTR(-EFAULT); 8721dea3b41SJohn Johansen 8731dea3b41SJohn Johansen return t; 8741dea3b41SJohn Johansen } 8751dea3b41SJohn Johansen 8761dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf, 8771dea3b41SJohn Johansen size_t size, loff_t *pos) 8781dea3b41SJohn Johansen { 8791dea3b41SJohn Johansen struct multi_transaction *t; 8801dea3b41SJohn Johansen ssize_t ret; 8811dea3b41SJohn Johansen 882d0d845a7SHamza Mahfooz spin_lock(&file->f_lock); 8831dea3b41SJohn Johansen t = get_multi_transaction(file->private_data); 884d0d845a7SHamza Mahfooz spin_unlock(&file->f_lock); 885d0d845a7SHamza Mahfooz 8861dea3b41SJohn Johansen if (!t) 8871dea3b41SJohn Johansen return 0; 8881dea3b41SJohn Johansen 8891dea3b41SJohn Johansen ret = simple_read_from_buffer(buf, size, pos, t->data, t->size); 8901dea3b41SJohn Johansen put_multi_transaction(t); 8911dea3b41SJohn Johansen 8921dea3b41SJohn Johansen return ret; 8931dea3b41SJohn Johansen } 8941dea3b41SJohn Johansen 8951dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file) 8961dea3b41SJohn Johansen { 8971dea3b41SJohn Johansen put_multi_transaction(file->private_data); 8981dea3b41SJohn Johansen 8991dea3b41SJohn Johansen return 0; 9001dea3b41SJohn Johansen } 9011dea3b41SJohn Johansen 902317d9a05SJohn Johansen #define QUERY_CMD_LABEL "label\0" 903317d9a05SJohn Johansen #define QUERY_CMD_LABEL_LEN 6 9044f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE "profile\0" 9054f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE_LEN 8 906317d9a05SJohn Johansen #define QUERY_CMD_LABELALL "labelall\0" 907317d9a05SJohn Johansen #define QUERY_CMD_LABELALL_LEN 9 908e025be0fSWilliam Hua #define QUERY_CMD_DATA "data\0" 909e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN 5 910e025be0fSWilliam Hua 911e025be0fSWilliam Hua /** 912e025be0fSWilliam Hua * aa_write_access - generic permissions and data query 913e025be0fSWilliam Hua * @file: pointer to open apparmorfs/access file 914e025be0fSWilliam Hua * @ubuf: user buffer containing the complete query string (NOT NULL) 915e025be0fSWilliam Hua * @count: size of ubuf 916e025be0fSWilliam Hua * @ppos: position in the file (MUST BE ZERO) 917e025be0fSWilliam Hua * 918e025be0fSWilliam Hua * Allows for one permissions or data query per open(), write(), and read() 919e025be0fSWilliam Hua * sequence. The only queries currently supported are label-based queries for 920e025be0fSWilliam Hua * permissions or data. 921e025be0fSWilliam Hua * 922e025be0fSWilliam Hua * For permissions queries, ubuf must begin with "label\0", followed by the 923e025be0fSWilliam Hua * profile query specific format described in the query_label() function 924e025be0fSWilliam Hua * documentation. 925e025be0fSWilliam Hua * 926e025be0fSWilliam Hua * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where 927e025be0fSWilliam Hua * <LABEL> is the name of the security confinement context and <KEY> is the 928e025be0fSWilliam Hua * name of the data to retrieve. 929e025be0fSWilliam Hua * 930e025be0fSWilliam Hua * Returns: number of bytes written or -errno on failure 931e025be0fSWilliam Hua */ 932e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf, 933e025be0fSWilliam Hua size_t count, loff_t *ppos) 934e025be0fSWilliam Hua { 9351dea3b41SJohn Johansen struct multi_transaction *t; 936e025be0fSWilliam Hua ssize_t len; 937e025be0fSWilliam Hua 938e025be0fSWilliam Hua if (*ppos) 939e025be0fSWilliam Hua return -ESPIPE; 940e025be0fSWilliam Hua 9411dea3b41SJohn Johansen t = multi_transaction_new(file, ubuf, count); 9421dea3b41SJohn Johansen if (IS_ERR(t)) 9431dea3b41SJohn Johansen return PTR_ERR(t); 944e025be0fSWilliam Hua 9454f3b3f2dSJohn Johansen if (count > QUERY_CMD_PROFILE_LEN && 9464f3b3f2dSJohn Johansen !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) { 9474f3b3f2dSJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 9484f3b3f2dSJohn Johansen t->data + QUERY_CMD_PROFILE_LEN, 9494f3b3f2dSJohn Johansen count - QUERY_CMD_PROFILE_LEN, true); 950317d9a05SJohn Johansen } else if (count > QUERY_CMD_LABEL_LEN && 951317d9a05SJohn Johansen !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) { 952317d9a05SJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 953317d9a05SJohn Johansen t->data + QUERY_CMD_LABEL_LEN, 954317d9a05SJohn Johansen count - QUERY_CMD_LABEL_LEN, true); 955317d9a05SJohn Johansen } else if (count > QUERY_CMD_LABELALL_LEN && 956317d9a05SJohn Johansen !memcmp(t->data, QUERY_CMD_LABELALL, 957317d9a05SJohn Johansen QUERY_CMD_LABELALL_LEN)) { 958317d9a05SJohn Johansen len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 959317d9a05SJohn Johansen t->data + QUERY_CMD_LABELALL_LEN, 960317d9a05SJohn Johansen count - QUERY_CMD_LABELALL_LEN, false); 9614f3b3f2dSJohn Johansen } else if (count > QUERY_CMD_DATA_LEN && 9621dea3b41SJohn Johansen !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { 9631dea3b41SJohn Johansen len = query_data(t->data, MULTI_TRANSACTION_LIMIT, 9641dea3b41SJohn Johansen t->data + QUERY_CMD_DATA_LEN, 965e025be0fSWilliam Hua count - QUERY_CMD_DATA_LEN); 966e025be0fSWilliam Hua } else 967e025be0fSWilliam Hua len = -EINVAL; 968e025be0fSWilliam Hua 9691dea3b41SJohn Johansen if (len < 0) { 9701dea3b41SJohn Johansen put_multi_transaction(t); 971e025be0fSWilliam Hua return len; 9721dea3b41SJohn Johansen } 973e025be0fSWilliam Hua 9741dea3b41SJohn Johansen multi_transaction_set(file, t, len); 975e025be0fSWilliam Hua 976e025be0fSWilliam Hua return count; 977e025be0fSWilliam Hua } 978e025be0fSWilliam Hua 979c97204baSJohn Johansen static const struct file_operations aa_sfs_access = { 980e025be0fSWilliam Hua .write = aa_write_access, 9811dea3b41SJohn Johansen .read = multi_transaction_read, 9821dea3b41SJohn Johansen .release = multi_transaction_release, 983e025be0fSWilliam Hua .llseek = generic_file_llseek, 984e025be0fSWilliam Hua }; 985e025be0fSWilliam Hua 986c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v) 987e74abcf3SKees Cook { 988c97204baSJohn Johansen struct aa_sfs_entry *fs_file = seq->private; 989e74abcf3SKees Cook 990e74abcf3SKees Cook if (!fs_file) 991e74abcf3SKees Cook return 0; 992e74abcf3SKees Cook 993e74abcf3SKees Cook switch (fs_file->v_type) { 994c97204baSJohn Johansen case AA_SFS_TYPE_BOOLEAN: 995e74abcf3SKees Cook seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); 996e74abcf3SKees Cook break; 997c97204baSJohn Johansen case AA_SFS_TYPE_STRING: 998a9bf8e9fSKees Cook seq_printf(seq, "%s\n", fs_file->v.string); 999a9bf8e9fSKees Cook break; 1000c97204baSJohn Johansen case AA_SFS_TYPE_U64: 1001e74abcf3SKees Cook seq_printf(seq, "%#08lx\n", fs_file->v.u64); 1002e74abcf3SKees Cook break; 1003e74abcf3SKees Cook default: 1004e74abcf3SKees Cook /* Ignore unpritable entry types. */ 1005e74abcf3SKees Cook break; 1006e74abcf3SKees Cook } 1007e74abcf3SKees Cook 1008e74abcf3SKees Cook return 0; 1009e74abcf3SKees Cook } 1010e74abcf3SKees Cook 1011c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file) 1012e74abcf3SKees Cook { 1013c97204baSJohn Johansen return single_open(file, aa_sfs_seq_show, inode->i_private); 1014e74abcf3SKees Cook } 1015e74abcf3SKees Cook 1016c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = { 1017e74abcf3SKees Cook .owner = THIS_MODULE, 1018c97204baSJohn Johansen .open = aa_sfs_seq_open, 1019e74abcf3SKees Cook .read = seq_read, 1020e74abcf3SKees Cook .llseek = seq_lseek, 1021e74abcf3SKees Cook .release = single_release, 1022e74abcf3SKees Cook }; 1023e74abcf3SKees Cook 102452b97de3SJohn Johansen /* 102552b97de3SJohn Johansen * profile based file operations 102652b97de3SJohn Johansen * policy/profiles/XXXX/profiles/ * 102752b97de3SJohn Johansen */ 102852b97de3SJohn Johansen 102952b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME) \ 103052b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\ 103152b97de3SJohn Johansen { \ 103252b97de3SJohn Johansen return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \ 103352b97de3SJohn Johansen } \ 103452b97de3SJohn Johansen \ 103552b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = { \ 103652b97de3SJohn Johansen .owner = THIS_MODULE, \ 103752b97de3SJohn Johansen .open = seq_profile_ ##NAME ##_open, \ 103852b97de3SJohn Johansen .read = seq_read, \ 103952b97de3SJohn Johansen .llseek = seq_lseek, \ 104052b97de3SJohn Johansen .release = seq_profile_release, \ 104152b97de3SJohn Johansen } \ 104252b97de3SJohn Johansen 104352b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file, 10440d259f04SJohn Johansen int (*show)(struct seq_file *, void *)) 10450d259f04SJohn Johansen { 10468399588aSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(inode->i_private); 10478399588aSJohn Johansen int error = single_open(file, show, proxy); 104863e2b423SJohn Johansen 10490d259f04SJohn Johansen if (error) { 10500d259f04SJohn Johansen file->private_data = NULL; 10518399588aSJohn Johansen aa_put_proxy(proxy); 10520d259f04SJohn Johansen } 10530d259f04SJohn Johansen 10540d259f04SJohn Johansen return error; 10550d259f04SJohn Johansen } 10560d259f04SJohn Johansen 105752b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file) 10580d259f04SJohn Johansen { 10590d259f04SJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 10600d259f04SJohn Johansen if (seq) 10618399588aSJohn Johansen aa_put_proxy(seq->private); 10620d259f04SJohn Johansen return single_release(inode, file); 10630d259f04SJohn Johansen } 10640d259f04SJohn Johansen 106552b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v) 10660d259f04SJohn Johansen { 10678399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1068637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1069637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 10700d259f04SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 1071637f688dSJohn Johansen aa_put_label(label); 10720d259f04SJohn Johansen 10730d259f04SJohn Johansen return 0; 10740d259f04SJohn Johansen } 10750d259f04SJohn Johansen 107652b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v) 10770d259f04SJohn Johansen { 10788399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1079637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1080637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 10810d259f04SJohn Johansen seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); 1082637f688dSJohn Johansen aa_put_label(label); 10830d259f04SJohn Johansen 10840d259f04SJohn Johansen return 0; 10850d259f04SJohn Johansen } 10860d259f04SJohn Johansen 108752b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v) 1088556d0be7SJohn Johansen { 10898399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1090637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1091637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 1092556d0be7SJohn Johansen if (profile->attach) 1093556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->attach); 1094556d0be7SJohn Johansen else if (profile->xmatch) 1095556d0be7SJohn Johansen seq_puts(seq, "<unknown>\n"); 1096556d0be7SJohn Johansen else 1097556d0be7SJohn Johansen seq_printf(seq, "%s\n", profile->base.name); 1098637f688dSJohn Johansen aa_put_label(label); 1099556d0be7SJohn Johansen 1100556d0be7SJohn Johansen return 0; 1101556d0be7SJohn Johansen } 1102556d0be7SJohn Johansen 110352b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v) 1104f8eb8a13SJohn Johansen { 11058399588aSJohn Johansen struct aa_proxy *proxy = seq->private; 1106637f688dSJohn Johansen struct aa_label *label = aa_get_label_rcu(&proxy->label); 1107637f688dSJohn Johansen struct aa_profile *profile = labels_profile(label); 1108f8eb8a13SJohn Johansen unsigned int i, size = aa_hash_size(); 1109f8eb8a13SJohn Johansen 1110f8eb8a13SJohn Johansen if (profile->hash) { 1111f8eb8a13SJohn Johansen for (i = 0; i < size; i++) 1112f8eb8a13SJohn Johansen seq_printf(seq, "%.2x", profile->hash[i]); 111347dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 1114f8eb8a13SJohn Johansen } 1115637f688dSJohn Johansen aa_put_label(label); 1116f8eb8a13SJohn Johansen 1117f8eb8a13SJohn Johansen return 0; 1118f8eb8a13SJohn Johansen } 1119f8eb8a13SJohn Johansen 112052b97de3SJohn Johansen SEQ_PROFILE_FOPS(name); 112152b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode); 112252b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach); 112352b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash); 1124f8eb8a13SJohn Johansen 112564c86970SJohn Johansen /* 112664c86970SJohn Johansen * namespace based files 112764c86970SJohn Johansen * several root files and 112864c86970SJohn Johansen * policy/ * 112964c86970SJohn Johansen */ 1130f8eb8a13SJohn Johansen 113164c86970SJohn Johansen #define SEQ_NS_FOPS(NAME) \ 113264c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \ 113364c86970SJohn Johansen { \ 113464c86970SJohn Johansen return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \ 113564c86970SJohn Johansen } \ 113664c86970SJohn Johansen \ 113764c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = { \ 113864c86970SJohn Johansen .owner = THIS_MODULE, \ 113964c86970SJohn Johansen .open = seq_ns_ ##NAME ##_open, \ 114064c86970SJohn Johansen .read = seq_read, \ 114164c86970SJohn Johansen .llseek = seq_lseek, \ 114264c86970SJohn Johansen .release = single_release, \ 114364c86970SJohn Johansen } \ 11443e3e5695SJohn Johansen 114540cde7fcSJohn Johansen static int seq_ns_stacked_show(struct seq_file *seq, void *v) 114640cde7fcSJohn Johansen { 114740cde7fcSJohn Johansen struct aa_label *label; 114840cde7fcSJohn Johansen 114940cde7fcSJohn Johansen label = begin_current_label_crit_section(); 115040cde7fcSJohn Johansen seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no"); 115140cde7fcSJohn Johansen end_current_label_crit_section(label); 115240cde7fcSJohn Johansen 115340cde7fcSJohn Johansen return 0; 115440cde7fcSJohn Johansen } 115540cde7fcSJohn Johansen 115640cde7fcSJohn Johansen static int seq_ns_nsstacked_show(struct seq_file *seq, void *v) 115740cde7fcSJohn Johansen { 115840cde7fcSJohn Johansen struct aa_label *label; 115940cde7fcSJohn Johansen struct aa_profile *profile; 116040cde7fcSJohn Johansen struct label_it it; 116140cde7fcSJohn Johansen int count = 1; 116240cde7fcSJohn Johansen 116340cde7fcSJohn Johansen label = begin_current_label_crit_section(); 116440cde7fcSJohn Johansen 116540cde7fcSJohn Johansen if (label->size > 1) { 116640cde7fcSJohn Johansen label_for_each(it, label, profile) 116740cde7fcSJohn Johansen if (profile->ns != labels_ns(label)) { 116840cde7fcSJohn Johansen count++; 116940cde7fcSJohn Johansen break; 117040cde7fcSJohn Johansen } 117140cde7fcSJohn Johansen } 117240cde7fcSJohn Johansen 117340cde7fcSJohn Johansen seq_printf(seq, "%s\n", count > 1 ? "yes" : "no"); 117440cde7fcSJohn Johansen end_current_label_crit_section(label); 117540cde7fcSJohn Johansen 117640cde7fcSJohn Johansen return 0; 117740cde7fcSJohn Johansen } 117840cde7fcSJohn Johansen 117964c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v) 1180a71ada30SJohn Johansen { 1181637f688dSJohn Johansen struct aa_label *label; 1182a71ada30SJohn Johansen 1183637f688dSJohn Johansen label = begin_current_label_crit_section(); 1184637f688dSJohn Johansen seq_printf(seq, "%d\n", labels_ns(label)->level); 1185637f688dSJohn Johansen end_current_label_crit_section(label); 1186a71ada30SJohn Johansen 1187a71ada30SJohn Johansen return 0; 1188a71ada30SJohn Johansen } 1189a71ada30SJohn Johansen 119064c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v) 11913e3e5695SJohn Johansen { 1192637f688dSJohn Johansen struct aa_label *label = begin_current_label_crit_section(); 1193040d9e2bSJohn Johansen seq_printf(seq, "%s\n", labels_ns(label)->base.name); 1194637f688dSJohn Johansen end_current_label_crit_section(label); 11953e3e5695SJohn Johansen 11963e3e5695SJohn Johansen return 0; 11973e3e5695SJohn Johansen } 11983e3e5695SJohn Johansen 119940cde7fcSJohn Johansen SEQ_NS_FOPS(stacked); 120040cde7fcSJohn Johansen SEQ_NS_FOPS(nsstacked); 120164c86970SJohn Johansen SEQ_NS_FOPS(level); 120264c86970SJohn Johansen SEQ_NS_FOPS(name); 12033e3e5695SJohn Johansen 12045d5182caSJohn Johansen 12055d5182caSJohn Johansen /* policy/raw_data/ * file ops */ 1206d61c57fdSJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 12075d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME) \ 12085d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\ 12095d5182caSJohn Johansen { \ 12105d5182caSJohn Johansen return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \ 12115d5182caSJohn Johansen } \ 12125d5182caSJohn Johansen \ 12135d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \ 12145d5182caSJohn Johansen .owner = THIS_MODULE, \ 12155d5182caSJohn Johansen .open = seq_rawdata_ ##NAME ##_open, \ 12165d5182caSJohn Johansen .read = seq_read, \ 12175d5182caSJohn Johansen .llseek = seq_lseek, \ 12185d5182caSJohn Johansen .release = seq_rawdata_release, \ 12195d5182caSJohn Johansen } \ 12205d5182caSJohn Johansen 12215d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file, 12225d5182caSJohn Johansen int (*show)(struct seq_file *, void *)) 12235ac8c355SJohn Johansen { 12245d5182caSJohn Johansen struct aa_loaddata *data = __aa_get_loaddata(inode->i_private); 12255d5182caSJohn Johansen int error; 12265d5182caSJohn Johansen 12275d5182caSJohn Johansen if (!data) 12285d5182caSJohn Johansen /* lost race this ent is being reaped */ 12295d5182caSJohn Johansen return -ENOENT; 12305d5182caSJohn Johansen 12315d5182caSJohn Johansen error = single_open(file, show, data); 12325d5182caSJohn Johansen if (error) { 12335d5182caSJohn Johansen AA_BUG(file->private_data && 12345d5182caSJohn Johansen ((struct seq_file *)file->private_data)->private); 12355d5182caSJohn Johansen aa_put_loaddata(data); 12365d5182caSJohn Johansen } 12375d5182caSJohn Johansen 12385d5182caSJohn Johansen return error; 12395d5182caSJohn Johansen } 12405d5182caSJohn Johansen 12415d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file) 12425d5182caSJohn Johansen { 12435d5182caSJohn Johansen struct seq_file *seq = (struct seq_file *) file->private_data; 12445d5182caSJohn Johansen 12455d5182caSJohn Johansen if (seq) 12465d5182caSJohn Johansen aa_put_loaddata(seq->private); 12475d5182caSJohn Johansen 12485d5182caSJohn Johansen return single_release(inode, file); 12495d5182caSJohn Johansen } 12505d5182caSJohn Johansen 12515d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v) 12525d5182caSJohn Johansen { 12535d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12545d5182caSJohn Johansen 12555d5182caSJohn Johansen seq_printf(seq, "v%d\n", data->abi); 12565ac8c355SJohn Johansen 12575ac8c355SJohn Johansen return 0; 12585ac8c355SJohn Johansen } 12595ac8c355SJohn Johansen 12605d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v) 12615ac8c355SJohn Johansen { 12625d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12635ac8c355SJohn Johansen 12645d5182caSJohn Johansen seq_printf(seq, "%ld\n", data->revision); 12655ac8c355SJohn Johansen 12665ac8c355SJohn Johansen return 0; 12675ac8c355SJohn Johansen } 12685ac8c355SJohn Johansen 12695d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v) 12705ac8c355SJohn Johansen { 12715d5182caSJohn Johansen struct aa_loaddata *data = seq->private; 12725ac8c355SJohn Johansen unsigned int i, size = aa_hash_size(); 12735ac8c355SJohn Johansen 12745d5182caSJohn Johansen if (data->hash) { 12755ac8c355SJohn Johansen for (i = 0; i < size; i++) 12765d5182caSJohn Johansen seq_printf(seq, "%.2x", data->hash[i]); 127747dbd1cdSMarkus Elfring seq_putc(seq, '\n'); 12785ac8c355SJohn Johansen } 12795ac8c355SJohn Johansen 12805ac8c355SJohn Johansen return 0; 12815ac8c355SJohn Johansen } 12825ac8c355SJohn Johansen 128363c16c3aSChris Coulson static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v) 128463c16c3aSChris Coulson { 128563c16c3aSChris Coulson struct aa_loaddata *data = seq->private; 128663c16c3aSChris Coulson 128763c16c3aSChris Coulson seq_printf(seq, "%zu\n", data->compressed_size); 128863c16c3aSChris Coulson 128963c16c3aSChris Coulson return 0; 129063c16c3aSChris Coulson } 129163c16c3aSChris Coulson 12925d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi); 12935d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision); 12945d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash); 129563c16c3aSChris Coulson SEQ_RAWDATA_FOPS(compressed_size); 129663c16c3aSChris Coulson 129763c16c3aSChris Coulson static int deflate_decompress(char *src, size_t slen, char *dst, size_t dlen) 129863c16c3aSChris Coulson { 129963c16c3aSChris Coulson int error; 130063c16c3aSChris Coulson struct z_stream_s strm; 130163c16c3aSChris Coulson 130263c16c3aSChris Coulson if (aa_g_rawdata_compression_level == 0) { 130363c16c3aSChris Coulson if (dlen < slen) 130463c16c3aSChris Coulson return -EINVAL; 130563c16c3aSChris Coulson memcpy(dst, src, slen); 130663c16c3aSChris Coulson return 0; 130763c16c3aSChris Coulson } 130863c16c3aSChris Coulson 130963c16c3aSChris Coulson memset(&strm, 0, sizeof(strm)); 131063c16c3aSChris Coulson 131163c16c3aSChris Coulson strm.workspace = kvzalloc(zlib_inflate_workspacesize(), GFP_KERNEL); 131263c16c3aSChris Coulson if (!strm.workspace) 131363c16c3aSChris Coulson return -ENOMEM; 131463c16c3aSChris Coulson 131563c16c3aSChris Coulson strm.next_in = src; 131663c16c3aSChris Coulson strm.avail_in = slen; 131763c16c3aSChris Coulson 131863c16c3aSChris Coulson error = zlib_inflateInit(&strm); 131963c16c3aSChris Coulson if (error != Z_OK) { 132063c16c3aSChris Coulson error = -ENOMEM; 132163c16c3aSChris Coulson goto fail_inflate_init; 132263c16c3aSChris Coulson } 132363c16c3aSChris Coulson 132463c16c3aSChris Coulson strm.next_out = dst; 132563c16c3aSChris Coulson strm.avail_out = dlen; 132663c16c3aSChris Coulson 132763c16c3aSChris Coulson error = zlib_inflate(&strm, Z_FINISH); 132863c16c3aSChris Coulson if (error != Z_STREAM_END) 132963c16c3aSChris Coulson error = -EINVAL; 133063c16c3aSChris Coulson else 133163c16c3aSChris Coulson error = 0; 133263c16c3aSChris Coulson 133363c16c3aSChris Coulson zlib_inflateEnd(&strm); 133463c16c3aSChris Coulson fail_inflate_init: 133563c16c3aSChris Coulson kvfree(strm.workspace); 133663c16c3aSChris Coulson return error; 133763c16c3aSChris Coulson } 13385ac8c355SJohn Johansen 13395ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, 13405ac8c355SJohn Johansen loff_t *ppos) 13415ac8c355SJohn Johansen { 134263c16c3aSChris Coulson struct rawdata_f_data *private = file->private_data; 13435ac8c355SJohn Johansen 134463c16c3aSChris Coulson return simple_read_from_buffer(buf, size, ppos, 134563c16c3aSChris Coulson RAWDATA_F_DATA_BUF(private), 134663c16c3aSChris Coulson private->loaddata->size); 13475ac8c355SJohn Johansen } 13485ac8c355SJohn Johansen 13495d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file) 13505ac8c355SJohn Johansen { 135163c16c3aSChris Coulson rawdata_f_data_free(file->private_data); 13525ac8c355SJohn Johansen 13535ac8c355SJohn Johansen return 0; 13545ac8c355SJohn Johansen } 13555ac8c355SJohn Johansen 13565d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file) 13575d5182caSJohn Johansen { 135863c16c3aSChris Coulson int error; 135963c16c3aSChris Coulson struct aa_loaddata *loaddata; 136063c16c3aSChris Coulson struct rawdata_f_data *private; 136163c16c3aSChris Coulson 136292de220aSJohn Johansen if (!aa_current_policy_view_capable(NULL)) 13635d5182caSJohn Johansen return -EACCES; 136463c16c3aSChris Coulson 136563c16c3aSChris Coulson loaddata = __aa_get_loaddata(inode->i_private); 136663c16c3aSChris Coulson if (!loaddata) 13675d5182caSJohn Johansen /* lost race: this entry is being reaped */ 13685d5182caSJohn Johansen return -ENOENT; 13695d5182caSJohn Johansen 137063c16c3aSChris Coulson private = rawdata_f_data_alloc(loaddata->size); 137163c16c3aSChris Coulson if (IS_ERR(private)) { 137263c16c3aSChris Coulson error = PTR_ERR(private); 137363c16c3aSChris Coulson goto fail_private_alloc; 137463c16c3aSChris Coulson } 137563c16c3aSChris Coulson 137663c16c3aSChris Coulson private->loaddata = loaddata; 137763c16c3aSChris Coulson 137863c16c3aSChris Coulson error = deflate_decompress(loaddata->data, loaddata->compressed_size, 137963c16c3aSChris Coulson RAWDATA_F_DATA_BUF(private), 138063c16c3aSChris Coulson loaddata->size); 138163c16c3aSChris Coulson if (error) 138263c16c3aSChris Coulson goto fail_decompress; 138363c16c3aSChris Coulson 138463c16c3aSChris Coulson file->private_data = private; 13855d5182caSJohn Johansen return 0; 138663c16c3aSChris Coulson 138763c16c3aSChris Coulson fail_decompress: 138863c16c3aSChris Coulson rawdata_f_data_free(private); 138963c16c3aSChris Coulson return error; 139063c16c3aSChris Coulson 139163c16c3aSChris Coulson fail_private_alloc: 139263c16c3aSChris Coulson aa_put_loaddata(loaddata); 139363c16c3aSChris Coulson return error; 13945d5182caSJohn Johansen } 13955d5182caSJohn Johansen 13965d5182caSJohn Johansen static const struct file_operations rawdata_fops = { 13975ac8c355SJohn Johansen .open = rawdata_open, 13985ac8c355SJohn Johansen .read = rawdata_read, 13995ac8c355SJohn Johansen .llseek = generic_file_llseek, 14005ac8c355SJohn Johansen .release = rawdata_release, 14015ac8c355SJohn Johansen }; 14025ac8c355SJohn Johansen 14035d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata) 14045d5182caSJohn Johansen { 14055d5182caSJohn Johansen int i; 14065d5182caSJohn Johansen 14075d5182caSJohn Johansen for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) { 14085d5182caSJohn Johansen if (!IS_ERR_OR_NULL(rawdata->dents[i])) { 14095d5182caSJohn Johansen /* no refcounts on i_private */ 1410c961ee5fSJohn Johansen aafs_remove(rawdata->dents[i]); 14115d5182caSJohn Johansen rawdata->dents[i] = NULL; 14125d5182caSJohn Johansen } 14135d5182caSJohn Johansen } 14145d5182caSJohn Johansen } 14155d5182caSJohn Johansen 14165d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata) 14175d5182caSJohn Johansen { 14185d5182caSJohn Johansen AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock)); 14195d5182caSJohn Johansen 14205d5182caSJohn Johansen if (rawdata->ns) { 14215d5182caSJohn Johansen remove_rawdata_dents(rawdata); 14225d5182caSJohn Johansen list_del_init(&rawdata->list); 14235d5182caSJohn Johansen aa_put_ns(rawdata->ns); 14245d5182caSJohn Johansen rawdata->ns = NULL; 14255d5182caSJohn Johansen } 14265d5182caSJohn Johansen } 14275d5182caSJohn Johansen 14285d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata) 14295d5182caSJohn Johansen { 14305d5182caSJohn Johansen struct dentry *dent, *dir; 14315d5182caSJohn Johansen 14325d5182caSJohn Johansen AA_BUG(!ns); 14335d5182caSJohn Johansen AA_BUG(!rawdata); 14345d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 14355d5182caSJohn Johansen AA_BUG(!ns_subdata_dir(ns)); 14365d5182caSJohn Johansen 14375d5182caSJohn Johansen /* 14385d5182caSJohn Johansen * just use ns revision dir was originally created at. This is 14395d5182caSJohn Johansen * under ns->lock and if load is successful revision will be 14405d5182caSJohn Johansen * bumped and is guaranteed to be unique 14415d5182caSJohn Johansen */ 14425d5182caSJohn Johansen rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision); 14435d5182caSJohn Johansen if (!rawdata->name) 14445d5182caSJohn Johansen return -ENOMEM; 14455d5182caSJohn Johansen 1446c961ee5fSJohn Johansen dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns)); 14475d5182caSJohn Johansen if (IS_ERR(dir)) 14485d5182caSJohn Johansen /* ->name freed when rawdata freed */ 14495d5182caSJohn Johansen return PTR_ERR(dir); 14505d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DIR] = dir; 14515d5182caSJohn Johansen 1452c961ee5fSJohn Johansen dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata, 14535d5182caSJohn Johansen &seq_rawdata_abi_fops); 14545d5182caSJohn Johansen if (IS_ERR(dent)) 14555d5182caSJohn Johansen goto fail; 14565d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_ABI] = dent; 14575d5182caSJohn Johansen 1458c961ee5fSJohn Johansen dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata, 14595d5182caSJohn Johansen &seq_rawdata_revision_fops); 14605d5182caSJohn Johansen if (IS_ERR(dent)) 14615d5182caSJohn Johansen goto fail; 14625d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_REVISION] = dent; 14635d5182caSJohn Johansen 14645d5182caSJohn Johansen if (aa_g_hash_policy) { 1465c961ee5fSJohn Johansen dent = aafs_create_file("sha1", S_IFREG | 0444, dir, 14665d5182caSJohn Johansen rawdata, &seq_rawdata_hash_fops); 14675d5182caSJohn Johansen if (IS_ERR(dent)) 14685d5182caSJohn Johansen goto fail; 14695d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_HASH] = dent; 14705d5182caSJohn Johansen } 14715d5182caSJohn Johansen 147263c16c3aSChris Coulson dent = aafs_create_file("compressed_size", S_IFREG | 0444, dir, 147363c16c3aSChris Coulson rawdata, 147463c16c3aSChris Coulson &seq_rawdata_compressed_size_fops); 147563c16c3aSChris Coulson if (IS_ERR(dent)) 147663c16c3aSChris Coulson goto fail; 147763c16c3aSChris Coulson rawdata->dents[AAFS_LOADDATA_COMPRESSED_SIZE] = dent; 147863c16c3aSChris Coulson 1479c961ee5fSJohn Johansen dent = aafs_create_file("raw_data", S_IFREG | 0444, 14805d5182caSJohn Johansen dir, rawdata, &rawdata_fops); 14815d5182caSJohn Johansen if (IS_ERR(dent)) 14825d5182caSJohn Johansen goto fail; 14835d5182caSJohn Johansen rawdata->dents[AAFS_LOADDATA_DATA] = dent; 14845d5182caSJohn Johansen d_inode(dent)->i_size = rawdata->size; 14855d5182caSJohn Johansen 14865d5182caSJohn Johansen rawdata->ns = aa_get_ns(ns); 14875d5182caSJohn Johansen list_add(&rawdata->list, &ns->rawdata_list); 14885d5182caSJohn Johansen /* no refcount on inode rawdata */ 14895d5182caSJohn Johansen 14905d5182caSJohn Johansen return 0; 14915d5182caSJohn Johansen 14925d5182caSJohn Johansen fail: 14935d5182caSJohn Johansen remove_rawdata_dents(rawdata); 14945d5182caSJohn Johansen 14955d5182caSJohn Johansen return PTR_ERR(dent); 14965d5182caSJohn Johansen } 1497d61c57fdSJohn Johansen #endif /* CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */ 1498d61c57fdSJohn Johansen 14995d5182caSJohn Johansen 15000d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/ 1501c97204baSJohn Johansen 1502c97204baSJohn Johansen /** 1503c97204baSJohn Johansen * 1504c97204baSJohn Johansen * Requires: @profile->ns->lock held 1505c97204baSJohn Johansen */ 1506c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile) 15070d259f04SJohn Johansen { 15080d259f04SJohn Johansen struct aa_profile *child; 15090d259f04SJohn Johansen int i; 15100d259f04SJohn Johansen 15110d259f04SJohn Johansen if (!profile) 15120d259f04SJohn Johansen return; 15130d259f04SJohn Johansen 15140d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) 1515c97204baSJohn Johansen __aafs_profile_rmdir(child); 15160d259f04SJohn Johansen 15170d259f04SJohn Johansen for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { 15188399588aSJohn Johansen struct aa_proxy *proxy; 15190d259f04SJohn Johansen if (!profile->dents[i]) 15200d259f04SJohn Johansen continue; 15210d259f04SJohn Johansen 15228399588aSJohn Johansen proxy = d_inode(profile->dents[i])->i_private; 1523c961ee5fSJohn Johansen aafs_remove(profile->dents[i]); 15248399588aSJohn Johansen aa_put_proxy(proxy); 15250d259f04SJohn Johansen profile->dents[i] = NULL; 15260d259f04SJohn Johansen } 15270d259f04SJohn Johansen } 15280d259f04SJohn Johansen 1529c97204baSJohn Johansen /** 1530c97204baSJohn Johansen * 1531c97204baSJohn Johansen * Requires: @old->ns->lock held 1532c97204baSJohn Johansen */ 1533c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old, 15340d259f04SJohn Johansen struct aa_profile *new) 15350d259f04SJohn Johansen { 15360d259f04SJohn Johansen int i; 15370d259f04SJohn Johansen 1538cbf2d0e1SJohn Johansen AA_BUG(!old); 1539cbf2d0e1SJohn Johansen AA_BUG(!new); 1540cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock)); 1541cbf2d0e1SJohn Johansen 15420d259f04SJohn Johansen for (i = 0; i < AAFS_PROF_SIZEOF; i++) { 15430d259f04SJohn Johansen new->dents[i] = old->dents[i]; 1544d671e890SJohn Johansen if (new->dents[i]) 1545078cd827SDeepa Dinamani new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); 15460d259f04SJohn Johansen old->dents[i] = NULL; 15470d259f04SJohn Johansen } 15480d259f04SJohn Johansen } 15490d259f04SJohn Johansen 15500d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name, 15510d259f04SJohn Johansen struct aa_profile *profile, 15520d259f04SJohn Johansen const struct file_operations *fops) 15530d259f04SJohn Johansen { 1554637f688dSJohn Johansen struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy); 15550d259f04SJohn Johansen struct dentry *dent; 15560d259f04SJohn Johansen 1557c961ee5fSJohn Johansen dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops); 15580d259f04SJohn Johansen if (IS_ERR(dent)) 15598399588aSJohn Johansen aa_put_proxy(proxy); 15600d259f04SJohn Johansen 15610d259f04SJohn Johansen return dent; 15620d259f04SJohn Johansen } 15630d259f04SJohn Johansen 1564d61c57fdSJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 15655d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile) 15665d5182caSJohn Johansen { 15675d5182caSJohn Johansen int depth = 0; 15685d5182caSJohn Johansen 15695d5182caSJohn Johansen rcu_read_lock(); 15705d5182caSJohn Johansen for (depth = 0; profile; profile = rcu_access_pointer(profile->parent)) 15715d5182caSJohn Johansen depth++; 15725d5182caSJohn Johansen rcu_read_unlock(); 15735d5182caSJohn Johansen 15745d5182caSJohn Johansen return depth; 15755d5182caSJohn Johansen } 15765d5182caSJohn Johansen 15771180b4c7SJohn Johansen static char *gen_symlink_name(int depth, const char *dirname, const char *fname) 15785d5182caSJohn Johansen { 15791180b4c7SJohn Johansen char *buffer, *s; 15805d5182caSJohn Johansen int error; 15811180b4c7SJohn Johansen int size = depth * 6 + strlen(dirname) + strlen(fname) + 11; 15821180b4c7SJohn Johansen 15831180b4c7SJohn Johansen s = buffer = kmalloc(size, GFP_KERNEL); 15841180b4c7SJohn Johansen if (!buffer) 15851180b4c7SJohn Johansen return ERR_PTR(-ENOMEM); 15865d5182caSJohn Johansen 15875d5182caSJohn Johansen for (; depth > 0; depth--) { 15881180b4c7SJohn Johansen strcpy(s, "../../"); 15891180b4c7SJohn Johansen s += 6; 15901180b4c7SJohn Johansen size -= 6; 15915d5182caSJohn Johansen } 15925d5182caSJohn Johansen 15931180b4c7SJohn Johansen error = snprintf(s, size, "raw_data/%s/%s", dirname, fname); 1594588558ebSColin Ian King if (error >= size || error < 0) { 1595588558ebSColin Ian King kfree(buffer); 15961180b4c7SJohn Johansen return ERR_PTR(-ENAMETOOLONG); 15975d5182caSJohn Johansen } 15985d5182caSJohn Johansen 15991180b4c7SJohn Johansen return buffer; 16005d5182caSJohn Johansen } 16015d5182caSJohn Johansen 16021180b4c7SJohn Johansen static void rawdata_link_cb(void *arg) 16031180b4c7SJohn Johansen { 16041180b4c7SJohn Johansen kfree(arg); 16051180b4c7SJohn Johansen } 16061180b4c7SJohn Johansen 16071180b4c7SJohn Johansen static const char *rawdata_get_link_base(struct dentry *dentry, 16081180b4c7SJohn Johansen struct inode *inode, 16091180b4c7SJohn Johansen struct delayed_call *done, 16101180b4c7SJohn Johansen const char *name) 16111180b4c7SJohn Johansen { 16121180b4c7SJohn Johansen struct aa_proxy *proxy = inode->i_private; 16131180b4c7SJohn Johansen struct aa_label *label; 16141180b4c7SJohn Johansen struct aa_profile *profile; 16151180b4c7SJohn Johansen char *target; 16161180b4c7SJohn Johansen int depth; 16171180b4c7SJohn Johansen 16181180b4c7SJohn Johansen if (!dentry) 16191180b4c7SJohn Johansen return ERR_PTR(-ECHILD); 16201180b4c7SJohn Johansen 16211180b4c7SJohn Johansen label = aa_get_label_rcu(&proxy->label); 16221180b4c7SJohn Johansen profile = labels_profile(label); 16231180b4c7SJohn Johansen depth = profile_depth(profile); 16241180b4c7SJohn Johansen target = gen_symlink_name(depth, profile->rawdata->name, name); 16251180b4c7SJohn Johansen aa_put_label(label); 16261180b4c7SJohn Johansen 16271180b4c7SJohn Johansen if (IS_ERR(target)) 16281180b4c7SJohn Johansen return target; 16291180b4c7SJohn Johansen 16301180b4c7SJohn Johansen set_delayed_call(done, rawdata_link_cb, target); 16311180b4c7SJohn Johansen 16321180b4c7SJohn Johansen return target; 16331180b4c7SJohn Johansen } 16341180b4c7SJohn Johansen 16351180b4c7SJohn Johansen static const char *rawdata_get_link_sha1(struct dentry *dentry, 16361180b4c7SJohn Johansen struct inode *inode, 16371180b4c7SJohn Johansen struct delayed_call *done) 16381180b4c7SJohn Johansen { 16391180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "sha1"); 16401180b4c7SJohn Johansen } 16411180b4c7SJohn Johansen 16421180b4c7SJohn Johansen static const char *rawdata_get_link_abi(struct dentry *dentry, 16431180b4c7SJohn Johansen struct inode *inode, 16441180b4c7SJohn Johansen struct delayed_call *done) 16451180b4c7SJohn Johansen { 16461180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "abi"); 16471180b4c7SJohn Johansen } 16481180b4c7SJohn Johansen 16491180b4c7SJohn Johansen static const char *rawdata_get_link_data(struct dentry *dentry, 16501180b4c7SJohn Johansen struct inode *inode, 16511180b4c7SJohn Johansen struct delayed_call *done) 16521180b4c7SJohn Johansen { 16531180b4c7SJohn Johansen return rawdata_get_link_base(dentry, inode, done, "raw_data"); 16541180b4c7SJohn Johansen } 16551180b4c7SJohn Johansen 16561180b4c7SJohn Johansen static const struct inode_operations rawdata_link_sha1_iops = { 16571180b4c7SJohn Johansen .get_link = rawdata_get_link_sha1, 16581180b4c7SJohn Johansen }; 16591180b4c7SJohn Johansen 16601180b4c7SJohn Johansen static const struct inode_operations rawdata_link_abi_iops = { 16611180b4c7SJohn Johansen .get_link = rawdata_get_link_abi, 16621180b4c7SJohn Johansen }; 16631180b4c7SJohn Johansen static const struct inode_operations rawdata_link_data_iops = { 16641180b4c7SJohn Johansen .get_link = rawdata_get_link_data, 16651180b4c7SJohn Johansen }; 1666d61c57fdSJohn Johansen #endif /* CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */ 16671180b4c7SJohn Johansen 16685d5182caSJohn Johansen /* 16695d5182caSJohn Johansen * Requires: @profile->ns->lock held 16705d5182caSJohn Johansen */ 1671c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) 16720d259f04SJohn Johansen { 16730d259f04SJohn Johansen struct aa_profile *child; 16740d259f04SJohn Johansen struct dentry *dent = NULL, *dir; 16750d259f04SJohn Johansen int error; 16760d259f04SJohn Johansen 1677cbf2d0e1SJohn Johansen AA_BUG(!profile); 1678cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock)); 1679cbf2d0e1SJohn Johansen 16800d259f04SJohn Johansen if (!parent) { 16810d259f04SJohn Johansen struct aa_profile *p; 16820d259f04SJohn Johansen p = aa_deref_parent(profile); 16830d259f04SJohn Johansen dent = prof_dir(p); 16840d259f04SJohn Johansen /* adding to parent that previously didn't have children */ 1685c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dent); 16860d259f04SJohn Johansen if (IS_ERR(dent)) 16870d259f04SJohn Johansen goto fail; 16880d259f04SJohn Johansen prof_child_dir(p) = parent = dent; 16890d259f04SJohn Johansen } 16900d259f04SJohn Johansen 16910d259f04SJohn Johansen if (!profile->dirname) { 16920d259f04SJohn Johansen int len, id_len; 16930d259f04SJohn Johansen len = mangle_name(profile->base.name, NULL); 16940d259f04SJohn Johansen id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); 16950d259f04SJohn Johansen 16960d259f04SJohn Johansen profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); 1697ffac1de6SDan Carpenter if (!profile->dirname) { 1698ffac1de6SDan Carpenter error = -ENOMEM; 1699ffac1de6SDan Carpenter goto fail2; 1700ffac1de6SDan Carpenter } 17010d259f04SJohn Johansen 17020d259f04SJohn Johansen mangle_name(profile->base.name, profile->dirname); 17030d259f04SJohn Johansen sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); 17040d259f04SJohn Johansen } 17050d259f04SJohn Johansen 1706c961ee5fSJohn Johansen dent = aafs_create_dir(profile->dirname, parent); 17070d259f04SJohn Johansen if (IS_ERR(dent)) 17080d259f04SJohn Johansen goto fail; 17090d259f04SJohn Johansen prof_dir(profile) = dir = dent; 17100d259f04SJohn Johansen 171152b97de3SJohn Johansen dent = create_profile_file(dir, "name", profile, 171252b97de3SJohn Johansen &seq_profile_name_fops); 17130d259f04SJohn Johansen if (IS_ERR(dent)) 17140d259f04SJohn Johansen goto fail; 17150d259f04SJohn Johansen profile->dents[AAFS_PROF_NAME] = dent; 17160d259f04SJohn Johansen 171752b97de3SJohn Johansen dent = create_profile_file(dir, "mode", profile, 171852b97de3SJohn Johansen &seq_profile_mode_fops); 17190d259f04SJohn Johansen if (IS_ERR(dent)) 17200d259f04SJohn Johansen goto fail; 17210d259f04SJohn Johansen profile->dents[AAFS_PROF_MODE] = dent; 17220d259f04SJohn Johansen 1723556d0be7SJohn Johansen dent = create_profile_file(dir, "attach", profile, 172452b97de3SJohn Johansen &seq_profile_attach_fops); 1725556d0be7SJohn Johansen if (IS_ERR(dent)) 1726556d0be7SJohn Johansen goto fail; 1727556d0be7SJohn Johansen profile->dents[AAFS_PROF_ATTACH] = dent; 1728556d0be7SJohn Johansen 1729f8eb8a13SJohn Johansen if (profile->hash) { 1730f8eb8a13SJohn Johansen dent = create_profile_file(dir, "sha1", profile, 173152b97de3SJohn Johansen &seq_profile_hash_fops); 1732f8eb8a13SJohn Johansen if (IS_ERR(dent)) 1733f8eb8a13SJohn Johansen goto fail; 1734f8eb8a13SJohn Johansen profile->dents[AAFS_PROF_HASH] = dent; 1735f8eb8a13SJohn Johansen } 1736f8eb8a13SJohn Johansen 1737d61c57fdSJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 17385ac8c355SJohn Johansen if (profile->rawdata) { 1739*482e8050SJohn Johansen if (aa_g_hash_policy) { 1740a68d59ffSJohn Johansen dent = aafs_create("raw_sha1", S_IFLNK | 0444, dir, 1741a68d59ffSJohn Johansen profile->label.proxy, NULL, NULL, 17421180b4c7SJohn Johansen &rawdata_link_sha1_iops); 17435ac8c355SJohn Johansen if (IS_ERR(dent)) 17445ac8c355SJohn Johansen goto fail; 17451180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 17465ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_HASH] = dent; 1747*482e8050SJohn Johansen } 1748a68d59ffSJohn Johansen dent = aafs_create("raw_abi", S_IFLNK | 0444, dir, 1749a68d59ffSJohn Johansen profile->label.proxy, NULL, NULL, 17501180b4c7SJohn Johansen &rawdata_link_abi_iops); 17515ac8c355SJohn Johansen if (IS_ERR(dent)) 17525ac8c355SJohn Johansen goto fail; 17531180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 17545ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_ABI] = dent; 17555ac8c355SJohn Johansen 1756a68d59ffSJohn Johansen dent = aafs_create("raw_data", S_IFLNK | 0444, dir, 1757a68d59ffSJohn Johansen profile->label.proxy, NULL, NULL, 17581180b4c7SJohn Johansen &rawdata_link_data_iops); 17595ac8c355SJohn Johansen if (IS_ERR(dent)) 17605ac8c355SJohn Johansen goto fail; 17611180b4c7SJohn Johansen aa_get_proxy(profile->label.proxy); 17625ac8c355SJohn Johansen profile->dents[AAFS_PROF_RAW_DATA] = dent; 17635ac8c355SJohn Johansen } 1764d61c57fdSJohn Johansen #endif /*CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */ 17655ac8c355SJohn Johansen 17660d259f04SJohn Johansen list_for_each_entry(child, &profile->base.profiles, base.list) { 1767c97204baSJohn Johansen error = __aafs_profile_mkdir(child, prof_child_dir(profile)); 17680d259f04SJohn Johansen if (error) 17690d259f04SJohn Johansen goto fail2; 17700d259f04SJohn Johansen } 17710d259f04SJohn Johansen 17720d259f04SJohn Johansen return 0; 17730d259f04SJohn Johansen 17740d259f04SJohn Johansen fail: 17750d259f04SJohn Johansen error = PTR_ERR(dent); 17760d259f04SJohn Johansen 17770d259f04SJohn Johansen fail2: 1778c97204baSJohn Johansen __aafs_profile_rmdir(profile); 17790d259f04SJohn Johansen 17800d259f04SJohn Johansen return error; 17810d259f04SJohn Johansen } 17820d259f04SJohn Johansen 1783549c7297SChristian Brauner static int ns_mkdir_op(struct user_namespace *mnt_userns, struct inode *dir, 1784549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 17854ae47f33SJohn Johansen { 17864ae47f33SJohn Johansen struct aa_ns *ns, *parent; 17874ae47f33SJohn Johansen /* TODO: improve permission check */ 1788637f688dSJohn Johansen struct aa_label *label; 1789637f688dSJohn Johansen int error; 1790637f688dSJohn Johansen 1791637f688dSJohn Johansen label = begin_current_label_crit_section(); 1792637f688dSJohn Johansen error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); 1793637f688dSJohn Johansen end_current_label_crit_section(label); 17944ae47f33SJohn Johansen if (error) 17954ae47f33SJohn Johansen return error; 17964ae47f33SJohn Johansen 17974ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 17984ae47f33SJohn Johansen AA_BUG(d_inode(ns_subns_dir(parent)) != dir); 17994ae47f33SJohn Johansen 18004ae47f33SJohn Johansen /* we have to unlock and then relock to get locking order right 18014ae47f33SJohn Johansen * for pin_fs 18024ae47f33SJohn Johansen */ 18034ae47f33SJohn Johansen inode_unlock(dir); 18044ae47f33SJohn Johansen error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 1805feb3c766SJohn Johansen mutex_lock_nested(&parent->lock, parent->level); 18064ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 18074ae47f33SJohn Johansen if (error) 18084ae47f33SJohn Johansen goto out; 18094ae47f33SJohn Johansen 18104ae47f33SJohn Johansen error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL, 18114ae47f33SJohn Johansen NULL, NULL, NULL); 18124ae47f33SJohn Johansen if (error) 18134ae47f33SJohn Johansen goto out_pin; 18144ae47f33SJohn Johansen 18154ae47f33SJohn Johansen ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name), 18164ae47f33SJohn Johansen dentry); 18174ae47f33SJohn Johansen if (IS_ERR(ns)) { 18184ae47f33SJohn Johansen error = PTR_ERR(ns); 18194ae47f33SJohn Johansen ns = NULL; 18204ae47f33SJohn Johansen } 18214ae47f33SJohn Johansen 18224ae47f33SJohn Johansen aa_put_ns(ns); /* list ref remains */ 18234ae47f33SJohn Johansen out_pin: 18244ae47f33SJohn Johansen if (error) 18254ae47f33SJohn Johansen simple_release_fs(&aafs_mnt, &aafs_count); 18264ae47f33SJohn Johansen out: 18274ae47f33SJohn Johansen mutex_unlock(&parent->lock); 18284ae47f33SJohn Johansen aa_put_ns(parent); 18294ae47f33SJohn Johansen 18304ae47f33SJohn Johansen return error; 18314ae47f33SJohn Johansen } 18324ae47f33SJohn Johansen 18334ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry) 18344ae47f33SJohn Johansen { 18354ae47f33SJohn Johansen struct aa_ns *ns, *parent; 18364ae47f33SJohn Johansen /* TODO: improve permission check */ 1837637f688dSJohn Johansen struct aa_label *label; 1838637f688dSJohn Johansen int error; 1839637f688dSJohn Johansen 1840637f688dSJohn Johansen label = begin_current_label_crit_section(); 1841637f688dSJohn Johansen error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); 1842637f688dSJohn Johansen end_current_label_crit_section(label); 18434ae47f33SJohn Johansen if (error) 18444ae47f33SJohn Johansen return error; 18454ae47f33SJohn Johansen 18464ae47f33SJohn Johansen parent = aa_get_ns(dir->i_private); 18474ae47f33SJohn Johansen /* rmdir calls the generic securityfs functions to remove files 18484ae47f33SJohn Johansen * from the apparmor dir. It is up to the apparmor ns locking 18494ae47f33SJohn Johansen * to avoid races. 18504ae47f33SJohn Johansen */ 18514ae47f33SJohn Johansen inode_unlock(dir); 18524ae47f33SJohn Johansen inode_unlock(dentry->d_inode); 18534ae47f33SJohn Johansen 1854feb3c766SJohn Johansen mutex_lock_nested(&parent->lock, parent->level); 18554ae47f33SJohn Johansen ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name, 18564ae47f33SJohn Johansen dentry->d_name.len)); 18574ae47f33SJohn Johansen if (!ns) { 18584ae47f33SJohn Johansen error = -ENOENT; 18594ae47f33SJohn Johansen goto out; 18604ae47f33SJohn Johansen } 18614ae47f33SJohn Johansen AA_BUG(ns_dir(ns) != dentry); 18624ae47f33SJohn Johansen 18634ae47f33SJohn Johansen __aa_remove_ns(ns); 18644ae47f33SJohn Johansen aa_put_ns(ns); 18654ae47f33SJohn Johansen 18664ae47f33SJohn Johansen out: 18674ae47f33SJohn Johansen mutex_unlock(&parent->lock); 18684ae47f33SJohn Johansen inode_lock_nested(dir, I_MUTEX_PARENT); 18694ae47f33SJohn Johansen inode_lock(dentry->d_inode); 18704ae47f33SJohn Johansen aa_put_ns(parent); 18714ae47f33SJohn Johansen 18724ae47f33SJohn Johansen return error; 18734ae47f33SJohn Johansen } 18744ae47f33SJohn Johansen 18754ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = { 18764ae47f33SJohn Johansen .lookup = simple_lookup, 18774ae47f33SJohn Johansen .mkdir = ns_mkdir_op, 18784ae47f33SJohn Johansen .rmdir = ns_rmdir_op, 18794ae47f33SJohn Johansen }; 18804ae47f33SJohn Johansen 18815d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns) 18825d5182caSJohn Johansen { 18835d5182caSJohn Johansen struct aa_loaddata *ent, *tmp; 18845d5182caSJohn Johansen 18855d5182caSJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 18865d5182caSJohn Johansen 18875d5182caSJohn Johansen list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list) 18885d5182caSJohn Johansen __aa_fs_remove_rawdata(ent); 18895d5182caSJohn Johansen } 18905d5182caSJohn Johansen 1891c97204baSJohn Johansen /** 1892c97204baSJohn Johansen * 1893c97204baSJohn Johansen * Requires: @ns->lock held 1894c97204baSJohn Johansen */ 1895c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns) 18960d259f04SJohn Johansen { 189798849dffSJohn Johansen struct aa_ns *sub; 18980d259f04SJohn Johansen struct aa_profile *child; 18990d259f04SJohn Johansen int i; 19000d259f04SJohn Johansen 19010d259f04SJohn Johansen if (!ns) 19020d259f04SJohn Johansen return; 1903cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 19040d259f04SJohn Johansen 19050d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) 1906c97204baSJohn Johansen __aafs_profile_rmdir(child); 19070d259f04SJohn Johansen 19080d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 1909feb3c766SJohn Johansen mutex_lock_nested(&sub->lock, sub->level); 1910c97204baSJohn Johansen __aafs_ns_rmdir(sub); 19110d259f04SJohn Johansen mutex_unlock(&sub->lock); 19120d259f04SJohn Johansen } 19130d259f04SJohn Johansen 19145d5182caSJohn Johansen __aa_fs_list_remove_rawdata(ns); 19155d5182caSJohn Johansen 1916b7fd2c03SJohn Johansen if (ns_subns_dir(ns)) { 1917b7fd2c03SJohn Johansen sub = d_inode(ns_subns_dir(ns))->i_private; 1918b7fd2c03SJohn Johansen aa_put_ns(sub); 1919b7fd2c03SJohn Johansen } 1920b7fd2c03SJohn Johansen if (ns_subload(ns)) { 1921b7fd2c03SJohn Johansen sub = d_inode(ns_subload(ns))->i_private; 1922b7fd2c03SJohn Johansen aa_put_ns(sub); 1923b7fd2c03SJohn Johansen } 1924b7fd2c03SJohn Johansen if (ns_subreplace(ns)) { 1925b7fd2c03SJohn Johansen sub = d_inode(ns_subreplace(ns))->i_private; 1926b7fd2c03SJohn Johansen aa_put_ns(sub); 1927b7fd2c03SJohn Johansen } 1928b7fd2c03SJohn Johansen if (ns_subremove(ns)) { 1929b7fd2c03SJohn Johansen sub = d_inode(ns_subremove(ns))->i_private; 1930b7fd2c03SJohn Johansen aa_put_ns(sub); 1931b7fd2c03SJohn Johansen } 1932d9bf2c26SJohn Johansen if (ns_subrevision(ns)) { 1933d9bf2c26SJohn Johansen sub = d_inode(ns_subrevision(ns))->i_private; 1934d9bf2c26SJohn Johansen aa_put_ns(sub); 1935d9bf2c26SJohn Johansen } 1936b7fd2c03SJohn Johansen 19370d259f04SJohn Johansen for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { 1938c961ee5fSJohn Johansen aafs_remove(ns->dents[i]); 19390d259f04SJohn Johansen ns->dents[i] = NULL; 19400d259f04SJohn Johansen } 19410d259f04SJohn Johansen } 19420d259f04SJohn Johansen 1943b7fd2c03SJohn Johansen /* assumes cleanup in caller */ 1944c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) 1945b7fd2c03SJohn Johansen { 1946b7fd2c03SJohn Johansen struct dentry *dent; 1947b7fd2c03SJohn Johansen 1948b7fd2c03SJohn Johansen AA_BUG(!ns); 1949b7fd2c03SJohn Johansen AA_BUG(!dir); 1950b7fd2c03SJohn Johansen 1951c961ee5fSJohn Johansen dent = aafs_create_dir("profiles", dir); 1952b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1953b7fd2c03SJohn Johansen return PTR_ERR(dent); 1954b7fd2c03SJohn Johansen ns_subprofs_dir(ns) = dent; 1955b7fd2c03SJohn Johansen 1956c961ee5fSJohn Johansen dent = aafs_create_dir("raw_data", dir); 1957b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1958b7fd2c03SJohn Johansen return PTR_ERR(dent); 1959b7fd2c03SJohn Johansen ns_subdata_dir(ns) = dent; 1960b7fd2c03SJohn Johansen 1961d9bf2c26SJohn Johansen dent = aafs_create_file("revision", 0444, dir, ns, 1962d9bf2c26SJohn Johansen &aa_fs_ns_revision_fops); 1963d9bf2c26SJohn Johansen if (IS_ERR(dent)) 1964d9bf2c26SJohn Johansen return PTR_ERR(dent); 1965d9bf2c26SJohn Johansen aa_get_ns(ns); 1966d9bf2c26SJohn Johansen ns_subrevision(ns) = dent; 1967d9bf2c26SJohn Johansen 1968c961ee5fSJohn Johansen dent = aafs_create_file(".load", 0640, dir, ns, 1969b7fd2c03SJohn Johansen &aa_fs_profile_load); 1970b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1971b7fd2c03SJohn Johansen return PTR_ERR(dent); 1972b7fd2c03SJohn Johansen aa_get_ns(ns); 1973b7fd2c03SJohn Johansen ns_subload(ns) = dent; 1974b7fd2c03SJohn Johansen 1975c961ee5fSJohn Johansen dent = aafs_create_file(".replace", 0640, dir, ns, 1976b7fd2c03SJohn Johansen &aa_fs_profile_replace); 1977b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1978b7fd2c03SJohn Johansen return PTR_ERR(dent); 1979b7fd2c03SJohn Johansen aa_get_ns(ns); 1980b7fd2c03SJohn Johansen ns_subreplace(ns) = dent; 1981b7fd2c03SJohn Johansen 1982c961ee5fSJohn Johansen dent = aafs_create_file(".remove", 0640, dir, ns, 1983b7fd2c03SJohn Johansen &aa_fs_profile_remove); 1984b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1985b7fd2c03SJohn Johansen return PTR_ERR(dent); 1986b7fd2c03SJohn Johansen aa_get_ns(ns); 1987b7fd2c03SJohn Johansen ns_subremove(ns) = dent; 1988b7fd2c03SJohn Johansen 19894ae47f33SJohn Johansen /* use create_dentry so we can supply private data */ 19904ae47f33SJohn Johansen dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL, 19914ae47f33SJohn Johansen &ns_dir_inode_operations); 1992b7fd2c03SJohn Johansen if (IS_ERR(dent)) 1993b7fd2c03SJohn Johansen return PTR_ERR(dent); 1994b7fd2c03SJohn Johansen aa_get_ns(ns); 1995b7fd2c03SJohn Johansen ns_subns_dir(ns) = dent; 1996b7fd2c03SJohn Johansen 1997b7fd2c03SJohn Johansen return 0; 1998b7fd2c03SJohn Johansen } 1999b7fd2c03SJohn Johansen 2000c97204baSJohn Johansen /* 2001c97204baSJohn Johansen * Requires: @ns->lock held 2002c97204baSJohn Johansen */ 200398407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name, 200498407f0aSJohn Johansen struct dentry *dent) 20050d259f04SJohn Johansen { 200698849dffSJohn Johansen struct aa_ns *sub; 20070d259f04SJohn Johansen struct aa_profile *child; 200898407f0aSJohn Johansen struct dentry *dir; 20090d259f04SJohn Johansen int error; 20100d259f04SJohn Johansen 2011b7fd2c03SJohn Johansen AA_BUG(!ns); 2012b7fd2c03SJohn Johansen AA_BUG(!parent); 2013b7fd2c03SJohn Johansen AA_BUG(!mutex_is_locked(&ns->lock)); 2014b7fd2c03SJohn Johansen 20150d259f04SJohn Johansen if (!name) 20160d259f04SJohn Johansen name = ns->base.name; 20170d259f04SJohn Johansen 2018c961ee5fSJohn Johansen if (!dent) { 2019b7fd2c03SJohn Johansen /* create ns dir if it doesn't already exist */ 2020c961ee5fSJohn Johansen dent = aafs_create_dir(name, parent); 20210d259f04SJohn Johansen if (IS_ERR(dent)) 20220d259f04SJohn Johansen goto fail; 2023c961ee5fSJohn Johansen } else 2024c961ee5fSJohn Johansen dget(dent); 20250d259f04SJohn Johansen ns_dir(ns) = dir = dent; 2026c97204baSJohn Johansen error = __aafs_ns_mkdir_entries(ns, dir); 2027b7fd2c03SJohn Johansen if (error) 2028b7fd2c03SJohn Johansen goto fail2; 20290d259f04SJohn Johansen 2030b7fd2c03SJohn Johansen /* profiles */ 20310d259f04SJohn Johansen list_for_each_entry(child, &ns->base.profiles, base.list) { 2032c97204baSJohn Johansen error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns)); 20330d259f04SJohn Johansen if (error) 20340d259f04SJohn Johansen goto fail2; 20350d259f04SJohn Johansen } 20360d259f04SJohn Johansen 2037b7fd2c03SJohn Johansen /* subnamespaces */ 20380d259f04SJohn Johansen list_for_each_entry(sub, &ns->sub_ns, base.list) { 2039feb3c766SJohn Johansen mutex_lock_nested(&sub->lock, sub->level); 204098407f0aSJohn Johansen error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL); 20410d259f04SJohn Johansen mutex_unlock(&sub->lock); 20420d259f04SJohn Johansen if (error) 20430d259f04SJohn Johansen goto fail2; 20440d259f04SJohn Johansen } 20450d259f04SJohn Johansen 20460d259f04SJohn Johansen return 0; 20470d259f04SJohn Johansen 20480d259f04SJohn Johansen fail: 20490d259f04SJohn Johansen error = PTR_ERR(dent); 20500d259f04SJohn Johansen 20510d259f04SJohn Johansen fail2: 2052c97204baSJohn Johansen __aafs_ns_rmdir(ns); 20530d259f04SJohn Johansen 20540d259f04SJohn Johansen return error; 20550d259f04SJohn Johansen } 20560d259f04SJohn Johansen 205729b3822fSJohn Johansen /** 205898849dffSJohn Johansen * __next_ns - find the next namespace to list 205929b3822fSJohn Johansen * @root: root namespace to stop search at (NOT NULL) 206029b3822fSJohn Johansen * @ns: current ns position (NOT NULL) 206129b3822fSJohn Johansen * 206229b3822fSJohn Johansen * Find the next namespace from @ns under @root and handle all locking needed 206329b3822fSJohn Johansen * while switching current namespace. 206429b3822fSJohn Johansen * 206529b3822fSJohn Johansen * Returns: next namespace or NULL if at last namespace under @root 206629b3822fSJohn Johansen * Requires: ns->parent->lock to be held 206729b3822fSJohn Johansen * NOTE: will not unlock root->lock 206829b3822fSJohn Johansen */ 206998849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) 207029b3822fSJohn Johansen { 207198849dffSJohn Johansen struct aa_ns *parent, *next; 207229b3822fSJohn Johansen 2073cbf2d0e1SJohn Johansen AA_BUG(!root); 2074cbf2d0e1SJohn Johansen AA_BUG(!ns); 2075cbf2d0e1SJohn Johansen AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock)); 2076cbf2d0e1SJohn Johansen 207729b3822fSJohn Johansen /* is next namespace a child */ 207829b3822fSJohn Johansen if (!list_empty(&ns->sub_ns)) { 207929b3822fSJohn Johansen next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); 2080feb3c766SJohn Johansen mutex_lock_nested(&next->lock, next->level); 208129b3822fSJohn Johansen return next; 208229b3822fSJohn Johansen } 208329b3822fSJohn Johansen 208429b3822fSJohn Johansen /* check if the next ns is a sibling, parent, gp, .. */ 208529b3822fSJohn Johansen parent = ns->parent; 2086ed2c7da3SJohn Johansen while (ns != root) { 208729b3822fSJohn Johansen mutex_unlock(&ns->lock); 208838dbd7d8SGeliang Tang next = list_next_entry(ns, base.list); 208929b3822fSJohn Johansen if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { 2090feb3c766SJohn Johansen mutex_lock_nested(&next->lock, next->level); 209129b3822fSJohn Johansen return next; 209229b3822fSJohn Johansen } 209329b3822fSJohn Johansen ns = parent; 209429b3822fSJohn Johansen parent = parent->parent; 209529b3822fSJohn Johansen } 209629b3822fSJohn Johansen 209729b3822fSJohn Johansen return NULL; 209829b3822fSJohn Johansen } 209929b3822fSJohn Johansen 210029b3822fSJohn Johansen /** 210129b3822fSJohn Johansen * __first_profile - find the first profile in a namespace 210229b3822fSJohn Johansen * @root: namespace that is root of profiles being displayed (NOT NULL) 210329b3822fSJohn Johansen * @ns: namespace to start in (NOT NULL) 210429b3822fSJohn Johansen * 210529b3822fSJohn Johansen * Returns: unrefcounted profile or NULL if no profile 210629b3822fSJohn Johansen * Requires: profile->ns.lock to be held 210729b3822fSJohn Johansen */ 210898849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root, 210998849dffSJohn Johansen struct aa_ns *ns) 211029b3822fSJohn Johansen { 2111cbf2d0e1SJohn Johansen AA_BUG(!root); 2112cbf2d0e1SJohn Johansen AA_BUG(ns && !mutex_is_locked(&ns->lock)); 2113cbf2d0e1SJohn Johansen 211498849dffSJohn Johansen for (; ns; ns = __next_ns(root, ns)) { 211529b3822fSJohn Johansen if (!list_empty(&ns->base.profiles)) 211629b3822fSJohn Johansen return list_first_entry(&ns->base.profiles, 211729b3822fSJohn Johansen struct aa_profile, base.list); 211829b3822fSJohn Johansen } 211929b3822fSJohn Johansen return NULL; 212029b3822fSJohn Johansen } 212129b3822fSJohn Johansen 212229b3822fSJohn Johansen /** 212329b3822fSJohn Johansen * __next_profile - step to the next profile in a profile tree 2124aa4ceed7SChenXiaoSong * @p: current profile in tree (NOT NULL) 212529b3822fSJohn Johansen * 212629b3822fSJohn Johansen * Perform a depth first traversal on the profile tree in a namespace 212729b3822fSJohn Johansen * 212829b3822fSJohn Johansen * Returns: next profile or NULL if done 212929b3822fSJohn Johansen * Requires: profile->ns.lock to be held 213029b3822fSJohn Johansen */ 213129b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p) 213229b3822fSJohn Johansen { 213329b3822fSJohn Johansen struct aa_profile *parent; 213498849dffSJohn Johansen struct aa_ns *ns = p->ns; 213529b3822fSJohn Johansen 2136cbf2d0e1SJohn Johansen AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock)); 2137cbf2d0e1SJohn Johansen 213829b3822fSJohn Johansen /* is next profile a child */ 213929b3822fSJohn Johansen if (!list_empty(&p->base.profiles)) 214029b3822fSJohn Johansen return list_first_entry(&p->base.profiles, typeof(*p), 214129b3822fSJohn Johansen base.list); 214229b3822fSJohn Johansen 214329b3822fSJohn Johansen /* is next profile a sibling, parent sibling, gp, sibling, .. */ 214429b3822fSJohn Johansen parent = rcu_dereference_protected(p->parent, 214529b3822fSJohn Johansen mutex_is_locked(&p->ns->lock)); 214629b3822fSJohn Johansen while (parent) { 214738dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 214829b3822fSJohn Johansen if (!list_entry_is_head(p, &parent->base.profiles, base.list)) 214929b3822fSJohn Johansen return p; 215029b3822fSJohn Johansen p = parent; 215129b3822fSJohn Johansen parent = rcu_dereference_protected(parent->parent, 215229b3822fSJohn Johansen mutex_is_locked(&parent->ns->lock)); 215329b3822fSJohn Johansen } 215429b3822fSJohn Johansen 215529b3822fSJohn Johansen /* is next another profile in the namespace */ 215638dbd7d8SGeliang Tang p = list_next_entry(p, base.list); 215729b3822fSJohn Johansen if (!list_entry_is_head(p, &ns->base.profiles, base.list)) 215829b3822fSJohn Johansen return p; 215929b3822fSJohn Johansen 216029b3822fSJohn Johansen return NULL; 216129b3822fSJohn Johansen } 216229b3822fSJohn Johansen 216329b3822fSJohn Johansen /** 216429b3822fSJohn Johansen * next_profile - step to the next profile in where ever it may be 216529b3822fSJohn Johansen * @root: root namespace (NOT NULL) 216629b3822fSJohn Johansen * @profile: current profile (NOT NULL) 216729b3822fSJohn Johansen * 216829b3822fSJohn Johansen * Returns: next profile or NULL if there isn't one 216929b3822fSJohn Johansen */ 217098849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root, 217129b3822fSJohn Johansen struct aa_profile *profile) 217229b3822fSJohn Johansen { 217329b3822fSJohn Johansen struct aa_profile *next = __next_profile(profile); 217429b3822fSJohn Johansen if (next) 217529b3822fSJohn Johansen return next; 217629b3822fSJohn Johansen 217729b3822fSJohn Johansen /* finished all profiles in namespace move to next namespace */ 217898849dffSJohn Johansen return __first_profile(root, __next_ns(root, profile->ns)); 217929b3822fSJohn Johansen } 218029b3822fSJohn Johansen 218129b3822fSJohn Johansen /** 218229b3822fSJohn Johansen * p_start - start a depth first traversal of profile tree 218329b3822fSJohn Johansen * @f: seq_file to fill 218429b3822fSJohn Johansen * @pos: current position 218529b3822fSJohn Johansen * 218629b3822fSJohn Johansen * Returns: first profile under current namespace or NULL if none found 218729b3822fSJohn Johansen * 218829b3822fSJohn Johansen * acquires first ns->lock 218929b3822fSJohn Johansen */ 219029b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos) 219129b3822fSJohn Johansen { 219229b3822fSJohn Johansen struct aa_profile *profile = NULL; 2193cf797c0eSJohn Johansen struct aa_ns *root = aa_get_current_ns(); 219429b3822fSJohn Johansen loff_t l = *pos; 2195cf797c0eSJohn Johansen f->private = root; 219629b3822fSJohn Johansen 219729b3822fSJohn Johansen /* find the first profile */ 2198feb3c766SJohn Johansen mutex_lock_nested(&root->lock, root->level); 219929b3822fSJohn Johansen profile = __first_profile(root, root); 220029b3822fSJohn Johansen 220129b3822fSJohn Johansen /* skip to position */ 220229b3822fSJohn Johansen for (; profile && l > 0; l--) 220329b3822fSJohn Johansen profile = next_profile(root, profile); 220429b3822fSJohn Johansen 220529b3822fSJohn Johansen return profile; 220629b3822fSJohn Johansen } 220729b3822fSJohn Johansen 220829b3822fSJohn Johansen /** 220929b3822fSJohn Johansen * p_next - read the next profile entry 221029b3822fSJohn Johansen * @f: seq_file to fill 221129b3822fSJohn Johansen * @p: profile previously returned 221229b3822fSJohn Johansen * @pos: current position 221329b3822fSJohn Johansen * 221429b3822fSJohn Johansen * Returns: next profile after @p or NULL if none 221529b3822fSJohn Johansen * 221629b3822fSJohn Johansen * may acquire/release locks in namespace tree as necessary 221729b3822fSJohn Johansen */ 221829b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos) 221929b3822fSJohn Johansen { 222029b3822fSJohn Johansen struct aa_profile *profile = p; 222198849dffSJohn Johansen struct aa_ns *ns = f->private; 222229b3822fSJohn Johansen (*pos)++; 222329b3822fSJohn Johansen 222429b3822fSJohn Johansen return next_profile(ns, profile); 222529b3822fSJohn Johansen } 222629b3822fSJohn Johansen 222729b3822fSJohn Johansen /** 222829b3822fSJohn Johansen * p_stop - stop depth first traversal 222929b3822fSJohn Johansen * @f: seq_file we are filling 223029b3822fSJohn Johansen * @p: the last profile writen 223129b3822fSJohn Johansen * 223229b3822fSJohn Johansen * Release all locking done by p_start/p_next on namespace tree 223329b3822fSJohn Johansen */ 223429b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p) 223529b3822fSJohn Johansen { 223629b3822fSJohn Johansen struct aa_profile *profile = p; 223798849dffSJohn Johansen struct aa_ns *root = f->private, *ns; 223829b3822fSJohn Johansen 223929b3822fSJohn Johansen if (profile) { 224029b3822fSJohn Johansen for (ns = profile->ns; ns && ns != root; ns = ns->parent) 224129b3822fSJohn Johansen mutex_unlock(&ns->lock); 224229b3822fSJohn Johansen } 224329b3822fSJohn Johansen mutex_unlock(&root->lock); 224498849dffSJohn Johansen aa_put_ns(root); 224529b3822fSJohn Johansen } 224629b3822fSJohn Johansen 224729b3822fSJohn Johansen /** 224829b3822fSJohn Johansen * seq_show_profile - show a profile entry 224929b3822fSJohn Johansen * @f: seq_file to file 225029b3822fSJohn Johansen * @p: current position (profile) (NOT NULL) 225129b3822fSJohn Johansen * 225229b3822fSJohn Johansen * Returns: error on failure 225329b3822fSJohn Johansen */ 225429b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p) 225529b3822fSJohn Johansen { 225629b3822fSJohn Johansen struct aa_profile *profile = (struct aa_profile *)p; 225798849dffSJohn Johansen struct aa_ns *root = f->private; 225829b3822fSJohn Johansen 2259637f688dSJohn Johansen aa_label_seq_xprint(f, root, &profile->label, 2260637f688dSJohn Johansen FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL); 2261637f688dSJohn Johansen seq_putc(f, '\n'); 226229b3822fSJohn Johansen 226329b3822fSJohn Johansen return 0; 226429b3822fSJohn Johansen } 226529b3822fSJohn Johansen 2266c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = { 226729b3822fSJohn Johansen .start = p_start, 226829b3822fSJohn Johansen .next = p_next, 226929b3822fSJohn Johansen .stop = p_stop, 227029b3822fSJohn Johansen .show = seq_show_profile, 227129b3822fSJohn Johansen }; 227229b3822fSJohn Johansen 227329b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file) 227429b3822fSJohn Johansen { 227592de220aSJohn Johansen if (!aa_current_policy_view_capable(NULL)) 22765ac8c355SJohn Johansen return -EACCES; 22775ac8c355SJohn Johansen 2278c97204baSJohn Johansen return seq_open(file, &aa_sfs_profiles_op); 227929b3822fSJohn Johansen } 228029b3822fSJohn Johansen 228129b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file) 228229b3822fSJohn Johansen { 228329b3822fSJohn Johansen return seq_release(inode, file); 228429b3822fSJohn Johansen } 228529b3822fSJohn Johansen 2286c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = { 228729b3822fSJohn Johansen .open = profiles_open, 228829b3822fSJohn Johansen .read = seq_read, 228929b3822fSJohn Johansen .llseek = seq_lseek, 229029b3822fSJohn Johansen .release = profiles_release, 229129b3822fSJohn Johansen }; 229229b3822fSJohn Johansen 229329b3822fSJohn Johansen 22940d259f04SJohn Johansen /** Base file system setup **/ 2295c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = { 2296c97204baSJohn Johansen AA_SFS_FILE_STRING("mask", 2297c97204baSJohn Johansen "create read write exec append mmap_exec link lock"), 2298a9bf8e9fSKees Cook { } 2299a9bf8e9fSKees Cook }; 2300a9bf8e9fSKees Cook 2301290f458aSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ptrace[] = { 2302290f458aSJohn Johansen AA_SFS_FILE_STRING("mask", "read trace"), 2303290f458aSJohn Johansen { } 2304290f458aSJohn Johansen }; 2305290f458aSJohn Johansen 2306cd1dbf76SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_signal[] = { 2307cd1dbf76SJohn Johansen AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK), 2308cd1dbf76SJohn Johansen { } 2309cd1dbf76SJohn Johansen }; 2310cd1dbf76SJohn Johansen 231173f488cdSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_attach[] = { 231273f488cdSJohn Johansen AA_SFS_FILE_BOOLEAN("xattr", 1), 231373f488cdSJohn Johansen { } 231473f488cdSJohn Johansen }; 2315c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = { 2316c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hat", 1), 2317c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_hatv", 1), 2318c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_onexec", 1), 2319c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("change_profile", 1), 23206c5fc8f1SJohn Johansen AA_SFS_FILE_BOOLEAN("stack", 1), 2321c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), 23229fcf78ccSJohn Johansen AA_SFS_FILE_BOOLEAN("post_nnp_subset", 1), 232321f60661SJohn Johansen AA_SFS_FILE_BOOLEAN("computed_longest_left", 1), 232473f488cdSJohn Johansen AA_SFS_DIR("attach_conditions", aa_sfs_entry_attach), 2325c97204baSJohn Johansen AA_SFS_FILE_STRING("version", "1.2"), 2326e74abcf3SKees Cook { } 2327e74abcf3SKees Cook }; 2328e74abcf3SKees Cook 2329c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = { 2330c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("v5", 1), 23315379a331SJohn Johansen AA_SFS_FILE_BOOLEAN("v6", 1), 23325379a331SJohn Johansen AA_SFS_FILE_BOOLEAN("v7", 1), 233356974a6fSJohn Johansen AA_SFS_FILE_BOOLEAN("v8", 1), 2334474d6b75SJohn Johansen { } 2335474d6b75SJohn Johansen }; 2336474d6b75SJohn Johansen 2337c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = { 2338c97204baSJohn Johansen AA_SFS_DIR("versions", aa_sfs_entry_versions), 2339c97204baSJohn Johansen AA_SFS_FILE_BOOLEAN("set_load", 1), 23400df34a64SJohn Johansen /* number of out of band transitions supported */ 23410df34a64SJohn Johansen AA_SFS_FILE_U64("outofband", MAX_OOB_SUPPORTED), 23429d910a3bSJohn Johansen { } 23439d910a3bSJohn Johansen }; 23449d910a3bSJohn Johansen 23452ea3ffb7SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_mount[] = { 23462ea3ffb7SJohn Johansen AA_SFS_FILE_STRING("mask", "mount umount pivot_root"), 23472ea3ffb7SJohn Johansen { } 23482ea3ffb7SJohn Johansen }; 23492ea3ffb7SJohn Johansen 235033f2eadaSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ns[] = { 235133f2eadaSJohn Johansen AA_SFS_FILE_BOOLEAN("profile", 1), 23522ea3ffb7SJohn Johansen AA_SFS_FILE_BOOLEAN("pivot_root", 0), 235333f2eadaSJohn Johansen { } 235433f2eadaSJohn Johansen }; 235533f2eadaSJohn Johansen 2356a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = { 23574f3b3f2dSJohn Johansen AA_SFS_FILE_STRING("perms", "allow deny audit quiet"), 2358a83bd86eSJohn Johansen AA_SFS_FILE_BOOLEAN("data", 1), 23591dea3b41SJohn Johansen AA_SFS_FILE_BOOLEAN("multi_transaction", 1), 2360a83bd86eSJohn Johansen { } 2361a83bd86eSJohn Johansen }; 2362a83bd86eSJohn Johansen 2363a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = { 2364a83bd86eSJohn Johansen AA_SFS_DIR("label", aa_sfs_entry_query_label), 2365a83bd86eSJohn Johansen { } 2366a83bd86eSJohn Johansen }; 2367c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = { 2368c97204baSJohn Johansen AA_SFS_DIR("policy", aa_sfs_entry_policy), 2369c97204baSJohn Johansen AA_SFS_DIR("domain", aa_sfs_entry_domain), 2370c97204baSJohn Johansen AA_SFS_DIR("file", aa_sfs_entry_file), 237156974a6fSJohn Johansen AA_SFS_DIR("network_v8", aa_sfs_entry_network), 23722ea3ffb7SJohn Johansen AA_SFS_DIR("mount", aa_sfs_entry_mount), 237333f2eadaSJohn Johansen AA_SFS_DIR("namespaces", aa_sfs_entry_ns), 2374c97204baSJohn Johansen AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 2375c97204baSJohn Johansen AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit), 2376c97204baSJohn Johansen AA_SFS_DIR("caps", aa_sfs_entry_caps), 2377290f458aSJohn Johansen AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace), 2378cd1dbf76SJohn Johansen AA_SFS_DIR("signal", aa_sfs_entry_signal), 2379a83bd86eSJohn Johansen AA_SFS_DIR("query", aa_sfs_entry_query), 2380e74abcf3SKees Cook { } 2381e74abcf3SKees Cook }; 2382e74abcf3SKees Cook 2383c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = { 2384bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access), 23856c5fc8f1SJohn Johansen AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops), 23866c5fc8f1SJohn Johansen AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops), 2387bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops), 2388bf81100fSJohn Johansen AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops), 2389bf81100fSJohn Johansen AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops), 2390c97204baSJohn Johansen AA_SFS_DIR("features", aa_sfs_entry_features), 23919acd494bSKees Cook { } 23929acd494bSKees Cook }; 239363e2b423SJohn Johansen 2394c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry = 2395c97204baSJohn Johansen AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor); 23969acd494bSKees Cook 23979acd494bSKees Cook /** 2398a481f4d9SJohn Johansen * entry_create_file - create a file entry in the apparmor securityfs 2399c97204baSJohn Johansen * @fs_file: aa_sfs_entry to build an entry for (NOT NULL) 24009acd494bSKees Cook * @parent: the parent dentry in the securityfs 24019acd494bSKees Cook * 2402a481f4d9SJohn Johansen * Use entry_remove_file to remove entries created with this fn. 24039acd494bSKees Cook */ 2404c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file, 24059acd494bSKees Cook struct dentry *parent) 240663e2b423SJohn Johansen { 24079acd494bSKees Cook int error = 0; 240863e2b423SJohn Johansen 24099acd494bSKees Cook fs_file->dentry = securityfs_create_file(fs_file->name, 24109acd494bSKees Cook S_IFREG | fs_file->mode, 24119acd494bSKees Cook parent, fs_file, 24129acd494bSKees Cook fs_file->file_ops); 24139acd494bSKees Cook if (IS_ERR(fs_file->dentry)) { 24149acd494bSKees Cook error = PTR_ERR(fs_file->dentry); 24159acd494bSKees Cook fs_file->dentry = NULL; 241663e2b423SJohn Johansen } 24179acd494bSKees Cook return error; 241863e2b423SJohn Johansen } 241963e2b423SJohn Johansen 2420c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir); 242163e2b423SJohn Johansen /** 2422a481f4d9SJohn Johansen * entry_create_dir - recursively create a directory entry in the securityfs 2423c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL) 24249acd494bSKees Cook * @parent: the parent dentry in the securityfs 242563e2b423SJohn Johansen * 2426a481f4d9SJohn Johansen * Use entry_remove_dir to remove entries created with this fn. 242763e2b423SJohn Johansen */ 2428c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir, 24299acd494bSKees Cook struct dentry *parent) 243063e2b423SJohn Johansen { 2431c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 24320d259f04SJohn Johansen struct dentry *dir; 24330d259f04SJohn Johansen int error; 243463e2b423SJohn Johansen 24350d259f04SJohn Johansen dir = securityfs_create_dir(fs_dir->name, parent); 24360d259f04SJohn Johansen if (IS_ERR(dir)) 24370d259f04SJohn Johansen return PTR_ERR(dir); 24380d259f04SJohn Johansen fs_dir->dentry = dir; 243963e2b423SJohn Johansen 24400d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2441c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2442a481f4d9SJohn Johansen error = entry_create_dir(fs_file, fs_dir->dentry); 24439acd494bSKees Cook else 2444a481f4d9SJohn Johansen error = entry_create_file(fs_file, fs_dir->dentry); 24459acd494bSKees Cook if (error) 24469acd494bSKees Cook goto failed; 24479acd494bSKees Cook } 24489acd494bSKees Cook 24499acd494bSKees Cook return 0; 24509acd494bSKees Cook 24519acd494bSKees Cook failed: 2452a481f4d9SJohn Johansen entry_remove_dir(fs_dir); 24530d259f04SJohn Johansen 24549acd494bSKees Cook return error; 24559acd494bSKees Cook } 24569acd494bSKees Cook 24579acd494bSKees Cook /** 2458c97204baSJohn Johansen * entry_remove_file - drop a single file entry in the apparmor securityfs 2459c97204baSJohn Johansen * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL) 24609acd494bSKees Cook */ 2461c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file) 24629acd494bSKees Cook { 24639acd494bSKees Cook if (!fs_file->dentry) 24649acd494bSKees Cook return; 24659acd494bSKees Cook 24669acd494bSKees Cook securityfs_remove(fs_file->dentry); 24679acd494bSKees Cook fs_file->dentry = NULL; 24689acd494bSKees Cook } 24699acd494bSKees Cook 24709acd494bSKees Cook /** 2471a481f4d9SJohn Johansen * entry_remove_dir - recursively drop a directory entry from the securityfs 2472c97204baSJohn Johansen * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL) 24739acd494bSKees Cook */ 2474c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir) 24759acd494bSKees Cook { 2476c97204baSJohn Johansen struct aa_sfs_entry *fs_file; 24779acd494bSKees Cook 24780d259f04SJohn Johansen for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2479c97204baSJohn Johansen if (fs_file->v_type == AA_SFS_TYPE_DIR) 2480a481f4d9SJohn Johansen entry_remove_dir(fs_file); 24819acd494bSKees Cook else 2482c97204baSJohn Johansen entry_remove_file(fs_file); 24839acd494bSKees Cook } 24849acd494bSKees Cook 2485c97204baSJohn Johansen entry_remove_file(fs_dir); 248663e2b423SJohn Johansen } 248763e2b423SJohn Johansen 248863e2b423SJohn Johansen /** 248963e2b423SJohn Johansen * aa_destroy_aafs - cleanup and free aafs 249063e2b423SJohn Johansen * 249163e2b423SJohn Johansen * releases dentries allocated by aa_create_aafs 249263e2b423SJohn Johansen */ 249363e2b423SJohn Johansen void __init aa_destroy_aafs(void) 249463e2b423SJohn Johansen { 2495c97204baSJohn Johansen entry_remove_dir(&aa_sfs_entry); 249663e2b423SJohn Johansen } 249763e2b423SJohn Johansen 2498a71ada30SJohn Johansen 2499a71ada30SJohn Johansen #define NULL_FILE_NAME ".null" 2500a71ada30SJohn Johansen struct path aa_null; 2501a71ada30SJohn Johansen 2502a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent) 2503a71ada30SJohn Johansen { 2504a71ada30SJohn Johansen struct vfsmount *mount = NULL; 2505a71ada30SJohn Johansen struct dentry *dentry; 2506a71ada30SJohn Johansen struct inode *inode; 2507a71ada30SJohn Johansen int count = 0; 2508a71ada30SJohn Johansen int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); 2509a71ada30SJohn Johansen 2510a71ada30SJohn Johansen if (error) 2511a71ada30SJohn Johansen return error; 2512a71ada30SJohn Johansen 2513a71ada30SJohn Johansen inode_lock(d_inode(parent)); 2514a71ada30SJohn Johansen dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); 2515a71ada30SJohn Johansen if (IS_ERR(dentry)) { 2516a71ada30SJohn Johansen error = PTR_ERR(dentry); 2517a71ada30SJohn Johansen goto out; 2518a71ada30SJohn Johansen } 2519a71ada30SJohn Johansen inode = new_inode(parent->d_inode->i_sb); 2520a71ada30SJohn Johansen if (!inode) { 2521a71ada30SJohn Johansen error = -ENOMEM; 2522a71ada30SJohn Johansen goto out1; 2523a71ada30SJohn Johansen } 2524a71ada30SJohn Johansen 2525a71ada30SJohn Johansen inode->i_ino = get_next_ino(); 2526a71ada30SJohn Johansen inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; 252724d0d03cSDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2528a71ada30SJohn Johansen init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, 2529a71ada30SJohn Johansen MKDEV(MEM_MAJOR, 3)); 2530a71ada30SJohn Johansen d_instantiate(dentry, inode); 2531a71ada30SJohn Johansen aa_null.dentry = dget(dentry); 2532a71ada30SJohn Johansen aa_null.mnt = mntget(mount); 2533a71ada30SJohn Johansen 2534a71ada30SJohn Johansen error = 0; 2535a71ada30SJohn Johansen 2536a71ada30SJohn Johansen out1: 2537a71ada30SJohn Johansen dput(dentry); 2538a71ada30SJohn Johansen out: 2539a71ada30SJohn Johansen inode_unlock(d_inode(parent)); 2540a71ada30SJohn Johansen simple_release_fs(&mount, &count); 2541a71ada30SJohn Johansen return error; 2542a71ada30SJohn Johansen } 2543a71ada30SJohn Johansen 2544a481f4d9SJohn Johansen 2545a481f4d9SJohn Johansen 2546a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry, 2547a481f4d9SJohn Johansen struct inode *inode, 2548a481f4d9SJohn Johansen struct delayed_call *done) 2549a481f4d9SJohn Johansen { 2550a481f4d9SJohn Johansen struct aa_ns *ns; 2551a481f4d9SJohn Johansen struct path path; 25521bc82070SAleksa Sarai int error; 2553a481f4d9SJohn Johansen 2554a481f4d9SJohn Johansen if (!dentry) 2555a481f4d9SJohn Johansen return ERR_PTR(-ECHILD); 25561bc82070SAleksa Sarai 2557a481f4d9SJohn Johansen ns = aa_get_current_ns(); 2558a481f4d9SJohn Johansen path.mnt = mntget(aafs_mnt); 2559a481f4d9SJohn Johansen path.dentry = dget(ns_dir(ns)); 25601bc82070SAleksa Sarai error = nd_jump_link(&path); 2561a481f4d9SJohn Johansen aa_put_ns(ns); 2562a481f4d9SJohn Johansen 25631bc82070SAleksa Sarai return ERR_PTR(error); 2564a481f4d9SJohn Johansen } 2565a481f4d9SJohn Johansen 2566a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer, 2567a481f4d9SJohn Johansen int buflen) 2568a481f4d9SJohn Johansen { 2569a481f4d9SJohn Johansen char name[32]; 2570a481f4d9SJohn Johansen int res; 2571a481f4d9SJohn Johansen 2572a0781209SJohn Johansen res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME, 2573a0781209SJohn Johansen d_inode(dentry)->i_ino); 2574a0781209SJohn Johansen if (res > 0 && res < sizeof(name)) 2575a481f4d9SJohn Johansen res = readlink_copy(buffer, buflen, name); 2576a0781209SJohn Johansen else 2577a0781209SJohn Johansen res = -ENOENT; 2578a481f4d9SJohn Johansen 2579a481f4d9SJohn Johansen return res; 2580a481f4d9SJohn Johansen } 2581a481f4d9SJohn Johansen 2582a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = { 2583a481f4d9SJohn Johansen .readlink = policy_readlink, 2584a481f4d9SJohn Johansen .get_link = policy_get_link, 2585a481f4d9SJohn Johansen }; 2586a481f4d9SJohn Johansen 2587a481f4d9SJohn Johansen 258863e2b423SJohn Johansen /** 258963e2b423SJohn Johansen * aa_create_aafs - create the apparmor security filesystem 259063e2b423SJohn Johansen * 259163e2b423SJohn Johansen * dentries created here are released by aa_destroy_aafs 259263e2b423SJohn Johansen * 259363e2b423SJohn Johansen * Returns: error on failure 259463e2b423SJohn Johansen */ 25953417d8d5SJames Morris static int __init aa_create_aafs(void) 259663e2b423SJohn Johansen { 2597b7fd2c03SJohn Johansen struct dentry *dent; 259863e2b423SJohn Johansen int error; 259963e2b423SJohn Johansen 260063e2b423SJohn Johansen if (!apparmor_initialized) 260163e2b423SJohn Johansen return 0; 260263e2b423SJohn Johansen 2603c97204baSJohn Johansen if (aa_sfs_entry.dentry) { 260463e2b423SJohn Johansen AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 260563e2b423SJohn Johansen return -EEXIST; 260663e2b423SJohn Johansen } 260763e2b423SJohn Johansen 2608a481f4d9SJohn Johansen /* setup apparmorfs used to virtualize policy/ */ 2609a481f4d9SJohn Johansen aafs_mnt = kern_mount(&aafs_ops); 2610a481f4d9SJohn Johansen if (IS_ERR(aafs_mnt)) 2611a481f4d9SJohn Johansen panic("can't set apparmorfs up\n"); 26121751e8a6SLinus Torvalds aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER; 2613a481f4d9SJohn Johansen 26149acd494bSKees Cook /* Populate fs tree. */ 2615c97204baSJohn Johansen error = entry_create_dir(&aa_sfs_entry, NULL); 261663e2b423SJohn Johansen if (error) 261763e2b423SJohn Johansen goto error; 261863e2b423SJohn Johansen 2619c97204baSJohn Johansen dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry, 2620b7fd2c03SJohn Johansen NULL, &aa_fs_profile_load); 2621cf916000SJohn Johansen if (IS_ERR(dent)) 2622cf916000SJohn Johansen goto dent_error; 2623b7fd2c03SJohn Johansen ns_subload(root_ns) = dent; 2624b7fd2c03SJohn Johansen 2625c97204baSJohn Johansen dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry, 2626b7fd2c03SJohn Johansen NULL, &aa_fs_profile_replace); 2627cf916000SJohn Johansen if (IS_ERR(dent)) 2628cf916000SJohn Johansen goto dent_error; 2629b7fd2c03SJohn Johansen ns_subreplace(root_ns) = dent; 2630b7fd2c03SJohn Johansen 2631c97204baSJohn Johansen dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry, 2632b7fd2c03SJohn Johansen NULL, &aa_fs_profile_remove); 2633cf916000SJohn Johansen if (IS_ERR(dent)) 2634cf916000SJohn Johansen goto dent_error; 2635b7fd2c03SJohn Johansen ns_subremove(root_ns) = dent; 2636b7fd2c03SJohn Johansen 2637d9bf2c26SJohn Johansen dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry, 2638d9bf2c26SJohn Johansen NULL, &aa_fs_ns_revision_fops); 2639cf916000SJohn Johansen if (IS_ERR(dent)) 2640cf916000SJohn Johansen goto dent_error; 2641d9bf2c26SJohn Johansen ns_subrevision(root_ns) = dent; 2642d9bf2c26SJohn Johansen 2643d9bf2c26SJohn Johansen /* policy tree referenced by magic policy symlink */ 2644feb3c766SJohn Johansen mutex_lock_nested(&root_ns->lock, root_ns->level); 2645c961ee5fSJohn Johansen error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy", 2646c961ee5fSJohn Johansen aafs_mnt->mnt_root); 2647b7fd2c03SJohn Johansen mutex_unlock(&root_ns->lock); 26480d259f04SJohn Johansen if (error) 26490d259f04SJohn Johansen goto error; 26500d259f04SJohn Johansen 2651c961ee5fSJohn Johansen /* magic symlink similar to nsfs redirects based on task policy */ 2652c961ee5fSJohn Johansen dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry, 2653c961ee5fSJohn Johansen NULL, &policy_link_iops); 2654cf916000SJohn Johansen if (IS_ERR(dent)) 2655cf916000SJohn Johansen goto dent_error; 2656c961ee5fSJohn Johansen 2657c97204baSJohn Johansen error = aa_mk_null_file(aa_sfs_entry.dentry); 2658a71ada30SJohn Johansen if (error) 2659a71ada30SJohn Johansen goto error; 2660a71ada30SJohn Johansen 2661a71ada30SJohn Johansen /* TODO: add default profile to apparmorfs */ 266263e2b423SJohn Johansen 266363e2b423SJohn Johansen /* Report that AppArmor fs is enabled */ 266463e2b423SJohn Johansen aa_info_message("AppArmor Filesystem Enabled"); 266563e2b423SJohn Johansen return 0; 266663e2b423SJohn Johansen 2667cf916000SJohn Johansen dent_error: 2668cf916000SJohn Johansen error = PTR_ERR(dent); 266963e2b423SJohn Johansen error: 267063e2b423SJohn Johansen aa_destroy_aafs(); 267163e2b423SJohn Johansen AA_ERROR("Error creating AppArmor securityfs\n"); 267263e2b423SJohn Johansen return error; 267363e2b423SJohn Johansen } 267463e2b423SJohn Johansen 267563e2b423SJohn Johansen fs_initcall(aa_create_aafs); 2676