Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp120718ybg; Mon, 8 Jun 2020 18:11:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpg7ED6tzcW/hZxhS6EA1G8WGQViFsgqQ2jNVW3/A+dYJmEu152wuaYDMWH9g+im5dQGWv X-Received: by 2002:a50:fe94:: with SMTP id d20mr23771412edt.254.1591665084486; Mon, 08 Jun 2020 18:11:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591665084; cv=none; d=google.com; s=arc-20160816; b=dqSmu9awbFL5NGewKjaOM/vQhIs5RojYd06HlGVNdPIYoKHPc2Zc7GqNtF3qnxWjc/ iYdO7zb2AFbP1z8RUZb7q/r56zDOOvI7Dwzamc2g6QaLE6auDQ1DL7jf1J3c8xYsb1g2 Fz2F6QEE/fzWekq+G64PY19lhodJ97MqKMCY3DYJDlL+dSpPfwvxxmPlmhGHQNrcEMDH C2UnpEJpre2PRlMXMTZhlMe2lVJVe9oxCGOU9wZn5+ycK8WVgybYX2thNGV8VHQOAvCU XWO7MxTS0Mf2xAzOi2Zt5YPCVDFJzwQzNM8sQbjinOcT3K/RTdzlhJ9HrFcrCwC/lVja 8HTg== 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=WcWZGq2pcGXPwZ9xFewoGbvR0La9loWKwZmwAy1oNQE=; b=J8d8SbiK4iRqecytszfTjAv33uOFiTquk17a6be+6ubh2x4+h7hNwHfvYIWxGD518I a3jcEh5IufaYpXpnkCcHUSQv8jxBwxmfESdtl6yKd04L27rvI59WoxcHC8vGoGY1vak1 k3qBbSb8GE/qnelrFQakPTMYSU0HEKoTvgXX6fF3Q+NQDU8c3f4ebBOE24cs0/XIbYHA Q335gdFEDf1zy4gZDgETNzIdIbprSLxj353FvQvmn72cd0VaXxJ9BVlGkKyR5R0a+WPS CjyvpGnw0gtWhjRA/V5DGIif76myW3IQt5llwTuw1JmGZ1W5oH3esyfgDtCnQ4I6P18V n9fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="EoJ/gV1E"; 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 y12si9891010edt.9.2020.06.08.18.11.02; Mon, 08 Jun 2020 18:11:24 -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="EoJ/gV1E"; 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 S1730331AbgFIBHY (ORCPT + 99 others); Mon, 8 Jun 2020 21:07:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:50600 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727768AbgFHXHA (ORCPT ); Mon, 8 Jun 2020 19:07:00 -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 271BA20823; Mon, 8 Jun 2020 23:06:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591657620; bh=m4xrXkotUS3ZKETk0RvSF7L9HwTMlQwabdrk1sTDb3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EoJ/gV1Eodkl7IO9V4Jz4R369oX4u/y57UmrMNcfIcB0Yay7je4OQj/b1uU5Dnqk9 1025n+2b2Q4rcwPWmJUtrDaD0xjenkQeUx7WJ/DOtlqmQvS+Vex6xCXmGYuJC9i295 /Hwr+Fk5QqbuIJbaohVB0B9VGbFLbcq7GFxe+new= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Luke Nelson , Will Deacon , Xi Wang , Luke Nelson , Marc Zyngier , Sasha Levin , linux-arm-kernel@lists.infradead.org, clang-built-linux@googlegroups.com Subject: [PATCH AUTOSEL 5.7 041/274] arm64: insn: Fix two bugs in encoding 32-bit logical immediates Date: Mon, 8 Jun 2020 19:02:14 -0400 Message-Id: <20200608230607.3361041-41-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200608230607.3361041-1-sashal@kernel.org> References: <20200608230607.3361041-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 579d1b3faa3735e781ff74aac0afd598515dbc63 ] This patch fixes two issues present in the current function for encoding arm64 logical immediates when using the 32-bit variants of instructions. First, the code does not correctly reject an all-ones 32-bit immediate, and returns an undefined instruction encoding. Second, the code incorrectly rejects some 32-bit immediates that are actually encodable as logical immediates. The root cause is that the code uses a default mask of 64-bit all-ones, even for 32-bit immediates. This causes an issue later on when the default mask is used to fill the top bits of the immediate with ones, shown here: /* * Pattern: 0..01..10..01..1 * * Fill the unused top bits with ones, and check if * the result is a valid immediate (all ones with a * contiguous ranges of zeroes). */ imm |= ~mask; if (!range_of_ones(~imm)) return AARCH64_BREAK_FAULT; To see the problem, consider an immediate of the form 0..01..10..01..1, where the upper 32 bits are zero, such as 0x80000001. The code checks if ~(imm | ~mask) contains a range of ones: the incorrect mask yields 1..10..01..10..0, which fails the check; the correct mask yields 0..01..10..0, which succeeds. The fix for both issues is to generate a correct mask based on the instruction immediate size, and use the mask to check for all-ones, all-zeroes, and values wider than the mask. Currently, arch/arm64/kvm/va_layout.c is the only user of this function, which uses 64-bit immediates and therefore won't trigger these bugs. We tested the new code against llvm-mc with all 1,302 encodable 32-bit logical immediates and all 5,334 encodable 64-bit logical immediates. Fixes: ef3935eeebff ("arm64: insn: Add encoder for bitwise operations using literals") Suggested-by: Will Deacon Co-developed-by: Xi Wang Signed-off-by: Xi Wang Signed-off-by: Luke Nelson Reviewed-by: Marc Zyngier Link: https://lore.kernel.org/r/20200508181547.24783-2-luke.r.nels@gmail.com Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- arch/arm64/kernel/insn.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/arm64/kernel/insn.c b/arch/arm64/kernel/insn.c index 4a9e773a177f..cc2f3d901c91 100644 --- a/arch/arm64/kernel/insn.c +++ b/arch/arm64/kernel/insn.c @@ -1535,16 +1535,10 @@ static u32 aarch64_encode_immediate(u64 imm, u32 insn) { unsigned int immr, imms, n, ones, ror, esz, tmp; - u64 mask = ~0UL; - - /* Can't encode full zeroes or full ones */ - if (!imm || !~imm) - return AARCH64_BREAK_FAULT; + u64 mask; switch (variant) { case AARCH64_INSN_VARIANT_32BIT: - if (upper_32_bits(imm)) - return AARCH64_BREAK_FAULT; esz = 32; break; case AARCH64_INSN_VARIANT_64BIT: @@ -1556,6 +1550,12 @@ static u32 aarch64_encode_immediate(u64 imm, return AARCH64_BREAK_FAULT; } + mask = GENMASK(esz - 1, 0); + + /* Can't encode full zeroes, full ones, or value wider than the mask */ + if (!imm || imm == mask || imm & ~mask) + return AARCH64_BREAK_FAULT; + /* * Inverse of Replicate(). Try to spot a repeating pattern * with a pow2 stride. -- 2.25.1