From patchwork Fri Jan 24 22:42:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: steven chen X-Patchwork-Id: 13949990 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C68D11E7C04; Fri, 24 Jan 2025 22:43:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758637; cv=none; b=IwpZgeT9W3R6zcTeNDTC+75G46VrqpIU+ZkbN9n91IjJLhAVX8iq4WcZCaoEtPvh6PNex5yPmNaBMyUGsFlf8YF2LjquOjXclwon5r3a2dh9SWaKI3/3C+EoMi51DLEIaJhjGxOls+vxcQDXY1Xf1k38cfcYbejRj0tmbJzV/eg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758637; c=relaxed/simple; bh=Ckw2SdcMshoLK5M1DqU4ul3lnClo0tYVfVa1/aedS14=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MTbTjYWg9X3vrh7aXhTlp92lViczIRPDzQ2o/u6HHY4I6jXIVVpTB+RxDIfJS6VvKlGieVKUTak9nqqspUCEbxeh0D3yDj8/5fvXudMSNTKbcwR8UytWNwu0ZsH1UtmTjRV+2iS0b48IJVlXblVyYYjM4mkWBv40Ltg3QVvdQNg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=T+v+o8JI; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="T+v+o8JI" Received: from localhost.localdomain (unknown [167.220.59.4]) by linux.microsoft.com (Postfix) with ESMTPSA id 30784210D0C8; Fri, 24 Jan 2025 14:43:55 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 30784210D0C8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737758635; bh=DwTIiVhUQSRukvrNOWSPzDC+an9T5YM/JpQkQduIjo4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T+v+o8JI2/bRrhkxz2FoGmikU6lxlxvKqPqoP+PqAwarwofdYRhp2EdHplJYmlf+p 5O+NALEFVDP5tjEnNDnkU8VvrqcJWEhxDoeiETJRkqqRN5Tg275r/WLeVa3kY5aoFB sqrBLZ87fD/GkXr6wZyclrEl3+7KtLoo0hv+SE78= From: steven chen To: zohar@linux.ibm.com, stefanb@linux.ibm.com, roberto.sassu@huaweicloud.com, petr@tesarici.cz, eric.snowberg@oracle.com, paul@paul-moore.com, code@tyhicks.com, nramas@linux.microsoft.com, James.Bottomley@HansenPartnership.com, madvenka@linux.microsoft.com Cc: linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chenste@linux.microsoft.com Subject: [PATCH v6 1/7] ima: define and call ima_alloc_kexec_file_buf Date: Fri, 24 Jan 2025 14:42:33 -0800 Message-Id: <20250124224239.22335-2-chenste@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250124224239.22335-1-chenste@linux.microsoft.com> References: <20250124224239.22335-1-chenste@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Carrying the IMA measurement list across kexec requires allocating a buffer and copying the measurement records. Separate allocating the buffer and copying the measurement records into separate functions in order to allocate the buffer at kexec 'load' and copy the measurements at kexec 'execute'. This patch includes the following changes: - Refactor ima_dump_measurement_list() to move the memory allocation to a separate function ima_alloc_kexec_file_buf() which allocates buffer of size 'kexec_segment_size' at kexec 'load'. - Make the local variable ima_kexec_file in ima_dump_measurement_list() a local static to the file, so that it can be accessed from ima_alloc_kexec_file_buf(). Compare actual memory required to ensure there is enough memory for the entire measurement record. - Copy as many measurement events as possible. - Make necessary changes to the function ima_add_kexec_buffer() to call the above two functions. - Compared the memory size allocated with memory size of the entire measurement record. If there is not enough memory, it will copy as many IMA measurement records as possible, and this situation will result in a failure of remote attestation. Author: Tushar Sugandhi Reviewed-by: Stefan Berger Suggested-by: Mimi Zohar Signed-off-by: Tushar Sugandhi Signed-off-by: steven chen --- security/integrity/ima/ima.h | 1 + security/integrity/ima/ima_kexec.c | 105 +++++++++++++++++++++-------- security/integrity/ima/ima_queue.c | 4 +- 3 files changed, 80 insertions(+), 30 deletions(-) diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index 3c323ca213d4..447a6eb07c2d 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -274,6 +274,7 @@ bool ima_template_has_modsig(const struct ima_template_desc *ima_template); int ima_restore_measurement_entry(struct ima_template_entry *entry); int ima_restore_measurement_list(loff_t bufsize, void *buf); int ima_measurements_show(struct seq_file *m, void *v); +int ima_get_binary_runtime_entry_size(struct ima_template_entry *entry); unsigned long ima_get_binary_runtime_size(void); int ima_init_template(void); void ima_init_template_list(void); diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c index 52e00332defe..b60a902460e2 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -15,62 +15,99 @@ #include "ima.h" #ifdef CONFIG_IMA_KEXEC +static struct seq_file ima_kexec_file; + +static void ima_reset_kexec_file(struct seq_file *sf) +{ + sf->buf = NULL; + sf->size = 0; + sf->read_pos = 0; + sf->count = 0; +} + +static void ima_free_kexec_file_buf(struct seq_file *sf) +{ + vfree(sf->buf); + ima_reset_kexec_file(sf); +} + +static int ima_alloc_kexec_file_buf(size_t segment_size) +{ + /* + * kexec 'load' may be called multiple times. + * Free and realloc the buffer only if the segment_size is + * changed from the previous kexec 'load' call. + */ + if (ima_kexec_file.buf && + (ima_kexec_file.size == segment_size)) { + goto out; + } + + ima_free_kexec_file_buf(&ima_kexec_file); + + /* segment size can't change between kexec load and execute */ + ima_kexec_file.buf = vmalloc(segment_size); + if (!ima_kexec_file.buf) + return -ENOMEM; + + ima_kexec_file.size = segment_size; + +out: + ima_kexec_file.read_pos = 0; + ima_kexec_file.count = sizeof(struct ima_kexec_hdr); /* reserved space */ + + return 0; +} + static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer, unsigned long segment_size) { struct ima_queue_entry *qe; - struct seq_file file; struct ima_kexec_hdr khdr; int ret = 0; + size_t entry_size = 0; - /* segment size can't change between kexec load and execute */ - file.buf = vmalloc(segment_size); - if (!file.buf) { - ret = -ENOMEM; - goto out; + if (!ima_kexec_file.buf) { + pr_err("Kexec file buf not allocated\n"); + return -EINVAL; } - file.file = NULL; - file.size = segment_size; - file.read_pos = 0; - file.count = sizeof(khdr); /* reserved space */ - memset(&khdr, 0, sizeof(khdr)); khdr.version = 1; + + /* Copy as many IMA measurements list records as possible */ list_for_each_entry_rcu(qe, &ima_measurements, later) { - if (file.count < file.size) { + entry_size += ima_get_binary_runtime_entry_size(qe->entry); + if (entry_size <= segment_size) { khdr.count++; - ima_measurements_show(&file, qe); + ima_measurements_show(&ima_kexec_file, qe); } else { ret = -EINVAL; + pr_err("IMA log file is too big for Kexec buf\n"); break; } } - if (ret < 0) - goto out; - /* * fill in reserved space with some buffer details * (eg. version, buffer size, number of measurements) */ - khdr.buffer_size = file.count; + khdr.buffer_size = ima_kexec_file.count; if (ima_canonical_fmt) { khdr.version = cpu_to_le16(khdr.version); khdr.count = cpu_to_le64(khdr.count); khdr.buffer_size = cpu_to_le64(khdr.buffer_size); } - memcpy(file.buf, &khdr, sizeof(khdr)); + memcpy(ima_kexec_file.buf, &khdr, sizeof(khdr)); print_hex_dump_debug("ima dump: ", DUMP_PREFIX_NONE, 16, 1, - file.buf, file.count < 100 ? file.count : 100, + ima_kexec_file.buf, ima_kexec_file.count < 100 ? + ima_kexec_file.count : 100, true); - *buffer_size = file.count; - *buffer = file.buf; -out: - if (ret == -EINVAL) - vfree(file.buf); + *buffer_size = ima_kexec_file.count; + *buffer = ima_kexec_file.buf; + return ret; } @@ -89,7 +126,7 @@ void ima_add_kexec_buffer(struct kimage *image) /* use more understandable variable names than defined in kbuf */ void *kexec_buffer = NULL; - size_t kexec_buffer_size; + size_t kexec_buffer_size = 0; size_t kexec_segment_size; int ret; @@ -109,13 +146,19 @@ void ima_add_kexec_buffer(struct kimage *image) return; } - ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer, - kexec_segment_size); - if (!kexec_buffer) { + ret = ima_alloc_kexec_file_buf(kexec_segment_size); + if (ret < 0) { pr_err("Not enough memory for the kexec measurement buffer.\n"); return; } + ret = ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer, + kexec_segment_size); + if (ret < 0) { + pr_err("Failed to dump IMA measurements. Error:%d.\n", ret); + return; + } + kbuf.buffer = kexec_buffer; kbuf.bufsz = kexec_buffer_size; kbuf.memsz = kexec_segment_size; @@ -130,6 +173,12 @@ void ima_add_kexec_buffer(struct kimage *image) image->ima_buffer_size = kexec_segment_size; image->ima_buffer = kexec_buffer; + /* + * kexec owns kexec_buffer after kexec_add_buffer() is called + * and it will vfree() that buffer. + */ + ima_reset_kexec_file(&ima_kexec_file); + kexec_dprintk("kexec measurement buffer for the loaded kernel at 0x%lx.\n", kbuf.mem); } diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c index 532da87ce519..a3559bae251f 100644 --- a/security/integrity/ima/ima_queue.c +++ b/security/integrity/ima/ima_queue.c @@ -71,7 +71,7 @@ static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value, * binary_runtime_measurement list entry, which contains a * couple of variable length fields (e.g template name and data). */ -static int get_binary_runtime_size(struct ima_template_entry *entry) +int ima_get_binary_runtime_entry_size(struct ima_template_entry *entry) { int size = 0; @@ -115,7 +115,7 @@ static int ima_add_digest_entry(struct ima_template_entry *entry, if (binary_runtime_size != ULONG_MAX) { int size; - size = get_binary_runtime_size(entry); + size = ima_get_binary_runtime_entry_size(entry); binary_runtime_size = (binary_runtime_size < ULONG_MAX - size) ? binary_runtime_size + size : ULONG_MAX; } From patchwork Fri Jan 24 22:42:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: steven chen X-Patchwork-Id: 13949991 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1785E1E7C1F; Fri, 24 Jan 2025 22:43:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758637; cv=none; b=SqREswXKVLRxQ8kI1nkHsA8ueeX2JP21B0duFq3Tzfsrfft1pOSOLhzQdQTW39U5dsLsAPG9ayBVyzv7qi5SN0V/OmSCg+mYoFmxCTuPUROQQBrKOudDIW/h50mIiZb3rOTBsydR+Ake94WsQh3JRjbK8jX3gOx68mvodj6ZGfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758637; c=relaxed/simple; bh=t4XqqrYhwscr6FS7U4bhL3N2YVK27fxTi1qasHS8jzw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e9wAsWrxakLY0GAMQXDVJcnuEPy2MsGaG9WuCdaFV+OVz33kyqZxHULDDLoOh8IuzTe6LOYy4RWNSWmAdYbpXdMglJq/xmX63bMVWRNqkLhMUZplqtk36+BieOl3TN6F0dunf3Le2c4IX48tQrVnGL3MpP7YuW9ZYMsClIN9vZg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=P8gCbu2R; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="P8gCbu2R" Received: from localhost.localdomain (unknown [167.220.59.4]) by linux.microsoft.com (Postfix) with ESMTPSA id 5E4BD210D0C9; Fri, 24 Jan 2025 14:43:55 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5E4BD210D0C9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737758635; bh=l74eF+4+r695BFOwyov/kRxEW4Z5jvFKpT/pUDEgh3Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P8gCbu2R60YGpB/N4bk0WnJE4C0o6SXbtVdYOl542XrfasQwU8WQF+kBMJq4cEL0f dYJGN/0huozCQJ2iQFZ77A2vlYvD9ll67/gbF+Zipo49WbpK2iShq+HtBMBN8yMnrP 2W/OL8AP14TR8EQIGcNTtlO2jtI203UbZ2tikaFQ= From: steven chen To: zohar@linux.ibm.com, stefanb@linux.ibm.com, roberto.sassu@huaweicloud.com, petr@tesarici.cz, eric.snowberg@oracle.com, paul@paul-moore.com, code@tyhicks.com, nramas@linux.microsoft.com, James.Bottomley@HansenPartnership.com, madvenka@linux.microsoft.com Cc: linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chenste@linux.microsoft.com Subject: [PATCH v6 2/7] kexec: define functions to map and unmap segments Date: Fri, 24 Jan 2025 14:42:34 -0800 Message-Id: <20250124224239.22335-3-chenste@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250124224239.22335-1-chenste@linux.microsoft.com> References: <20250124224239.22335-1-chenste@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently, the mechanism to map and unmap segments to the kimage structure is not available to the subsystems outside of kexec. This functionality is needed when IMA is allocating the memory segments during kexec 'load' operation. Implement functions to map and unmap segments to kimage. Implement kimage_map_segment() to enable mapping of IMA buffer source pages to the kimage structure post kexec 'load'. This function, accepting a kimage pointer, an address, and a size, will gather the source pages within the specified address range, create an array of page pointers, and map these to a contiguous virtual address range. The function returns the start of this range if successful, or NULL if unsuccessful. Implement kimage_unmap_segment() for unmapping segments using vunmap(). From: Tushar Sugandhi Author: Tushar Sugandhi Reviewed-by: Stefan Berger Reviewed-by: Mimi Zohar Signed-off-by: Tushar Sugandhi Signed-off-by: steven chen --- include/linux/kexec.h | 7 ++++++ kernel/kexec_core.c | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f0e9f8eda7a3..f8413ea5c8c8 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -467,6 +467,9 @@ extern bool kexec_file_dbg_print; #define kexec_dprintk(fmt, arg...) \ do { if (kexec_file_dbg_print) pr_info(fmt, ##arg); } while (0) +extern void *kimage_map_segment(struct kimage *image, + unsigned long addr, unsigned long size); +extern void kimage_unmap_segment(void *buffer); #else /* !CONFIG_KEXEC_CORE */ struct pt_regs; struct task_struct; @@ -474,6 +477,10 @@ static inline void __crash_kexec(struct pt_regs *regs) { } static inline void crash_kexec(struct pt_regs *regs) { } static inline int kexec_should_crash(struct task_struct *p) { return 0; } static inline int kexec_crash_loaded(void) { return 0; } +static inline void *kimage_map_segment(struct kimage *image, + unsigned long addr, unsigned long size) +{ return NULL; } +static inline void kimage_unmap_segment(void *buffer) { } #define kexec_in_progress false #endif /* CONFIG_KEXEC_CORE */ diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index c0caa14880c3..4029df8f6860 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -867,6 +867,60 @@ int kimage_load_segment(struct kimage *image, return result; } +void *kimage_map_segment(struct kimage *image, + unsigned long addr, unsigned long size) +{ + unsigned long eaddr = addr + size; + unsigned long src_page_addr, dest_page_addr; + unsigned int npages; + struct page **src_pages; + int i; + kimage_entry_t *ptr, entry; + void *vaddr = NULL; + + /* + * Collect the source pages and map them in a contiguous VA range. + */ + npages = PFN_UP(eaddr) - PFN_DOWN(addr); + src_pages = kmalloc_array(npages, sizeof(*src_pages), GFP_KERNEL); + if (!src_pages) { + pr_err("Could not allocate ima pages array.\n"); + return NULL; + } + + i = 0; + for_each_kimage_entry(image, ptr, entry) { + if (entry & IND_DESTINATION) + dest_page_addr = entry & PAGE_MASK; + else if (entry & IND_SOURCE) { + if (dest_page_addr >= addr && dest_page_addr < eaddr) { + src_page_addr = entry & PAGE_MASK; + src_pages[i++] = + virt_to_page(__va(src_page_addr)); + if (i == npages) + break; + dest_page_addr += PAGE_SIZE; + } + } + } + + /* Sanity check. */ + WARN_ON(i < npages); + + vaddr = vmap(src_pages, npages, VM_MAP, PAGE_KERNEL); + kfree(src_pages); + + if (!vaddr) + pr_err("Could not map ima buffer.\n"); + + return vaddr; +} + +void kimage_unmap_segment(void *segment_buffer) +{ + vunmap(segment_buffer); +} + struct kexec_load_limit { /* Mutex protects the limit count. */ struct mutex mutex; From patchwork Fri Jan 24 22:42:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: steven chen X-Patchwork-Id: 13949992 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3306D1E7C34; Fri, 24 Jan 2025 22:43:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758637; cv=none; b=lc0WTOoFLhmtMWhId1m/6t8T7LAnIgAtYXs/BChmHaAmqn+Nyv/6D5l88Ewa20GyVWXKP3h40EDhr55/pf7+IUKlKduuVuA+r/YOaMb98251DLPx/D86hGaUIkGQ0s6/xkiCJ0NIPy2Mt4ZCelSjW/pYd8/Tij0XLSq4lMwJ7L8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758637; c=relaxed/simple; bh=+/vM8Wp0in6MfYqVPUlNCdxA+Ip9RDetizxsc5AYjso=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AfxuS2NyZ2qaNs0YWu3stb0lQZBJNbi7lCEzAGyd8f7gxrEAjC4hgj8dw9MwSTttws+gHps5/BuisrferxAucUCC//iPpbs0bLTAE1oCR5eF34Ed0vShEd/DpIxBBwU45SmJxR0Gcq1oTV0P715T+sg8Jn/BgsX5J9P3pJf2zsY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=MIHYYHVd; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="MIHYYHVd" Received: from localhost.localdomain (unknown [167.220.59.4]) by linux.microsoft.com (Postfix) with ESMTPSA id 9101D210D0CA; Fri, 24 Jan 2025 14:43:55 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 9101D210D0CA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737758635; bh=bmgLmYsYlhYHbjbOJ7FsAVpXQjF5XYvbaC/P+6RVs/M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MIHYYHVdvHhDoyS5mr7/GCJk1RVU2krxQ2Psp2hcplXEXtEih4JJuGrIiwctVoDlM aPDv988eCSoCtgrwYckZ0SVkm6CkIbAFG8939JP20QlsyVbrENLWybrsGuNnXG32vB qquu57rF1NZQZcKdcrL1WcOjmEgy/Apj3ky3MHiU= From: steven chen To: zohar@linux.ibm.com, stefanb@linux.ibm.com, roberto.sassu@huaweicloud.com, petr@tesarici.cz, eric.snowberg@oracle.com, paul@paul-moore.com, code@tyhicks.com, nramas@linux.microsoft.com, James.Bottomley@HansenPartnership.com, madvenka@linux.microsoft.com Cc: linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chenste@linux.microsoft.com Subject: [PATCH v6 3/7] ima: kexec: skip IMA segment validation after kexec soft reboot Date: Fri, 24 Jan 2025 14:42:35 -0800 Message-Id: <20250124224239.22335-4-chenste@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250124224239.22335-1-chenste@linux.microsoft.com> References: <20250124224239.22335-1-chenste@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 kexec_calculate_store_digests() calculates and stores the digest of the segment at kexec_file_load syscall where the IMA segment is also allocated. With this series, the IMA segment will be updated with the measurement log at kexec soft reboot. Therefore, it may fail digest verification in verify_sha256_digest() after kexec soft reboot into the new Kernel. Therefore, the digest calculation/verification of the IMA segment needs to be skipped. Skip IMA segment from calculating and storing digest in function kexec_calculate_store_digests() so that it is not added to the 'purgatory_sha_regions'. Since verify_sha256_digest() only verifies 'purgatory_sha_regions', no change is needed in verify_sha256_digest() in this context. With this change, the IMA segment is not included in the digest calculation, storage, and verification. Author: Tushar Sugandhi Signed-off-by: Tushar Sugandhi Signed-off-by: steven chen --- include/linux/kexec.h | 3 +++ kernel/kexec_file.c | 10 ++++++++++ security/integrity/ima/ima_kexec.c | 3 +++ 3 files changed, 16 insertions(+) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f8413ea5c8c8..f3246e881ac8 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -362,6 +362,9 @@ struct kimage { phys_addr_t ima_buffer_addr; size_t ima_buffer_size; + + unsigned long ima_segment_index; + bool is_ima_segment_index_set; #endif /* Core ELF header buffer */ diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 3eedb8c226ad..4ff3ba0f3e8e 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -764,6 +764,16 @@ static int kexec_calculate_store_digests(struct kimage *image) if (ksegment->kbuf == pi->purgatory_buf) continue; +#ifdef CONFIG_IMA_KEXEC + /* + * Skip the segment if ima_segment_index is set and matches + * the current index + */ + if (image->is_ima_segment_index_set && + i == image->ima_segment_index) + continue; +#endif + ret = crypto_shash_update(desc, ksegment->kbuf, ksegment->bufsz); if (ret) diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c index b60a902460e2..283860d20521 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -162,6 +162,7 @@ void ima_add_kexec_buffer(struct kimage *image) kbuf.buffer = kexec_buffer; kbuf.bufsz = kexec_buffer_size; kbuf.memsz = kexec_segment_size; + image->is_ima_segment_index_set = false; ret = kexec_add_buffer(&kbuf); if (ret) { pr_err("Error passing over kexec measurement buffer.\n"); @@ -172,6 +173,8 @@ void ima_add_kexec_buffer(struct kimage *image) image->ima_buffer_addr = kbuf.mem; image->ima_buffer_size = kexec_segment_size; image->ima_buffer = kexec_buffer; + image->ima_segment_index = image->nr_segments - 1; + image->is_ima_segment_index_set = true; /* * kexec owns kexec_buffer after kexec_add_buffer() is called From patchwork Fri Jan 24 22:42:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: steven chen X-Patchwork-Id: 13949993 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6244D1E98EA; Fri, 24 Jan 2025 22:43:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758638; cv=none; b=YatZjpVkONQ1M/RLloUXJUd3hQVgKsunDNaJGvlRFI2qR6jyB1E3QrPUTCDSQi3hd/6Pgyg0LFUF4AAmB5m/8W9yMuqNdUC0Peo1ym/oBvOjFXl/dMuAbvVmwGYi3U0bY4DwPfAPtof1fnBr3JNlAOsQrG0/tZkE5MaXIkeGU+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758638; c=relaxed/simple; bh=EGSzYZ/wUc8bEEIuGyf1CrDl3ntDab0/DRlAcjZi9fE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=E+kcYbRG7TIyiTk1BacROoVB/WGFWm6IP77FG1Z9r9a5RRgj/syx+0QBETrQLqDgUH8t293gir6mRCvGMSRKZMgBbMMzq3rzpdox7lpAnyx9eqt29awdUhmdk27xxPARuJtxjelWhetVgAwdBf3T12iQjN9b9xmq4odB89BJXFg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=ZkP8MpnW; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="ZkP8MpnW" Received: from localhost.localdomain (unknown [167.220.59.4]) by linux.microsoft.com (Postfix) with ESMTPSA id BED92210D0CB; Fri, 24 Jan 2025 14:43:55 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com BED92210D0CB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737758635; bh=VwNfvVLfL00S8Xn+X+PulIftmjpln6BGP2uOmQnQBPE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZkP8MpnWGiucQtWzr+cPpMTHt+aYFLGm06YFaBIYeMV1MUiSL0SEoviSaV8Ne8+HG eMY4TdpTQ+mmuNpzqy35+xBZhAmfXebn2Mh7AazoVPY6f99oHKGoA41yD6RxpdXAZo To3Op9skiiHUM8X94jo4aFUGXTHBBSWKkmhjV9Fg= From: steven chen To: zohar@linux.ibm.com, stefanb@linux.ibm.com, roberto.sassu@huaweicloud.com, petr@tesarici.cz, eric.snowberg@oracle.com, paul@paul-moore.com, code@tyhicks.com, nramas@linux.microsoft.com, James.Bottomley@HansenPartnership.com, madvenka@linux.microsoft.com Cc: linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chenste@linux.microsoft.com Subject: [PATCH v6 4/7] ima: kexec: define functions to copy IMA log at soft boot Date: Fri, 24 Jan 2025 14:42:36 -0800 Message-Id: <20250124224239.22335-5-chenste@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250124224239.22335-1-chenste@linux.microsoft.com> References: <20250124224239.22335-1-chenste@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 IMA log is copied to the new Kernel during kexec 'load' using ima_dump_measurement_list(). The log copy at kexec 'load' may result in loss of IMA measurements during kexec soft reboot. It needs to be copied over during kexec 'execute'. Setup the needed infrastructure to move the IMA log copy from kexec 'load' to 'execute'. Define a new IMA hook ima_update_kexec_buffer() as a stub function. It will be used to call ima_dump_measurement_list() during kexec 'execute'. Implement ima_kexec_post_load() function to be invoked after the new Kernel image has been loaded for kexec. ima_kexec_post_load() maps the IMA buffer to a segment in the newly loaded Kernel. It also registers the reboot notifier_block to trigger ima_update_kexec_buffer() at exec 'execute'. Author: Tushar Sugandhi Reviewed-by: Stefan Berger Suggested-by: Mimi Zohar Reviewed-by: "Petr Tesařík" Signed-off-by: Tushar Sugandhi Signed-off-by: steven chen --- include/linux/ima.h | 3 ++ security/integrity/ima/ima_kexec.c | 46 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/ima.h b/include/linux/ima.h index 0bae61a15b60..8e29cb4e6a01 100644 --- a/include/linux/ima.h +++ b/include/linux/ima.h @@ -32,6 +32,9 @@ static inline void ima_appraise_parse_cmdline(void) {} #ifdef CONFIG_IMA_KEXEC extern void ima_add_kexec_buffer(struct kimage *image); +extern void ima_kexec_post_load(struct kimage *image); +#else +static inline void ima_kexec_post_load(struct kimage *image) {} #endif #else diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c index 283860d20521..854b90d34e2d 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -12,10 +12,14 @@ #include #include #include +#include +#include #include "ima.h" #ifdef CONFIG_IMA_KEXEC static struct seq_file ima_kexec_file; +static void *ima_kexec_buffer; +static bool ima_kexec_update_registered; static void ima_reset_kexec_file(struct seq_file *sf) { @@ -185,6 +189,48 @@ void ima_add_kexec_buffer(struct kimage *image) kexec_dprintk("kexec measurement buffer for the loaded kernel at 0x%lx.\n", kbuf.mem); } + +/* + * Called during kexec execute so that IMA can update the measurement list. + */ +static int ima_update_kexec_buffer(struct notifier_block *self, + unsigned long action, void *data) +{ + return NOTIFY_OK; +} + +struct notifier_block update_buffer_nb = { + .notifier_call = ima_update_kexec_buffer, +}; + +/* + * Create a mapping for the source pages that contain the IMA buffer + * so we can update it later. + */ +void ima_kexec_post_load(struct kimage *image) +{ + if (ima_kexec_buffer) { + kimage_unmap_segment(ima_kexec_buffer); + ima_kexec_buffer = NULL; + } + + if (!image->ima_buffer_addr) + return; + + ima_kexec_buffer = kimage_map_segment(image, + image->ima_buffer_addr, + image->ima_buffer_size); + if (!ima_kexec_buffer) { + pr_err("Could not map measurements buffer.\n"); + return; + } + + if (!ima_kexec_update_registered) { + register_reboot_notifier(&update_buffer_nb); + ima_kexec_update_registered = true; + } +} + #endif /* IMA_KEXEC */ /* From patchwork Fri Jan 24 22:42:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: steven chen X-Patchwork-Id: 13949997 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B99FC1EE009; Fri, 24 Jan 2025 22:43:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758640; cv=none; b=YJE2Mb3NKQ1isZPfbY3ylTh0suGs8P9lxJ0YQtmEGwdLNTPSXI6phy/UDM/IVPmjea9leTA9z/+8CXSRQtkXdsD3SqRCP1yQkkeiZhsVjelp8X4PJ8jRltiSJfc3yeFLdw/+yqpw1cncwTORkb/0Qp7LyupFCjeJnB9TluaGAqU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758640; c=relaxed/simple; bh=Pxre783PzACACeV34ZisP2a76BNcKKtQ+N5YCrzjsfo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h2Ue2GVoKg6AFmWxX7cKGClPGblJVPXGitQWrdp+yvHYavcSlfGFER6py9BSL7RKWv7WwKMoccuxjGRyUpQ9xEujSNCVbK6gKKUTLvgsVpZXZ35ltjSvojRAfvM5K3nKLB8lYCqDbKmw3onkhTUEx5hG5IZAacIU2fRZAJCLP8M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=JkM3uRhD; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="JkM3uRhD" Received: from localhost.localdomain (unknown [167.220.59.4]) by linux.microsoft.com (Postfix) with ESMTPSA id ED00520BEBD5; Fri, 24 Jan 2025 14:43:55 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com ED00520BEBD5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737758636; bh=SToxeWWYWW6LWJe1NQgF6XwKRGWLZYBJcF4B7FrieaE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JkM3uRhDsMp/kxo6zxlneH22Jq0OsN7XQZWhDethQmm5YK0/K2e4Gsyd2wFh3G3WA Q1N3LxVpEnngKoT58i7DfVc3JcgDDWNqKVYo6t3/JZuAbxHHMKH6k3VoU2eCQP36gL 5XOrCXILB0SWmrJFaFfpYGE+Fo2ALlP9o+lxkwxU= From: steven chen To: zohar@linux.ibm.com, stefanb@linux.ibm.com, roberto.sassu@huaweicloud.com, petr@tesarici.cz, eric.snowberg@oracle.com, paul@paul-moore.com, code@tyhicks.com, nramas@linux.microsoft.com, James.Bottomley@HansenPartnership.com, madvenka@linux.microsoft.com Cc: linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chenste@linux.microsoft.com Subject: [PATCH v6 5/7] ima: kexec: move IMA log copy from kexec load to execute Date: Fri, 24 Jan 2025 14:42:37 -0800 Message-Id: <20250124224239.22335-6-chenste@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250124224239.22335-1-chenste@linux.microsoft.com> References: <20250124224239.22335-1-chenste@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 ima_dump_measurement_list() is called during kexec 'load', which may result in loss of IMA measurements during kexec soft reboot. It needs to be called during kexec 'execute'. This patch includes the following changes: - Implement kimage_file_post_load() function to be invoked after the new Kernel image has been loaded for kexec. - Call kimage_file_post_load() from kexec_file_load() syscall only for kexec soft reboot scenarios and not for KEXEC_FILE_ON_CRASH. It will map the IMA segment, and register reboot notifier for the function ima_update_kexec_buffer() which would copy the IMA log at kexec soft reboot. - Make kexec_segment_size variable local static to the file, for it to be accessible both during kexec 'load' and 'execute'. - Move ima_dump_measurement_list() call from ima_add_kexec_buffer() to ima_update_kexec_buffer(). - Remove ima_reset_kexec_file() call from ima_add_kexec_buffer(), now that the buffer is being copied at kexec 'execute', and resetting the file at kexec 'load' will corrupt the buffer. Author: Tushar Sugandhi Reviewed-by: Tyler Hicks Signed-off-by: Tushar Sugandhi Signed-off-by: steven chen --- kernel/kexec_file.c | 8 ++++++ security/integrity/ima/ima_kexec.c | 43 +++++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 4ff3ba0f3e8e..d05ce3ee2e53 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -186,6 +186,11 @@ kimage_validate_signature(struct kimage *image) } #endif +static void kimage_file_post_load(struct kimage *image) +{ + ima_kexec_post_load(image); +} + /* * In file mode list of segments is prepared by kernel. Copy relevant * data from user space, do error checking, prepare segment list @@ -413,6 +418,9 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd, kimage_terminate(image); + if (!(flags & KEXEC_FILE_ON_CRASH)) + kimage_file_post_load(image); + ret = machine_kexec_post_load(image); if (ret) goto out; diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c index 854b90d34e2d..d5f004cfeaec 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -19,6 +19,7 @@ #ifdef CONFIG_IMA_KEXEC static struct seq_file ima_kexec_file; static void *ima_kexec_buffer; +static size_t kexec_segment_size; static bool ima_kexec_update_registered; static void ima_reset_kexec_file(struct seq_file *sf) @@ -131,7 +132,6 @@ void ima_add_kexec_buffer(struct kimage *image) /* use more understandable variable names than defined in kbuf */ void *kexec_buffer = NULL; size_t kexec_buffer_size = 0; - size_t kexec_segment_size; int ret; /* @@ -156,13 +156,6 @@ void ima_add_kexec_buffer(struct kimage *image) return; } - ret = ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer, - kexec_segment_size); - if (ret < 0) { - pr_err("Failed to dump IMA measurements. Error:%d.\n", ret); - return; - } - kbuf.buffer = kexec_buffer; kbuf.bufsz = kexec_buffer_size; kbuf.memsz = kexec_segment_size; @@ -180,12 +173,6 @@ void ima_add_kexec_buffer(struct kimage *image) image->ima_segment_index = image->nr_segments - 1; image->is_ima_segment_index_set = true; - /* - * kexec owns kexec_buffer after kexec_add_buffer() is called - * and it will vfree() that buffer. - */ - ima_reset_kexec_file(&ima_kexec_file); - kexec_dprintk("kexec measurement buffer for the loaded kernel at 0x%lx.\n", kbuf.mem); } @@ -196,7 +183,33 @@ void ima_add_kexec_buffer(struct kimage *image) static int ima_update_kexec_buffer(struct notifier_block *self, unsigned long action, void *data) { - return NOTIFY_OK; + void *buf = NULL; + size_t buf_size = 0; + int ret = NOTIFY_OK; + + if (!kexec_in_progress) { + pr_info("No kexec in progress.\n"); + return ret; + } + + if (!ima_kexec_buffer) { + pr_err("Kexec buffer not set.\n"); + return ret; + } + + ret = ima_dump_measurement_list(&buf_size, &buf, + kexec_segment_size); + + if (ret) + pr_err("Dump measurements failed. Error:%d\n", ret); + + if (buf_size != 0) + memcpy(ima_kexec_buffer, buf, buf_size); + + kimage_unmap_segment(ima_kexec_buffer); + ima_kexec_buffer = NULL; + + return ret; } struct notifier_block update_buffer_nb = { From patchwork Fri Jan 24 22:42:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: steven chen X-Patchwork-Id: 13949995 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 928591E98FC; Fri, 24 Jan 2025 22:43:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758639; cv=none; b=b/HnslO5dcxWDvdPiDpNTvHghIQiN9+hzm/pIN7NGoPrGRXPIbwk0+pARRWFqXu1CikIIjb+nhk8GYnSYd8iLOo2PJK3yythTTxc8PAsq4l1qr8ctvgMu6UfMrCmXAHQY7Xcmn5rUgTh4lu5RG+shlaYwIhr1g392dkoPjZTNjg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758639; c=relaxed/simple; bh=jn33izLaF7k8JTTLeThaIMhJzaisLVYTCVaFAQihJ4o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=G4hAHODXNFB86ShV8fdE+iGziCG25N6FxGnnBMY+9WDZsdD7yQC2PC2ryujoX80lD8DnrdmHIn9jOeaKozK5ZtgEex+n/7Kdf4gqeeTvnEDhVXvAmCBVO9M4OzIqZY3eeicibY90SqvBhmVljrwSMaWwbsjw9ie/0yhB2tB9bsc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=doMACrdr; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="doMACrdr" Received: from localhost.localdomain (unknown [167.220.59.4]) by linux.microsoft.com (Postfix) with ESMTPSA id 27492210D0CC; Fri, 24 Jan 2025 14:43:56 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 27492210D0CC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737758636; bh=geSM+tomMYqqRJERm+x35GcWFvYZOAcnOC3d66dp8xI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=doMACrdrdSbNqRyH4SUEynzTSKCCWaXeqT7wCdTeK/LvoFun5vo5d4iGs64ijwjmS fxLVP4rxxAKjeI6YSIcoejVHQyf9t4vpXRQ+R2pfpxEqoRHNgvlFVQdEPoKjasGv/0 vBFt1SyNsgd/NvHqrq1ADGg/pwwrIlJvWDJREzEc= From: steven chen To: zohar@linux.ibm.com, stefanb@linux.ibm.com, roberto.sassu@huaweicloud.com, petr@tesarici.cz, eric.snowberg@oracle.com, paul@paul-moore.com, code@tyhicks.com, nramas@linux.microsoft.com, James.Bottomley@HansenPartnership.com, madvenka@linux.microsoft.com Cc: linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chenste@linux.microsoft.com Subject: [PATCH v6 6/7] ima: make the kexec extra memory configurable Date: Fri, 24 Jan 2025 14:42:38 -0800 Message-Id: <20250124224239.22335-7-chenste@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250124224239.22335-1-chenste@linux.microsoft.com> References: <20250124224239.22335-1-chenste@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The extra memory allocated for carrying the IMA measurement list across kexec is hard-coded as half a PAGE. Make it configurable. Define a Kconfig option, IMA_KEXEC_EXTRA_MEMORY_KB, to configure the extra memory (in kb) to be allocated for IMA measurements added during kexec soft reboot. Ensure the default value of the option is set such that extra half a page of memory for additional measurements is allocated for the additional measurements. Update ima_add_kexec_buffer() function to allocate memory based on the Kconfig option value, rather than the currently hard-coded one. From: Tushar Sugandhi Author: Tushar Sugandhi Suggested-by: Stefan Berger Signed-off-by: Tushar Sugandhi Signed-off-by: steven chen --- security/integrity/ima/Kconfig | 10 ++++++++++ security/integrity/ima/ima_kexec.c | 16 ++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/security/integrity/ima/Kconfig b/security/integrity/ima/Kconfig index 475c32615006..7dd2ed8b2cdc 100644 --- a/security/integrity/ima/Kconfig +++ b/security/integrity/ima/Kconfig @@ -321,4 +321,14 @@ config IMA_DISABLE_HTABLE help This option disables htable to allow measurement of duplicate records. +config IMA_KEXEC_EXTRA_MEMORY_KB + int + depends on IMA_KEXEC + default 0 + help + IMA_KEXEC_EXTRA_MEMORY_KB determines the extra memory to be + allocated (in kb) for IMA measurements added during kexec soft reboot. + If set to the default value, an extra half a page of memory for those + additional measurements will be allocated. + endif diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c index d5f004cfeaec..c9c916f69ca7 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -128,22 +128,26 @@ void ima_add_kexec_buffer(struct kimage *image) .buf_min = 0, .buf_max = ULONG_MAX, .top_down = true }; unsigned long binary_runtime_size; - + unsigned long extra_size; /* use more understandable variable names than defined in kbuf */ void *kexec_buffer = NULL; size_t kexec_buffer_size = 0; int ret; /* - * Reserve an extra half page of memory for additional measurements - * added during the kexec load. + * Reserve extra memory for measurements added during kexec. */ - binary_runtime_size = ima_get_binary_runtime_size(); + if (CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB <= 0) + extra_size = PAGE_SIZE / 2; + else + extra_size = CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB * 1024; + binary_runtime_size = ima_get_binary_runtime_size() + extra_size; + if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE) kexec_segment_size = ULONG_MAX; else - kexec_segment_size = ALIGN(ima_get_binary_runtime_size() + - PAGE_SIZE / 2, PAGE_SIZE); + kexec_segment_size = ALIGN(binary_runtime_size, PAGE_SIZE); + if ((kexec_segment_size == ULONG_MAX) || ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages() / 2)) { pr_err("Binary measurement list too large.\n"); From patchwork Fri Jan 24 22:42:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: steven chen X-Patchwork-Id: 13949996 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8A241EE7B3; Fri, 24 Jan 2025 22:43:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758639; cv=none; b=oiHfJ5/AKSApSX34z27w5fK36accJMg3p6dt2spzHneMC8T6gOgoEXUoXLuvoZ+YVG9VHtT8MJozVAeGycUUrVO61v9PLYV3WGU/tP+xm119/OQy4iuxgvOpsTMM+iDU7cJe0KWbLQh/Om7frs0yGctwpz1iiNpqhgZmrAYnQY8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737758639; c=relaxed/simple; bh=XcCYE/P+N1xe2haU4t0///zpxWhVLSW13VRkFoNCq2o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Rp+33cC+ZTV4z6iVrwaKo3htFYa9n46nUei4OczC11PfQ8WEddkgcv/RzqPg0D9Q3KldgsgGSMTbNPohPulO0ooJN2adkyk5oirWagRKsZEkOWX/juOolZTbWtA+rTuG1kKHCxV7+2WEvezPyfZ+h/UTw2MhMi4ZSAlMunq2jaU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=AEm/mO+6; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="AEm/mO+6" Received: from localhost.localdomain (unknown [167.220.59.4]) by linux.microsoft.com (Postfix) with ESMTPSA id 55621210D0CD; Fri, 24 Jan 2025 14:43:56 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 55621210D0CD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737758636; bh=6H+05HlDdwLJYuzzGaRsVd9LIBrCOlgbmTBvfn2uPEs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AEm/mO+6kketxvjE6PnN0AsHzqZddslTQvpRaKATfKb703GXRzKt5oa9jYFRS7e+y x4LhUt/TZOZ22LnNBGTYvh6cuVZKgEdIBYu4oaJaOjaVz8JC1WnxUokiUG/7Keijx0 Vfaxv0+yZqXZLDRo/SzOzb7ZetxHpgGNYfrlApjs= From: steven chen To: zohar@linux.ibm.com, stefanb@linux.ibm.com, roberto.sassu@huaweicloud.com, petr@tesarici.cz, eric.snowberg@oracle.com, paul@paul-moore.com, code@tyhicks.com, nramas@linux.microsoft.com, James.Bottomley@HansenPartnership.com, madvenka@linux.microsoft.com Cc: linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chenste@linux.microsoft.com Subject: [PATCH 7/7] ima: measure kexec load and exec events as critical data Date: Fri, 24 Jan 2025 14:42:39 -0800 Message-Id: <20250124224239.22335-8-chenste@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250124224239.22335-1-chenste@linux.microsoft.com> References: <20250124224239.22335-1-chenste@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The amount of memory allocated at kexec load, even with the extra memory allocated, might not be large enough for the entire measurement list. The indeterminate interval between kexec 'load' and 'execute' could exacerbate this problem. Define two new IMA events, 'kexec_load' and 'kexec_execute', to be measured as critical data at kexec 'load' and 'execute' respectively. Report the allocated kexec segment size, IMA binary log size and the runtime measurements count as part of those events. These events, and the values reported through them, serve as markers in the IMA log to verify the IMA events are captured during kexec soft reboot. The presence of a 'kexec_load' event in between the last two 'boot_aggregate' events in the IMA log implies this is a kexec soft reboot, and not a cold-boot. And the absence of 'kexec_execute' event after kexec soft reboot implies missing events in that window which results in inconsistency with TPM PCR quotes, necessitating a cold boot for a successful remote attestation. Reviewed-by: Stefan Berger Author: Tushar Sugandhi Signed-off-by: Tushar Sugandhi Signed-off-by: steven chen --- security/integrity/ima/ima_kexec.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c index c9c916f69ca7..d416ca0382cb 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -17,6 +17,8 @@ #include "ima.h" #ifdef CONFIG_IMA_KEXEC +#define IMA_KEXEC_EVENT_LEN 256 + static struct seq_file ima_kexec_file; static void *ima_kexec_buffer; static size_t kexec_segment_size; @@ -36,6 +38,24 @@ static void ima_free_kexec_file_buf(struct seq_file *sf) ima_reset_kexec_file(sf); } +static void ima_measure_kexec_event(const char *event_name) +{ + char ima_kexec_event[IMA_KEXEC_EVENT_LEN]; + size_t buf_size = 0; + long len; + + buf_size = ima_get_binary_runtime_size(); + len = atomic_long_read(&ima_htable.len); + + scnprintf(ima_kexec_event, IMA_KEXEC_EVENT_LEN, + "kexec_segment_size=%lu;ima_binary_runtime_size=%lu;" + "ima_runtime_measurements_count=%ld;", + kexec_segment_size, buf_size, len); + + ima_measure_critical_data("ima_kexec", event_name, ima_kexec_event, + strlen(ima_kexec_event), false, NULL, 0); +} + static int ima_alloc_kexec_file_buf(size_t segment_size) { /* @@ -60,6 +80,7 @@ static int ima_alloc_kexec_file_buf(size_t segment_size) out: ima_kexec_file.read_pos = 0; ima_kexec_file.count = sizeof(struct ima_kexec_hdr); /* reserved space */ + ima_measure_kexec_event("kexec_load"); return 0; } @@ -206,6 +227,8 @@ static int ima_update_kexec_buffer(struct notifier_block *self, if (ret) pr_err("Dump measurements failed. Error:%d\n", ret); + else + ima_measure_kexec_event("kexec_execute"); if (buf_size != 0) memcpy(ima_kexec_buffer, buf, buf_size);