platform/x86: hp-bioscfg: concurrent writes to the SPM sk/kek attributes can free each other's key buffer
Hi,
sk_store() and kek_store() in drivers/platform/x86/hp/hp-bioscfg/spmobj-attributes.c keep their temporary copy of the user's key in a global pointer and use it without any lock (mainline ebb7a8dc1e74):
/* :237 */
bioscfg_drv.spm_data.signing_key = kmemdup(buf, length,
GFP_KERNEL);
...
ret = hp_wmi_perform_query(HPWMI_SECUREPLATFORM_SET_SK, ...,
(void *)bioscfg_drv.spm_data.signing_key, length, 0);
...
kfree(bioscfg_drv.spm_data.signing_key); /* :252 */
bioscfg_drv.spm_data.signing_key = NULL;kek_store() does the same with spm_data.endorsement_key (:272-290).
sysfs serialises writes only per open file, so two writes to /sys/class/firmware-attributes/hp-bioscfg/authentication/SPM/sk (or kek) through two open file descriptions run concurrently. Then:
W1: signing_key = buf1
W2: signing_key = buf2 (buf1 is now leaked)
W1: hp_wmi_perform_query(.., signing_key=buf2, len1)
W2: hp_wmi_perform_query(..) ... kfree(buf2); signing_key = NULL
W1: ... kfree(NULL) or a second kfree(buf2)
Depending on the interleaving this gives a use-after-free read of the key buffer in hp_wmi_perform_query() (it copies the buffer into the WMI input), a double free, the wrong key (or a buffer of the wrong length) sent to the BIOS, or a leak. Since the pointer is only a temporary for one call, using a local variable in both functions (or a mutex around the sequence) would remove the race.
Affected: since the driver was added (b2715aa2e135, 2023-07); unchanged in mainline ebb7a8dc1e74 and next-20261125. This is different from a7508c7959ff ("fix heap OOB read in sk_store() and kek_store()"), which fixed a single-threaded length issue. Trigger: root only (the attributes are 0200) on HP machines with Secure Platform Management. Impact: memory corruption or wrong key material sent to the firmware.
Found by a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and this report. The tool runs verbatim copies of the kernel functions with stubs for the rest of the kernel; it found the interleaving above, and the interleaving was replayed natively on that harness under the tool's schedule. Functions
involved: sk_store() and kek_store() (the WMI call stubbed). It has not been reproduced on HP hardware.
Thanks,
Zhenwen Liang