From patchwork Tue Jul 11 13:07:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Ungerer X-Patchwork-Id: 13308713 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9885BEB64DC for ; Tue, 11 Jul 2023 13:09:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EC986B0071; Tue, 11 Jul 2023 09:09:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 176576B0074; Tue, 11 Jul 2023 09:09:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 03E416B0075; Tue, 11 Jul 2023 09:09:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E94136B0071 for ; Tue, 11 Jul 2023 09:09:13 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 763F880249 for ; Tue, 11 Jul 2023 13:09:13 +0000 (UTC) X-FDA: 80999361786.23.1AC2E67 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id 047F04010C for ; Tue, 11 Jul 2023 13:08:45 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=C04HhYVc; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of gerg@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gerg@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689080926; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mU1LZrk5d/prmU26/qtXamyfQyAoWkUSQ2WjEfQA8+4=; b=uddZpDBGSozQlf48TGjovrXnNj5gdAISNXj7nXu29gLlO7FockJT5vKnvYYMRT/gxAkMPc 3Z5U6R6G9Fb4cbbbVd9R8adzqiBzNRWN3HjzalMm5ZZrnQoVbmGz+fGa5UkHLd6OVUdZ0E tGP3APOY7R3NrfKo44za2bWGpPDtlLg= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=C04HhYVc; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of gerg@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gerg@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689080926; a=rsa-sha256; cv=none; b=HsYiRS5purOM5KWKDz2hGAjlgeJ0BKgH410mo8hIr39Fn8iY2XlNNoka41LbeBt80LQGi9 wjEL6FbS/m0cKSo/87xN/SiKc3QowwEoB8OAU7kabB8u70rSiH59rS+uaVw8Hb9oNAnYwu obl9te6hBIrq4DRLYsZ9wvY3VF6wzpU= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E4B72614BF; Tue, 11 Jul 2023 13:08:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F8BAC433C8; Tue, 11 Jul 2023 13:08:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689080924; bh=QNUVKJXnoZSUVP2Eq6Ceyt+qtwl6KbgMvYZ6bIQgM18=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C04HhYVc8+putRViF/blzDQorcqlJp+TzjaE8DP3/7WfNIsss1zpO4ZzPcaxzK/ES +pMNCbzp8A1+JLlJJEGzXyYo5tnBmtQsDEAjSYWwU14ELLL15eS8NQw+/SgzFz0aKP 5R2M01ThIBXh2jnUQaBGHmFS8tnsS9kfM94fK0iAy2F6H/IPTr6pGJxgOoxSzpQosd MJnDcqvxKNo1Dj5ZzkUJkubVYqkN6Bil381ee+L9D4HqQnYC/mV8G+UNIGWQZmJWrx iGbrc89PzwQY2Dq0uI5lSg77b1+B6aHnM9z1tANEQF/MjHrhbC85d3Q7F1k1G0NZV3 c7MaXU1umlJyA== From: Greg Ungerer To: linux-riscv@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: palmer@dabbelt.com, viro@zeniv.linux.org.uk, ebiederm@xmission.com, keescook@chromium.org, brauner@kernel.org, Greg Ungerer Subject: [PATCH v2 1/2] binfmt_elf_fdpic: support 64-bit systems Date: Tue, 11 Jul 2023 23:07:53 +1000 Message-Id: <20230711130754.481209-2-gerg@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230711130754.481209-1-gerg@kernel.org> References: <20230711130754.481209-1-gerg@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 047F04010C X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 9h6a6o9oc5suwqouqxeupant6dwa4rbi X-HE-Tag: 1689080925-518826 X-HE-Meta: U2FsdGVkX1+++KnncGxYtI2uZjeqHToD44DXlU0/XJXpVSBykilzzF7F6nO+ZL28/kbF3L50Qb8CnuTLEhp4cCLzXHGVT00k6rzWUtPuSnxqavKXodHe8Blhzf9MuVSbrrsM9fOhUlUc1dmfpJiwR0ltlLbQtBPQ+velD5E7TWFD0yCrkL8Mo8yLHqPBC0ds53vDiCDnSBM+txBnNsrtS1HuwTGpGyMFJYrO8Wdxx2vRfokDS6shMRgQPt3dd8r6BmEESLipS6vY0Y8OGecwx/LM/Irpig89yvSegP7tUDSaTlHTEYW3LLvU8vS3MlZ9SmHj9EP93woGfdQys+YfB9oB9/iiTVXrLkEF9tJBq33BhwKgYVuN+PhrC5Xm1/ujqDhQdwb1LnIKT1yvc7LqM1xDvK7dO/ohCwf0eHAi/V464pbjm228tmdoLglu0Lvji5lysWJ+N6rwsZmBIwK1uw2LC+/5YiHZk9wqBIdooo/i9aK+rJ2aNtJvmldPfYss3edLbU+Oex3WJR/aUf3CcVAM1oHZjZZnV63qATQVgIUP9iMTARGscAPKAooth3GEvoR7mr3Ag6gBUCUay+mSn3/Ju1rpy8ocE3CW25gv9utd/5eQpp5cK6pR6VeWdU3O4rT393mmKnfUEKRqprmJms4vFm75uc+xPMnhvWwmlzIiKM3dAWYmLBH85aDPkqKnShhjxFfH7x3Sqf3V0FWCeIJS6xEJLzDo+rQY2EtZmrrGS2kk6T1OJ2jH2ZjJzjPGWEty4CenwPHmMr08ZUXYB1Kzr28X48CS9RYlHrt7jU69jexhAWoN7b3SvPRUdPJc7m0IGr/ygdXO0iUFQS/UrhjLfQ05ijqjXc2RPRU/fXAxasuwEtRWauKxXZvvt/r+bPvbPF9NH/zBVD7pmPXYqEejMDjuMszy9EUHfKjP54phyT6QBCsuzV+YZFHBGgqrOTe5XF6YyiCAkJzeEt8 T1nVkuzo uFaU3RVHsYPmOrjvt7wkdAY1u2cmzr4TSgoy/8w/pWJ3G9Qc7qh+SzEGSE60DsayM7dxSgUeML7thFpYiNUv8Pst8BIWxzAvWUxhkHaRTvr0cVQBmm+4pLZHnrzrbCjkHk0IEmz+OobW4W7OIbMvzIJZsq61nwdboukV2Wtsgsq6wFddL3/BnRFltK2ilk5OXbtjn21BUznP/vkDlonF2pGXyk8cQxKcToSLfRoTaVDHMfQKZ7Rp3XR9anZACY5I81R3JzB6KFdJBkrAsUpKcqAGknZX1Y3YrkVQI+cDk9PGcL5GiqtdwAaPgvhgTmqnGwRzrx+0M5t4N7GcbpFKAplzv/ia6/c8X8JJ9C/gqk4CYD9kSwU2eG6q2iHgAPZRqGSOM X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The binfmt_flat_fdpic code has a number of 32-bit specific data structures associated with it. Extend it to be able to support and be used on 64-bit systems as well. The new code defines a number of key 64-bit variants of the core elf-fdpic data structures - along side the existing 32-bit sized ones. A common set of generic named structures are defined to be either the 32-bit or 64-bit ones as required at compile time. This is a similar technique to that used in the ELF binfmt loader. For example: elf_fdpic_loadseg is either elf32_fdpic_loadseg or elf64_fdpic_loadseg elf_fdpic_loadmap is either elf32_fdpic_loadmap or elf64_fdpic_loadmap the choice based on ELFCLASS32 or ELFCLASS64. Signed-off-by: Greg Ungerer Acked-by: Kees Cook --- v1->v2: rebase onto linux-6.5-rc1 fs/binfmt_elf_fdpic.c | 38 +++++++++++++++++----------------- include/linux/elf-fdpic.h | 14 ++++++++++++- include/uapi/linux/elf-fdpic.h | 15 ++++++++++++++ 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c index 1c6c5832af86..43b2a2851ba3 100644 --- a/fs/binfmt_elf_fdpic.c +++ b/fs/binfmt_elf_fdpic.c @@ -138,7 +138,7 @@ static int is_constdisp(struct elfhdr *hdr) static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, struct file *file) { - struct elf32_phdr *phdr; + struct elf_phdr *phdr; unsigned long size; int retval, loop; loff_t pos = params->hdr.e_phoff; @@ -560,8 +560,8 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm, sp &= ~7UL; /* stack the load map(s) */ - len = sizeof(struct elf32_fdpic_loadmap); - len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs; + len = sizeof(struct elf_fdpic_loadmap); + len += sizeof(struct elf_fdpic_loadseg) * exec_params->loadmap->nsegs; sp = (sp - len) & ~7UL; exec_params->map_addr = sp; @@ -571,8 +571,8 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm, current->mm->context.exec_fdpic_loadmap = (unsigned long) sp; if (interp_params->loadmap) { - len = sizeof(struct elf32_fdpic_loadmap); - len += sizeof(struct elf32_fdpic_loadseg) * + len = sizeof(struct elf_fdpic_loadmap); + len += sizeof(struct elf_fdpic_loadseg) * interp_params->loadmap->nsegs; sp = (sp - len) & ~7UL; interp_params->map_addr = sp; @@ -740,13 +740,13 @@ static int elf_fdpic_map_file(struct elf_fdpic_params *params, struct mm_struct *mm, const char *what) { - struct elf32_fdpic_loadmap *loadmap; + struct elf_fdpic_loadmap *loadmap; #ifdef CONFIG_MMU - struct elf32_fdpic_loadseg *mseg; + struct elf_fdpic_loadseg *mseg; unsigned long load_addr; #endif - struct elf32_fdpic_loadseg *seg; - struct elf32_phdr *phdr; + struct elf_fdpic_loadseg *seg; + struct elf_phdr *phdr; unsigned nloads, tmp; unsigned long stop; int loop, ret; @@ -766,7 +766,7 @@ static int elf_fdpic_map_file(struct elf_fdpic_params *params, params->loadmap = loadmap; - loadmap->version = ELF32_FDPIC_LOADMAP_VERSION; + loadmap->version = ELF_FDPIC_LOADMAP_VERSION; loadmap->nsegs = nloads; /* map the requested LOADs into the memory space */ @@ -839,8 +839,8 @@ static int elf_fdpic_map_file(struct elf_fdpic_params *params, if (phdr->p_vaddr >= seg->p_vaddr && phdr->p_vaddr + phdr->p_memsz <= seg->p_vaddr + seg->p_memsz) { - Elf32_Dyn __user *dyn; - Elf32_Sword d_tag; + Elf_Dyn __user *dyn; + Elf_Sword d_tag; params->dynamic_addr = (phdr->p_vaddr - seg->p_vaddr) + @@ -850,11 +850,11 @@ static int elf_fdpic_map_file(struct elf_fdpic_params *params, * one item, and that the last item is a NULL * entry */ if (phdr->p_memsz == 0 || - phdr->p_memsz % sizeof(Elf32_Dyn) != 0) + phdr->p_memsz % sizeof(Elf_Dyn) != 0) goto dynamic_error; - tmp = phdr->p_memsz / sizeof(Elf32_Dyn); - dyn = (Elf32_Dyn __user *)params->dynamic_addr; + tmp = phdr->p_memsz / sizeof(Elf_Dyn); + dyn = (Elf_Dyn __user *)params->dynamic_addr; if (get_user(d_tag, &dyn[tmp - 1].d_tag) || d_tag != 0) goto dynamic_error; @@ -923,8 +923,8 @@ static int elf_fdpic_map_file_constdisp_on_uclinux( struct file *file, struct mm_struct *mm) { - struct elf32_fdpic_loadseg *seg; - struct elf32_phdr *phdr; + struct elf_fdpic_loadseg *seg; + struct elf_phdr *phdr; unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0; int loop, ret; @@ -1007,8 +1007,8 @@ static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params, struct file *file, struct mm_struct *mm) { - struct elf32_fdpic_loadseg *seg; - struct elf32_phdr *phdr; + struct elf_fdpic_loadseg *seg; + struct elf_phdr *phdr; unsigned long load_addr, delta_vaddr; int loop, dvset; diff --git a/include/linux/elf-fdpic.h b/include/linux/elf-fdpic.h index 3bea95a1af53..e533f4513194 100644 --- a/include/linux/elf-fdpic.h +++ b/include/linux/elf-fdpic.h @@ -10,13 +10,25 @@ #include +#if ELF_CLASS == ELFCLASS32 +#define Elf_Sword Elf32_Sword +#define elf_fdpic_loadseg elf32_fdpic_loadseg +#define elf_fdpic_loadmap elf32_fdpic_loadmap +#define ELF_FDPIC_LOADMAP_VERSION ELF32_FDPIC_LOADMAP_VERSION +#else +#define Elf_Sword Elf64_Sxword +#define elf_fdpic_loadmap elf64_fdpic_loadmap +#define elf_fdpic_loadseg elf64_fdpic_loadseg +#define ELF_FDPIC_LOADMAP_VERSION ELF64_FDPIC_LOADMAP_VERSION +#endif + /* * binfmt binary parameters structure */ struct elf_fdpic_params { struct elfhdr hdr; /* ref copy of ELF header */ struct elf_phdr *phdrs; /* ref copy of PT_PHDR table */ - struct elf32_fdpic_loadmap *loadmap; /* loadmap to be passed to userspace */ + struct elf_fdpic_loadmap *loadmap; /* loadmap to be passed to userspace */ unsigned long elfhdr_addr; /* mapped ELF header user address */ unsigned long ph_addr; /* mapped PT_PHDR user address */ unsigned long map_addr; /* mapped loadmap user address */ diff --git a/include/uapi/linux/elf-fdpic.h b/include/uapi/linux/elf-fdpic.h index 4fcc6cfebe18..ec23f0871129 100644 --- a/include/uapi/linux/elf-fdpic.h +++ b/include/uapi/linux/elf-fdpic.h @@ -32,4 +32,19 @@ struct elf32_fdpic_loadmap { #define ELF32_FDPIC_LOADMAP_VERSION 0x0000 +/* segment mappings for ELF FDPIC libraries/executables/interpreters */ +struct elf64_fdpic_loadseg { + Elf64_Addr addr; /* core address to which mapped */ + Elf64_Addr p_vaddr; /* VMA recorded in file */ + Elf64_Word p_memsz; /* allocation size recorded in file */ +}; + +struct elf64_fdpic_loadmap { + Elf64_Half version; /* version of these structures, just in case... */ + Elf64_Half nsegs; /* number of segments */ + struct elf64_fdpic_loadseg segs[]; +}; + +#define ELF64_FDPIC_LOADMAP_VERSION 0x0000 + #endif /* _UAPI_LINUX_ELF_FDPIC_H */ From patchwork Tue Jul 11 13:07:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Ungerer X-Patchwork-Id: 13308714 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4FDF6C001DC for ; Tue, 11 Jul 2023 13:09:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E217F6B0074; Tue, 11 Jul 2023 09:09:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DAADF6B0075; Tue, 11 Jul 2023 09:09:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4B616B0078; Tue, 11 Jul 2023 09:09:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B34B56B0074 for ; Tue, 11 Jul 2023 09:09:29 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7AB8FC01DF for ; Tue, 11 Jul 2023 13:09:29 +0000 (UTC) X-FDA: 80999362458.13.1367F88 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf14.hostedemail.com (Postfix) with ESMTP id 9CDAB1000AD for ; Tue, 11 Jul 2023 13:08:52 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="m8vIO/3o"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf14.hostedemail.com: domain of gerg@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gerg@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689080932; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JykG/UzUnVwFeMJrnBIWl4WMZH6IAo4uTuNPnZHvTzA=; b=a8nSPiaRt6hSqa0u39339BcG0StTeBwyqB4w7h8MHWE4bmpimmKBMBd3n4BRyROqqP1m9a aGVwWMeASz+UO0uEc4R6dvDCeO5GsFpt8FLIKQU8HnwFwgv8qnYtur0F0bI/gD+E6eL9sQ 9cwNm1egyhwaMhgRxCYSZMlM1k72BWQ= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="m8vIO/3o"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf14.hostedemail.com: domain of gerg@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gerg@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689080932; a=rsa-sha256; cv=none; b=5Xoi7exNuEtyjRpNthL0xYLexn6FW1pTemT3Gmy1ntvhHU6uMztrUD2sbovW0eVKaUGLAl jijaSe5lZBRjJ3hmkEPBbBPfeRT0IFAwXigK6trBNGPCCnx8X8XcOc5qI+WIYnXslzFwcq /vgaZ/HS4v9nBVek79mWGYtgjxhNqnw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B058B614C8; Tue, 11 Jul 2023 13:08:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EEFD1C433C8; Tue, 11 Jul 2023 13:08:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689080931; bh=YRG1SatEXSQvibLkPPPJGs1s5S3gHEmvd7gZSzwoAnU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m8vIO/3oz0iV+JgNzmjwzr+CqcICYWIe7Rog2iDkpGmFwZJYExFefwtunqZdT6VMl EQnVR6hgqYxvFUCn1yz1VxthdhaUjLWAQNQknByoD2ylRfeXIGTkinPU7DRPpEARDZ 7wX0sSRV4gsPE+0pjG7tc7lPTroIyaiNr9krJQaOVEEiaUTnd9kPtjM1i7eDyrWveS QtdstMrnsA2dPoXWxPs7dLSeHSQaNy11g5joZAo4bkYF0MURxHjYoP8QJ1BL2CHNxl dRj4mzhN1/YDtHZnmVaI3l9GfljurrccBjsCIrXpFRYcC9/hqYTui5DrmkZ/0JYoKJ JsEwgA/i1IYjg== From: Greg Ungerer To: linux-riscv@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: palmer@dabbelt.com, viro@zeniv.linux.org.uk, ebiederm@xmission.com, keescook@chromium.org, brauner@kernel.org, Greg Ungerer Subject: [PATCH v2 2/2] riscv: support the elf-fdpic binfmt loader Date: Tue, 11 Jul 2023 23:07:54 +1000 Message-Id: <20230711130754.481209-3-gerg@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230711130754.481209-1-gerg@kernel.org> References: <20230711130754.481209-1-gerg@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: n8nae61e5zngpnyxneuouu36d3afp4xk X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9CDAB1000AD X-HE-Tag: 1689080932-634203 X-HE-Meta: U2FsdGVkX1+E+eCpu9+C4Ymv4IUISP+m0uQbD0oGclu1dG72SsSLY9qSQPqYAIjtrzfE49JBKFQCSOxPKU+wWez1IVf5/PCVmb3Qm8ByDAo9pXpXbhDr+77mfhipb8qXyT/FV9wFU+6VEjITY3bZXNffCz/qNGhkD+ksgpVC0oFEph9LVAOmYGLYi/h7clNOpbvidc8EKaGvZ7fWSzO4lp/C3/NsUjm1qmMvVpo44oAFTPGW3HGSxpIcTdnkyvxR8mrKppeab5uuiW/YTbnF/KMUmRyuEydo7TrHDCftF3Cv2pcQHumV3l54Kpj3VAAK/7WCMsGTegx+f55VOJPALCUtNMyjm5Imm1va6xtHivnQyWqgC1EgU9HGATGMgPEfj6wPZClPT1VrLUNceKIE/LwnThwSf6mjzO7J1nLZFyJEFuh9od+pkdzazpc9Knq3foVE2s0BGdZdMM+5GqMnxdeRkNfO52l5m5pnx9iUKEHjms2aCqdN/73AanXnUnWWzU7hrSvIwLzPVMtqcVT+wc2E/6qSHSq3ZUkZp04FT2TH0Iabo9k83W77G+DPSmnyB/Q/RvoWz8O5mF6fU0XbV0j1yLLOtifUuSY5KZTrgWa0EazBQx3kssl3bbV9anAXaaKb8TaDLfxXYDVSHeQsZcFdV4Fp7kfOc+aYwf6geugZU+S7HIk1U2QPRGqmoirnS6NstNvW2c9EuONLIQpOhw7aqcvWO18381JmWJHncFDk3smPHUIM+rNKCrGnTC3wFRttVNuxqkLPEtpFSVL/tnt1EFWDlwYQQf3wg45rO4RykeW5ySjQ836plD0bSgdV0o4B267qopJQ7xeLZVLxg0yFZpfzypIoqWL8Ar2kxXFCmLRCMeUtTR5ZD6JwWEx/YccMOl0nJBtm56Sng88NZjFKDuaOxsLMM2sYQJ2gBFryKTy3ckNnpyiQgKWVWpJqr8LvheGE82ILgwSoOIV K7e1sXWj wZ6TLqLPbKlJhFKU+KM9Gf/vOv/dJYPATEaMapZXkU0tSFjVB1J9i7RHrFkH4tUvRlPeAp1jsmob70Lgs7qMP/UYtpkjNiZ7ED1gcSbYZMRt1Rd5usokFhodiXnS6obdNEMmeDhRu53Eqqc1mlzQw2uysu3/Tfg+QZVJB10O3830sC43NznTuwiVCS4JrAqvze7y+R/WKl1CIup4S/zUD8U3rsHtaS3iNgIjQ7TE52cRk0Rq1oevmsNVA8xJMRVPsrxoNMyoOb9+fzJCZd4yQvijTrHHgWwKNGvXzpJOM/b25U0xFCqpy3vxYhBwVZ9spVlG2Sojp6t+h83V0soETiYSn2w== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add support for enabling and using the binfmt_elf_fdpic program loader on RISC-V platforms. The most important change is to setup registers during program load to pass the mapping addresses to the new process. One of the interesting features of the elf-fdpic loader is that it also allows appropriately compiled ELF format binaries to be loaded on nommu systems. Appropriate being those compiled with -pie. Signed-off-by: Greg Ungerer Acked-by: Kees Cook --- v1->v2: rebase onto linux-6.5-rc1 increment PTRACE_GETFDPIC value to keep it unique arch/riscv/include/asm/elf.h | 11 ++++++++++- arch/riscv/include/asm/mmu.h | 4 ++++ arch/riscv/include/uapi/asm/ptrace.h | 5 +++++ fs/Kconfig.binfmt | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h index c24280774caf..c33fe923ef6d 100644 --- a/arch/riscv/include/asm/elf.h +++ b/arch/riscv/include/asm/elf.h @@ -41,6 +41,7 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr); #define compat_elf_check_arch compat_elf_check_arch #define CORE_DUMP_USE_REGSET +#define ELF_FDPIC_CORE_EFLAGS 0 #define ELF_EXEC_PAGESIZE (PAGE_SIZE) /* @@ -69,6 +70,13 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr); #define ELF_HWCAP riscv_get_elf_hwcap() extern unsigned long elf_hwcap; +#define ELF_FDPIC_PLAT_INIT(_r, _exec_map_addr, _interp_map_addr, dynamic_addr) \ + do { \ + (_r)->a1 = _exec_map_addr; \ + (_r)->a2 = _interp_map_addr; \ + (_r)->a3 = dynamic_addr; \ + } while (0) + /* * This yields a string that ld.so will use to load implementation * specific libraries for optimization. This is more specific in @@ -78,7 +86,6 @@ extern unsigned long elf_hwcap; #define COMPAT_ELF_PLATFORM (NULL) -#ifdef CONFIG_MMU #define ARCH_DLINFO \ do { \ /* \ @@ -115,6 +122,8 @@ do { \ else \ NEW_AUX_ENT(AT_IGNORE, 0); \ } while (0) + +#ifdef CONFIG_MMU #define ARCH_HAS_SETUP_ADDITIONAL_PAGES struct linux_binprm; extern int arch_setup_additional_pages(struct linux_binprm *bprm, diff --git a/arch/riscv/include/asm/mmu.h b/arch/riscv/include/asm/mmu.h index 0099dc116168..355504b37f8e 100644 --- a/arch/riscv/include/asm/mmu.h +++ b/arch/riscv/include/asm/mmu.h @@ -20,6 +20,10 @@ typedef struct { /* A local icache flush is needed before user execution can resume. */ cpumask_t icache_stale_mask; #endif +#ifdef CONFIG_BINFMT_ELF_FDPIC + unsigned long exec_fdpic_loadmap; + unsigned long interp_fdpic_loadmap; +#endif } mm_context_t; void __init create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa, diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index e17c550986a6..30f6d6537adc 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -10,6 +10,11 @@ #include +#define PTRACE_GETFDPIC 33 + +#define PTRACE_GETFDPIC_EXEC 0 +#define PTRACE_GETFDPIC_INTERP 1 + /* * User-mode register state for core dumps, ptrace, sigcontext * diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt index 93539aac0e5b..f5693164ca9a 100644 --- a/fs/Kconfig.binfmt +++ b/fs/Kconfig.binfmt @@ -58,7 +58,7 @@ config ARCH_USE_GNU_PROPERTY config BINFMT_ELF_FDPIC bool "Kernel support for FDPIC ELF binaries" default y if !BINFMT_ELF - depends on ARM || ((M68K || SUPERH || XTENSA) && !MMU) + depends on ARM || ((M68K || RISCV || SUPERH || XTENSA) && !MMU) select ELFCORE help ELF FDPIC binaries are based on ELF, but allow the individual load