Skip to content

Commit 8e81cd5

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 5a9f7e9 commit 8e81cd5

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

drivers/crypto/qat/qat_common/adf_aer.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,28 @@ static void adf_device_reset_worker(struct work_struct *work)
139139
if (adf_dev_init(accel_dev) || adf_dev_start(accel_dev)) {
140140
/* The device hanged and we can't restart it so stop here */
141141
dev_err(&GET_DEV(accel_dev), "Restart device failed\n");
142-
if (reset_data->mode == ADF_DEV_RESET_ASYNC)
142+
if (reset_data->mode == ADF_DEV_RESET_ASYNC ||
143+
completion_done(&reset_data->compl))
143144
kfree(reset_data);
144145
WARN(1, "QAT: device restart failed. Device is unusable\n");
145146
return;
146147
}
147148
adf_dev_restarted_notify(accel_dev);
148149
clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
149150

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

157166
static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
@@ -184,8 +193,9 @@ static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
184193
dev_err(&GET_DEV(accel_dev),
185194
"Reset device timeout expired\n");
186195
ret = -EFAULT;
196+
} else {
197+
kfree(reset_data);
187198
}
188-
kfree(reset_data);
189199
return ret;
190200
}
191201
return 0;

0 commit comments

Comments
 (0)