Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp429303imi; Fri, 22 Jul 2022 02:06:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vsj0CwNNq0GlS6Bi0crDrFgOZMpBLS2FPBQEYsaBiICMYqVBU/DpcreJ9xecDkMIDAT9GP X-Received: by 2002:a17:907:6818:b0:72b:5bac:c3a3 with SMTP id qz24-20020a170907681800b0072b5bacc3a3mr2247164ejc.139.1658480766840; Fri, 22 Jul 2022 02:06:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658480766; cv=none; d=google.com; s=arc-20160816; b=MxoppcgQ8Tr72ArF7KHlg2AjE29Map7r/LOACqWHWpp/sfHNZi1Rb6vTrrwKWqJaKe 7uzDRtDMTOWhRWwt406GfHdjZ/VLCJTbPSeFGeUvFOgMklqbbDDkO4JWiCpFsp4kN95W aa9PHEBN4npgbxGJOEBLIIUZWxsCORZaWoHh5zHwib4f8ANKgITAcOIGb0/qFQLKgV9b oIcPGPIZjjVzXpocczFwc+ok9xGX5hGTZVp4Q0eR1y2StbbCAc1F/ihq7pkIj3mCKUXf oeFcj32YYRrBFKok80a/Om2lwYQc7DYpaQc3f67dn0VAxlV+ld+TcPJrTaSE5Ojpy4Ta HwNw== 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=KPJ7NU0SBJ5IGQBa0MGzAB99cv4bRZOacuI2wl8M0yY=; b=w7jITabr5887TchpPEKcFgoskIlbuxLHu6eYTgXnPZXmPkPIBRM225PKuxsljHLtgS yvIKWwRnwfJehjwG0VUR/RB+VKlY46f+k1cF+yjnYYeLTrlAT8/qDZIENm5sZ8N6lA7f 7yDq10nf379dH/aQoTNTfyQNleNd27YlSNWsl0Q5bXy2OFF2pMNiEtZFuH9dQ8frsO0w z9aeso8w55oEOZ1kH0YKy5xhFL2um9E9iN3YoZuDUEI+Qc6ZfwP86pc+1GXBQ+Lf59Dj D9OysO4bFnQN62uSeYZrGzCvbMTMnvnt8AaQU6tQS3/B70A4yXy/CO0gWlDklVMHbKfZ xRTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZWLOBqvX; 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 v25-20020aa7d9d9000000b0043b92aaa91dsi4349772eds.540.2022.07.22.02.05.42; Fri, 22 Jul 2022 02:06:06 -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=ZWLOBqvX; 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 S234365AbiGVIoZ (ORCPT + 99 others); Fri, 22 Jul 2022 04:44:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229559AbiGVIoS (ORCPT ); Fri, 22 Jul 2022 04:44:18 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 02D559E7B3 for ; Fri, 22 Jul 2022 01:44:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658479457; 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=KPJ7NU0SBJ5IGQBa0MGzAB99cv4bRZOacuI2wl8M0yY=; b=ZWLOBqvXaQF8PY90zYMXThDKiKVd5w9IxB1+uT7BIE2ZM3EZHwKBRpSA7QP9dXFYUxDLgG e8i1sUv/4RdCdgZ8SqIMu6eO9rfQ+W/zj3crv3X78yJo++6uTmm1DaBUb+xpA6EGFB8AaH gBZ0FESgDong0pyUMtuKGAPtovu3z1M= Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-620-ri0mfdKgNMWriFG-bxjFcg-1; Fri, 22 Jul 2022 04:44:09 -0400 X-MC-Unique: ri0mfdKgNMWriFG-bxjFcg-1 Received: by mail-pg1-f197.google.com with SMTP id f128-20020a636a86000000b0041a4b07b039so2083462pgc.5 for ; Fri, 22 Jul 2022 01:44:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=KPJ7NU0SBJ5IGQBa0MGzAB99cv4bRZOacuI2wl8M0yY=; b=ucj0/eiis7iltKt6Z26XKnm0eqtwOD0nD6BYpyVf0+h7vDv57B9faXE45SmRz2VTfq 8lDqL8H/yUeHrBv6Wf/Oeh76F2eGgyAo3VRm4Z/alf2uZ/mXOx6WeRR0sUtj22MHsXsy Q8J1jm2qcO04G30/V+1iJagr18SZOO11xMZ6fNA3Qr+7rJaWLiM55IZNwOaNefgk8sj/ c26bnOH+yiXSTscwJus9ODC/ncCIh5dwOZpLuXXNYioAVJt2IHclXdaie1rHOWPoy03B TSZpbVJc8k9mQfkpMlaYAp/Asm3Fj3cWjNLU320Q7Wqx1Dfjxmkut2il4+QQTqJgp8WB WpSA== X-Gm-Message-State: AJIora8aFMMPV7mTUrKkvuGUfKR0K3avungydAgFTeiRYl8Jo+FT25rF nSpSAYHCnF+4AB/VUkCsF0P1aFDiRJaAtI+wT7CnHkRLym1Pd6TV6dtGiDnL1Xtsg4jYrEstTbE ju+jNnoklN1/x41Th9fCIxF7l8C0zDu5gAqRXurNX X-Received: by 2002:a17:90b:4a08:b0:1ef:f36b:18e1 with SMTP id kk8-20020a17090b4a0800b001eff36b18e1mr16127734pjb.246.1658479448527; Fri, 22 Jul 2022 01:44:08 -0700 (PDT) X-Received: by 2002:a17:90b:4a08:b0:1ef:f36b:18e1 with SMTP id kk8-20020a17090b4a0800b001eff36b18e1mr16127693pjb.246.1658479448127; Fri, 22 Jul 2022 01:44:08 -0700 (PDT) MIME-Version: 1.0 References: <20220721153625.1282007-1-benjamin.tissoires@redhat.com> <20220721153625.1282007-3-benjamin.tissoires@redhat.com> In-Reply-To: From: Benjamin Tissoires Date: Fri, 22 Jul 2022 10:43:57 +0200 Message-ID: Subject: Re: [PATCH bpf-next v7 02/24] bpf/verifier: allow kfunc to read user provided context To: Kumar Kartikeya Dwivedi Cc: 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" , Networking , bpf , "open list:KERNEL SELFTEST FRAMEWORK" , Linux Doc Mailing List 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_NONE, SPF_HELO_NONE,SPF_NONE 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 Thu, Jul 21, 2022 at 10:15 PM Kumar Kartikeya Dwivedi wrote: > > On Thu, 21 Jul 2022 at 17:36, 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. > > > > Signed-off-by: Benjamin Tissoires > > > > --- > > > > LGTM, with just a couple more nits. > Acked-by: Kumar Kartikeya Dwivedi Thanks a lot for the quick review. Sending the new version of just this patch now. Cheers, Benjamin > > > 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..d5fe7e618c52 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) { > > + enum bpf_access_type atype = meta->raw_mode ? BPF_WRITE : BPF_READ; > > + int offset = access_size - 1; > > + > > + /* Allow zero-byte read from NULL or PTR_TO_CTX */ > > This will not be handling the case for NULL, only for kfunc(ptr_to_ctx, 0) > A null pointer has its reg->type as scalar, so it will be handled by > the default case. > > > + if (access_size == 0) > > + return zero_size_allowed ? 0 : -EINVAL; > > We should use -EACCES, just to be consistent. > > > + > > + return check_mem_access(env, env->insn_idx, regno, offset, BPF_B, > > + atype, -1, false); > > + } > > + > > + fallthrough; > > default: /* scalar_value or invalid ptr */ > > /* Allow zero-byte read from NULL, regardless of pointer type */ > > if (zero_size_allowed && access_size == 0 && > > @@ -5335,6 +5355,7 @@ int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state > > WARN_ON_ONCE(regno < BPF_REG_2 || regno > BPF_REG_5); > > > > memset(&meta, 0, sizeof(meta)); > > + meta.is_kfunc = true; > > > > if (may_be_null) { > > saved_reg = *mem_reg; > > -- > > 2.36.1 > > >