From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=35.83.148.184 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795977010; b=kkj8SwIJpCI/+cE9AFMs1Ro5yxNUr2N+Zb6tW/gkqYHq6HI+1ewzswXe3c5lQ8fDW9j+ xaWlfnHozIHaCVo3c8LTJz1PTK8Mqv2A3XchQeF8L8BBNzFXLYQJnqHCS2d1tyxIYeuo6 WWRXdVXlsloVCKMIrfqCtmSMrjr5ZMY5xoi41/XU6xGYFsQa0wke4L4n1PNkGdeITfxFq dyc5qH/x2Qnjkp9FMvVYccpnmDMfCr6rnMBS55mNscGp2iErlvucnINc9RkHggDqWLmfN HyZX5hU3U0GiAPnf+o66KT7fOEa8srpl9Ag0zbi8TWLsW0wb9S/beknNukaHWJNbDyA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795977010; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=QZKrFhiNp+wB/st/cCmB5Ri+7MS6FGl7ZeDRGALBK9c=; b=BcKASGb7D+6G+J+h1HTv6zVS6YLdqzsSVxD8SAqQGeBSqfelR2UZEupAryn8yVuzYfBv E0mgynf+qeMuXUkBkkArjII8PdZFxMsGJAHFWDjAKdkVAmxxdf59TAy9x+WZUynqa+ivY FWvSRxwmHINlTuWdHLHxdOj7b7SJVwaBoGEXsKkivCIwqKFShS287XeFW+rTR2ysg8SoK tZYiROVkuc0Lu+peERnLEkyRonaGJMeJygxOKYXEP178N+Ov1LFGbTGWxKjrEUMZPvJPc m3/Uu3ODJ49r7VOjYmJ2afHNtK22yV0n4AgS1cDEeNhKa3viSxMOPeDzTA8OoKN7GEw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.com; spf=pass smtp.mailfrom=amazon.de; dkim=pass header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=AqBr+SqJ; arc=none smtp.remote-ip=35.83.148.184 Received: from pdx-out-014.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-014.esa.us-west-2.outbound.mail-perimeter.amazon.com [35.83.148.184]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B1AA606A323; Sun, 29 Nov 2026 18:30:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0B1AA606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=AqBr+SqJ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795977010; x=1827513010; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QZKrFhiNp+wB/st/cCmB5Ri+7MS6FGl7ZeDRGALBK9c=; b=AqBr+SqJ4PztmKooVLrUGhRO1PnCACBWI/6RqvWsFXvnIqz939YpzK6P b+tDAwJozZKtJe2SEBwH3p9J80rUrcFNBbZi1KclHpQLMfXVavtTSv3K/ FnVNjaw+VB08EkxS7UweYpHR8MB65cVnasVYFr2EjcB5jwncJBdtNM+lM mw5Jnc26pU9mtMy7KPJuLsUFlMGxoUncf76/R4tgeIiYf0kaegg/8S4BZ seJk8Mg/qalyhUkqqjHuQOnXP7ZTP5i5MLgrBoCkv2AX6eczXsTc3ph/q +CsNTEipyLDF3ZqTLmpSgbqAhOxOuOd2rpQL63iex1WbCU7bpRx//XJAY A==; X-CSE-ConnectionGUID: EM5Cvh5cQG2T7/3TwUYjuw== X-CSE-MsgGUID: 6mPssrV4TTGkg+zobhwmXQ== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="35204611" Received: from ip-10-5-9-48.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.9.48]) by internal-pdx-out-014.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:30:09 +0000 Received: from EX19MTAUWA001.ant.amazon.com [205.251.233.236:16675] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.48.218:2525] with esmtp (Farcaster) id 5b4d94ef-4108-4123-97b9-03f6c1bd52d8; Sun, 29 Nov 2026 18:30:09 +0000 (UTC) X-Farcaster-Flow-ID: 5b4d94ef-4108-4123-97b9-03f6c1bd52d8 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA001.ant.amazon.com (10.250.64.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Sun, 29 Nov 2026 18:30:09 +0000 Received: from ip-10-253-83-51.amazon.com (172.19.99.218) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Sun, 29 Nov 2026 18:30:05 +0000 From: Alexander Graf To: Greg Kroah-Hartman , Arnd Bergmann CC: The AWS Nitro Enclaves Team , , Paolo Bonzini , "Stefano Garzarella" , Stefan Hajnoczi , Marek Szyprowski , Robin Murphy , Andrew Morton , "David Hildenbrand" , Mike Rapoport , "Jonathan Corbet" , Shuah Khan , "Randy Dunlap" , , , , , , , , , Subject: [PATCH v2 14/23] nitro_enclaves: Keep the CPU pool when the new CPU list does not parse Date: Sun, 29 Nov 2026 18:21:43 +0000 Message-ID: <63a2d0304a163cc1ba4bae5f23d10f4c5a36af40.1795965038.git.graf@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWA002.ant.amazon.com (10.13.139.96) To EX19D001UWA001.ant.amazon.com (10.13.138.214) An admin who mistypes a CPU list into ne_cpus loses the pool that was there, because ne_set_kernel_param() tears the pool down before ne_setup_cpu_pool() parses the new value. The write fails, but the pool's CPUs come back online and ne_cpus reads empty. A list that parses but does not fit the 512 byte ne_cpus buffer fails the same way, only later: param_set_copystring() refuses it at the end of the replace, after the new pool is already in place, and the teardown that follows destroys a pool a new enclave may already have drawn threads from. A sysfs write takes a page, and a list of single-CPU entries for 128 CPUs is over 600 bytes, so a valid list can reach this. Parse the list and check its length before the teardown and pass the mask down. The other checks stay after the teardown, because they want every CPU of the list online, and a list that extends the current pool names offline CPUs. Fixes: ff8a4d3e3a99 ("nitro_enclaves: Add logic for setting an enclave vCPU") Signed-off-by: Alexander Graf --- v1 -> v2: - Refuse a list that does not fit ne_cpus before replacing the pool drivers/virt/nitro_enclaves/ne_misc_dev.c | 77 ++++++++++++++--------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index 6db8cf5c80257..6efd55cfb6cb6 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -212,7 +212,7 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n * or not using CPU 0, which should remain available for * the primary / parent VM. Offline the CPUs from the * pool after the checks passed. - * @ne_cpu_list: The CPU list used for setting NE CPU pool. + * @cpu_pool: The CPU mask used for setting NE CPU pool. * * Context: Process context. No driver lock may be held: remove_cpu() and * add_cpu() take device_hotplug_lock. The pool mutex only guards the @@ -221,11 +221,10 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n * * 0 on success. * * Negative return value on failure. */ -static int ne_setup_cpu_pool(const char *ne_cpu_list) +static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) { int core_id = -1; unsigned int cpu = 0; - cpumask_var_t cpu_pool; unsigned int cpu_sibling = 0; unsigned int i = 0; unsigned int nr_parent_vm_cores = 0; @@ -234,23 +233,13 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) int rc = -EINVAL; cpumask_var_t *threads_per_core = NULL; - if (!zalloc_cpumask_var(&cpu_pool, GFP_KERNEL)) - return -ENOMEM; - - rc = cpulist_parse(ne_cpu_list, cpu_pool); - if (rc < 0) { - pr_err("%s: Error in cpulist parse [rc=%d]\n", ne_misc_dev.name, rc); - - goto free_pool_cpumask; - } - cpu = cpumask_any(cpu_pool); if (cpu >= nr_cpu_ids) { pr_err("%s: No CPUs available in CPU pool\n", ne_misc_dev.name); rc = -EINVAL; - goto free_pool_cpumask; + goto free_cores_cpumask; } /* @@ -264,7 +253,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL; - goto free_pool_cpumask; + goto free_cores_cpumask; } /* @@ -279,7 +268,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL; - goto free_pool_cpumask; + goto free_cores_cpumask; } } else { if (numa_node != cpu_to_node(cpu)) { @@ -288,7 +277,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL; - goto free_pool_cpumask; + goto free_cores_cpumask; } } @@ -301,7 +290,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL; - goto free_pool_cpumask; + goto free_cores_cpumask; } for_each_cpu(cpu_sibling, topology_sibling_cpumask(0)) { @@ -311,7 +300,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL; - goto free_pool_cpumask; + goto free_cores_cpumask; } } @@ -328,7 +317,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL; - goto free_pool_cpumask; + goto free_cores_cpumask; } } } @@ -344,7 +333,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) if (!threads_per_core) { rc = -ENOMEM; - goto free_pool_cpumask; + goto free_cores_cpumask; } for (i = 0; i < nr_parent_vm_cores; i++) @@ -395,8 +384,6 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) } } - free_cpumask_var(cpu_pool); - mutex_lock(&ne_cpu_pool.mutex); ne_cpu_pool.avail_threads_per_core = threads_per_core; @@ -413,8 +400,6 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) add_cpu(cpu); free_cores_cpumask: ne_free_core_slots(threads_per_core, nr_parent_vm_cores); -free_pool_cpumask: - free_cpumask_var(cpu_pool); return rc; } @@ -455,6 +440,7 @@ static void ne_teardown_cpu_pool(void) static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) { char error_val[] = ""; + cpumask_var_t cpu_pool; struct ne_pci_dev *ne_pci_dev = NULL; cpumask_var_t *threads_per_core = NULL; unsigned int nr_cores = 0; @@ -463,9 +449,12 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) if (!capable(CAP_SYS_ADMIN)) return -EPERM; + if (!zalloc_cpumask_var(&cpu_pool, GFP_KERNEL)) + return -ENOMEM; + rc = down_read_killable(&ne_devs.lock); if (rc) - return rc; + goto free_cpumask; ne_pci_dev = ne_devs.ne_pci_dev; @@ -490,6 +479,31 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) goto unlock; } + /* + * Refuse a list the parameter buffer cannot hold before the detach + * below, so that the copy at the end of the replace cannot fail + * once the new pool is in place. + */ + if (strlen(val) >= NE_CPUS_SIZE) { + pr_err("%s: The CPU list does not fit ne_cpus\n", ne_misc_dev.name); + + rc = -ENOSPC; + + goto unlock; + } + + /* + * Parse the list before the detach below takes the pool out of the + * driver's view, so that a value that does not parse leaves it in + * place. + */ + rc = cpulist_parse(val, cpu_pool); + if (rc < 0) { + pr_err("%s: Error in cpulist parse [rc=%d]\n", ne_misc_dev.name, rc); + + goto unlock; + } + /* * Take the pool out of the driver's view while the enclave list is * still locked, so an enclave created once the locks drop sees no pool @@ -511,13 +525,13 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) ne_free_core_slots(threads_per_core, nr_cores); } - rc = ne_setup_cpu_pool(val); + rc = ne_setup_cpu_pool(cpu_pool); if (rc < 0) { pr_err("%s: Error in setup CPU pool [rc=%d]\n", ne_misc_dev.name, rc); param_set_copystring(error_val, kp); - return rc; + goto free_cpumask; } rc = param_set_copystring(val, kp); @@ -527,11 +541,9 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) ne_teardown_cpu_pool(); param_set_copystring(error_val, kp); - - return rc; } - return 0; + goto free_cpumask; unlock: if (ne_pci_dev) @@ -540,6 +552,9 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) unlock_devs: up_read(&ne_devs.lock); +free_cpumask: + free_cpumask_var(cpu_pool); + return rc; } -- 2.47.1