Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2576890pxb; Mon, 19 Apr 2021 08:47:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYclz3M3d7OwzTfbRbYD9WZhmIfjh0NDJvjl9JMHXNYH5TnpwdGRDvBDpszqXZ5j4ZqQH+ X-Received: by 2002:a17:90a:4e46:: with SMTP id t6mr3581931pjl.236.1618847264467; Mon, 19 Apr 2021 08:47:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618847264; cv=none; d=google.com; s=arc-20160816; b=sjMxtoUVKQzCbkLGF5w3QN6inNU4C0HwCZBNldr8maWoI8jGJ6I4Jm/eJQ4Q4msduL SUlbtCPUqXj+4c4ZaaQLmwachUwDrZLcQq+1cF/sTiNSHjhpda3BDmuWTa6Eo2eLgItg 84P6tDerGLvtEijf8GcEp3XumZNGAUQ3DyGK/ohf+l4n1CXLSfrZvcZr+/AWZ40M8X+M fO2ZRJMILTFo9GUERvDaCnEaDKgV++KjwaJKBUrW8NUuzIGTzo7r3JPvmfk9k1vjtCVk 27PIVosnPWC/SVpfUwVj7YzCnMNRr2YQXmh5waEg/9gJXuOfIf7Atssao9ZqorCfyD/X KStg== 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=V6LO+msKPOIz/MjS2FYSryjY6+43vnPDXOA6XHLuTbc=; b=PWbj3WJjRjdOFlXNcIAHxxscDWr9/lN2oi7N9ujdoKHsf7+awANvYvylE+KuJrQOsv yOJiXPQ2AwgjhfvYoI8Mb+BbHX9wwRe1Sll5kBm8hqYYAyCpkk/EdooF0EaGIiluJlP7 nzBCwxf4EfXJmnIU73qL6nzssI7wLG0tQRAgoimnUcVugjcEKgCluL/qyNFXc1SRHAFh F5JW95EtbablIKk6RaAs5KZLKREz5iOcrC1hS0AKdF/Ejv7YtL3EtQ1i4cIQ0lrgh3/o yv72F18duye7IavEIDIS1r9FShRze6DHBHhdzvN4KV1aKW5/aOJX7/n7Tjy/QFMN8Z4j 1R3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oVgPkHY+; 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 5si7560595pli.426.2021.04.19.08.47.29; Mon, 19 Apr 2021 08:47:44 -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=oVgPkHY+; 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 S242832AbhDSNaA (ORCPT + 99 others); Mon, 19 Apr 2021 09:30:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:56988 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240474AbhDSNU7 (ORCPT ); Mon, 19 Apr 2021 09:20:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4E548613F2; Mon, 19 Apr 2021 13:17:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1618838252; bh=AsrEI+qowd99zURiR490fpGnJMWtfKk9IPHL+q5qYb8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oVgPkHY+nbUvqep8SJdiHdUPP8AWIblUKda3J2BmPdDNlLVEkqWlHpDgJmiwaKvDy RoG3VOXrDxBFyCsdFEVLdT+2fS32JBI0dPzBF1pkXQlwVCBxbLiC4WSmJZ/e0s8vNz jXxSHyYSnnatOeC6BRRz00RExgXdkepLQudpY2PA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Borkmann , John Fastabend , Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.10 097/103] bpf: Ensure off_reg has no mixed signed bounds for all types Date: Mon, 19 Apr 2021 15:06:48 +0200 Message-Id: <20210419130531.117728799@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210419130527.791982064@linuxfoundation.org> References: <20210419130527.791982064@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 [ Upstream commit 24c109bb1537c12c02aeed2d51a347b4d6a9b76e ] The mixed signed bounds check really belongs into retrieve_ptr_limit() instead of outside of it in adjust_ptr_min_max_vals(). The reason is that this check is not tied to PTR_TO_MAP_VALUE only, but to all pointer types that we handle in retrieve_ptr_limit() and given errors from the latter propagate back to adjust_ptr_min_max_vals() and lead to rejection of the program, it's a better place to reside to avoid anything slipping through for future types. The reason why we must reject such off_reg is that we otherwise would not be able to derive a mask, see details in 9d7eceede769 ("bpf: restrict unknown scalars of mixed signed bounds for unprivileged"). Signed-off-by: Daniel Borkmann Reviewed-by: John Fastabend Acked-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/verifier.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 2eaefd9c4152..a2a74b7ed2c6 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5329,12 +5329,18 @@ static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) } static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, - u32 *ptr_limit, u8 opcode, bool off_is_neg) + const struct bpf_reg_state *off_reg, + u32 *ptr_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; + if (!tnum_is_const(off_reg->var_off) && + (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) + return -EACCES; + switch (ptr_reg->type) { case PTR_TO_STACK: /* Offset 0 is out-of-bounds, but acceptable start for the @@ -5427,7 +5433,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env, alu_state |= ptr_is_dst_reg ? BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; - err = retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg); + err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode); if (err < 0) return err; @@ -5472,8 +5478,8 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; - u32 dst = insn->dst_reg, src = insn->src_reg; u8 opcode = BPF_OP(insn->code); + u32 dst = insn->dst_reg; int ret; dst_reg = ®s[dst]; @@ -5521,13 +5527,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, verbose(env, "R%d pointer arithmetic on %s prohibited\n", dst, reg_type_str[ptr_reg->type]); return -EACCES; - case PTR_TO_MAP_VALUE: - if (!env->env->bypass_spec_v1 && !known && (smin_val < 0) != (smax_val < 0)) { - verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", - off_reg == dst_reg ? dst : src); - return -EACCES; - } - fallthrough; default: break; } -- 2.30.2