Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp796296rwe; Wed, 31 Aug 2022 11:07:27 -0700 (PDT) X-Google-Smtp-Source: AA6agR4l5A2AQfEcigUmRMSa3sMtOKRI1vkfzySDiEyl9oe22uAY/5iy1IywB+st0bbrLp5DgDKJ X-Received: by 2002:a65:594b:0:b0:42b:f6bc:a04d with SMTP id g11-20020a65594b000000b0042bf6bca04dmr14659241pgu.313.1661969247621; Wed, 31 Aug 2022 11:07:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661969247; cv=none; d=google.com; s=arc-20160816; b=McWph0SlTSMFxk+aPbG0twZpe3yGzAsydDjK2ld++9vqQFsKwsznul6SCVe7RNc8i9 8mIkj7YA+hFZnJHOhTBJCx0iwZ1y1m2RT64OQTiRJEUE8qTbD/+bjPCamrp6iOdY20lb KbZ8D9Tefy9pCjshzcMHTj1oKmrFfooHGvDwgAy0pJ6gt64CM6sjFisO+8F7x+m/kNaW FzNdf18y/Nx5EPTQOy06Tr3lIIGwi46PeOJtWUv3aJxb8G81G3/0pqrpqSyJCLVxOiCU U4RXg034IvunhNFnA6byFeQ+7NA82ULH5oSE3cpI7kEjfmKHSeH5fKIwKynd8s1ZtNdi 5uaw== 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=lKRE16b9MM8u6Z8O6Bdi3U8ibkp6UbNP1fi8My+b5A8=; b=LTw8h2QbkKGn0xzaDvNdtGkOa7QFX2hCkmcdVqBHL/bfinxo9J5dKD45qA+BxCWtkt LXLrefHGKNhc7U8UdVXttG0fxg6F2APXB6t4ItEjZZJmUn83HB6FoguitUEqAwgdwRYo H1ByfXAKb4fP7/nxAmMiFUY87PIX4pUHW2QPphTBb0+gDL9d14Ht6yyXD48jSHhvgLKP rRFNbv+jDAaPF8UUmUdxQiL7BC1rmwoaH+zRluoDgxLETVwXg4RZ39emAlQY2yYessZ5 YuqfmWkblKvk+lJNoNr+tsCpHjZOors5SPX4+vtlNudKA9Qcn2sx3ueXWFd7CkWJypzu J+Kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VYG3BHLt; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e27-20020a631e1b000000b0041b64ff7fe9si5349568pge.79.2022.08.31.11.07.16; Wed, 31 Aug 2022 11:07:27 -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=@redhat.com header.s=mimecast20190719 header.b=VYG3BHLt; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231583AbiHaR5J (ORCPT + 99 others); Wed, 31 Aug 2022 13:57:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230490AbiHaR5G (ORCPT ); Wed, 31 Aug 2022 13:57:06 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74F92D83FD for ; Wed, 31 Aug 2022 10:57:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661968621; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=lKRE16b9MM8u6Z8O6Bdi3U8ibkp6UbNP1fi8My+b5A8=; b=VYG3BHLtpT+3HSAtiCg2HvePBO+xqAACMKFW91pU+WYQMKdCy2/guomaQ0jNd+wIE6Jabn eqb//43YD6bqgQFZPR+8o1XOelDwEI3vNWFPedhg6T2tKoaI+WMZZAjhxVDc3eZgDfV2nN s9pF5aBI+ccvpFjz/0TrlWXz3O9yptE= Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-611-ZlsuAi8AOuyChTIf4W-fvQ-1; Wed, 31 Aug 2022 13:56:58 -0400 X-MC-Unique: ZlsuAi8AOuyChTIf4W-fvQ-1 Received: by mail-pf1-f200.google.com with SMTP id s13-20020a056a00194d00b005385093da2dso4210854pfk.13 for ; Wed, 31 Aug 2022 10:56:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=lKRE16b9MM8u6Z8O6Bdi3U8ibkp6UbNP1fi8My+b5A8=; b=hUkL6fHw7uzHFfexuKprU1geYJZ0Nm5ruorXR20l5MGFO7s1YBeaPCN9y8niAeulEO BygbVCbgOuZrMqecGiS+JtyhMgs0CMLo7kWoBSAwmybedRSxUxoGDwAmKKKqpW7JCiWG l1glCnz8xHCHVyHspSsuin4rL6GWsDiIAZq400w51yJkJXEH2LUf/Q+E4kawooXqI3lf JAnPoUABTQXAZwiCi2ZCpgBR3EZZQJ4WtIw+eAs3gW/8Rf+SQ9NSCXekiG4MN56VmJKZ 0ts00X6SAqNt0qOCpKfrK+ToW+w7tKG3hfo4b/O919dbgu5Vmygb1ImP5ccLD5x8ERBZ 8BDA== X-Gm-Message-State: ACgBeo1NAwuvxi28G/LfE+t+p3cndf4uFocq+CxFLpbw9Y/DxUjd9bR+ 5axIZ6nCCUDckbCWPWUULWasX+qsnbwNnhRcnAh/6INkKFpQMtJ5YDfiW35kjOX8UH3a4abRvJx MI8EmLxmXYxhXlvgyiC0y2AusTuFeWmQUXixvXEkj X-Received: by 2002:a65:6255:0:b0:42c:87b1:485b with SMTP id q21-20020a656255000000b0042c87b1485bmr8853669pgv.491.1661968617019; Wed, 31 Aug 2022 10:56:57 -0700 (PDT) X-Received: by 2002:a65:6255:0:b0:42c:87b1:485b with SMTP id q21-20020a656255000000b0042c87b1485bmr8853631pgv.491.1661968616657; Wed, 31 Aug 2022 10:56:56 -0700 (PDT) MIME-Version: 1.0 References: <20220824134055.1328882-1-benjamin.tissoires@redhat.com> <20220824134055.1328882-2-benjamin.tissoires@redhat.com> In-Reply-To: From: Benjamin Tissoires Date: Wed, 31 Aug 2022 19:56:45 +0200 Message-ID: Subject: Re: [PATCH bpf-next v9 01/23] bpf/verifier: allow all functions to read user provided context To: Alexei Starovoitov Cc: Kumar Kartikeya Dwivedi , Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Shuah Khan , Dave Marchevsky , Joe Stringer , Jonathan Corbet , Tero Kristo , LKML , "open list:HID CORE LAYER" , Network Development , bpf , "open list:KERNEL SELFTEST FRAMEWORK" , "open list:DOCUMENTATION" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 Wed, Aug 31, 2022 at 6:37 PM Alexei Starovoitov wrote: > > On Tue, Aug 30, 2022 at 7:29 AM Benjamin Tissoires > wrote: > > > > On Fri, Aug 26, 2022 at 3:51 AM Kumar Kartikeya Dwivedi > > wrote: > > > > > > On Fri, 26 Aug 2022 at 03:42, Alexei Starovoitov > > > wrote: > > > > > > > > On Wed, Aug 24, 2022 at 6:41 AM Benjamin Tissoires > > > > wrote: > > > > > > > > > > When a function was trying to access data from context in a syscall eBPF > > > > > program, the verifier was rejecting the call unless it was accessing the > > > > > first element. > > > > > This is because the syscall context is not known at compile time, and > > > > > so we need to check this when actually accessing it. > > > > > > > > > > Check for the valid memory access if there is no convert_ctx callback, > > > > > and allow such situation to happen. > > > > > > > > > > There is a slight hiccup with subprogs. btf_check_subprog_arg_match() > > > > > will check that the types are matching, which is a good thing, but to > > > > > have an accurate result, it hides the fact that the context register may > > > > > be null. This makes env->prog->aux->max_ctx_offset being set to the size > > > > > of the context, which is incompatible with a NULL context. > > > > > > > > > > Solve that last problem by storing max_ctx_offset before the type check > > > > > and restoring it after. > > > > > > > > > > Acked-by: Kumar Kartikeya Dwivedi > > > > > Signed-off-by: Benjamin Tissoires > > > > > > > > > > --- > > > > > > > > > > changes in v9: > > > > > - rewrote the commit title and description > > > > > - made it so all functions can make use of context even if there is > > > > > no convert_ctx > > > > > - remove the is_kfunc field in bpf_call_arg_meta > > > > > > > > > > changes in v8: > > > > > - fixup comment > > > > > - return -EACCESS instead of -EINVAL for consistency > > > > > > > > > > changes in v7: > > > > > - renamed access_t into atype > > > > > - allow zero-byte read > > > > > - check_mem_access() to the correct offset/size > > > > > > > > > > new in v6 > > > > > --- > > > > > kernel/bpf/btf.c | 11 ++++++++++- > > > > > kernel/bpf/verifier.c | 19 +++++++++++++++++++ > > > > > 2 files changed, 29 insertions(+), 1 deletion(-) > > > > > > > > > > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c > > > > > index 903719b89238..386300f52b23 100644 > > > > > --- a/kernel/bpf/btf.c > > > > > +++ b/kernel/bpf/btf.c > > > > > @@ -6443,8 +6443,8 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, > > > > > { > > > > > struct bpf_prog *prog = env->prog; > > > > > struct btf *btf = prog->aux->btf; > > > > > + u32 btf_id, max_ctx_offset; > > > > > bool is_global; > > > > > - u32 btf_id; > > > > > int err; > > > > > > > > > > if (!prog->aux->func_info) > > > > > @@ -6457,9 +6457,18 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, > > > > > if (prog->aux->func_info_aux[subprog].unreliable) > > > > > return -EINVAL; > > > > > > > > > > + /* subprogs arguments are not actually accessing the data, we need > > > > > + * to check for the types if they match. > > > > > + * Store the max_ctx_offset and restore it after btf_check_func_arg_match() > > > > > + * given that this function will have a side effect of changing it. > > > > > + */ > > > > > + max_ctx_offset = env->prog->aux->max_ctx_offset; > > > > > + > > > > > is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; > > > > > err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, 0); > > > > > > > > > > + env->prog->aux->max_ctx_offset = max_ctx_offset; > > > > > > > > I don't understand this. > > > > If we pass a ctx into a helper and it's going to > > > > access [0..N] bytes from it why do we need to hide it? > > > > max_ctx_offset will be used later raw_tp, tp, syscall progs > > > > to determine whether it's ok to load them. > > > > By hiding the actual size of access somebody can construct > > > > a prog that reads out of bounds. > > > > How is this related to NULL-ness property? > > > > > > Same question, was just typing exactly the same thing. > > > > The test I have that is failing in patch 2/23 is the following, with > > args being set to NULL by userspace: > > > > SEC("syscall") > > int kfunc_syscall_test_null(struct syscall_test_args *args) > > { > > bpf_kfunc_call_test_mem_len_pass1(args, 0); > > > > return 0; > > } > > > > Basically: > > if userspace declares the following: > > DECLARE_LIBBPF_OPTS(bpf_test_run_opts, syscall_topts, > > .ctx_in = NULL, > > .ctx_size_in = 0, > > ); > > > > The verifier is happy with the current released kernel: > > kfunc_syscall_test_fail() never dereferences the ctx pointer, it just > > passes it around to bpf_kfunc_call_test_mem_len_pass1(), which in turn > > is also happy because it says it is not accessing the data at all (0 > > size memory parameter). > > > > In the current code, check_helper_mem_access() actually returns > > -EINVAL, but doesn't change max_ctx_offset (it's still at the value of > > 0 here). The program is now marked as unreliable, but the verifier > > goes on. > > > > When adding this patch, if we declare a syscall eBPF (or any other > > function that doesn't have env->ops->convert_ctx_access), the previous > > "test" is failing because this ensures the syscall program has to have > > a valid ctx pointer. > > btf_check_func_arg_match() now calls check_mem_access() which > > basically validates the fact that the program can dereference the ctx. > > > > So now, without the max_ctx_offset store/restore, the verifier > > enforces that the provided ctx is not null. > > > > What I thought that would happen was that if we were to pass a NULL > > context from userspace, but the eBPF program dereferences it (or in > > that case have a subprog or a function call that dereferences it), > > then max_ctx_offset would still be set to the proper value because of > > that internal dereference, and so the verifier would reject with > > -EINVAL the call to the eBPF program. > > > > If I add another test that has the following ebpf prog (with ctx_in > > being set to NULL by the userspace): > > > > SEC("syscall") > > int kfunc_syscall_test_null_fail(struct syscall_test_args *args) > > { > > bpf_kfunc_call_test_mem_len_pass1(args, sizeof(*args)); > > > > return 0; > > } > > > > Then the call of the program is actually failing with -EINVAL, even > > with this patch. > > > > But again, if setting from userspace a ctx of NULL with a 0 size is > > not considered as valid, then we can just drop that hunk and add a > > test to enforce it. > > PTR_TO_CTX in the verifier always means valid pointer. > All code paths in the verifier assumes that it's not NULL. > Pointer to skb, to xdp, to pt_regs, etc. > The syscall prog type is little bit special, since it > makes sense not to pass any argument to such prog. > So ctx_size_in == 0 is enforced after the verification: > if (ctx_size_in < prog->aux->max_ctx_offset || > ctx_size_in > U16_MAX) > return -EINVAL; > The verifier should be able to proceed assuming ctx != NULL > and remember max max_ctx_offset. > If max_ctx_offset == 4 and ctx_size_in == 0 then > it doesn't matter whether the actual 'ctx' pointer is NULL > or points to a valid memory. > So it's ok for the verifier to assume ctx != NULL everywhere. Ok, thanks for the detailed explanation. > > Back to the issue at hand. > With this patch the line: > bpf_kfunc_call_test_mem_len_pass1(args, sizeof(*args)); > will be seen as access_size == sizeof(*args), right? > So this part: > + if (access_size == 0) > + return zero_size_allowed ? 0 : -EACCES; > > will be skipped and > the newly added check_mem_access() will call check_ctx_access() > which will call syscall_prog_is_valid_access() and it will say > that any off < U16_MAX is fine and will simply > record max max_ctx_offset. > The ctx_size_in < prog->aux->max_ctx_offset check is done later. Yep, this is correct and this is working now, with a proper error (and no, this is not the error I am trying to fix, see below): eBPF prog: ``` SEC("?syscall") int kfunc_syscall_test_null_fail(struct syscall_test_args *args) { bpf_kfunc_call_test_mem_len_pass1(args, sizeof(*args)); return 0; } ``` before this patch (1/23): * with ctx not NULL: libbpf: prog 'kfunc_syscall_test_null_fail': BPF program load failed: Invalid argument R1 type=ctx expected=fp arg#0 arg#1 memory, len pair leads to invalid memory access => this is not correct, we expect the program to be loaded (and it is expected, this is the bug that is fixed) * Same result with ctx being NULL from the caller With just the hunk in kernel/bpf/verifier.c (so without touching max_ctx_offset: * with ctx not NULL: program is loaded, and executed correctly * with ctx being NULL: program is now loaded, but execution returns -EINVAL, as expected So this case is fully solved by just the hunk in verifier.c With the full patch: same results, with or without ctx being set to NULL, so no side effects. > > So when you're saying: > "call of the program is actually failing with -EINVAL" > that's the check you're referring to? No. I am referring to the following eBPF program: ``` SEC("syscall") int kfunc_syscall_test_null(struct syscall_test_args *args) { return 0; } ``` (no calls, just the declaration of a program) This one is supposed to be loaded and properly run whatever the context is, right? However, without the hunk in the btf.c file (max_ctx_offset), we have the following (ctx is set to NULL by the userspace): verify_success:FAIL:kfunc_syscall_test_null unexpected error: -22 (errno 22) The reason is that the verifier is calling btf_check_subprog_arg_match() on programs too, and considers that ctx is not NULL, and bumps the max_ctx_offset value. > > If so, everything works as expected. Not exactly, we can not call a syscall program with a null context without this hunk. > The verifier thinks that bpf_kfunc_call_test_mem_len_pass1() > can read that many bytes from args, > so it has to reject running the loaded prog in bpf_prog_test_run_syscall(). Yes, that part works. I am focusing on the program declaration. > > So what are you trying to achieve ? See above :) > Make the verifier understand that ctx can be NULL ? Nope. I am fine with the way it is. But any eBPF (sub)prog is checked against btf_check_subprog_arg_match(), which in turns marks all of these calls accessing the entire ctx, even if the ctx is null when that case is valid. > If so that is a probably huge undertaking. > Something else? > Hopefully this is clearer now. Cheers, Benjamin