Skip to content

Commit 987fd1a

Browse files
jxu75herbertx
authored andcommitted
crypto: qat - optimize allocations for fw authentication
The memory requested to hold the image data for authentication will never exceed `ICP_QAT_CSS_RSA4K_MAX_IMAGE_LEN`. Therefore, we can simplify the allocation by always requesting the maximum size needed for any image. Also introduce the following checks: * Ensure the allocated memory is 8-byte aligned to meet the requirements of the authentication firmware. * Prevent overflow when constructing the authentication descriptor. Signed-off-by: Jack Xu <[email protected]> Reviewed-by: Ahsan Atta <[email protected]> Reviewed-by: Giovanni Cabiddu <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Signed-off-by: Giovanni Cabiddu <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 0d5cb73 commit 987fd1a

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

drivers/crypto/intel/qat/qat_common/icp_qat_uclo.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#define ICP_QAT_SUOF_OBJS "SUF_OBJS"
4444
#define ICP_QAT_SUOF_IMAG "SUF_IMAG"
4545
#define ICP_QAT_SIMG_AE_INIT_SEQ_LEN (50 * sizeof(unsigned long long))
46-
#define ICP_QAT_SIMG_AE_INSTS_LEN (0x4000 * sizeof(unsigned long long))
4746

4847
#define DSS_FWSK_MODULUS_LEN 384 /* RSA3K */
4948
#define DSS_FWSK_EXPONENT_LEN 4
@@ -75,13 +74,6 @@
7574
DSS_SIGNATURE_LEN : \
7675
CSS_SIGNATURE_LEN)
7776

78-
#define ICP_QAT_CSS_AE_IMG_LEN (sizeof(struct icp_qat_simg_ae_mode) + \
79-
ICP_QAT_SIMG_AE_INIT_SEQ_LEN + \
80-
ICP_QAT_SIMG_AE_INSTS_LEN)
81-
#define ICP_QAT_CSS_AE_SIMG_LEN(handle) (sizeof(struct icp_qat_css_hdr) + \
82-
ICP_QAT_CSS_FWSK_PUB_LEN(handle) + \
83-
ICP_QAT_CSS_SIGNATURE_LEN(handle) + \
84-
ICP_QAT_CSS_AE_IMG_LEN)
8577
#define ICP_QAT_AE_IMG_OFFSET(handle) (sizeof(struct icp_qat_css_hdr) + \
8678
ICP_QAT_CSS_FWSK_MODULUS_LEN(handle) + \
8779
ICP_QAT_CSS_FWSK_EXPONENT_LEN(handle) + \

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
22
/* Copyright(c) 2014 - 2020 Intel Corporation */
3+
#include <linux/align.h>
34
#include <linux/slab.h>
45
#include <linux/ctype.h>
56
#include <linux/kernel.h>
@@ -1414,16 +1415,21 @@ static int qat_uclo_map_auth_fw(struct icp_qat_fw_loader_handle *handle,
14141415
struct icp_qat_fw_auth_desc *auth_desc;
14151416
struct icp_qat_auth_chunk *auth_chunk;
14161417
u64 virt_addr, bus_addr, virt_base;
1417-
unsigned int length, simg_offset = sizeof(*auth_chunk);
1418+
unsigned int simg_offset = sizeof(*auth_chunk);
14181419
struct icp_qat_simg_ae_mode *simg_ae_mode;
14191420
struct icp_firml_dram_desc img_desc;
1421+
int ret;
14201422

1421-
length = (css_hdr->fw_type == CSS_AE_FIRMWARE) ?
1422-
ICP_QAT_CSS_AE_SIMG_LEN(handle) + simg_offset :
1423-
size + ICP_QAT_CSS_FWSK_PAD_LEN(handle) + simg_offset;
1424-
if (qat_uclo_simg_alloc(handle, &img_desc, length)) {
1423+
ret = qat_uclo_simg_alloc(handle, &img_desc, ICP_QAT_CSS_RSA4K_MAX_IMAGE_LEN);
1424+
if (ret) {
14251425
pr_err("QAT: error, allocate continuous dram fail\n");
1426-
return -ENOMEM;
1426+
return ret;
1427+
}
1428+
1429+
if (!IS_ALIGNED(img_desc.dram_size, 8) || !img_desc.dram_bus_addr) {
1430+
pr_debug("QAT: invalid address\n");
1431+
qat_uclo_simg_free(handle, &img_desc);
1432+
return -EINVAL;
14271433
}
14281434

14291435
auth_chunk = img_desc.dram_base_addr_v;
@@ -1481,6 +1487,13 @@ static int qat_uclo_map_auth_fw(struct icp_qat_fw_loader_handle *handle,
14811487
auth_desc->img_high = (unsigned int)(bus_addr >> BITS_IN_DWORD);
14821488
auth_desc->img_low = (unsigned int)bus_addr;
14831489
auth_desc->img_len = size - ICP_QAT_AE_IMG_OFFSET(handle);
1490+
if (bus_addr + auth_desc->img_len > img_desc.dram_bus_addr +
1491+
ICP_QAT_CSS_RSA4K_MAX_IMAGE_LEN) {
1492+
pr_err("QAT: insufficient memory size for authentication data\n");
1493+
qat_uclo_simg_free(handle, &img_desc);
1494+
return -ENOMEM;
1495+
}
1496+
14841497
memcpy((void *)(uintptr_t)virt_addr,
14851498
(void *)(image + ICP_QAT_AE_IMG_OFFSET(handle)),
14861499
auth_desc->img_len);

0 commit comments

Comments
 (0)