Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp2524879rwp; Fri, 14 Jul 2023 07:27:07 -0700 (PDT) X-Google-Smtp-Source: APBJJlFwYeL1PTMe8pR+Jl9SY/VPacPTqnPHubcI378ac1lwYgKLcu0ifFGchnv0oZFKvydfqweU X-Received: by 2002:a17:902:dad2:b0:1b8:b3f7:4872 with SMTP id q18-20020a170902dad200b001b8b3f74872mr5834637plx.28.1689344826950; Fri, 14 Jul 2023 07:27:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689344826; cv=none; d=google.com; s=arc-20160816; b=HKLzfuNuzHLRXqP790IdkmmmSgef+FcevxSuEJMN6ISIw08xH5/Z4ad+ROCHdZ9afl 10Nxlshbab9RY73/COl0SzkYCcZ8e45il9forGkC2I64KWBgj2F0T7I2gV2+N4xYb9y8 9I9s0eANizcEBUbBIgZ0zP5WS9GOLkEIxVOr4C9+4fZHsdHw4wbqDx9JfvmEw9Pz9VyK LvE+eOx4CPDNGA++D7iOz2bYhLqguFiAC2t/djooGhGlyakqkNzekgWGSGLrEww3hL8s +oIoaTLAYgZzg5TQaHg26YFH8fH+EkWxGDQhYNGHW8lj0jdR2drHvp5H1VaTvS+yyEtE b2iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=8qk8T3EVPKdRYf+naU4m+YVtLzpGUKvj2gEOLRZFi7k=; fh=2sg2q7H8sim4Fm1AbvHvV4k3HSKIwWA7VUbqHVkgvXY=; b=jp4lcShuxC9ydPs1jT9qTATeTEAvRV3dF3OtqazCHl8etJEyS8Egy4baLePYiHSAMN ztaauetKBzDgYL1NHTSsgIt1UCxUexGPRPbHv6MpuaJPJMCtydLDeEEWDz/6MmlDEg6O cHEZI7np7ma3prMhKjcEq7QT+IdrgBn+tp8NMwCpVC8bZ/fY9uTMIMyO0VgeStZtO9gx 16MY/qPvfjWVR1imcwrrNi6QDe/PwmQ5+3WmXiDyOD3tc+hzHA9xPVkKd42AyD97kVSY X4mCemkemjyVkJE7Vb1bBDO1S7tjhpzPNy7NjYbAS97d7s6IfbJr2g5J9JyaPwuyXaPo TCpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@isovalent.com header.s=google header.b=fZmmnLin; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=isovalent.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s3-20020a170902ea0300b001b890b3a512si7362178plg.615.2023.07.14.07.26.52; Fri, 14 Jul 2023 07:27:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@isovalent.com header.s=google header.b=fZmmnLin; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=isovalent.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235524AbjGNOUQ (ORCPT + 99 others); Fri, 14 Jul 2023 10:20:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235418AbjGNOUL (ORCPT ); Fri, 14 Jul 2023 10:20:11 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A50E35BB for ; Fri, 14 Jul 2023 07:19:59 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-51e6113437cso2347271a12.2 for ; Fri, 14 Jul 2023 07:19:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689344397; x=1691936397; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=8qk8T3EVPKdRYf+naU4m+YVtLzpGUKvj2gEOLRZFi7k=; b=fZmmnLinrKMYrKhEK0Ja70FkcaLdJD7wwVXadLH0V3hRFzduwn6n00RysF14lvageo JsAppEggMr42ZWN1aLrpKNzo2qv5GGcBc/1Y/Bp9px2acb1JlEkaYeBmKIbg1OH0tCU3 nECSVyAnpJZ+Ckza0WyWUMs9e7sQ4oz/c7ccFxMH+P1oM85hBV9vtxQGuvFE6kD5lYwf LzO0QciKwgQiKxGjFF2BIeujLoqdIgYuF5SU0iXUABihsf7b8YonSsL7LI1mgQ9XEaGq ZAYTaYQ0qsTJILqkKDA7KIw0ZURDZWhHWAqveJQMjOJbqQDsRC0q55gW+99LAHgoFVQD rg1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689344397; x=1691936397; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8qk8T3EVPKdRYf+naU4m+YVtLzpGUKvj2gEOLRZFi7k=; b=FCo44DlIA0C+HgXokxK8ZCzic945VRL+vOhq824UnuN5HoxZDItioexxxQyLazrSPx wN+r/2oE2GFC5K6pPfnkPolS6Htxgk0zrT/9aWNx4jHmldwo8dpVXMmylAZvQR9ZMBrC z0lkSDyK6C/LYc/p90L394glgH2SB58+Boga+GRi4OtyMQXpc2+l4zXRZrcOOpgrMwvC LEmRJ/sWQiI/I2Bc16VkkmbmnhohDccKgKG6Q+36K7in5i77OmE1qS3Jdug+IqKK2Bex m7FSXdUZ7WvqZzKOxuTrXK1soHORcSE5oLpkb7NbFhxaIWmvQ+ntit8xFjQ7C8iIOYY9 pByg== X-Gm-Message-State: ABy/qLYgBe9c2cMZAi2b27pQCHzM1sZikvFiQmMwjnqj0xKs0YNgTFDb g25DLrOMpsBtM2Jk185aoZghIQ== X-Received: by 2002:a17:906:9146:b0:989:450:e585 with SMTP id y6-20020a170906914600b009890450e585mr3728345ejw.45.1689344397549; Fri, 14 Jul 2023 07:19:57 -0700 (PDT) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id n11-20020a1709061d0b00b00982cfe1fe5dsm5469294ejh.65.2023.07.14.07.19.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jul 2023 07:19:57 -0700 (PDT) From: Anton Protopopov To: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Hou Tao , Joe Stringer , Anton Protopopov , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 1/3] bpf: consider CONST_PTR_TO_MAP as trusted pointer to struct bpf_map Date: Fri, 14 Jul 2023 14:20:58 +0000 Message-Id: <20230714142100.42265-2-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230714142100.42265-1-aspsk@isovalent.com> References: <20230714141747.41560-1-aspsk@isovalent.com> <20230714142100.42265-1-aspsk@isovalent.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Patch verifier to regard values of type CONST_PTR_TO_MAP as trusted pointers to struct bpf_map. This allows kfuncs to work with `struct bpf_map *` arguments. Save some bytes by defining btf_bpf_map_id as BTF_ID_LIST_GLOBAL_SINGLE (which is u32[1]), not as BTF_ID_LIST (which is u32[64]). Signed-off-by: Anton Protopopov --- include/linux/btf_ids.h | 1 + kernel/bpf/map_iter.c | 3 +-- kernel/bpf/verifier.c | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/linux/btf_ids.h b/include/linux/btf_ids.h index 00950cc03bff..a3462a9b8e18 100644 --- a/include/linux/btf_ids.h +++ b/include/linux/btf_ids.h @@ -267,5 +267,6 @@ MAX_BTF_TRACING_TYPE, extern u32 btf_tracing_ids[]; extern u32 bpf_cgroup_btf_id[]; extern u32 bpf_local_storage_map_btf_id[]; +extern u32 btf_bpf_map_id[]; #endif diff --git a/kernel/bpf/map_iter.c b/kernel/bpf/map_iter.c index d06d3b7150e5..b67996147895 100644 --- a/kernel/bpf/map_iter.c +++ b/kernel/bpf/map_iter.c @@ -78,8 +78,7 @@ static const struct seq_operations bpf_map_seq_ops = { .show = bpf_map_seq_show, }; -BTF_ID_LIST(btf_bpf_map_id) -BTF_ID(struct, bpf_map) +BTF_ID_LIST_GLOBAL_SINGLE(btf_bpf_map_id, struct, bpf_map) static const struct bpf_iter_seq_info bpf_map_seq_info = { .seq_ops = &bpf_map_seq_ops, diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 0b9da95331d7..5663f97ef292 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5419,6 +5419,9 @@ static bool is_trusted_reg(const struct bpf_reg_state *reg) if (reg->ref_obj_id) return true; + if (reg->type == CONST_PTR_TO_MAP) + return true; + /* If a register is not referenced, it is trusted if it has the * MEM_ALLOC or PTR_TRUSTED type modifiers, and no others. Some of the * other type modifiers may be safe, but we elect to take an opt-in @@ -10052,13 +10055,13 @@ static bool __btf_type_is_scalar_struct(struct bpf_verifier_env *env, return true; } - static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { #ifdef CONFIG_NET [PTR_TO_SOCKET] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK], [PTR_TO_SOCK_COMMON] = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], [PTR_TO_TCP_SOCK] = &btf_sock_ids[BTF_SOCK_TYPE_TCP], #endif + [CONST_PTR_TO_MAP] = btf_bpf_map_id, }; enum kfunc_ptr_arg_type { -- 2.34.1