1f381c272SMimi Zohar /* 2f381c272SMimi Zohar * Copyright (C) 2008 IBM Corporation 3f381c272SMimi Zohar * 4f381c272SMimi Zohar * Authors: 5f381c272SMimi Zohar * Mimi Zohar <zohar@us.ibm.com> 6f381c272SMimi Zohar * 7f381c272SMimi Zohar * This program is free software; you can redistribute it and/or 8f381c272SMimi Zohar * modify it under the terms of the GNU General Public License as 9f381c272SMimi Zohar * published by the Free Software Foundation, version 2 of the 10f381c272SMimi Zohar * License. 11f381c272SMimi Zohar * 12f381c272SMimi Zohar * File: integrity_iint.c 13f381c272SMimi Zohar * - implements the integrity hooks: integrity_inode_alloc, 14f381c272SMimi Zohar * integrity_inode_free 15f381c272SMimi Zohar * - cache integrity information associated with an inode 16f381c272SMimi Zohar * using a rbtree tree. 17f381c272SMimi Zohar */ 18f381c272SMimi Zohar #include <linux/slab.h> 19f381c272SMimi Zohar #include <linux/module.h> 20f381c272SMimi Zohar #include <linux/spinlock.h> 21f381c272SMimi Zohar #include <linux/rbtree.h> 22e3c4abbfSDmitry Kasatkin #include <linux/file.h> 23e3c4abbfSDmitry Kasatkin #include <linux/uaccess.h> 24f381c272SMimi Zohar #include "integrity.h" 25f381c272SMimi Zohar 26f381c272SMimi Zohar static struct rb_root integrity_iint_tree = RB_ROOT; 27a10bf26bSDmitry Kasatkin static DEFINE_RWLOCK(integrity_iint_lock); 28f381c272SMimi Zohar static struct kmem_cache *iint_cache __read_mostly; 29f381c272SMimi Zohar 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; 77d79d72e0SMimi Zohar iint->ima_file_status = INTEGRITY_UNKNOWN; 78d79d72e0SMimi Zohar iint->ima_mmap_status = INTEGRITY_UNKNOWN; 79d79d72e0SMimi Zohar iint->ima_bprm_status = INTEGRITY_UNKNOWN; 80c6af8efeSMimi Zohar iint->ima_read_status = INTEGRITY_UNKNOWN; 81fb788d8bSDmitry Kasatkin iint->evm_status = INTEGRITY_UNKNOWN; 8296d450bbSEric Richter iint->measured_pcrs = 0; 83f381c272SMimi Zohar kmem_cache_free(iint_cache, iint); 84f381c272SMimi Zohar } 85f381c272SMimi Zohar 86f381c272SMimi Zohar /** 87bf2276d1SDmitry Kasatkin * integrity_inode_get - find or allocate an iint associated with an inode 88f381c272SMimi Zohar * @inode: pointer to the inode 89bf2276d1SDmitry Kasatkin * @return: allocated iint 90bf2276d1SDmitry Kasatkin * 91bf2276d1SDmitry Kasatkin * Caller must lock i_mutex 92f381c272SMimi Zohar */ 93bf2276d1SDmitry Kasatkin struct integrity_iint_cache *integrity_inode_get(struct inode *inode) 94f381c272SMimi Zohar { 95f381c272SMimi Zohar struct rb_node **p; 96bf2276d1SDmitry Kasatkin struct rb_node *node, *parent = NULL; 97bf2276d1SDmitry Kasatkin struct integrity_iint_cache *iint, *test_iint; 98f381c272SMimi Zohar 99bf2276d1SDmitry Kasatkin iint = integrity_iint_find(inode); 100bf2276d1SDmitry Kasatkin if (iint) 101bf2276d1SDmitry Kasatkin return iint; 102f381c272SMimi Zohar 103bf2276d1SDmitry Kasatkin iint = kmem_cache_alloc(iint_cache, GFP_NOFS); 104bf2276d1SDmitry Kasatkin if (!iint) 105bf2276d1SDmitry Kasatkin return NULL; 106f381c272SMimi Zohar 107a10bf26bSDmitry Kasatkin write_lock(&integrity_iint_lock); 108f381c272SMimi Zohar 109f381c272SMimi Zohar p = &integrity_iint_tree.rb_node; 110f381c272SMimi Zohar while (*p) { 111f381c272SMimi Zohar parent = *p; 112f381c272SMimi Zohar test_iint = rb_entry(parent, struct integrity_iint_cache, 113f381c272SMimi Zohar rb_node); 114f381c272SMimi Zohar if (inode < test_iint->inode) 115f381c272SMimi Zohar p = &(*p)->rb_left; 116f381c272SMimi Zohar else 117bf2276d1SDmitry Kasatkin p = &(*p)->rb_right; 118f381c272SMimi Zohar } 119f381c272SMimi Zohar 120bf2276d1SDmitry Kasatkin iint->inode = inode; 121bf2276d1SDmitry Kasatkin node = &iint->rb_node; 122f381c272SMimi Zohar inode->i_flags |= S_IMA; 123bf2276d1SDmitry Kasatkin rb_link_node(node, parent, p); 124bf2276d1SDmitry Kasatkin rb_insert_color(node, &integrity_iint_tree); 125f381c272SMimi Zohar 126a10bf26bSDmitry Kasatkin write_unlock(&integrity_iint_lock); 127bf2276d1SDmitry Kasatkin return iint; 128f381c272SMimi Zohar } 129f381c272SMimi Zohar 130f381c272SMimi Zohar /** 131f381c272SMimi Zohar * integrity_inode_free - called on security_inode_free 132f381c272SMimi Zohar * @inode: pointer to the inode 133f381c272SMimi Zohar * 134f381c272SMimi Zohar * Free the integrity information(iint) associated with an inode. 135f381c272SMimi Zohar */ 136f381c272SMimi Zohar void integrity_inode_free(struct inode *inode) 137f381c272SMimi Zohar { 138f381c272SMimi Zohar struct integrity_iint_cache *iint; 139f381c272SMimi Zohar 140f381c272SMimi Zohar if (!IS_IMA(inode)) 141f381c272SMimi Zohar return; 142f381c272SMimi Zohar 143a10bf26bSDmitry Kasatkin write_lock(&integrity_iint_lock); 144f381c272SMimi Zohar iint = __integrity_iint_find(inode); 145f381c272SMimi Zohar rb_erase(&iint->rb_node, &integrity_iint_tree); 146a10bf26bSDmitry Kasatkin write_unlock(&integrity_iint_lock); 147f381c272SMimi Zohar 148f381c272SMimi Zohar iint_free(iint); 149f381c272SMimi Zohar } 150f381c272SMimi Zohar 151f381c272SMimi Zohar static void init_once(void *foo) 152f381c272SMimi Zohar { 153f381c272SMimi Zohar struct integrity_iint_cache *iint = foo; 154f381c272SMimi Zohar 1552bb930abSDmitry Kasatkin memset(iint, 0, sizeof(*iint)); 156f381c272SMimi Zohar iint->version = 0; 157f381c272SMimi Zohar iint->flags = 0UL; 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; 16224e0198eSDmitry Kasatkin iint->evm_status = INTEGRITY_UNKNOWN; 16396d450bbSEric Richter iint->measured_pcrs = 0; 164f381c272SMimi Zohar } 165f381c272SMimi Zohar 166f381c272SMimi Zohar static int __init integrity_iintcache_init(void) 167f381c272SMimi Zohar { 168f381c272SMimi Zohar iint_cache = 169f381c272SMimi Zohar kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache), 170f381c272SMimi Zohar 0, SLAB_PANIC, init_once); 171f381c272SMimi Zohar return 0; 172f381c272SMimi Zohar } 173f381c272SMimi Zohar security_initcall(integrity_iintcache_init); 174e3c4abbfSDmitry Kasatkin 175e3c4abbfSDmitry Kasatkin 176e3c4abbfSDmitry Kasatkin /* 177e3c4abbfSDmitry Kasatkin * integrity_kernel_read - read data from the file 178e3c4abbfSDmitry Kasatkin * 179e3c4abbfSDmitry Kasatkin * This is a function for reading file content instead of kernel_read(). 180e3c4abbfSDmitry Kasatkin * It does not perform locking checks to ensure it cannot be blocked. 181e3c4abbfSDmitry Kasatkin * It does not perform security checks because it is irrelevant for IMA. 182e3c4abbfSDmitry Kasatkin * 183e3c4abbfSDmitry Kasatkin */ 184e3c4abbfSDmitry Kasatkin int integrity_kernel_read(struct file *file, loff_t offset, 185bb543e39SThiago Jung Bauermann void *addr, unsigned long count) 186e3c4abbfSDmitry Kasatkin { 187e3c4abbfSDmitry Kasatkin mm_segment_t old_fs; 188e3c4abbfSDmitry Kasatkin char __user *buf = (char __user *)addr; 1896fb5032eSDmitry Kasatkin ssize_t ret; 190e3c4abbfSDmitry Kasatkin 191e3c4abbfSDmitry Kasatkin if (!(file->f_mode & FMODE_READ)) 192e3c4abbfSDmitry Kasatkin return -EBADF; 193e3c4abbfSDmitry Kasatkin 194e3c4abbfSDmitry Kasatkin old_fs = get_fs(); 195e3c4abbfSDmitry Kasatkin set_fs(get_ds()); 1966fb5032eSDmitry Kasatkin ret = __vfs_read(file, buf, count, &offset); 197e3c4abbfSDmitry Kasatkin set_fs(old_fs); 1986fb5032eSDmitry Kasatkin 199e3c4abbfSDmitry Kasatkin return ret; 200e3c4abbfSDmitry Kasatkin } 201e3c4abbfSDmitry Kasatkin 202e3c4abbfSDmitry Kasatkin /* 203e3c4abbfSDmitry Kasatkin * integrity_read_file - read entire file content into the buffer 204e3c4abbfSDmitry Kasatkin * 205e3c4abbfSDmitry Kasatkin * This is function opens a file, allocates the buffer of required 206e3c4abbfSDmitry Kasatkin * size, read entire file content to the buffer and closes the file 207e3c4abbfSDmitry Kasatkin * 208e3c4abbfSDmitry Kasatkin * It is used only by init code. 209e3c4abbfSDmitry Kasatkin * 210e3c4abbfSDmitry Kasatkin */ 211e3c4abbfSDmitry Kasatkin int __init integrity_read_file(const char *path, char **data) 212e3c4abbfSDmitry Kasatkin { 213e3c4abbfSDmitry Kasatkin struct file *file; 214e3c4abbfSDmitry Kasatkin loff_t size; 215e3c4abbfSDmitry Kasatkin char *buf; 216e3c4abbfSDmitry Kasatkin int rc = -EINVAL; 217e3c4abbfSDmitry Kasatkin 2189d03a721SDmitry Kasatkin if (!path || !*path) 2199d03a721SDmitry Kasatkin return -EINVAL; 2209d03a721SDmitry Kasatkin 221e3c4abbfSDmitry Kasatkin file = filp_open(path, O_RDONLY, 0); 222e3c4abbfSDmitry Kasatkin if (IS_ERR(file)) { 223e3c4abbfSDmitry Kasatkin rc = PTR_ERR(file); 224e3c4abbfSDmitry Kasatkin pr_err("Unable to open file: %s (%d)", path, rc); 225e3c4abbfSDmitry Kasatkin return rc; 226e3c4abbfSDmitry Kasatkin } 227e3c4abbfSDmitry Kasatkin 228e3c4abbfSDmitry Kasatkin size = i_size_read(file_inode(file)); 229e3c4abbfSDmitry Kasatkin if (size <= 0) 230e3c4abbfSDmitry Kasatkin goto out; 231e3c4abbfSDmitry Kasatkin 232e3c4abbfSDmitry Kasatkin buf = kmalloc(size, GFP_KERNEL); 233e3c4abbfSDmitry Kasatkin if (!buf) { 234e3c4abbfSDmitry Kasatkin rc = -ENOMEM; 235e3c4abbfSDmitry Kasatkin goto out; 236e3c4abbfSDmitry Kasatkin } 237e3c4abbfSDmitry Kasatkin 238e3c4abbfSDmitry Kasatkin rc = integrity_kernel_read(file, 0, buf, size); 239cc4e719eSAl Viro if (rc == size) { 240e3c4abbfSDmitry Kasatkin *data = buf; 241cc4e719eSAl Viro } else { 242cc4e719eSAl Viro kfree(buf); 243cc4e719eSAl Viro if (rc >= 0) 244cc4e719eSAl Viro rc = -EIO; 245cc4e719eSAl Viro } 246e3c4abbfSDmitry Kasatkin out: 247e3c4abbfSDmitry Kasatkin fput(file); 248e3c4abbfSDmitry Kasatkin return rc; 249e3c4abbfSDmitry Kasatkin } 250c9cd2ce2SDmitry Kasatkin 251c9cd2ce2SDmitry Kasatkin /* 252c9cd2ce2SDmitry Kasatkin * integrity_load_keys - load integrity keys hook 253c9cd2ce2SDmitry Kasatkin * 254c9cd2ce2SDmitry Kasatkin * Hooks is called from init/main.c:kernel_init_freeable() 255c9cd2ce2SDmitry Kasatkin * when rootfs is ready 256c9cd2ce2SDmitry Kasatkin */ 257c9cd2ce2SDmitry Kasatkin void __init integrity_load_keys(void) 258c9cd2ce2SDmitry Kasatkin { 259c9cd2ce2SDmitry Kasatkin ima_load_x509(); 2602ce523ebSDmitry Kasatkin evm_load_x509(); 261c9cd2ce2SDmitry Kasatkin } 262