Skip to content

Commit 75d95e2

Browse files
anirudhrbgregkh
authored andcommitted
firmware_loader: fix use-after-free in firmware_fallback_sysfs
This use-after-free happens when a fw_priv object has been freed but hasn't been removed from the pending list (pending_fw_head). The next time fw_load_sysfs_fallback tries to insert into the list, it ends up accessing the pending_list member of the previously freed fw_priv. The root cause here is that all code paths that abort the fw load don't delete it from the pending list. For example: _request_firmware() -> fw_abort_batch_reqs() -> fw_state_aborted() To fix this, delete the fw_priv from the list in __fw_set_state() if the new state is DONE or ABORTED. This way, all aborts will remove the fw_priv from the list. Accordingly, remove calls to list_del_init that were being made before calling fw_state_(aborted|done). Also, in fw_load_sysfs_fallback, don't add the fw_priv to the pending list if it is already aborted. Instead, just jump out and return early. Fixes: bcfbd35 ("firmware: fix a double abort case with fw_load_sysfs_fallback") Cc: stable <[email protected]> Reported-by: [email protected] Tested-by: [email protected] Reviewed-by: Shuah Khan <[email protected]> Acked-by: Luis Chamberlain <[email protected]> Signed-off-by: Anirudh Rayabharam <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0d6434e commit 75d95e2

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

drivers/base/firmware_loader/fallback.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ static void __fw_load_abort(struct fw_priv *fw_priv)
8989
{
9090
/*
9191
* There is a small window in which user can write to 'loading'
92-
* between loading done and disappearance of 'loading'
92+
* between loading done/aborted and disappearance of 'loading'
9393
*/
94-
if (fw_sysfs_done(fw_priv))
94+
if (fw_state_is_aborted(fw_priv) || fw_sysfs_done(fw_priv))
9595
return;
9696

97-
list_del_init(&fw_priv->pending_list);
9897
fw_state_aborted(fw_priv);
9998
}
10099

@@ -280,7 +279,6 @@ static ssize_t firmware_loading_store(struct device *dev,
280279
* Same logic as fw_load_abort, only the DONE bit
281280
* is ignored and we set ABORT only on failure.
282281
*/
283-
list_del_init(&fw_priv->pending_list);
284282
if (rc) {
285283
fw_state_aborted(fw_priv);
286284
written = rc;
@@ -513,6 +511,11 @@ static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
513511
}
514512

515513
mutex_lock(&fw_lock);
514+
if (fw_state_is_aborted(fw_priv)) {
515+
mutex_unlock(&fw_lock);
516+
retval = -EINTR;
517+
goto out;
518+
}
516519
list_add(&fw_priv->pending_list, &pending_fw_head);
517520
mutex_unlock(&fw_lock);
518521

@@ -538,6 +541,7 @@ static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
538541
} else if (fw_priv->is_paged_buf && !fw_priv->data)
539542
retval = -ENOMEM;
540543

544+
out:
541545
device_del(f_dev);
542546
err_put_dev:
543547
put_device(f_dev);

drivers/base/firmware_loader/firmware.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,16 @@ static inline void __fw_state_set(struct fw_priv *fw_priv,
117117

118118
WRITE_ONCE(fw_st->status, status);
119119

120-
if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
120+
if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED) {
121+
#ifdef CONFIG_FW_LOADER_USER_HELPER
122+
/*
123+
* Doing this here ensures that the fw_priv is deleted from
124+
* the pending list in all abort/done paths.
125+
*/
126+
list_del_init(&fw_priv->pending_list);
127+
#endif
121128
complete_all(&fw_st->completion);
129+
}
122130
}
123131

124132
static inline void fw_state_aborted(struct fw_priv *fw_priv)

drivers/base/firmware_loader/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,10 @@ static void fw_abort_batch_reqs(struct firmware *fw)
783783
return;
784784

785785
fw_priv = fw->priv;
786+
mutex_lock(&fw_lock);
786787
if (!fw_state_is_aborted(fw_priv))
787788
fw_state_aborted(fw_priv);
789+
mutex_unlock(&fw_lock);
788790
}
789791

790792
/* called from request_firmware() and request_firmware_work_func() */

0 commit comments

Comments
 (0)