Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp409607rwe; Wed, 24 Aug 2022 03:11:55 -0700 (PDT) X-Google-Smtp-Source: AA6agR4A/oLXzg8KIh0W/EHpFfErtVXJtrz8pyqg+X//3LlPYzSkDSuQsR3UdMKMyw9D2ogY6B3q X-Received: by 2002:a17:907:94c6:b0:730:b3a6:d612 with SMTP id dn6-20020a17090794c600b00730b3a6d612mr2629234ejc.28.1661335915357; Wed, 24 Aug 2022 03:11:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661335915; cv=none; d=google.com; s=arc-20160816; b=gAxoQPD+xzK1+gOl45hSPHpnoJbmdeiqjj1lzb2IdyN873P/k8Mn83G3ZzE5DdHC6I x8ccuNE6a6KBq5ERBb1dGczGoua7Peb+hf3IcyrmpWV8VtdD98EIo+qkRQs9r3cE0Kag aqnxiayDObYz+n3GJvjkrnADuF5jqU3IW3Wnks/oP/pcSglrNdu8d9v33i+EQ7hQahCM h0KEbczxCJ1Qsi1JNacIfnLUbtk5Nt1d22eZBTgStUi0Xr3ew1dN2yPpd31eNQB8FTny KEz9ZGPMt2VsaeaLOqtZEg/LQZQYboJL0F6BSPB19MgOpGYOET3TTrtnkh7yMmnDC/5z JZBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:references :cc:to:from:content-language:subject:user-agent:mime-version:date :message-id:dkim-signature; bh=67TcKJ9jWGMiEw4kg52hWXxqV/N5/I9VlmIszyd4tqI=; b=IQWJ+0/UHixRvGWL6tbVJvppt5FZigvnYMk1jtqt+60IBuZqJn+EAmAsl6h1dwIA69 gkd7gTayjWJtxO8/9FjhFS2ypja3vxrjjx6VilwEg0EO7Z7oH4B+vkC3BPb8TqvQ97oI m17uyXSWZX10r5OtKflsuRryAVu8h4f9zHRKCQztvuGcTIgBSrLSBDRmHQs6tW+aOQvv dQ39XB6R00pO+7GQBDGusS3Wsk4L0iIrh30akI4206Y2dvusp0N6CWbAc0uKSFfa7STm MVun+IAr7dbUjW6HaAZ1clVCS7oE2lua2Xr6GYcD4VxQ254QTEWcAC9PYF3cnwMshWIt 01vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bvEH1HGH; 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 nd21-20020a170907629500b00734bbe8d2e1si1569724ejc.545.2022.08.24.03.11.28; Wed, 24 Aug 2022 03:11:55 -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=bvEH1HGH; 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 S234910AbiHXJ5T (ORCPT + 99 others); Wed, 24 Aug 2022 05:57:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232895AbiHXJ5N (ORCPT ); Wed, 24 Aug 2022 05:57:13 -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 B14D979A76 for ; Wed, 24 Aug 2022 02:57:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661335031; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=67TcKJ9jWGMiEw4kg52hWXxqV/N5/I9VlmIszyd4tqI=; b=bvEH1HGHJzdiAwtzVRPh+TAAZYPV+PosX4ArREjQzdBJtfLhHWdA0rKu+IBplUWWLdi3KP KOl4Kp2N+zIw55D92Rztwuy/5yRFSFsga+WZ/C+nc9WbFRF8p1lvZ/hlYfje/qU1fm4yrv z5+M5Cr+ZPeqJhs87I+8h8M5Bfp+rEw= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-365-dCN4VzZQPjCjHH9U-_2dyQ-1; Wed, 24 Aug 2022 05:57:10 -0400 X-MC-Unique: dCN4VzZQPjCjHH9U-_2dyQ-1 Received: by mail-wm1-f72.google.com with SMTP id n7-20020a1c2707000000b003a638356355so7019490wmn.2 for ; Wed, 24 Aug 2022 02:57:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:references:cc:to:from :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=67TcKJ9jWGMiEw4kg52hWXxqV/N5/I9VlmIszyd4tqI=; b=GNT1jta/qXEcc9mgSjxifh+ElDgCwq7jZv2CbRjN2lbWZTj4E72H3z4lX5qxlY9MR7 ZvXRvH+uUgktYOp5crWpcqDVi8nTDJaprkn0JmwhZohCXg+0GM9tmHq8iEv882pK7iaT LVdQlZ2hBaEyGZZDEIiO/IdZHa8TuhXncAKUj1dj0ly+I8pHW6F3Te7Ln7hN+XIAi7Hc wMlJ8wHyWVuytyP6LJtYLsrgpR4bvPF2qEyYQY3TwMduzIvk03smH/QksRew48Jr29fe plHMJHMszlq63EdQscxiUpztxJtl5gHI7fyykW1tAl7K53wqxJA+gIsvcqCtfsywkD3g qiBQ== X-Gm-Message-State: ACgBeo1dLd+EzulLws3k5SELWL1SA+oJ7ZY0gOno1KRW2f2Wfq3zSANZ tAmTHkVs3R0ifxIHFpBvOJpTb+rXJY6Np8j9ms9WVNXY5f4dH7Ne9MUNOoTHbxZ9EeM35sGTPuv o8a+GoUY9M5HZ/HV9XY7ya2Xv X-Received: by 2002:a05:6000:606:b0:225:7264:8f06 with SMTP id bn6-20020a056000060600b0022572648f06mr985430wrb.27.1661335029400; Wed, 24 Aug 2022 02:57:09 -0700 (PDT) X-Received: by 2002:a05:6000:606:b0:225:7264:8f06 with SMTP id bn6-20020a056000060600b0022572648f06mr985403wrb.27.1661335029194; Wed, 24 Aug 2022 02:57:09 -0700 (PDT) Received: from [192.168.110.200] (82-65-22-26.subs.proxad.net. [82.65.22.26]) by smtp.gmail.com with ESMTPSA id z13-20020a5d44cd000000b00222ed7ea203sm16236679wrr.100.2022.08.24.02.57.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Aug 2022 02:57:08 -0700 (PDT) Message-ID: <5bba0f0e-544e-85ef-627b-6dd35244871a@redhat.com> Date: Wed, 24 Aug 2022 11:57:07 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH bpf-next v8 02/24] bpf/verifier: allow kfunc to read user provided context Content-Language: en-US From: Benjamin Tissoires To: Alexei Starovoitov Cc: Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , Kumar Kartikeya Dwivedi , 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" References: <20220721153625.1282007-3-benjamin.tissoires@redhat.com> <20220722084556.1342406-1-benjamin.tissoires@redhat.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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,NICE_REPLY_A, 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 7/25/22 18:36, Benjamin Tissoires wrote: > On Fri, Jul 22, 2022 at 6:16 PM Alexei Starovoitov > wrote: >> >> On Fri, Jul 22, 2022 at 1:46 AM Benjamin Tissoires >> wrote: >>> >>> When a kfunc was trying to access data from context in a syscall eBPF >>> program, the verifier was rejecting the call. >>> 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 and allow such situation to happen. >>> >>> Acked-by: Kumar Kartikeya Dwivedi >>> Signed-off-by: Benjamin Tissoires >>> >>> --- >>> >>> 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/verifier.c | 21 +++++++++++++++++++++ >>> 1 file changed, 21 insertions(+) >>> >>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c >>> index 7c1e056624f9..c807c5d7085a 100644 >>> --- a/kernel/bpf/verifier.c >>> +++ b/kernel/bpf/verifier.c >>> @@ -248,6 +248,7 @@ struct bpf_call_arg_meta { >>> struct bpf_map *map_ptr; >>> bool raw_mode; >>> bool pkt_access; >>> + bool is_kfunc; >>> u8 release_regno; >>> int regno; >>> int access_size; >>> @@ -5170,6 +5171,7 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, >>> struct bpf_call_arg_meta *meta) >>> { >>> struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; >>> + enum bpf_prog_type prog_type = resolve_prog_type(env->prog); >>> u32 *max_access; >>> >>> switch (base_type(reg->type)) { >>> @@ -5223,6 +5225,24 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, >>> env, >>> regno, reg->off, access_size, >>> zero_size_allowed, ACCESS_HELPER, meta); >>> + case PTR_TO_CTX: >>> + /* in case of a kfunc called in a program of type SYSCALL, the context is >>> + * user supplied, so not computed statically. >>> + * Dynamically check it now >>> + */ >>> + if (prog_type == BPF_PROG_TYPE_SYSCALL && meta && meta->is_kfunc) { >> >> prog_type check looks a bit odd here. >> Can we generalize with >> if (!env->ops->convert_ctx_access > > Yep, seems to be working fine for my use case and the test cases I > have in this series. > >> >> In other words any program type that doesn't have ctx rewrites can >> use helpers to access ctx fields ? >> >> Also why kfunc only? >> It looks safe to allow normal helpers as well. > > Well, not sure what is happening here, but if I remove the check for > kfunc, the test for PTR_TO_CTX == NULL and size == 0 gives me a > -EINVAL. I finally managed to track down the issue. The reason was that if we now call check_mem_access for every function check, but also subprogs. And so we ensure that a subprog can access context. This is all fine, but that test now tags the subprog accessing the context, even if it is actually null and not accessing it in the code. So to restore the previous behavior, I am storing env->prog->aux->max_ctx_offset in btf_check_subprog_arg_match() and restore it after the call to check for the arguments. See the v9 for the detail in the code. Cheers, Benjamin > > The original reason for kfunc only was because I wanted to scope the > changes to something I can control, but now I am completely out of > ideas on why the NULL test fails if it enters the if branch. > > Unfortunately I won't have a lot of time this week to tackle this (I > am on holiday with my family), and next will be tough too (at home but > doing renovations). > > I can send the fixup to remove the prog_type check as I just made sure > it works with the selftests. But I won't be able to dig further why it > fails without the kfunc check, because not enough time and > concentration. > > Cheers, > Benjamin