ima_fs.c 9.4 KB
Newer Older
M
Mimi Zohar 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
 */
18
#include <linux/fcntl.h>
19
#include <linux/slab.h>
M
Mimi Zohar 已提交
20 21 22 23
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/rculist.h>
#include <linux/rcupdate.h>
M
Mimi Zohar 已提交
24
#include <linux/parser.h>
M
Mimi Zohar 已提交
25 26 27

#include "ima.h"

M
Mimi Zohar 已提交
28
static int valid_policy = 1;
M
Mimi Zohar 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#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);
}

47
static const struct file_operations ima_htable_violations_ops = {
48 49
	.read = ima_show_htable_violations,
	.llseek = generic_file_llseek,
M
Mimi Zohar 已提交
50 51 52 53 54 55 56 57 58 59
};

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);

}

60
static const struct file_operations ima_measurements_count_ops = {
61 62
	.read = ima_show_measurements_count,
	.llseek = generic_file_llseek,
M
Mimi Zohar 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
};

/* 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();
D
Dmitry Kasatkin 已提交
91
	qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
M
Mimi Zohar 已提交
92 93 94 95 96 97 98 99 100 101
	rcu_read_unlock();
	(*pos)++;

	return (&qe->later == &ima_measurements) ? NULL : qe;
}

static void ima_measurements_stop(struct seq_file *m, void *v)
{
}

102
void ima_putc(struct seq_file *m, void *data, int datalen)
M
Mimi Zohar 已提交
103 104 105 106 107 108 109 110 111 112
{
	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
113
 *       [eventdata length]
M
Mimi Zohar 已提交
114 115 116 117 118 119 120 121 122
 *       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;
123
	bool is_ima_template = false;
124
	int i;
M
Mimi Zohar 已提交
125 126 127 128 129 130 131 132 133 134 135

	/* get entry */
	e = qe->entry;
	if (e == NULL)
		return -1;

	/*
	 * 1st: PCRIndex
	 * PCR used is always the same (config option) in
	 * little-endian format
	 */
136
	ima_putc(m, &pcr, sizeof(pcr));
M
Mimi Zohar 已提交
137 138

	/* 2nd: template digest */
139
	ima_putc(m, e->digest, TPM_DIGEST_SIZE);
M
Mimi Zohar 已提交
140 141

	/* 3rd: template name size */
142
	namelen = strlen(e->template_desc->name);
143
	ima_putc(m, &namelen, sizeof(namelen));
M
Mimi Zohar 已提交
144 145

	/* 4th:  template name */
146 147 148
	ima_putc(m, e->template_desc->name, namelen);

	/* 5th:  template length (except for 'ima' template) */
149 150 151 152
	if (strcmp(e->template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0)
		is_ima_template = true;

	if (!is_ima_template)
153 154
		ima_putc(m, &e->template_data_len,
			 sizeof(e->template_data_len));
M
Mimi Zohar 已提交
155

156 157
	/* 6th:  template specific data */
	for (i = 0; i < e->template_desc->num_fields; i++) {
158 159 160 161 162
		enum ima_show_type show = IMA_SHOW_BINARY;
		struct ima_template_field *field = e->template_desc->fields[i];

		if (is_ima_template && strcmp(field->field_id, "d") == 0)
			show = IMA_SHOW_BINARY_NO_FIELD_LEN;
163 164
		if (is_ima_template && strcmp(field->field_id, "n") == 0)
			show = IMA_SHOW_BINARY_OLD_STRING_FMT;
165
		field->field_show(m, show, &e->template_data[i]);
166
	}
M
Mimi Zohar 已提交
167 168 169
	return 0;
}

J
James Morris 已提交
170
static const struct seq_operations ima_measurments_seqops = {
M
Mimi Zohar 已提交
171 172 173 174 175 176 177 178 179 180 181
	.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);
}

182
static const struct file_operations ima_measurements_ops = {
M
Mimi Zohar 已提交
183 184 185 186 187 188
	.open = ima_measurements_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = seq_release,
};

189
void ima_print_digest(struct seq_file *m, u8 *digest, int size)
M
Mimi Zohar 已提交
190 191 192
{
	int i;

193
	for (i = 0; i < size; i++)
M
Mimi Zohar 已提交
194 195 196 197 198 199 200 201 202
		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;
203
	int i;
M
Mimi Zohar 已提交
204 205 206 207 208 209 210 211 212 213

	/* 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 */
214
	ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
M
Mimi Zohar 已提交
215 216

	/* 3th:  template name */
217
	seq_printf(m, " %s", e->template_desc->name);
M
Mimi Zohar 已提交
218 219

	/* 4th:  template specific data */
220 221 222 223 224 225 226 227 228
	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");
M
Mimi Zohar 已提交
229 230 231
	return 0;
}

J
James Morris 已提交
232
static const struct seq_operations ima_ascii_measurements_seqops = {
M
Mimi Zohar 已提交
233 234 235 236 237 238 239 240 241 242 243
	.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);
}

244
static const struct file_operations ima_ascii_measurements_ops = {
M
Mimi Zohar 已提交
245 246 247 248 249 250
	.open = ima_ascii_measurements_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = seq_release,
};

M
Mimi Zohar 已提交
251 252 253
static ssize_t ima_write_policy(struct file *file, const char __user *buf,
				size_t datalen, loff_t *ppos)
{
254 255
	char *data = NULL;
	ssize_t result;
M
Mimi Zohar 已提交
256 257

	if (datalen >= PAGE_SIZE)
258 259 260 261 262 263 264 265
		datalen = PAGE_SIZE - 1;

	/* No partial writes. */
	result = -EINVAL;
	if (*ppos != 0)
		goto out;

	result = -ENOMEM;
M
Mimi Zohar 已提交
266 267
	data = kmalloc(datalen + 1, GFP_KERNEL);
	if (!data)
268
		goto out;
M
Mimi Zohar 已提交
269 270 271

	*(data + datalen) = '\0';

272 273 274 275 276 277 278 279
	result = -EFAULT;
	if (copy_from_user(data, buf, datalen))
		goto out;

	result = ima_parse_add_rule(data);
out:
	if (result < 0)
		valid_policy = 0;
M
Mimi Zohar 已提交
280
	kfree(data);
281
	return result;
M
Mimi Zohar 已提交
282 283
}

M
Mimi Zohar 已提交
284 285 286 287 288
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;
M
Mimi Zohar 已提交
289 290
static struct dentry *ima_policy;

M
Mimi Zohar 已提交
291 292 293 294
static atomic_t policy_opencount = ATOMIC_INIT(1);
/*
 * ima_open_policy: sequentialize access to the policy file
 */
295
static int ima_open_policy(struct inode *inode, struct file *filp)
M
Mimi Zohar 已提交
296
{
297 298 299
	/* No point in being allowed to open it if you aren't going to write */
	if (!(filp->f_flags & O_WRONLY))
		return -EACCES;
M
Mimi Zohar 已提交
300 301 302 303 304
	if (atomic_dec_and_test(&policy_opencount))
		return 0;
	return -EBUSY;
}

M
Mimi Zohar 已提交
305 306 307 308
/*
 * ima_release_policy - start using the new measure policy rules.
 *
 * Initially, ima_measure points to the default policy rules, now
M
Mimi Zohar 已提交
309 310
 * point to the new policy rules, and remove the securityfs policy file,
 * assuming a valid policy.
M
Mimi Zohar 已提交
311 312 313 314 315
 */
static int ima_release_policy(struct inode *inode, struct file *file)
{
	if (!valid_policy) {
		ima_delete_rules();
M
Mimi Zohar 已提交
316 317
		valid_policy = 1;
		atomic_set(&policy_opencount, 1);
M
Mimi Zohar 已提交
318 319 320 321 322 323 324 325
		return 0;
	}
	ima_update_policy();
	securityfs_remove(ima_policy);
	ima_policy = NULL;
	return 0;
}

326
static const struct file_operations ima_measure_policy_ops = {
M
Mimi Zohar 已提交
327
	.open = ima_open_policy,
M
Mimi Zohar 已提交
328
	.write = ima_write_policy,
329 330
	.release = ima_release_policy,
	.llseek = generic_file_llseek,
M
Mimi Zohar 已提交
331
};
M
Mimi Zohar 已提交
332

333
int __init ima_fs_init(void)
M
Mimi Zohar 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
{
	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;

M
Mimi Zohar 已提交
366
	ima_policy = securityfs_create_file("policy",
367
					    S_IWUSR,
M
Mimi Zohar 已提交
368 369 370 371
					    ima_dir, NULL,
					    &ima_measure_policy_ops);
	if (IS_ERR(ima_policy))
		goto out;
M
Mimi Zohar 已提交
372

M
Mimi Zohar 已提交
373
	return 0;
M
Mimi Zohar 已提交
374
out:
375
	securityfs_remove(violations);
M
Mimi Zohar 已提交
376 377 378 379
	securityfs_remove(runtime_measurements_count);
	securityfs_remove(ascii_runtime_measurements);
	securityfs_remove(binary_runtime_measurements);
	securityfs_remove(ima_dir);
M
Mimi Zohar 已提交
380
	securityfs_remove(ima_policy);
M
Mimi Zohar 已提交
381 382
	return -1;
}