xref: /linux/security/integrity/ima/ima_fs.c (revision fcb0318a29696c13c9f8af0109855793a34371e6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
4  *
5  * Authors:
6  * Kylene Hall <kjhall@us.ibm.com>
7  * Reiner Sailer <sailer@us.ibm.com>
8  * Mimi Zohar <zohar@us.ibm.com>
9  *
10  * File: ima_fs.c
11  *	implemenents security file system for reporting
12  *	current measurement list and IMA statistics
13  */
14 
15 #include <linux/fcntl.h>
16 #include <linux/kernel_read_file.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/seq_file.h>
20 #include <linux/rculist.h>
21 #include <linux/rcupdate.h>
22 #include <linux/parser.h>
23 #include <linux/vmalloc.h>
24 
25 #include "ima.h"
26 
27 /*
28  * Requests:
29  * 'A\n': stage the entire measurements list
30  * 'D\n': delete all staged measurements
31  * '[1, ULONG_MAX]\n' delete N measurements records
32  */
33 #define STAGED_REQ_LENGTH 21
34 
35 static DEFINE_MUTEX(ima_write_mutex);
36 static DEFINE_MUTEX(ima_measure_mutex);
37 static long ima_measure_users;
38 static struct task_struct *measure_writer;
39 static long measure_writer_extra_writes;
40 
41 bool ima_canonical_fmt;
42 static int __init default_canonical_fmt_setup(char *str)
43 {
44 #ifdef __BIG_ENDIAN
45 	ima_canonical_fmt = true;
46 #endif
47 	return 1;
48 }
49 __setup("ima_canonical_fmt", default_canonical_fmt_setup);
50 
51 static int valid_policy = 1;
52 
53 static ssize_t ima_show_counter(char __user *buf, size_t count, loff_t *ppos,
54 				atomic_long_t *val)
55 {
56 	char tmpbuf[32];	/* greater than largest 'long' string value */
57 	ssize_t len;
58 
59 	len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", atomic_long_read(val));
60 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
61 }
62 
63 static ssize_t ima_show_num_violations(struct file *filp, char __user *buf,
64 				       size_t count, loff_t *ppos)
65 {
66 	return ima_show_counter(buf, count, ppos, &ima_num_violations);
67 }
68 
69 static const struct file_operations ima_num_violations_ops = {
70 	.read = ima_show_num_violations,
71 	.llseek = generic_file_llseek,
72 };
73 
74 static ssize_t ima_show_measurements_count(struct file *filp,
75 					   char __user *buf,
76 					   size_t count, loff_t *ppos)
77 {
78 	return ima_show_counter(buf, count, ppos, &ima_num_records[BINARY]);
79 }
80 
81 static const struct file_operations ima_measurements_count_ops = {
82 	.read = ima_show_measurements_count,
83 	.llseek = generic_file_llseek,
84 };
85 
86 /* returns pointer to hlist_node */
87 static void *_ima_measurements_start(struct seq_file *m, loff_t *pos,
88 				     struct list_head *head)
89 {
90 	loff_t l = *pos;
91 	struct ima_queue_entry *qe;
92 
93 	/* we need a lock since pos could point beyond last element */
94 	rcu_read_lock();
95 	list_for_each_entry_rcu(qe, head, later) {
96 		if (!l--) {
97 			rcu_read_unlock();
98 			return qe;
99 		}
100 	}
101 	rcu_read_unlock();
102 	return NULL;
103 }
104 
105 static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
106 {
107 	return _ima_measurements_start(m, pos, &ima_measurements);
108 }
109 
110 static void *ima_measurements_staged_start(struct seq_file *m, loff_t *pos)
111 {
112 	return _ima_measurements_start(m, pos, &ima_measurements_staged);
113 }
114 
115 static void *_ima_measurements_next(struct seq_file *m, void *v, loff_t *pos,
116 				    struct list_head *head)
117 {
118 	struct ima_queue_entry *qe = v;
119 
120 	/* lock protects when reading beyond last element
121 	 * against concurrent list-extension
122 	 */
123 	rcu_read_lock();
124 	qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
125 	rcu_read_unlock();
126 	(*pos)++;
127 
128 	return (&qe->later == head) ? NULL : qe;
129 }
130 
131 static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
132 {
133 	return _ima_measurements_next(m, v, pos, &ima_measurements);
134 }
135 
136 static void *ima_measurements_staged_next(struct seq_file *m, void *v,
137 					  loff_t *pos)
138 {
139 	return _ima_measurements_next(m, v, pos, &ima_measurements_staged);
140 }
141 
142 static void ima_measurements_stop(struct seq_file *m, void *v)
143 {
144 }
145 
146 void ima_putc(struct seq_file *m, void *data, int datalen)
147 {
148 	while (datalen--)
149 		seq_putc(m, *(char *)data++);
150 }
151 
152 /* print format:
153  *       32bit-le=pcr#
154  *       char[n]=template digest
155  *       32bit-le=template name size
156  *       char[n]=template name
157  *       [eventdata length]
158  *       eventdata[n]=template specific data
159  */
160 int ima_measurements_show(struct seq_file *m, void *v)
161 {
162 	/* the list never shrinks, so we don't need a lock here */
163 	struct ima_queue_entry *qe = v;
164 	struct ima_template_entry *e;
165 	char *template_name;
166 	u32 pcr, namelen, template_data_len; /* temporary fields */
167 	bool is_ima_template = false;
168 	int i, algo_idx;
169 
170 	algo_idx = ima_sha1_idx;
171 
172 	if (m->file != NULL)
173 		algo_idx = (unsigned long)file_inode(m->file)->i_private;
174 
175 	/* get entry */
176 	e = qe->entry;
177 	if (e == NULL)
178 		return -1;
179 
180 	template_name = (e->template_desc->name[0] != '\0') ?
181 	    e->template_desc->name : e->template_desc->fmt;
182 
183 	/*
184 	 * 1st: PCRIndex
185 	 * PCR used defaults to the same (config option) in
186 	 * little-endian format, unless set in policy
187 	 */
188 	pcr = !ima_canonical_fmt ? e->pcr : (__force u32)cpu_to_le32(e->pcr);
189 	ima_putc(m, &pcr, sizeof(e->pcr));
190 
191 	/* 2nd: template digest */
192 	ima_putc(m, e->digests[algo_idx].digest,
193 		 ima_algo_array[algo_idx].digest_size);
194 
195 	/* 3rd: template name size */
196 	namelen = !ima_canonical_fmt ? strlen(template_name) :
197 		(__force u32)cpu_to_le32(strlen(template_name));
198 	ima_putc(m, &namelen, sizeof(namelen));
199 
200 	/* 4th:  template name */
201 	ima_putc(m, template_name, strlen(template_name));
202 
203 	/* 5th:  template length (except for 'ima' template) */
204 	if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
205 		is_ima_template = true;
206 
207 	if (!is_ima_template) {
208 		template_data_len = !ima_canonical_fmt ? e->template_data_len :
209 			(__force u32)cpu_to_le32(e->template_data_len);
210 		ima_putc(m, &template_data_len, sizeof(e->template_data_len));
211 	}
212 
213 	/* 6th:  template specific data */
214 	for (i = 0; i < e->template_desc->num_fields; i++) {
215 		enum ima_show_type show = IMA_SHOW_BINARY;
216 		const struct ima_template_field *field =
217 			e->template_desc->fields[i];
218 
219 		if (is_ima_template && strcmp(field->field_id, "d") == 0)
220 			show = IMA_SHOW_BINARY_NO_FIELD_LEN;
221 		if (is_ima_template && strcmp(field->field_id, "n") == 0)
222 			show = IMA_SHOW_BINARY_OLD_STRING_FMT;
223 		field->field_show(m, show, &e->template_data[i]);
224 	}
225 	return 0;
226 }
227 
228 static const struct seq_operations ima_measurments_seqops = {
229 	.start = ima_measurements_start,
230 	.next = ima_measurements_next,
231 	.stop = ima_measurements_stop,
232 	.show = ima_measurements_show
233 };
234 
235 static const struct seq_operations ima_measurments_staged_seqops = {
236 	.start = ima_measurements_staged_start,
237 	.next = ima_measurements_staged_next,
238 	.stop = ima_measurements_stop,
239 	.show = ima_measurements_show
240 };
241 
242 static int ima_measure_lock(bool write)
243 {
244 	mutex_lock(&ima_measure_mutex);
245 	/* Overflow check. */
246 	if (!write && ima_measure_users == LONG_MAX) {
247 		mutex_unlock(&ima_measure_mutex);
248 		return -ENFILE;
249 	}
250 
251 	/* Same writer can do additional writes or read/writes. */
252 	if (write && current == measure_writer) {
253 		measure_writer_extra_writes++;
254 		mutex_unlock(&ima_measure_mutex);
255 		return 0;
256 	}
257 
258 	/*
259 	 * ima_measure_users: > 0 open readers
260 	 * ima_measure_users: == -1 open writer
261 	 */
262 	if ((write && ima_measure_users != 0) ||
263 	    (!write && ima_measure_users < 0)) {
264 		mutex_unlock(&ima_measure_mutex);
265 		return -EBUSY;
266 	}
267 
268 	if (write) {
269 		ima_measure_users--;
270 		/* Pointer valid, no reuse while the file descriptor is open. */
271 		measure_writer = current;
272 	} else {
273 		ima_measure_users++;
274 	}
275 	mutex_unlock(&ima_measure_mutex);
276 	return 0;
277 }
278 
279 static void ima_measure_unlock(bool write)
280 {
281 	mutex_lock(&ima_measure_mutex);
282 	/* Decrement additional writes or read/writes. */
283 	if (write && current == measure_writer &&
284 	    measure_writer_extra_writes != 0) {
285 		measure_writer_extra_writes--;
286 		mutex_unlock(&ima_measure_mutex);
287 		return;
288 	}
289 	if (write) {
290 		ima_measure_users++;
291 		measure_writer = NULL;
292 	} else {
293 		ima_measure_users--;
294 	}
295 	mutex_unlock(&ima_measure_mutex);
296 }
297 
298 static int _ima_measurements_open(struct inode *inode, struct file *file,
299 				  const struct seq_operations *seq_ops)
300 {
301 	bool write = (file->f_mode & FMODE_WRITE);
302 	int ret;
303 
304 	if (write && !capable(CAP_SYS_ADMIN))
305 		return -EPERM;
306 
307 	ret = ima_measure_lock(write);
308 	if (ret < 0)
309 		return ret;
310 
311 	ret = seq_open(file, seq_ops);
312 	if (ret < 0)
313 		ima_measure_unlock(write);
314 
315 	return ret;
316 }
317 
318 static int ima_measurements_open(struct inode *inode, struct file *file)
319 {
320 	return _ima_measurements_open(inode, file, &ima_measurments_seqops);
321 }
322 
323 static int ima_measurements_release(struct inode *inode, struct file *file)
324 {
325 	bool write = (file->f_mode & FMODE_WRITE);
326 	int ret;
327 
328 	/* seq_release() always returns zero. */
329 	ret = seq_release(inode, file);
330 
331 	ima_measure_unlock(write);
332 
333 	return ret;
334 }
335 
336 static int ima_measurements_staged_open(struct inode *inode, struct file *file)
337 {
338 	return _ima_measurements_open(inode, file,
339 				      &ima_measurments_staged_seqops);
340 }
341 
342 static ssize_t _ima_measurements_write(struct file *file,
343 				       const char __user *buf, size_t datalen,
344 				       loff_t *ppos, bool staged_interface)
345 {
346 	char req[STAGED_REQ_LENGTH];
347 	unsigned long req_value;
348 	int ret;
349 
350 	if (datalen < 2 || datalen > STAGED_REQ_LENGTH)
351 		return -EINVAL;
352 
353 	if (copy_from_user(req, buf, datalen) != 0)
354 		return -EFAULT;
355 
356 	if (req[datalen - 1] != '\n')
357 		return -EINVAL;
358 
359 	req[datalen - 1] = '\0';
360 
361 	switch (req[0]) {
362 	case 'A':
363 		if (datalen != 2 || !staged_interface)
364 			return -EINVAL;
365 
366 		ret = ima_queue_stage();
367 		break;
368 	case 'D':
369 		if (datalen != 2 || !staged_interface)
370 			return -EINVAL;
371 
372 		ret = ima_queue_staged_delete_all();
373 		break;
374 	default:
375 		if (staged_interface)
376 			return -EINVAL;
377 
378 		if (ima_flush_htable) {
379 			pr_debug("Deleting staged N measurements not supported when flushing the hash table is requested\n");
380 			return -EINVAL;
381 		}
382 
383 		ret = kstrtoul(req, 10, &req_value);
384 		if (ret < 0)
385 			return ret;
386 
387 		if (req_value == 0) {
388 			pr_debug("Must delete at least one entry\n");
389 			return -EINVAL;
390 		}
391 
392 		ret = ima_queue_delete_partial(req_value);
393 	}
394 
395 	if (ret < 0)
396 		return ret;
397 
398 	return datalen;
399 }
400 
401 static ssize_t ima_measurements_write(struct file *file, const char __user *buf,
402 				      size_t datalen, loff_t *ppos)
403 {
404 	return _ima_measurements_write(file, buf, datalen, ppos, false);
405 }
406 
407 static ssize_t ima_measurements_staged_write(struct file *file,
408 					     const char __user *buf,
409 					     size_t datalen, loff_t *ppos)
410 {
411 	return _ima_measurements_write(file, buf, datalen, ppos, true);
412 }
413 
414 static const struct file_operations ima_measurements_ops = {
415 	.open = ima_measurements_open,
416 	.read = seq_read,
417 	.write = ima_measurements_write,
418 	.llseek = seq_lseek,
419 	.release = ima_measurements_release,
420 };
421 
422 static const struct file_operations ima_measurements_staged_ops = {
423 	.open = ima_measurements_staged_open,
424 	.read = seq_read,
425 	.write = ima_measurements_staged_write,
426 	.llseek = seq_lseek,
427 	.release = ima_measurements_release,
428 };
429 
430 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
431 {
432 	u32 i;
433 
434 	for (i = 0; i < size; i++)
435 		seq_printf(m, "%02x", *(digest + i));
436 }
437 
438 /* print in ascii */
439 static int ima_ascii_measurements_show(struct seq_file *m, void *v)
440 {
441 	/* the list never shrinks, so we don't need a lock here */
442 	struct ima_queue_entry *qe = v;
443 	struct ima_template_entry *e;
444 	char *template_name;
445 	int i, algo_idx;
446 
447 	algo_idx = ima_sha1_idx;
448 
449 	if (m->file != NULL)
450 		algo_idx = (unsigned long)file_inode(m->file)->i_private;
451 
452 	/* get entry */
453 	e = qe->entry;
454 	if (e == NULL)
455 		return -1;
456 
457 	template_name = (e->template_desc->name[0] != '\0') ?
458 	    e->template_desc->name : e->template_desc->fmt;
459 
460 	/* 1st: PCR used (config option) */
461 	seq_printf(m, "%2d ", e->pcr);
462 
463 	/* 2nd: template hash */
464 	ima_print_digest(m, e->digests[algo_idx].digest,
465 			 ima_algo_array[algo_idx].digest_size);
466 
467 	/* 3th:  template name */
468 	seq_printf(m, " %s", template_name);
469 
470 	/* 4th:  template specific data */
471 	for (i = 0; i < e->template_desc->num_fields; i++) {
472 		seq_puts(m, " ");
473 		if (e->template_data[i].len == 0)
474 			continue;
475 
476 		e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
477 							&e->template_data[i]);
478 	}
479 	seq_puts(m, "\n");
480 	return 0;
481 }
482 
483 static const struct seq_operations ima_ascii_measurements_seqops = {
484 	.start = ima_measurements_start,
485 	.next = ima_measurements_next,
486 	.stop = ima_measurements_stop,
487 	.show = ima_ascii_measurements_show
488 };
489 
490 static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
491 {
492 	return _ima_measurements_open(inode, file,
493 				      &ima_ascii_measurements_seqops);
494 }
495 
496 static const struct file_operations ima_ascii_measurements_ops = {
497 	.open = ima_ascii_measurements_open,
498 	.read = seq_read,
499 	.write = ima_measurements_write,
500 	.llseek = seq_lseek,
501 	.release = ima_measurements_release,
502 };
503 
504 static const struct seq_operations ima_ascii_measurements_staged_seqops = {
505 	.start = ima_measurements_staged_start,
506 	.next = ima_measurements_staged_next,
507 	.stop = ima_measurements_stop,
508 	.show = ima_ascii_measurements_show
509 };
510 
511 static int ima_ascii_measurements_staged_open(struct inode *inode,
512 					      struct file *file)
513 {
514 	return _ima_measurements_open(inode, file,
515 				      &ima_ascii_measurements_staged_seqops);
516 }
517 
518 static const struct file_operations ima_ascii_measurements_staged_ops = {
519 	.open = ima_ascii_measurements_staged_open,
520 	.read = seq_read,
521 	.write = ima_measurements_staged_write,
522 	.llseek = seq_lseek,
523 	.release = ima_measurements_release,
524 };
525 
526 static ssize_t ima_read_policy(char *path)
527 {
528 	void *data = NULL;
529 	char *datap;
530 	size_t size;
531 	int rc, pathlen = strlen(path);
532 
533 	char *p;
534 
535 	/* remove \n */
536 	datap = path;
537 	strsep(&datap, "\n");
538 
539 	rc = kernel_read_file_from_path(path, 0, &data, INT_MAX, NULL,
540 					READING_POLICY);
541 	if (rc < 0) {
542 		pr_err("Unable to open file: %s (%d)", path, rc);
543 		return rc;
544 	}
545 	size = rc;
546 	rc = 0;
547 
548 	datap = data;
549 	while (size > 0 && (p = strsep(&datap, "\n"))) {
550 		pr_debug("rule: %s\n", p);
551 		rc = ima_parse_add_rule(p);
552 		if (rc < 0)
553 			break;
554 		size -= rc;
555 	}
556 
557 	vfree(data);
558 	if (rc < 0)
559 		return rc;
560 	else if (size)
561 		return -EINVAL;
562 	else
563 		return pathlen;
564 }
565 
566 static ssize_t ima_write_policy(struct file *file, const char __user *buf,
567 				size_t datalen, loff_t *ppos)
568 {
569 	char *data;
570 	ssize_t result;
571 
572 	if (datalen >= PAGE_SIZE)
573 		datalen = PAGE_SIZE - 1;
574 
575 	/* No partial writes. */
576 	result = -EINVAL;
577 	if (*ppos != 0)
578 		goto out;
579 
580 	data = memdup_user_nul(buf, datalen);
581 	if (IS_ERR(data)) {
582 		result = PTR_ERR(data);
583 		goto out;
584 	}
585 
586 	result = mutex_lock_interruptible(&ima_write_mutex);
587 	if (result < 0)
588 		goto out_free;
589 
590 	if (data[0] == '/') {
591 		result = ima_read_policy(data);
592 	} else if (ima_appraise & IMA_APPRAISE_POLICY) {
593 		pr_err("signed policy file (specified as an absolute pathname) required\n");
594 		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
595 				    "policy_update", "signed policy required",
596 				    1, 0);
597 		result = -EACCES;
598 	} else {
599 		result = ima_parse_add_rule(data);
600 	}
601 	mutex_unlock(&ima_write_mutex);
602 out_free:
603 	kfree(data);
604 out:
605 	if (result < 0)
606 		valid_policy = 0;
607 
608 	return result;
609 }
610 
611 static struct dentry *ima_dir;
612 static struct dentry *ima_symlink;
613 
614 enum ima_fs_flags {
615 	IMA_FS_BUSY,
616 };
617 
618 static unsigned long ima_fs_flags;
619 
620 #ifdef	CONFIG_IMA_READ_POLICY
621 static const struct seq_operations ima_policy_seqops = {
622 		.start = ima_policy_start,
623 		.next = ima_policy_next,
624 		.stop = ima_policy_stop,
625 		.show = ima_policy_show,
626 };
627 #endif
628 
629 static int __init create_securityfs_measurement_lists(bool staging)
630 {
631 	const struct file_operations *ascii_ops = &ima_ascii_measurements_ops;
632 	const struct file_operations *binary_ops = &ima_measurements_ops;
633 	umode_t permissions = (S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP);
634 	const char *file_suffix = "";
635 	int count = NR_BANKS(ima_tpm_chip);
636 
637 	if (staging) {
638 		ascii_ops = &ima_ascii_measurements_staged_ops;
639 		binary_ops = &ima_measurements_staged_ops;
640 		file_suffix = "_staged";
641 	}
642 
643 	if (ima_sha1_idx >= NR_BANKS(ima_tpm_chip))
644 		count++;
645 
646 	for (int i = 0; i < count; i++) {
647 		u16 algo = ima_algo_array[i].algo;
648 		char file_name[NAME_MAX + 1];
649 		struct dentry *dentry;
650 
651 		if (algo == HASH_ALGO__LAST)
652 			snprintf(file_name, sizeof(file_name),
653 				 "ascii_runtime_measurements_tpm_alg_%x%s",
654 				 ima_tpm_chip->allocated_banks[i].alg_id,
655 				 file_suffix);
656 		else
657 			snprintf(file_name, sizeof(file_name),
658 				 "ascii_runtime_measurements_%s%s",
659 				 hash_algo_name[algo], file_suffix);
660 		dentry = securityfs_create_file(file_name, permissions,
661 						ima_dir, (void *)(uintptr_t)i,
662 						ascii_ops);
663 		if (IS_ERR(dentry))
664 			return PTR_ERR(dentry);
665 
666 		if (algo == HASH_ALGO__LAST)
667 			snprintf(file_name, sizeof(file_name),
668 				 "binary_runtime_measurements_tpm_alg_%x%s",
669 				 ima_tpm_chip->allocated_banks[i].alg_id,
670 				 file_suffix);
671 		else
672 			snprintf(file_name, sizeof(file_name),
673 				 "binary_runtime_measurements_%s%s",
674 				 hash_algo_name[algo], file_suffix);
675 
676 		dentry = securityfs_create_file(file_name, permissions,
677 						ima_dir, (void *)(uintptr_t)i,
678 						binary_ops);
679 		if (IS_ERR(dentry))
680 			return PTR_ERR(dentry);
681 	}
682 
683 	return 0;
684 }
685 
686 static int __init create_securityfs_staging_links(void)
687 {
688 	struct dentry *dentry;
689 
690 	dentry = securityfs_create_symlink("binary_runtime_measurements_staged",
691 		ima_dir, "binary_runtime_measurements_sha1_staged", NULL);
692 	if (IS_ERR(dentry))
693 		return PTR_ERR(dentry);
694 
695 	dentry = securityfs_create_symlink("ascii_runtime_measurements_staged",
696 		ima_dir, "ascii_runtime_measurements_sha1_staged", NULL);
697 	if (IS_ERR(dentry))
698 		return PTR_ERR(dentry);
699 
700 	return 0;
701 }
702 
703 /*
704  * ima_open_policy: sequentialize access to the policy file
705  */
706 static int ima_open_policy(struct inode *inode, struct file *filp)
707 {
708 	if (!(filp->f_flags & O_WRONLY)) {
709 #ifndef	CONFIG_IMA_READ_POLICY
710 		return -EACCES;
711 #else
712 		if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
713 			return -EACCES;
714 		if (!capable(CAP_SYS_ADMIN))
715 			return -EPERM;
716 		return seq_open(filp, &ima_policy_seqops);
717 #endif
718 	}
719 	if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
720 		return -EBUSY;
721 	return 0;
722 }
723 
724 /*
725  * ima_release_policy - start using the new measure policy rules.
726  *
727  * Initially, ima_measure points to the default policy rules, now
728  * point to the new policy rules, and remove the securityfs policy file,
729  * assuming a valid policy.
730  */
731 static int ima_release_policy(struct inode *inode, struct file *file)
732 {
733 	const char *cause = valid_policy ? "completed" : "failed";
734 
735 	if ((file->f_flags & O_ACCMODE) == O_RDONLY)
736 		return seq_release(inode, file);
737 
738 	if (valid_policy && ima_check_policy() < 0) {
739 		cause = "failed";
740 		valid_policy = 0;
741 	}
742 
743 	pr_info("policy update %s\n", cause);
744 	integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
745 			    "policy_update", cause, !valid_policy, 0);
746 
747 	if (!valid_policy) {
748 		ima_delete_rules();
749 		valid_policy = 1;
750 		clear_bit(IMA_FS_BUSY, &ima_fs_flags);
751 		return 0;
752 	}
753 
754 	ima_update_policy();
755 #if !defined(CONFIG_IMA_WRITE_POLICY) && !defined(CONFIG_IMA_READ_POLICY)
756 	securityfs_remove(file->f_path.dentry);
757 #elif defined(CONFIG_IMA_WRITE_POLICY)
758 	clear_bit(IMA_FS_BUSY, &ima_fs_flags);
759 #elif defined(CONFIG_IMA_READ_POLICY)
760 	inode->i_mode &= ~S_IWUSR;
761 #endif
762 	return 0;
763 }
764 
765 static const struct file_operations ima_measure_policy_ops = {
766 	.open = ima_open_policy,
767 	.write = ima_write_policy,
768 	.read = seq_read,
769 	.release = ima_release_policy,
770 	.llseek = generic_file_llseek,
771 };
772 
773 int __init ima_fs_init(void)
774 {
775 	struct dentry *dentry;
776 	int ret;
777 
778 	ret = integrity_fs_init();
779 	if (ret < 0)
780 		return ret;
781 
782 	ima_dir = securityfs_create_dir("ima", integrity_dir);
783 	if (IS_ERR(ima_dir)) {
784 		ret = PTR_ERR(ima_dir);
785 		goto out;
786 	}
787 
788 	ima_symlink = securityfs_create_symlink("ima", NULL, "integrity/ima",
789 						NULL);
790 	if (IS_ERR(ima_symlink)) {
791 		ret = PTR_ERR(ima_symlink);
792 		goto out;
793 	}
794 
795 	ret = create_securityfs_measurement_lists(false);
796 	if (ret == 0 && IS_ENABLED(CONFIG_IMA_STAGING)) {
797 		ret = create_securityfs_measurement_lists(true);
798 		if (ret == 0)
799 			ret = create_securityfs_staging_links();
800 	}
801 
802 	if (ret != 0)
803 		goto out;
804 
805 	dentry = securityfs_create_symlink("binary_runtime_measurements", ima_dir,
806 				      "binary_runtime_measurements_sha1", NULL);
807 	if (IS_ERR(dentry)) {
808 		ret = PTR_ERR(dentry);
809 		goto out;
810 	}
811 
812 	dentry = securityfs_create_symlink("ascii_runtime_measurements", ima_dir,
813 				      "ascii_runtime_measurements_sha1", NULL);
814 	if (IS_ERR(dentry)) {
815 		ret = PTR_ERR(dentry);
816 		goto out;
817 	}
818 
819 	dentry = securityfs_create_file("runtime_measurements_count",
820 				   S_IRUSR | S_IRGRP, ima_dir, NULL,
821 				   &ima_measurements_count_ops);
822 	if (IS_ERR(dentry)) {
823 		ret = PTR_ERR(dentry);
824 		goto out;
825 	}
826 
827 	dentry = securityfs_create_file("violations", S_IRUSR | S_IRGRP,
828 				   ima_dir, NULL, &ima_num_violations_ops);
829 	if (IS_ERR(dentry)) {
830 		ret = PTR_ERR(dentry);
831 		goto out;
832 	}
833 
834 	dentry = securityfs_create_file("policy", POLICY_FILE_FLAGS,
835 					    ima_dir, NULL,
836 					    &ima_measure_policy_ops);
837 	if (IS_ERR(dentry)) {
838 		ret = PTR_ERR(dentry);
839 		goto out;
840 	}
841 
842 	return 0;
843 out:
844 	securityfs_remove(ima_symlink);
845 	securityfs_remove(ima_dir);
846 	integrity_fs_fini();
847 
848 	return ret;
849 }
850