Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp403456pxj; Thu, 20 May 2021 12:01:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzY17Bthul9lQ11yPoEABB7kurt7EdNI2Z4my8xzr/cl7lI4r/GcKdgOWLj60WI/okysayf X-Received: by 2002:a05:6638:3010:: with SMTP id r16mr8167777jak.126.1621537292802; Thu, 20 May 2021 12:01:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621537292; cv=none; d=google.com; s=arc-20160816; b=DFpAk40jSu9QuSZkHNYt8rynBUvOS2TBxa5t+cqVmI6dYw1bp7pdS5GRqkrjSmr/5n kEj3Xy2pTDX+0DZydURHeVDtRu6I0AvbwttFTs1M0U1Bi7v7t4oofG/K3NBvZcnGtsdF 35apyS8n0fkjn+PPv2vI7lmKAVoUK8bbRoovJujMCiyq9jlkP8FYtPR4ADvugtO3nHZR EHQnfEHbNsZ30IHBwlKoGKUp8wht77hh1SyLaxPhODy1xQW/MSKpqHE3GQdGVyRmv/7w Jw7kHRMEyFgGW9XC/860BJvAIxYX4KAXENXfF1nzS1RlP/t6G674b9bWivWy/UW8PpvG R9DA== 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=7UqY+pI3zhZlHdxNL2pk25Ecv3lNlnenZARh0pMv8pM=; b=hCyejWUuM+kKmrsKfluvNBnE1qrmZGMKBdLcEsFG3r6mrYFrnpntSBDYBwWleWmyPU F7gw523ODeygtkv1rQpCwNwrwpV9pNbMYr+hL1WKOPT1BFVUxJktWyd8QbEh6F3wq6EB ZjyHR40w0nM0DpdxLl2zs9aOtf1yTfIvuSRC9tnga5Umwfsgl6/pjc5dpodzxJXvG+f8 KFTWDUTTNPeYiyqzN9GscTziZa5bb6W2GXkZddMNrrtj73/jc873PfuOUwseJvF3QGOo DlAzareYjQFc1Gjsg+9akFEqOGMe8Miiy8YHxScmST9iFQcoUSJllV/3a+Yf0UwWMRrt yLCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DIHHPNt5; 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 g10si3490863ilf.140.2021.05.20.12.01.17; Thu, 20 May 2021 12:01:32 -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=DIHHPNt5; 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 S236123AbhETK1N (ORCPT + 99 others); Thu, 20 May 2021 06:27:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:47008 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236380AbhETKPF (ORCPT ); Thu, 20 May 2021 06:15:05 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 25F9661988; Thu, 20 May 2021 09:45:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621503914; bh=0zuttgrd05FXqtI5INQETtbr+QYI9ZKIz/LtNQ5g3sU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DIHHPNt5n4/NinMU3sUYYjQ2Ppk8uk68LMLanfvdYs60/A9bncXqDpbx+ur05ga7h 4Rr84PTnbw7Nj07goZOdCRiilFb2vzLYPCXwntvmc0H1qZMiSaTMKZOBV/yPUlJw6H AhlIMEbAganSo0nHakyX5BxhLGsn7TnhHyKZx/L4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Borkmann , Piotr Krysiuk , John Fastabend , Alexei Starovoitov Subject: [PATCH 4.14 011/323] bpf: Fix masking negation logic upon negative dst register Date: Thu, 20 May 2021 11:18:23 +0200 Message-Id: <20210520092120.502093886@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@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 b9b34ddbe2076ade359cd5ce7537d5ed019e9807 upstream. The negation logic for the case where the off_reg is sitting in the dst register is not correct given then we cannot just invert the add to a sub or vice versa. As a fix, perform the final bitwise and-op unconditionally into AX from the off_reg, then move the pointer from the src to dst and finally use AX as the source for the original pointer arithmetic operation such that the inversion yields a correct result. The single non-AX mov in between is possible given constant blinding is retaining it as it's not an immediate based operation. Fixes: 979d63d50c0c ("bpf: prevent out of bounds speculation on pointer arithmetic") Signed-off-by: Daniel Borkmann Tested-by: Piotr Krysiuk Reviewed-by: Piotr Krysiuk Reviewed-by: John Fastabend Acked-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -4816,14 +4816,10 @@ static int fixup_bpf_calls(struct bpf_ve *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); - if (issrc) { - *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, - off_reg); - insn->src_reg = BPF_REG_AX; - } else { - *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, - BPF_REG_AX); - } + *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); + if (!issrc) + *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); + insn->src_reg = BPF_REG_AX; if (isneg) insn->code = insn->code == code_add ? code_sub : code_add;