2019-06-01 15:08:55 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2009-02-04 21:06:59 +07: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>
|
|
|
|
*
|
|
|
|
* File: ima_fs.c
|
|
|
|
* implemenents security file system for reporting
|
|
|
|
* current measurement list and IMA statistics
|
|
|
|
*/
|
2018-04-24 21:30:38 +07:00
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2009-05-13 02:13:55 +07: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 15:04:11 +07:00
|
|
|
#include <linux/slab.h>
|
2018-12-10 03:36:29 +07:00
|
|
|
#include <linux/init.h>
|
2009-02-04 21:06:59 +07:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/rculist.h>
|
|
|
|
#include <linux/rcupdate.h>
|
2009-02-04 21:07:00 +07:00
|
|
|
#include <linux/parser.h>
|
2014-04-11 21:47:01 +07:00
|
|
|
#include <linux/vmalloc.h>
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
#include "ima.h"
|
|
|
|
|
2015-12-02 22:47:54 +07:00
|
|
|
static DEFINE_MUTEX(ima_write_mutex);
|
|
|
|
|
2016-12-20 07:22:57 +07:00
|
|
|
bool ima_canonical_fmt;
|
|
|
|
static int __init default_canonical_fmt_setup(char *str)
|
|
|
|
{
|
|
|
|
#ifdef __BIG_ENDIAN
|
2017-10-07 21:02:21 +07:00
|
|
|
ima_canonical_fmt = true;
|
2016-12-20 07:22:57 +07:00
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
__setup("ima_canonical_fmt", default_canonical_fmt_setup);
|
|
|
|
|
2009-02-04 21:07:00 +07:00
|
|
|
static int valid_policy = 1;
|
2018-09-08 04:33:24 +07:00
|
|
|
|
2009-02-04 21:06:59 +07:00
|
|
|
static ssize_t ima_show_htable_value(char __user *buf, size_t count,
|
|
|
|
loff_t *ppos, atomic_long_t *val)
|
|
|
|
{
|
2018-09-08 04:33:24 +07:00
|
|
|
char tmpbuf[32]; /* greater than largest 'long' string value */
|
2009-02-04 21:06:59 +07:00
|
|
|
ssize_t len;
|
|
|
|
|
2018-09-08 04:33:24 +07:00
|
|
|
len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", atomic_long_read(val));
|
2009-02-04 21:06:59 +07:00
|
|
|
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-02 05:43:56 +07:00
|
|
|
static const struct file_operations ima_htable_violations_ops = {
|
2010-07-08 04:40:15 +07:00
|
|
|
.read = ima_show_htable_violations,
|
|
|
|
.llseek = generic_file_llseek,
|
2009-02-04 21:06:59 +07: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-02 05:43:56 +07:00
|
|
|
static const struct file_operations ima_measurements_count_ops = {
|
2010-07-08 04:40:15 +07:00
|
|
|
.read = ima_show_measurements_count,
|
|
|
|
.llseek = generic_file_llseek,
|
2009-02-04 21:06:59 +07: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();
|
2013-10-10 13:56:13 +07:00
|
|
|
qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
|
2009-02-04 21:06:59 +07:00
|
|
|
rcu_read_unlock();
|
|
|
|
(*pos)++;
|
|
|
|
|
|
|
|
return (&qe->later == &ima_measurements) ? NULL : qe;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ima_measurements_stop(struct seq_file *m, void *v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-07 17:16:30 +07:00
|
|
|
void ima_putc(struct seq_file *m, void *data, int datalen)
|
2009-02-04 21:06:59 +07:00
|
|
|
{
|
|
|
|
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
|
2013-06-07 17:16:33 +07:00
|
|
|
* [eventdata length]
|
2009-02-04 21:06:59 +07:00
|
|
|
* eventdata[n]=template specific data
|
|
|
|
*/
|
2016-12-20 07:22:48 +07:00
|
|
|
int ima_measurements_show(struct seq_file *m, void *v)
|
2009-02-04 21:06:59 +07:00
|
|
|
{
|
|
|
|
/* the list never shrinks, so we don't need a lock here */
|
|
|
|
struct ima_queue_entry *qe = v;
|
|
|
|
struct ima_template_entry *e;
|
2014-10-13 19:08:39 +07:00
|
|
|
char *template_name;
|
2016-12-20 07:22:57 +07:00
|
|
|
u32 pcr, namelen, template_data_len; /* temporary fields */
|
2013-11-09 01:21:40 +07:00
|
|
|
bool is_ima_template = false;
|
2013-06-07 17:16:33 +07:00
|
|
|
int i;
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
/* get entry */
|
|
|
|
e = qe->entry;
|
|
|
|
if (e == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2014-10-13 19:08:39 +07:00
|
|
|
template_name = (e->template_desc->name[0] != '\0') ?
|
|
|
|
e->template_desc->name : e->template_desc->fmt;
|
|
|
|
|
2009-02-04 21:06:59 +07:00
|
|
|
/*
|
|
|
|
* 1st: PCRIndex
|
2016-06-02 01:14:04 +07:00
|
|
|
* PCR used defaults to the same (config option) in
|
|
|
|
* little-endian format, unless set in policy
|
2009-02-04 21:06:59 +07:00
|
|
|
*/
|
2016-12-20 07:22:57 +07:00
|
|
|
pcr = !ima_canonical_fmt ? e->pcr : cpu_to_le32(e->pcr);
|
|
|
|
ima_putc(m, &pcr, sizeof(e->pcr));
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
/* 2nd: template digest */
|
2013-03-12 07:29:47 +07:00
|
|
|
ima_putc(m, e->digest, TPM_DIGEST_SIZE);
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
/* 3rd: template name size */
|
2016-12-20 07:22:57 +07:00
|
|
|
namelen = !ima_canonical_fmt ? strlen(template_name) :
|
|
|
|
cpu_to_le32(strlen(template_name));
|
2014-03-04 23:04:20 +07:00
|
|
|
ima_putc(m, &namelen, sizeof(namelen));
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
/* 4th: template name */
|
2016-12-20 07:22:57 +07:00
|
|
|
ima_putc(m, template_name, strlen(template_name));
|
2013-06-07 17:16:33 +07:00
|
|
|
|
|
|
|
/* 5th: template length (except for 'ima' template) */
|
2014-10-13 19:08:39 +07:00
|
|
|
if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
|
2013-11-09 01:21:40 +07:00
|
|
|
is_ima_template = true;
|
|
|
|
|
2016-12-20 07:22:57 +07:00
|
|
|
if (!is_ima_template) {
|
|
|
|
template_data_len = !ima_canonical_fmt ? e->template_data_len :
|
|
|
|
cpu_to_le32(e->template_data_len);
|
|
|
|
ima_putc(m, &template_data_len, sizeof(e->template_data_len));
|
|
|
|
}
|
2009-02-04 21:06:59 +07:00
|
|
|
|
2013-06-07 17:16:33 +07:00
|
|
|
/* 6th: template specific data */
|
|
|
|
for (i = 0; i < e->template_desc->num_fields; i++) {
|
2013-11-09 01:21:40 +07:00
|
|
|
enum ima_show_type show = IMA_SHOW_BINARY;
|
2018-09-08 03:22:23 +07:00
|
|
|
const struct ima_template_field *field =
|
|
|
|
e->template_desc->fields[i];
|
2013-11-09 01:21:40 +07:00
|
|
|
|
|
|
|
if (is_ima_template && strcmp(field->field_id, "d") == 0)
|
|
|
|
show = IMA_SHOW_BINARY_NO_FIELD_LEN;
|
2014-02-03 19:56:04 +07:00
|
|
|
if (is_ima_template && strcmp(field->field_id, "n") == 0)
|
|
|
|
show = IMA_SHOW_BINARY_OLD_STRING_FMT;
|
2013-11-09 01:21:40 +07:00
|
|
|
field->field_show(m, show, &e->template_data[i]);
|
2013-06-07 17:16:33 +07:00
|
|
|
}
|
2009-02-04 21:06:59 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-23 06:43:43 +07:00
|
|
|
static const struct seq_operations ima_measurments_seqops = {
|
2009-02-04 21:06:59 +07: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-02 05:43:56 +07:00
|
|
|
static const struct file_operations ima_measurements_ops = {
|
2009-02-04 21:06:59 +07:00
|
|
|
.open = ima_measurements_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release,
|
|
|
|
};
|
|
|
|
|
2015-06-11 22:54:42 +07:00
|
|
|
void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
|
2009-02-04 21:06:59 +07:00
|
|
|
{
|
2015-06-11 22:54:42 +07:00
|
|
|
u32 i;
|
2009-02-04 21:06:59 +07:00
|
|
|
|
2013-03-12 07:29:47 +07:00
|
|
|
for (i = 0; i < size; i++)
|
2009-02-04 21:06:59 +07:00
|
|
|
seq_printf(m, "%02x", *(digest + i));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2014-10-13 19:08:39 +07:00
|
|
|
char *template_name;
|
2013-06-07 17:16:33 +07:00
|
|
|
int i;
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
/* get entry */
|
|
|
|
e = qe->entry;
|
|
|
|
if (e == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2014-10-13 19:08:39 +07:00
|
|
|
template_name = (e->template_desc->name[0] != '\0') ?
|
|
|
|
e->template_desc->name : e->template_desc->fmt;
|
|
|
|
|
2009-02-04 21:06:59 +07:00
|
|
|
/* 1st: PCR used (config option) */
|
2016-06-02 01:14:04 +07:00
|
|
|
seq_printf(m, "%2d ", e->pcr);
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
/* 2nd: SHA1 template hash */
|
2013-03-12 07:29:47 +07:00
|
|
|
ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
/* 3th: template name */
|
2014-10-13 19:08:39 +07:00
|
|
|
seq_printf(m, " %s", template_name);
|
2009-02-04 21:06:59 +07:00
|
|
|
|
|
|
|
/* 4th: template specific data */
|
2013-06-07 17:16:33 +07:00
|
|
|
for (i = 0; i < e->template_desc->num_fields; i++) {
|
|
|
|
seq_puts(m, " ");
|
|
|
|
if (e->template_data[i].len == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
|
|
|
|
&e->template_data[i]);
|
|
|
|
}
|
|
|
|
seq_puts(m, "\n");
|
2009-02-04 21:06:59 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-23 06:43:43 +07:00
|
|
|
static const struct seq_operations ima_ascii_measurements_seqops = {
|
2009-02-04 21:06:59 +07: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-02 05:43:56 +07:00
|
|
|
static const struct file_operations ima_ascii_measurements_ops = {
|
2009-02-04 21:06:59 +07:00
|
|
|
.open = ima_ascii_measurements_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release,
|
|
|
|
};
|
|
|
|
|
2014-04-11 21:47:01 +07:00
|
|
|
static ssize_t ima_read_policy(char *path)
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
char *datap;
|
|
|
|
loff_t size;
|
|
|
|
int rc, pathlen = strlen(path);
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* remove \n */
|
|
|
|
datap = path;
|
|
|
|
strsep(&datap, "\n");
|
|
|
|
|
|
|
|
rc = kernel_read_file_from_path(path, &data, &size, 0, READING_POLICY);
|
|
|
|
if (rc < 0) {
|
|
|
|
pr_err("Unable to open file: %s (%d)", path, rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
datap = data;
|
|
|
|
while (size > 0 && (p = strsep(&datap, "\n"))) {
|
|
|
|
pr_debug("rule: %s\n", p);
|
|
|
|
rc = ima_parse_add_rule(p);
|
|
|
|
if (rc < 0)
|
|
|
|
break;
|
|
|
|
size -= rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
vfree(data);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
else if (size)
|
|
|
|
return -EINVAL;
|
|
|
|
else
|
|
|
|
return pathlen;
|
|
|
|
}
|
|
|
|
|
2009-02-04 21:07:00 +07:00
|
|
|
static ssize_t ima_write_policy(struct file *file, const char __user *buf,
|
|
|
|
size_t datalen, loff_t *ppos)
|
|
|
|
{
|
2016-01-03 22:36:38 +07:00
|
|
|
char *data;
|
2010-04-20 21:20:54 +07:00
|
|
|
ssize_t result;
|
2009-02-04 21:07:00 +07:00
|
|
|
|
|
|
|
if (datalen >= PAGE_SIZE)
|
2010-04-20 21:20:54 +07:00
|
|
|
datalen = PAGE_SIZE - 1;
|
|
|
|
|
|
|
|
/* No partial writes. */
|
|
|
|
result = -EINVAL;
|
|
|
|
if (*ppos != 0)
|
|
|
|
goto out;
|
|
|
|
|
2017-05-06 22:40:18 +07:00
|
|
|
data = memdup_user_nul(buf, datalen);
|
|
|
|
if (IS_ERR(data)) {
|
|
|
|
result = PTR_ERR(data);
|
2010-04-20 21:20:54 +07:00
|
|
|
goto out;
|
2017-05-06 22:40:18 +07:00
|
|
|
}
|
2010-04-20 21:20:54 +07:00
|
|
|
|
2016-01-03 22:36:38 +07:00
|
|
|
result = mutex_lock_interruptible(&ima_write_mutex);
|
|
|
|
if (result < 0)
|
|
|
|
goto out_free;
|
|
|
|
|
2016-01-15 22:17:12 +07:00
|
|
|
if (data[0] == '/') {
|
2014-04-11 21:47:01 +07:00
|
|
|
result = ima_read_policy(data);
|
2016-01-15 22:17:12 +07:00
|
|
|
} else if (ima_appraise & IMA_APPRAISE_POLICY) {
|
2018-04-24 21:30:38 +07:00
|
|
|
pr_err("signed policy file (specified as an absolute pathname) required\n");
|
2016-01-15 22:17:12 +07:00
|
|
|
integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
|
|
|
|
"policy_update", "signed policy required",
|
|
|
|
1, 0);
|
|
|
|
if (ima_appraise & IMA_APPRAISE_ENFORCE)
|
|
|
|
result = -EACCES;
|
|
|
|
} else {
|
2014-04-11 21:47:01 +07:00
|
|
|
result = ima_parse_add_rule(data);
|
2016-01-15 22:17:12 +07:00
|
|
|
}
|
2014-04-11 21:47:01 +07:00
|
|
|
mutex_unlock(&ima_write_mutex);
|
2016-01-03 22:36:38 +07:00
|
|
|
out_free:
|
|
|
|
kfree(data);
|
2010-04-20 21:20:54 +07:00
|
|
|
out:
|
|
|
|
if (result < 0)
|
|
|
|
valid_policy = 0;
|
2015-12-02 22:47:54 +07:00
|
|
|
|
2010-04-20 21:20:54 +07:00
|
|
|
return result;
|
2009-02-04 21:07:00 +07:00
|
|
|
}
|
|
|
|
|
2009-02-04 21:06:59 +07:00
|
|
|
static struct dentry *ima_dir;
|
2018-05-12 06:12:34 +07:00
|
|
|
static struct dentry *ima_symlink;
|
2009-02-04 21:06:59 +07:00
|
|
|
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 21:07:00 +07:00
|
|
|
static struct dentry *ima_policy;
|
|
|
|
|
2014-10-03 18:40:21 +07:00
|
|
|
enum ima_fs_flags {
|
|
|
|
IMA_FS_BUSY,
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long ima_fs_flags;
|
|
|
|
|
2015-12-02 22:47:56 +07:00
|
|
|
#ifdef CONFIG_IMA_READ_POLICY
|
|
|
|
static const struct seq_operations ima_policy_seqops = {
|
|
|
|
.start = ima_policy_start,
|
|
|
|
.next = ima_policy_next,
|
|
|
|
.stop = ima_policy_stop,
|
|
|
|
.show = ima_policy_show,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2009-02-04 21:07:01 +07:00
|
|
|
/*
|
|
|
|
* ima_open_policy: sequentialize access to the policy file
|
|
|
|
*/
|
2014-03-04 23:04:20 +07:00
|
|
|
static int ima_open_policy(struct inode *inode, struct file *filp)
|
2009-02-04 21:07:01 +07:00
|
|
|
{
|
2015-12-02 22:47:56 +07:00
|
|
|
if (!(filp->f_flags & O_WRONLY)) {
|
|
|
|
#ifndef CONFIG_IMA_READ_POLICY
|
2009-05-13 02:13:55 +07:00
|
|
|
return -EACCES;
|
2015-12-02 22:47:56 +07:00
|
|
|
#else
|
|
|
|
if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
|
|
|
|
return -EACCES;
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
return seq_open(filp, &ima_policy_seqops);
|
|
|
|
#endif
|
|
|
|
}
|
2014-10-03 18:40:21 +07:00
|
|
|
if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
|
|
|
|
return -EBUSY;
|
|
|
|
return 0;
|
2009-02-04 21:07:01 +07:00
|
|
|
}
|
|
|
|
|
2009-02-04 21:07:00 +07:00
|
|
|
/*
|
|
|
|
* ima_release_policy - start using the new measure policy rules.
|
|
|
|
*
|
|
|
|
* Initially, ima_measure points to the default policy rules, now
|
2009-02-04 21:07:01 +07:00
|
|
|
* point to the new policy rules, and remove the securityfs policy file,
|
|
|
|
* assuming a valid policy.
|
2009-02-04 21:07:00 +07:00
|
|
|
*/
|
|
|
|
static int ima_release_policy(struct inode *inode, struct file *file)
|
|
|
|
{
|
2014-10-03 18:40:21 +07:00
|
|
|
const char *cause = valid_policy ? "completed" : "failed";
|
|
|
|
|
2015-12-02 22:47:56 +07:00
|
|
|
if ((file->f_flags & O_ACCMODE) == O_RDONLY)
|
2016-10-14 05:47:36 +07:00
|
|
|
return seq_release(inode, file);
|
2015-12-02 22:47:56 +07:00
|
|
|
|
2015-12-22 20:51:23 +07:00
|
|
|
if (valid_policy && ima_check_policy() < 0) {
|
|
|
|
cause = "failed";
|
|
|
|
valid_policy = 0;
|
|
|
|
}
|
|
|
|
|
2018-04-24 21:30:38 +07:00
|
|
|
pr_info("policy update %s\n", cause);
|
2014-10-03 18:40:21 +07:00
|
|
|
integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
|
|
|
|
"policy_update", cause, !valid_policy, 0);
|
|
|
|
|
2009-02-04 21:07:00 +07:00
|
|
|
if (!valid_policy) {
|
|
|
|
ima_delete_rules();
|
2009-02-04 21:07:01 +07:00
|
|
|
valid_policy = 1;
|
2014-10-03 18:40:21 +07:00
|
|
|
clear_bit(IMA_FS_BUSY, &ima_fs_flags);
|
2009-02-04 21:07:00 +07:00
|
|
|
return 0;
|
|
|
|
}
|
2015-12-02 22:47:56 +07:00
|
|
|
|
2009-02-04 21:07:00 +07:00
|
|
|
ima_update_policy();
|
2017-06-28 03:10:39 +07:00
|
|
|
#if !defined(CONFIG_IMA_WRITE_POLICY) && !defined(CONFIG_IMA_READ_POLICY)
|
2009-02-04 21:07:00 +07:00
|
|
|
securityfs_remove(ima_policy);
|
|
|
|
ima_policy = NULL;
|
2017-06-28 03:10:39 +07:00
|
|
|
#elif defined(CONFIG_IMA_WRITE_POLICY)
|
2015-12-02 22:47:54 +07:00
|
|
|
clear_bit(IMA_FS_BUSY, &ima_fs_flags);
|
2018-04-20 20:28:57 +07:00
|
|
|
#elif defined(CONFIG_IMA_READ_POLICY)
|
|
|
|
inode->i_mode &= ~S_IWUSR;
|
2015-12-02 22:47:54 +07:00
|
|
|
#endif
|
2009-02-04 21:07:00 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-02 05:43:56 +07:00
|
|
|
static const struct file_operations ima_measure_policy_ops = {
|
2009-02-04 21:07:01 +07:00
|
|
|
.open = ima_open_policy,
|
2009-02-04 21:07:00 +07:00
|
|
|
.write = ima_write_policy,
|
2015-12-02 22:47:56 +07:00
|
|
|
.read = seq_read,
|
2010-07-08 04:40:15 +07:00
|
|
|
.release = ima_release_policy,
|
|
|
|
.llseek = generic_file_llseek,
|
2009-02-04 21:07:00 +07:00
|
|
|
};
|
2009-02-04 21:06:59 +07:00
|
|
|
|
2009-05-22 02:43:32 +07:00
|
|
|
int __init ima_fs_init(void)
|
2009-02-04 21:06:59 +07:00
|
|
|
{
|
2018-05-12 06:12:34 +07:00
|
|
|
ima_dir = securityfs_create_dir("ima", integrity_dir);
|
2009-02-04 21:06:59 +07:00
|
|
|
if (IS_ERR(ima_dir))
|
|
|
|
return -1;
|
|
|
|
|
2018-05-12 06:12:34 +07:00
|
|
|
ima_symlink = securityfs_create_symlink("ima", NULL, "integrity/ima",
|
|
|
|
NULL);
|
|
|
|
if (IS_ERR(ima_symlink))
|
|
|
|
goto out;
|
|
|
|
|
2009-02-04 21:06:59 +07:00
|
|
|
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;
|
|
|
|
|
2015-12-02 22:47:56 +07:00
|
|
|
ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS,
|
2009-02-04 21:07:00 +07:00
|
|
|
ima_dir, NULL,
|
|
|
|
&ima_measure_policy_ops);
|
|
|
|
if (IS_ERR(ima_policy))
|
|
|
|
goto out;
|
2009-02-04 21:06:59 +07:00
|
|
|
|
2009-02-04 21:07:00 +07:00
|
|
|
return 0;
|
2009-02-04 21:06:59 +07:00
|
|
|
out:
|
2012-01-30 07:19:08 +07:00
|
|
|
securityfs_remove(violations);
|
2009-02-04 21:06:59 +07:00
|
|
|
securityfs_remove(runtime_measurements_count);
|
|
|
|
securityfs_remove(ascii_runtime_measurements);
|
|
|
|
securityfs_remove(binary_runtime_measurements);
|
2018-05-12 06:12:34 +07:00
|
|
|
securityfs_remove(ima_symlink);
|
2009-02-04 21:06:59 +07:00
|
|
|
securityfs_remove(ima_dir);
|
2009-02-04 21:07:00 +07:00
|
|
|
securityfs_remove(ima_policy);
|
2009-02-04 21:06:59 +07:00
|
|
|
return -1;
|
|
|
|
}
|