163e2b423SJohn Johansen /* 263e2b423SJohn Johansen * AppArmor security module 363e2b423SJohn Johansen * 463e2b423SJohn Johansen * This file contains AppArmor /sys/kernel/security/apparmor interface functions 563e2b423SJohn Johansen * 663e2b423SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 763e2b423SJohn Johansen * Copyright 2009-2010 Canonical Ltd. 863e2b423SJohn Johansen * 963e2b423SJohn Johansen * This program is free software; you can redistribute it and/or 1063e2b423SJohn Johansen * modify it under the terms of the GNU General Public License as 1163e2b423SJohn Johansen * published by the Free Software Foundation, version 2 of the 1263e2b423SJohn Johansen * License. 1363e2b423SJohn Johansen */ 1463e2b423SJohn Johansen 1563e2b423SJohn Johansen #include <linux/security.h> 1663e2b423SJohn Johansen #include <linux/vmalloc.h> 1763e2b423SJohn Johansen #include <linux/module.h> 1863e2b423SJohn Johansen #include <linux/seq_file.h> 1963e2b423SJohn Johansen #include <linux/uaccess.h> 2063e2b423SJohn Johansen #include <linux/namei.h> 21e74abcf3SKees Cook #include <linux/capability.h> 2263e2b423SJohn Johansen 2363e2b423SJohn Johansen #include "include/apparmor.h" 2463e2b423SJohn Johansen #include "include/apparmorfs.h" 2563e2b423SJohn Johansen #include "include/audit.h" 2663e2b423SJohn Johansen #include "include/context.h" 2763e2b423SJohn Johansen #include "include/policy.h" 2863e2b423SJohn Johansen 2963e2b423SJohn Johansen /** 3063e2b423SJohn Johansen * aa_simple_write_to_buffer - common routine for getting policy from user 3163e2b423SJohn Johansen * @op: operation doing the user buffer copy 3263e2b423SJohn Johansen * @userbuf: user buffer to copy data from (NOT NULL) 333ed02adaSJohn Johansen * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 3463e2b423SJohn Johansen * @copy_size: size of data to copy from user buffer 3563e2b423SJohn Johansen * @pos: position write is at in the file (NOT NULL) 3663e2b423SJohn Johansen * 3763e2b423SJohn Johansen * Returns: kernel buffer containing copy of user buffer data or an 3863e2b423SJohn Johansen * ERR_PTR on failure. 3963e2b423SJohn Johansen */ 4063e2b423SJohn Johansen static char *aa_simple_write_to_buffer(int op, const char __user *userbuf, 4163e2b423SJohn Johansen size_t alloc_size, size_t copy_size, 4263e2b423SJohn Johansen loff_t *pos) 4363e2b423SJohn Johansen { 4463e2b423SJohn Johansen char *data; 4563e2b423SJohn Johansen 463ed02adaSJohn Johansen BUG_ON(copy_size > alloc_size); 473ed02adaSJohn Johansen 4863e2b423SJohn Johansen if (*pos != 0) 4963e2b423SJohn Johansen /* only writes from pos 0, that is complete writes */ 5063e2b423SJohn Johansen return ERR_PTR(-ESPIPE); 5163e2b423SJohn Johansen 5263e2b423SJohn Johansen /* 5363e2b423SJohn Johansen * Don't allow profile load/replace/remove from profiles that don't 5463e2b423SJohn Johansen * have CAP_MAC_ADMIN 5563e2b423SJohn Johansen */ 5663e2b423SJohn Johansen if (!aa_may_manage_policy(op)) 5763e2b423SJohn Johansen return ERR_PTR(-EACCES); 5863e2b423SJohn Johansen 5963e2b423SJohn Johansen /* freed by caller to simple_write_to_buffer */ 6063e2b423SJohn Johansen data = kvmalloc(alloc_size); 6163e2b423SJohn Johansen if (data == NULL) 6263e2b423SJohn Johansen return ERR_PTR(-ENOMEM); 6363e2b423SJohn Johansen 6463e2b423SJohn Johansen if (copy_from_user(data, userbuf, copy_size)) { 6563e2b423SJohn Johansen kvfree(data); 6663e2b423SJohn Johansen return ERR_PTR(-EFAULT); 6763e2b423SJohn Johansen } 6863e2b423SJohn Johansen 6963e2b423SJohn Johansen return data; 7063e2b423SJohn Johansen } 7163e2b423SJohn Johansen 7263e2b423SJohn Johansen 7363e2b423SJohn Johansen /* .load file hook fn to load policy */ 7463e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 7563e2b423SJohn Johansen loff_t *pos) 7663e2b423SJohn Johansen { 7763e2b423SJohn Johansen char *data; 7863e2b423SJohn Johansen ssize_t error; 7963e2b423SJohn Johansen 8063e2b423SJohn Johansen data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos); 8163e2b423SJohn Johansen 8263e2b423SJohn Johansen error = PTR_ERR(data); 8363e2b423SJohn Johansen if (!IS_ERR(data)) { 8463e2b423SJohn Johansen error = aa_replace_profiles(data, size, PROF_ADD); 8563e2b423SJohn Johansen kvfree(data); 8663e2b423SJohn Johansen } 8763e2b423SJohn Johansen 8863e2b423SJohn Johansen return error; 8963e2b423SJohn Johansen } 9063e2b423SJohn Johansen 9163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = { 926038f373SArnd Bergmann .write = profile_load, 936038f373SArnd Bergmann .llseek = default_llseek, 9463e2b423SJohn Johansen }; 9563e2b423SJohn Johansen 9663e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */ 9763e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf, 9863e2b423SJohn Johansen size_t size, loff_t *pos) 9963e2b423SJohn Johansen { 10063e2b423SJohn Johansen char *data; 10163e2b423SJohn Johansen ssize_t error; 10263e2b423SJohn Johansen 10363e2b423SJohn Johansen data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos); 10463e2b423SJohn Johansen error = PTR_ERR(data); 10563e2b423SJohn Johansen if (!IS_ERR(data)) { 10663e2b423SJohn Johansen error = aa_replace_profiles(data, size, PROF_REPLACE); 10763e2b423SJohn Johansen kvfree(data); 10863e2b423SJohn Johansen } 10963e2b423SJohn Johansen 11063e2b423SJohn Johansen return error; 11163e2b423SJohn Johansen } 11263e2b423SJohn Johansen 11363e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = { 1146038f373SArnd Bergmann .write = profile_replace, 1156038f373SArnd Bergmann .llseek = default_llseek, 11663e2b423SJohn Johansen }; 11763e2b423SJohn Johansen 11863e2b423SJohn Johansen /* .remove file hook fn to remove loaded policy */ 11963e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf, 12063e2b423SJohn Johansen size_t size, loff_t *pos) 12163e2b423SJohn Johansen { 12263e2b423SJohn Johansen char *data; 12363e2b423SJohn Johansen ssize_t error; 12463e2b423SJohn Johansen 12563e2b423SJohn Johansen /* 12663e2b423SJohn Johansen * aa_remove_profile needs a null terminated string so 1 extra 12763e2b423SJohn Johansen * byte is allocated and the copied data is null terminated. 12863e2b423SJohn Johansen */ 12963e2b423SJohn Johansen data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos); 13063e2b423SJohn Johansen 13163e2b423SJohn Johansen error = PTR_ERR(data); 13263e2b423SJohn Johansen if (!IS_ERR(data)) { 13363e2b423SJohn Johansen data[size] = 0; 13463e2b423SJohn Johansen error = aa_remove_profiles(data, size); 13563e2b423SJohn Johansen kvfree(data); 13663e2b423SJohn Johansen } 13763e2b423SJohn Johansen 13863e2b423SJohn Johansen return error; 13963e2b423SJohn Johansen } 14063e2b423SJohn Johansen 14163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = { 1426038f373SArnd Bergmann .write = profile_remove, 1436038f373SArnd Bergmann .llseek = default_llseek, 14463e2b423SJohn Johansen }; 14563e2b423SJohn Johansen 146e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v) 147e74abcf3SKees Cook { 148e74abcf3SKees Cook struct aa_fs_entry *fs_file = seq->private; 149e74abcf3SKees Cook 150e74abcf3SKees Cook if (!fs_file) 151e74abcf3SKees Cook return 0; 152e74abcf3SKees Cook 153e74abcf3SKees Cook switch (fs_file->v_type) { 154e74abcf3SKees Cook case AA_FS_TYPE_BOOLEAN: 155e74abcf3SKees Cook seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); 156e74abcf3SKees Cook break; 157*a9bf8e9fSKees Cook case AA_FS_TYPE_STRING: 158*a9bf8e9fSKees Cook seq_printf(seq, "%s\n", fs_file->v.string); 159*a9bf8e9fSKees Cook break; 160e74abcf3SKees Cook case AA_FS_TYPE_U64: 161e74abcf3SKees Cook seq_printf(seq, "%#08lx\n", fs_file->v.u64); 162e74abcf3SKees Cook break; 163e74abcf3SKees Cook default: 164e74abcf3SKees Cook /* Ignore unpritable entry types. */ 165e74abcf3SKees Cook break; 166e74abcf3SKees Cook } 167e74abcf3SKees Cook 168e74abcf3SKees Cook return 0; 169e74abcf3SKees Cook } 170e74abcf3SKees Cook 171e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file) 172e74abcf3SKees Cook { 173e74abcf3SKees Cook return single_open(file, aa_fs_seq_show, inode->i_private); 174e74abcf3SKees Cook } 175e74abcf3SKees Cook 176e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = { 177e74abcf3SKees Cook .owner = THIS_MODULE, 178e74abcf3SKees Cook .open = aa_fs_seq_open, 179e74abcf3SKees Cook .read = seq_read, 180e74abcf3SKees Cook .llseek = seq_lseek, 181e74abcf3SKees Cook .release = single_release, 182e74abcf3SKees Cook }; 183e74abcf3SKees Cook 18463e2b423SJohn Johansen /** Base file system setup **/ 18563e2b423SJohn Johansen 186*a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = { 187*a9bf8e9fSKees Cook AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \ 188*a9bf8e9fSKees Cook "link lock"), 189*a9bf8e9fSKees Cook { } 190*a9bf8e9fSKees Cook }; 191*a9bf8e9fSKees Cook 192e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = { 193e74abcf3SKees Cook AA_FS_FILE_BOOLEAN("change_hat", 1), 194e74abcf3SKees Cook AA_FS_FILE_BOOLEAN("change_hatv", 1), 195e74abcf3SKees Cook AA_FS_FILE_BOOLEAN("change_onexec", 1), 196e74abcf3SKees Cook AA_FS_FILE_BOOLEAN("change_profile", 1), 197e74abcf3SKees Cook { } 198e74abcf3SKees Cook }; 199e74abcf3SKees Cook 200e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = { 201e74abcf3SKees Cook AA_FS_DIR("domain", aa_fs_entry_domain), 202*a9bf8e9fSKees Cook AA_FS_DIR("file", aa_fs_entry_file), 203e74abcf3SKees Cook AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 204e74abcf3SKees Cook { } 205e74abcf3SKees Cook }; 206e74abcf3SKees Cook 2079acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = { 2089acd494bSKees Cook AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load), 2099acd494bSKees Cook AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace), 2109acd494bSKees Cook AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove), 211e74abcf3SKees Cook AA_FS_DIR("features", aa_fs_entry_features), 2129acd494bSKees Cook { } 2139acd494bSKees Cook }; 21463e2b423SJohn Johansen 2159acd494bSKees Cook static struct aa_fs_entry aa_fs_entry = 2169acd494bSKees Cook AA_FS_DIR("apparmor", aa_fs_entry_apparmor); 2179acd494bSKees Cook 2189acd494bSKees Cook /** 2199acd494bSKees Cook * aafs_create_file - create a file entry in the apparmor securityfs 2209acd494bSKees Cook * @fs_file: aa_fs_entry to build an entry for (NOT NULL) 2219acd494bSKees Cook * @parent: the parent dentry in the securityfs 2229acd494bSKees Cook * 2239acd494bSKees Cook * Use aafs_remove_file to remove entries created with this fn. 2249acd494bSKees Cook */ 2259acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file, 2269acd494bSKees Cook struct dentry *parent) 22763e2b423SJohn Johansen { 2289acd494bSKees Cook int error = 0; 22963e2b423SJohn Johansen 2309acd494bSKees Cook fs_file->dentry = securityfs_create_file(fs_file->name, 2319acd494bSKees Cook S_IFREG | fs_file->mode, 2329acd494bSKees Cook parent, fs_file, 2339acd494bSKees Cook fs_file->file_ops); 2349acd494bSKees Cook if (IS_ERR(fs_file->dentry)) { 2359acd494bSKees Cook error = PTR_ERR(fs_file->dentry); 2369acd494bSKees Cook fs_file->dentry = NULL; 23763e2b423SJohn Johansen } 2389acd494bSKees Cook return error; 23963e2b423SJohn Johansen } 24063e2b423SJohn Johansen 24163e2b423SJohn Johansen /** 2429acd494bSKees Cook * aafs_create_dir - recursively create a directory entry in the securityfs 2439acd494bSKees Cook * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL) 2449acd494bSKees Cook * @parent: the parent dentry in the securityfs 24563e2b423SJohn Johansen * 2469acd494bSKees Cook * Use aafs_remove_dir to remove entries created with this fn. 24763e2b423SJohn Johansen */ 2489acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir, 2499acd494bSKees Cook struct dentry *parent) 25063e2b423SJohn Johansen { 2519acd494bSKees Cook int error; 2529acd494bSKees Cook struct aa_fs_entry *fs_file; 25363e2b423SJohn Johansen 2549acd494bSKees Cook fs_dir->dentry = securityfs_create_dir(fs_dir->name, parent); 2559acd494bSKees Cook if (IS_ERR(fs_dir->dentry)) { 2569acd494bSKees Cook error = PTR_ERR(fs_dir->dentry); 2579acd494bSKees Cook fs_dir->dentry = NULL; 2589acd494bSKees Cook goto failed; 2599acd494bSKees Cook } 26063e2b423SJohn Johansen 2619acd494bSKees Cook for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) { 2629acd494bSKees Cook if (fs_file->v_type == AA_FS_TYPE_DIR) 2639acd494bSKees Cook error = aafs_create_dir(fs_file, fs_dir->dentry); 2649acd494bSKees Cook else 2659acd494bSKees Cook error = aafs_create_file(fs_file, fs_dir->dentry); 2669acd494bSKees Cook if (error) 2679acd494bSKees Cook goto failed; 2689acd494bSKees Cook } 2699acd494bSKees Cook 2709acd494bSKees Cook return 0; 2719acd494bSKees Cook 2729acd494bSKees Cook failed: 2739acd494bSKees Cook return error; 2749acd494bSKees Cook } 2759acd494bSKees Cook 2769acd494bSKees Cook /** 2779acd494bSKees Cook * aafs_remove_file - drop a single file entry in the apparmor securityfs 2789acd494bSKees Cook * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL) 2799acd494bSKees Cook */ 2809acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file) 2819acd494bSKees Cook { 2829acd494bSKees Cook if (!fs_file->dentry) 2839acd494bSKees Cook return; 2849acd494bSKees Cook 2859acd494bSKees Cook securityfs_remove(fs_file->dentry); 2869acd494bSKees Cook fs_file->dentry = NULL; 2879acd494bSKees Cook } 2889acd494bSKees Cook 2899acd494bSKees Cook /** 2909acd494bSKees Cook * aafs_remove_dir - recursively drop a directory entry from the securityfs 2919acd494bSKees Cook * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL) 2929acd494bSKees Cook */ 2939acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir) 2949acd494bSKees Cook { 2959acd494bSKees Cook struct aa_fs_entry *fs_file; 2969acd494bSKees Cook 2979acd494bSKees Cook for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) { 2989acd494bSKees Cook if (fs_file->v_type == AA_FS_TYPE_DIR) 2999acd494bSKees Cook aafs_remove_dir(fs_file); 3009acd494bSKees Cook else 3019acd494bSKees Cook aafs_remove_file(fs_file); 3029acd494bSKees Cook } 3039acd494bSKees Cook 3049acd494bSKees Cook aafs_remove_file(fs_dir); 30563e2b423SJohn Johansen } 30663e2b423SJohn Johansen 30763e2b423SJohn Johansen /** 30863e2b423SJohn Johansen * aa_destroy_aafs - cleanup and free aafs 30963e2b423SJohn Johansen * 31063e2b423SJohn Johansen * releases dentries allocated by aa_create_aafs 31163e2b423SJohn Johansen */ 31263e2b423SJohn Johansen void __init aa_destroy_aafs(void) 31363e2b423SJohn Johansen { 3149acd494bSKees Cook aafs_remove_dir(&aa_fs_entry); 31563e2b423SJohn Johansen } 31663e2b423SJohn Johansen 31763e2b423SJohn Johansen /** 31863e2b423SJohn Johansen * aa_create_aafs - create the apparmor security filesystem 31963e2b423SJohn Johansen * 32063e2b423SJohn Johansen * dentries created here are released by aa_destroy_aafs 32163e2b423SJohn Johansen * 32263e2b423SJohn Johansen * Returns: error on failure 32363e2b423SJohn Johansen */ 3243417d8d5SJames Morris static int __init aa_create_aafs(void) 32563e2b423SJohn Johansen { 32663e2b423SJohn Johansen int error; 32763e2b423SJohn Johansen 32863e2b423SJohn Johansen if (!apparmor_initialized) 32963e2b423SJohn Johansen return 0; 33063e2b423SJohn Johansen 3319acd494bSKees Cook if (aa_fs_entry.dentry) { 33263e2b423SJohn Johansen AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 33363e2b423SJohn Johansen return -EEXIST; 33463e2b423SJohn Johansen } 33563e2b423SJohn Johansen 3369acd494bSKees Cook /* Populate fs tree. */ 3379acd494bSKees Cook error = aafs_create_dir(&aa_fs_entry, NULL); 33863e2b423SJohn Johansen if (error) 33963e2b423SJohn Johansen goto error; 34063e2b423SJohn Johansen 34163e2b423SJohn Johansen /* TODO: add support for apparmorfs_null and apparmorfs_mnt */ 34263e2b423SJohn Johansen 34363e2b423SJohn Johansen /* Report that AppArmor fs is enabled */ 34463e2b423SJohn Johansen aa_info_message("AppArmor Filesystem Enabled"); 34563e2b423SJohn Johansen return 0; 34663e2b423SJohn Johansen 34763e2b423SJohn Johansen error: 34863e2b423SJohn Johansen aa_destroy_aafs(); 34963e2b423SJohn Johansen AA_ERROR("Error creating AppArmor securityfs\n"); 35063e2b423SJohn Johansen return error; 35163e2b423SJohn Johansen } 35263e2b423SJohn Johansen 35363e2b423SJohn Johansen fs_initcall(aa_create_aafs); 354