Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4328305pxj; Tue, 8 Jun 2021 11:37:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyLKko9yaigSyBP1w3oRrrjofFh1GMDyUYcCdSd1sGh631hV1WXh9w0101rK1AmKcohlXu X-Received: by 2002:a17:906:a0a:: with SMTP id w10mr25111509ejf.416.1623177428717; Tue, 08 Jun 2021 11:37:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623177428; cv=none; d=google.com; s=arc-20160816; b=JawJKeABzb1gyjI3peNMEJWvxcZ0sWBYsyLpoRUNbq5j7T7M38RiBgfCwYxHJDoe0K NpG4vgzCU/3uhbkzq9/WMDSaV/7Q+Yv1FURkznFWyRYBtKEoEOKzczDLd8LJiw1fV9bG rV0xNUASnd8mISahBa3TX1+k6J7nCBcJpFAwb37QuG3vY3D5HBjDlrJyU5eEJgGXnbNB 9RyZSMQvNpvyay3ZqsuowgdsgiLq7nE7+05Q6q9YtTYnC+nNVXhsnqeTVPiLB6x4qCjF XYLtnyzwObF0+OThYwHXNZceNnd5Y5OnOwcbfhglNUwOu0jmf+Z9ln+9uszIxy2vUVeW VuEw== 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=BkUxK59cgIIncpMWNSjJ30gAxRVB7E5DTdVm7tieF3M=; b=c0Dq0s5CNNquKaMRgMK4GqiMr+MUeO42OGRxjme4jyd0sQJiTd6q1Jg/Vk16J5hF4r vo+0WxVX6jA75uBt3kTToxoTMTPqLZ8/3I1FWhOzJUMemK1jSzNCpkuV5jAENrtLk08J +j3BVGY3O4AS3mzkDA+yQf1bP6aB7aDADKrKLZdq9YH+aWywKiarZ8568DhVoIIFlV/Y 9VELk7VXvWaJLrtHSwaWyAQxpS8uAUbitMBYu1D23LvNPTM9fXpIM74xy+iQj8QvS9sC EcrjPfSRM2B/AkCuvo60d4msz8GeQSewMm7Yo/V/ArfIR2OFqijTSOrYwqie8a5tvi4+ L3rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Upx8F3+e; 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 h9si339056edw.186.2021.06.08.11.36.44; Tue, 08 Jun 2021 11:37:08 -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=Upx8F3+e; 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 S234390AbhFHSgA (ORCPT + 99 others); Tue, 8 Jun 2021 14:36:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:58040 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234700AbhFHSd7 (ORCPT ); Tue, 8 Jun 2021 14:33:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 46F6C613D5; Tue, 8 Jun 2021 18:31:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623177093; bh=xSZSnl7m2UGgysvLCU5ppFhx0YrUxXVXZlphh5Nb7fE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Upx8F3+eoSBGMYzCyru4w4rl+lE8Jf9JSNrWF+Qzi6i+XjEb2V7XKtVZh6fA/tS1V pw8fcLBWv65WS3icz7f7usAdKnhrKEDv1pCkBO6XwI/HVh/2awfAikt74sFWmtJmxS iKFHVH+PN546TXdNEuWCWcZ51oYVGzmTcl1u3gpg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Daniel Borkmann , John Fastabend , Alexei Starovoitov Subject: [PATCH 4.14 30/47] bpf: Rework ptr_limit into alu_limit and add common error path Date: Tue, 8 Jun 2021 20:27:13 +0200 Message-Id: <20210608175931.466685613@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175930.477274100@linuxfoundation.org> References: <20210608175930.477274100@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 --- kernel/bpf/verifier.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2026,12 +2026,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)) @@ -2045,22 +2045,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,