Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp863831imu; Sat, 17 Nov 2018 10:59:57 -0800 (PST) X-Google-Smtp-Source: AJdET5fcmvUEp/AEpoJu3Y3q0D4/Sm9InxZajfN1Pqyt8qN0lc+YosSahUaXmQl+lLny7uv/uZdO X-Received: by 2002:a63:4f20:: with SMTP id d32mr14502812pgb.47.1542481197117; Sat, 17 Nov 2018 10:59:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542481197; cv=none; d=google.com; s=arc-20160816; b=hzv4EdrqcMJOcPcH/cmaxJMcAPYeNSCc/B9Zz1EAVGYDsV5dOetvWNDtmCs11PAUIa bAx6UYPrb1JD72weeTFTDhT79kf4V+q7uO+HaSP81iDFuxCPyuIxiLKRucQPuaY4opyY jAOYjJlOtYUnWqnndDh57tcwjOjfTaBGBFcURFI1KFIBG4PiwxWhgqfA2sWSZWIxm3Lc lnhVuVDaqLKjcdyUrSchg1Gk4fLJ8Tu+xBe18DpWDyNLV8xYUJ63Z0/ggYcIcpzqiP4Y Gt7yyGMu2twOjt9xt22V8CAqfzYir0FAKmI91Cgpli+LdMznGDjt49b3vNvtEYxbQXtv hkhw== 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=vKtcwajq6uet7RwpHABNJRRfvhJ5RLh+7DwnrP6tkoU=; b=KrL2sRVbOHwUkY/tNmHqdMFRxjWClE48B2+295e92sBCnFlKjzdHUj2P3YDk/PhWeE 69Kh5AW/qQRMWp26lUSOTDuR70iMLOH7hOefakzX1JZuyiXfp6dISGQsq1M2Er4Aodmu JCLLY3ZWAZzn+eTvPi7TolDmfGcTHe488aH8b8HxIAUHRKS0z+yUQ7QfV/aiSE7XnuOl SgHHgoSnJEwt/MiHe4w5g/TGK7PMUwwtFKQ/cDL9b6IpS9QR+n64aiS2zni3/F2SkkwQ HdGtcKoXpP5jovvx6lU4FG4IHCyTIaeaz+qDlUFQ//oOoVgVtuVnikmp2L2affe9yRUs ysxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HCrZnyJK; 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 b5-v6si1437013pfe.168.2018.11.17.10.59.41; Sat, 17 Nov 2018 10:59:57 -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=HCrZnyJK; 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 S1726720AbeKRFO4 (ORCPT + 99 others); Sun, 18 Nov 2018 00:14:56 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:42843 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725734AbeKRFO4 (ORCPT ); Sun, 18 Nov 2018 00:14:56 -0500 Received: by mail-pg1-f193.google.com with SMTP id d72so8580121pga.9 for ; Sat, 17 Nov 2018 10:57:21 -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; bh=vKtcwajq6uet7RwpHABNJRRfvhJ5RLh+7DwnrP6tkoU=; b=HCrZnyJKCfFnJaZK1DpCvziEmRstnj4rwYTbT92UFXz73rvqQ9ZSjTGDU8wRnGgNJ+ ExMscyjZDgE+uBnbk94MoYbWGi/Ppcc1XZG5FsvUrcfYFnni03qp7O8Pw8kD/mV3EtA8 4qJ0sf0q5nq3snS/fKcmV1/2jIO7ddsYaFvVw= 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=vKtcwajq6uet7RwpHABNJRRfvhJ5RLh+7DwnrP6tkoU=; b=SGXvjkV8ANfkNjZlp5iETpAsyhItEP8O8GTlBSqM5nOyG7LV8xLFHcfnW+0Hw428p4 IBG9NbuWIvljTwemY5nT1GeXRL+wHPSFPN2Ts2ZuUWth6HZ/SqMBF/YQpJ2dxLiQ9DXK kQko3xITadnBJPvNjbjo1JhwsdTmxWCuAXM4Qj11HinOI4zfpmPIa0ydrepPo7yM8hSZ 74HenuH8TDkBKPmndk6+aBeFQ5ySP2JQsaVog11NyyTArwlpoB/lbCKc2KGeYUyd0trE hfRLF6lzh3V6IJYSOgUUw4RzrSdTh8YNECGrp21xJc3gJfsPFtvj3i9zfRC52sD36RRp bKBw== X-Gm-Message-State: AGRZ1gIajYTR+swMejtv57BaszcKF+BplpEamHjKn9jt1C+EdMdyvXxQ ZBVdj51Mto2EfeV0J78qsQ4cckAYT2dRoA== X-Received: by 2002:a62:cac4:: with SMTP id y65-v6mr16264821pfk.27.1542481040868; Sat, 17 Nov 2018 10:57:20 -0800 (PST) Received: from mba13.psav.com ([64.114.255.114]) by smtp.gmail.com with ESMTPSA id u76-v6sm49550745pfa.176.2018.11.17.10.57.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Nov 2018 10:57:20 -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 , Ralf Baechle , Paul Burton , James Hogan , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "David S. Miller" , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, sparclinux@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 1/4] bpf: account for freed JIT allocations in arch code Date: Sat, 17 Nov 2018 10:57:12 -0800 Message-Id: <20181117185715.25198-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181117185715.25198-1-ard.biesheuvel@linaro.org> References: <20181117185715.25198-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit ede95a63b5e84 ("bpf: add bpf_jit_limit knob to restrict unpriv allocations") added a call to bpf_jit_uncharge_modmem() to the routine bpf_jit_binary_free() which is called from the __weak bpf_jit_free(). This function is overridden by arches, some of which do not call bpf_jit_binary_free() to release the memory, and so the released memory is not accounted for, potentially leading to spurious allocation failures. So replace the direct calls to module_memfree() in the arch code with calls to bpf_jit_binary_free(). Signed-off-by: Ard Biesheuvel --- arch/mips/net/bpf_jit.c | 2 +- arch/powerpc/net/bpf_jit_comp.c | 2 +- arch/powerpc/net/bpf_jit_comp64.c | 5 +---- arch/sparc/net/bpf_jit_comp_32.c | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/arch/mips/net/bpf_jit.c b/arch/mips/net/bpf_jit.c index 4d8cb9bb8365..1b69897274a1 100644 --- a/arch/mips/net/bpf_jit.c +++ b/arch/mips/net/bpf_jit.c @@ -1264,7 +1264,7 @@ void bpf_jit_compile(struct bpf_prog *fp) void bpf_jit_free(struct bpf_prog *fp) { if (fp->jited) - module_memfree(fp->bpf_func); + bpf_jit_binary_free(bpf_jit_binary_hdr(fp)); bpf_prog_unlock_free(fp); } diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c index d5bfe24bb3b5..a1ea1ea6b40d 100644 --- a/arch/powerpc/net/bpf_jit_comp.c +++ b/arch/powerpc/net/bpf_jit_comp.c @@ -683,7 +683,7 @@ void bpf_jit_compile(struct bpf_prog *fp) void bpf_jit_free(struct bpf_prog *fp) { if (fp->jited) - module_memfree(fp->bpf_func); + bpf_jit_binary_free(bpf_jit_binary_hdr(fp)); bpf_prog_unlock_free(fp); } diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c index 50b129785aee..84c8f013a6c6 100644 --- a/arch/powerpc/net/bpf_jit_comp64.c +++ b/arch/powerpc/net/bpf_jit_comp64.c @@ -1024,11 +1024,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) /* Overriding bpf_jit_free() as we don't set images read-only. */ void bpf_jit_free(struct bpf_prog *fp) { - unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK; - struct bpf_binary_header *bpf_hdr = (void *)addr; - if (fp->jited) - bpf_jit_binary_free(bpf_hdr); + bpf_jit_binary_free(bpf_jit_binary_hdr(fp)); bpf_prog_unlock_free(fp); } diff --git a/arch/sparc/net/bpf_jit_comp_32.c b/arch/sparc/net/bpf_jit_comp_32.c index a5ff88643d5c..01bda6bc9e7f 100644 --- a/arch/sparc/net/bpf_jit_comp_32.c +++ b/arch/sparc/net/bpf_jit_comp_32.c @@ -759,7 +759,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf]; void bpf_jit_free(struct bpf_prog *fp) { if (fp->jited) - module_memfree(fp->bpf_func); + bpf_jit_binary_free(bpf_jit_binary_hdr(fp)); bpf_prog_unlock_free(fp); } -- 2.17.1