Skip to content

Commit 6348dd2

Browse files
Charan Teja Kallasumitsemwal
authored andcommitted
dmabuf: use spinlock to access dmabuf->name
There exists a sleep-while-atomic bug while accessing the dmabuf->name under mutex in the dmabuffs_dname(). This is caused from the SELinux permissions checks on a process where it tries to validate the inherited files from fork() by traversing them through iterate_fd() (which traverse files under spin_lock) and call match_file(security/selinux/hooks.c) where the permission checks happen. This audit information is logged using dump_common_audit_data() where it calls d_path() to get the file path name. If the file check happen on the dmabuf's fd, then it ends up in ->dmabuffs_dname() and use mutex to access dmabuf->name. The flow will be like below: flush_unauthorized_files() iterate_fd() spin_lock() --> Start of the atomic section. match_file() file_has_perm() avc_has_perm() avc_audit() slow_avc_audit() common_lsm_audit() dump_common_audit_data() audit_log_d_path() d_path() dmabuffs_dname() mutex_lock()--> Sleep while atomic. Call trace captured (on 4.19 kernels) is below: ___might_sleep+0x204/0x208 __might_sleep+0x50/0x88 __mutex_lock_common+0x5c/0x1068 __mutex_lock_common+0x5c/0x1068 mutex_lock_nested+0x40/0x50 dmabuffs_dname+0xa0/0x170 d_path+0x84/0x290 audit_log_d_path+0x74/0x130 common_lsm_audit+0x334/0x6e8 slow_avc_audit+0xb8/0xf8 avc_has_perm+0x154/0x218 file_has_perm+0x70/0x180 match_file+0x60/0x78 iterate_fd+0x128/0x168 selinux_bprm_committing_creds+0x178/0x248 security_bprm_committing_creds+0x30/0x48 install_exec_creds+0x1c/0x68 load_elf_binary+0x3a4/0x14e0 search_binary_handler+0xb0/0x1e0 So, use spinlock to access dmabuf->name to avoid sleep-while-atomic. Cc: <[email protected]> [5.3+] Signed-off-by: Charan Teja Kalla <[email protected]> Reviewed-by: Michael J. Ruhl <[email protected]> Acked-by: Christian König <[email protected]> [sumits: added comment to spinlock_t definition to avoid warning] Signed-off-by: Sumit Semwal <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 2249357 commit 6348dd2

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

drivers/dma-buf/dma-buf.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen)
4545
size_t ret = 0;
4646

4747
dmabuf = dentry->d_fsdata;
48-
dma_resv_lock(dmabuf->resv, NULL);
48+
spin_lock(&dmabuf->name_lock);
4949
if (dmabuf->name)
5050
ret = strlcpy(name, dmabuf->name, DMA_BUF_NAME_LEN);
51-
dma_resv_unlock(dmabuf->resv);
51+
spin_unlock(&dmabuf->name_lock);
5252

5353
return dynamic_dname(dentry, buffer, buflen, "/%s:%s",
5454
dentry->d_name.name, ret > 0 ? name : "");
@@ -338,8 +338,10 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
338338
kfree(name);
339339
goto out_unlock;
340340
}
341+
spin_lock(&dmabuf->name_lock);
341342
kfree(dmabuf->name);
342343
dmabuf->name = name;
344+
spin_unlock(&dmabuf->name_lock);
343345

344346
out_unlock:
345347
dma_resv_unlock(dmabuf->resv);
@@ -402,10 +404,10 @@ static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
402404
/* Don't count the temporary reference taken inside procfs seq_show */
403405
seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1);
404406
seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name);
405-
dma_resv_lock(dmabuf->resv, NULL);
407+
spin_lock(&dmabuf->name_lock);
406408
if (dmabuf->name)
407409
seq_printf(m, "name:\t%s\n", dmabuf->name);
408-
dma_resv_unlock(dmabuf->resv);
410+
spin_unlock(&dmabuf->name_lock);
409411
}
410412

411413
static const struct file_operations dma_buf_fops = {
@@ -542,6 +544,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
542544
dmabuf->size = exp_info->size;
543545
dmabuf->exp_name = exp_info->exp_name;
544546
dmabuf->owner = exp_info->owner;
547+
spin_lock_init(&dmabuf->name_lock);
545548
init_waitqueue_head(&dmabuf->poll);
546549
dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll;
547550
dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0;

include/linux/dma-buf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ struct dma_buf {
311311
void *vmap_ptr;
312312
const char *exp_name;
313313
const char *name;
314+
spinlock_t name_lock; /* spinlock to protect name access */
314315
struct module *owner;
315316
struct list_head list_node;
316317
void *priv;

0 commit comments

Comments
 (0)