Skip to content

Commit bb279ea

Browse files
dmuszynsgregkh
authored andcommitted
crypto: qat - resolve race condition during AER recovery
[ Upstream commit 7d42e09 ] During the PCI AER system's error recovery process, the kernel driver may encounter a race condition with freeing the reset_data structure's memory. If the device restart will take more than 10 seconds the function scheduling that restart will exit due to a timeout, and the reset_data structure will be freed. However, this data structure is used for completion notification after the restart is completed, which leads to a UAF bug. This results in a KFENCE bug notice. BUG: KFENCE: use-after-free read in adf_device_reset_worker+0x38/0xa0 [intel_qat] Use-after-free read at 0x00000000bc56fddf (in kfence-#142): adf_device_reset_worker+0x38/0xa0 [intel_qat] process_one_work+0x173/0x340 To resolve this race condition, the memory associated to the container of the work_struct is freed on the worker if the timeout expired, otherwise on the function that schedules the worker. The timeout detection can be done by checking if the caller is still waiting for completion or not by using completion_done() function. Fixes: d8cba25 ("crypto: qat - Intel(R) QAT driver framework") Cc: <[email protected]> Signed-off-by: Damian Muszynski <[email protected]> Reviewed-by: Giovanni Cabiddu <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 66d301a commit bb279ea

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

drivers/crypto/intel/qat/qat_common/adf_aer.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,28 @@ static void adf_device_reset_worker(struct work_struct *work)
9292
if (adf_dev_restart(accel_dev)) {
9393
/* The device hanged and we can't restart it so stop here */
9494
dev_err(&GET_DEV(accel_dev), "Restart device failed\n");
95-
if (reset_data->mode == ADF_DEV_RESET_ASYNC)
95+
if (reset_data->mode == ADF_DEV_RESET_ASYNC ||
96+
completion_done(&reset_data->compl))
9697
kfree(reset_data);
9798
WARN(1, "QAT: device restart failed. Device is unusable\n");
9899
return;
99100
}
100101
adf_dev_restarted_notify(accel_dev);
101102
clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
102103

103-
/* The dev is back alive. Notify the caller if in sync mode */
104-
if (reset_data->mode == ADF_DEV_RESET_SYNC)
105-
complete(&reset_data->compl);
106-
else
104+
/*
105+
* The dev is back alive. Notify the caller if in sync mode
106+
*
107+
* If device restart will take a more time than expected,
108+
* the schedule_reset() function can timeout and exit. This can be
109+
* detected by calling the completion_done() function. In this case
110+
* the reset_data structure needs to be freed here.
111+
*/
112+
if (reset_data->mode == ADF_DEV_RESET_ASYNC ||
113+
completion_done(&reset_data->compl))
107114
kfree(reset_data);
115+
else
116+
complete(&reset_data->compl);
108117
}
109118

110119
static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
@@ -137,8 +146,9 @@ static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
137146
dev_err(&GET_DEV(accel_dev),
138147
"Reset device timeout expired\n");
139148
ret = -EFAULT;
149+
} else {
150+
kfree(reset_data);
140151
}
141-
kfree(reset_data);
142152
return ret;
143153
}
144154
return 0;

0 commit comments

Comments
 (0)