EDAC/mc: Fix use-after-free and memleaks during device removal
PATCH v2 1/1 EDAC/mc: Fix use-after-free and memleaks during device removal
A test kernel with the options set below revealed several issues when removing a mci device:
DEBUG_TEST_DRIVER_REMOVE
KASAN
DEBUG_KMEMLEAK
Issues seen:
1) Use-after-free:
Quoted 2 lines · John Garry
[ 22.104498] BUG: KASAN: use-after-free in
edac_remove_sysfs_mci_device+0x148/0x180
The use-after-free is caused by the mci_for_each_dimm() iterator that is called in edac_remove_sysfs_mci_device(). The iterator was introduced with commit c498afaf7df8 ("EDAC: Introduce an mci_for_each_dimm() iterator"). The iterator loop calls function device_unregister(&dimm->dev), which removes the sysfs entry of the device, but also frees the dimm struct in dimm_attr_release(). When incrementing the loop in mci_for_each_dimm(), the dimm struct is accessed again, but it is already freed.
The fix is to free all the mci device's subsequent dimm and csrow objects at a later point when the mci device is freed. This keeps the data structures intact and the mci device can be fully used until its removal.
2) Memory leaks:
Following memory leaks have been detected:
# grep edac /sys/kernel/debug/kmemleak | sort | uniq -c
1 [<000000003c0f58f9>] edac_mc_alloc+0x3bc/0x9d0 # mci->csrows
16 [<00000000bb932dc0>] edac_mc_alloc+0x49c/0x9d0 # csr->channels
16 [<00000000e2734dba>] edac_mc_alloc+0x518/0x9d0 # csr->channels[chn]
1 [<00000000eb040168>] edac_mc_alloc+0x5c8/0x9d0 # mci->dimms
34 [<00000000ef737c29>] ghes_edac_register+0x1c8/0x3f8 # see edac_mc_alloc()All leaks are from memory created by edac_mc_alloc().
Note: The test above shows that edac_mc_alloc() was called here from ghes_edac_register(), thus both functions show up in the stack dump, but the driver causing the leaks is edac_mc. The comments with the data structures involved were made manually by analyzing the objdump.
The data structures listed above and created by edac_mc_alloc() are not properly removed during device removal, which is done in edac_mc_free(). There are two paths implemented to remove the device depending on device registration, _edac_mc_free() is called if the device is not registered and edac_unregister_sysfs() otherwise. The implemenations differ. For the sysfs case the mci device removal lacks the removal of subsequent data structures (csrows, channels, dimms). This causes the memory leaks (see mci_attr_release()).
Fixing this as follows:
Unify code and implement a mci_release() function which is used to remove a struct mci regardless of the device registration status. Use put_device() to release the struct. Free all subsequent data structs of the mci's children in that release function. An effect of this is that no data is freed in edac_mc_sysfs.c (except the "mc" sysfs root node). All sysfs entries have the mci device as a parent, so its refcount will keep the mci parent as long as sysfs entries exist. This prevents struct mci from being freed until all sysfs entries have been removed which is done in edac_remove_sysfs_mci_device(). With the changes made the mci_for_each_dimm() loop is now save to release dimm devices from sysfs.
The patch has been tested with the above kernel options, no issues seen any longer.
---
V2:
* added Fixes tags,
* added Aristeu's ACK,
* reworded patch description,
* no code changes
--- drivers/edac/edac_mc.c | 20 +++---- drivers/edac/edac_mc_sysfs.c | 100 +++++++++++++---------------------- drivers/edac/edac_module.h | 1 - 3 files changed, 49 insertions(+), 72 deletions(-)
drivers/edac/edac_mc.c +11 -9
@@ -278,6 +278,12 @@ void *edac_align_ptr(void **p, unsigned int size, int n_elems)static void _edac_mc_free (struct mem_ctl_info *mci) { +put_device (&mci->dev); +} + +static void mci_release (struct device *dev) +{ + struct mem_ctl_info *mci = container_of(dev, struct mem_ctl_info, dev);struct csrow_info *csr;int i, chn, row;
@@ -371,6 +377,9 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,if (mci ==NULL )return NULL ; + mci->dev.release = mci_release; +device_initialize (&mci->dev); +/* Adjust pointers so they point within the memory we just allocated * rather than an imaginary chunk of memory located at address 0. */
@@ -505,16 +514,9 @@ void edac_mc_free(struct mem_ctl_info *mci) {edac_dbg (1 ,"\n" ); -/* If we're not yet registered with sysfs free only what was allocated -* in edac_mc_alloc(). -*/ -if (!device_is_registered (&mci->dev)) { -_edac_mc_free (mci); -return ; - } +edac_remove_sysfs_mci_device (mci); -/* the mci instance is freed here, when the sysfs object is dropped */ -edac_unregister_sysfs (mci); +_edac_mc_free (mci); }EXPORT_SYMBOL_GPL (edac_mc_free);
drivers/edac/edac_mc_sysfs.c +38 -62
217 lines not on this page · raw message
drivers/edac/edac_module.h +0 -1
@@ -28,7 +28,6 @@ void edac_mc_sysfs_exit(void);extern int edac_create_sysfs_mci_device (struct mem_ctl_info *mci,const struct attribute_group **groups);extern void edac_remove_sysfs_mci_device (struct mem_ctl_info *mci); -void edac_unregister_sysfs (struct mem_ctl_info *mci);extern int edac_get_log_ue (void );extern int edac_get_log_ce (void );extern int edac_get_panic_on_ue (void );
Signature · 1 line
2.20.1
Quoted 80 lines · Robert Richter
A test kernel with the options set below revealed several issues when removing a mci device:
DEBUG_TEST_DRIVER_REMOVE
KASAN
DEBUG_KMEMLEAKIssues seen:
1) Use-after-free:
Quoted 2 lines · John Garry
[ 22.104498] BUG: KASAN: use-after-free in
edac_remove_sysfs_mci_device+0x148/0x180The use-after-free is caused by the
mci_for_each_dimm()iterator that is called inedac_remove_sysfs_mci_device(). The iterator was introduced with commitc498afaf7df8("EDAC: Introduce anmci_for_each_dimm()iterator"). The iterator loop calls functiondevice_unregister(&dimm->dev), which removes the sysfs entry of the device, but also frees the dimm struct indimm_attr_release(). When incrementing the loop inmci_for_each_dimm(), the dimm struct is accessed again, but it is already freed.The fix is to free all the mci device's subsequent dimm and csrow objects at a later point when the mci device is freed. This keeps the data structures intact and the mci device can be fully used until its removal.
2) Memory leaks:
Following memory leaks have been detected:
# grep edac /sys/kernel/debug/kmemleak | sort | uniq -c 1 [<000000003c0f58f9>] edac_mc_alloc+0x3bc/0x9d0 # mci->csrows 16 [<00000000bb932dc0>] edac_mc_alloc+0x49c/0x9d0 # csr->channels 16 [<00000000e2734dba>] edac_mc_alloc+0x518/0x9d0 # csr->channels[chn] 1 [<00000000eb040168>] edac_mc_alloc+0x5c8/0x9d0 # mci->dimms 34 [<00000000ef737c29>] ghes_edac_register+0x1c8/0x3f8 # see edac_mc_alloc()All leaks are from memory created by
edac_mc_alloc().Note: The test above shows that
edac_mc_alloc()was called here fromghes_edac_register(), thus both functions show up in the stack dump, but the driver causing the leaks isedac_mc. The comments with the data structures involved were made manually by analyzing the objdump.The data structures listed above and created by
edac_mc_alloc()are not properly removed during device removal, which is done inedac_mc_free(). There are two paths implemented to remove the device depending on device registration,_edac_mc_free()is called if the device is not registered andedac_unregister_sysfs()otherwise. The implemenations differ. For the sysfs case the mci device removal lacks the removal of subsequent data structures (csrows, channels, dimms). This causes the memory leaks (seemci_attr_release()).Fixing this as follows:
Unify code and implement a
mci_release()function which is used to remove a struct mci regardless of the device registration status. Useput_device()to release the struct. Free all subsequent data structs of the mci's children in that release function. An effect of this is that no data is freed inedac_mc_sysfs.c(except the "mc" sysfs root node). All sysfs entries have the mci device as a parent, so its refcount will keep the mci parent as long as sysfs entries exist. This prevents struct mci from being freed until all sysfs entries have been removed which is done inedac_remove_sysfs_mci_device(). With the changes made themci_for_each_dimm()loop is now save to release dimm devices from sysfs.The patch has been tested with the above kernel options, no issues seen any longer.
Reported-by John Garry <[email protected]>Fixes c498afaf7df8 ("EDAC: Introduce an mci_for_each_dimm() iterator")Fixes faa2ad09c01c ("edac_mc: edac_mc_free() cannot assume mem_ctl_info is registered in sysfs.")Fixes 7a623c039075 ("edac: rewrite the sysfs code to use struct device")Signed-off-by Robert Richter <[email protected]>Acked-by Aristeu Rozanski <[email protected]>Signed-off-by Robert Richter <[email protected]>
Quoted 2 lines
---
V2:
Kasan warnings and leak reports are gone:
Cheers