1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f381c272SMimi Zohar /* 3f381c272SMimi Zohar * Copyright (C) 2008 IBM Corporation 4f381c272SMimi Zohar * 5f381c272SMimi Zohar * Authors: 6f381c272SMimi Zohar * Mimi Zohar <zohar@us.ibm.com> 7f381c272SMimi Zohar * 8f381c272SMimi Zohar * File: integrity_iint.c 9f381c272SMimi Zohar * - implements the integrity hooks: integrity_inode_alloc, 10f381c272SMimi Zohar * integrity_inode_free 11f381c272SMimi Zohar * - cache integrity information associated with an inode 12f381c272SMimi Zohar * using a rbtree tree. 13f381c272SMimi Zohar */ 14f381c272SMimi Zohar #include <linux/slab.h> 15876979c9SPaul Gortmaker #include <linux/init.h> 16f381c272SMimi Zohar #include <linux/spinlock.h> 17f381c272SMimi Zohar #include <linux/rbtree.h> 18e3c4abbfSDmitry Kasatkin #include <linux/file.h> 19e3c4abbfSDmitry Kasatkin #include <linux/uaccess.h> 200c343af8SMatthew Garrett #include <linux/security.h> 215b89c1bdSKees Cook #include <linux/lsm_hooks.h> 22f381c272SMimi Zohar #include "integrity.h" 23f381c272SMimi Zohar 24f381c272SMimi Zohar static struct rb_root integrity_iint_tree = RB_ROOT; 25a10bf26bSDmitry Kasatkin static DEFINE_RWLOCK(integrity_iint_lock); 26f381c272SMimi Zohar static struct kmem_cache *iint_cache __read_mostly; 27f381c272SMimi Zohar 280c343af8SMatthew Garrett struct dentry *integrity_dir; 290c343af8SMatthew Garrett 30f381c272SMimi Zohar /* 31f381c272SMimi Zohar * __integrity_iint_find - return the iint associated with an inode 32f381c272SMimi Zohar */ 33f381c272SMimi Zohar static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode) 34f381c272SMimi Zohar { 35f381c272SMimi Zohar struct integrity_iint_cache *iint; 36f381c272SMimi Zohar struct rb_node *n = integrity_iint_tree.rb_node; 37f381c272SMimi Zohar 38f381c272SMimi Zohar while (n) { 39f381c272SMimi Zohar iint = rb_entry(n, struct integrity_iint_cache, rb_node); 40f381c272SMimi Zohar 41f381c272SMimi Zohar if (inode < iint->inode) 42f381c272SMimi Zohar n = n->rb_left; 43f381c272SMimi Zohar else if (inode > iint->inode) 44f381c272SMimi Zohar n = n->rb_right; 45f381c272SMimi Zohar else 46f381c272SMimi Zohar break; 47f381c272SMimi Zohar } 48f381c272SMimi Zohar if (!n) 49f381c272SMimi Zohar return NULL; 50f381c272SMimi Zohar 51f381c272SMimi Zohar return iint; 52f381c272SMimi Zohar } 53f381c272SMimi Zohar 54f381c272SMimi Zohar /* 55f381c272SMimi Zohar * integrity_iint_find - return the iint associated with an inode 56f381c272SMimi Zohar */ 57f381c272SMimi Zohar struct integrity_iint_cache *integrity_iint_find(struct inode *inode) 58f381c272SMimi Zohar { 59f381c272SMimi Zohar struct integrity_iint_cache *iint; 60f381c272SMimi Zohar 61f381c272SMimi Zohar if (!IS_IMA(inode)) 62f381c272SMimi Zohar return NULL; 63f381c272SMimi Zohar 64a10bf26bSDmitry Kasatkin read_lock(&integrity_iint_lock); 65f381c272SMimi Zohar iint = __integrity_iint_find(inode); 66a10bf26bSDmitry Kasatkin read_unlock(&integrity_iint_lock); 67f381c272SMimi Zohar 68f381c272SMimi Zohar return iint; 69f381c272SMimi Zohar } 70f381c272SMimi Zohar 71f381c272SMimi Zohar static void iint_free(struct integrity_iint_cache *iint) 72f381c272SMimi Zohar { 73a35c3fb6SDmitry Kasatkin kfree(iint->ima_hash); 74a35c3fb6SDmitry Kasatkin iint->ima_hash = NULL; 75f381c272SMimi Zohar iint->version = 0; 76f381c272SMimi Zohar iint->flags = 0UL; 77e2598077SMimi Zohar iint->atomic_flags = 0UL; 78d79d72e0SMimi Zohar iint->ima_file_status = INTEGRITY_UNKNOWN; 79d79d72e0SMimi Zohar iint->ima_mmap_status = INTEGRITY_UNKNOWN; 80d79d72e0SMimi Zohar iint->ima_bprm_status = INTEGRITY_UNKNOWN; 81c6af8efeSMimi Zohar iint->ima_read_status = INTEGRITY_UNKNOWN; 82d906c10dSMatthew Garrett iint->ima_creds_status = INTEGRITY_UNKNOWN; 83fb788d8bSDmitry Kasatkin iint->evm_status = INTEGRITY_UNKNOWN; 8496d450bbSEric Richter iint->measured_pcrs = 0; 85f381c272SMimi Zohar kmem_cache_free(iint_cache, iint); 86f381c272SMimi Zohar } 87f381c272SMimi Zohar 88f381c272SMimi Zohar /** 89bf2276d1SDmitry Kasatkin * integrity_inode_get - find or allocate an iint associated with an inode 90f381c272SMimi Zohar * @inode: pointer to the inode 91bf2276d1SDmitry Kasatkin * @return: allocated iint 92bf2276d1SDmitry Kasatkin * 93bf2276d1SDmitry Kasatkin * Caller must lock i_mutex 94f381c272SMimi Zohar */ 95bf2276d1SDmitry Kasatkin struct integrity_iint_cache *integrity_inode_get(struct inode *inode) 96f381c272SMimi Zohar { 97f381c272SMimi Zohar struct rb_node **p; 98bf2276d1SDmitry Kasatkin struct rb_node *node, *parent = NULL; 99bf2276d1SDmitry Kasatkin struct integrity_iint_cache *iint, *test_iint; 100f381c272SMimi Zohar 101bf2276d1SDmitry Kasatkin iint = integrity_iint_find(inode); 102bf2276d1SDmitry Kasatkin if (iint) 103bf2276d1SDmitry Kasatkin return iint; 104f381c272SMimi Zohar 105bf2276d1SDmitry Kasatkin iint = kmem_cache_alloc(iint_cache, GFP_NOFS); 106bf2276d1SDmitry Kasatkin if (!iint) 107bf2276d1SDmitry Kasatkin return NULL; 108f381c272SMimi Zohar 109a10bf26bSDmitry Kasatkin write_lock(&integrity_iint_lock); 110f381c272SMimi Zohar 111f381c272SMimi Zohar p = &integrity_iint_tree.rb_node; 112f381c272SMimi Zohar while (*p) { 113f381c272SMimi Zohar parent = *p; 114f381c272SMimi Zohar test_iint = rb_entry(parent, struct integrity_iint_cache, 115f381c272SMimi Zohar rb_node); 116f381c272SMimi Zohar if (inode < test_iint->inode) 117f381c272SMimi Zohar p = &(*p)->rb_left; 118f381c272SMimi Zohar else 119bf2276d1SDmitry Kasatkin p = &(*p)->rb_right; 120f381c272SMimi Zohar } 121f381c272SMimi Zohar 122bf2276d1SDmitry Kasatkin iint->inode = inode; 123bf2276d1SDmitry Kasatkin node = &iint->rb_node; 124f381c272SMimi Zohar inode->i_flags |= S_IMA; 125bf2276d1SDmitry Kasatkin rb_link_node(node, parent, p); 126bf2276d1SDmitry Kasatkin rb_insert_color(node, &integrity_iint_tree); 127f381c272SMimi Zohar 128a10bf26bSDmitry Kasatkin write_unlock(&integrity_iint_lock); 129bf2276d1SDmitry Kasatkin return iint; 130f381c272SMimi Zohar } 131f381c272SMimi Zohar 132f381c272SMimi Zohar /** 133f381c272SMimi Zohar * integrity_inode_free - called on security_inode_free 134f381c272SMimi Zohar * @inode: pointer to the inode 135f381c272SMimi Zohar * 136f381c272SMimi Zohar * Free the integrity information(iint) associated with an inode. 137f381c272SMimi Zohar */ 138f381c272SMimi Zohar void integrity_inode_free(struct inode *inode) 139f381c272SMimi Zohar { 140f381c272SMimi Zohar struct integrity_iint_cache *iint; 141f381c272SMimi Zohar 142f381c272SMimi Zohar if (!IS_IMA(inode)) 143f381c272SMimi Zohar return; 144f381c272SMimi Zohar 145a10bf26bSDmitry Kasatkin write_lock(&integrity_iint_lock); 146f381c272SMimi Zohar iint = __integrity_iint_find(inode); 147f381c272SMimi Zohar rb_erase(&iint->rb_node, &integrity_iint_tree); 148a10bf26bSDmitry Kasatkin write_unlock(&integrity_iint_lock); 149f381c272SMimi Zohar 150f381c272SMimi Zohar iint_free(iint); 151f381c272SMimi Zohar } 152f381c272SMimi Zohar 153f381c272SMimi Zohar static void init_once(void *foo) 154f381c272SMimi Zohar { 155f381c272SMimi Zohar struct integrity_iint_cache *iint = foo; 156f381c272SMimi Zohar 1572bb930abSDmitry Kasatkin memset(iint, 0, sizeof(*iint)); 158d79d72e0SMimi Zohar iint->ima_file_status = INTEGRITY_UNKNOWN; 159d79d72e0SMimi Zohar iint->ima_mmap_status = INTEGRITY_UNKNOWN; 160d79d72e0SMimi Zohar iint->ima_bprm_status = INTEGRITY_UNKNOWN; 161c6af8efeSMimi Zohar iint->ima_read_status = INTEGRITY_UNKNOWN; 162d906c10dSMatthew Garrett iint->ima_creds_status = INTEGRITY_UNKNOWN; 16324e0198eSDmitry Kasatkin iint->evm_status = INTEGRITY_UNKNOWN; 1640d73a552SDmitry Kasatkin mutex_init(&iint->mutex); 165f381c272SMimi Zohar } 166f381c272SMimi Zohar 167f381c272SMimi Zohar static int __init integrity_iintcache_init(void) 168f381c272SMimi Zohar { 169f381c272SMimi Zohar iint_cache = 170f381c272SMimi Zohar kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache), 171f381c272SMimi Zohar 0, SLAB_PANIC, init_once); 172f381c272SMimi Zohar return 0; 173f381c272SMimi Zohar } 1743d6e5f6dSKees Cook DEFINE_LSM(integrity) = { 17507aed2f2SKees Cook .name = "integrity", 1763d6e5f6dSKees Cook .init = integrity_iintcache_init, 1773d6e5f6dSKees Cook }; 178e3c4abbfSDmitry Kasatkin 179e3c4abbfSDmitry Kasatkin 180e3c4abbfSDmitry Kasatkin /* 181e3c4abbfSDmitry Kasatkin * integrity_kernel_read - read data from the file 182e3c4abbfSDmitry Kasatkin * 183e3c4abbfSDmitry Kasatkin * This is a function for reading file content instead of kernel_read(). 184e3c4abbfSDmitry Kasatkin * It does not perform locking checks to ensure it cannot be blocked. 185e3c4abbfSDmitry Kasatkin * It does not perform security checks because it is irrelevant for IMA. 186e3c4abbfSDmitry Kasatkin * 187e3c4abbfSDmitry Kasatkin */ 188e3c4abbfSDmitry Kasatkin int integrity_kernel_read(struct file *file, loff_t offset, 189bb543e39SThiago Jung Bauermann void *addr, unsigned long count) 190e3c4abbfSDmitry Kasatkin { 191e3c4abbfSDmitry Kasatkin mm_segment_t old_fs; 192e3c4abbfSDmitry Kasatkin char __user *buf = (char __user *)addr; 1936fb5032eSDmitry Kasatkin ssize_t ret; 194e3c4abbfSDmitry Kasatkin 195e3c4abbfSDmitry Kasatkin if (!(file->f_mode & FMODE_READ)) 196e3c4abbfSDmitry Kasatkin return -EBADF; 197e3c4abbfSDmitry Kasatkin 198e3c4abbfSDmitry Kasatkin old_fs = get_fs(); 199736706beSLinus Torvalds set_fs(KERNEL_DS); 2006fb5032eSDmitry Kasatkin ret = __vfs_read(file, buf, count, &offset); 201e3c4abbfSDmitry Kasatkin set_fs(old_fs); 2026fb5032eSDmitry Kasatkin 203e3c4abbfSDmitry Kasatkin return ret; 204e3c4abbfSDmitry Kasatkin } 205e3c4abbfSDmitry Kasatkin 206e3c4abbfSDmitry Kasatkin /* 207c9cd2ce2SDmitry Kasatkin * integrity_load_keys - load integrity keys hook 208c9cd2ce2SDmitry Kasatkin * 209c9cd2ce2SDmitry Kasatkin * Hooks is called from init/main.c:kernel_init_freeable() 210c9cd2ce2SDmitry Kasatkin * when rootfs is ready 211c9cd2ce2SDmitry Kasatkin */ 212c9cd2ce2SDmitry Kasatkin void __init integrity_load_keys(void) 213c9cd2ce2SDmitry Kasatkin { 214c9cd2ce2SDmitry Kasatkin ima_load_x509(); 2152ce523ebSDmitry Kasatkin evm_load_x509(); 216c9cd2ce2SDmitry Kasatkin } 2170c343af8SMatthew Garrett 2180c343af8SMatthew Garrett static int __init integrity_fs_init(void) 2190c343af8SMatthew Garrett { 2200c343af8SMatthew Garrett integrity_dir = securityfs_create_dir("integrity", NULL); 2210c343af8SMatthew Garrett if (IS_ERR(integrity_dir)) { 222ac2409a5SSudeep Holla int ret = PTR_ERR(integrity_dir); 223ac2409a5SSudeep Holla 224ac2409a5SSudeep Holla if (ret != -ENODEV) 225ac2409a5SSudeep Holla pr_err("Unable to create integrity sysfs dir: %d\n", 226ac2409a5SSudeep Holla ret); 2270c343af8SMatthew Garrett integrity_dir = NULL; 228ac2409a5SSudeep Holla return ret; 2290c343af8SMatthew Garrett } 2300c343af8SMatthew Garrett 2310c343af8SMatthew Garrett return 0; 2320c343af8SMatthew Garrett } 2330c343af8SMatthew Garrett 2340c343af8SMatthew Garrett late_initcall(integrity_fs_init) 235