From patchwork Fri Oct 5 08:13:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 148148 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp150974lji; Fri, 5 Oct 2018 01:13:46 -0700 (PDT) X-Google-Smtp-Source: ACcGV62SpdwrJCF1r54MPH8ZThpLy748LE2hw70/487Pxv2D3ToFskZAcEwpUz6csOcFWXzd6K20 X-Received: by 2002:a63:441f:: with SMTP id r31-v6mr9172219pga.60.1538727226521; Fri, 05 Oct 2018 01:13:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538727226; cv=none; d=google.com; s=arc-20160816; b=VV8QL1h3L/bvWuY2uPnIBKj7UJLVXU00VxNxOe2x8Mf3saO98qmLyIqNv+hI+6T+7h eAVORJ/Vno5pV9PYIKZfPhfRop6d0szXYcPvjkzSwZHeXPJ5BvW5ywqqdpyxgjHZ2ItS 7IHPYEsjm1cYU0Ji0scQvzJNyZ2Y5ynCr4TbPimbPGQx3woSQr1MLs22koRU2cDlvHRT jMFnKDwXubXzjLt4da09vFC7zKtIs2HtM1ucY9hHol5zOzhoM4LvtW1IZ8sqlQuxaSi6 wGuUNUnLgBrGMhuatTmA3uXyZaWEnAW6DeBarWNF7KoOPwq73r7V3G4ZJAomIFUlYnU9 icKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=Bsz7BDS1SKX9gHYXcW+7Yci+Ue2vSEITfG0cme0MbDA=; b=YTSOHzfZBvRrMVh8aXvBGSb5w2GV2idw6NVyuQBd78Q4imBKzYJpFbCbhBtVvitTaW eBqz4rFXvaTnpmAiTTRtzOpOupgKqMvSCVKFc/wjrLMWPgLExbCZ7S2lfPeUIfpnwfq/ oof2XzjJEwuyiNRrHEslcjOPCCmNtxK7uTZbn92bvjnBaRE4t4XiZy2keuR9at8JIb34 Y+TAzzSmfyrDcyMSkdOU6xb4zpuDkRhenWHiMOCT9QfqjKS632Qx4B2cLn+kUAiFm9Rz ji5yacM3hVGP7I/n3DbTiC7msXHBrhDJpODDnkeBZcbxHGiDucOqQuLCWmmZ7R9tCTJr q2WA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kI1I9Xac; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y30-v6si7612388pgk.13.2018.10.05.01.13.46; Fri, 05 Oct 2018 01:13:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kI1I9Xac; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728259AbeJEPLT (ORCPT + 2 others); Fri, 5 Oct 2018 11:11:19 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:51290 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728200AbeJEPLS (ORCPT ); Fri, 5 Oct 2018 11:11:18 -0400 Received: by mail-wm1-f66.google.com with SMTP id 143-v6so983090wmf.1 for ; Fri, 05 Oct 2018 01:13:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Bsz7BDS1SKX9gHYXcW+7Yci+Ue2vSEITfG0cme0MbDA=; b=kI1I9XaciHUjWQntHHKbdAEv8Lm6aYT5IbNk7lfmiPNfZV7PrVjSNpmFq2Jb0VlwbE ZuX3XeBgMPpPy+wOgvVxMR/QHwgKKUOYnCADUKvUpr9aZKOY7+D76TJkI/VCLKkKNez8 +gmvc8uxFGNprC67bkbat2uDXwlxiRbel6D0c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Bsz7BDS1SKX9gHYXcW+7Yci+Ue2vSEITfG0cme0MbDA=; b=Az9rJt5gA6GT55rQBpDZ0v+fRPiqQdwwj8O7d9F5KrypBeOJ4RtqkDDnm9a13stRVA 77QmpSTPo6aXuAyITGCDB/5/QIHUZiypbPrtldKZNi6SfcGHPZJmSDgjf+wFS4jVyuDp tpoJw/KuXzDBcGXvF9Lg6ay930Q1F+BUqeT6XfeWxoy37VG6fM03Wh+5GfJXnygfF8Vy l6cLB/nW6gN8OpUWgz80iH0KFV9CBT8NFAau1OJp4eUfV7KNItKxsd7D4Gq10dl43t3e hiJaw0sFWuTuqOPFoiEz++p26DRVv8Pm0k65aLZyo2PFWs+lMC4WLr6Gpawady0Cm4Um cDgw== X-Gm-Message-State: ABuFfohRgNPAxbwqEepJcc3XsqqzJCCB2408C9TlGc5VdaV5T4hkO+BL u497pTkzIUKCR/AlvTxzq117+g== X-Received: by 2002:a1c:14d1:: with SMTP id 200-v6mr7367930wmu.106.1538727221719; Fri, 05 Oct 2018 01:13:41 -0700 (PDT) Received: from localhost.localdomain ([2a01:cb1d:112:6f00:697e:67d9:a05d:22c7]) by smtp.gmail.com with ESMTPSA id t4-v6sm6565620wrb.45.2018.10.05.01.13.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 Oct 2018 01:13:40 -0700 (PDT) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , "Jason A . Donenfeld" , Eric Biggers , Samuel Neves , Andy Lutomirski , Arnd Bergmann , Herbert Xu , "David S. Miller" , Catalin Marinas , Will Deacon , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Thomas Gleixner , Ingo Molnar , Kees Cook , "Martin K. Petersen" , Greg Kroah-Hartman , Andrew Morton , Richard Weinberger , Peter Zijlstra , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org Subject: [RFC PATCH 2/9] arm64: kernel: add arch support for patchable function pointers Date: Fri, 5 Oct 2018 10:13:26 +0200 Message-Id: <20181005081333.15018-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181005081333.15018-1-ard.biesheuvel@linaro.org> References: <20181005081333.15018-1-ard.biesheuvel@linaro.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Implement arm64 support for patchable function pointers by emitting them as branch instructions (and a couple of NOPs in case the new target is out of range of a normal branch instruction.) Signed-off-by: Ard Biesheuvel --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/ffp.h | 35 ++++++++++++++++++++ arch/arm64/kernel/insn.c | 22 ++++++++++++ 3 files changed, 58 insertions(+) -- 2.11.0 diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 1b1a0e95c751..db8c9e51c56d 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -102,6 +102,7 @@ config ARM64 select HAVE_ALIGNED_STRUCT_PAGE if SLUB select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_BITREVERSE + select HAVE_ARCH_FFP select HAVE_ARCH_HUGE_VMAP select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_KASAN if !(ARM64_16K_PAGES && ARM64_VA_BITS_48) diff --git a/arch/arm64/include/asm/ffp.h b/arch/arm64/include/asm/ffp.h new file mode 100644 index 000000000000..678dc1262218 --- /dev/null +++ b/arch/arm64/include/asm/ffp.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_FFP_H +#define __ASM_FFP_H + +struct ffp { + u32 insn[5]; + u32 def_branch; +}; + +#define DECLARE_FFP(_fn, _def) \ + extern typeof(_def) _fn; \ + extern struct ffp const __ffp_ ## _fn + +#define DEFINE_FFP(_fn, _def) \ + DECLARE_FFP(_fn, _def); \ + asm(" .pushsection \".text\", \"ax\", %progbits \n" \ + " .align 3 \n" \ + " .globl " #_fn " \n" \ + " .globl __ffp_" #_fn " \n" \ + #_fn " : \n" \ + "__ffp_" #_fn " : \n" \ + " b " #_def " \n" \ + " nop \n" \ + " nop \n" \ + " nop \n" \ + " nop \n" \ + " b " #_def " \n" \ + " .popsection \n"); \ + EXPORT_SYMBOL(__ffp_ ## _fn) + +extern void ffp_set_target(const struct ffp *m, void *new_fn); +extern void ffp_reset_target(const struct ffp *m); + +#endif diff --git a/arch/arm64/kernel/insn.c b/arch/arm64/kernel/insn.c index 2b3413549734..a2ed547fd171 100644 --- a/arch/arm64/kernel/insn.c +++ b/arch/arm64/kernel/insn.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -1603,3 +1604,24 @@ u32 aarch64_insn_gen_extr(enum aarch64_insn_variant variant, insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, Rn); return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, Rm); } + +void ffp_set_target(const struct ffp *m, void *new_fn) +{ + u32 branch = aarch64_insn_gen_branch_imm((u64)m, (u64)new_fn, + AARCH64_INSN_BRANCH_NOLINK); + + if (branch == AARCH64_BREAK_FAULT) { + /* TODO out of range - use a PLT sequence instead */ + } else { + aarch64_insn_patch_text((void *[]){ (void *)m }, &branch, 1); + } +} +EXPORT_SYMBOL(ffp_set_target); + +void ffp_reset_target(const struct ffp *m) +{ + u32 branch = le32_to_cpu(m->def_branch); + + aarch64_insn_patch_text((void *[]){ (void *)m }, &branch, 1); +} +EXPORT_SYMBOL(ffp_reset_target);