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