efi: Fix pstore runtime en/disable
Subsystems (3)
v2 cover efi: Fix pstore runtime en/disable
From: Ard Biesheuvel <[email protected]>
Fix buggy use of /sys/mode/efi-pstore/params to control the module at runtime
Changes since v1:
- Address feedback from Uwe and Sashiko
* Two fixes for pre-existing issues
- Split the conversion patch into two
Ard Biesheuvel (5):
efi: pstore: Make pstore_disable parameter read-only again
efi: efivars: Don't dereference NULL __efivars in efivar_lock()
efi: Statically initialize efivar_ops_nh
efi: Tie 'efivars' pdev lifecycle to generic efivars
efi: pstore: Convert into a platform driver for 'efivars'
drivers/firmware/efi/efi-pstore.c | 120 +++++++++++++------- drivers/firmware/efi/efi.c | 71 +++++++++++- drivers/firmware/efi/vars.c | 4 +- include/linux/efi.h | 11 ++ 4 files changed, 158 insertions(+), 48 deletions(-)
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH v2 1/5 efi: pstore: Make pstore_disable parameter read-only again
From: Ard Biesheuvel <[email protected]>
Commit a28655c330ab ("efi: pstore: Allow dynamic initialization based on module parameter") wired up a kernel_param_ops::set callback for pstore_disable that invokes efivars_pstore_init() directly.
Because parse_args() runs before module initialization (and before mm_core_init() during early boot when built-in), calling efivars_pstore_init() from the parameter setter is fundamentally broken:
- Loading efi-pstore as a module with pstore_disable=N initializes and registers efi_pstore_info during parse_args(), and then runs efivars_pstore_init() a second time from do_init_module(), leaking the first buffer, failing pstore_register() with -EBUSY, and zeroing efi_pstore_info.buf/bufsize while leaving efi_pstore_info registered with pstore.
- If load_module() fails after parse_args() (e.g., due to a subsequent unknown parameter), the module is freed without calling module_exit, leaving pstore with a dangling backend pointer.
- Passing pstore_disable=N before record_size=<N> initializes the buffer using the default record_size before record_size is parsed.
Module parameters are meant for boot/load-time configuration rather than runtime driver state control via sysfs. Revert the dynamic parameter callback changes and tighten the sysfs permissions of pstore_disable from 0644 to 0444 (matching record_size) so it is no longer misleadingly writable at runtime.
--- drivers/firmware/efi/efi-pstore.c | 39 +++----------------- 1 file changed, 6 insertions(+), 33 deletions(-)
drivers/firmware/efi/efi-pstore.c +6 -33
@@ -19,37 +19,10 @@ MODULE_PARM_DESC(record_size, "size of each pstore UEFI var (in bytes, min/defau EFI_VARIABLE_BOOTSERVICE_ACCESS | \ EFI_VARIABLE_RUNTIME_ACCESS) -static bool pstore_disable =IS_ENABLED (CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE ); +static bool efivars_pstore_disable = +IS_ENABLED (CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE ); -static int efivars_pstore_init (void ); -static void efivars_pstore_exit (void ); - -static int efi_pstore_disable_set (const char *val,const struct kernel_param *kp) -{ -int err; -bool old_pstore_disable = pstore_disable; - - err =param_set_bool (val, kp); -if (err) -return err; - -if (old_pstore_disable != pstore_disable) { -if (pstore_disable) -efivars_pstore_exit (); -else -efivars_pstore_init (); - } - -return 0 ; -} - -static const struct kernel_param_ops pstore_disable_ops = { - .set = efi_pstore_disable_set, - .get = param_get_bool, -}; - -module_param_cb (pstore_disable, &pstore_disable_ops, &pstore_disable,0644 ); -__MODULE_PARM_TYPE (pstore_disable,"bool" ); +module_param_named (pstore_disable, efivars_pstore_disable, bool,0444 );static int efi_pstore_open (struct pstore_info *psi) {
@@ -255,12 +228,12 @@ static struct pstore_info efi_pstore_info = { .erase = efi_pstore_erase, }; -static int efivars_pstore_init (void ) +static __init int efivars_pstore_init (void ) {if (!efivar_supports_writes ())return 0 ; -if (pstore_disable) +if (efivars_pstore_disable)return 0 ;/*
@@ -287,7 +260,7 @@ static int efivars_pstore_init(void)return 0 ; } -static void efivars_pstore_exit (void ) +static __exit void efivars_pstore_exit(void) {if (!efi_pstore_info.bufsize)return ;
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH v2 2/5 efi: efivars: Don't dereference NULL __efivars in efivar_lock()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
efivar_lock() and efivar_trylock() check __efivars->ops after taking efivars_lock, but __efivars itself is NULL whenever no efivars implementation is registered.
That happens at runtime, not only before efivars is set up. For example, tee_stmm_efi_probe() unregisters the generic efivars ops before registering its own, and qcom_uefisecapp_remove() and gsmi_exit() unregister efivars outright. A concurrent or subsequent efivarfs access, or a write from a pstore backend that is still registered, then dereferences a NULL pointer instead of failing with -ENODEV.
Check __efivars itself as well as __efivars->ops.
--- drivers/firmware/efi/vars.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/efi/vars.c +2 -2
@@ -143,7 +143,7 @@ int efivar_lock(void) {if (down_interruptible (&efivars_lock))return -EINTR ; -if (!__efivars->ops) { +if (!__efivars || !__efivars->ops) {up (&efivars_lock);return -ENODEV ; }
@@ -159,7 +159,7 @@ int efivar_trylock(void) {if (down_trylock (&efivars_lock))return -EBUSY ; -if (!__efivars->ops) { +if (!__efivars || !__efivars->ops) {up (&efivars_lock);return -ENODEV ; }
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH v2 3/5 efi: Statically initialize efivar_ops_nh
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
efisubsys_init() initializes the efivar_ops_nh notifier head at runtime, but only after generic_ops_register() has already called efivars_register(), which invokes blocking_notifier_call_chain() on it.
This is currently harmless: the head is zero-initialized, and blocking_notifier_call_chain() returns early on an empty chain before touching the rwsem. Still, it is fragile, and there is no reason to set up the notifier head at runtime at all.
Use BLOCKING_NOTIFIER_HEAD() so it is valid from the start, and drop the runtime initialization.
--- drivers/firmware/efi/efi.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/firmware/efi/efi.c +1 -3
@@ -184,7 +184,7 @@ static const struct attribute_group efi_subsys_attr_group = { .is_visible_const = efi_attr_is_visible, }; -struct blocking_notifier_head efivar_ops_nh; +BLOCKING_NOTIFIER_HEAD (efivar_ops_nh);EXPORT_SYMBOL_GPL (efivar_ops_nh);static struct efivars generic_efivars;
@@ -485,8 +485,6 @@ static int __init efisubsys_init(void)platform_device_register_simple ("efivars" ,0 ,NULL ,0 ); } -BLOCKING_INIT_NOTIFIER_HEAD (&efivar_ops_nh); - error =sysfs_create_group (efi_kobj, &efi_subsys_attr_group);if (error) { pr_err("efi: Sysfs attribute export failed with error %d.\n",
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH v2 4/5 efi: Tie 'efivars' pdev lifecycle to generic efivars
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Currently, efisubsys_init() registers the "efivars" platform device, which efi-pstore matches via MODULE_ALIAS("platform:efivars"), whenever EFI_BOOT is set and GetVariable/GetNextVariableName are marked supported in efi.runtime_supported_mask. The device is never unregistered.
That makes the device's existence unrelated to what efi-pstore actually needs:
- it is registered even when generic_ops_supported() returns false (leaving no efivars implementation registered) or when SetVariable() is unsupported;
- it remains registered after tee_stmm_efi_probe() replaces the generic ops with the OP-TEE StandaloneMM implementation, which does not support non-blocking SetVariable() calls from the pstore dump path.
Other efivars implementations (Coreboot GSMI, Qualcomm UEFI Secure App) should not be exposed to efi-pstore at all. qcuefi_set_variable() takes a mutex and allocates memory, and efivar_set_variable_locked() falls back to it from pstore_dump(), which runs with a raw spinlock held and interrupts disabled. On GSMI systems, efi-pstore would also compete with ramoops for pstore's single backend slot.
Instead, register the "efivars" platform device from generic_ops_register() once efivars_register() has succeeded with SetVariable() support, and unregister it from generic_ops_unregister() before efivars_unregister(). Consumers are then always detached while the ops they use are still in place.
Serialize both paths with a new generic_ops_lock. They can also be reached at runtime via efivars_generic_ops_[un]register() from tee_stmm_efi. The device is registered and unregistered outside efivars_lock, so a consumer can take efivars_lock from its probe or remove path.
--- drivers/firmware/efi/efi.c | 52 +++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-)
drivers/firmware/efi/efi.c +50 -2
@@ -31,6 +31,7 @@#include <linux/acpi.h> #include <linux/ucs2_string.h> #include <linux/memblock.h> +#include <linux/mutex.h> #include <linux/security.h> #include <linux/notifier.h>
@@ -190,6 +191,21 @@ EXPORT_SYMBOL_GPL(efivar_ops_nh);static struct efivars generic_efivars;static struct efivar_operations generic_ops; +/* +* Serializes generic_ops_register() and generic_ops_unregister(), which may +* also be called at runtime via efivars_generic_ops_[un]register(), and +* protects efivars_pdev. +*/ +static DEFINE_MUTEX (generic_ops_lock); + +/* +* The "efivars" platform device that efi-pstore binds to. It only exists +* while the generic, EFI runtime services based efivars ops are registered +* and support SetVariable(), as other efivars implementations may not +* provide a SetVariable() that is safe to call from the pstore dump path. +*/ +static struct platform_device *efivars_pdev; +static bool generic_ops_supported (void ) {unsigned long name_size;
@@ -210,6 +226,11 @@ static bool generic_ops_supported(void)static int generic_ops_register (void ) { +struct platform_device *pdev; +int ret; + +guard (mutex)(&generic_ops_lock); +if (!generic_ops_supported ())return 0 ;
@@ -222,14 +243,42 @@ static int generic_ops_register(void) generic_ops.set_variable = efi.set_variable; generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking; } -return efivars_register (&generic_efivars, &generic_ops); + + ret =efivars_register (&generic_efivars, &generic_ops); +if (ret || !generic_ops.set_variable) +return ret; + +/* +* Register the platform device only after efivars_register() has +* returned, so that a consumer probing synchronously from +* platform_device_register_simple() can take efivars_lock. +*/ + pdev =platform_device_register_simple ("efivars" ,0 ,NULL ,0 ); +if (IS_ERR (pdev)) +pr_warn ("efi: failed to register efivars platform device: %pe\n" , + pdev); +else + efivars_pdev = pdev; + +return 0 ; }static void generic_ops_unregister (void ) { +guard (mutex)(&generic_ops_lock); +if (!generic_ops.get_variable)return ; +/* +* Unregister the platform device first, so that consumers are +* detached while the efivars ops they rely on are still registered. +*/ +if (efivars_pdev) { +platform_device_unregister (efivars_pdev); + efivars_pdev =NULL ; + } +efivars_unregister (&generic_efivars); }
@@ -482,7 +531,6 @@ static int __init efisubsys_init(void) error =efivar_ssdt_load ();if (error)pr_err ("efi: failed to load SSDT, error %d.\n" , error); -platform_device_register_simple ("efivars" ,0 ,NULL ,0 ); } error =sysfs_create_group (efi_kobj, &efi_subsys_attr_group);
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH v2 5/5 efi: pstore: Convert into a platform driver for 'efivars'
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
efi-pstore already carries MODULE_ALIAS("platform:efivars") to match the "efivars" platform device, but never registered an actual platform_driver for it. As a result, it stays registered with pstore even after the "efivars" device goes away, e.g., when tee_stmm_efi replaces the generic efivars ops.
Convert efi-pstore into a proper platform_driver, so that its pstore backend lifetime follows that of the "efivars" device. The EFI core registers that device only while the generic, EFI runtime services based efivars ops are in use and support SetVariable().
efi_pstore_info is a singleton, so only bind to the device named "efivars" that carries struct efivars_pdev_data. This rejects devices whose driver_override has been set to "efi-pstore" via sysfs: binding to one of those would race with the "efivars" device under a different device_lock and clobber efi_pstore_info.buf while it is registered.
When pstore_disable is set (via CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE=y or efi_pstore.pstore_disable=Y), refuse the probes that the driver core performs automatically during platform_driver_register() and during registration of the "efivars" device, while keeping the driver registered. Each window is tracked by a flag: a module-local one for driver registration, and efivars_pdev_data::registering, which the EFI core sets while registering the device. With PROBE_FORCE_SYNCHRONOUS, the automatic probes complete within these windows. This lets userspace enable or disable efi-pstore at runtime without rebooting, via the standard driver model sysfs interface:
echo efivars.0 > /sys/bus/platform/drivers/efi-pstore/bind echo efivars.0 > /sys/bus/platform/drivers/efi-pstore/unbind
An explicit bind that races with either registration window is refused. A manual binding does not carry over if the "efivars" device is re-registered, e.g., when tee_stmm_efi is unbound and the generic ops are restored; the new device is again subject to pstore_disable.
--- drivers/firmware/efi/efi-pstore.c | 87 +++++++++++++++++--- drivers/firmware/efi/efi.c | 25 ++++-- include/linux/efi.h | 11 +++ 3 files changed, 107 insertions(+), 16 deletions(-)
drivers/firmware/efi/efi-pstore.c +76 -11
@@ -2,8 +2,10 @@#include <linux/efi.h> #include <linux/module.h> +#include <linux/platform_device.h> #include <linux/pstore.h> #include <linux/slab.h> +#include <linux/string.h> #include <linux/ucs2_string.h> MODULE_IMPORT_NS ("EFIVAR" );
@@ -21,6 +23,7 @@ MODULE_PARM_DESC(record_size, "size of each pstore UEFI var (in bytes, min/defaustatic bool efivars_pstore_disable =IS_ENABLED (CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE ); +static bool efi_pstore_driver_registering;module_param_named (pstore_disable, efivars_pstore_disable, bool,0444 );
@@ -228,13 +231,42 @@ static struct pstore_info efi_pstore_info = { .erase = efi_pstore_erase, }; -static __init int efivars_pstore_init (void ) +static int efi_pstore_probe (struct platform_device *pdev) { +const struct efivars_pdev_data *pdata =dev_get_platdata (&pdev->dev); +int ret; + +/* +* efi_pstore_info is a singleton, so only bind to the "efivars" device +* registered by the EFI core, of which at most one exists at any given +* time, and whose probe and remove are serialized by its device_lock. +* This also rejects other devices whose driver_override was set to +* "efi-pstore" via sysfs, which platform_match() honours before +* consulting the id table. +*/ +if (!pdata ||strcmp (pdev->name,EFIVARS_PDEV_NAME )) +return -ENODEV ; +if (!efivar_supports_writes ()) -return 0 ; +return -ENODEV ; -if (efivars_pstore_disable) -return 0 ; +/* +* If efi-pstore is disabled, only permit binding it explicitly via +* sysfs, by refusing the probes that the driver core performs +* automatically when the driver or the "efivars" device is +* registered. As the driver uses PROBE_FORCE_SYNCHRONOUS, such probes +* always complete before platform_driver_register() or +* platform_device_register_data() return, and therefore before the +* respective flag is cleared again. (This also means that an explicit +* bind that races with either registration is refused.) +* +* The flags may be cleared concurrently by another CPU, without +* holding this device's device_lock, hence the READ_ONCE()s. +*/ +if (efivars_pstore_disable && + (READ_ONCE (efi_pstore_driver_registering) || +READ_ONCE (pdata->registering))) +return -ENODEV ;/* * Notice that 1024 is the minimum here to prevent issues with
@@ -251,29 +283,62 @@ static __init int efivars_pstore_init(void) efi_pstore_info.bufsize = record_size; -if (pstore_register (&efi_pstore_info)) { + ret =pstore_register (&efi_pstore_info); +if (ret) {kfree (efi_pstore_info.buf); efi_pstore_info.buf =NULL ; efi_pstore_info.bufsize =0 ; } -return 0 ; +return ret; } -static __exit void efivars_pstore_exit(void) +static void efi_pstore_remove (struct platform_device *pdev) { -if (!efi_pstore_info.bufsize) -return ; -pstore_unregister (&efi_pstore_info);kfree (efi_pstore_info.buf); efi_pstore_info.buf =NULL ; efi_pstore_info.bufsize =0 ; } +static const struct platform_device_id efi_pstore_ids[] = { + { .name =EFIVARS_PDEV_NAME , }, + { } +}; +MODULE_DEVICE_TABLE (platform, efi_pstore_ids); + +static struct platform_driver efi_pstore_driver = { + .probe = efi_pstore_probe, + .remove = efi_pstore_remove, + .id_table = efi_pstore_ids, + .driver = { + .name ="efi-pstore" , + .probe_type =PROBE_FORCE_SYNCHRONOUS , + }, +}; + +static __init int efivars_pstore_init (void ) +{ +int ret; + +/* +* efi_pstore_driver becomes visible on the platform bus and in sysfs +* before platform_driver_register() returns, so a concurrent probe on +* another CPU may read the flag while it is being cleared. +*/ +WRITE_ONCE (efi_pstore_driver_registering, true); + ret =platform_driver_register (&efi_pstore_driver); +WRITE_ONCE (efi_pstore_driver_registering, false); +return ret; +} + +static __exit void efivars_pstore_exit(void) +{ +platform_driver_unregister (&efi_pstore_driver); +} +module_init (efivars_pstore_init);module_exit (efivars_pstore_exit);MODULE_DESCRIPTION ("EFI variable backend for pstore" );MODULE_LICENSE ("GPL" ); -MODULE_ALIAS ("platform:efivars" );
drivers/firmware/efi/efi.c +20 -5
@@ -226,6 +226,8 @@ static bool generic_ops_supported(void)static int generic_ops_register (void ) { +struct efivars_pdev_data pdata = { .registering = true }; +struct efivars_pdev_data *pdev_data;struct platform_device *pdev;int ret;
@@ -251,14 +253,27 @@ static int generic_ops_register(void)/* * Register the platform device only after efivars_register() has * returned, so that a consumer probing synchronously from -* platform_device_register_simple() can take efivars_lock. +* platform_device_register_data() can take efivars_lock. */ - pdev =platform_device_register_simple ("efivars" ,0 ,NULL ,0 ); -if (IS_ERR (pdev)) + pdev =platform_device_register_data (NULL ,EFIVARS_PDEV_NAME ,0 , + &pdata,sizeof (pdata)); +if (IS_ERR (pdev)) {pr_warn ("efi: failed to register efivars platform device: %pe\n" , pdev); -else - efivars_pdev = pdev; +return 0 ; + } + +/* +* Any probe performed automatically by the driver core as part of +* registering the device has completed by now (for drivers that +* probe synchronously). Clear the flag so that consumers can tell +* such probes apart from later ones, e.g., explicit binds via sysfs. +* The device is already visible in sysfs, so a concurrent probe on +* another CPU may read the flag without serialization. +*/ + pdev_data =dev_get_platdata (&pdev->dev); +WRITE_ONCE (pdev_data->registering, false); + efivars_pdev = pdev;return 0 ; }
include/linux/efi.h +11 -0
@@ -1069,6 +1069,17 @@ struct efivars {const struct efivar_operations *ops; }; +/* +* The "efivars" platform device is registered while the generic, EFI runtime +* services based efivars ops are registered and support SetVariable(). +*/ +#define EFIVARS_PDEV_NAME "efivars" + +struct efivars_pdev_data { +/* true only while platform_device_register_data() is in progress */ +bool registering; +}; +#ifdef CONFIG_X86 u64 __attribute_const__ efivar_reserved_space(void);#else
Signature · 1 line
2.56.1.831.g720f3f5937-goog