Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 217423
b: refs/heads/master
c: f8f72ad
h: refs/heads/master
i:
  217421: 9e9d74c
  217419: 0317308
  217415: 2d44184
  217407: aec49a4
v: v3
  • Loading branch information
KAMEZAWA Hiroyuki authored and Linus Torvalds committed Oct 26, 2010
1 parent d84cfb8 commit 6afbc20
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 204 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: f9ba5375a8aae4aeea6be15df77e24707a429812
refs/heads/master: f8f72ad5396987e05a42cf7eff826fb2a15ff148
1 change: 0 additions & 1 deletion trunk/fs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <linux/mount.h>
#include <linux/async.h>
#include <linux/posix_acl.h>
#include <linux/ima.h>

/*
* This is needed for the following functions:
Expand Down
6 changes: 0 additions & 6 deletions trunk/include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ struct inodes_stat_t {
#define S_NOCMTIME 128 /* Do not update file c/mtime */
#define S_SWAPFILE 256 /* Do not truncate: swapon got its bmaps */
#define S_PRIVATE 512 /* Inode is fs-internal */
#define S_IMA 1024 /* Inode has an associated IMA struct */

/*
* Note that nosuid etc flags are inode-specific: setting some file-system
Expand Down Expand Up @@ -266,7 +265,6 @@ struct inodes_stat_t {
#define IS_NOCMTIME(inode) ((inode)->i_flags & S_NOCMTIME)
#define IS_SWAPFILE(inode) ((inode)->i_flags & S_SWAPFILE)
#define IS_PRIVATE(inode) ((inode)->i_flags & S_PRIVATE)
#define IS_IMA(inode) ((inode)->i_flags & S_IMA)

/* the read-only stuff doesn't really belong here, but any other place is
probably as bad and I don't want to create yet another include file. */
Expand Down Expand Up @@ -774,10 +772,6 @@ struct inode {

unsigned int i_flags;

#ifdef CONFIG_IMA
/* protected by i_lock */
unsigned int i_readcount; /* struct files open RO */
#endif
atomic_t i_writecount;
#ifdef CONFIG_SECURITY
void *i_security;
Expand Down
2 changes: 1 addition & 1 deletion trunk/mm/memory_hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
* Scanning pfn is much easier than scanning lru list.
* Scan pfn from start to end and Find LRU page.
*/
int scan_lru_pages(unsigned long start, unsigned long end)
unsigned long scan_lru_pages(unsigned long start, unsigned long end)
{
unsigned long pfn;
struct page *page;
Expand Down
18 changes: 11 additions & 7 deletions trunk/security/integrity/ima/ima.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ int ima_init(void);
void ima_cleanup(void);
int ima_fs_init(void);
void ima_fs_cleanup(void);
int ima_inode_alloc(struct inode *inode);
int ima_add_template_entry(struct ima_template_entry *entry, int violation,
const char *op, struct inode *inode);
int ima_calc_hash(struct file *file, char *digest);
Expand All @@ -97,16 +96,19 @@ static inline unsigned long ima_hash_key(u8 *digest)
}

/* iint cache flags */
#define IMA_MEASURED 0x01
#define IMA_MEASURED 1

/* integrity data associated with an inode */
struct ima_iint_cache {
struct rb_node rb_node; /* rooted in ima_iint_tree */
struct inode *inode; /* back pointer to inode in question */
u64 version; /* track inode changes */
unsigned char flags;
unsigned long flags;
u8 digest[IMA_DIGEST_SIZE];
struct mutex mutex; /* protects: version, flags, digest */
long readcount; /* measured files readcount */
long writecount; /* measured files writecount */
long opencount; /* opens reference count */
struct kref refcount; /* ima_iint_cache reference count */
struct rcu_head rcu;
};

/* LIM API function definitions */
Expand All @@ -120,11 +122,13 @@ int ima_store_template(struct ima_template_entry *entry, int violation,
void ima_template_show(struct seq_file *m, void *e,
enum ima_show_type show);

/* rbtree tree calls to lookup, insert, delete
/* radix tree calls to lookup, insert, delete
* integrity data associated with an inode.
*/
struct ima_iint_cache *ima_iint_insert(struct inode *inode);
struct ima_iint_cache *ima_iint_find(struct inode *inode);
struct ima_iint_cache *ima_iint_find_get(struct inode *inode);
void iint_free(struct kref *kref);
void iint_rcu_free(struct rcu_head *rcu);

/* IMA policy related functions */
enum ima_hooks { FILE_CHECK = 1, FILE_MMAP, BPRM_CHECK };
Expand Down
2 changes: 1 addition & 1 deletion trunk/security/integrity/ima/ima_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int ima_must_measure(struct ima_iint_cache *iint, struct inode *inode,
{
int must_measure;

if (iint && iint->flags & IMA_MEASURED)
if (iint->flags & IMA_MEASURED)
return 1;

must_measure = ima_match_policy(inode, function, mask);
Expand Down
158 changes: 66 additions & 92 deletions trunk/security/integrity/ima/ima_iint.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,119 +12,98 @@
* File: ima_iint.c
* - implements the IMA hooks: ima_inode_alloc, ima_inode_free
* - cache integrity information associated with an inode
* using a rbtree tree.
* using a radix tree.
*/
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/rbtree.h>
#include <linux/radix-tree.h>
#include "ima.h"

static struct rb_root ima_iint_tree = RB_ROOT;
static DEFINE_SPINLOCK(ima_iint_lock);
RADIX_TREE(ima_iint_store, GFP_ATOMIC);
DEFINE_SPINLOCK(ima_iint_lock);
static struct kmem_cache *iint_cache __read_mostly;

int iint_initialized = 0;

/*
* __ima_iint_find - return the iint associated with an inode
/* ima_iint_find_get - return the iint associated with an inode
*
* ima_iint_find_get gets a reference to the iint. Caller must
* remember to put the iint reference.
*/
static struct ima_iint_cache *__ima_iint_find(struct inode *inode)
struct ima_iint_cache *ima_iint_find_get(struct inode *inode)
{
struct ima_iint_cache *iint;
struct rb_node *n = ima_iint_tree.rb_node;

assert_spin_locked(&ima_iint_lock);

while (n) {
iint = rb_entry(n, struct ima_iint_cache, rb_node);

if (inode < iint->inode)
n = n->rb_left;
else if (inode > iint->inode)
n = n->rb_right;
else
break;
}
if (!n)
return NULL;

rcu_read_lock();
iint = radix_tree_lookup(&ima_iint_store, (unsigned long)inode);
if (!iint)
goto out;
kref_get(&iint->refcount);
out:
rcu_read_unlock();
return iint;
}

/*
* ima_iint_find - return the iint associated with an inode
/**
* ima_inode_alloc - allocate an iint associated with an inode
* @inode: pointer to the inode
*/
struct ima_iint_cache *ima_iint_find(struct inode *inode)
int ima_inode_alloc(struct inode *inode)
{
struct ima_iint_cache *iint;
struct ima_iint_cache *iint = NULL;
int rc = 0;

iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
if (!iint)
return -ENOMEM;

if (!IS_IMA(inode))
return NULL;
rc = radix_tree_preload(GFP_NOFS);
if (rc < 0)
goto out;

spin_lock(&ima_iint_lock);
iint = __ima_iint_find(inode);
rc = radix_tree_insert(&ima_iint_store, (unsigned long)inode, iint);
spin_unlock(&ima_iint_lock);
radix_tree_preload_end();
out:
if (rc < 0)
kmem_cache_free(iint_cache, iint);

return iint;
return rc;
}

static void iint_free(struct ima_iint_cache *iint)
/* iint_free - called when the iint refcount goes to zero */
void iint_free(struct kref *kref)
{
struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache,
refcount);
iint->version = 0;
iint->flags = 0UL;
if (iint->readcount != 0) {
printk(KERN_INFO "%s: readcount: %ld\n", __func__,
iint->readcount);
iint->readcount = 0;
}
if (iint->writecount != 0) {
printk(KERN_INFO "%s: writecount: %ld\n", __func__,
iint->writecount);
iint->writecount = 0;
}
if (iint->opencount != 0) {
printk(KERN_INFO "%s: opencount: %ld\n", __func__,
iint->opencount);
iint->opencount = 0;
}
kref_init(&iint->refcount);
kmem_cache_free(iint_cache, iint);
}

/**
* ima_inode_alloc - allocate an iint associated with an inode
* @inode: pointer to the inode
*/
int ima_inode_alloc(struct inode *inode)
void iint_rcu_free(struct rcu_head *rcu_head)
{
struct rb_node **p;
struct rb_node *new_node, *parent = NULL;
struct ima_iint_cache *new_iint, *test_iint;
int rc;

new_iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
if (!new_iint)
return -ENOMEM;

new_iint->inode = inode;
new_node = &new_iint->rb_node;

mutex_lock(&inode->i_mutex); /* i_flags */
spin_lock(&ima_iint_lock);

p = &ima_iint_tree.rb_node;
while (*p) {
parent = *p;
test_iint = rb_entry(parent, struct ima_iint_cache, rb_node);

rc = -EEXIST;
if (inode < test_iint->inode)
p = &(*p)->rb_left;
else if (inode > test_iint->inode)
p = &(*p)->rb_right;
else
goto out_err;
}

inode->i_flags |= S_IMA;
rb_link_node(new_node, parent, p);
rb_insert_color(new_node, &ima_iint_tree);

spin_unlock(&ima_iint_lock);
mutex_unlock(&inode->i_mutex); /* i_flags */

return 0;
out_err:
spin_unlock(&ima_iint_lock);
mutex_unlock(&inode->i_mutex); /* i_flags */
iint_free(new_iint);

return rc;
struct ima_iint_cache *iint = container_of(rcu_head,
struct ima_iint_cache, rcu);
kref_put(&iint->refcount, iint_free);
}

/**
Expand All @@ -137,20 +116,11 @@ void ima_inode_free(struct inode *inode)
{
struct ima_iint_cache *iint;

if (inode->i_readcount)
printk(KERN_INFO "%s: readcount: %u\n", __func__, inode->i_readcount);

inode->i_readcount = 0;

if (!IS_IMA(inode))
return;

spin_lock(&ima_iint_lock);
iint = __ima_iint_find(inode);
rb_erase(&iint->rb_node, &ima_iint_tree);
iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
spin_unlock(&ima_iint_lock);

iint_free(iint);
if (iint)
call_rcu(&iint->rcu, iint_rcu_free);
}

static void init_once(void *foo)
Expand All @@ -161,6 +131,10 @@ static void init_once(void *foo)
iint->version = 0;
iint->flags = 0UL;
mutex_init(&iint->mutex);
iint->readcount = 0;
iint->writecount = 0;
iint->opencount = 0;
kref_init(&iint->refcount);
}

static int __init ima_iintcache_init(void)
Expand Down
Loading

0 comments on commit 6afbc20

Please sign in to comment.