Skip to content

Commit 7d42e09

Browse files
dmuszynsherbertx
authored andcommitted
crypto: qat - resolve race condition during AER recovery
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]>
1 parent c2304e1 commit 7d42e09

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
@@ -130,7 +130,8 @@ static void adf_device_reset_worker(struct work_struct *work)
130130
if (adf_dev_restart(accel_dev)) {
131131
/* The device hanged and we can't restart it so stop here */
132132
dev_err(&GET_DEV(accel_dev), "Restart device failed\n");
133-
if (reset_data->mode == ADF_DEV_RESET_ASYNC)
133+
if (reset_data->mode == ADF_DEV_RESET_ASYNC ||
134+
completion_done(&reset_data->compl))
134135
kfree(reset_data);
135136
WARN(1, "QAT: device restart failed. Device is unusable\n");
136137
return;
@@ -146,11 +147,19 @@ static void adf_device_reset_worker(struct work_struct *work)
146147
adf_dev_restarted_notify(accel_dev);
147148
clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
148149

149-
/* The dev is back alive. Notify the caller if in sync mode */
150-
if (reset_data->mode == ADF_DEV_RESET_SYNC)
151-
complete(&reset_data->compl);
152-
else
150+
/*
151+
* The dev is back alive. Notify the caller if in sync mode
152+
*
153+
* If device restart will take a more time than expected,
154+
* the schedule_reset() function can timeout and exit. This can be
155+
* detected by calling the completion_done() function. In this case
156+
* the reset_data structure needs to be freed here.
157+
*/
158+
if (reset_data->mode == ADF_DEV_RESET_ASYNC ||
159+
completion_done(&reset_data->compl))
153160
kfree(reset_data);
161+
else
162+
complete(&reset_data->compl);
154163
}
155164

156165
static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
@@ -183,8 +192,9 @@ static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
183192
dev_err(&GET_DEV(accel_dev),
184193
"Reset device timeout expired\n");
185194
ret = -EFAULT;
195+
} else {
196+
kfree(reset_data);
186197
}
187-
kfree(reset_data);
188198
return ret;
189199
}
190200
return 0;

0 commit comments

Comments
 (0)