Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp494403pxy; Wed, 5 May 2021 07:12:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOLmlGSQcUIAydsgp6UetqxGTHNBAbynV1oMRCUt/S+NKrhyfhd0yHqpcJUAI/3IGNix5N X-Received: by 2002:a65:6a07:: with SMTP id m7mr28649331pgu.136.1620223951011; Wed, 05 May 2021 07:12:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620223951; cv=none; d=google.com; s=arc-20160816; b=ENM0gA8NqAIwEEj+XRZUYoZ82eN2W2UoduCKNNJbNHofcPNFUz2Fqf4BjEFyp5dNOU vHEFi9FfdjtW6zZYY21NCUe9znP+vo7FockuYLXKyj4yElpeBOKJcYPR5lfALuiHpBMu eZ0fFGkn2+nVs+F4iQ/lqc1oD4/TahoQsoMNXhhMF09os3Qn/PVcbb0Okra7/CA7CPde wPKeX6gKM48wXiprE0UyniFF9aXf9HYAJZsp9wGMI39Wrv5QG1Ix+ymirGgpr4kphQMd bzaJ7wZdI2hC2fDLCeLtSHy5a1eC8eEXPv0rEARkOY9J7foYiyioKQ4uftEqvEySm9rH 9WhA== 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=MZN22IkTiAB21BjG4cCpBH/sUnpDYsXuz+AqHMjthDQ=; b=a0a63rAYpYwPs5qMJbNQb9YL934kRicojtqX1iYDxvtCFjdwxHyjHymnYsGLuWiw5I 0XqxTpMaJDMW5d9csL8NeRXzPdCEx9G4DeiqDhO6VPpKLmtZ0fs9lQt04Gf6J2hCiQeK fLNsYtr070RGPoEo7W12WNuu9P19/MatpcVGfS57g/XC8K3xLXCG1bbJH1R/nrCtmuG6 HrF6Ya75EW8OO6o0weczGo8Zi6c66E8+YmfEpzWXTCCrJXLQB/te/ra9KgZNuEXsmniO w+SAp231OxpiuXZVhiXyEtgQxxidTSmuUjpK4FE4FzSrMXSvX5XTdlus9A1axUjF/yz/ F1uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uw83iOjt; 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 s185si4081229pgb.277.2021.05.05.07.12.03; Wed, 05 May 2021 07:12:30 -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=uw83iOjt; 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 S233813AbhEEMNr (ORCPT + 99 others); Wed, 5 May 2021 08:13:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:50940 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233612AbhEEMKA (ORCPT ); Wed, 5 May 2021 08:10:00 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 00C5D613C0; Wed, 5 May 2021 12:09:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620216543; bh=eU2sDmOduaakX+v5s3OrSYnhsI/duPUm2BLsnhaQZrU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uw83iOjtBOIenM6qCzOAnwpI8Qm3ixmTvXm43zMZafQXD2JqjjFU5Sbif1yMvValO 8vsToB2JEYUuS8slx+x/PhuymF2uOd/t0tJVjqkXjvbLcGpcKHV3zlbCDiqDlfM69e Ls6wjuZ8mVUhj2+6gCn8GqQLQAPBjBLUgINrUs5o= 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 5.11 06/31] bpf: Fix masking negation logic upon negative dst register Date: Wed, 5 May 2021 14:05:55 +0200 Message-Id: <20210505112326.877642789@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210505112326.672439569@linuxfoundation.org> References: <20210505112326.672439569@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 @@ -11543,14 +11543,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;