Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp400929pxy; Wed, 5 May 2021 05:09:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmws36us5UAJxISYN6A5dCSg/C9mS53tBbBykoB4lgk+Vb39WdTxxQRqB8TbpvmOn7veIQ X-Received: by 2002:aa7:914e:0:b029:28c:48d6:b27c with SMTP id 14-20020aa7914e0000b029028c48d6b27cmr27173551pfi.79.1620216574279; Wed, 05 May 2021 05:09:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620216574; cv=none; d=google.com; s=arc-20160816; b=tpYic+OvEdjC58kH5ksmXWfoK0ZIo0+6inltdP2B+XIHnjchW4rYODV3uMt1wnBmym iiOGWk/Zi7paEWszrEw4j2ICanSNb3vo1Lebg4kW/s6gzE4qucH/W6mbqPmkfqj4OPKm P//LLKGGT2LyPNDCE8HclbgNTJytDQdrFEfY+Aq6lq7TMYNK64emla1g2QItqoAvWRwW s+47jh0rQJOdMeUAZwZ42k2RKqKvN5zp4RMr3p11iyLLlh9TiAYYdkRnPYv07vu5ks97 0EYWXvCeg3i5+7uwFUVITLX72KiGclfIiPA/kRrvkMgjxsmmBcXOT+nxKGk5/IvHSpMh 05bw== 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=sr10LtCCAHi8qMmH18gao0oSViZfgJun+8S0U5Ro+vM=; b=Qg0XLPvzrE8y6ZIDUQPgelRtNe+OcRuSl4Rn1jroU3dvWxxB+PeGsicFgbSdonqc02 8gs5iLtVacGl1/FqNBtNITjCTbVWgU+mKobE2qwOSMlvGVnMEPgUzHy1lNom8N0uXqIw rVqlpJ3dqM+AIGIdOK76DxL79uqTFMGPLcn8039kmNns4hK7CLVl/QMhhSGkeIbE4aJ7 Zl10B5/QKBAKvJ3rF08NkNIx3j2kmOZMUEuxwgjRZP+S3TlyqIp+yChIT8dhy1EBr1JU nfvzMuJX6w9t7IctYbIQFjdh/GnApNLqEZHP4guz/IN98JSnhZVfrW2iNVpogjdilsfo ft6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zB7i7RZL; 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 w91si7605964pjw.173.2021.05.05.05.09.19; Wed, 05 May 2021 05:09:34 -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=zB7i7RZL; 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 S233479AbhEEMHV (ORCPT + 99 others); Wed, 5 May 2021 08:07:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:46304 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233383AbhEEMGy (ORCPT ); Wed, 5 May 2021 08:06:54 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 29FD3613D8; Wed, 5 May 2021 12:05:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620216357; bh=7oa022Jfyd3u6Fh5ErzdA5kuhNB8T5+6oov5Pli7f0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zB7i7RZLEvbQT0AMlF7uG++lHHVoCcGXQtheLPZfrTFSkn2D/UDv5doAs/ZecXt7S nVWcOVkTcJUziuIhOXiXMc8d02GEBmvepotBJTZ9XUMX96HYrfgfFud/m0G0W1N5TE pOfH1czSCVQx00Sk0OJJFtlqA1pVnb472tKpiatQ= 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.19 07/15] bpf: Fix masking negation logic upon negative dst register Date: Wed, 5 May 2021 14:05:12 +0200 Message-Id: <20210505120504.015714232@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210505120503.781531508@linuxfoundation.org> References: <20210505120503.781531508@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 @@ -6099,14 +6099,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;