Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3241632imu; Sat, 24 Nov 2018 00:53:36 -0800 (PST) X-Google-Smtp-Source: AFSGD/X541G9Ntu9igAn9Kg7VFuGWVkSmBtY2YwtYosjBL23ZOJ/Vh4E8drp0OZBwQB8d/173daf X-Received: by 2002:a62:5182:: with SMTP id f124mr17090052pfb.238.1543049616424; Sat, 24 Nov 2018 00:53:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543049616; cv=none; d=google.com; s=arc-20160816; b=BcKuzQ96wV3cK8va9StP3hzjZPXxk0ErXZe5CITuUbt3duF60YCnnZaWVYuLckHDH9 Sxu9VJQ6vWRTRCv8dgw+8kcfuuLWm3C0Uw56otQpO+ulnG2HsFdMjL928zj4j0ssxaZZ CfNWwu7KXoOnPGJn95LHNNvgfIbPC3MYQr85OxdOpcqv0Wy8BksLHGdLH+5/4XGoOmo6 ZAM0Mjtr1Fuw+3CLYlZK7BizLE0VijcFwdDcP4zMNUdv97v66kFT88cF5rFwIvNfhgzP WBDHNrxsYWRWHrYG0OFyyn8C9LXHKKJWFWpLZ1i+RR8YsWw2+XP5ZlA90gJMFn+0z+rT pM1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+ZFGLcFsJGBJa7lwONQ9ze1SyW/x9MifBvOkbZkDguY=; b=b6RTRs34BecNPmHvEggklVaNYCuYPmTLRSok9bIMZ11btM2Jbgnd+LmRr7lpT9mJyi uwaiklGC1OeEm3ko25u0RTvZcsxdNn281K65Ut1SiR4Z2xmiRzqN9DAR9vOzzyQOzT4Q YweyLNyTOOk/gfMXD+oqZglzQt0otTNMQS/oMM60SDACBj3g0T8UPNtS4XBfS29EeAAT EcLJhSFm+Vj/twIHxRT6J0P5Ffdc5GjW/uuaOaVwLOQfXGORpiyDLIR7703eRB5IHGjg JHegQJ+/ob10mYCol64KYQFa9qw9sAfjBV7JgfuszB2IJf1g3w/WMGKDwrcU5bki/9fn YjdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Emne+hOT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 f34si46888975pgm.318.2018.11.24.00.53.22; Sat, 24 Nov 2018 00:53:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=Emne+hOT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 S1727419AbeKXJEb (ORCPT + 99 others); Sat, 24 Nov 2018 04:04:31 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:33978 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727338AbeKXJEb (ORCPT ); Sat, 24 Nov 2018 04:04:31 -0500 Received: by mail-wm1-f68.google.com with SMTP id y185so8402304wmd.1 for ; Fri, 23 Nov 2018 14:18:26 -0800 (PST) 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 :mime-version:content-transfer-encoding; bh=+ZFGLcFsJGBJa7lwONQ9ze1SyW/x9MifBvOkbZkDguY=; b=Emne+hOT+VIshM/P0PVECpzJvCjCTEAHU4CgmMgHaVVjc/CQIdqc/nhh+BSX0p41t8 9zNZI9dpIytkxwi9W+rED1uFCrlRDR7ofv6T67YKYbF4UAF5BTvwdv8hzdRUDKfqBBlh xe07d88+VV0aODCzewN9dJkVy3UaruscOzP+I= 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:mime-version:content-transfer-encoding; bh=+ZFGLcFsJGBJa7lwONQ9ze1SyW/x9MifBvOkbZkDguY=; b=ChQzvrOCOaJTYvPL9G/qBzoAygdrdLFSuY/caKTDQ+vLsTtcglhwU+h9Gx5yqCHh6v ttQXCAO9v/o9wRb/RxH9sXnXO6goRhX0AGTqhH8mWbvEUZgiJoVrdNAIXTJLJujTC6fl 4awwU/YRnLio7fvoevllZ0B9kGqLTOxrPYBj1cw3Wxse1MIrLNdp6srhLv0cOs5Blk0b l+azVpu2uhNntPZfZHSUq0cz/eNUIIi3qRZ5uN2EmEDDV0VqRFbgh1RrO2CSh8YIgu5r H6pFM0/YdNuqOEGbIu4eDFrFhRsKfTZeDD3t2CrvIHn9j/1el88b/YULXHHqEm69XvTs UeFg== X-Gm-Message-State: AGRZ1gIewVWhlVcrE6/iKFuj5YeeuJD8nw+S3hwErynniK/E1+6da5QI mmR8rZGDuoze9rPQcc06iPFRgeJpewY= X-Received: by 2002:a1c:b54b:: with SMTP id e72mr15403683wmf.73.1543011505644; Fri, 23 Nov 2018 14:18:25 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:6913:f64b:5e59:5ba5]) by smtp.gmail.com with ESMTPSA id y13sm12578267wrw.85.2018.11.23.14.18.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Nov 2018 14:18:24 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Daniel Borkmann , Alexei Starovoitov , Rick Edgecombe , Eric Dumazet , Jann Horn , Kees Cook , Jessica Yu , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , "David S. Miller" , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org Subject: [PATCH v4 2/2] arm64/bpf: don't allocate BPF JIT programs in module memory Date: Fri, 23 Nov 2018 23:18:04 +0100 Message-Id: <20181123221804.440-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181123221804.440-1-ard.biesheuvel@linaro.org> References: <20181123221804.440-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The arm64 module region is a 128 MB region that is kept close to the core kernel, in order to ensure that relative branches are always in range. So using the same region for programs that do not have this restriction is wasteful, and preferably avoided. Now that the core BPF JIT code permits the alloc/free routines to be overridden, implement them by vmalloc()/vfree() calls from a dedicated 128 MB region set aside for BPF programs. This ensures that BPF programs are still in branching range of each other, which is something the JIT currently depends upon (and is not guaranteed when using module_alloc() on KASLR kernels like we do currently). It also ensures that placement of BPF programs does not correlate with the placement of the core kernel or modules, making it less likely that leaking the former will reveal the latter. This also solves an issue under KASAN, where shadow memory is needlessly allocated for all BPF programs (which don't require KASAN shadow pages since they are not KASAN instrumented) Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/memory.h | 5 ++++- arch/arm64/net/bpf_jit_comp.c | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h index b96442960aea..ee20fc63899c 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -62,8 +62,11 @@ #define PAGE_OFFSET (UL(0xffffffffffffffff) - \ (UL(1) << (VA_BITS - 1)) + 1) #define KIMAGE_VADDR (MODULES_END) +#define BPF_JIT_REGION_START (VA_START + KASAN_SHADOW_SIZE) +#define BPF_JIT_REGION_SIZE (SZ_128M) +#define BPF_JIT_REGION_END (BPF_JIT_REGION_START + BPF_JIT_REGION_SIZE) #define MODULES_END (MODULES_VADDR + MODULES_VSIZE) -#define MODULES_VADDR (VA_START + KASAN_SHADOW_SIZE) +#define MODULES_VADDR (BPF_JIT_REGION_END) #define MODULES_VSIZE (SZ_128M) #define VMEMMAP_START (PAGE_OFFSET - VMEMMAP_SIZE) #define PCI_IO_END (VMEMMAP_START - SZ_2M) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index a6fdaea07c63..76c2ab40c02d 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -940,3 +940,16 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) tmp : orig_prog); return prog; } + +void *bpf_jit_alloc_exec(unsigned long size) +{ + return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START, + BPF_JIT_REGION_END, GFP_KERNEL, + PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE, + __builtin_return_address(0)); +} + +void bpf_jit_free_exec(void *addr) +{ + return vfree(addr); +} -- 2.19.1