From patchwork Fri Sep 4 11:23:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357371 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=YtuBg19G; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb7L4pqKz9sTg for ; Fri, 4 Sep 2020 21:29:50 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730178AbgIDL3p (ORCPT ); Fri, 4 Sep 2020 07:29:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730193AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE2AFC061233 for ; Fri, 4 Sep 2020 04:24:14 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id s13so5718147wmh.4 for ; Fri, 04 Sep 2020 04:24:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UsZcPNRuXjAj8BE6HDRDUYJhFGRJK7BfcdRk8sZ8GDQ=; b=YtuBg19GPz8DWrNVVsU8ZrW/10qMfj50ogiJo08i8yu17bpZh8aQ0EKI823xkmTKrV eWqaBcILybItyqNwyrdvKaM69rCXJ72EINmS2O+fXF+i5s27oIV2HD4YOElwxvamGD8O fBUT+4MCgwgeFjuk8D4RWPwvZA7HHc1MywWbk= 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=UsZcPNRuXjAj8BE6HDRDUYJhFGRJK7BfcdRk8sZ8GDQ=; b=RZDXRLLOqAebdVpW4Ijy37DmytIrDELe1xtDRByP9YWIi9f9FRq1a8Hdr8sglmnq0w nM4Fvm+YbFmhilsBP/TL0NhGjpOIBvFsLBMh0P6OU6ed+mmSh+HdQXRa0yWJGiv9/jGM VXwG/MsZxZwOhh8dCam+Hx4vFYEm4gnNaGtpbyFF0NA9DXzKHjXfDJ4O1kvl4pI4l7si 5Mo3Sku40ogmaplba2jsxLwa3VrMFOMWa9PnUGH3kmWbouksxyU1redYIgim7jHBEKa7 zxmJS/TvTcR3zXxk+hKler4ChjfBGAH44cfAouga9FUHC+rUOGLRpyY1BAj3lJLkhC6N QK9g== X-Gm-Message-State: AOAM531/waw+ZdAo1dvSyjl9OlfKjwRaHWYrPvmB1Zii5XJdjjEBtpRa 4Kx7zRmURw+oNnzHXmTMY2eh6Q== X-Google-Smtp-Source: ABdhPJxwtNebf6IeEa0Jv492IQAbM2vaZWF56FrY2QjO0a1OdHsPeLnw/83uQm6aBdwGdUO12W8Z6A== X-Received: by 2002:a7b:c0c5:: with SMTP id s5mr7026992wmh.152.1599218653412; Fri, 04 Sep 2020 04:24:13 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:12 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 01/11] btf: Fix BTF_SET_START_GLOBAL macro Date: Fri, 4 Sep 2020 12:23:51 +0100 Message-Id: <20200904112401.667645-2-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The extern symbol declaration should be on the BTF_SET_START macro, not on BTF_SET_START_GLOBAL, since in the global case the symbol will be declared in a header somewhere. Fixes: eae2e83e6263 ("bpf: Add BTF_SET_START/END macros") Signed-off-by: Lorenz Bauer --- include/linux/btf_ids.h | 6 +++--- tools/include/linux/btf_ids.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/linux/btf_ids.h b/include/linux/btf_ids.h index 210b086188a3..42aa667d4433 100644 --- a/include/linux/btf_ids.h +++ b/include/linux/btf_ids.h @@ -121,7 +121,8 @@ asm( \ #define BTF_SET_START(name) \ __BTF_ID_LIST(name, local) \ -__BTF_SET_START(name, local) +__BTF_SET_START(name, local) \ +extern struct btf_id_set name; #define BTF_SET_START_GLOBAL(name) \ __BTF_ID_LIST(name, globl) \ @@ -131,8 +132,7 @@ __BTF_SET_START(name, globl) asm( \ ".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \ ".size __BTF_ID__set__" #name ", .-" #name " \n" \ -".popsection; \n"); \ -extern struct btf_id_set name; +".popsection; \n"); #else diff --git a/tools/include/linux/btf_ids.h b/tools/include/linux/btf_ids.h index 210b086188a3..42aa667d4433 100644 --- a/tools/include/linux/btf_ids.h +++ b/tools/include/linux/btf_ids.h @@ -121,7 +121,8 @@ asm( \ #define BTF_SET_START(name) \ __BTF_ID_LIST(name, local) \ -__BTF_SET_START(name, local) +__BTF_SET_START(name, local) \ +extern struct btf_id_set name; #define BTF_SET_START_GLOBAL(name) \ __BTF_ID_LIST(name, globl) \ @@ -131,8 +132,7 @@ __BTF_SET_START(name, globl) asm( \ ".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \ ".size __BTF_ID__set__" #name ", .-" #name " \n" \ -".popsection; \n"); \ -extern struct btf_id_set name; +".popsection; \n"); #else From patchwork Fri Sep 4 11:23:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357366 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=LftXd0gA; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb6546rvz9sTg for ; Fri, 4 Sep 2020 21:28:45 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730182AbgIDL2j (ORCPT ); Fri, 4 Sep 2020 07:28:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730187AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F067C06123A for ; Fri, 4 Sep 2020 04:24:16 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id z1so6408977wrt.3 for ; Fri, 04 Sep 2020 04:24:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Wv2ddb+2QDJ2ZGRPuXQTiHYRYSzKaO0Pxw0bhRAUNbo=; b=LftXd0gAOiyniRk9C7WdEhC4KppmqK4hsYGQlUk5mKlxWg5X9r3MsPE8O1gnVfNlij aQg3Me/sxqLYaZd99RlpCyQAM4S6sC3pW5hA5uDfg4gqIPnaRpl3Wxd+6UI/3Ma5vSXB PvbJCye+wOqOOeaP5byZSOMPCWpE29016yC+k= 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=Wv2ddb+2QDJ2ZGRPuXQTiHYRYSzKaO0Pxw0bhRAUNbo=; b=Ctjj9r3Eaf7nVdKQ4aNe/SS0wk5bFjLbJvA+5rxjX+t4+mp67vWd3f5TEl0OO2I3Jf 7oknmCl1G1PKXId/3P1Hj1gw0K7y4De+1B5h6dn62e5SB0oify1oDvVqv8TYcb9CkkFy mdLnVKtV2RrfWmAx9ZeLhIrLsJH1k16LGubnnRsp14wzwApxjY486WRCVga5QZyaHP/b YkTkcDJ7Qt1J4DQdQ6rA82m6bItGuVXrKjNiw01poRZcj7PXKlex48zjpF/7+WfwQ3r2 YmbQsydl34E+IowdFlQCGX4BlJupra7ovn42YKPsSvAQ2wmdHoSLY9tuZDkNqULc+nAW /R8Q== X-Gm-Message-State: AOAM532ARh7PJzBrpN7nFjYDOPwx14z1dR35VxQZuVPy/93Rnt9jkaME w+RvyJO0jShBwxcOp4MpgopgyQ== X-Google-Smtp-Source: ABdhPJyKnrZeNZ2IMQWcgv7+l+SG0nUyHTLMeud8GZY4Zfc9Huk/HsF8nPIUwN95oHJggIxADlKtsA== X-Received: by 2002:adf:f082:: with SMTP id n2mr1838517wro.35.1599218654825; Fri, 04 Sep 2020 04:24:14 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:14 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 02/11] btf: add a global set of valid BTF socket ids Date: Fri, 4 Sep 2020 12:23:52 +0100 Message-Id: <20200904112401.667645-3-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Similar to the existing btf_sock_ids, add a set for the same data. This allows searching for sockets using btf_set_contains. Signed-off-by: Lorenz Bauer --- include/linux/btf_ids.h | 1 + net/core/filter.c | 7 +++++++ tools/include/linux/btf_ids.h | 1 + 3 files changed, 9 insertions(+) diff --git a/include/linux/btf_ids.h b/include/linux/btf_ids.h index 42aa667d4433..eb6739ebbba4 100644 --- a/include/linux/btf_ids.h +++ b/include/linux/btf_ids.h @@ -174,6 +174,7 @@ MAX_BTF_SOCK_TYPE, }; extern u32 btf_sock_ids[]; +extern struct btf_id_set btf_sock_ids_set; #endif #endif diff --git a/net/core/filter.c b/net/core/filter.c index 47eef9a0be6a..c7f96cfea1b0 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -9903,8 +9903,15 @@ BTF_ID_LIST_GLOBAL(btf_sock_ids) #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type) BTF_SOCK_TYPE_xxx #undef BTF_SOCK_TYPE + +BTF_SET_START_GLOBAL(btf_sock_ids_set) +#define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type) +BTF_SOCK_TYPE_xxx +#undef BTF_SOCK_TYPE +BTF_SET_END(btf_sock_ids_set) #else u32 btf_sock_ids[MAX_BTF_SOCK_TYPE]; +struct btf_id_set btf_sock_ids_set; #endif static bool check_arg_btf_id(u32 btf_id, u32 arg) diff --git a/tools/include/linux/btf_ids.h b/tools/include/linux/btf_ids.h index 42aa667d4433..eb6739ebbba4 100644 --- a/tools/include/linux/btf_ids.h +++ b/tools/include/linux/btf_ids.h @@ -174,6 +174,7 @@ MAX_BTF_SOCK_TYPE, }; extern u32 btf_sock_ids[]; +extern struct btf_id_set btf_sock_ids_set; #endif #endif From patchwork Fri Sep 4 11:23:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357370 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=LQLaK3E4; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb795Xlmz9sTg for ; Fri, 4 Sep 2020 21:29:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730143AbgIDL3j (ORCPT ); Fri, 4 Sep 2020 07:29:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730196AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38654C0611E0 for ; Fri, 4 Sep 2020 04:24:17 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id x14so6332418wrl.12 for ; Fri, 04 Sep 2020 04:24:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=owpNQQ+uIJCQXVSt0+1sgwOe70+wghAb9TxeyhOSzdA=; b=LQLaK3E4hhgF0sqaa0xcxlqC4hcBE8zEPudcwELM/WbxI905d8LURtEJjAXPqG+q47 b6yOTtoRGYqonMVvHroDJW+iavdF/IQ8n4XQqHeu/AxzXXyJalJAPvrl1UZYCkzGPl+X 1nZgnwHgh0euMJFvEWXHynGxVt4J43Rq2eizo= 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=owpNQQ+uIJCQXVSt0+1sgwOe70+wghAb9TxeyhOSzdA=; b=Myn5anc0nb37YB3nV03FwavCMOMU5PL8MHcRjYO7J/hHbay0mCAb/lmHQuj+6iCDO+ Tmpk3gmT+2/S7Am0ZVff5ZQsHIy1imCYf3M0Rjyn63maxRPzXOoSbdb0pV2RdTnj7Rof YzW8N2KAAlNTuD1c1nGAmuYxCsbJqs6axnFHorK5lLkHfi5zBLXchiuwtenvRkheYQdH gMbIN698CqBP0QXld7BIUHZpwGOAoyLJ31V4ZymRj/5jqUsOmb48GDTD6I2bymNYFjdR Oz0Guq/tw5iJzqY9c6OoynSYX6hJMe/stVosGeorzEmrIisAzb/wb3oandP6PuMcVTgM w5oQ== X-Gm-Message-State: AOAM532spqdVlRaQaffxEHa+sEITU9diNh7+DK6Ngn8Z6odtQrBhuWrn N5d4xysYALMJywyHSzVKQBeeMQ== X-Google-Smtp-Source: ABdhPJyRYozlZl96pAlyrtGQZ0j5cFKkqb1QRVHAHa/ObAd1annJ6d+ETPAwRDlJQ5te7Eb7L37ccA== X-Received: by 2002:adf:8b48:: with SMTP id v8mr7121008wra.21.1599218655945; Fri, 04 Sep 2020 04:24:15 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:15 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 03/11] btf: make btf_set_contains take a const pointer Date: Fri, 4 Sep 2020 12:23:53 +0100 Message-Id: <20200904112401.667645-4-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org bsearch doesn't modify the contents of the array, so we can take a const pointer. Signed-off-by: Lorenz Bauer Acked-by: Andrii Nakryiko --- include/linux/bpf.h | 2 +- kernel/bpf/btf.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index c6d9f2c444f4..6b72cdf52ebc 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1900,6 +1900,6 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t, void *addr1, void *addr2); struct btf_id_set; -bool btf_id_set_contains(struct btf_id_set *set, u32 id); +bool btf_id_set_contains(const struct btf_id_set *set, u32 id); #endif /* _LINUX_BPF_H */ diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index f9ac6935ab3c..a2330f6fe2e6 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -4772,7 +4772,7 @@ static int btf_id_cmp_func(const void *a, const void *b) return *pa - *pb; } -bool btf_id_set_contains(struct btf_id_set *set, u32 id) +bool btf_id_set_contains(const struct btf_id_set *set, u32 id) { return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL; } From patchwork Fri Sep 4 11:23:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357364 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=BclZNy56; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb5z5fPSz9sV8 for ; Fri, 4 Sep 2020 21:28:39 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726171AbgIDL2i (ORCPT ); Fri, 4 Sep 2020 07:28:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730197AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C651C0619C2 for ; Fri, 4 Sep 2020 04:24:18 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id w5so6369042wrp.8 for ; Fri, 04 Sep 2020 04:24:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KaDl48Pga4QEbzcA0z5fJMx4Mo2Pt/XvOLNWfi7T27A=; b=BclZNy566ovAagP2odZPY0wMHOwiqUO5dP3BPTBIwcJ8axSeP+m5DlRyFSNG5n9Ncy iGoFgglP8Rg52SzdlR2wFqUC2WeGvq2N4NScgHB2brde3TM1yg77mNNktPguAz920y69 Q8/kg2homlZPRww6CQ6nUiQyOHCWfDUZ0OXW0= 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=KaDl48Pga4QEbzcA0z5fJMx4Mo2Pt/XvOLNWfi7T27A=; b=XuXp6hlMTI+ls4leqr7WCMY7J2bPItKTRkpiQExoTNTUSm0AUtvtwHBkRboBPhTZZp FDmpbxEplTGLXXnZAaaYpfjpwkIjngs5LAcCSCw38V3/e2T6q9ndJU2QypbXD2xN0OeK LF/3waPT6Ag6ljjsdZsmX0/r/N3l+Vn1r1KQRXlGXS6w2EXKJVJMUtGvq/NxhF/xD7X7 4qaOkoeHOVhwko58q8KUgw6h0KKF0noMleipuJsuI66cVX72biswbtuIaiiS2YKvNX7+ GxfzhUoGQzKYF26mBgBO78MXJSSB9BYQM4ZkEGeaykNYBlBVLrO7fwY3xaSDXPNci9WO jvtg== X-Gm-Message-State: AOAM530kCTSZy6el+nlXUpi7zDquXAevF8SXztYk3m5f7PXcySYo94+Z w6nns3DV3zjvscmR0q1MS0shIw== X-Google-Smtp-Source: ABdhPJxcHsE2SFbxXWtbozn4SX6A2LJf/kFtMUYuhgZQk8aqbDWNcGPPNK17VEzVQDxLsfOgn67Txg== X-Received: by 2002:a05:6000:1c4:: with SMTP id t4mr2815717wrx.350.1599218657081; Fri, 04 Sep 2020 04:24:17 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:16 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 04/11] bpf: check scalar or invalid register in check_helper_mem_access Date: Fri, 4 Sep 2020 12:23:54 +0100 Message-Id: <20200904112401.667645-5-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Move the check for a NULL or zero register to check_helper_mem_access. This makes check_stack_boundary easier to understand. Signed-off-by: Lorenz Bauer Acked-by: Andrii Nakryiko --- kernel/bpf/verifier.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 509754c3aa7d..649bcfb4535e 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3594,18 +3594,6 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno, struct bpf_func_state *state = func(env, reg); int err, min_off, max_off, i, j, slot, spi; - if (reg->type != PTR_TO_STACK) { - /* Allow zero-byte read from NULL, regardless of pointer type */ - if (zero_size_allowed && access_size == 0 && - register_is_null(reg)) - return 0; - - verbose(env, "R%d type=%s expected=%s\n", regno, - reg_type_str[reg->type], - reg_type_str[PTR_TO_STACK]); - return -EACCES; - } - if (tnum_is_const(reg->var_off)) { min_off = max_off = reg->var_off.value + reg->off; err = __check_stack_boundary(env, regno, min_off, access_size, @@ -3750,9 +3738,19 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, access_size, zero_size_allowed, "rdwr", &env->prog->aux->max_rdwr_access); - default: /* scalar_value|ptr_to_stack or invalid ptr */ + case PTR_TO_STACK: return check_stack_boundary(env, regno, access_size, zero_size_allowed, meta); + default: /* scalar_value or invalid ptr */ + /* Allow zero-byte read from NULL, regardless of pointer type */ + if (zero_size_allowed && access_size == 0 && + register_is_null(reg)) + return 0; + + verbose(env, "R%d type=%s expected=%s\n", regno, + reg_type_str[reg->type], + reg_type_str[PTR_TO_STACK]); + return -EACCES; } } From patchwork Fri Sep 4 11:23:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357372 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=m5pEEfuW; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb7y1b1wz9sVM for ; Fri, 4 Sep 2020 21:30:22 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728588AbgIDLaJ (ORCPT ); Fri, 4 Sep 2020 07:30:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730192AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C86AEC0619C3 for ; Fri, 4 Sep 2020 04:24:19 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id a17so6355604wrn.6 for ; Fri, 04 Sep 2020 04:24:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Kiip+FIZjsqE39hHxIETodeCupWy6+FqJRJO6QdJavQ=; b=m5pEEfuWK/i47r8GC4HjH69neelLfHx4D/S3p2S55G4gj3Jh0QRy9y0/1PQYV9eYyB aKRZmwYL2MNVO5pFqavcd1i+O56O+IobqftICmfqVxftEtNYXtuGxHXF2aNmGVxgGuC0 iPTAiqc8i0F7TR7g0Qo2/4U/s0uwX+EJ8yZLU= 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=Kiip+FIZjsqE39hHxIETodeCupWy6+FqJRJO6QdJavQ=; b=RKBk4PH4yfC+UMeDgi1YhhEvJnQSkuHejJIabSLg/hACdvomWYHwT8+dbut6O+2OuX uz8Oe6K7yMkufPMx9mn+KQDwoX3+sUaN+6wsuMrWEfvu9RSSd7ZJPIRnncB+uU6OCX5c 572RMnINhFLFG/bA74pNi6VoAcegwVhRqnkhCyb//dcOXZ+z78swo31UjYMuc/sSdsbg Qd4xf2/i78OYq75te/90fycloHzDT6T5YVhAYzoAt5fq+8C6/83KFpgHPg2clj1C90iL 14i+jw1I/rK6CwG2sjEn1FnLDukQTsJ1+f4cmNyJvPIQuikoFcILNpB7byMFrCktviCC Rp2g== X-Gm-Message-State: AOAM531znDEKOSH5+mayrQbzBdw0jYb0twSUPAaQGLHLVF7XBJSoe4Hx PjIuok9Xkz2R8lRm00elFwUph827elJj7w== X-Google-Smtp-Source: ABdhPJx2nrFAqKs27I/atwrEekRj1bPdRdNV2WyFT1kf0kphK6Nbi55B5xlNjNzhdmivpETB7TNYbg== X-Received: by 2002:adf:c981:: with SMTP id f1mr7137718wrh.14.1599218658373; Fri, 04 Sep 2020 04:24:18 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:17 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 05/11] bpf: allow specifying a set of BTF IDs for helper arguments Date: Fri, 4 Sep 2020 12:23:55 +0100 Message-Id: <20200904112401.667645-6-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Function prototypes using ARG_PTR_TO_BTF_ID currently use two ways to signal which BTF IDs are acceptable. First, bpf_func_proto.btf_id is an array of IDs, one for each argument. This array is only accessed up to the highest numbered argument that uses ARG_PTR_TO_BTF_ID and may therefore be less than five arguments long. It usually points at a BTF_ID_LIST. Second, check_btf_id is a function pointer that is called by the verifier if present. It gets the actual BTF ID of the register, and the argument number we're currently checking. It turns out that the only user check_arg_btf_id ignores the argument, and is simply used to check whether the BTF ID matches one of the socket types. Replace both of these mechanisms with explicit btf_id_sets for each argument in a function proto. The verifier can now check that a PTR_TO_BTF_ID is one of several IDs, and the code that does the type checking becomes simpler. Add a small optimisation to btf_set_contains for the common case of a set with a single entry. Signed-off-by: Lorenz Bauer --- include/linux/bpf.h | 22 ++++++++++--------- kernel/bpf/bpf_inode_storage.c | 8 +++---- kernel/bpf/btf.c | 22 ++++++------------- kernel/bpf/stackmap.c | 5 +++-- kernel/bpf/verifier.c | 39 +++++++++++++--------------------- kernel/trace/bpf_trace.c | 15 +++++++------ net/core/bpf_sk_storage.c | 10 +++++---- net/core/filter.c | 31 ++++++++++----------------- net/ipv4/bpf_tcp_ca.c | 24 +++++++++------------ 9 files changed, 76 insertions(+), 100 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 6b72cdf52ebc..36276e78dc75 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -31,6 +31,7 @@ struct sock; struct seq_file; struct btf; struct btf_type; +struct btf_id_set; struct exception_table_entry; struct seq_operations; struct bpf_iter_aux_info; @@ -326,12 +327,16 @@ struct bpf_func_proto { }; enum bpf_arg_type arg_type[5]; }; - int *btf_id; /* BTF ids of arguments */ - bool (*check_btf_id)(u32 btf_id, u32 arg); /* if the argument btf_id is - * valid. Often used if more - * than one btf id is permitted - * for this argument. - */ + union { + struct { + struct btf_id_set *arg1_btf_ids; + struct btf_id_set *arg2_btf_ids; + struct btf_id_set *arg3_btf_ids; + struct btf_id_set *arg4_btf_ids; + struct btf_id_set *arg5_btf_ids; + }; + struct btf_id_set *arg_btf_ids[5]; + }; int *ret_btf_id; /* return value btf_id */ bool (*allowed)(const struct bpf_prog *prog); }; @@ -1379,9 +1384,7 @@ int btf_struct_access(struct bpf_verifier_log *log, enum bpf_access_type atype, u32 *next_btf_id); bool btf_struct_ids_match(struct bpf_verifier_log *log, - int off, u32 id, u32 need_type_id); -int btf_resolve_helper_id(struct bpf_verifier_log *log, - const struct bpf_func_proto *fn, int); + int off, u32 id, const struct btf_id_set *needed); int btf_distill_func_proto(struct bpf_verifier_log *log, struct btf *btf, @@ -1899,7 +1902,6 @@ enum bpf_text_poke_type { int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t, void *addr1, void *addr2); -struct btf_id_set; bool btf_id_set_contains(const struct btf_id_set *set, u32 id); #endif /* _LINUX_BPF_H */ diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c index 75be02799c0f..d447d2655cce 100644 --- a/kernel/bpf/bpf_inode_storage.c +++ b/kernel/bpf/bpf_inode_storage.c @@ -249,9 +249,9 @@ const struct bpf_map_ops inode_storage_map_ops = { .map_owner_storage_ptr = inode_storage_ptr, }; -BTF_ID_LIST(bpf_inode_storage_btf_ids) -BTF_ID_UNUSED +BTF_SET_START(bpf_inode_storage_btf_ids) BTF_ID(struct, inode) +BTF_SET_END(bpf_inode_storage_btf_ids) const struct bpf_func_proto bpf_inode_storage_get_proto = { .func = bpf_inode_storage_get, @@ -259,9 +259,9 @@ const struct bpf_func_proto bpf_inode_storage_get_proto = { .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_BTF_ID, + .arg2_btf_ids = &bpf_inode_storage_btf_ids, .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL, .arg4_type = ARG_ANYTHING, - .btf_id = bpf_inode_storage_btf_ids, }; const struct bpf_func_proto bpf_inode_storage_delete_proto = { @@ -270,5 +270,5 @@ const struct bpf_func_proto bpf_inode_storage_delete_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_BTF_ID, - .btf_id = bpf_inode_storage_btf_ids, + .arg2_btf_ids = &bpf_inode_storage_btf_ids, }; diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index a2330f6fe2e6..dec7f03b9229 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -4163,13 +4163,13 @@ int btf_struct_access(struct bpf_verifier_log *log, } bool btf_struct_ids_match(struct bpf_verifier_log *log, - int off, u32 id, u32 need_type_id) + int off, u32 id, const struct btf_id_set *needed) { const struct btf_type *type; int err; /* Are we already done? */ - if (need_type_id == id && off == 0) + if (off == 0 && btf_id_set_contains(needed, id)) return true; again: @@ -4185,7 +4185,7 @@ bool btf_struct_ids_match(struct bpf_verifier_log *log, * continue the search with offset 0 in the new * type. */ - if (need_type_id != id) { + if (!btf_id_set_contains(needed, id)) { off = 0; goto again; } @@ -4193,19 +4193,6 @@ bool btf_struct_ids_match(struct bpf_verifier_log *log, return true; } -int btf_resolve_helper_id(struct bpf_verifier_log *log, - const struct bpf_func_proto *fn, int arg) -{ - int id; - - if (fn->arg_type[arg] != ARG_PTR_TO_BTF_ID || !btf_vmlinux) - return -EINVAL; - id = fn->btf_id[arg]; - if (!id || id > btf_vmlinux->nr_types) - return -EINVAL; - return id; -} - static int __get_type_size(struct btf *btf, u32 btf_id, const struct btf_type **bad_type) { @@ -4774,5 +4761,8 @@ static int btf_id_cmp_func(const void *a, const void *b) bool btf_id_set_contains(const struct btf_id_set *set, u32 id) { + if (set->cnt == 1) + return set->ids[0] == id; + return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL; } diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index a2fa006f430e..3142e8aab4ed 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -665,18 +665,19 @@ BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf, return __bpf_get_stack(regs, task, NULL, buf, size, flags); } -BTF_ID_LIST(bpf_get_task_stack_btf_ids) +BTF_SET_START(bpf_get_task_stack_btf_ids) BTF_ID(struct, task_struct) +BTF_SET_END(bpf_get_task_stack_btf_ids) const struct bpf_func_proto bpf_get_task_stack_proto = { .func = bpf_get_task_stack, .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_ids = &bpf_get_task_stack_btf_ids, .arg2_type = ARG_PTR_TO_UNINIT_MEM, .arg3_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, - .btf_id = bpf_get_task_stack_btf_ids, }; BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx, diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 649bcfb4535e..45759638e1b8 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -238,7 +238,6 @@ struct bpf_call_arg_meta { u64 msize_max_value; int ref_obj_id; int func_id; - u32 btf_id; }; struct btf *btf_vmlinux; @@ -3906,8 +3905,9 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env, return 0; } -BTF_ID_LIST(btf_fullsock_ids) +BTF_SET_START(btf_fullsock_ids) BTF_ID(struct, sock) +BTF_SET_END(btf_fullsock_ids) static int check_func_arg(struct bpf_verifier_env *env, u32 arg, struct bpf_call_arg_meta *meta, @@ -3917,6 +3917,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; enum bpf_reg_type expected_type, type = reg->type; enum bpf_arg_type arg_type = fn->arg_type[arg]; + const struct btf_id_set *btf_ids = NULL; int err = 0; if (arg_type == ARG_DONTCARE) @@ -4005,7 +4006,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, type != PTR_TO_BTF_ID) goto err_type; } - meta->btf_id = btf_fullsock_ids[0]; + btf_ids = &btf_fullsock_ids; } else if (arg_type == ARG_PTR_TO_BTF_ID) { expected_type = PTR_TO_BTF_ID; if (type != expected_type) @@ -4065,26 +4066,21 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, } if (type == PTR_TO_BTF_ID) { - bool ids_match = false; + if (fn->arg_btf_ids[arg]) + btf_ids = fn->arg_btf_ids[arg]; - if (!fn->check_btf_id) { - if (reg->btf_id != meta->btf_id) { - ids_match = btf_struct_ids_match(&env->log, reg->off, reg->btf_id, - meta->btf_id); - if (!ids_match) { - verbose(env, "Helper has type %s got %s in R%d\n", - kernel_type_name(meta->btf_id), - kernel_type_name(reg->btf_id), regno); - return -EACCES; - } - } - } else if (!fn->check_btf_id(reg->btf_id, arg)) { - verbose(env, "Helper does not support %s in R%d\n", - kernel_type_name(reg->btf_id), regno); + if (!btf_ids) { + verbose(env, "verifier internal error: missing BTF IDs\n"); + return -EFAULT; + } + if (!btf_struct_ids_match(&env->log, reg->off, reg->btf_id, + btf_ids)) { + verbose(env, "R%d has incompatible type %s\n", regno, + kernel_type_name(reg->btf_id)); return -EACCES; } - if ((reg->off && !ids_match) || !tnum_is_const(reg->var_off) || reg->var_off.value) { + if (!tnum_is_const(reg->var_off) || reg->var_off.value) { verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", regno); return -EACCES; @@ -4903,11 +4899,6 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn meta.func_id = func_id; /* check args */ for (i = 0; i < 5; i++) { - if (!fn->check_btf_id) { - err = btf_resolve_helper_id(&env->log, fn, i); - if (err > 0) - meta.btf_id = err; - } err = check_func_arg(env, i, &meta, fn); if (err) return err; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index b2a5380eb187..92b3e50ad516 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -743,19 +743,20 @@ BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size, return err; } -BTF_ID_LIST(bpf_seq_printf_btf_ids) +BTF_SET_START(bpf_seq_printf_btf_ids) BTF_ID(struct, seq_file) +BTF_SET_END(bpf_seq_printf_btf_ids) static const struct bpf_func_proto bpf_seq_printf_proto = { .func = bpf_seq_printf, .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_ids = &bpf_seq_printf_btf_ids, .arg2_type = ARG_PTR_TO_MEM, .arg3_type = ARG_CONST_SIZE, .arg4_type = ARG_PTR_TO_MEM_OR_NULL, .arg5_type = ARG_CONST_SIZE_OR_ZERO, - .btf_id = bpf_seq_printf_btf_ids, }; BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len) @@ -763,17 +764,18 @@ BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len) return seq_write(m, data, len) ? -EOVERFLOW : 0; } -BTF_ID_LIST(bpf_seq_write_btf_ids) +BTF_SET_START(bpf_seq_write_btf_ids) BTF_ID(struct, seq_file) +BTF_SET_END(bpf_seq_write_btf_ids) static const struct bpf_func_proto bpf_seq_write_proto = { .func = bpf_seq_write, .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_ids = &bpf_seq_write_btf_ids, .arg2_type = ARG_PTR_TO_MEM, .arg3_type = ARG_CONST_SIZE_OR_ZERO, - .btf_id = bpf_seq_write_btf_ids, }; static __always_inline int @@ -1130,17 +1132,18 @@ static bool bpf_d_path_allowed(const struct bpf_prog *prog) return btf_id_set_contains(&btf_allowlist_d_path, prog->aux->attach_btf_id); } -BTF_ID_LIST(bpf_d_path_btf_ids) +BTF_SET_START(bpf_d_path_btf_ids) BTF_ID(struct, path) +BTF_SET_END(bpf_d_path_btf_ids) static const struct bpf_func_proto bpf_d_path_proto = { .func = bpf_d_path, .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_ids = &bpf_d_path_btf_ids, .arg2_type = ARG_PTR_TO_MEM, .arg3_type = ARG_CONST_SIZE_OR_ZERO, - .btf_id = bpf_d_path_btf_ids, .allowed = bpf_d_path_allowed, }; diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index a0d1a3265b71..cfd3b81366f4 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -357,6 +357,7 @@ const struct bpf_func_proto bpf_sk_storage_get_proto = { .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_SOCKET, + .arg2_btf_ids = &btf_sock_ids_set, .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL, .arg4_type = ARG_ANYTHING, }; @@ -377,11 +378,12 @@ const struct bpf_func_proto bpf_sk_storage_delete_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_SOCKET, + .arg2_btf_ids = &btf_sock_ids_set, }; -BTF_ID_LIST(sk_storage_btf_ids) -BTF_ID_UNUSED +BTF_SET_START(sk_storage_btf_ids) BTF_ID(struct, sock) +BTF_SET_END(sk_storage_btf_ids) const struct bpf_func_proto sk_storage_get_btf_proto = { .func = bpf_sk_storage_get, @@ -389,9 +391,9 @@ const struct bpf_func_proto sk_storage_get_btf_proto = { .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_BTF_ID, + .arg2_btf_ids = &sk_storage_btf_ids, .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL, .arg4_type = ARG_ANYTHING, - .btf_id = sk_storage_btf_ids, }; const struct bpf_func_proto sk_storage_delete_btf_proto = { @@ -400,7 +402,7 @@ const struct bpf_func_proto sk_storage_delete_btf_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_BTF_ID, - .btf_id = sk_storage_btf_ids, + .arg2_btf_ids = &sk_storage_btf_ids, }; struct bpf_sk_storage_diag { diff --git a/net/core/filter.c b/net/core/filter.c index c7f96cfea1b0..0f25ce7485db 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -3803,19 +3803,20 @@ static const struct bpf_func_proto bpf_skb_event_output_proto = { .arg5_type = ARG_CONST_SIZE_OR_ZERO, }; -BTF_ID_LIST(bpf_skb_output_btf_ids) +BTF_SET_START(bpf_skb_output_btf_ids) BTF_ID(struct, sk_buff) +BTF_SET_END(bpf_skb_output_btf_ids) const struct bpf_func_proto bpf_skb_output_proto = { .func = bpf_skb_event_output, .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_ids = &bpf_skb_output_btf_ids, .arg2_type = ARG_CONST_MAP_PTR, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_MEM, .arg5_type = ARG_CONST_SIZE_OR_ZERO, - .btf_id = bpf_skb_output_btf_ids, }; static unsigned short bpf_tunnel_key_af(u64 flags) @@ -4199,19 +4200,20 @@ static const struct bpf_func_proto bpf_xdp_event_output_proto = { .arg5_type = ARG_CONST_SIZE_OR_ZERO, }; -BTF_ID_LIST(bpf_xdp_output_btf_ids) +BTF_SET_START(bpf_xdp_output_btf_ids) BTF_ID(struct, xdp_buff) +BTF_SET_END(bpf_xdp_output_btf_ids) const struct bpf_func_proto bpf_xdp_output_proto = { .func = bpf_xdp_event_output, .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_ids = &bpf_xdp_output_btf_ids, .arg2_type = ARG_CONST_MAP_PTR, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_MEM, .arg5_type = ARG_CONST_SIZE_OR_ZERO, - .btf_id = bpf_xdp_output_btf_ids, }; BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb) @@ -9914,17 +9916,6 @@ u32 btf_sock_ids[MAX_BTF_SOCK_TYPE]; struct btf_id_set btf_sock_ids_set; #endif -static bool check_arg_btf_id(u32 btf_id, u32 arg) -{ - int i; - - /* only one argument, no need to check arg */ - for (i = 0; i < MAX_BTF_SOCK_TYPE; i++) - if (btf_sock_ids[i] == btf_id) - return true; - return false; -} - BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk) { /* tcp6_sock type is not generated in dwarf and hence btf, @@ -9943,7 +9934,7 @@ const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = { .gpl_only = false, .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, .arg1_type = ARG_PTR_TO_BTF_ID, - .check_btf_id = check_arg_btf_id, + .arg1_btf_ids = &btf_sock_ids_set, .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6], }; @@ -9960,7 +9951,7 @@ const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = { .gpl_only = false, .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, .arg1_type = ARG_PTR_TO_BTF_ID, - .check_btf_id = check_arg_btf_id, + .arg1_btf_ids = &btf_sock_ids_set, .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP], }; @@ -9984,7 +9975,7 @@ const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = { .gpl_only = false, .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, .arg1_type = ARG_PTR_TO_BTF_ID, - .check_btf_id = check_arg_btf_id, + .arg1_btf_ids = &btf_sock_ids_set, .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW], }; @@ -10008,7 +9999,7 @@ const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = { .gpl_only = false, .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, .arg1_type = ARG_PTR_TO_BTF_ID, - .check_btf_id = check_arg_btf_id, + .arg1_btf_ids = &btf_sock_ids_set, .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ], }; @@ -10030,6 +10021,6 @@ const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = { .gpl_only = false, .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, .arg1_type = ARG_PTR_TO_BTF_ID, - .check_btf_id = check_arg_btf_id, + .arg1_btf_ids = &btf_sock_ids_set, .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6], }; diff --git a/net/ipv4/bpf_tcp_ca.c b/net/ipv4/bpf_tcp_ca.c index e3939f76b024..37a890440f46 100644 --- a/net/ipv4/bpf_tcp_ca.c +++ b/net/ipv4/bpf_tcp_ca.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -28,23 +29,22 @@ static u32 unsupported_ops[] = { static const struct btf_type *tcp_sock_type; static u32 tcp_sock_id, sock_id; -static int btf_sk_storage_get_ids[5]; +BTF_SET_START(btf_tcp_sock_ids) +BTF_ID(struct, tcp_sock) +BTF_SET_END(btf_tcp_sock_ids) + static struct bpf_func_proto btf_sk_storage_get_proto __read_mostly; - -static int btf_sk_storage_delete_ids[5]; static struct bpf_func_proto btf_sk_storage_delete_proto __read_mostly; -static void convert_sk_func_proto(struct bpf_func_proto *to, int *to_btf_ids, - const struct bpf_func_proto *from) +static void convert_sk_func_proto(struct bpf_func_proto *to, const struct bpf_func_proto *from) { int i; *to = *from; - to->btf_id = to_btf_ids; for (i = 0; i < ARRAY_SIZE(to->arg_type); i++) { if (to->arg_type[i] == ARG_PTR_TO_SOCKET) { to->arg_type[i] = ARG_PTR_TO_BTF_ID; - to->btf_id[i] = tcp_sock_id; + to->arg_btf_ids[i] = &btf_tcp_sock_ids; } } } @@ -64,12 +64,8 @@ static int bpf_tcp_ca_init(struct btf *btf) tcp_sock_id = type_id; tcp_sock_type = btf_type_by_id(btf, tcp_sock_id); - convert_sk_func_proto(&btf_sk_storage_get_proto, - btf_sk_storage_get_ids, - &bpf_sk_storage_get_proto); - convert_sk_func_proto(&btf_sk_storage_delete_proto, - btf_sk_storage_delete_ids, - &bpf_sk_storage_delete_proto); + convert_sk_func_proto(&btf_sk_storage_get_proto, &bpf_sk_storage_get_proto); + convert_sk_func_proto(&btf_sk_storage_delete_proto, &bpf_sk_storage_delete_proto); return 0; } @@ -185,8 +181,8 @@ static const struct bpf_func_proto bpf_tcp_send_ack_proto = { /* In case we want to report error later */ .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_ids = &btf_tcp_sock_ids, .arg2_type = ARG_ANYTHING, - .btf_id = &tcp_sock_id, }; static const struct bpf_func_proto * From patchwork Fri Sep 4 11:23:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357367 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=mzr0PB3a; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb6t5dPVz9sV8 for ; Fri, 4 Sep 2020 21:29:26 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730232AbgIDL3T (ORCPT ); Fri, 4 Sep 2020 07:29:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728263AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05F64C0619C4 for ; Fri, 4 Sep 2020 04:24:21 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id k15so6342663wrn.10 for ; Fri, 04 Sep 2020 04:24:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QTxDMqIedGHd6nEHPaqkTSyOMImcvTE/s8Ne5HgNwhY=; b=mzr0PB3aDKq5/Lxp4ONFu0pyaVkB36cYtaOb5BsF08YNWZmxJHN74LjX9ZwrqlpYrW ecugmyEAooZbNNjTjiXHnZof4QcEc0Cf4aerzP4OeA02EB08cRcOrCE+5XdDP9Xwjvyl 8UkvynE89RuKwLnaoO3mqkCl6aPgmBWMOsT0A= 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=QTxDMqIedGHd6nEHPaqkTSyOMImcvTE/s8Ne5HgNwhY=; b=IxVSi6NnysfugmCHFyGeMTv582jbzdaMqVr8UUL8CKZcIsgc7Ia2Q41oDwGti5URog sZI1+2mFHlp0GCQrWezY1LeezkKN5iw3OnUHOejGx+6Q5qyeeWtXR0CxUCgvInbPCviR Y+8QUf6P/9IJ+QYlq8s+IuVZx+yckjCwW36zTUUPDqcNaWVCBGcrAZvSLTet8ISQVDGN ddRDWCsgLQ63gUSCKKL8s0Xsbbh/H5uVrpZfM0iRtb7K3BhoXhBhF2h8Mg6x8m4bmtzy opzoXXCjQyEUoDlEHypBNOLVGwKVeG9P9cF9c1k8iXpYN8/bxDXD3a5QGYDyxlNklRpj H4rw== X-Gm-Message-State: AOAM531gFXkK1fTzRCoZ6o9KPmHqT11DD5qiHxmkvBYGIxyr+Z/nSfRq f0HvJLY5YMTm/55A8gkyvAodqQ== X-Google-Smtp-Source: ABdhPJwr9RvTnKQ8LB1QlqOFU/xwIYprm8BN2DMQ1bbX4TWGOaeAlGKuleWkvMPFuuC+KzSAizE+7Q== X-Received: by 2002:adf:df0f:: with SMTP id y15mr7087825wrl.127.1599218659714; Fri, 04 Sep 2020 04:24:19 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:19 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 06/11] bpf: make reference tracking in check_func_arg generic Date: Fri, 4 Sep 2020 12:23:56 +0100 Message-Id: <20200904112401.667645-7-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Instead of dealing with reg->ref_obj_id individually for every arg type that needs it, rely on the fact that ref_obj_id is zero if the register is not reference tracked. Signed-off-by: Lorenz Bauer Acked-by: Andrii Nakryiko --- kernel/bpf/verifier.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 45759638e1b8..c7df4ccad8e2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3988,15 +3988,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ if (!type_is_sk_pointer(type)) goto err_type; - if (reg->ref_obj_id) { - if (meta->ref_obj_id) { - verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", - regno, reg->ref_obj_id, - meta->ref_obj_id); - return -EFAULT; - } - meta->ref_obj_id = reg->ref_obj_id; - } } else if (arg_type == ARG_PTR_TO_SOCKET || arg_type == ARG_PTR_TO_SOCKET_OR_NULL) { expected_type = PTR_TO_SOCKET; @@ -4047,13 +4038,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, /* final test in check_stack_boundary() */; else if (type != expected_type) goto err_type; - if (meta->ref_obj_id) { - verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", - regno, reg->ref_obj_id, - meta->ref_obj_id); - return -EFAULT; - } - meta->ref_obj_id = reg->ref_obj_id; } else if (arg_type_is_int_ptr(arg_type)) { expected_type = PTR_TO_STACK; if (!type_is_pkt_pointer(type) && @@ -4087,6 +4071,16 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, } } + if (reg->ref_obj_id) { + if (meta->ref_obj_id) { + verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", + regno, reg->ref_obj_id, + meta->ref_obj_id); + return -EFAULT; + } + meta->ref_obj_id = reg->ref_obj_id; + } + if (arg_type == ARG_CONST_MAP_PTR) { /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ meta->map_ptr = reg->map_ptr; From patchwork Fri Sep 4 11:23:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357369 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=KnR2n4I0; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb791KySz9sVM for ; Fri, 4 Sep 2020 21:29:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730044AbgIDL3j (ORCPT ); Fri, 4 Sep 2020 07:29:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730202AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49054C0619C5 for ; Fri, 4 Sep 2020 04:24:22 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id s13so5718509wmh.4 for ; Fri, 04 Sep 2020 04:24:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=55VGPuW8E1FFOJXYmazHOmmDWGJoPY4/0mNdm6zV/ZQ=; b=KnR2n4I0ZBE+/DEJ0TXHS9GIUnsm5gt1Lwpi1BBrWqg9RRQFefOGWueZJScxPHyhpe M/EZuVKnY785pXOfSlHppv3hcy0BxqOi9zaiM/vjHsJ6oLgEhLxfo/08l5P5THlrZfYU V4KKV0GlYkQxYGcKUfB0C3UD9A2S+jlYBoOUg= 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=55VGPuW8E1FFOJXYmazHOmmDWGJoPY4/0mNdm6zV/ZQ=; b=mQi5Sx2JCggPNMkoAfDOsh1wvzeNyN+HDggyUXgp0pGFpFTusailszK40bfgH1/iwh dJZLL6mmiGYGzDzUCUYyNbhv6bgyXvriqgKOkZ4ypn7E5WGHKS6Ys1jxufsYawRRti67 Iy26cnm3Edp38V3/33qmE7SIdOhpM3yKUaI8/v2YCxLXO26uPmIbuvPcEZ3WgAvCKl3L ZX7fH1jHoltHb5ul89FDEIaXzzW5ZpzbYwlnnzbl0p548hiBeaYoTDAUz+xXvr0w5AMh P+FJy5CYU10AZMgMSNVE4/ycmqAxAwAPLnvNPS0banWmAhDxjYTxLrvk9lX1fKQIKUwn Zi2Q== X-Gm-Message-State: AOAM5300Plykx0jPo8IrLulADi1L1cW8/5GpWHfb4qZEYg+zpB1L8jns 0t17QKhAAZ+CgU+mjSNdnhiTGudHR1LpFg== X-Google-Smtp-Source: ABdhPJzUS5ogQZmPtTU0+pOrqs/97OQzVwfd6J2wgxTDd9aLgq4Rf9gi12qss+/erQ76heEc3EmRXg== X-Received: by 2002:a7b:c775:: with SMTP id x21mr6889037wmk.47.1599218660995; Fri, 04 Sep 2020 04:24:20 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:20 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 07/11] bpf: always check access to PTR_TO_CTX regardless of arg_type Date: Fri, 4 Sep 2020 12:23:57 +0100 Message-Id: <20200904112401.667645-8-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Always check context access if the register we're operating on is PTR_TO_CTX, rather than relying on ARG_PTR_TO_CTX. Signed-off-by: Lorenz Bauer --- kernel/bpf/verifier.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index c7df4ccad8e2..ba710a702cae 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3979,9 +3979,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, arg_type == ARG_PTR_TO_CTX_OR_NULL)) { if (type != expected_type) goto err_type; - err = check_ctx_reg(env, reg, regno); - if (err < 0) - return err; } } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { expected_type = PTR_TO_SOCK_COMMON; @@ -4081,6 +4078,12 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, meta->ref_obj_id = reg->ref_obj_id; } + if (type == PTR_TO_CTX) { + err = check_ctx_reg(env, reg, regno); + if (err < 0) + return err; + } + if (arg_type == ARG_CONST_MAP_PTR) { /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ meta->map_ptr = reg->map_ptr; From patchwork Fri Sep 4 11:23:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357365 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=mkTfdlgk; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb614rZbz9sTg for ; Fri, 4 Sep 2020 21:28:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730160AbgIDL2j (ORCPT ); Fri, 4 Sep 2020 07:28:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730203AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91CC9C0619C6 for ; Fri, 4 Sep 2020 04:24:23 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id e16so6393704wrm.2 for ; Fri, 04 Sep 2020 04:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mAW7ccSoKpXBNDJ+uJzqj1Q1GSSeJCNiacCC/Ro7bB0=; b=mkTfdlgko5wg5uZiJ0lV2YS/JwjYLs2HjjrxPsgFRNjpZZ/IAOWQzahNX4kyE1A1re NlQVx15axULO6RwdBs4L/arzhtgqDLc1WYoIOI7xr4JhytGjcKJkvN/QkkeJMvB499Fp rz1GyiSZF/Ws2V3Wx5azQAU0Jzz1Ywx89V9Ho= 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=mAW7ccSoKpXBNDJ+uJzqj1Q1GSSeJCNiacCC/Ro7bB0=; b=SE/vNRoLmjeU5ij5KMbAhEF4q7X5l4xGb7sbV4S2Z53d/AcdrPEcCxthuumdxUc3UW DMJltxE8SR87UgnhRRwHhqH8y7rNLcdNjXWmiUMDm5EQ0aU3C2MhKyyCzDoC3BFQsOCc laM7dtFdB0XGzh2+mw/jJZ8rsaMPwlOfB95umNFvYGBmQyQtQspFhNdqOtqKsDFhpp25 m1hMJWeRmXqzyRNYFAyhWuWBX5lMdDxL4R11IPD8GoLZjZbdsKpVRHwame70KA1jGsZ6 1XbKZfj66BDHW83bEU/rFdvT8DboJdxyh6gBN5Qe70SERaF9Mhm1A5XYlig+/ABDe36P ifkQ== X-Gm-Message-State: AOAM531Hbeqg7GeNBZ0Jmnm7FL/TCiCHIuSO39OQ9kYTvgQvbPz4R6P6 xqGU8YF4k5ft27XTgABSHX/okY3nyvbSvQ== X-Google-Smtp-Source: ABdhPJwPXY1RC25RKYQh/t2x402qG4EJFgZn4IFEk6nZKs36cSd80P7gxAUap0WbjXNKdBEnfiIihQ== X-Received: by 2002:adf:eece:: with SMTP id a14mr6880096wrp.330.1599218662203; Fri, 04 Sep 2020 04:24:22 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:21 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 08/11] bpf: set meta->raw_mode for pointers to memory closer to it's use Date: Fri, 4 Sep 2020 12:23:58 +0100 Message-Id: <20200904112401.667645-9-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org If we encounter a pointer to memory, we set meta->raw_mode depending on the type of memory we point at. What isn't obvious is that this information is only used when the next memory size argument is encountered. Move the assignment closer to where it's used, and add a comment that explains what is going on. Signed-off-by: Lorenz Bauer --- kernel/bpf/verifier.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index ba710a702cae..734ae5af9db9 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -4027,7 +4027,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, type != PTR_TO_RDWR_BUF && type != expected_type) goto err_type; - meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; } else if (arg_type_is_alloc_mem_ptr(arg_type)) { expected_type = PTR_TO_MEM; if (register_is_null(reg) && @@ -4120,6 +4119,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err = check_helper_mem_access(env, regno, meta->map_ptr->value_size, false, meta); + } else if (arg_type_is_mem_ptr(arg_type)) { + /* The access to this pointer is only checked when we hit the + * next is_mem_size argument below. + */ + meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); } else if (arg_type_is_mem_size(arg_type)) { bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); From patchwork Fri Sep 4 11:23:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357368 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=TsHonHfI; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb785CB5z9sTg for ; Fri, 4 Sep 2020 21:29:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730014AbgIDL3a (ORCPT ); Fri, 4 Sep 2020 07:29:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730044AbgIDL0C (ORCPT ); Fri, 4 Sep 2020 07:26:02 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBA33C0619C7 for ; Fri, 4 Sep 2020 04:24:24 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id c18so6345954wrm.9 for ; Fri, 04 Sep 2020 04:24:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UlbYcEjSkKSPkXTMNH7qDHYVFjzIZuiUHBvMI19uz40=; b=TsHonHfIIUWg4ZnsLK/guRIHs/stKJa/IrJaigwRs4I8Rt7sMV5fA7dfRX64Bo6n1o 3iWYyZA4fsxtM+U3f3ZB0zZpSmDE6hFEVDEZ05URZrFn1XYZ3BmmqVNd31nVzl7wjbU2 8y/cO69T6GbAko0X0J2qyzeJJO2U0BGEpelh4= 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=UlbYcEjSkKSPkXTMNH7qDHYVFjzIZuiUHBvMI19uz40=; b=uSIJeGW0LgYHOcBG25vYwNMRkqTbL9eH/3HmhQ0KGHqzTSxdNJToayXlH4BJ4ozyYf 9fpBmcwHJyWV3ItTLWyOlZ2JXAOIJtqPeqe5yA5IhMEt9ViAoDRftM0Lb/RmB5LwyLgd K2ZV1HrVJmmTxdJ+JuikgChVJqnZFICS+FIN7UBcuJa0dGEu2gSa9pGUf3n8DUVWMXKt YNgdReRWBofzAdmUME/nheczTVpwt2IeFgAg37B6NcNVbr/8+cZwc/FlzCobqDWZAvJb j5UopAvQK2FPp540SttwWLrkAocQTpGCXaJaIgYQM7PxDmxg5nPKUbT2KYfKePnevqF1 VKUg== X-Gm-Message-State: AOAM530WIpbx0RD9poGD8yhfmWQqjRWhLuErV2bDpYWCFcMSpe71xZLW u9s+hWhm3+9pE99IMfsokA3lSQ== X-Google-Smtp-Source: ABdhPJytAzXJV7Z9lXp5Nu8vhdF/3ToSkvO98Kb+GYlZgkkDmeay/vejOCxAxh4GreyaacwVVZvMmQ== X-Received: by 2002:adf:ba83:: with SMTP id p3mr7405832wrg.246.1599218663454; Fri, 04 Sep 2020 04:24:23 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:22 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 09/11] bpf: check ARG_PTR_TO_SPINLOCK register type in check_func_arg Date: Fri, 4 Sep 2020 12:23:59 +0100 Message-Id: <20200904112401.667645-10-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Move the check for PTR_TO_MAP_VALUE to check_func_arg, where all other checking is done as well. Move the invocation of process_spin_lock away from the register type checking, to allow a future refactoring. Signed-off-by: Lorenz Bauer --- kernel/bpf/verifier.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 734ae5af9db9..8d060da0b068 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3781,10 +3781,6 @@ static int process_spin_lock(struct bpf_verifier_env *env, int regno, struct bpf_map *map = reg->map_ptr; u64 val = reg->var_off.value; - if (reg->type != PTR_TO_MAP_VALUE) { - verbose(env, "R%d is not a pointer to map_value\n", regno); - return -EINVAL; - } if (!is_const) { verbose(env, "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", @@ -4000,16 +3996,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, if (type != expected_type) goto err_type; } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { - if (meta->func_id == BPF_FUNC_spin_lock) { - if (process_spin_lock(env, regno, true)) - return -EACCES; - } else if (meta->func_id == BPF_FUNC_spin_unlock) { - if (process_spin_lock(env, regno, false)) - return -EACCES; - } else { - verbose(env, "verifier internal error\n"); - return -EFAULT; - } + expected_type = PTR_TO_MAP_VALUE; + if (type != expected_type) + goto err_type; } else if (arg_type_is_mem_ptr(arg_type)) { expected_type = PTR_TO_STACK; /* One exception here. In case function allows for NULL to be @@ -4119,6 +4108,17 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err = check_helper_mem_access(env, regno, meta->map_ptr->value_size, false, meta); + } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { + if (meta->func_id == BPF_FUNC_spin_lock) { + if (process_spin_lock(env, regno, true)) + return -EACCES; + } else if (meta->func_id == BPF_FUNC_spin_unlock) { + if (process_spin_lock(env, regno, false)) + return -EACCES; + } else { + verbose(env, "verifier internal error\n"); + return -EFAULT; + } } else if (arg_type_is_mem_ptr(arg_type)) { /* The access to this pointer is only checked when we hit the * next is_mem_size argument below. From patchwork Fri Sep 4 11:24:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357360 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=kLN2gbh2; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb4Y5WZFz9sV8 for ; Fri, 4 Sep 2020 21:27:25 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730178AbgIDL1U (ORCPT ); Fri, 4 Sep 2020 07:27:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730172AbgIDL0U (ORCPT ); Fri, 4 Sep 2020 07:26:20 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86822C0619C8 for ; Fri, 4 Sep 2020 04:24:26 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id z4so6381785wrr.4 for ; Fri, 04 Sep 2020 04:24:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PawVYjT5/eWIPnpFICuKYzn2R6ZAhUHfnyNB/zWr9kI=; b=kLN2gbh2C6rZRiVPngrgnJYmrvXKhdbY0HrcGEmok7EFh7EtUOYg+cHLD0COdgFaFE x3k5W3SKbk1aU4ZENvRT/4wAsZl4I9MD6jJdf1TCfWDy8K+cCHYbqok6sa+BSCancQVY 0GMkeoeaF8Wnq1QJmUbuJwS4mJ3yG9m4tTWxk= 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=PawVYjT5/eWIPnpFICuKYzn2R6ZAhUHfnyNB/zWr9kI=; b=Gm13V4FddOAf8VAfkTdKfDHIQZWe76iv972Rkt826AD4LY1PnQP4CtmyP/+2ZVGNp2 hdd7lIek08bZwORw9MNT1FWvg7ERGTbsnvp9QdwMKqMVvFAsiZkEFjuJOMga9BLGp1oV xQz9breLzeRUH/PsBZRXEEgn/KiUc1WXWCbi41fIKiy97hX2sUn918GACxqsclUwKsP1 F7OHe0qDpfe52luTi2RqHYeHE07lzDTNGOK54aG4B5cU3v5fGYzgd+iJ1jSURW7stcbv ONXYkLZj5KD4cIve0+gnDYE561b2hQQA6R5aktH2LHwo44jYHsVcIQ64cZCrs2mj/y9g rhaA== X-Gm-Message-State: AOAM530AOEniWNTPqAphUrPr0BPL9rcWrSG8MQaBmBKw4QsjRU7PL+gt skPUud38CUECcJa8kPPfSCr0B6M5nAdNHw== X-Google-Smtp-Source: ABdhPJyZGPO53IDXjhfYt3swBWDuoMxXBQdAbaL8s7gG08i8PQIjOJ4XlO4itdmJLC8KAuiZMFy/VA== X-Received: by 2002:adf:9795:: with SMTP id s21mr7081627wrb.418.1599218665200; Fri, 04 Sep 2020 04:24:25 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:24 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 10/11] bpf: hoist type checking for nullable arg types Date: Fri, 4 Sep 2020 12:24:00 +0100 Message-Id: <20200904112401.667645-11-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org check_func_arg has a plethora of weird if statements with empty branches. They work around the fact that *_OR_NULL argument types should accept a SCALAR_VALUE register, as long as it's value is 0. These statements make it difficult to reason about the type checking logic. Instead, skip more detailed type checking logic iff the register is 0, and the function expects a nullable type. This allows simplifying the type checking itself. Signed-off-by: Lorenz Bauer Acked-by: Andrii Nakryiko --- kernel/bpf/verifier.c | 66 ++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 8d060da0b068..f124551c316a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -435,6 +435,15 @@ static bool arg_type_may_be_refcounted(enum bpf_arg_type type) return type == ARG_PTR_TO_SOCK_COMMON; } +static bool arg_type_may_be_null(enum bpf_arg_type type) +{ + return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || + type == ARG_PTR_TO_MEM_OR_NULL || + type == ARG_PTR_TO_CTX_OR_NULL || + type == ARG_PTR_TO_SOCKET_OR_NULL || + type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; +} + /* Determine whether the function releases some resources allocated by another * function call. The first reference type argument will be assumed to be * released by release_reference(). @@ -3946,17 +3955,20 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, return err; } + if (register_is_null(reg) && arg_type_may_be_null(arg_type)) + /* A NULL register has a SCALAR_VALUE type, so skip + * type checking. + */ + goto skip_type_check; + if (arg_type == ARG_PTR_TO_MAP_KEY || arg_type == ARG_PTR_TO_MAP_VALUE || arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { expected_type = PTR_TO_STACK; - if (register_is_null(reg) && - arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) - /* final test in check_stack_boundary() */; - else if (!type_is_pkt_pointer(type) && - type != PTR_TO_MAP_VALUE && - type != expected_type) + if (!type_is_pkt_pointer(type) && + type != PTR_TO_MAP_VALUE && + type != expected_type) goto err_type; } else if (arg_type == ARG_CONST_SIZE || arg_type == ARG_CONST_SIZE_OR_ZERO || @@ -3971,11 +3983,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, } else if (arg_type == ARG_PTR_TO_CTX || arg_type == ARG_PTR_TO_CTX_OR_NULL) { expected_type = PTR_TO_CTX; - if (!(register_is_null(reg) && - arg_type == ARG_PTR_TO_CTX_OR_NULL)) { - if (type != expected_type) - goto err_type; - } + if (type != expected_type) + goto err_type; } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { expected_type = PTR_TO_SOCK_COMMON; /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ @@ -3984,12 +3993,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, } else if (arg_type == ARG_PTR_TO_SOCKET || arg_type == ARG_PTR_TO_SOCKET_OR_NULL) { expected_type = PTR_TO_SOCKET; - if (!(register_is_null(reg) && - arg_type == ARG_PTR_TO_SOCKET_OR_NULL)) { - if (type != expected_type && - type != PTR_TO_BTF_ID) - goto err_type; - } + if (type != expected_type && + type != PTR_TO_BTF_ID) + goto err_type; btf_ids = &btf_fullsock_ids; } else if (arg_type == ARG_PTR_TO_BTF_ID) { expected_type = PTR_TO_BTF_ID; @@ -4001,27 +4007,16 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, goto err_type; } else if (arg_type_is_mem_ptr(arg_type)) { expected_type = PTR_TO_STACK; - /* One exception here. In case function allows for NULL to be - * passed in as argument, it's a SCALAR_VALUE type. Final test - * happens during stack boundary checking. - */ - if (register_is_null(reg) && - (arg_type == ARG_PTR_TO_MEM_OR_NULL || - arg_type == ARG_PTR_TO_ALLOC_MEM_OR_NULL)) - /* final test in check_stack_boundary() */; - else if (!type_is_pkt_pointer(type) && - type != PTR_TO_MAP_VALUE && - type != PTR_TO_MEM && - type != PTR_TO_RDONLY_BUF && - type != PTR_TO_RDWR_BUF && - type != expected_type) + if (!type_is_pkt_pointer(type) && + type != PTR_TO_MAP_VALUE && + type != PTR_TO_MEM && + type != PTR_TO_RDONLY_BUF && + type != PTR_TO_RDWR_BUF && + type != expected_type) goto err_type; } else if (arg_type_is_alloc_mem_ptr(arg_type)) { expected_type = PTR_TO_MEM; - if (register_is_null(reg) && - arg_type == ARG_PTR_TO_ALLOC_MEM_OR_NULL) - /* final test in check_stack_boundary() */; - else if (type != expected_type) + if (type != expected_type) goto err_type; } else if (arg_type_is_int_ptr(arg_type)) { expected_type = PTR_TO_STACK; @@ -4056,6 +4051,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, } } +skip_type_check: if (reg->ref_obj_id) { if (meta->ref_obj_id) { verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", From patchwork Fri Sep 4 11:24:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 1357362 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google header.b=n3/x74Ae; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bjb4Z4Z2Zz9sVR for ; Fri, 4 Sep 2020 21:27:26 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730208AbgIDL1T (ORCPT ); Fri, 4 Sep 2020 07:27:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730178AbgIDL01 (ORCPT ); Fri, 4 Sep 2020 07:26:27 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C7E6C0619C9 for ; Fri, 4 Sep 2020 04:24:27 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id c15so6334495wrs.11 for ; Fri, 04 Sep 2020 04:24:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JqaHMp9F5knJnlUKGdFXE/JSaVFbob0Mhs7o6M9E2z8=; b=n3/x74AeScH7iu2PzPIE5BhVRoR2Y3qQKcFwWnMVroYUf/KmqEVd7JvvXcgY/ZrOQx EVfrdEFpnw82tps9UcFEcGS8Zu1BEzBaplt2xWJyl0gEEMSce06MvSJz/6iSkEYkuGk+ YwlXBmGcmxpMkweKUNvyOrcybYz0GXmX97Nfc= 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=JqaHMp9F5knJnlUKGdFXE/JSaVFbob0Mhs7o6M9E2z8=; b=Xl6cayoaTbDyA5lKvJSirJjMec0eBLFJwR56UlXl7u/bPFQuPIiVaIZ1m7GpiRux2C g8vjXd2DOLzJK4sCVEPUiDXm7vyguu2B9vBnTgxcLWjVAzjL09dGhgL8QNqeWLR/uTou 0jS6LFA0xYlh/KATZGuDgTEbEyB5buOxIqjyxSaxnKlq1ahBXbHZ7W6KSwr1klhxJTwe jDAZlT4JBHZVUPvD1xdNcGlhiaHUtrLSVISgLYJyhAidjpM+6Q2hX7DF71miRIbHPyo/ n/ykPl6xMrAWmsuAoKP93EnmnVBaR7GTlJIaH4uU5+jVgreqdnceldJ6eNNTvgI1VxPy 4CEw== X-Gm-Message-State: AOAM533ms1hD4jEMr/EUwgIOjNHCa9mDBIMTxOt72rmJjpalYbYkl3bY d0rVYQ22Wron0z0tflMdpuLEfQ== X-Google-Smtp-Source: ABdhPJxRMGTH6v3cOAD5kiSeuckVp2qhR+FDRWGGODbinSe4AAQmJPYVfOUNaqpLjLtJQp2QFhSqYA== X-Received: by 2002:adf:8b48:: with SMTP id v8mr7121617wra.21.1599218666217; Fri, 04 Sep 2020 04:24:26 -0700 (PDT) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id v2sm9104408wrm.16.2020.09.04.04.24.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 04:24:25 -0700 (PDT) From: Lorenz Bauer To: ast@kernel.org, yhs@fb.com, daniel@iogearbox.net, kafai@fb.com Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [PATCH bpf-next 11/11] bpf: use a table to drive helper arg type checks Date: Fri, 4 Sep 2020 12:24:01 +0100 Message-Id: <20200904112401.667645-12-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200904112401.667645-1-lmb@cloudflare.com> References: <20200904112401.667645-1-lmb@cloudflare.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The mapping between bpf_arg_type and bpf_reg_type is encoded in a big hairy if statement that is hard to follow. The debug output also leaves to be desired: if a reg_type doesn't match we only print one of the options, instead printing all the valid ones. Convert the if statement into a table which is then used to drive type checking. If none of the reg_types match we print all options, e.g.: R2 type=rdonly_buf expected=[fp, pkt, pkt_meta, map_value] Signed-off-by: Lorenz Bauer --- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 193 +++++++++++++++++++++++++----------------- 2 files changed, 117 insertions(+), 77 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 36276e78dc75..ff52f22cedf7 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -293,6 +293,7 @@ enum bpf_arg_type { ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */ ARG_PTR_TO_ALLOC_MEM_OR_NULL, /* pointer to dynamically allocated memory or NULL */ ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */ + __ARG_TYPE_MAX, }; /* type of values returned from helper functions */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f124551c316a..5c61a378c805 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3856,12 +3856,6 @@ static bool arg_type_is_mem_size(enum bpf_arg_type type) type == ARG_CONST_SIZE_OR_ZERO; } -static bool arg_type_is_alloc_mem_ptr(enum bpf_arg_type type) -{ - return type == ARG_PTR_TO_ALLOC_MEM || - type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; -} - static bool arg_type_is_alloc_size(enum bpf_arg_type type) { return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; @@ -3914,15 +3908,120 @@ BTF_SET_START(btf_fullsock_ids) BTF_ID(struct, sock) BTF_SET_END(btf_fullsock_ids) +struct bpf_reg_types { + const enum bpf_reg_type types[10]; + const struct btf_id_set *btf_ids; +}; + +static const struct bpf_reg_types map_key_value_types = { + .types = { + PTR_TO_STACK, + PTR_TO_PACKET, + PTR_TO_PACKET_META, + PTR_TO_MAP_VALUE, + }, +}; + +static const struct bpf_reg_types sock_types = { + .types = { + PTR_TO_SOCK_COMMON, + PTR_TO_SOCKET, + PTR_TO_TCP_SOCK, + PTR_TO_XDP_SOCK, + }, +}; + +static const struct bpf_reg_types mem_types = { + .types = { + PTR_TO_STACK, + PTR_TO_PACKET, + PTR_TO_PACKET_META, + PTR_TO_MAP_VALUE, + PTR_TO_MEM, + PTR_TO_RDONLY_BUF, + PTR_TO_RDWR_BUF, + }, +}; + +static const struct bpf_reg_types int_ptr_types = { + .types = { + PTR_TO_STACK, + PTR_TO_PACKET, + PTR_TO_PACKET_META, + PTR_TO_MAP_VALUE, + }, +}; + +static const struct bpf_reg_types fullsock_types = { + .types = { PTR_TO_SOCKET, PTR_TO_BTF_ID }, + .btf_ids = &btf_fullsock_ids, +}; + +static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; +static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; +static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; +static const struct bpf_reg_types const_map_ptr_types = { .types = {CONST_PTR_TO_MAP } }; +static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; +static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; + +static const struct bpf_reg_types *compatible_reg_types[] = { + [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, + [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, + [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, + [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, + [ARG_CONST_SIZE] = &scalar_types, + [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, + [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, + [ARG_CONST_MAP_PTR] = &const_map_ptr_types, + [ARG_PTR_TO_CTX] = &context_types, + [ARG_PTR_TO_CTX_OR_NULL] = &context_types, + [ARG_PTR_TO_SOCK_COMMON] = &sock_types, + [ARG_PTR_TO_SOCKET] = &fullsock_types, + [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, + [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, + [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, + [ARG_PTR_TO_MEM] = &mem_types, + [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, + [ARG_PTR_TO_UNINIT_MEM] = &mem_types, + [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, + [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, + [ARG_PTR_TO_INT] = &int_ptr_types, + [ARG_PTR_TO_LONG] = &int_ptr_types, + [__ARG_TYPE_MAX] = NULL, +}; + +static int check_reg_type(struct bpf_verifier_env *env, u32 regno, + const struct bpf_reg_types *compatible) +{ + struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; + enum bpf_reg_type expected, type = reg->type; + int i, j; + + for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { + expected = compatible->types[i]; + if (expected == NOT_INIT) + break; + + if (type == expected) + return 0; + } + + verbose(env, "R%d type=%s expected=[", regno, reg_type_str[type]); + for (j = 0; j + 1 < i; j++) + verbose(env, "%s, ", reg_type_str[compatible->types[j]]); + verbose(env, "%s]\n", reg_type_str[compatible->types[j]]); + return -EACCES; +} + static int check_func_arg(struct bpf_verifier_env *env, u32 arg, struct bpf_call_arg_meta *meta, const struct bpf_func_proto *fn) { u32 regno = BPF_REG_1 + arg; struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; - enum bpf_reg_type expected_type, type = reg->type; enum bpf_arg_type arg_type = fn->arg_type[arg]; - const struct btf_id_set *btf_ids = NULL; + const struct bpf_reg_types *compatible; + enum bpf_reg_type type = reg->type; int err = 0; if (arg_type == ARG_DONTCARE) @@ -3961,75 +4060,19 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, */ goto skip_type_check; - if (arg_type == ARG_PTR_TO_MAP_KEY || - arg_type == ARG_PTR_TO_MAP_VALUE || - arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || - arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { - expected_type = PTR_TO_STACK; - if (!type_is_pkt_pointer(type) && - type != PTR_TO_MAP_VALUE && - type != expected_type) - goto err_type; - } else if (arg_type == ARG_CONST_SIZE || - arg_type == ARG_CONST_SIZE_OR_ZERO || - arg_type == ARG_CONST_ALLOC_SIZE_OR_ZERO) { - expected_type = SCALAR_VALUE; - if (type != expected_type) - goto err_type; - } else if (arg_type == ARG_CONST_MAP_PTR) { - expected_type = CONST_PTR_TO_MAP; - if (type != expected_type) - goto err_type; - } else if (arg_type == ARG_PTR_TO_CTX || - arg_type == ARG_PTR_TO_CTX_OR_NULL) { - expected_type = PTR_TO_CTX; - if (type != expected_type) - goto err_type; - } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { - expected_type = PTR_TO_SOCK_COMMON; - /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ - if (!type_is_sk_pointer(type)) - goto err_type; - } else if (arg_type == ARG_PTR_TO_SOCKET || - arg_type == ARG_PTR_TO_SOCKET_OR_NULL) { - expected_type = PTR_TO_SOCKET; - if (type != expected_type && - type != PTR_TO_BTF_ID) - goto err_type; - btf_ids = &btf_fullsock_ids; - } else if (arg_type == ARG_PTR_TO_BTF_ID) { - expected_type = PTR_TO_BTF_ID; - if (type != expected_type) - goto err_type; - } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { - expected_type = PTR_TO_MAP_VALUE; - if (type != expected_type) - goto err_type; - } else if (arg_type_is_mem_ptr(arg_type)) { - expected_type = PTR_TO_STACK; - if (!type_is_pkt_pointer(type) && - type != PTR_TO_MAP_VALUE && - type != PTR_TO_MEM && - type != PTR_TO_RDONLY_BUF && - type != PTR_TO_RDWR_BUF && - type != expected_type) - goto err_type; - } else if (arg_type_is_alloc_mem_ptr(arg_type)) { - expected_type = PTR_TO_MEM; - if (type != expected_type) - goto err_type; - } else if (arg_type_is_int_ptr(arg_type)) { - expected_type = PTR_TO_STACK; - if (!type_is_pkt_pointer(type) && - type != PTR_TO_MAP_VALUE && - type != expected_type) - goto err_type; - } else { - verbose(env, "unsupported arg_type %d\n", arg_type); + compatible = compatible_reg_types[arg_type]; + if (!compatible) { + verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); return -EFAULT; } + err = check_reg_type(env, regno, compatible); + if (err) + return err; + if (type == PTR_TO_BTF_ID) { + const struct btf_id_set *btf_ids = compatible->btf_ids; + if (fn->arg_btf_ids[arg]) btf_ids = fn->arg_btf_ids[arg]; @@ -4185,10 +4228,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, } return err; -err_type: - verbose(env, "R%d type=%s expected=%s\n", regno, - reg_type_str[type], reg_type_str[expected_type]); - return -EACCES; } static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id)