Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp7818557rwp; Wed, 19 Jul 2023 00:14:19 -0700 (PDT) X-Google-Smtp-Source: APBJJlG4Q0ujTnWr5eRBjEULLkD6GKgBonZE325PVEu8Dl6zFr0a00Sntv63OMT8vD1Hz0dsTzco X-Received: by 2002:a05:6808:1452:b0:3a3:fe16:3147 with SMTP id x18-20020a056808145200b003a3fe163147mr18497892oiv.16.1689750859400; Wed, 19 Jul 2023 00:14:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689750859; cv=none; d=google.com; s=arc-20160816; b=UjgXhqlmkKQ3b+Tq4OJDWN9UrZUkFC4gWKqFhfIFqXR7R3l/r7uIIFoMbSgi8kVxoT 44KKPf7HXCTZIVLP7Veb1tCV35yIDnDMJ6ifXVLO9xNStvi9GLpcrFJPyXbDJPo0GS3k fXvMBt0S5US59vYoZgnKF8cSI+lhDWMvdeU1wZU4BD/XOfa0NTQgDIBxe0HpnEnBnGz+ 0zdXdA2VFgOl1W1wURZ0N2YQ1rmEv81TPO1IWv13v908ImPs7iOhl3GeTC+nL+cUrpqe yBEYnRUT+kvGE1xQKAuICVaoDfUUoWOffQVrGHVx0fIEKR5ljvHAfISehmHq8JgV1a0l MvQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=AnRQdh3EEqJvpxETy5ODliVLYVhTAipKqSwZ1tgyM7s=; fh=qH/QEk0Tkh7H9+AnPzwGhnd8VOU2cZ8UrnutYKfkUFU=; b=dbxbg4tfbbX00mSmGlMHheeNFzLpTR/lSw9oV/G/evLgwYqPxYGAiEITfYBYmsWtT1 UMUR7H4BbWywmaWXcq4Z3jxnrhM+V2gANaJUaGd19W8ch2qWaklqZoIQ8iYCqDxqiP5O ZbPn9A/Hjsa3UovQTxvno1mQB5h+PoRWjSjOoW6wYJx5IKziGdbZ1RlsAotpwS8FmQHp AxXau8c4vD8P4KqOc8PNfl8/75V/u7HSVlaS7CUytI25ZZCX7fauSUDBNmlBcY7XJtEa 3mW7OoHUkQtY5cbBHcndwO+JDcim+9RUv7boY/btLZ9IitqGZHHiDQZOeS+vzN9drImt EcBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@isovalent.com header.s=google header.b=aTwmtP37; 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 fy14-20020a17090b020e00b00263c48abf8asi929177pjb.31.2023.07.19.00.14.07; Wed, 19 Jul 2023 00:14:19 -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=aTwmtP37; 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 S229562AbjGSHJT (ORCPT + 99 others); Wed, 19 Jul 2023 03:09:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229497AbjGSHJR (ORCPT ); Wed, 19 Jul 2023 03:09:17 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E41E3E43 for ; Wed, 19 Jul 2023 00:09:15 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-307d58b3efbso6316171f8f.0 for ; Wed, 19 Jul 2023 00:09:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689750554; x=1692342554; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=AnRQdh3EEqJvpxETy5ODliVLYVhTAipKqSwZ1tgyM7s=; b=aTwmtP37x0bkdUYFbjUqUYnJC/Hnr+D9La8jEgyiierVL4utIhqhD1JTy+P+f+y/mq UXKokW7pEV5meRgolDZSxJ7N19/4DmWjiMeVcvYTPDLdjlidkQUIu6fATD7bXgYsUi5+ QDsDb68YKoRkNiyCH5mzvEE+uIsQvdaHoFtI5FrxdLVyaP/VsEGlxadzIdv39txlqr8M ZmOPhQxhoDjonu4G4cLR6BjLvY5eh7gwcdHL91j5C314qlUhimptswBFolJ9CVmGacBk Ml8ckDptZTszCTHgyoEHGqJ2lzHA0ZRe9xh4tuA8rIrFHESi3UpHfTen3XB+x58nc/YG JJOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689750554; x=1692342554; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AnRQdh3EEqJvpxETy5ODliVLYVhTAipKqSwZ1tgyM7s=; b=iMzvHmPhjJY6B9nWdozz0t5mPVs1ZLE1YReCCiGwIWaBFZjBuNoY00ygItKToeNOEx Ta5jLQX/K/nvAdFsJTcz6qay4KKve0nUyyOFodM8fC9O/iLQqb/NT4l+/q/awYiRi18v ZUke/P+y6X/HV0HBT/IkkMz4RR5ScteDXL3m8LvQ4uWQa25GZNiGdAlS/Syy3cTp1YIb heaYd2fzehpdzAdW7Qn9XWvxHSj1lP2x8MHqpE1sG4p0R7zpdXLXqxd6/wlLVe6GGIcF nsUqj2YrBTDmXMVjf7BhYPGAwIKTqsN7Xog/shtgNt5XROUta+awesy4vx5oZI+ABTUL 5RVA== X-Gm-Message-State: ABy/qLaeIFz/NvUghOS5nV/STFHKv1MHpsHPh17LLJSXAkprk0eXIXfx quFYZ1fG6IxhheYrKbXxOJ/fsA== X-Received: by 2002:adf:f48f:0:b0:315:adee:2e47 with SMTP id l15-20020adff48f000000b00315adee2e47mr14943637wro.30.1689750554376; Wed, 19 Jul 2023 00:09:14 -0700 (PDT) Received: from zh-lab-node-5 ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id n4-20020a5d4204000000b00315a1c160casm4388505wrq.99.2023.07.19.00.09.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 00:09:13 -0700 (PDT) Date: Wed, 19 Jul 2023 07:10:23 +0000 From: Anton Protopopov To: Alexei Starovoitov Cc: 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 , bpf , LKML , "open list:KERNEL SELFTEST FRAMEWORK" Subject: Re: [PATCH bpf-next 1/3] bpf: consider CONST_PTR_TO_MAP as trusted pointer to struct bpf_map Message-ID: References: <20230714141747.41560-1-aspsk@isovalent.com> <20230714142100.42265-1-aspsk@isovalent.com> <20230714142100.42265-2-aspsk@isovalent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: 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=unavailable 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 On Tue, Jul 18, 2023 at 05:54:27PM -0700, Alexei Starovoitov wrote: > On Sun, Jul 16, 2023 at 12:49 AM Anton Protopopov wrote: > > > > On Fri, Jul 14, 2023 at 10:56:00AM -0700, Alexei Starovoitov wrote: > > > On Fri, Jul 14, 2023 at 7:20 AM Anton Protopopov wrote: > > > > > > > > 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; > > > > + > > > > > > Overall it looks great. > > > Instead of above, how about the following instead: > > > > > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > > > index 0b9da95331d7..cd08167dc347 100644 > > > --- a/kernel/bpf/verifier.c > > > +++ b/kernel/bpf/verifier.c > > > @@ -10775,7 +10775,7 @@ static int check_kfunc_args(struct > > > bpf_verifier_env *env, struct bpf_kfunc_call_ > > > if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta)) > > > break; > > > > > > - if (!is_trusted_reg(reg)) { > > > + if (!is_trusted_reg(reg) && > > > !reg2btf_ids[base_type(reg->type)]) { > > > > > > > > > This way we won't need to list every convertible type in is_trusted_reg. > > > > > > I'm a bit hesitant to put reg2btf_ids[] check directly into is_trusted_reg(). > > > Maybe it's ok, but it needs more analysis. > > > > I am not sure I see a difference in adding a check you proposed above and > > adding the reg2btf_ids[] check directly into the is_trusted_reg() function. > > Basically, we say "if type is in reg2btf_ids[], then consider it trusted" in > > both cases. AFAIS, currently the reg2btf_ids[] contains only trusted types, > > however, could it happen that we add a non-trusted type there? > > > > So, I would leave the patch as is (which also makes sense because the > > const-ptr-to-map is a special case), or add the "reg2btf_ids[] check" > > directly into the is_trusted_reg() function. > > Fair enough. Let's add reg2btf_ids[] to is_trusted_reg() directly. Thanks, I will send v2.