From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 3sKsrG4WdbzDrcf for ; Fri, 26 Aug 2016 04:18:54 +1000 (AEST) Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.11/8.16.0.11) with SMTP id u7PIHPnN087966 for ; Thu, 25 Aug 2016 14:18:52 -0400 Received: from e24smtp02.br.ibm.com (e24smtp02.br.ibm.com [32.104.18.86]) by mx0b-001b2d01.pphosted.com with ESMTP id 2525tvg0k5-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 25 Aug 2016 14:18:51 -0400 Received: from localhost by e24smtp02.br.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 25 Aug 2016 15:18:49 -0300 Received: from d24relay03.br.ibm.com (d24relay03.br.ibm.com [9.13.184.25]) by d24dlp02.br.ibm.com (Postfix) with ESMTP id 6E3801DC006F for ; Thu, 25 Aug 2016 14:18:38 -0400 (EDT) Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.8.31.91]) by d24relay03.br.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u7PIIkvb55312558 for ; Thu, 25 Aug 2016 15:18:46 -0300 Received: from d24av01.br.ibm.com (localhost [127.0.0.1]) by d24av01.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u7PIIj7o017852 for ; Thu, 25 Aug 2016 15:18:46 -0300 From: Thiago Jung Bauermann To: kexec@lists.infradead.org Cc: linux-security-module@vger.kernel.org, linux-ima-devel@lists.sourceforge.net, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Eric Biederman , Dave Young , Vivek Goyal , Baoquan He , Michael Ellerman , Stewart Smith , Mimi Zohar , Eric Richter , Andrew Morton , Balbir Singh , Thiago Jung Bauermann Subject: [PATCH v3 4/5] kexec_file: Add mechanism to update kexec segments. Date: Thu, 25 Aug 2016 15:18:30 -0300 In-Reply-To: <1472149111-30598-1-git-send-email-bauerman@linux.vnet.ibm.com> References: <1472149111-30598-1-git-send-email-bauerman@linux.vnet.ibm.com> Message-Id: <1472149111-30598-5-git-send-email-bauerman@linux.vnet.ibm.com> List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , kexec_update_segment allows a given segment in kexec_image to have its contents updated. This is useful if the current kernel wants to send information to the next kernel that is up-to-date at the time of reboot. Signed-off-by: Thiago Jung Bauermann --- include/linux/kexec.h | 2 ++ kernel/kexec_core.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index edadff6c86ff..ff3aa93649e2 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -256,6 +256,8 @@ extern int kexec_purgatory_get_set_symbol(struct kimage *image, unsigned int size, bool get_value); extern void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name); +int kexec_update_segment(const char *buffer, size_t bufsz, + unsigned long load_addr, size_t memsz); extern void __crash_kexec(struct pt_regs *); extern void crash_kexec(struct pt_regs *); int kexec_should_crash(struct task_struct *); diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 561675589511..11ca5f8678df 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -721,6 +721,105 @@ static struct page *kimage_alloc_page(struct kimage *image, return page; } +/** + * kexec_update_segment - update the contents of a kimage segment + * @buffer: New contents of the segment. + * @bufsz: @buffer size. + * @load_addr: Segment's physical address in the next kernel. + * @memsz: Segment size. + * + * This function assumes kexec_mutex is held. + * + * Return: 0 on success, negative errno on error. + */ +int kexec_update_segment(const char *buffer, size_t bufsz, + unsigned long load_addr, size_t memsz) +{ + int i; + unsigned long entry; + unsigned long *ptr = NULL; + void *dest = NULL; + + if (kexec_image == NULL) { + pr_err("Can't update segment: no kexec image loaded.\n"); + return -EINVAL; + } + + /* + * kexec_add_buffer rounds up segment sizes to PAGE_SIZE, so + * we have to do it here as well. + */ + memsz = ALIGN(memsz, PAGE_SIZE); + + for (i = 0; i < kexec_image->nr_segments; i++) + /* We only support updating whole segments. */ + if (load_addr == kexec_image->segment[i].mem && + memsz == kexec_image->segment[i].memsz) { + if (!kexec_image->segment[i].skip_checksum) { + pr_err("Trying to update non-modifiable segment.\n"); + return -EINVAL; + } + + break; + } + if (i == kexec_image->nr_segments) { + pr_err("Couldn't find segment to update: 0x%lx, size 0x%zx\n", + load_addr, memsz); + return -EINVAL; + } + + for (entry = kexec_image->head; !(entry & IND_DONE) && memsz; + entry = *ptr++) { + void *addr = (void *) (entry & PAGE_MASK); + + switch (entry & IND_FLAGS) { + case IND_DESTINATION: + dest = addr; + break; + case IND_INDIRECTION: + ptr = __va(entry & PAGE_MASK); + break; + case IND_SOURCE: + /* Shouldn't happen, but verify just to be safe. */ + if (dest == NULL) { + pr_err("Invalid kexec entries list."); + return -EINVAL; + } + + if (dest == (void *) load_addr) { + struct page *page; + char *ptr; + size_t uchunk, mchunk; + + page = kmap_to_page(addr); + + ptr = kmap_atomic(page); + ptr += load_addr & ~PAGE_MASK; + mchunk = min_t(size_t, memsz, + PAGE_SIZE - (load_addr & ~PAGE_MASK)); + uchunk = min(bufsz, mchunk); + memcpy(ptr, buffer, uchunk); + + kunmap_atomic(ptr); + + bufsz -= uchunk; + load_addr += mchunk; + buffer += mchunk; + memsz -= mchunk; + } + dest += PAGE_SIZE; + } + + /* Shouldn't happen, but verify just to be safe. */ + if (ptr == NULL) { + pr_err("Invalid kexec entries list."); + return -EINVAL; + } + } + + return 0; +} + static int kimage_load_normal_segment(struct kimage *image, struct kexec_segment *segment) { -- 1.9.1