From patchwork Thu Aug 4 12:24:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mimi Zohar X-Patchwork-Id: 655785 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3s4q8X0frjz9sR8 for ; Thu, 4 Aug 2016 22:32:44 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3s4q8W71wdzDrNX for ; Thu, 4 Aug 2016 22:32:43 +1000 (AEST) X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3s4pzs5YFRzDqV1 for ; Thu, 4 Aug 2016 22:25:13 +1000 (AEST) Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.11/8.16.0.11) with SMTP id u74COGbW073315 for ; Thu, 4 Aug 2016 08:25:11 -0400 Received: from e23smtp03.au.ibm.com (e23smtp03.au.ibm.com [202.81.31.145]) by mx0a-001b2d01.pphosted.com with ESMTP id 24kkak1gyb-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 04 Aug 2016 08:25:11 -0400 Received: from localhost by e23smtp03.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 4 Aug 2016 22:25:07 +1000 Received: from d23dlp01.au.ibm.com (202.81.31.203) by e23smtp03.au.ibm.com (202.81.31.209) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 4 Aug 2016 22:25:05 +1000 X-IBM-Helo: d23dlp01.au.ibm.com X-IBM-MailFrom: zohar@linux.vnet.ibm.com X-IBM-RcptTo: linuxppc-dev@lists.ozlabs.org Received: from d23relay06.au.ibm.com (d23relay06.au.ibm.com [9.185.63.219]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id A40EA2CE8056 for ; Thu, 4 Aug 2016 22:25:04 +1000 (EST) Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay06.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u74CP4aW32178212 for ; Thu, 4 Aug 2016 22:25:04 +1000 Received: from d23av03.au.ibm.com (localhost [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u74CP40m019661 for ; Thu, 4 Aug 2016 22:25:04 +1000 Received: from localhost.localdomain.localdomain ([9.80.106.198]) by d23av03.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u74COe3U018943; Thu, 4 Aug 2016 22:25:01 +1000 From: Mimi Zohar To: linux-security-module@vger.kernel.org Subject: [PATCH 5/7] ima: on soft reboot, save the measurement list Date: Thu, 4 Aug 2016 08:24:33 -0400 X-Mailer: git-send-email 2.1.0 In-Reply-To: <1470313475-20090-1-git-send-email-zohar@linux.vnet.ibm.com> References: <1470313475-20090-1-git-send-email-zohar@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16080412-0008-0000-0000-000000AD1748 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16080412-0009-0000-0000-000007C96A76 Message-Id: <1470313475-20090-6-git-send-email-zohar@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-08-04_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1604210000 definitions=main-1608040136 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Dave Young , kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-ima-devel@lists.sourceforge.net, Thiago Jung Bauermann , Mimi Zohar , linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" From: Thiago Jung Bauermann This patch uses the kexec buffer passing mechanism to pass the serialized IMA binary_runtime_measurements to the next kernel. Changelog: - updated to call IMA functions (Mimi) - move code from ima_template.c to ima_kexec.c (Mimi) Signed-off-by: Thiago Jung Bauermann Signed-off-by: Mimi Zohar --- include/linux/ima.h | 15 +++++++ kernel/kexec_file.c | 3 ++ security/integrity/ima/ima_kexec.c | 83 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/include/linux/ima.h b/include/linux/ima.h index b553367..ba484ed 100644 --- a/include/linux/ima.h +++ b/include/linux/ima.h @@ -11,6 +11,7 @@ #define _LINUX_IMA_H #include +#include struct linux_binprm; enum ima_buffer_id { @@ -35,6 +36,14 @@ extern int ima_add_measurement_check(const char *hashname, u8 *digest, loff_t size, enum ima_buffer_id buffer_id, char *hint); +#ifdef CONFIG_IMA_KEXEC +extern void ima_add_kexec_buffer(struct kimage *image); +#else +static inline void ima_add_kexec_buffer(struct kimage *image) +{ +} +#endif + #else static inline int ima_bprm_check(struct linux_binprm *bprm) { @@ -85,6 +94,12 @@ static inline int ima_add_measurement_check(const char *hashname, u8 *digest, { return 0; } + +#ifdef CONFIG_IMA_KEXEC +static inline void ima_add_kexec_buffer(struct kimage *image) +{ +} +#endif #endif /* CONFIG_IMA */ #ifdef CONFIG_IMA_APPRAISE diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 852adb2..622c126 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -202,6 +202,9 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, return ret; image->kernel_buf_len = size; + /* IMA needs to pass the measurement list to the next kernel. */ + ima_add_kexec_buffer(image); + /* Call arch image probe handlers */ ret = arch_kexec_kernel_image_probe(image, image->kernel_buf, image->kernel_buf_len); diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c index e77ca9d..3fed417 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -23,6 +23,11 @@ #include "ima.h" +#ifdef CONFIG_IMA_KEXEC +/* Physical address of the measurement buffer in the next kernel. */ +static unsigned long kexec_buffer_load_addr; +static size_t kexec_segment_size; + static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer, unsigned long segment_size) { @@ -75,6 +80,84 @@ out: } /* + * Called during kexec execute so that IMA can save the measurement list. + */ +static int ima_update_kexec_buffer(struct notifier_block *self, + unsigned long action, void *data) +{ + void *kexec_buffer = NULL; + size_t kexec_buffer_size; + int ret; + + if (!kexec_in_progress) + return NOTIFY_OK; + + kexec_buffer_size = ima_get_binary_runtime_size(); + if (kexec_buffer_size > + (kexec_segment_size - sizeof(struct ima_kexec_hdr))) { + pr_err("Binary measurement list grew too large.\n"); + goto out; + } + + ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer, + kexec_segment_size); + if (!kexec_buffer) { + pr_err("Not enough memory for the kexec measurement buffer.\n"); + goto out; + } + ret = kexec_update_segment(kexec_buffer, kexec_buffer_size, + kexec_buffer_load_addr, kexec_segment_size); + if (ret) + pr_err("Error updating kexec buffer: %d\n", ret); +out: + return NOTIFY_OK; +} + +struct notifier_block update_buffer_nb = { + .notifier_call = ima_update_kexec_buffer, +}; + +/* + * Called during kexec_file_load so that IMA can add a segment to the kexec + * image for the measurement list for the next kernel. + */ +void ima_add_kexec_buffer(struct kimage *image) +{ + struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE, + .buf_min = 0, .buf_max = ULONG_MAX, + .top_down = true }; + int ret; + + if (!kexec_can_hand_over_buffer()) + return; + + kexec_segment_size = ALIGN(ima_get_binary_runtime_size() + PAGE_SIZE, + PAGE_SIZE); + + if (kexec_segment_size >= (ULONG_MAX - sizeof(long))) { + pr_err("Binary measurement list too large.\n"); + return; + } + + /* Ask not to checksum the segment, we will update it later. */ + kbuf.buffer = NULL; + kbuf.bufsz = 0; + kbuf.memsz = kexec_segment_size; + ret = kexec_add_handover_buffer(&kbuf, false); + if (ret) { + pr_err("Error passing over kexec measurement buffer.\n"); + return; + } + kexec_buffer_load_addr = kbuf.mem; + + register_reboot_notifier(&update_buffer_nb); + + pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n", + kexec_buffer_load_addr); +} +#endif /* IMA_KEXEC */ + +/* * Restore the measurement list from the previous kernel. */ void ima_load_kexec_buffer(void)