2009-02-04 14:06:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005,2006,2007,2008 IBM Corporation
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Kylene Hall <kjhall@us.ibm.com>
|
|
|
|
* Reiner Sailer <sailer@us.ibm.com>
|
|
|
|
* Mimi Zohar <zohar@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation, version 2 of the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* File: ima_fs.c
|
|
|
|
* implemenents security file system for reporting
|
|
|
|
* current measurement list and IMA statistics
|
|
|
|
*/
|
2009-05-12 19:13:55 +00:00
|
|
|
#include <linux/fcntl.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2009-02-04 14:06:59 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/rculist.h>
|
|
|
|
#include <linux/rcupdate.h>
|
2009-02-04 14:07:00 +00:00
|
|
|
#include <linux/parser.h>
|
2009-02-04 14:06:59 +00:00
|
|
|
|
|
|
|
#include "ima.h"
|
|
|
|
|
2009-02-04 14:07:00 +00:00
|
|
|
static int valid_policy = 1;
|
2009-02-04 14:06:59 +00:00
|
|
|
#define TMPBUFLEN 12
|
|
|
|
static ssize_t ima_show_htable_value(char __user *buf, size_t count,
|
|
|
|
loff_t *ppos, atomic_long_t *val)
|
|
|
|
{
|
|
|
|
char tmpbuf[TMPBUFLEN];
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
|
|
|
|
return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ima_show_htable_violations(struct file *filp,
|
|
|
|
char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
|
|
|
|
}
|
|
|
|
|
2009-10-01 22:43:56 +00:00
|
|
|
static const struct file_operations ima_htable_violations_ops = {
|
2010-07-07 21:40:15 +00:00
|
|
|
.read = ima_show_htable_violations,
|
|
|
|
.llseek = generic_file_llseek,
|
2009-02-04 14:06:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t ima_show_measurements_count(struct file *filp,
|
|
|
|
char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-10-01 22:43:56 +00:00
|
|
|
static const struct file_operations ima_measurements_count_ops = {
|
2010-07-07 21:40:15 +00:00
|
|
|
.read = ima_show_measurements_count,
|
|
|
|
.llseek = generic_file_llseek,
|
2009-02-04 14:06:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* returns pointer to hlist_node */
|
|
|
|
static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
|
|
|
|
{
|
|
|
|
loff_t l = *pos;
|
|
|
|
struct ima_queue_entry *qe;
|
|
|
|
|
|
|
|
/* we need a lock since pos could point beyond last element */
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(qe, &ima_measurements, later) {
|
|
|
|
if (!l--) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
return qe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct ima_queue_entry *qe = v;
|
|
|
|
|
|
|
|
/* lock protects when reading beyond last element
|
|
|
|
* against concurrent list-extension
|
|
|
|
*/
|
|
|
|
rcu_read_lock();
|
2009-04-14 18:17:16 +00:00
|
|
|
qe = list_entry_rcu(qe->later.next,
|
|
|
|
struct ima_queue_entry, later);
|
2009-02-04 14:06:59 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
(*pos)++;
|
|
|
|
|
|
|
|
return (&qe->later == &ima_measurements) ? NULL : qe;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ima_measurements_stop(struct seq_file *m, void *v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ima_putc(struct seq_file *m, void *data, int datalen)
|
|
|
|
{
|
|
|
|
while (datalen--)
|
|
|
|
seq_putc(m, *(char *)data++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print format:
|
|
|
|
* 32bit-le=pcr#
|
|
|
|
* char[20]=template digest
|
|
|
|
* 32bit-le=template name size
|
|
|
|
* char[n]=template name
|
|
|
|
* eventdata[n]=template specific data
|
|
|
|
*/
|
|
|
|
static int ima_measurements_show(struct seq_file *m, void *v)
|
|
|
|
{
|
|
|
|
/* the list never shrinks, so we don't need a lock here */
|
|
|
|
struct ima_queue_entry *qe = v;
|
|
|
|
struct ima_template_entry *e;
|
|
|
|
int namelen;
|
|
|
|
u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
|
|
|
|
|
|
|
|
/* get entry */
|
|
|
|
e = qe->entry;
|
|
|
|
if (e == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1st: PCRIndex
|
|
|
|
* PCR used is always the same (config option) in
|
|
|
|
* little-endian format
|
|
|
|
*/
|
|
|
|
ima_putc(m, &pcr, sizeof pcr);
|
|
|
|
|
|
|
|
/* 2nd: template digest */
|
|
|
|
ima_putc(m, e->digest, IMA_DIGEST_SIZE);
|
|
|
|
|
|
|
|
/* 3rd: template name size */
|
|
|
|
namelen = strlen(e->template_name);
|
|
|
|
ima_putc(m, &namelen, sizeof namelen);
|
|
|
|
|
|
|
|
/* 4th: template name */
|
2009-02-11 16:12:28 +00:00
|
|
|
ima_putc(m, (void *)e->template_name, namelen);
|
2009-02-04 14:06:59 +00:00
|
|
|
|
|
|
|
/* 5th: template specific data */
|
|
|
|
ima_template_show(m, (struct ima_template_data *)&e->template,
|
|
|
|
IMA_SHOW_BINARY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-22 23:43:43 +00:00
|
|
|
static const struct seq_operations ima_measurments_seqops = {
|
2009-02-04 14:06:59 +00:00
|
|
|
.start = ima_measurements_start,
|
|
|
|
.next = ima_measurements_next,
|
|
|
|
.stop = ima_measurements_stop,
|
|
|
|
.show = ima_measurements_show
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ima_measurements_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return seq_open(file, &ima_measurments_seqops);
|
|
|
|
}
|
|
|
|
|
2009-10-01 22:43:56 +00:00
|
|
|
static const struct file_operations ima_measurements_ops = {
|
2009-02-04 14:06:59 +00:00
|
|
|
.open = ima_measurements_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ima_print_digest(struct seq_file *m, u8 *digest)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < IMA_DIGEST_SIZE; i++)
|
|
|
|
seq_printf(m, "%02x", *(digest + i));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show)
|
|
|
|
{
|
|
|
|
struct ima_template_data *entry = e;
|
|
|
|
int namelen;
|
|
|
|
|
|
|
|
switch (show) {
|
|
|
|
case IMA_SHOW_ASCII:
|
|
|
|
ima_print_digest(m, entry->digest);
|
|
|
|
seq_printf(m, " %s\n", entry->file_name);
|
|
|
|
break;
|
|
|
|
case IMA_SHOW_BINARY:
|
|
|
|
ima_putc(m, entry->digest, IMA_DIGEST_SIZE);
|
|
|
|
|
|
|
|
namelen = strlen(entry->file_name);
|
|
|
|
ima_putc(m, &namelen, sizeof namelen);
|
|
|
|
ima_putc(m, entry->file_name, namelen);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print in ascii */
|
|
|
|
static int ima_ascii_measurements_show(struct seq_file *m, void *v)
|
|
|
|
{
|
|
|
|
/* the list never shrinks, so we don't need a lock here */
|
|
|
|
struct ima_queue_entry *qe = v;
|
|
|
|
struct ima_template_entry *e;
|
|
|
|
|
|
|
|
/* get entry */
|
|
|
|
e = qe->entry;
|
|
|
|
if (e == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* 1st: PCR used (config option) */
|
|
|
|
seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
|
|
|
|
|
|
|
|
/* 2nd: SHA1 template hash */
|
|
|
|
ima_print_digest(m, e->digest);
|
|
|
|
|
|
|
|
/* 3th: template name */
|
|
|
|
seq_printf(m, " %s ", e->template_name);
|
|
|
|
|
|
|
|
/* 4th: template specific data */
|
|
|
|
ima_template_show(m, (struct ima_template_data *)&e->template,
|
|
|
|
IMA_SHOW_ASCII);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-22 23:43:43 +00:00
|
|
|
static const struct seq_operations ima_ascii_measurements_seqops = {
|
2009-02-04 14:06:59 +00:00
|
|
|
.start = ima_measurements_start,
|
|
|
|
.next = ima_measurements_next,
|
|
|
|
.stop = ima_measurements_stop,
|
|
|
|
.show = ima_ascii_measurements_show
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return seq_open(file, &ima_ascii_measurements_seqops);
|
|
|
|
}
|
|
|
|
|
2009-10-01 22:43:56 +00:00
|
|
|
static const struct file_operations ima_ascii_measurements_ops = {
|
2009-02-04 14:06:59 +00:00
|
|
|
.open = ima_ascii_measurements_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release,
|
|
|
|
};
|
|
|
|
|
2009-02-04 14:07:00 +00:00
|
|
|
static ssize_t ima_write_policy(struct file *file, const char __user *buf,
|
|
|
|
size_t datalen, loff_t *ppos)
|
|
|
|
{
|
2010-04-20 14:20:54 +00:00
|
|
|
char *data = NULL;
|
|
|
|
ssize_t result;
|
2009-02-04 14:07:00 +00:00
|
|
|
|
|
|
|
if (datalen >= PAGE_SIZE)
|
2010-04-20 14:20:54 +00:00
|
|
|
datalen = PAGE_SIZE - 1;
|
|
|
|
|
|
|
|
/* No partial writes. */
|
|
|
|
result = -EINVAL;
|
|
|
|
if (*ppos != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
result = -ENOMEM;
|
2009-02-04 14:07:00 +00:00
|
|
|
data = kmalloc(datalen + 1, GFP_KERNEL);
|
|
|
|
if (!data)
|
2010-04-20 14:20:54 +00:00
|
|
|
goto out;
|
2009-02-04 14:07:00 +00:00
|
|
|
|
|
|
|
*(data + datalen) = '\0';
|
|
|
|
|
2010-04-20 14:20:54 +00:00
|
|
|
result = -EFAULT;
|
|
|
|
if (copy_from_user(data, buf, datalen))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
result = ima_parse_add_rule(data);
|
|
|
|
out:
|
|
|
|
if (result < 0)
|
|
|
|
valid_policy = 0;
|
2009-02-04 14:07:00 +00:00
|
|
|
kfree(data);
|
2010-04-20 14:20:54 +00:00
|
|
|
return result;
|
2009-02-04 14:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 14:06:59 +00:00
|
|
|
static struct dentry *ima_dir;
|
|
|
|
static struct dentry *binary_runtime_measurements;
|
|
|
|
static struct dentry *ascii_runtime_measurements;
|
|
|
|
static struct dentry *runtime_measurements_count;
|
|
|
|
static struct dentry *violations;
|
2009-02-04 14:07:00 +00:00
|
|
|
static struct dentry *ima_policy;
|
|
|
|
|
2009-02-04 14:07:01 +00:00
|
|
|
static atomic_t policy_opencount = ATOMIC_INIT(1);
|
|
|
|
/*
|
|
|
|
* ima_open_policy: sequentialize access to the policy file
|
|
|
|
*/
|
2011-08-30 00:18:30 +00:00
|
|
|
static int ima_open_policy(struct inode * inode, struct file * filp)
|
2009-02-04 14:07:01 +00:00
|
|
|
{
|
2009-05-12 19:13:55 +00:00
|
|
|
/* No point in being allowed to open it if you aren't going to write */
|
|
|
|
if (!(filp->f_flags & O_WRONLY))
|
|
|
|
return -EACCES;
|
2009-02-04 14:07:01 +00:00
|
|
|
if (atomic_dec_and_test(&policy_opencount))
|
|
|
|
return 0;
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2009-02-04 14:07:00 +00:00
|
|
|
/*
|
|
|
|
* ima_release_policy - start using the new measure policy rules.
|
|
|
|
*
|
|
|
|
* Initially, ima_measure points to the default policy rules, now
|
2009-02-04 14:07:01 +00:00
|
|
|
* point to the new policy rules, and remove the securityfs policy file,
|
|
|
|
* assuming a valid policy.
|
2009-02-04 14:07:00 +00:00
|
|
|
*/
|
|
|
|
static int ima_release_policy(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
if (!valid_policy) {
|
|
|
|
ima_delete_rules();
|
2009-02-04 14:07:01 +00:00
|
|
|
valid_policy = 1;
|
|
|
|
atomic_set(&policy_opencount, 1);
|
2009-02-04 14:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ima_update_policy();
|
|
|
|
securityfs_remove(ima_policy);
|
|
|
|
ima_policy = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-01 22:43:56 +00:00
|
|
|
static const struct file_operations ima_measure_policy_ops = {
|
2009-02-04 14:07:01 +00:00
|
|
|
.open = ima_open_policy,
|
2009-02-04 14:07:00 +00:00
|
|
|
.write = ima_write_policy,
|
2010-07-07 21:40:15 +00:00
|
|
|
.release = ima_release_policy,
|
|
|
|
.llseek = generic_file_llseek,
|
2009-02-04 14:07:00 +00:00
|
|
|
};
|
2009-02-04 14:06:59 +00:00
|
|
|
|
2009-05-21 19:43:32 +00:00
|
|
|
int __init ima_fs_init(void)
|
2009-02-04 14:06:59 +00:00
|
|
|
{
|
|
|
|
ima_dir = securityfs_create_dir("ima", NULL);
|
|
|
|
if (IS_ERR(ima_dir))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
binary_runtime_measurements =
|
|
|
|
securityfs_create_file("binary_runtime_measurements",
|
|
|
|
S_IRUSR | S_IRGRP, ima_dir, NULL,
|
|
|
|
&ima_measurements_ops);
|
|
|
|
if (IS_ERR(binary_runtime_measurements))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ascii_runtime_measurements =
|
|
|
|
securityfs_create_file("ascii_runtime_measurements",
|
|
|
|
S_IRUSR | S_IRGRP, ima_dir, NULL,
|
|
|
|
&ima_ascii_measurements_ops);
|
|
|
|
if (IS_ERR(ascii_runtime_measurements))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
runtime_measurements_count =
|
|
|
|
securityfs_create_file("runtime_measurements_count",
|
|
|
|
S_IRUSR | S_IRGRP, ima_dir, NULL,
|
|
|
|
&ima_measurements_count_ops);
|
|
|
|
if (IS_ERR(runtime_measurements_count))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
violations =
|
|
|
|
securityfs_create_file("violations", S_IRUSR | S_IRGRP,
|
|
|
|
ima_dir, NULL, &ima_htable_violations_ops);
|
|
|
|
if (IS_ERR(violations))
|
|
|
|
goto out;
|
|
|
|
|
2009-02-04 14:07:00 +00:00
|
|
|
ima_policy = securityfs_create_file("policy",
|
2009-05-12 19:13:55 +00:00
|
|
|
S_IWUSR,
|
2009-02-04 14:07:00 +00:00
|
|
|
ima_dir, NULL,
|
|
|
|
&ima_measure_policy_ops);
|
|
|
|
if (IS_ERR(ima_policy))
|
|
|
|
goto out;
|
2009-02-04 14:06:59 +00:00
|
|
|
|
2009-02-04 14:07:00 +00:00
|
|
|
return 0;
|
2009-02-04 14:06:59 +00:00
|
|
|
out:
|
2012-01-30 00:19:08 +00:00
|
|
|
securityfs_remove(violations);
|
2009-02-04 14:06:59 +00:00
|
|
|
securityfs_remove(runtime_measurements_count);
|
|
|
|
securityfs_remove(ascii_runtime_measurements);
|
|
|
|
securityfs_remove(binary_runtime_measurements);
|
|
|
|
securityfs_remove(ima_dir);
|
2009-02-04 14:07:00 +00:00
|
|
|
securityfs_remove(ima_policy);
|
2009-02-04 14:06:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|