From patchwork Mon Feb 13 04:53:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13137858 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 B02C9C636CC for ; Mon, 13 Feb 2023 04:54:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DAD986B0078; Sun, 12 Feb 2023 23:54:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D2D096B0075; Sun, 12 Feb 2023 23:54:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA4416B0078; Sun, 12 Feb 2023 23:54:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id AC4FE6B0074 for ; Sun, 12 Feb 2023 23:54:15 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 625E11415F7 for ; Mon, 13 Feb 2023 04:54:15 +0000 (UTC) X-FDA: 80461052070.15.7749EAA Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf05.hostedemail.com (Postfix) with ESMTP id 92112100007 for ; Mon, 13 Feb 2023 04:54:13 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=kQCkDlkh; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676264053; 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=oP4wZ0K6nVayFSsIwI2M/qbAeCayW2BZaB6BdNSazd0=; b=JKwSBs+n5iWPVZg8yafueid/OpvPmY4GyanaiL1bBXRW/KWAkovDRxuoBu3+yGAnhA2Y1W z6ocLaDGu+OiqNLwQuYwyoVG6nflhVqa6Th9eYqubznNf43Xia4gX6spFbIQICZYFpj6vY T2u+SxglXKd7H+VXayG9TCPgr3V5Uu0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=kQCkDlkh; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676264053; a=rsa-sha256; cv=none; b=Vr+fscQojEFVBqeQuCcYm2LAkvVdTJ5/colWZQP90kNy8356l0wuUh0qTGFN/2KT9bEQAd OACsl/M7AtADB97I3lBkZppgUdnS1Squ4LVPQW8JGXys0WH75yUvbFfY1rkrqEZulgVIzA IoDUgAJvLP1oZZXAofZY0FgjifXtxkI= Received: by mail-pj1-f51.google.com with SMTP id w14-20020a17090a5e0e00b00233d3b9650eso3255655pjf.4 for ; Sun, 12 Feb 2023 20:54:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oP4wZ0K6nVayFSsIwI2M/qbAeCayW2BZaB6BdNSazd0=; b=kQCkDlkhAa4XVmRG1hxXFbpWGlXPMBYAGXgy0L0uPsR9g5pVZDofnEGhHEJYmp4t70 GThKDRHlZLQeCmf3sShq+tYAdJikORjaEoHzhV8XPsEC3fuBeKmrypEuXS7bnM4Hr1Mu ejAuWJdj4rylP7L48un2eYEoyEM3T/HgHUJ+XVCnMOro/vM0ja9j4XcrCb2wVTAb2ykU VVkETKJNdrlyLnPFCjstqmRxjQbAorY6hbHg6u7JiS4XZd2HNWZ+rc/s5B7cfxPqGP5E y7sCTgg87DvCTzj8NBBDe3V2Zk1icq2em5kbBEcbVFNjlxLiW6d6zxFc1ZAJ17l5bZV5 /rrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oP4wZ0K6nVayFSsIwI2M/qbAeCayW2BZaB6BdNSazd0=; b=PJqOGYyrjQFqkCaKiVpOM2sgkOCQFtg70ZreMLfH0ewWhOE4EHqUY2xoXdwJ/N7M02 WJeh22OnUpBqPublX8jXvV3QzT2g0neuYNXwYu30FamWTB92C8iSfwIG7DQH9RpoSXPf t/v2JRlPKS7G+f6r+zFPl1i6OHluEiDPCafCcuP25UL0OEnpKotis/FpVnfzEfHJeyY8 TGLwsapYCGmnpLDfbT1MqpU5+AKjouQKmN6neMdRdlj9WN9vuDUvKclIxU2LFQr9Y9tO SosbsjglO4Lgcj2KIeT2Ou7LSm0EjyDjHZNMG2bdk9CdyGo5knTt1J3jeJS+3VDXMqnt 6apA== X-Gm-Message-State: AO0yUKWUksGNhukruc4KG/LCzSgeg2l7wvLgEMWvWLyHws08AEfUsyg8 y9fH23c8gGd5avSyedH+GAY4Og== X-Google-Smtp-Source: AK7set/Y/udvpjSPyiRHvzVVe58rgU9SM3O2saFl6KwR15sjL2H2TEzdTJbI8CgO0QDxGQ+4+3WsRQ== X-Received: by 2002:a17:902:ce86:b0:19a:9580:750 with SMTP id f6-20020a170902ce8600b0019a95800750mr5863992plg.16.1676264052424; Sun, 12 Feb 2023 20:54:12 -0800 (PST) Received: from debug.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id e5-20020a170902784500b00189e7cb8b89sm7078303pln.127.2023.02.12.20.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Feb 2023 20:54:12 -0800 (PST) From: Deepak Gupta To: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Alexander Viro , Eric Biederman , Kees Cook , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Deepak Gupta , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 RFC Zisslpcfi 07/20] elf: ELF header parsing in GNU property for cfi state Date: Sun, 12 Feb 2023 20:53:36 -0800 Message-Id: <20230213045351.3945824-8-debug@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230213045351.3945824-1-debug@rivosinc.com> References: <20230213045351.3945824-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 92112100007 X-Rspam-User: X-Stat-Signature: braq1pr6hhtbxtpniwktszzwkmjnk9j5 X-HE-Tag: 1676264053-631226 X-HE-Meta: U2FsdGVkX194wuSMax6cT93FLRufsx56iDRrfLkvRMPlP27f1TmH+JoBiOETTvVlbhgP1qOCStraNly62A/Gl9dxh6XwKMK0j+ijlcuiEUtlVy7Qss8YZreuunlQlzYhCKo1bRw6MLdpUCS7B6DCIBeN0Y4v8FSONiRX7kTNr5ddmkWaKPSLNRlRQbL5XTqC9iqBHcxeY1+BkBORODCsMP3hnXVLxwAVWvRkuBQQGcLahVd13sZRBuA9yNGNWlYyPmCsyBkqI1P/Qth8iCj3IsD/r7MaGmnRDdlMOmcwbxH+tsSo/5opN7gMYMYPlWqxQHNHdmLoWwXpEwubeY23DyQIvyxKf29mshvdxnTjUtq7gsa7t5OxXqSgdIQrfWwQRVDyQQZeM/e9irlX5uQ54NDj+92G/dSxc9B6aks081Eb9tU2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Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Binaries enabled with support for control-flow integrity will have new instructions that may fault on cpus which dont implement cfi mechanisms. This change adds - stub for setting up cfi state when loading a binary. Architecture specific implementation can choose to implement this stub and setup cfi state for program. - define riscv ELF flag marker for forward cfi and backward cfi in uapi/linux/elf.h Signed-off-by: Deepak Gupta --- fs/binfmt_elf.c | 5 +++++ include/linux/elf.h | 8 ++++++++ include/uapi/linux/elf.h | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 9a780fafc539..bb431052eb01 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1277,6 +1277,11 @@ static int load_elf_binary(struct linux_binprm *bprm) set_binfmt(&elf_format); +#if defined(CONFIG_USER_SHADOW_STACK) || defined(CONFIG_USER_INDIRECT_BR_LP) + retval = arch_elf_setup_cfi_state(&arch_state); + if (retval < 0) + goto out; +#endif #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES retval = ARCH_SETUP_ADDITIONAL_PAGES(bprm, elf_ex, !!interpreter); if (retval < 0) diff --git a/include/linux/elf.h b/include/linux/elf.h index c9a46c4e183b..106d28f065aa 100644 --- a/include/linux/elf.h +++ b/include/linux/elf.h @@ -109,4 +109,12 @@ static inline int arch_elf_adjust_prot(int prot, } #endif +#if defined(CONFIG_USER_SHADOW_STACK) || defined(CONFIG_USER_INDIRECT_BR_LP) +extern int arch_elf_setup_cfi_state(const struct arch_elf_state *state); +#else +static inline int arch_elf_setup_cfi_state(const struct arch_elf_state *state) +{ + return 0; +} +#endif #endif /* _LINUX_ELF_H */ diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index 4c6a8fa5e7ed..1cbd332061dc 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -468,4 +468,10 @@ typedef struct elf64_note { /* Bits for GNU_PROPERTY_AARCH64_FEATURE_1_BTI */ #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1U << 0) +/* .note.gnu.property types for RISCV: */ +/* Bits for GNU_PROPERTY_RISCV_FEATURE_1_FCFI/BCFI */ +#define GNU_PROPERTY_RISCV_FEATURE_1_AND 0xc0000000 +#define GNU_PROPERTY_RISCV_FEATURE_1_FCFI (1u << 0) +#define GNU_PROPERTY_RISCV_FEATURE_1_BCFI (1u << 1) + #endif /* _UAPI_LINUX_ELF_H */