2019-06-01 16:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-03-10 03:13:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 IBM Corporation
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Mimi Zohar <zohar@us.ibm.com>
|
|
|
|
*
|
|
|
|
* File: integrity_iint.c
|
|
|
|
* - implements the integrity hooks: integrity_inode_alloc,
|
|
|
|
* integrity_inode_free
|
|
|
|
* - cache integrity information associated with an inode
|
|
|
|
* using a rbtree tree.
|
|
|
|
*/
|
|
|
|
#include <linux/slab.h>
|
2018-12-10 04:36:29 +08:00
|
|
|
#include <linux/init.h>
|
2011-03-10 03:13:22 +08:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/rbtree.h>
|
2014-11-05 23:01:12 +08:00
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/uaccess.h>
|
2018-05-12 07:12:34 +08:00
|
|
|
#include <linux/security.h>
|
2018-10-11 08:18:21 +08:00
|
|
|
#include <linux/lsm_hooks.h>
|
2011-03-10 03:13:22 +08:00
|
|
|
#include "integrity.h"
|
|
|
|
|
|
|
|
static struct rb_root integrity_iint_tree = RB_ROOT;
|
2012-02-09 03:15:42 +08:00
|
|
|
static DEFINE_RWLOCK(integrity_iint_lock);
|
2011-03-10 03:13:22 +08:00
|
|
|
static struct kmem_cache *iint_cache __read_mostly;
|
|
|
|
|
2018-05-12 07:12:34 +08:00
|
|
|
struct dentry *integrity_dir;
|
|
|
|
|
2011-03-10 03:13:22 +08:00
|
|
|
/*
|
|
|
|
* __integrity_iint_find - return the iint associated with an inode
|
|
|
|
*/
|
|
|
|
static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct integrity_iint_cache *iint;
|
|
|
|
struct rb_node *n = integrity_iint_tree.rb_node;
|
|
|
|
|
|
|
|
while (n) {
|
|
|
|
iint = rb_entry(n, struct integrity_iint_cache, rb_node);
|
|
|
|
|
|
|
|
if (inode < iint->inode)
|
|
|
|
n = n->rb_left;
|
|
|
|
else if (inode > iint->inode)
|
|
|
|
n = n->rb_right;
|
|
|
|
else
|
2023-06-01 14:42:44 +08:00
|
|
|
return iint;
|
2011-03-10 03:13:22 +08:00
|
|
|
}
|
|
|
|
|
2023-06-01 14:42:44 +08:00
|
|
|
return NULL;
|
2011-03-10 03:13:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* integrity_iint_find - return the iint associated with an inode
|
|
|
|
*/
|
|
|
|
struct integrity_iint_cache *integrity_iint_find(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct integrity_iint_cache *iint;
|
|
|
|
|
|
|
|
if (!IS_IMA(inode))
|
|
|
|
return NULL;
|
|
|
|
|
2012-02-09 03:15:42 +08:00
|
|
|
read_lock(&integrity_iint_lock);
|
2011-03-10 03:13:22 +08:00
|
|
|
iint = __integrity_iint_find(inode);
|
2012-02-09 03:15:42 +08:00
|
|
|
read_unlock(&integrity_iint_lock);
|
2011-03-10 03:13:22 +08:00
|
|
|
|
|
|
|
return iint;
|
|
|
|
}
|
|
|
|
|
2023-10-05 19:15:58 +08:00
|
|
|
#define IMA_MAX_NESTING (FILESYSTEM_MAX_STACK_DEPTH+1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is not clear that IMA should be nested at all, but as long is it measures
|
|
|
|
* files both on overlayfs and on underlying fs, we need to annotate the iint
|
|
|
|
* mutex to avoid lockdep false positives related to IMA + overlayfs.
|
|
|
|
* See ovl_lockdep_annotate_inode_mutex_key() for more details.
|
|
|
|
*/
|
|
|
|
static inline void iint_lockdep_annotate(struct integrity_iint_cache *iint,
|
|
|
|
struct inode *inode)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_LOCKDEP
|
|
|
|
static struct lock_class_key iint_mutex_key[IMA_MAX_NESTING];
|
|
|
|
|
|
|
|
int depth = inode->i_sb->s_stack_depth;
|
|
|
|
|
|
|
|
if (WARN_ON_ONCE(depth < 0 || depth >= IMA_MAX_NESTING))
|
|
|
|
depth = 0;
|
|
|
|
|
|
|
|
lockdep_set_class(&iint->mutex, &iint_mutex_key[depth]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iint_init_always(struct integrity_iint_cache *iint,
|
|
|
|
struct inode *inode)
|
2011-03-10 03:13:22 +08:00
|
|
|
{
|
2013-04-25 15:44:04 +08:00
|
|
|
iint->ima_hash = NULL;
|
2011-03-10 03:13:22 +08:00
|
|
|
iint->version = 0;
|
|
|
|
iint->flags = 0UL;
|
2018-01-23 23:00:41 +08:00
|
|
|
iint->atomic_flags = 0UL;
|
2012-12-04 06:08:11 +08:00
|
|
|
iint->ima_file_status = INTEGRITY_UNKNOWN;
|
|
|
|
iint->ima_mmap_status = INTEGRITY_UNKNOWN;
|
|
|
|
iint->ima_bprm_status = INTEGRITY_UNKNOWN;
|
2015-11-20 01:39:22 +08:00
|
|
|
iint->ima_read_status = INTEGRITY_UNKNOWN;
|
2018-01-09 05:36:20 +08:00
|
|
|
iint->ima_creds_status = INTEGRITY_UNKNOWN;
|
2011-08-15 20:30:11 +08:00
|
|
|
iint->evm_status = INTEGRITY_UNKNOWN;
|
2016-06-02 02:14:00 +08:00
|
|
|
iint->measured_pcrs = 0;
|
2023-10-05 19:15:58 +08:00
|
|
|
mutex_init(&iint->mutex);
|
|
|
|
iint_lockdep_annotate(iint, inode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iint_free(struct integrity_iint_cache *iint)
|
|
|
|
{
|
|
|
|
kfree(iint->ima_hash);
|
|
|
|
mutex_destroy(&iint->mutex);
|
2011-03-10 03:13:22 +08:00
|
|
|
kmem_cache_free(iint_cache, iint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-19 17:04:40 +08:00
|
|
|
* integrity_inode_get - find or allocate an iint associated with an inode
|
2011-03-10 03:13:22 +08:00
|
|
|
* @inode: pointer to the inode
|
2011-10-19 17:04:40 +08:00
|
|
|
* @return: allocated iint
|
|
|
|
*
|
|
|
|
* Caller must lock i_mutex
|
2011-03-10 03:13:22 +08:00
|
|
|
*/
|
2011-10-19 17:04:40 +08:00
|
|
|
struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
|
2011-03-10 03:13:22 +08:00
|
|
|
{
|
|
|
|
struct rb_node **p;
|
2011-10-19 17:04:40 +08:00
|
|
|
struct rb_node *node, *parent = NULL;
|
|
|
|
struct integrity_iint_cache *iint, *test_iint;
|
2011-03-10 03:13:22 +08:00
|
|
|
|
2011-10-19 17:04:40 +08:00
|
|
|
iint = integrity_iint_find(inode);
|
|
|
|
if (iint)
|
|
|
|
return iint;
|
2011-03-10 03:13:22 +08:00
|
|
|
|
2011-10-19 17:04:40 +08:00
|
|
|
iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
|
|
|
|
if (!iint)
|
|
|
|
return NULL;
|
2011-03-10 03:13:22 +08:00
|
|
|
|
2023-10-05 19:15:58 +08:00
|
|
|
iint_init_always(iint, inode);
|
|
|
|
|
2012-02-09 03:15:42 +08:00
|
|
|
write_lock(&integrity_iint_lock);
|
2011-03-10 03:13:22 +08:00
|
|
|
|
|
|
|
p = &integrity_iint_tree.rb_node;
|
|
|
|
while (*p) {
|
|
|
|
parent = *p;
|
|
|
|
test_iint = rb_entry(parent, struct integrity_iint_cache,
|
|
|
|
rb_node);
|
2023-06-01 14:42:44 +08:00
|
|
|
if (inode < test_iint->inode) {
|
2011-03-10 03:13:22 +08:00
|
|
|
p = &(*p)->rb_left;
|
2023-06-01 14:42:44 +08:00
|
|
|
} else if (inode > test_iint->inode) {
|
2011-10-19 17:04:40 +08:00
|
|
|
p = &(*p)->rb_right;
|
2023-06-01 14:42:44 +08:00
|
|
|
} else {
|
|
|
|
write_unlock(&integrity_iint_lock);
|
|
|
|
kmem_cache_free(iint_cache, iint);
|
|
|
|
return test_iint;
|
|
|
|
}
|
2011-03-10 03:13:22 +08:00
|
|
|
}
|
|
|
|
|
2011-10-19 17:04:40 +08:00
|
|
|
iint->inode = inode;
|
|
|
|
node = &iint->rb_node;
|
2011-03-10 03:13:22 +08:00
|
|
|
inode->i_flags |= S_IMA;
|
2011-10-19 17:04:40 +08:00
|
|
|
rb_link_node(node, parent, p);
|
|
|
|
rb_insert_color(node, &integrity_iint_tree);
|
2011-03-10 03:13:22 +08:00
|
|
|
|
2012-02-09 03:15:42 +08:00
|
|
|
write_unlock(&integrity_iint_lock);
|
2011-10-19 17:04:40 +08:00
|
|
|
return iint;
|
2011-03-10 03:13:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* integrity_inode_free - called on security_inode_free
|
|
|
|
* @inode: pointer to the inode
|
|
|
|
*
|
|
|
|
* Free the integrity information(iint) associated with an inode.
|
|
|
|
*/
|
|
|
|
void integrity_inode_free(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct integrity_iint_cache *iint;
|
|
|
|
|
|
|
|
if (!IS_IMA(inode))
|
|
|
|
return;
|
|
|
|
|
2012-02-09 03:15:42 +08:00
|
|
|
write_lock(&integrity_iint_lock);
|
2011-03-10 03:13:22 +08:00
|
|
|
iint = __integrity_iint_find(inode);
|
|
|
|
rb_erase(&iint->rb_node, &integrity_iint_tree);
|
2012-02-09 03:15:42 +08:00
|
|
|
write_unlock(&integrity_iint_lock);
|
2011-03-10 03:13:22 +08:00
|
|
|
|
|
|
|
iint_free(iint);
|
|
|
|
}
|
|
|
|
|
2023-10-05 19:15:58 +08:00
|
|
|
static void iint_init_once(void *foo)
|
2011-03-10 03:13:22 +08:00
|
|
|
{
|
2021-04-07 09:44:38 +08:00
|
|
|
struct integrity_iint_cache *iint = (struct integrity_iint_cache *) foo;
|
2011-03-10 03:13:22 +08:00
|
|
|
|
2014-03-05 00:04:20 +08:00
|
|
|
memset(iint, 0, sizeof(*iint));
|
2011-03-10 03:13:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __init integrity_iintcache_init(void)
|
|
|
|
{
|
|
|
|
iint_cache =
|
|
|
|
kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache),
|
2023-10-05 19:15:58 +08:00
|
|
|
0, SLAB_PANIC, iint_init_once);
|
2011-03-10 03:13:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-10-11 08:18:23 +08:00
|
|
|
DEFINE_LSM(integrity) = {
|
2018-10-11 08:18:24 +08:00
|
|
|
.name = "integrity",
|
2018-10-11 08:18:23 +08:00
|
|
|
.init = integrity_iintcache_init,
|
security: Introduce LSM_ORDER_LAST and set it for the integrity LSM
Introduce LSM_ORDER_LAST, to satisfy the requirement of LSMs needing to be
last, e.g. the 'integrity' LSM, without changing the kernel command line or
configuration.
Also, set this order for the 'integrity' LSM. While not enforced, this is
the only LSM expected to use it.
Similarly to LSM_ORDER_FIRST, LSMs with LSM_ORDER_LAST are always enabled
and put at the end of the LSM list, if selected in the kernel
configuration. Setting one of these orders alone, does not cause the LSMs
to be selected and compiled built-in in the kernel.
Finally, for LSM_ORDER_MUTABLE LSMs, set the found variable to true if an
LSM is found, regardless of its order. In this way, the kernel would not
wrongly report that the LSM is not built-in in the kernel if its order is
LSM_ORDER_LAST.
Fixes: 79f7865d844c ("LSM: Introduce "lsm=" for boottime LSM selection")
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Acked-by: Mimi Zohar <zohar@linux.ibm.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
2023-03-10 16:53:59 +08:00
|
|
|
.order = LSM_ORDER_LAST,
|
2018-10-11 08:18:23 +08:00
|
|
|
};
|
2014-11-05 23:01:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* integrity_kernel_read - read data from the file
|
|
|
|
*
|
|
|
|
* This is a function for reading file content instead of kernel_read().
|
|
|
|
* It does not perform locking checks to ensure it cannot be blocked.
|
|
|
|
* It does not perform security checks because it is irrelevant for IMA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int integrity_kernel_read(struct file *file, loff_t offset,
|
2017-06-08 09:49:10 +08:00
|
|
|
void *addr, unsigned long count)
|
2014-11-05 23:01:12 +08:00
|
|
|
{
|
2020-05-08 14:54:27 +08:00
|
|
|
return __kernel_read(file, addr, count, &offset);
|
2014-11-05 23:01:12 +08:00
|
|
|
}
|
|
|
|
|
2014-11-05 23:01:15 +08:00
|
|
|
/*
|
|
|
|
* integrity_load_keys - load integrity keys hook
|
|
|
|
*
|
|
|
|
* Hooks is called from init/main.c:kernel_init_freeable()
|
|
|
|
* when rootfs is ready
|
|
|
|
*/
|
|
|
|
void __init integrity_load_keys(void)
|
|
|
|
{
|
|
|
|
ima_load_x509();
|
2021-05-14 23:27:43 +08:00
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_IMA_LOAD_X509))
|
|
|
|
evm_load_x509();
|
2014-11-05 23:01:15 +08:00
|
|
|
}
|
2018-05-12 07:12:34 +08:00
|
|
|
|
|
|
|
static int __init integrity_fs_init(void)
|
|
|
|
{
|
|
|
|
integrity_dir = securityfs_create_dir("integrity", NULL);
|
|
|
|
if (IS_ERR(integrity_dir)) {
|
2018-06-05 18:25:45 +08:00
|
|
|
int ret = PTR_ERR(integrity_dir);
|
|
|
|
|
|
|
|
if (ret != -ENODEV)
|
|
|
|
pr_err("Unable to create integrity sysfs dir: %d\n",
|
|
|
|
ret);
|
2018-05-12 07:12:34 +08:00
|
|
|
integrity_dir = NULL;
|
2018-06-05 18:25:45 +08:00
|
|
|
return ret;
|
2018-05-12 07:12:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
late_initcall(integrity_fs_init)
|