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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54D03C6379F for ; Mon, 13 Feb 2023 04:54:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229898AbjBMEyo (ORCPT ); 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-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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 */ -- 2.25.1