194c3aac5SMimi Zohar /*
294c3aac5SMimi Zohar  * Copyright (C) 2016 IBM Corporation
394c3aac5SMimi Zohar  *
494c3aac5SMimi Zohar  * Authors:
594c3aac5SMimi Zohar  * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>
694c3aac5SMimi Zohar  * Mimi Zohar <zohar@linux.vnet.ibm.com>
794c3aac5SMimi Zohar  *
894c3aac5SMimi Zohar  * This program is free software; you can redistribute it and/or modify
994c3aac5SMimi Zohar  * it under the terms of the GNU General Public License as published by
1094c3aac5SMimi Zohar  * the Free Software Foundation; either version 2 of the License, or
1194c3aac5SMimi Zohar  * (at your option) any later version.
1294c3aac5SMimi Zohar  */
13de636769SPetr Vorel #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14de636769SPetr Vorel 
157b8589ccSMimi Zohar #include <linux/seq_file.h>
167b8589ccSMimi Zohar #include <linux/vmalloc.h>
177b8589ccSMimi Zohar #include <linux/kexec.h>
1894c3aac5SMimi Zohar #include "ima.h"
1994c3aac5SMimi Zohar 
207b8589ccSMimi Zohar #ifdef CONFIG_IMA_KEXEC
217b8589ccSMimi Zohar static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer,
227b8589ccSMimi Zohar 				     unsigned long segment_size)
237b8589ccSMimi Zohar {
247b8589ccSMimi Zohar 	struct ima_queue_entry *qe;
257b8589ccSMimi Zohar 	struct seq_file file;
26d68a6fe9SMimi Zohar 	struct ima_kexec_hdr khdr;
277b8589ccSMimi Zohar 	int ret = 0;
287b8589ccSMimi Zohar 
297b8589ccSMimi Zohar 	/* segment size can't change between kexec load and execute */
307b8589ccSMimi Zohar 	file.buf = vmalloc(segment_size);
317b8589ccSMimi Zohar 	if (!file.buf) {
327b8589ccSMimi Zohar 		ret = -ENOMEM;
337b8589ccSMimi Zohar 		goto out;
347b8589ccSMimi Zohar 	}
357b8589ccSMimi Zohar 
367b8589ccSMimi Zohar 	file.size = segment_size;
377b8589ccSMimi Zohar 	file.read_pos = 0;
387b8589ccSMimi Zohar 	file.count = sizeof(khdr);	/* reserved space */
397b8589ccSMimi Zohar 
40d68a6fe9SMimi Zohar 	memset(&khdr, 0, sizeof(khdr));
41d68a6fe9SMimi Zohar 	khdr.version = 1;
427b8589ccSMimi Zohar 	list_for_each_entry_rcu(qe, &ima_measurements, later) {
437b8589ccSMimi Zohar 		if (file.count < file.size) {
447b8589ccSMimi Zohar 			khdr.count++;
457b8589ccSMimi Zohar 			ima_measurements_show(&file, qe);
467b8589ccSMimi Zohar 		} else {
477b8589ccSMimi Zohar 			ret = -EINVAL;
487b8589ccSMimi Zohar 			break;
497b8589ccSMimi Zohar 		}
507b8589ccSMimi Zohar 	}
517b8589ccSMimi Zohar 
527b8589ccSMimi Zohar 	if (ret < 0)
537b8589ccSMimi Zohar 		goto out;
547b8589ccSMimi Zohar 
557b8589ccSMimi Zohar 	/*
567b8589ccSMimi Zohar 	 * fill in reserved space with some buffer details
577b8589ccSMimi Zohar 	 * (eg. version, buffer size, number of measurements)
587b8589ccSMimi Zohar 	 */
597b8589ccSMimi Zohar 	khdr.buffer_size = file.count;
60d68a6fe9SMimi Zohar 	if (ima_canonical_fmt) {
61d68a6fe9SMimi Zohar 		khdr.version = cpu_to_le16(khdr.version);
62d68a6fe9SMimi Zohar 		khdr.count = cpu_to_le64(khdr.count);
63d68a6fe9SMimi Zohar 		khdr.buffer_size = cpu_to_le64(khdr.buffer_size);
64d68a6fe9SMimi Zohar 	}
657b8589ccSMimi Zohar 	memcpy(file.buf, &khdr, sizeof(khdr));
66d68a6fe9SMimi Zohar 
677b8589ccSMimi Zohar 	print_hex_dump(KERN_DEBUG, "ima dump: ", DUMP_PREFIX_NONE,
687b8589ccSMimi Zohar 			16, 1, file.buf,
697b8589ccSMimi Zohar 			file.count < 100 ? file.count : 100, true);
707b8589ccSMimi Zohar 
717b8589ccSMimi Zohar 	*buffer_size = file.count;
727b8589ccSMimi Zohar 	*buffer = file.buf;
737b8589ccSMimi Zohar out:
747b8589ccSMimi Zohar 	if (ret == -EINVAL)
757b8589ccSMimi Zohar 		vfree(file.buf);
767b8589ccSMimi Zohar 	return ret;
777b8589ccSMimi Zohar }
787b8589ccSMimi Zohar 
797b8589ccSMimi Zohar /*
807b8589ccSMimi Zohar  * Called during kexec_file_load so that IMA can add a segment to the kexec
817b8589ccSMimi Zohar  * image for the measurement list for the next kernel.
827b8589ccSMimi Zohar  *
837b8589ccSMimi Zohar  * This function assumes that kexec_mutex is held.
847b8589ccSMimi Zohar  */
857b8589ccSMimi Zohar void ima_add_kexec_buffer(struct kimage *image)
867b8589ccSMimi Zohar {
877b8589ccSMimi Zohar 	struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE,
887b8589ccSMimi Zohar 				  .buf_min = 0, .buf_max = ULONG_MAX,
897b8589ccSMimi Zohar 				  .top_down = true };
907b8589ccSMimi Zohar 	unsigned long binary_runtime_size;
917b8589ccSMimi Zohar 
927b8589ccSMimi Zohar 	/* use more understandable variable names than defined in kbuf */
937b8589ccSMimi Zohar 	void *kexec_buffer = NULL;
947b8589ccSMimi Zohar 	size_t kexec_buffer_size;
957b8589ccSMimi Zohar 	size_t kexec_segment_size;
967b8589ccSMimi Zohar 	int ret;
977b8589ccSMimi Zohar 
987b8589ccSMimi Zohar 	/*
997b8589ccSMimi Zohar 	 * Reserve an extra half page of memory for additional measurements
1007b8589ccSMimi Zohar 	 * added during the kexec load.
1017b8589ccSMimi Zohar 	 */
1027b8589ccSMimi Zohar 	binary_runtime_size = ima_get_binary_runtime_size();
1037b8589ccSMimi Zohar 	if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE)
1047b8589ccSMimi Zohar 		kexec_segment_size = ULONG_MAX;
1057b8589ccSMimi Zohar 	else
1067b8589ccSMimi Zohar 		kexec_segment_size = ALIGN(ima_get_binary_runtime_size() +
1077b8589ccSMimi Zohar 					   PAGE_SIZE / 2, PAGE_SIZE);
1087b8589ccSMimi Zohar 	if ((kexec_segment_size == ULONG_MAX) ||
109ca79b0c2SArun KS 	    ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages() / 2)) {
1107b8589ccSMimi Zohar 		pr_err("Binary measurement list too large.\n");
1117b8589ccSMimi Zohar 		return;
1127b8589ccSMimi Zohar 	}
1137b8589ccSMimi Zohar 
1147b8589ccSMimi Zohar 	ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
1157b8589ccSMimi Zohar 				  kexec_segment_size);
1167b8589ccSMimi Zohar 	if (!kexec_buffer) {
1177b8589ccSMimi Zohar 		pr_err("Not enough memory for the kexec measurement buffer.\n");
1187b8589ccSMimi Zohar 		return;
1197b8589ccSMimi Zohar 	}
1207b8589ccSMimi Zohar 
1217b8589ccSMimi Zohar 	kbuf.buffer = kexec_buffer;
1227b8589ccSMimi Zohar 	kbuf.bufsz = kexec_buffer_size;
1237b8589ccSMimi Zohar 	kbuf.memsz = kexec_segment_size;
1247b8589ccSMimi Zohar 	ret = kexec_add_buffer(&kbuf);
1257b8589ccSMimi Zohar 	if (ret) {
1267b8589ccSMimi Zohar 		pr_err("Error passing over kexec measurement buffer.\n");
1277b8589ccSMimi Zohar 		return;
1287b8589ccSMimi Zohar 	}
1297b8589ccSMimi Zohar 
1307b8589ccSMimi Zohar 	ret = arch_ima_add_kexec_buffer(image, kbuf.mem, kexec_segment_size);
1317b8589ccSMimi Zohar 	if (ret) {
1327b8589ccSMimi Zohar 		pr_err("Error passing over kexec measurement buffer.\n");
1337b8589ccSMimi Zohar 		return;
1347b8589ccSMimi Zohar 	}
1357b8589ccSMimi Zohar 
1367b8589ccSMimi Zohar 	pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
1377b8589ccSMimi Zohar 		 kbuf.mem);
1387b8589ccSMimi Zohar }
1397b8589ccSMimi Zohar #endif /* IMA_KEXEC */
1407b8589ccSMimi Zohar 
14194c3aac5SMimi Zohar /*
14294c3aac5SMimi Zohar  * Restore the measurement list from the previous kernel.
14394c3aac5SMimi Zohar  */
14494c3aac5SMimi Zohar void ima_load_kexec_buffer(void)
14594c3aac5SMimi Zohar {
14694c3aac5SMimi Zohar 	void *kexec_buffer = NULL;
14794c3aac5SMimi Zohar 	size_t kexec_buffer_size = 0;
14894c3aac5SMimi Zohar 	int rc;
14994c3aac5SMimi Zohar 
15094c3aac5SMimi Zohar 	rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size);
15194c3aac5SMimi Zohar 	switch (rc) {
15294c3aac5SMimi Zohar 	case 0:
15394c3aac5SMimi Zohar 		rc = ima_restore_measurement_list(kexec_buffer_size,
15494c3aac5SMimi Zohar 						  kexec_buffer);
15594c3aac5SMimi Zohar 		if (rc != 0)
15694c3aac5SMimi Zohar 			pr_err("Failed to restore the measurement list: %d\n",
15794c3aac5SMimi Zohar 				rc);
15894c3aac5SMimi Zohar 
15994c3aac5SMimi Zohar 		ima_free_kexec_buffer();
16094c3aac5SMimi Zohar 		break;
16194c3aac5SMimi Zohar 	case -ENOTSUPP:
16294c3aac5SMimi Zohar 		pr_debug("Restoring the measurement list not supported\n");
16394c3aac5SMimi Zohar 		break;
16494c3aac5SMimi Zohar 	case -ENOENT:
16594c3aac5SMimi Zohar 		pr_debug("No measurement list to restore\n");
16694c3aac5SMimi Zohar 		break;
16794c3aac5SMimi Zohar 	default:
16894c3aac5SMimi Zohar 		pr_debug("Error restoring the measurement list: %d\n", rc);
16994c3aac5SMimi Zohar 	}
17094c3aac5SMimi Zohar }
171