Skip to content

Commit 1149e7d

Browse files
cneveuxjforissier
authored andcommitted
driver: tee: Handle NULL pointer indication from client
TEE Client introduce a new capability "TEE_GEN_CAP_MEMREF_NULL" to handle the support of the shared memory buffer with a NULL pointer. This capability depends on TEE Capabilities and driver support. Driver and TEE exchange capabilities at driver initialization. Signed-off-by: Michael Whitfield <[email protected]> Signed-off-by: Cedric Neveux <[email protected]> Reviewed-by: Jens Wiklander <[email protected]> Reviewed-by: Joakim Bech <[email protected]> Tested-by: Joakim Bech <[email protected]> (QEMU)
1 parent 1ad01d3 commit 1149e7d

File tree

5 files changed

+57
-18
lines changed

5 files changed

+57
-18
lines changed

drivers/tee/optee/core.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ static void optee_get_version(struct tee_device *teedev,
216216

217217
if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
218218
v.gen_caps |= TEE_GEN_CAP_REG_MEM;
219+
if (optee->sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL)
220+
v.gen_caps |= TEE_GEN_CAP_MEMREF_NULL;
219221
*vers = v;
220222
}
221223

@@ -247,6 +249,11 @@ static int optee_open(struct tee_context *ctx)
247249
mutex_init(&ctxdata->mutex);
248250
INIT_LIST_HEAD(&ctxdata->sess_list);
249251

252+
if (optee->sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL)
253+
ctx->cap_memref_null = true;
254+
else
255+
ctx->cap_memref_null = false;
256+
250257
ctx->data = ctxdata;
251258
return 0;
252259
}

drivers/tee/optee/optee_smc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ struct optee_smc_get_shm_config_result {
215215
*/
216216
#define OPTEE_SMC_SEC_CAP_DYNAMIC_SHM BIT(2)
217217

218+
/* Secure world supports Shared Memory with a NULL buffer reference */
219+
#define OPTEE_SMC_SEC_CAP_MEMREF_NULL BIT(4)
220+
218221
#define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9
219222
#define OPTEE_SMC_EXCHANGE_CAPABILITIES \
220223
OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES)

drivers/tee/tee_core.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -268,25 +268,38 @@ static int params_from_user(struct tee_context *ctx, struct tee_param *params,
268268
case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
269269
case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
270270
/*
271-
* If we fail to get a pointer to a shared memory
272-
* object (and increase the ref count) from an
273-
* identifier we return an error. All pointers that
274-
* has been added in params have an increased ref
275-
* count. It's the callers responibility to do
276-
* tee_shm_put() on all resolved pointers.
271+
* If a NULL pointer is passed to a TA in the TEE,
272+
* the ip.c IOCTL parameters is set to TEE_MEMREF_NULL
273+
* indicating a NULL memory reference.
277274
*/
278-
shm = tee_shm_get_from_id(ctx, ip.c);
279-
if (IS_ERR(shm))
280-
return PTR_ERR(shm);
281-
282-
/*
283-
* Ensure offset + size does not overflow offset
284-
* and does not overflow the size of the referred
285-
* shared memory object.
286-
*/
287-
if ((ip.a + ip.b) < ip.a ||
288-
(ip.a + ip.b) > shm->size) {
289-
tee_shm_put(shm);
275+
if (ip.c != TEE_MEMREF_NULL) {
276+
/*
277+
* If we fail to get a pointer to a shared
278+
* memory object (and increase the ref count)
279+
* from an identifier we return an error. All
280+
* pointers that has been added in params have
281+
* an increased ref count. It's the callers
282+
* responibility to do tee_shm_put() on all
283+
* resolved pointers.
284+
*/
285+
shm = tee_shm_get_from_id(ctx, ip.c);
286+
if (IS_ERR(shm))
287+
return PTR_ERR(shm);
288+
289+
/*
290+
* Ensure offset + size does not overflow
291+
* offset and does not overflow the size of
292+
* the referred shared memory object.
293+
*/
294+
if ((ip.a + ip.b) < ip.a ||
295+
(ip.a + ip.b) > shm->size) {
296+
tee_shm_put(shm);
297+
return -EINVAL;
298+
}
299+
} else if (ctx->cap_memref_null) {
300+
/* Pass NULL pointer to OP-TEE */
301+
shm = NULL;
302+
} else {
290303
return -EINVAL;
291304
}
292305

include/linux/tee_drv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct tee_shm_pool;
4646
* and just return with an error code. It is needed for requests
4747
* that arises from TEE based kernel drivers that should be
4848
* non-blocking in nature.
49+
* @cap_memref_null: flag indicating if the TEE Client support shared
50+
* memory buffer with a NULL pointer.
4951
*/
5052
struct tee_context {
5153
struct tee_device *teedev;
@@ -54,6 +56,7 @@ struct tee_context {
5456
struct kref refcount;
5557
bool releasing;
5658
bool supp_nowait;
59+
bool cap_memref_null;
5760
};
5861

5962
struct tee_param_memref {

include/uapi/linux/tee.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
#define TEE_GEN_CAP_GP (1 << 0)/* GlobalPlatform compliant TEE */
5252
#define TEE_GEN_CAP_PRIVILEGED (1 << 1)/* Privileged device (for supplicant) */
5353
#define TEE_GEN_CAP_REG_MEM (1 << 2)/* Supports registering shared memory */
54+
#define TEE_GEN_CAP_MEMREF_NULL (1 << 3)/* NULL MemRef support */
55+
56+
#define TEE_MEMREF_NULL (__u64)(-1) /* NULL MemRef Buffer */
5457

5558
/*
5659
* TEE Implementation ID
@@ -219,6 +222,16 @@ struct tee_ioctl_buf_data {
219222
* a part of a shared memory by specifying an offset (@a) and size (@b) of
220223
* the object. To supply the entire shared memory object set the offset
221224
* (@a) to 0 and size (@b) to the previously returned size of the object.
225+
*
226+
* A client may need to present a NULL pointer in the argument
227+
* passed to a trusted application in the TEE.
228+
* This is also a requirement in GlobalPlatform Client API v1.0c
229+
* (section 3.2.5 memory references), which can be found at
230+
* http://www.globalplatform.org/specificationsdevice.asp
231+
*
232+
* If a NULL pointer is passed to a TA in the TEE, the (@c)
233+
* IOCTL parameters value must be set to TEE_MEMREF_NULL indicating a NULL
234+
* memory reference.
222235
*/
223236
struct tee_ioctl_param {
224237
__u64 attr;

0 commit comments

Comments
 (0)