From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13AE7C00140 for ; Wed, 24 Aug 2022 09:57:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236054AbiHXJ5W (ORCPT ); Wed, 24 Aug 2022 05:57:22 -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-Google-Smtp-Source: AA6agR4gyv5u3qg2yLq2zqzn2c9wgxUKEY2z8IPI+Y201XRbuYkIl5QRMnrv7NBI5d1k0Z7rQs+kWw== 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 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