Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp597892ybt; Fri, 19 Jun 2020 09:02:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3Nz7cX2Oov0V2guLDEF8qfkDpjMTSNTk+CgL1A2F/hV0XA+oRBFIZR60pZfSG6H6Es5zb X-Received: by 2002:a05:6402:1fc:: with SMTP id i28mr4143855edy.63.1592582521464; Fri, 19 Jun 2020 09:02:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592582521; cv=none; d=google.com; s=arc-20160816; b=Fz7/arRSet4FHlrUl8FsxIQ3zt0CQcYZYtJ/JIXE3IpjAilSHbR+79x3RqUs2qM0fw aJyloLAZdeMOI4gIexigt1xdAGifprpDrUt965E4I3sEPo072Xut3uGC1tdPiaAmiWxU nkb04F2ak40VoJLg8g8aH9jDH66NwDG8R+0FkcPsX+M81r5lKMHCoUMwCpmPGLXIezMB M+264UO9MoKJ9tp/ePlDsb/A3uS1LxG1oU9MNdRu2RgvuprMBLi0TW6lwP1GEHpbDYwB y3fzWI64ojau4DUbSdqlX3A47HFVR7tkI+y8QqWgInyPxWlxnLufFnDadtJSLIN3q/8L FPqg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WcWZGq2pcGXPwZ9xFewoGbvR0La9loWKwZmwAy1oNQE=; b=QsVqQYYot9N6P90T8f3NiHBy1uWoY6g5HnQ7mlHAq+iGBN1M7s3cfTErHALxMVNNVL 1OhjeJ0xyHGesV/3KY6m8E7OPapx8rrBtaAS1m6L+Chk4Kk3BLiPhpnvEkjsNekcwpKK dpR72yRkJA3FdxBkOJf4b6guqEm+CX4VKb/XYt/IJr5WXf3Mm83akMou8mXqyO0Rsgo2 XR/GybTsARd4HAgfZ0A/913V46O8lZiBNBVfus/jOyHlmzDunOQ/xcivBrXUXIUHIDJt 8XwRG6W3LeivVb8g2JdKv6TL3mihEavXCucSGpXgyi6/yChVJzE6B6VPyrKwDcBMhR7P 68bQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Jzp7puPs; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x11si4159551ejj.420.2020.06.19.09.01.38; Fri, 19 Jun 2020 09:02:01 -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=Jzp7puPs; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392576AbgFSPTU (ORCPT + 99 others); Fri, 19 Jun 2020 11:19:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:48626 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392577AbgFSPR7 (ORCPT ); Fri, 19 Jun 2020 11:17:59 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9BDF22158C; Fri, 19 Jun 2020 15:17:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592579879; bh=m4xrXkotUS3ZKETk0RvSF7L9HwTMlQwabdrk1sTDb3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Jzp7puPsJHsjtDdq8V2vNZ5xzH2k+GTzC/ogg6yVTOSYcSQ7ST0JajUbCmE84hK13 pZm7PAhef++j0hAYVLoEmv6lXZSx8pJjfdeA8NtjrlIwpQiXCke9mCifcz7X2doBFD uMHBsgIWqiJFoOA+jGEaQR3RrdHRKgZP9l8gWD78= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Will Deacon , Xi Wang , Luke Nelson , Marc Zyngier , Sasha Levin Subject: [PATCH 5.7 038/376] arm64: insn: Fix two bugs in encoding 32-bit logical immediates Date: Fri, 19 Jun 2020 16:29:16 +0200 Message-Id: <20200619141712.158603528@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200619141710.350494719@linuxfoundation.org> References: <20200619141710.350494719@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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