Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3421790rdg; Tue, 17 Oct 2023 14:26:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHWq1T8//ALGDEUOwJZLAS3ypxCQFlCN+7DqbTmPpsz9L7ToqxPK1yE3NDbQFzduaryKrD5 X-Received: by 2002:a05:6358:3a17:b0:166:e6e1:7ea3 with SMTP id g23-20020a0563583a1700b00166e6e17ea3mr1976728rwe.22.1697578014287; Tue, 17 Oct 2023 14:26:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697578014; cv=none; d=google.com; s=arc-20160816; b=eK/eKGDN7tzCf/e3N+/EVEVTRBooJdPuCYAHQhfANWLU+79crEKUOZhSepUp4vjDcg BhZnMWbEh5DOl8hfx3QQYJSLPO1kllYDNZduh9mZhlG426JndRQ9QA5HZm3ycvgqG8Yu Q6yUHJauedWeiem++8EkSPTytt1wzbQHC95tOjc2iFwnIFlpI0gteg0mqXNPMMAfa2lb JLayyYax/6spTyuzoXZzK9Z6t2RIV/gr36cph2Bb+k6o3vJPq91h81HDGljfDQwObUTq dMWfJkYqqhcrVL+v8cp1+KBaypOoLfrdj+VNnCIg4uduzwd9tQsbbp6uAsWrbCZ5wtcI s/vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:date:mime-version:references:subject:cc:to:from :dkim-signature:dkim-signature:message-id; bh=VEdToj6uFLFPCubRLaSXMguz5WtO049V21/nw5QnioQ=; fh=R+WxcBGZyo57R+TPSn152AJAQMcXVsHBy/hq2X3US2s=; b=ypGAOkoS4UZJ0a2cVFklAaJHcgFpE0gDj9xoTyZAUOs7c/IB+NvQvRrViQpxgIdSCw hPNXY7uY7WzVBi1u9HURDAgmXHaHXF9SyOkEOIeCjifu1QGp7lSwuAMSKN82W4KrdFG+ z8VCJqwJMzDmqxcpc0pkCitpxj86bUTTEEvxO2dmol30ZzFF0ZqHrAxlq3feEK2CPRsi UZJSbS+X7UxHoXBfCODkkNV9HWgBhiMc1Zh6fD6RGTh3qi4YD47q54ZscB26mE5gPyez zlymwz2w4FkdhjPgMLNqm7Da3Mm/hWat39JuLaV+5UsUvPFkykuG/WmAaFgcyxOXKKQp UIHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JefohOHT; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=c8k0IaT0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id s6-20020a625e06000000b0068a38a9ab84si2278905pfb.176.2023.10.17.14.26.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 14:26:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JefohOHT; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=c8k0IaT0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 87F5D810EC34; Tue, 17 Oct 2023 14:26:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344415AbjJQV03 (ORCPT + 99 others); Tue, 17 Oct 2023 17:26:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235107AbjJQVZh (ORCPT ); Tue, 17 Oct 2023 17:25:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD7BB197 for ; Tue, 17 Oct 2023 14:24:19 -0700 (PDT) Message-ID: <20231017211724.135503217@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1697577858; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=VEdToj6uFLFPCubRLaSXMguz5WtO049V21/nw5QnioQ=; b=JefohOHTevmlDp2/uxgFm8oECSsOI3vi8HUaz9d5ArSiWDVufmUMM+VBtn4lmpVsS79J17 WnjJmOFqFFN8YJANwBrijER9mfwnOrWZEsDR9zy6rX65ksmIV5mp/f6MojQvGiw4AbMCN1 XKFpdZT6mjqtWLfQ8MNN/3/4T+/99JqCQ22J7CJz5gIzM545SKNTebvWMZInz1DIxk8zlk vE9O3rlzD9CHvaTzCamIQ2Izzr+IkYpqbXYgnI/pELwZmDgxvsULkbp6dtdk9iXeIaHfJQ QmXM+66N6IlAMXNZ6KsWkRu3wQico18mGYOKUw2QHhS+6OqZGRo9bYUBfOhiDw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1697577858; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=VEdToj6uFLFPCubRLaSXMguz5WtO049V21/nw5QnioQ=; b=c8k0IaT0LYrO60QNA3c65L2MjogS8hWdiE5sFlgdAModYkSrH0Z1fpgwYo9TKChQJOIGrx VoGOHoHfiZkOY9BQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , Ashok Raj Subject: [patch V5 39/39] x86/microcode/intel: Add a minimum required revision for late loading References: <20231017200758.877560658@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Tue, 17 Oct 2023 23:24:17 +0200 (CEST) X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Tue, 17 Oct 2023 14:26:51 -0700 (PDT) From: Ashok Raj In general users, don't have the necessary information to determine whether late loading of a new microcode version is safe and does not modify anything which the currently running kernel uses already, e.g. removal of CPUID bits or behavioural changes of MSRs. To address this issue, Intel has added a "minimum required version" field to a previously reserved field in the microcode header. Microcode updates should only be applied if the current microcode version is equal to, or greater than this minimum required version. Thomas made some suggestions on how meta-data in the microcode file could provide Linux with information to decide if the new microcode is suitable candidate for late loading. But even the "simpler" option requires a lot of metadata and corresponding kernel code to parse it, so the final suggestion was to add the 'minimum required version' field in the header. When microcode changes visible features, microcode will set the minimum required version to its own revision which prevents late loading. Old microcode blobs have the minimum revision field always set to 0, which indicates that there is no information and the kernel considers it unsafe. This is a pure OS software mechanism. The hardware/firmware ignores this header field. For early loading there is no restriction because OS visible features are enumerated after the early load and therefore a change has no effect. The check is always enabled, but by default not enforced. It can be enforced via Kconfig or kernel command line. If enforced, the kernel refuses to late load microcode with a minimum required version field which is zero or when the currently loaded microcode revision is smaller than the minimum required revision. If not enforced the load happens independent of the revision check to stay compatible with the existing behaviour, but it influences the decision whether the kernel is tainted or not. If the check signals that the late load is safe, then the kernel is not tainted. Early loading is not affected by this. [ tglx: Massaged changelog and fixed up the implementation ] Suggested-by: Thomas Gleixner Signed-off-by: Ashok Raj Signed-off-by: Thomas Gleixner --- arch/x86/include/asm/microcode.h | 3 +- arch/x86/kernel/cpu/microcode/intel.c | 37 ++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) --- --- a/arch/x86/include/asm/microcode.h +++ b/arch/x86/include/asm/microcode.h @@ -38,7 +38,8 @@ struct microcode_header_intel { unsigned int datasize; unsigned int totalsize; unsigned int metasize; - unsigned int reserved[2]; + unsigned int min_req_ver; + unsigned int reserved; }; struct microcode_intel { --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -457,16 +457,40 @@ static enum ucode_state apply_microcode_ return ret; } +static bool ucode_validate_minrev(struct microcode_header_intel *mc_header) +{ + int cur_rev = boot_cpu_data.microcode; + + /* + * When late-loading, ensure the header declares a minimum revision + * required to perform a late-load. The previously reserved field + * is 0 in older microcode blobs. + */ + if (!mc_header->min_req_ver) { + pr_info("Unsafe microcode update: Microcode header does not specify a required min version\n"); + return false; + } + + /* + * Check whether the current revision is either greater or equal to + * to the minimum revision specified in the header. + */ + if (cur_rev < mc_header->min_req_ver) { + pr_info("Unsafe microcode update: Current revision 0x%x too old\n", cur_rev); + pr_info("Current should be at 0x%x or higher. Use early loading instead\n", mc_header->min_req_ver); + return false; + } + return true; +} + static enum ucode_state parse_microcode_blobs(int cpu, struct iov_iter *iter) { struct ucode_cpu_info *uci = ucode_cpu_info + cpu; + bool is_safe, new_is_safe = false; int cur_rev = uci->cpu_sig.rev; unsigned int curr_mc_size = 0; u8 *new_mc = NULL, *mc = NULL; - if (force_minrev) - return UCODE_NFOUND; - while (iov_iter_count(iter)) { struct microcode_header_intel mc_header; unsigned int mc_size, data_size; @@ -509,9 +533,14 @@ static enum ucode_state parse_microcode_ if (!intel_find_matching_signature(mc, &uci->cpu_sig)) continue; + is_safe = ucode_validate_minrev(&mc_header); + if (force_minrev && !is_safe) + continue; + kvfree(new_mc); cur_rev = mc_header.rev; new_mc = mc; + new_is_safe = is_safe; mc = NULL; } @@ -523,7 +552,7 @@ static enum ucode_state parse_microcode_ return UCODE_NFOUND; ucode_patch_late = (struct microcode_intel *)new_mc; - return UCODE_NEW; + return new_is_safe ? UCODE_NEW_SAFE : UCODE_NEW; fail: kvfree(mc);