Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2620942pxj; Mon, 31 May 2021 06:45:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxsJgOgfsymLmMzWesHU2NBj6CS/5tieRy36ZyzzGLCVrsjd1LR2CK3cC/Pb34QXB3pET8o X-Received: by 2002:aa7:c9c5:: with SMTP id i5mr15296569edt.160.1622468715786; Mon, 31 May 2021 06:45:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622468715; cv=none; d=google.com; s=arc-20160816; b=crza1gDGscAwOeCOjR6gLSKfy8wqhPBhh1gAweZGgmMjgjcVFGfIP6glOpP+QW0+Vw cmo8iXptzG26QXL/SKiih8GKS7stBSCoo6y3SOrdm8On6lFvR/eftjcmv6zIf+QsftUJ vyE0GSVg7uR3WvjcH2U0hv1IQIRK4PRyfYfruB5wl8tIOUOOpVDyY/GREHD1cwJlp+5F klFNobuWSBJ0dQSXDFGYdUDsEcXIEMtciO2YyPd/shao1JrbuF95Mtm5Zb1Rjy3xvsJv azPdNKU03c67b3nt7jBOYWsoYHg2TFBFTA0hXGcpdHYfAMbZhuBJnwlxgUp+hKFKlcV5 XGkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=++Qu+h6BEWD8hW+hJfhgUOFSEPs6EIoMAIkc6Meg0nE=; b=de7JyJmi7M3ZWZvN+/r55GDpwHWl4m+JPQv4n06IWlNnuwZdjLxstwvb0i9uQpUf3o DlCh+fBC5WbEiWkCAQH12fNrlAEJt843c5zakya626G1CJCzT/TjxI487p4b4dbpwAEs z8vdv+iU3N8bdR3qbxDFALD9S0LlGkkxkyHQBg4LQIruZi7mdLY/+JA3Cnus84Of7wCM Wme6Ou7DEmec6jhkjsumlLDKZikudWWR2SKEcSJgGRGzGR2b+EBbhVBzr4CTtUftInE5 JphJ/t/gHafEQxEyHKKLbgbmDryocP/UD7/v6aX+orFD0pylZOc9Cnz619PbSeOSJ4D2 nPAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=w3D8jjsy; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n16si551469edq.422.2021.05.31.06.44.52; Mon, 31 May 2021 06:45:15 -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=@linuxfoundation.org header.s=korg header.b=w3D8jjsy; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231960AbhEaNo4 (ORCPT + 99 others); Mon, 31 May 2021 09:44:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:38860 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231576AbhEaNbH (ORCPT ); Mon, 31 May 2021 09:31:07 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CD0716140F; Mon, 31 May 2021 13:23:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622467395; bh=VAf0sI2V21i5ddVvKlfZEQ4IyqyOpRQptxraRFMNqhk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=w3D8jjsylVDH2JIdcUMusg3JOYELSURQwy2EmvusRfnmz7K7J/jYI/RXsgDxnwx61 75Nz/mTFkQX+njACgpcfmmpXd0GS6FH5g/Kl0u4AsW0weKJuOAFV60cz72hOW1CjM5 u/TQMYx3akxRPwawRNcuaL8jO9MbF1tWnuuDyMJM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Daniel Borkmann , John Fastabend , Alexei Starovoitov , Ovidiu Panait Subject: [PATCH 4.19 049/116] bpf: Rework ptr_limit into alu_limit and add common error path Date: Mon, 31 May 2021 15:13:45 +0200 Message-Id: <20210531130641.839723130@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130640.131924542@linuxfoundation.org> References: <20210531130640.131924542@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Borkmann commit b658bbb844e28f1862867f37e8ca11a8e2aa94a3 upstream. Small refactor with no semantic changes in order to consolidate the max ptr_limit boundary check. Signed-off-by: Daniel Borkmann Reviewed-by: John Fastabend Acked-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman [OP: backport to 4.19] Signed-off-by: Ovidiu Panait Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2731,12 +2731,12 @@ static struct bpf_insn_aux_data *cur_aux static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, const struct bpf_reg_state *off_reg, - u32 *ptr_limit, u8 opcode) + u32 *alu_limit, u8 opcode) { bool off_is_neg = off_reg->smin_value < 0; bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || (opcode == BPF_SUB && !off_is_neg); - u32 off, max; + u32 off, max = 0, ptr_limit = 0; if (!tnum_is_const(off_reg->var_off) && (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) @@ -2750,22 +2750,27 @@ static int retrieve_ptr_limit(const stru max = MAX_BPF_STACK + mask_to_left; off = ptr_reg->off + ptr_reg->var_off.value; if (mask_to_left) - *ptr_limit = MAX_BPF_STACK + off; + ptr_limit = MAX_BPF_STACK + off; else - *ptr_limit = -off - 1; - return *ptr_limit >= max ? -ERANGE : 0; + ptr_limit = -off - 1; + break; case PTR_TO_MAP_VALUE: max = ptr_reg->map_ptr->value_size; if (mask_to_left) { - *ptr_limit = ptr_reg->umax_value + ptr_reg->off; + ptr_limit = ptr_reg->umax_value + ptr_reg->off; } else { off = ptr_reg->smin_value + ptr_reg->off; - *ptr_limit = ptr_reg->map_ptr->value_size - off - 1; + ptr_limit = ptr_reg->map_ptr->value_size - off - 1; } - return *ptr_limit >= max ? -ERANGE : 0; + break; default: return -EINVAL; } + + if (ptr_limit >= max) + return -ERANGE; + *alu_limit = ptr_limit; + return 0; } static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,