Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp266660ybj; Fri, 8 May 2020 11:18:02 -0700 (PDT) X-Google-Smtp-Source: APiQypJziyu2KcKUgqR4EunOHJS83ItsjntkgLpW5nJkl5lIvZ0zvQWFoDo6v0aSsEpBcIB/jNSE X-Received: by 2002:a05:6402:1fb:: with SMTP id i27mr3470546edy.37.1588961882134; Fri, 08 May 2020 11:18:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588961882; cv=none; d=google.com; s=arc-20160816; b=kCbmpDxqjIU/XNvp9Zv4tqkWgTj0VQ1Ob72PaNXCK1hgKb/YIas8BHsjImH7HDuMBN khK62JUP5XDkVDpKB9Pmhz6Zm4xYnvTM+at5iSxOcDT4tYxmLZHZOQbSHG0KribU/0vw PZ7MW8IfS/OTX7Z3WNjEhdfYvTVAsLYWTbRWuuhq6bUHJs2ReAPO9GXc+fjNSxqKXqJN Dw/oXlZTEkdVxhS2MXzcaK5kujZBN7MNy8jnjxaeNDXkHrlQQ6xzYn860tWCPYloy5hr smf8oxZNOVS4N/5fZmY3UJqwHTA3bhL81tHOPKQdjBpbOSe8Wd7uJjFWNl9vbNogLe4y LodA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=+grFk/1b6m5GXQB6Do7/JZsbw7ECp6r+S8upQGe4+gI=; b=Kycd0IhqpSYyo8iiAh82ewLh0ATS/FWzbPnz7e77bocYju3it7bXbaqxpkb1ZFyZku KuiDCvayDlCBhpwyVaCdOSWfp+yRGhCN8ERLKHEqKi8iy+OYS+yUt9iOyvaFsG/aKSRu 6v7qA1ikA7/Emqf7fV5ySCmN5S57rSVBMkIUQTEAe0ZP2lnaoqrxF14jV458nzaHfWp0 5evabZygvHxmpxeJMkTThkWb4SNIcbwE4t7jq+rbO6n4FsybF1U07q1Ew/6MywKc7REK Vpk8VSEpi8+XQMOp7AQepttbNPw9Z/cT9IY8TVXi/BIEpgg8W1yXhDKR/aeMkWgfFRIa rWbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cs.washington.edu header.s=goo201206 header.b=I6sVrCye; 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=cs.washington.edu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d13si1428640eds.361.2020.05.08.11.17.38; Fri, 08 May 2020 11:18:02 -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=@cs.washington.edu header.s=goo201206 header.b=I6sVrCye; 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=cs.washington.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727838AbgEHSQF (ORCPT + 99 others); Fri, 8 May 2020 14:16:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727774AbgEHSQB (ORCPT ); Fri, 8 May 2020 14:16:01 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7925C05BD09 for ; Fri, 8 May 2020 11:16:01 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id mq3so4672761pjb.1 for ; Fri, 08 May 2020 11:16:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+grFk/1b6m5GXQB6Do7/JZsbw7ECp6r+S8upQGe4+gI=; b=I6sVrCyeN1Al9+YXglL0XwffqWho/yTDT1r7S/K6Apz4jUPalQFiMfGpHSI54ALlc+ V6dIZM6i+WLjQlh5ywJ5byFfTeg9EfIH49M7i84H/uOfLRN+vZzIfo59Q3OEXEIJQEsx 7x2tnn1OPyog3FsuhfzlEBVIKdmsUugJfu5W4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+grFk/1b6m5GXQB6Do7/JZsbw7ECp6r+S8upQGe4+gI=; b=sQnVcpqrJiMH4Vn3fwrxNZG8hdVLPnS/2AXMs17BUjE9BQD8D3Urwbq+4id9rK9RZD qNsh5P8nvf9s7NZpOHcD+GLNcpYJzEFGoc4QqQpLhZe+p8qRvFPL9GHMUvgOt/DG2cwJ RNwkJYHzG+bP0G6/CSa7Iza4OuaSdvYVbrM2p6VomoQi+L6h3WGc+rNDI2IGCvVAsTP7 9Vfhos26UqewZm4tziF0Lw86n0BL4khsPxR5WgjXp6Lwa6s8awb8XWhTsYES5ugXmqm7 MXQ3rQ0E4V+RMYEQlAhIeuSZJMuM3FoUwT16N/5pcdurNCRYGJiEkBfxskoGvEHog7Yw YERA== X-Gm-Message-State: AGi0PuZODnffijyNSZ10mR6PUOR1Rj3mhzZcCOcsF2wT4DpwcLBk7mID kNiJhFWvhglgo1xtvewWuN+XjA== X-Received: by 2002:a17:902:6114:: with SMTP id t20mr3762825plj.324.1588961760860; Fri, 08 May 2020 11:16:00 -0700 (PDT) Received: from localhost.localdomain (c-73-53-94-119.hsd1.wa.comcast.net. [73.53.94.119]) by smtp.gmail.com with ESMTPSA id e11sm2349463pfl.85.2020.05.08.11.15.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 May 2020 11:16:00 -0700 (PDT) From: Luke Nelson X-Google-Original-From: Luke Nelson To: bpf@vger.kernel.org Cc: Luke Nelson , Xi Wang , Catalin Marinas , Will Deacon , Daniel Borkmann , Alexei Starovoitov , Zi Shen Lim , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Mark Rutland , Alexios Zavras , Thomas Gleixner , Christoffer Dall , Marc Zyngier , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, clang-built-linux@googlegroups.com Subject: [PATCH bpf-next v2 1/3] arm64: insn: Fix two bugs in encoding 32-bit logical immediates Date: Fri, 8 May 2020 11:15:44 -0700 Message-Id: <20200508181547.24783-2-luke.r.nels@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200508181547.24783-1-luke.r.nels@gmail.com> References: <20200508181547.24783-1-luke.r.nels@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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") Co-developed-by: Xi Wang Signed-off-by: Xi Wang Signed-off-by: Luke Nelson Reviewed-by: Marc Zyngier Suggested-by: Will Deacon --- 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.17.1