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