Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2631549ybz; Mon, 27 Apr 2020 01:10:30 -0700 (PDT) X-Google-Smtp-Source: APiQypLItleKBWF5vs68XvBAbibrwg9HeYpvQto8y8Xr7CmMIpkwlLX6Eb00zkZeiA9pXwuY0Hdg X-Received: by 2002:a17:906:1ccb:: with SMTP id i11mr18213176ejh.101.1587975030547; Mon, 27 Apr 2020 01:10:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587975030; cv=none; d=google.com; s=arc-20160816; b=BV5stCTTY5HlrAeXWK0UcMmemtFlzc4fTIsBU9rqx4/royghlxSGz6ohWsrXJTy0ck dMaRc7rjkBLDeDXwjmS0YgsVopSQgOispMwNm20SjqxPSrrf2QcE+oC43bmnUZw012Ro aqAxRsxcHRllEilX5z9f2m+GYjd0QnADDnaGhlLaOyAEtf/zLJ8ZUdZlE/Tw9rJr1g4F EDAOrfErrRHhYKo0y5cOwmQl68KRy+QNPGGpHUFKr/pl6F9ywmP9gEFnD2R6IFO2wVB2 YGSGrt/xEMN8adX4J5S3x7tpBQQvdHUUxhkX+5u0bHxM7PM95rpbZUoukAPqKRBYDxPW MCMA== 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=tGgIVpxz9WUK/2JdnEOgbUU3bqCa/maRFGSs7Nj+Jk8=; b=ifRygZJCensDz+C7Iyv1ijruOTsx+Mwbr0urhg9znNE6XVB5WOMrAbLbbLXv+z0i6z 8IRimAvBf0qqf/KOKn18iofaGi9HqOA/1uEdPH9pSf14VKrHfZ7dJR+LIu94qrSKFVbC 9kP4a6V4CamQoKn64eRy9MAjI+wQT0pZIUu2/MIQME+1wYJ2E6CYC9/N/72OUi4hJ0sW H5eDqXtQQlLVuAYBG176kGbZpDAv5mwcScvB3b4ke3e89ytI3PhXjtQgvDUzrdSa0nC1 jQUV2kQr0QQByRIz0X03z3Q9biHcBWYPBRhgaSJBkU6GDjn++hgrguj2A/vDmrm3FHp6 yyNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=S7E5OSLL; 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=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g88si7170512edd.505.2020.04.27.01.10.07; Mon, 27 Apr 2020 01:10:30 -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=@oracle.com header.s=corp-2020-01-29 header.b=S7E5OSLL; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726537AbgD0IIt (ORCPT + 99 others); Mon, 27 Apr 2020 04:08:49 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:57134 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726407AbgD0IIt (ORCPT ); Mon, 27 Apr 2020 04:08:49 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 03R836a2062342; Mon, 27 Apr 2020 08:08:28 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=tGgIVpxz9WUK/2JdnEOgbUU3bqCa/maRFGSs7Nj+Jk8=; b=S7E5OSLL0l86lbdaSDrfP4+Dt3OsQmPPILiUf2E1HadZKn8nYxyRscwYHjPU0ko+Dcd4 t3V7MkR2vU+SlqjVZbd4RYeGNZ6faphlaM9k2VaT7+Nch/zHg7dZFZGxbpxqHntyu9ox Fg/1sQD7GLUaddHdBOi7Enw0zzlP0+PJ9c1uf8q/e8X4akb8La1Q2LDxGcQoDgXOw8jH 3r2j+bMpPooGuVtCr3Cv1/a4IkBWfJqT6IvKEcJnwdn5UiULGGOUy9Avgjc+Y/isV0Lf PtBM4YSqbow3cxwbQNaPFKK6WPAgYTFl9iL9cGOXoIOqOvNxMwMD8Ib4taFSpJYQJlm5 gg== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2130.oracle.com with ESMTP id 30mcmqw25g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 27 Apr 2020 08:08:28 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 03R82aDF038627; Mon, 27 Apr 2020 08:08:27 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userp3030.oracle.com with ESMTP id 30mxpcp22r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 27 Apr 2020 08:08:27 +0000 Received: from abhmp0015.oracle.com (abhmp0015.oracle.com [141.146.116.21]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 03R88K6A003051; Mon, 27 Apr 2020 08:08:20 GMT Received: from mihai.localdomain (/10.153.73.25) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 27 Apr 2020 01:08:20 -0700 From: Mihai Carabas To: linux-kernel@vger.kernel.org Cc: Mihai Carabas , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Jonathan Corbet , linux-doc@vger.kernel.org Subject: [PATCH RFC 2/3] x86: microcode: intel: process microcode metadata Date: Mon, 27 Apr 2020 10:27:58 +0300 Message-Id: <1587972479-10971-3-git-send-email-mihai.carabas@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1587972479-10971-1-git-send-email-mihai.carabas@oracle.com> References: <1587972479-10971-1-git-send-email-mihai.carabas@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9603 signatures=668686 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 malwarescore=0 mlxscore=0 bulkscore=0 adultscore=0 phishscore=0 suspectscore=1 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004270071 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9603 signatures=668686 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 spamscore=0 clxscore=1011 priorityscore=1501 suspectscore=1 impostorscore=0 adultscore=0 malwarescore=0 bulkscore=0 lowpriorityscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004270071 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Process the microcode metadata to see what changes are done on CPU feature bits. The default kernel policy imposed by is_microcode_allowed() is to abort any microcode late loading if there will be removed a feature and will continue the loading otherwise. Complex policies can be created in the future. Signed-off-by: Mihai Carabas --- arch/x86/kernel/cpu/microcode/intel.c | 85 +++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c index a54d5e6..2ef4338 100644 --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -965,6 +966,81 @@ static bool is_blacklisted(unsigned int cpu) return false; } +/** + * is_microcode_allowed - parse and enforce policies + * @firmware - metadata blob + * + * Parses the metadata file provided with the microcode in order to see the + * changes brought in by the new blob. + * + * Based on the type of changes, currently it enforces the following policy: if + * there is a feature being removed by the new microcode, reject the loading. + * + * RETURNS: + * false if it rejects the microcode or true if it accepts the microcode. + */ +static bool is_microcode_allowed(const struct firmware *firmware) +{ + int ret, index, leaf, eax, ebx, ecx, edx, line_nr = 0; + char *data = (char *) firmware->data; + char *line; + char op; + + /* + * Get each line and match it with regular expression: + * {m|c} {+|-} u32 [u32]* + * + * See section Late loading metadata file from + * Documentation/x86/microcode.rst + */ + while (data && (data - ((char *)(firmware->data)) < firmware->size)) { + line = strsep(&data, "\n"); + line_nr++; + + /* + * For each line check the first letter to see the type of + * operation done by the microcode and create a sscanf call + * the would match the rest of the line of that operation. + * If it is not a match, exist with parsing error. If it is + * a match and is a remove action (-) do not allow microcode + * to be loaded. + */ + switch (line[0]) { + case 'c': + ret = sscanf(&line[1], " %c %x %x %x %x %x %x", &op, + &index, &leaf, &eax, &ebx, &ecx, &edx); + if (ret != 7) + goto out_error; + if (op == '-') + goto out_cpuid_rem_notsupp; + break; + case 'm': + ret = sscanf(&line[1], " %c %x", &op, &index); + if (ret != 2) + goto out_error; + if (op == '-') + goto out_msr_rem_notsupp; + break; + default: + goto out_error; + } + } + + return true; + +out_error: + pr_warn("Microcode metadata failed parsing at line %d\n", line_nr); + return false; + +out_msr_rem_notsupp: + pr_warn("Kernel policy does not allow to remove MSR: %x\n", index); + return false; + +out_cpuid_rem_notsupp: + pr_warn("Kernel policy does not allow to remove CPUID: %x leaf: %x, eax: %x, ebx: %x, ecx: %x, edx: %x\n", + index, leaf, eax, ebx, ecx, edx); + return false; +} static enum ucode_state request_microcode_fw(int cpu, struct device *device, bool refresh_fw) @@ -992,10 +1068,19 @@ static enum ucode_state request_microcode_fw(int cpu, struct device *device, return UCODE_NFOUND; } + /* Load the metadata file in memory. */ if (request_firmware_direct(&meta_firmware, meta_name, device)) { pr_debug("metadata file %s load failed\n", name); pr_debug("no feature check will be done pre-loading the microcode\n"); } else { + /* Check the policy based on the metadata file. */ + if (!is_microcode_allowed(meta_firmware)) { + pr_warn("kernel does not support the new microcode: %s\n", + name); + release_firmware(meta_firmware); + release_firmware(firmware); + return UCODE_ERROR; + } release_firmware(meta_firmware); } -- 1.8.3.1