Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2621876pxj; Mon, 31 May 2021 06:46:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0UeShSsbjIOD/KXbHAzDC9+rJNjzP3HiXa5A75qt813SMWcm09obH1qv3QqJkLNw59gWn X-Received: by 2002:a05:6e02:1393:: with SMTP id d19mr17068068ilo.90.1622468798843; Mon, 31 May 2021 06:46:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622468798; cv=none; d=google.com; s=arc-20160816; b=rJlvbJwSx20dwZ8hGo7ndatwhUdvb6CEURSdARWkqMF2fnJRBldzT3nfK98/VbG3QY 07FEKsBzUqp5MPJstlGFR9V2JbY5vlEaYIBzccbRo81QeTG1hRfdyNfhsBWkRu53PbQ3 LRX0qvd8lo8Gty9/rPlEws445paudPdM/mIk3U1wGdaBvF6Qr27Bmd+K352cXwyyJfV4 A1gfoW6JAnX7DIFlkh49qIBrWw1DMAqW6u6XOBFT000G6L8ZzQ1Tr2a0O/HDNMEUYn00 AeevFnlpSilInkDjFndQQPYz54SG9TvcFrqJ8EEbNxIZCrNRBjhwaTj4FVdilFVM92uZ pvUQ== 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=0APUjeeYPJ3Uh6IsPatTH1xFTkLCkPiXblmYK3MmVAs=; b=RrEuueQAFqQWVeOTaKLAOUqVI/DNf+3G2d6qcMQiSNLYQHCeajGdjW7BjWX6UrmVP3 3LOtzFxh/hT+xV8pRKMkQs6nKw1Sxsmxb6xSLKUff1cEz+Q7BNRb1XUwktLKhYCl0MHA dVfOOVIzfV/7JwahXMSCc1jdRIBITqRDHiLFF6lq3aYBlNwr0thHt1TcSL53W8Gdd38q peG25hCiS2GMsPsVhlnoLYzlGFNaYJY1m/hvDQWHMYd2mKMSGpiCsjRJie8Ah5p3ljNL hWLBS4/zccEr0ajWdxTdptrbt8XKhaSgPm7h5KKkkpGzhv9amgkkBUKgAJcqCoEKy5G/ Ie/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oeEfACIa; 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 y17si14221698jak.57.2021.05.31.06.46.25; Mon, 31 May 2021 06:46:38 -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=oeEfACIa; 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 S230322AbhEaNpn (ORCPT + 99 others); Mon, 31 May 2021 09:45:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:39186 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232161AbhEaNbZ (ORCPT ); Mon, 31 May 2021 09:31:25 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0A25D613B6; Mon, 31 May 2021 13:23:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622467416; bh=vRqAwUqYtFJ1JEWXTPZFIsAOh+Kh2rbyILSszCFx2fw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oeEfACIamPmDRERrZIpYbCC+/ustdhrmWCF2YRlNFZRsRpnl9cxDmrLqJLn6U8F/D rjUHToqtT8mEFc5JN6CE8xLHv6t97oa/OA5Ke8r7hUkwDgz0PuMlXWnSqMO7oZi/5w JUYxUbNwVj14v/mi9I254UNRaihGqOyUIddK2+Jc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Piotr Krysiuk , Daniel Borkmann , Alexei Starovoitov , Ovidiu Panait Subject: [PATCH 4.19 057/116] bpf: Fix mask direction swap upon off reg sign change Date: Mon, 31 May 2021 15:13:53 +0200 Message-Id: <20210531130642.106387425@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 bb01a1bba579b4b1c5566af24d95f1767859771e upstream Masking direction as indicated via mask_to_left is considered to be calculated once and then used to derive pointer limits. Thus, this needs to be placed into bpf_sanitize_info instead so we can pass it to sanitize_ptr_alu() call after the pointer move. Piotr noticed a corner case where the off reg causes masking direction change which then results in an incorrect final aux->alu_limit. Fixes: 7fedb63a8307 ("bpf: Tighten speculative pointer arithmetic mask") Reported-by: Piotr Krysiuk Signed-off-by: Daniel Borkmann Reviewed-by: Piotr Krysiuk Acked-by: Alexei Starovoitov Signed-off-by: Ovidiu Panait Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2738,18 +2738,10 @@ enum { }; static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, - const struct bpf_reg_state *off_reg, - u32 *alu_limit, u8 opcode) + u32 *alu_limit, bool mask_to_left) { - 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 max = 0, ptr_limit = 0; - if (!tnum_is_const(off_reg->var_off) && - (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) - return REASON_BOUNDS; - switch (ptr_reg->type) { case PTR_TO_STACK: /* Offset 0 is out-of-bounds, but acceptable start for the @@ -2817,6 +2809,7 @@ static bool sanitize_needed(u8 opcode) struct bpf_sanitize_info { struct bpf_insn_aux_data aux; + bool mask_to_left; }; static int sanitize_ptr_alu(struct bpf_verifier_env *env, @@ -2848,7 +2841,16 @@ static int sanitize_ptr_alu(struct bpf_v if (vstate->speculative) goto do_sim; - err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode); + if (!commit_window) { + if (!tnum_is_const(off_reg->var_off) && + (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) + return REASON_BOUNDS; + + info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || + (opcode == BPF_SUB && !off_is_neg); + } + + err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); if (err < 0) return err;