cdx: Fix double free when sysfs file creation fails
PATCH v2 1/1 cdx: Fix double free when sysfs file creation fails
In cdx_create_res_attr(), if sysfs_create_bin_file() fails, the code frees res_attr but doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Then cdx_destroy_res_attr() frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] after sysfs_create_bin_file() completes.
--- drivers/cdx/cdx.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/cdx/cdx.c +5 -2
@@ -738,7 +738,6 @@ static int cdx_create_res_attr(struct cdx_device *cdx_dev, int num)sysfs_bin_attr_init (res_attr); - cdx_dev->res_attr[num] = res_attr;sprintf (res_attr_name,"resource%d" , num); res_attr->mmap = cdx_mmap_resource;
@@ -747,8 +746,12 @@ static int cdx_create_res_attr(struct cdx_device *cdx_dev, int num) res_attr->size =cdx_resource_len (cdx_dev, num); res_attr->private = (void *)(unsigned long )num; ret =sysfs_create_bin_file (&cdx_dev->dev.kobj, res_attr); -if (ret) +if (ret) {kfree (res_attr); +return ret; + } + + cdx_dev->res_attr[num] = res_attr;return ret; }
Signature · 1 line
2.49.0
Quoted 13 lines
-----Original Message-----
From: Prasanna Kumar T S M <[email protected]>
Sent: Friday, July 24, 2026 2:57 PM
To: Gupta, Nipun <[email protected]>; Agarwal, Nikhil <[email protected]>; [email protected]; Gupta, Puneet (DCG- ENG) <[email protected]>; Gangurde, Abhijit <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: [PATCH v2] cdx: Fix double free when sysfs file creation fails
Quoted 8 lines
In
cdx_create_res_attr(), ifsysfs_create_bin_file()fails, the code freesres_attrbut doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Thencdx_destroy_res_attr()frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] aftersysfs_create_bin_file()completes.Fixes aeda33ab8160 ("cdx: create sysfs bin files for cdx resources")Signed-off-by Prasanna Kumar T S M <[email protected]>