From patchwork Wed Sep 27 22:48:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 145631 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2957105vqu; Wed, 27 Sep 2023 16:15:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEdtbe369+2lsb2jnkPZkf7U65kqVU9miHUIn2J/el8Kih7OxuRP8c1D1yQGnWgpt3GArrS X-Received: by 2002:a17:90b:3ec4:b0:274:dd15:87bf with SMTP id rm4-20020a17090b3ec400b00274dd1587bfmr10876612pjb.20.1695856514196; Wed, 27 Sep 2023 16:15:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695856514; cv=none; d=google.com; s=arc-20160816; b=FbrFxr1FO+3s8D58cP4KrbJOOJe3q2K4nrLLrbuZ3iyEcw0yjlqScPNwJ037DxxgX3 exNxglpucWn9Cw/sbUw4lu9jhscl9FODTIvLLC3zod9zpQ9F5vPkVd3JCpO35DZqkyUi 0P3gj+kqUGQ8tGgBNz0U2Xh0uHbUWUr5M9C7nQx12Uj9CirbaLzD/t0eFS8fXtmRkbN9 gdoflFm7wYxxZACzGQF3kRf2MdD/d1tcXxj0jg0xebpfi/oDsZ72HSM9Na7plpr0qI4e 966waK58fZ1DfFOSFo0oeAa616LCNxPdNSO/2Wr4U2wTQjwwTeYsQbpqhUvr+Nlx7Uaa XhnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=6h9NlhRD8hF3JGpC0yj7VRjIJqQSgsUALXY/NiPb6h0=; fh=yvN63QVq0nFna1WLidysZ4ad5g6OhQJzYTOcPhqxM3o=; b=v/bREM5YTZ3uyJ1nWJw4gXWNRoT5Xd7Rd5Z58DMgNLxvdxFZKkxwxjl9l0l+uYJ8IO e4RH3tH3Udz7yCGEQq2mL4u5Dm8lGKE6o0Orub5KdMp0nw6bjqRChk7FMvOj4i8cW6Zq ffLZ/xJkiKy7cZK+am2tVSpGa03rgaDJRp4vQnykFlKPZxOcCgzjP+veFqAoQJrSYOO3 GSasP81CESRhAgNO8GkMByWbr1zDpEnfMtBED4zyrgYD8qu8cOYKnS6idK860ZBBKQD5 dXz7BeLvLoIKSzBYHvXL0MFJMMOxlq7GijmROrb36kU4FzOiVoLOHrkgGyhHPybawFJB uX4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="W9P9xxx/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id i11-20020a17090a974b00b00271af83a25asi5148622pjw.59.2023.09.27.16.15.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 16:15:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="W9P9xxx/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 8997282D7572; Wed, 27 Sep 2023 15:48:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230049AbjI0Wsd (ORCPT + 20 others); Wed, 27 Sep 2023 18:48:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230036AbjI0WsO (ORCPT ); Wed, 27 Sep 2023 18:48:14 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9342102 for ; Wed, 27 Sep 2023 15:48:12 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1c5fddcf2f1so135790325ad.0 for ; Wed, 27 Sep 2023 15:48:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695854892; x=1696459692; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6h9NlhRD8hF3JGpC0yj7VRjIJqQSgsUALXY/NiPb6h0=; b=W9P9xxx/gnYx5osYpeI76vzlOzUEZfBvOLYTAO10FfRylT1mRq2bCMYcS1+hZft3AY eGZxePpApMxNN6c3WlWJBYdNELeMLib6wf1nyJ5oA4p5JDg7FCt1iW+URV2HCAkBPm2b eqQeNetpEjf0hkEkQ0YyuyF1TCs6Vs9UTvLLXUReoJtc6/JIdz05h1AAlp1beXi2B0lQ isFXBAT7jFPTgJQ8P+kuxLj6ro5eRYIb2ZFthvt5nDPRxsDGJoktPaerzsb8uVoQZzDu n62biOqvhU0Oo26flZg4+asLR8fOy1TKIRJYK5XRG5g8V4oNQYf9ODdXlY15hK8yzQ+i 2DEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695854892; x=1696459692; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6h9NlhRD8hF3JGpC0yj7VRjIJqQSgsUALXY/NiPb6h0=; b=oWKkRB7qu8zr+7JcouQvxbeuXqo44qFvVnYE5l3LGh3qHItGKZQPzuHtLfPKib4Z7r i+kAY4TGNeQMY+tgrEDYR9HZNP9mdfVKgLbFueL4HQbHEv/fHwsJFcFYtQbtckJOBNfd t5f2OWadFWaqvUy5qPluC6F9CjAg0/vAZKnpihor4NgXSTkldVJpqwlTJzcNnU1JWKFL mW3wrkTwHCR8SN8e6bz+aENjPI70aWUTEA4Sk78LXMrQ1tBUUyU3PfF2VCRLA44rSq6x N2emBL1gUzbYKm5yiPMIvr+cwFRTveCG2n7HNEpwIpjE3v1C526rh7wFAecR0X9aSY65 fFmw== X-Gm-Message-State: AOJu0Yz71/nrM7dM5v3awK3SlMAcAJPP+laIxxttpcyL8rpIQfq6OPIJ zQPVVIQ1P2mW0+EDwZ96/DBtniKs+XD9RA3ZdHE= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:903:2350:b0:1c6:d25:8750 with SMTP id c16-20020a170903235000b001c60d258750mr47383plh.10.1695854892186; Wed, 27 Sep 2023 15:48:12 -0700 (PDT) Date: Wed, 27 Sep 2023 22:48:04 +0000 In-Reply-To: <20230927224757.1154247-8-samitolvanen@google.com> Mime-Version: 1.0 References: <20230927224757.1154247-8-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=2082; i=samitolvanen@google.com; h=from:subject; bh=4N5TA1RYsqEG6l9M0xplln+cBa1Qt0iz0MCbO5csiTU=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBlFLEdbuqhUGRkvHlpnRFoLVngxWUbGDzCKHZjB gXmrezGpZuJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZRSxHQAKCRBMtfaEi7xW 7uL3C/9V/COsf71wMsf8LAD8PbXFSUssu/Ulme80Bc+IvZbku+VH0yCAi+0as+ACJEpRsTnNbIQ pScn4vacTqNH+5w4IriZtrxed/+JlJlJ3oFsv2kgI5IxJP44McljBfgT1GRyGCHurDabqPsJYX5 TaGUz9a7+gFF6iVgMLHb9uNWunfwjDU0mlECp6rp+occD5rm4k0XsmhUdYYDNB2N7vwL4ar7t7Y bRUrWcDXIDTzByGd1qZFeUPJ4XRuZmExIhm3XrLYrmbZvkYIH6oKzVv5knn9MHzzjsjfdiVDoME e2Bibk2Mj6F9ABYdpfXOcVNTMcGMZtsjvwmAv9Qx3j7PwTa5naJ0np9PriPDIM2jsshK4hOOa65 Z+fMEcQII52/5PTX5HX9sXBqn/ly0o9tU3LTzbR4GGVs6W1Rzk4Yoden9lVz+2Ti9QdMMMmqjSM hvdcZcYO8OTXRXZtW4dNPx1A4wMY6pfJadi3BaYatIPrJyHzova74Yk95MQNhaBqyh0DU= X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230927224757.1154247-14-samitolvanen@google.com> Subject: [PATCH v4 6/6] lkdtm: Fix CFI_BACKWARD on RISC-V From: Sami Tolvanen To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Kees Cook Cc: Clement Leger , Guo Ren , Deepak Gupta , Nathan Chancellor , Nick Desaulniers , Fangrui Song , linux-riscv@lists.infradead.org, llvm@lists.linux.dev, linux-kernel@vger.kernel.org, Sami Tolvanen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 27 Sep 2023 15:48:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778234440428645340 X-GMAIL-MSGID: 1778234440428645340 On RISC-V, the return address is before the current frame pointer, unlike on most other architectures. Use the correct offset on RISC-V to fix the CFI_BACKWARD test. Signed-off-by: Sami Tolvanen Acked-by: Kees Cook Tested-by: Nathan Chancellor --- drivers/misc/lkdtm/cfi.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/misc/lkdtm/cfi.c b/drivers/misc/lkdtm/cfi.c index fc28714ae3a6..6a33889d0902 100644 --- a/drivers/misc/lkdtm/cfi.c +++ b/drivers/misc/lkdtm/cfi.c @@ -68,12 +68,20 @@ static void lkdtm_CFI_FORWARD_PROTO(void) #define no_pac_addr(addr) \ ((__force __typeof__(addr))((uintptr_t)(addr) | PAGE_OFFSET)) +#ifdef CONFIG_RISCV +/* https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#frame-pointer-convention */ +#define FRAME_RA_OFFSET (-1) +#else +#define FRAME_RA_OFFSET 1 +#endif + /* The ultimate ROP gadget. */ static noinline __no_ret_protection void set_return_addr_unchecked(unsigned long *expected, unsigned long *addr) { /* Use of volatile is to make sure final write isn't seen as a dead store. */ - unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1; + unsigned long * volatile *ret_addr = + (unsigned long **)__builtin_frame_address(0) + FRAME_RA_OFFSET; /* Make sure we've found the right place on the stack before writing it. */ if (no_pac_addr(*ret_addr) == expected) @@ -88,7 +96,8 @@ static noinline void set_return_addr(unsigned long *expected, unsigned long *addr) { /* Use of volatile is to make sure final write isn't seen as a dead store. */ - unsigned long * volatile *ret_addr = (unsigned long **)__builtin_frame_address(0) + 1; + unsigned long * volatile *ret_addr = + (unsigned long **)__builtin_frame_address(0) + FRAME_RA_OFFSET; /* Make sure we've found the right place on the stack before writing it. */ if (no_pac_addr(*ret_addr) == expected)