Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp196429rdg; Thu, 12 Oct 2023 03:04:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH8D+jR9m1UKxhZGs+YtIpt9zgUjbxS/2wg5Fgz+f3Lhb7FV3fc3JirKllTQWpuSnsAThcL X-Received: by 2002:a05:6808:10d4:b0:3ae:32:e5b9 with SMTP id s20-20020a05680810d400b003ae0032e5b9mr28996276ois.57.1697105090293; Thu, 12 Oct 2023 03:04:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697105090; cv=none; d=google.com; s=arc-20160816; b=H8XzOPzbwph2bTZZ1NBmBWYfEYdbkSHLnM5vtoVpa+UBw3ePAwLo4yzoSxvMJaZJsA UmrKBVcTuJRKNog+LMQUYxqBKYFG1YWAIKgwH5js8eTPCSUEEhRmsQCndq2GT5nqr4Td TZ+nIg909b2QpxgxDRzfYzkLUxT8q63EkaFWnfY1CHPsFRq3lLU5EarUgPKYqAZUDAOH dOP20RsmUVKWzoNth2mHhAsNNR1boXiux8u+xShHKVs0qcs5k+QiNjP7vC2NkDpgUYF5 yZKxkU5UkqJVa1q/8VW+VWEE1FrkkwUVt2DZLoiCJgqoRTHI16+L94HVHJgDY/nzJlxo RUEw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=FvnW4GC/EA+hIluOOBcOWvhFVq1V+VziXSvUcjywg6c=; fh=TZ1FtIqeUfZfQ3GkbpRP0NuCIS8eHq7hqM2LIHeinyY=; b=SOzM2nWGxaWVaP41gP/W2fHEtEK3TxYTeqwHk5KDlsjMWoj0wtY9Ze0bRsZQpvE0k1 cPWUPPHU64aNyRazcIhdE5Og4tyFfl80EeaY3QSRqG7cC9CkOJ5R2LTuVkSPI8aVV6aY hUaPH81N+732qkYrYy22m3wpuXyRj4TkKc1yLNm7y5exPAZI4FpcaRPqBvD0rzChGXSA 7362xi17ZduojYzZ+iAgttGYrnRKhGdOg330BlTiLOYtFVs3ry59859bec5i3jaIk/PD 5f3SuVzOAiF7NdA63pitY/G4YcJvrF/GjKmfYQI1gtMvx+kTHyFall6DnG/4hKVdDk5l yzaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="HFpV/+3C"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id g126-20020a636b84000000b005655bf61e32si1901691pgc.23.2023.10.12.03.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 03:04:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="HFpV/+3C"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id ACA3C818CC9A; Thu, 12 Oct 2023 03:04:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235638AbjJLKEk (ORCPT + 99 others); Thu, 12 Oct 2023 06:04:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235618AbjJLKEi (ORCPT ); Thu, 12 Oct 2023 06:04:38 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F951A9; Thu, 12 Oct 2023 03:04:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697105076; x=1728641076; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=MYvAKDdK1BbhX247ponvSXkbWwL0F8DFfz3s+y5S/6A=; b=HFpV/+3CBDf04+aVkrABphGBAWZjNx8q25IMzStybLeiNreelx7+n+Gq jZR50QQEIM20CpK9+OfTaz6YB/k7zn4u/lsy0/hGI+y4CFD9RFUdPjv5/ nunh/62XYe/LsxFVSy/TYQzQDOLopsCRF5rEndkql02dOfKKK5y0ouggP upXryH1GyKTxIn3K3XqGcBQMYD/M6i3Z+29j5P9/jOqpsgsFX2sJVUJpV zntzp93yySzH4eLw8c7lRY3smu/VHTLD5uABXFQG9K2PcBqVMJEKY7qKk 9SAGin9aKdnAAUppCSC0zbQloq7J5oGKI9+SgmoEzXWNbEoUT0gx6aRTf A==; X-IronPort-AV: E=McAfee;i="6600,9927,10860"; a="388744141" X-IronPort-AV: E=Sophos;i="6.03,218,1694761200"; d="scan'208";a="388744141" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Oct 2023 03:04:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10860"; a="789338995" X-IronPort-AV: E=Sophos;i="6.03,218,1694761200"; d="scan'208";a="789338995" Received: from nmalinin-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.58.130]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Oct 2023 03:04:31 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 3466E10A1B1; Thu, 12 Oct 2023 13:04:28 +0300 (+03) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Peter Zijlstra Cc: x86@kernel.org, "H. Peter Anvin" , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Fei Yang , stable@vger.kernel.org Subject: [PATCHv3] x86/alternatives: Disable KASAN in apply_alternatives() Date: Thu, 12 Oct 2023 13:04:24 +0300 Message-ID: <20231012100424.1456-1-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 12 Oct 2023 03:04:47 -0700 (PDT) Fei has reported that KASAN triggers during apply_alternatives() on 5-level paging machine: BUG: KASAN: out-of-bounds in rcu_is_watching Read of size 4 at addr ff110003ee6419a0 by task swapper/0/0 ... __asan_load4 rcu_is_watching trace_hardirqs_on text_poke_early apply_alternatives ... On machines with 5-level paging, cpu_feature_enabled(X86_FEATURE_LA57) got patched. It includes KASAN code, where KASAN_SHADOW_START depends on __VIRTUAL_MASK_SHIFT, which is defined with the cpu_feature_enabled(). KASAN gets confused when apply_alternatives() patches the KASAN_SHADOW_START users. A test patch that makes KASAN_SHADOW_START static, by replacing __VIRTUAL_MASK_SHIFT with 56, fixes the issue. Disable KASAN while kernel patches alternatives. Signed-off-by: Kirill A. Shutemov Reported-by: Fei Yang Fixes: 6657fca06e3f ("x86/mm: Allow to boot without LA57 if CONFIG_X86_5LEVEL=y") Cc: stable@vger.kernel.org --- v3: - Summarize KASAN splat; - Update comment in apply_alternatives(); v2: - Move kasan_disable/_enable_current() to cover whole loop, not only text_poke_early(); - Adjust commit message. --- arch/x86/kernel/alternative.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 517ee01503be..73be3931e4f0 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -403,6 +403,17 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, u8 insn_buff[MAX_PATCH_LEN]; DPRINTK(ALT, "alt table %px, -> %px", start, end); + + /* + * In the case CONFIG_X86_5LEVEL=y, KASAN_SHADOW_START is defined using + * cpu_feature_enabled(X86_FEATURE_LA57) and is therefore patched here. + * During the process, KASAN becomes confused seeing partial LA57 + * conversion and triggers a false-positive out-of-bound report. + * + * Disable KASAN until the patching is complete. + */ + kasan_disable_current(); + /* * The scan order should be from start to end. A later scanned * alternative code can overwrite previously scanned alternative code. @@ -452,6 +463,8 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, text_poke_early(instr, insn_buff, insn_buff_sz); } + + kasan_enable_current(); } static inline bool is_jcc32(struct insn *insn) -- 2.41.0