Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp277063pxy; Thu, 22 Apr 2021 01:45:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyCzoaMVw3VwpqB4OKf2CbsR5t4N6wkJW5zHZDlvIPKJefq8LPfzZdimKrPldihQFw619kL X-Received: by 2002:a17:906:dbcd:: with SMTP id yc13mr2066489ejb.99.1619081104652; Thu, 22 Apr 2021 01:45:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619081104; cv=none; d=google.com; s=arc-20160816; b=ZKyN4DbiDsyGnRba5hOsFmStwh11QCN8uf5IbNcHNvlWM0kthAY3bBgXk8dlx+G7Qn 7cMX1SYlsuXVBLSTosAksoMhGcMLrI+v/ZMuZdM+C/2f/QgePHTfizhpkI2wprS9zfqY Im1ZHBTizsDDelieO6lbN43j2CCwvqf1b4s/RABN3+EwZ8OpYhBrJsixyeLO2FIpz70W Z776fVCeqCkNAiDiN4HhoSrix7n3wge4TzeGFsLXM8HRylSLRSqzryt7rDegg/tD7ns8 mAeKRM2TdaxCzlFBi5gLuGRqTrjeg1ti8OVmZhCAvYsK6T6bI/XFYlBBvRYtkxF+MCPI yxaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=b/9GF1YfDxhn16pA4SsWRwQdnqJ92y1Idd/tVVwB0RI=; b=wLtIL+2umnvfvbPw6ijPOXFs8zEqeOm70OTH9jO6vy0WVi4yrVFJ2PyvpyudE+C6Kp hbXGZCAFDsBRLc/ee/ILd1L8nBYVWYdkc9KPeajC7PhOOJsohwHeM9FPrrxFLAbYZUrj Uzss7fZe+YmGkGtaCA1wqmCQHsCtnkt5NBmy5uegHlStKbH7DOo3beWKuqZYA4yeSY1I AYnZMgcwFvwMDIE/gSzd3wOwvos5E6o3mZScPtabp3aC3/D4Pw5pDucpU8PBMm67zfLD CNxG59JUEn/bEH+B/zW+gxyKE6KmsFz9N9K/gS60NW1EeGPYDW6SAvRFpF81/AsVKitx MKmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=LC4HP9PG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lo4si1650713ejb.222.2021.04.22.01.44.41; Thu, 22 Apr 2021 01:45:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=LC4HP9PG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235459AbhDVImW (ORCPT + 99 others); Thu, 22 Apr 2021 04:42:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230341AbhDVImV (ORCPT ); Thu, 22 Apr 2021 04:42:21 -0400 Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDE11C06174A for ; Thu, 22 Apr 2021 01:41:45 -0700 (PDT) Received: by mail-io1-xd2f.google.com with SMTP id a11so1616950ioo.0 for ; Thu, 22 Apr 2021 01:41:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=b/9GF1YfDxhn16pA4SsWRwQdnqJ92y1Idd/tVVwB0RI=; b=LC4HP9PGz3ERWrfyedQJ/0xIkEpplhFDn+XUs9iYGqkhD9kYwGk/i+JCiDAlkCZ4x4 XBztsSL7yd8Tic7J2/ckricsb/Vre2VFLtd7ZfsA94VNcNH3nsRwbCz/wbBfA0ozbZQA Xrs9j2tUZ3EBJm0m46prqwwemTt1N4PlPFf2M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=b/9GF1YfDxhn16pA4SsWRwQdnqJ92y1Idd/tVVwB0RI=; b=Z9bE77PW6dP66w61R/L/ITp7aAE/NoAbcOrNNm1v4BsQUWPFFZVWRaoDYG/KIZlGmF 6XAmio+Fw+wf0nkYDRmf5hBdVdYMYmvvcvhk6MQIw6kdIIS/SsJFKFvd4+GsnALLMjI+ WvjzBK9zv4Va4w+V4+H9UwqgHtojD4GT2QpNaaM2P8uFNgvYl/jpWDnKR+eGyj9e31yv ISTkrP6Q58NS8BFCymN8+G+/Ax5efkYW1v0CeS7E6LTlDd9Dr2g6xY7ihOwa84FQW9ci 0zspjtI8EPonVmAHlxGWWhivkz0UQ/NxESO3DC4CN34MhK14TUgZKKXbk0CQ9YCaCPvw TsnQ== X-Gm-Message-State: AOAM531T+4bPWjbaloxXZ4d0LkComw8kK/n4Uj5LhEi3EVzLUJVnFuTv ttCuBtqjGAX2WexjZbKXNIQD1JJF+6v51njUNAxcZQ== X-Received: by 2002:a05:6638:2515:: with SMTP id v21mr2236163jat.110.1619080905325; Thu, 22 Apr 2021 01:41:45 -0700 (PDT) MIME-Version: 1.0 References: <20210419155243.1632274-1-revest@chromium.org> <20210419155243.1632274-3-revest@chromium.org> <20210419225404.chlkiaku5vaxmmyh@ast-mbp.dhcp.thefacebook.com> In-Reply-To: From: Florent Revest Date: Thu, 22 Apr 2021 10:41:34 +0200 Message-ID: Subject: Re: [PATCH bpf-next v5 2/6] bpf: Add a ARG_PTR_TO_CONST_STR argument type To: Alexei Starovoitov Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Yonghong Song , KP Singh , Brendan Jackman , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 20, 2021 at 5:23 PM Alexei Starovoitov wrote: > > On Tue, Apr 20, 2021 at 5:35 AM Florent Revest wrote: > > > > On Tue, Apr 20, 2021 at 12:54 AM Alexei Starovoitov > > wrote: > > > > > > On Mon, Apr 19, 2021 at 05:52:39PM +0200, Florent Revest wrote: > > > > This type provides the guarantee that an argument is going to be a const > > > > pointer to somewhere in a read-only map value. It also checks that this > > > > pointer is followed by a zero character before the end of the map value. > > > > > > > > Signed-off-by: Florent Revest > > > > Acked-by: Andrii Nakryiko > > > > --- > > > > include/linux/bpf.h | 1 + > > > > kernel/bpf/verifier.c | 41 +++++++++++++++++++++++++++++++++++++++++ > > > > 2 files changed, 42 insertions(+) > > > > > > > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > > > index 77d1d8c65b81..c160526fc8bf 100644 > > > > --- a/include/linux/bpf.h > > > > +++ b/include/linux/bpf.h > > > > @@ -309,6 +309,7 @@ enum bpf_arg_type { > > > > ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */ > > > > ARG_PTR_TO_FUNC, /* pointer to a bpf program function */ > > > > ARG_PTR_TO_STACK_OR_NULL, /* pointer to stack or NULL */ > > > > + ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */ > > > > __BPF_ARG_TYPE_MAX, > > > > }; > > > > > > > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > > > > index 852541a435ef..5f46dd6f3383 100644 > > > > --- a/kernel/bpf/verifier.c > > > > +++ b/kernel/bpf/verifier.c > > > > @@ -4787,6 +4787,7 @@ static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALU > > > > static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; > > > > static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; > > > > static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; > > > > +static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; > > > > > > > > static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { > > > > [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, > > > > @@ -4817,6 +4818,7 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { > > > > [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, > > > > [ARG_PTR_TO_FUNC] = &func_ptr_types, > > > > [ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types, > > > > + [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, > > > > }; > > > > > > > > static int check_reg_type(struct bpf_verifier_env *env, u32 regno, > > > > @@ -5067,6 +5069,45 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, > > > > if (err) > > > > return err; > > > > err = check_ptr_alignment(env, reg, 0, size, true); > > > > + } else if (arg_type == ARG_PTR_TO_CONST_STR) { > > > > + struct bpf_map *map = reg->map_ptr; > > > > + int map_off; > > > > + u64 map_addr; > > > > + char *str_ptr; > > > > + > > > > + if (reg->type != PTR_TO_MAP_VALUE || !map || > > > > > > I think the 'type' check is redundant, > > > since check_reg_type() did it via compatible_reg_types. > > > If so it's probably better to remove it here ? > > > > > > '!map' looks unnecessary. Can it ever happen? If yes, it's a verifier bug. > > > For example in check_mem_access() we just deref reg->map_ptr without checking > > > which, I think, is correct. > > > > I agree with all of the above. I only thought it's better to be safe > > than sorry but if you'd like I could follow up with a patch that > > removes some checks? > ... > > Sure, does not hurt. I can also follow up with a patch unless if you > > prefer doing it yourself. > > Please send a follow up patch. Okay, doing that today :) > I consider this kind of "safe than sorry" to be defensive programming that > promotes less-thinking-is-fine-because-its-faster-to-code style. Fair > I'm sure you've seen my rants against defensive programming in the past :) Ahah, I haven't yet but I surely don't want to make you rant again ;)