Received: by 10.223.185.116 with SMTP id b49csp2109385wrg; Mon, 12 Feb 2018 04:35:09 -0800 (PST) X-Google-Smtp-Source: AH8x22669LLyCuO6AE7M2rMr8I0Wz/TRjPwJo3Incf8OswLw8xY5QPhNLdDoVZ7TTshGlsAQMMtj X-Received: by 10.98.196.204 with SMTP id h73mr11744696pfk.143.1518438909117; Mon, 12 Feb 2018 04:35:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518438909; cv=none; d=google.com; s=arc-20160816; b=PK3EJOnQTEPn0e9WPyWCzd17ItxzBMpdWj/VG3y/ssyuP5HM3rDwfitjhPp2CqArQk 1N51oel3ver/mgjmXy7POt1x7+KRT+cd7tPdnTGTFRVjF5QJO0LO03rTzT8vPzGD+QFf nOIC/ENLOslFS1Z5203bydlYKngijBx+YANHv97I4KVYmCt16Ze9tcbx+cmlRHBp5XKe XnqP2ZYHahqD9dk7tz12ayrIYpsJ7R21n8jcflCGlJVHhCiLgSdAdSYds+ZQxZabARSq PwXa6Too0FzEn6qK8TICltC2CD80qgjKx3j+/PoQ4YM3hijctrRveWlVPxyLRHipW6XC yi1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:references:in-reply-to:date :subject:cc:to:from:arc-authentication-results; bh=4nv2Xj7GM+KyIq2Xduvw8w2OSR1pDP3LoDHbtamqO0c=; b=bGvfp1r4drtPzhIC8gM5GPb8V12maTxYuQpihqOXOTeIjzdm1/592tsqIzAGKx1NES XeoDoEScIzc52MLV9LagepBkd5RLI2rDzWoCKsEvhMJZWmqHbDEFOdkt7W9DOTXK1r4s PDgzJYDmuoQ2GZlxUC3g3RFtMLmPXUQMceBXP0dnoR8hgVDt17//LtaHVUpTzCnS8Jsj SjEKlLTewmQYFE6bHX5b4vg4jX8c9aTVYdZLJyWy4vDGlZW4+28Atlej4jZHBUCVtUY+ QuFjFjDb/mMDZA4PVkUbFbPQEvYDNATzXq2Un9877vsz4/C8oNSb0VKUQM5F6p6QMGNm YY2A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w186si3585925pgd.745.2018.02.12.04.34.54; Mon, 12 Feb 2018 04:35:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933579AbeBLKIv (ORCPT + 99 others); Mon, 12 Feb 2018 05:08:51 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:37090 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933578AbeBLKI1 (ORCPT ); Mon, 12 Feb 2018 05:08:27 -0500 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w1CA7FLt063593 for ; Mon, 12 Feb 2018 05:08:26 -0500 Received: from e06smtp15.uk.ibm.com (e06smtp15.uk.ibm.com [195.75.94.111]) by mx0a-001b2d01.pphosted.com with ESMTP id 2g35j3fjb2-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 12 Feb 2018 05:08:26 -0500 Received: from localhost by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 12 Feb 2018 10:08:23 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 12 Feb 2018 10:08:19 -0000 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w1CA8IL750790438; Mon, 12 Feb 2018 10:08:18 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 80836AE04D; Mon, 12 Feb 2018 09:59:24 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2C6ACAE056; Mon, 12 Feb 2018 09:59:24 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Mon, 12 Feb 2018 09:59:24 +0000 (GMT) From: Philipp Rudo To: kexec@lists.infradead.org, linux-s390@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Eric Biederman , Vivek Goyal , Michael Ellerman , Thiago Jung Bauermann , Martin Schwidefsky , Heiko Carstens , Andrew Morton , x86@kernel.org Subject: [PATCH 17/17] s390/kexec_file: Add ELF loader Date: Mon, 12 Feb 2018 11:07:54 +0100 X-Mailer: git-send-email 2.13.5 In-Reply-To: <20180212100754.55121-1-prudo@linux.vnet.ibm.com> References: <20180212100754.55121-1-prudo@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18021210-0020-0000-0000-000003F4BBFD X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18021210-0021-0000-0000-000042874295 Message-Id: <20180212100754.55121-18-prudo@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-02-12_05:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1802120131 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add an ELF loader for kexec_file. The main task here is to do proper sanity checks on the ELF file. Basically all other functionality was already implemented for the image loader. Signed-off-by: Philipp Rudo --- arch/s390/include/asm/kexec.h | 1 + arch/s390/kernel/Makefile | 2 +- arch/s390/kernel/kexec_elf.c | 149 ++++++++++++++++++++++++++++++++++ arch/s390/kernel/machine_kexec_file.c | 1 + 4 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 arch/s390/kernel/kexec_elf.c diff --git a/arch/s390/include/asm/kexec.h b/arch/s390/include/asm/kexec.h index 532f5e4f198f..e450b82e2716 100644 --- a/arch/s390/include/asm/kexec.h +++ b/arch/s390/include/asm/kexec.h @@ -67,5 +67,6 @@ void kexec_file_update_kernel(struct kimage *iamge, struct s390_load_data *data); extern const struct kexec_file_ops s390_kexec_image_ops; +extern const struct kexec_file_ops s390_kexec_elf_ops; #endif /*_S390_KEXEC_H */ diff --git a/arch/s390/kernel/Makefile b/arch/s390/kernel/Makefile index 58a590709cc3..66d7b432c2a8 100644 --- a/arch/s390/kernel/Makefile +++ b/arch/s390/kernel/Makefile @@ -60,7 +60,7 @@ obj-y += debug.o irq.o ipl.o dis.o diag.o vdso.o als.o obj-y += sysinfo.o jump_label.o lgr.o os_info.o machine_kexec.o pgm_check.o obj-y += runtime_instr.o cache.o fpu.o dumpstack.o guarded_storage.o sthyi.o obj-y += entry.o reipl.o relocate_kernel.o kdebugfs.o alternative.o -obj-y += machine_kexec_file.o kexec_image.o +obj-y += machine_kexec_file.o kexec_image.o kexec_elf.o extra-y += head.o head64.o vmlinux.lds diff --git a/arch/s390/kernel/kexec_elf.c b/arch/s390/kernel/kexec_elf.c new file mode 100644 index 000000000000..919ab2f1256f --- /dev/null +++ b/arch/s390/kernel/kexec_elf.c @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * ELF loader for kexec_file_load system call. + * + * Copyright IBM Corp. 2018 + * + * Author(s): Philipp Rudo + */ + +#include +#include +#include +#include + +static int kexec_file_add_elf_kernel(struct kimage *image, + struct s390_load_data *data, + char *kernel, unsigned long kernel_len) +{ + struct kexec_buf buf; + const Elf_Ehdr *ehdr; + const Elf_Phdr *phdr; + int i, ret; + + ehdr = (Elf_Ehdr *)kernel; + buf.image = image; + + phdr = (void *)ehdr + ehdr->e_phoff; + for (i = 0; i < ehdr->e_phnum; i++, phdr++) { + if (phdr->p_type != PT_LOAD) + continue; + + buf.buffer = kernel + phdr->p_offset; + buf.bufsz = phdr->p_filesz; + + buf.mem = ALIGN(phdr->p_paddr, phdr->p_align); + buf.memsz = phdr->p_memsz; + + if (phdr->p_paddr == 0) { + data->kernel_buf = buf.buffer; + data->memsz += STARTUP_NORMAL_OFFSET; + + buf.buffer += STARTUP_NORMAL_OFFSET; + buf.bufsz -= STARTUP_NORMAL_OFFSET; + + buf.mem += STARTUP_NORMAL_OFFSET; + buf.memsz -= STARTUP_NORMAL_OFFSET; + } + + if (image->type == KEXEC_TYPE_CRASH) + buf.mem += crashk_res.start; + + ret = kexec_add_buffer(&buf); + if (ret) + return ret; + + data->memsz += buf.memsz; + } + + return 0; +} + +static void *s390_elf_load(struct kimage *image, + char *kernel, unsigned long kernel_len, + char *initrd, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len) +{ + struct s390_load_data data = {0}; + const Elf_Ehdr *ehdr; + const Elf_Phdr *phdr; + size_t size; + int i, ret; + + /* image->fobs->probe already checked for valid ELF magic number. */ + ehdr = (Elf_Ehdr *)kernel; + + if (ehdr->e_type != ET_EXEC || + ehdr->e_ident[EI_CLASS] != ELFCLASS64 || + !elf_check_arch(ehdr)) + return ERR_PTR(-EINVAL); + + if (!ehdr->e_phnum || ehdr->e_phentsize != sizeof(Elf_Phdr)) + return ERR_PTR(-EINVAL); + + size = ehdr->e_ehsize + ehdr->e_phoff; + size += ehdr->e_phentsize * ehdr->e_phnum; + if (size > kernel_len) + return ERR_PTR(-EINVAL); + + phdr = (void *)ehdr + ehdr->e_phoff; + size = ALIGN(size, phdr->p_align); + for (i = 0; i < ehdr->e_phnum; i++, phdr++) { + if (phdr->p_type == PT_INTERP) + return ERR_PTR(-EINVAL); + + if (phdr->p_offset > kernel_len) + return ERR_PTR(-EINVAL); + + size += ALIGN(phdr->p_filesz, phdr->p_align); + } + + if (size > kernel_len) + return ERR_PTR(-EINVAL); + + ret = kexec_file_add_elf_kernel(image, &data, kernel, kernel_len); + if (ret) + return ERR_PTR(ret); + + if (!data.memsz) + return ERR_PTR(-EINVAL); + + if (initrd) { + ret = kexec_file_add_initrd(image, &data, initrd, initrd_len); + if (ret) + return ERR_PTR(ret); + } + + ret = kexec_file_add_purgatory(image, &data); + if (ret) + return ERR_PTR(ret); + + kexec_file_update_kernel(image, &data); + + return NULL; +} + +static int s390_elf_probe(const char *buf, unsigned long len) +{ + const Elf_Ehdr *ehdr; + + if (len < sizeof(Elf_Ehdr)) + return -ENOEXEC; + + ehdr = (Elf_Ehdr *)buf; + + /* Only check the ELF magic number here and do proper validity check + * in the loader. Any check here that fails would send the erroneous + * ELF file to the image loader that does not care what it gets. + * (Most likely) causing behavior not intended by the user. + */ + if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) + return -ENOEXEC; + + return 0; +} + +const struct kexec_file_ops s390_kexec_elf_ops = { + .probe = s390_elf_probe, + .load = s390_elf_load, +}; diff --git a/arch/s390/kernel/machine_kexec_file.c b/arch/s390/kernel/machine_kexec_file.c index 050ceab746ec..8938e0eca4b0 100644 --- a/arch/s390/kernel/machine_kexec_file.c +++ b/arch/s390/kernel/machine_kexec_file.c @@ -12,6 +12,7 @@ #include const struct kexec_file_ops * const kexec_file_loaders[] = { + &s390_kexec_elf_ops, &s390_kexec_image_ops, NULL, }; -- 2.13.5