Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1881797ybz; Thu, 30 Apr 2020 07:09:35 -0700 (PDT) X-Google-Smtp-Source: APiQypKruo6Ko1PhN4IQKzM/c6w3qFNyNMH/Jin0v86qhBPzOty/aBPsZgw9nyFjAJmCK2AzW2S2 X-Received: by 2002:aa7:cd01:: with SMTP id b1mr2886710edw.163.1588255775511; Thu, 30 Apr 2020 07:09:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588255775; cv=none; d=google.com; s=arc-20160816; b=IokQwTCJ1GJmNwW62U0qdWGMua2Y3KYZmX6aLwkjRcdULk4BKJw2+tjhzd2Y5DVjqc mX9Gvp7HeA0opftzn1XP5iic5UPuFAyPKeji7Y66y1hICo6n/tf7AVPXQDdJ0Jaamymb ieaVr3hbkpzc6urHkNOpI2+aT6eXfgnhvtcaNa5pm3tpoQgPR4YvuNnj6rIgXG9t0f5T t8oppxAvSooFIh3OKFB+zinPxq3zTgIGNU9+P0nbG7Sfy0NH8NFslJyOhz+wCE8v+2Ob X4+wOKRdRQoq7RYM6BhB2Eesipemo423masZQgHgGjR0/zyHJP/jDyzjs4K/2ZvJ9uVx Q6Gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ze+4v5vdp5dJEhPsHKHdr35+cpq5/bcbSo3AHNsH65g=; b=KEdkBaXP9Tsw3tZf7UBHXtBYGA4Ik2ARpAf1K6qgfkPBusQGVyfccJd8e/wpR+EqON KytuyFqfmnfbIkSXHayvDTSgcuYliYVG0zQs8DTdLxcAWLnK5odJBYohIZX81McBSTDb uZx+8C5zCLPiuDFOUpVL3e+nqs6LiELZnbbRm4aAUSMLvvdtLLWkSBBhPfaWln3L2PvC VkLcwVe6bf+cRsuowMQ3Nuv5as3uu57rCHiEq2iVpOmc+mdQWZB6SQztscivCpLldCtk BdvzLieNWRGUqCUHJFbOnupldweoXb3Hmd2w/lCp7iFsDK2yo/oLlLRgdVC7qFu3FqKx g19g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ptH+TACq; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b24si5391851edr.338.2020.04.30.07.09.09; Thu, 30 Apr 2020 07:09:35 -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=@kernel.org header.s=default header.b=ptH+TACq; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729489AbgD3OEN (ORCPT + 99 others); Thu, 30 Apr 2020 10:04:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:35072 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727840AbgD3NxV (ORCPT ); Thu, 30 Apr 2020 09:53:21 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B765224955; Thu, 30 Apr 2020 13:53:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588254800; bh=4bu4rfAJxuAOnrRC2ZlHCtAsDKCEP4KUPZLuuFxDVDI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ptH+TACq5TkEMVNVy9xYMgmiP1zfYDG80p7x+AQRAISosl/h6YaXt5taA06SV8UOg 9mNmZ2KImLtTsd6/594tr+eVPxw0slSOnDigYJSEXU0HX6rIyOnWmrwkFY/XRnPluT XWMSeOxwLIEsS2+l1noWnsXQOUvyHjq3pnNjqkDg= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Luke Nelson , Xi Wang , Luke Nelson , Alexei Starovoitov , "H . Peter Anvin" , Wang YanQing , Sasha Levin , netdev@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 55/57] bpf, x86_32: Fix incorrect encoding in BPF_LDX zero-extension Date: Thu, 30 Apr 2020 09:52:16 -0400 Message-Id: <20200430135218.20372-55-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200430135218.20372-1-sashal@kernel.org> References: <20200430135218.20372-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Luke Nelson [ Upstream commit 5fa9a98fb10380e48a398998cd36a85e4ef711d6 ] The current JIT uses the following sequence to zero-extend into the upper 32 bits of the destination register for BPF_LDX BPF_{B,H,W}, when the destination register is not on the stack: EMIT3(0xC7, add_1reg(0xC0, dst_hi), 0); The problem is that C7 /0 encodes a MOV instruction that requires a 4-byte immediate; the current code emits only 1 byte of the immediate. This means that the first 3 bytes of the next instruction will be treated as the rest of the immediate, breaking the stream of instructions. This patch fixes the problem by instead emitting "xor dst_hi,dst_hi" to clear the upper 32 bits. This fixes the problem and is more efficient than using MOV to load a zero immediate. This bug may not be currently triggerable as BPF_REG_AX is the only register not stored on the stack and the verifier uses it in a limited way, and the verifier implements a zero-extension optimization. But the JIT should avoid emitting incorrect encodings regardless. Fixes: 03f5781be2c7b ("bpf, x86_32: add eBPF JIT compiler for ia32") Signed-off-by: Xi Wang Signed-off-by: Luke Nelson Signed-off-by: Alexei Starovoitov Reviewed-by: H. Peter Anvin (Intel) Acked-by: Wang YanQing Link: https://lore.kernel.org/bpf/20200422173630.8351-1-luke.r.nels@gmail.com Signed-off-by: Sasha Levin --- arch/x86/net/bpf_jit_comp32.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/net/bpf_jit_comp32.c b/arch/x86/net/bpf_jit_comp32.c index 4d2a7a7646026..cc9ad3892ea6b 100644 --- a/arch/x86/net/bpf_jit_comp32.c +++ b/arch/x86/net/bpf_jit_comp32.c @@ -1854,7 +1854,9 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, STACK_VAR(dst_hi)); EMIT(0x0, 4); } else { - EMIT3(0xC7, add_1reg(0xC0, dst_hi), 0); + /* xor dst_hi,dst_hi */ + EMIT2(0x33, + add_2reg(0xC0, dst_hi, dst_hi)); } break; case BPF_DW: -- 2.20.1