platform/x86: asus-armoury: fix init errors and modernize resource management
v4 cover platform/x86: asus-armoury: fix init errors and modernize resource management
This patch series addresses a Use-After-Free vulnerability and a memory leak during driver initialization in the asus-armoury driver, and modernizes its memory management.
Patch 1 fixes the UAF and memory leak by propagating the error code from init_rog_tunables(), deferring the global pointer updates, and adding a rollback path.
Patch 2 refactors init_rog_tunables() to use the cleanup infrastructure via __free(kfree) and no_free_ptr() to automate memory management and prevent future leaks.
Changes in v4:
- Rebased on top of next-20260707 to resolve conflicts cleanly. No functional changes applied.
Changes in v3:
- Added a second patch to convert init_rog_tunables() to use the __free(kfree) and no_free_ptr() cleanup helper macros.
- Added #include <linux/cleanup.h>.
Thread for v3:
https://lore.kernel.org/platform-driver-x86/[email protected]/
Changes in v2:
- Restructured init_rog_tunables() to use local pointers (ac_rog_tunables, dc_rog_tunables) and only update the global static asus_armoury.rog_tunables structure once all allocations have successfully succeeded. This removes the risk of exposing freed or partially allocated pointers to the global struct.
- Removed redundant assignments to NULL in the error paths. Since initialization fails and the driver is not loaded, cleaning the global static pointers to NULL is not necessary.
- In init_rog_tunables(), replaced 'goto err_nomem' with direct 'return -ENOMEM'. The 'err_nomem' label in the original code printed a generic OOM error message, which is discouraged in the kernel as the allocator already issues OOM details. Because that label did not perform rollback cleanup, a direct return is cleaner and simpler.
- Replaced inline cleanup in asus_fw_init() with a standard goto rollback block at the end of the function for cleaner error path styling.
Thread for v2:
https://lore.kernel.org/platform-driver-x86/[email protected]/
Marco Scardovi (2):
platform/x86: asus-armoury: fix Use-After-Free and memory leak in
driver init
platform/x86: asus-armoury: use cleanup.h to manage tunables
drivers/platform/x86/asus-armoury.c | 46 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 18 deletions(-)
Signature · 1 line
2.55.0
PATCH v4 1/2 platform/x86: asus-armoury: fix Use-After-Free and memory leak in driver init
In init_rog_tunables(), if dc_limits are defined and allocating dc_rog_tunables fails, the already allocated ac_rog_tunables gets freed but the pointer stored in asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_AC] is not cleared. Since init_rog_tunables() returns void, the driver initialization continues, which can lead to a Use-After-Free (UAF) when asus_fw_attr_add() accesses the freed AC tunables pointer.
Additionally, if init_rog_tunables() succeeds but asus_fw_attr_add() fails, the allocated tunables are not freed, resulting in a memory leak.
Fix these issues by making init_rog_tunables() return an error code and propagating it in asus_fw_init(). Defer setting the global pointers in asus_armoury.rog_tunables until both tunables have been successfully allocated. If asus_fw_attr_add() fails, release the allocated resources using a standard goto rollback block in asus_fw_init().
--- drivers/platform/x86/asus-armoury.c | 38 ++++++++++++++++++----------- 1 file changed, 24 insertions(+), 14 deletions(-)
drivers/platform/x86/asus-armoury.c +24 -14
@@ -1007,7 +1007,7 @@ static int asus_fw_attr_add(void)/* Init / exit ****************************************************************/ /* Set up the min/max and defaults for ROG tunables */ -static void init_rog_tunables (void ) +static int init_rog_tunables (void ) {const struct power_limits *ac_limits, *dc_limits;struct rog_tunables *ac_rog_tunables =NULL , *dc_rog_tunables =NULL ;
@@ -1018,14 +1018,14 @@ static void init_rog_tunables(void) dmi_id =dmi_first_match (power_limits);if (!dmi_id) {pr_warn ("No matching power limits found for this system\n" ); -return ; +return 0 ; }/* Get the power data for this system */ power_data = dmi_id->driver_data;if (!power_data) {pr_info ("No power data available for this system\n" ); -return ; +return 0 ; } asus_armoury.requires_fan_curve = power_data->requires_fan_curve;
@@ -1033,11 +1033,10 @@ static void init_rog_tunables(void)/* Initialize AC power tunables */ ac_limits = power_data->ac_data;if (ac_limits) { - ac_rog_tunables =kzalloc_obj (*asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_AC ]); + ac_rog_tunables =kzalloc_obj (*ac_rog_tunables);if (!ac_rog_tunables) - goto err_nomem; +return -ENOMEM ; - asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_AC ] = ac_rog_tunables; ac_rog_tunables->power_limits = ac_limits;/* Set initial AC values */
@@ -1080,13 +1079,12 @@ static void init_rog_tunables(void)/* Initialize DC power tunables */ dc_limits = power_data->dc_data;if (dc_limits) { - dc_rog_tunables =kzalloc_obj (*asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_DC ]); + dc_rog_tunables =kzalloc_obj (*dc_rog_tunables);if (!dc_rog_tunables) {kfree (ac_rog_tunables); - goto err_nomem; +return -ENOMEM ; } - asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_DC ] = dc_rog_tunables; dc_rog_tunables->power_limits = dc_limits;/* Set initial DC values */
@@ -1126,15 +1124,16 @@ static void init_rog_tunables(void)pr_debug ("No DC PPT limits defined\n" ); } -return ; + asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_AC ] = ac_rog_tunables; + asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_DC ] = dc_rog_tunables; -err_nomem: -pr_err ("Failed to allocate memory for tunables\n" ); +return 0 ; }static int __init asus_fw_init (void ) {char *wmi_uid; +int err; wmi_uid =wmi_get_acpi_device_uid (ASUS_WMI_MGMT_GUID );if (!wmi_uid)
@@ -1147,10 +1146,21 @@ static int __init asus_fw_init(void)if (!strcmp (wmi_uid,ASUS_ACPI_UID_ASUSWMI ))return -ENODEV ; -init_rog_tunables (); + err =init_rog_tunables (); +if (err) +return err;/* Must always be last step to ensure data is available */ -return asus_fw_attr_add (); + err =asus_fw_attr_add (); +if (err) + goto err_free_tunables; + +return 0 ; + +err_free_tunables: +kfree (asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_AC ]); +kfree (asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_DC ]); +return err; } static void __exit asus_fw_exit(void)
Signature · 1 line
2.55.0
PATCH v4 2/2 platform/x86: asus-armoury: use cleanup.h to manage tunables
In reply to Marco ScardoviConvert init_rog_tunables() to use the cleanup infrastructure to automatically free ac_rog_tunables if dc_rog_tunables allocation fails.
By declaring local pointers with the __free(kfree) attribute, the manual kfree() in the error path can be removed. Upon successful initialization, the ownership is transferred to the global struct using no_free_ptr().
--- drivers/platform/x86/asus-armoury.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/platform/x86/asus-armoury.c +6 -6
@@ -16,6 +16,7 @@#include <linux/acpi.h> #include <linux/array_size.h> #include <linux/bitfield.h> +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/dmi.h> #include <linux/err.h>
@@ -1010,7 +1011,8 @@ static int asus_fw_attr_add(void)static int init_rog_tunables (void ) {const struct power_limits *ac_limits, *dc_limits; -struct rog_tunables *ac_rog_tunables =NULL , *dc_rog_tunables =NULL ; + struct rog_tunables *ac_rog_tunables __free(kfree) = NULL; + struct rog_tunables *dc_rog_tunables __free(kfree) = NULL;const struct power_data *power_data;const struct dmi_system_id *dmi_id;
@@ -1080,10 +1082,8 @@ static int init_rog_tunables(void) dc_limits = power_data->dc_data;if (dc_limits) { dc_rog_tunables =kzalloc_obj (*dc_rog_tunables); -if (!dc_rog_tunables) { -kfree (ac_rog_tunables); +if (!dc_rog_tunables)return -ENOMEM ; - } dc_rog_tunables->power_limits = dc_limits;
@@ -1124,8 +1124,8 @@ static int init_rog_tunables(void)pr_debug ("No DC PPT limits defined\n" ); } - asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_AC ] = ac_rog_tunables; - asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_DC ] = dc_rog_tunables; + asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_AC ] =no_free_ptr (ac_rog_tunables); + asus_armoury.rog_tunables[ASUS_ROG_TUNABLE_DC ] =no_free_ptr (dc_rog_tunables);return 0 ; }
Signature · 1 line
2.55.0
Ilpo Järvinen
maintainerQuoted 9 lines · Marco Scardovi
This patch series addresses a Use-After-Free vulnerability and a memory leak during driver initialization in the asus-armoury driver, and modernizes its memory management.
Patch 1 fixes the UAF and memory leak by propagating the error code from
init_rog_tunables(), deferring the global pointer updates, and adding a rollback path.[...]
Thank you for your contribution, it has been applied to my local review-ilpo-next branch. Note it will show up in the public platform-drivers-x86/review-ilpo-next branch only once I've pushed my local branch there, which might take a while.
FYI [if applicable to your patch], as per Linus' policy change, also fixes are mostly routed through for-next unless the fix is for a commit introduced in the most recent cycle or is clearly a regression fix.
The list of commits applied:
[1/2] platform/x86: asus-armoury: fix Use-After-Free and memory leak in driver init
commit: 5948b235891fb699b9cb8bd7475b1f17ec3e1b96
[2/2] platform/x86: asus-armoury: use cleanup.h to manage tunables
commit: 55a3149ce66a0ada5ecaff17961c1a3ca3e95498--
i.