Private shared memory object must not be referenced from user space. To guarantee that, don't assign an id to shared memory objects which are driver private.
Unified · Split
| @@ -37,7 +37,8 @@ struct tee_shm_pool { · 36 unmodified lines | ||
| 37 | 37 | * @num_users: number of active users of this device |
| 38 | 38 | * @c_no_user: completion used when unregistering the device |
| 39 | 39 | * @mutex: mutex protecting @num_users and @idr |
| 40 | − * @idr: register of shared memory object allocated on this device | |
| 40 | + * @idr: register of user space shared memory objects allocated or | |
| 41 | + * registered on this device | |
| 41 | 42 | * @pool: shared memory pool |
| 42 | 43 | */ |
| 43 | 44 | struct tee_device { |
| @@ -15,9 +15,11 @@ static void tee_shm_release(struct tee_shm *shm) · 14 unmodified lines | ||
| 15 | 15 | { |
| 16 | 16 | struct tee_device *teedev = shm->teedev; |
| 17 | 17 | |
| 18 | − mutex_lock(&teedev->mutex); | |
| 19 | − idr_remove(&teedev->idr, shm->id); | |
| 20 | − mutex_unlock(&teedev->mutex); | |
| 18 | + if (shm->flags & TEE_SHM_DMA_BUF) { | |
| 19 | + mutex_lock(&teedev->mutex); | |
| 20 | + idr_remove(&teedev->idr, shm->id); | |
| 21 | + mutex_unlock(&teedev->mutex); | |
| 22 | + } | |
| 21 | 23 | |
| 22 | 24 | if (shm->flags & TEE_SHM_POOL) { |
| 23 | 25 | struct tee_shm_pool_mgr *poolm; |
| @@ -137,17 +139,18 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) · 113 unmodified lines | ||
| 137 | 139 | goto err_kfree; |
| 138 | 140 | } |
| 139 | 141 | |
| 140 | − mutex_lock(&teedev->mutex); | |
| 141 | − shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); | |
| 142 | − mutex_unlock(&teedev->mutex); | |
| 143 | − if (shm->id < 0) { | |
| 144 | − ret = ERR_PTR(shm->id); | |
| 145 | − goto err_pool_free; | |
| 146 | − } | |
| 147 | 142 | |
| 148 | 143 | if (flags & TEE_SHM_DMA_BUF) { |
| 149 | 144 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 150 | 145 | |
| 146 | + mutex_lock(&teedev->mutex); | |
| 147 | + shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); | |
| 148 | + mutex_unlock(&teedev->mutex); | |
| 149 | + if (shm->id < 0) { | |
| 150 | + ret = ERR_PTR(shm->id); | |
| 151 | + goto err_pool_free; | |
| 152 | + } | |
| 153 | + | |
| 151 | 154 | exp_info.ops = &tee_shm_dma_buf_ops; |
| 152 | 155 | exp_info.size = shm->size; |
| 153 | 156 | exp_info.flags = O_RDWR; |
| @@ -165,9 +168,11 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) · 11 unmodified lines | ||
| 165 | 168 | |
| 166 | 169 | return shm; |
| 167 | 170 | err_rem: |
| 168 | − mutex_lock(&teedev->mutex); | |
| 169 | − idr_remove(&teedev->idr, shm->id); | |
| 170 | − mutex_unlock(&teedev->mutex); | |
| 171 | + if (flags & TEE_SHM_DMA_BUF) { | |
| 172 | + mutex_lock(&teedev->mutex); | |
| 173 | + idr_remove(&teedev->idr, shm->id); | |
| 174 | + mutex_unlock(&teedev->mutex); | |
| 175 | + } | |
| 171 | 176 | err_pool_free: |
| 172 | 177 | poolm->ops->free(poolm, shm); |
| 173 | 178 | err_kfree: |