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: 56029 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2175341wrn; Sun, 12 Feb 2023 20:55:35 -0800 (PST) X-Google-Smtp-Source: AK7set/vWxFt+XLfeTQMSMZ1cRMsv6kAPP4fu+/FXzPaiEVnqN6p7Jr/rFFNuAeAaJMSInMPSCsh X-Received: by 2002:aa7:9d1b:0:b0:5a8:b37e:bb5 with SMTP id k27-20020aa79d1b000000b005a8b37e0bb5mr2729683pfp.12.1676264135136; Sun, 12 Feb 2023 20:55:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676264135; cv=none; d=google.com; s=arc-20160816; b=kdyvzPQIKVFnV9yR/93VU2AyKwwJEeR7MvmSmTwE1o/7TwsdqCJucdqR34m4+9KnPM 2zqR+VLwp5A/7bXQusyaqbJjqmKNxf6JSmUrPFgoU0csGeYiaZ0bQ02gCyD1ys0SbUCl 6HdXgGRBFs0DDIdFMxitceLmxoYQo3kFmRKKRmhUcw25WwEvz7XHmGLqyHNSNJm/48pO EVDbUQbRxMl9fpmx1AGhp6py/tb+X/EJMmI28nnCx83q4izWbdD8W2UkpmSJY0jZUK5U OvDClSUsM3rEUTfG8CyFQAz57r2/jsg7TuezwnvCH9Wm0vzUo4wSNJzUZJV/CMYyqzHP 9tRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oP4wZ0K6nVayFSsIwI2M/qbAeCayW2BZaB6BdNSazd0=; b=G2t0t0pMgF1MehNW/ehYgUoZbNx1bLVsr6iiTFQrPp5BU6AO+QS5c6iozhR/DwUsqx 5cxu3LLW+luL4/lA+a/iO/xMgaMoMd++mW8n8XMWKLSY4sFngCLU3aZnTdPHpdXUvKs6 122Eag9ziBhpuVi1LniRPms8j95c1o6ZwZTrsD3CatSMwCH8VmJ+1ztRl+zeuZIyW6Nd IpqOhFJt587k1xf2qrNoiawLOTxZaW3fhmMNpP7bQQekiESnc4eKEnUPQQdu28SoFYDu 2EA/B4G7q60nV0fZcT3CP3iXXJdeJtyoC10JmV8IUxykEJLuZKayfZIIF89v94cOSJlq Ni9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=kQCkDlkh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w5-20020a626205000000b005925ce11f0asi10455939pfb.364.2023.02.12.20.55.23; Sun, 12 Feb 2023 20:55:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=kQCkDlkh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbjBMEyo (ORCPT + 99 others); Sun, 12 Feb 2023 23:54:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229814AbjBMEy0 (ORCPT ); Sun, 12 Feb 2023 23:54:26 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E7CB10ABE for ; Sun, 12 Feb 2023 20:54:13 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id r8so12328259pls.2 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=i080UbwPAEig8D8zShLUOIUP2xS9NbJMj3L2lIz0THLouULhxAkP6h3KVb+qrzjzPh /lZTonafhu7PMEAjPoc6b95tshRepMYGLxn6cnb+FgR/xaEmlmA9MIHkA41D79ZUEsx1 /cMo12zGiLsV5DM4ayjRFYxju0AiqBgE4+SJ+ipXK0n0A+k4FZCjet7MH5laF0uvOuQ4 dQeB8FnKjhr28cbnavMIpVc5PJloOTNgIwf+u1AT/9xd2oL8Y2Eoud/eTA38fDcQcgDy V/Xl/cQGrqm2dx4ik6dK2aUQxkSbUG5wrG1RmRinhuxdBrvqPmkX9BNaJqbHJb4NrL6L Vhhw== X-Gm-Message-State: AO0yUKXmsDwF4OB/SdEYa9f8v9ZVKIAie+KcMfdeSk+kImI/MG8ViwEP EGZSbZ3kHpcFjKc3YBwuMvM4vmRE+6G4Pt5b 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-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757690341414845206?= X-GMAIL-MSGID: =?utf-8?q?1757690341414845206?= 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 */