IB/hfi1: Ensure correct mm is used at all times
Subsystems (4)
Dennis Dalessandro
maintainerPATCH v5 1/1 IB/hfi1: Ensure correct mm is used at all times
Two earlier bug fixes have created a security problem in the hfi1 driver. One fix aimed to solve an issue where current->mm was not valid when closing the hfi1 cdev. It attempted to do this by saving a cached value of the current->mm pointer at file open time. This is a problem if another process with access to the FD calls in via write() or ioctl() to pin pages via the hfi driver. The other fix tried to solve a use after free by taking a reference on the mm.
To fix this correctly we use the existing cached value of the mm in the mmu notifier. Now we can check in the insert, evict, etc. routines that current->mm matched what the notifier was registered for. If not, then don't allow access. The register of the mmu notifier will save the mm pointer.
Note the check in the unregister is not needed in the event that current->mm is empty. This means the tear down is happening due to a SigKill or OOM Killer, something along those lines. If current->mm has a value then it must be checked and only the task that did the register can do the unregister.
Since in do_exit() the exit_mm() is called before exit_files(), which would call our close routine a reference is needed on the mm. We rely on the mmgrab done by the registration of the notifier, whereas before it was explicit. The mmu notifier deregistration happens when the user context is torn down, the creation of which triggered the registration.
Also of note is we do not do any explicit work to protect the interval tree notifier. It doesn't seem that this is going to be needed since we aren't actually doing anything with current->mm. The interval tree notifier stuff still has a FIXME noted from a previous commit that will be addressed in a follow on patch.
---
Changes since v0:
----------------
Removed the checking of the pid and limitation that whatever task opens the dev is the only one that can do write() or ioctl(). While this limitation is OK it doesn't appear to be strictly necessary.
Rebased on top of 5.10-rc1. Testing has been done on 5.9 due to a bug in 5.10 that is being worked (separate issue).
Changes since v1:
----------------
Remove explicit mmget/put to rely on the notifier register's mmgrab instead.
Fixed missing check in rb_unregister to only check current->mm if its actually valid.
Moved mm_from_tid_node to exp_rcv header and use it
Changes since v2:
----------------
Change Reported-by to Suggested-by for Jann
Commit msg updates
Remove private mm pointer and use notifier's
Changes since v3:
-----------------
Added Ira's RB and Cc stable list
Updated commit message
Added comment to mmu_rb_unregister
Renamed confusing variable in mmu_rb_register
Changes since v4:
-----------------
Remove conditional mmu notifier registration and fix up commit msg.
--- drivers/infiniband/hw/hfi1/file_ops.c | 4 -- drivers/infiniband/hw/hfi1/hfi.h | 2 - drivers/infiniband/hw/hfi1/mmu_rb.c | 68 +++++++++++++++-------------- drivers/infiniband/hw/hfi1/mmu_rb.h | 16 ++++++- drivers/infiniband/hw/hfi1/user_exp_rcv.c | 12 +++-- drivers/infiniband/hw/hfi1/user_exp_rcv.h | 6 +++ drivers/infiniband/hw/hfi1/user_sdma.c | 13 +++--- drivers/infiniband/hw/hfi1/user_sdma.h | 7 +++ 8 files changed, 79 insertions(+), 49 deletions(-)
drivers/infiniband/hw/hfi1/file_ops.c +1 -3
@@ -1,4 +1,5 @@/* +* Copyright(c) 2020 Cornelis Networks, Inc. * Copyright(c) 2015-2020 Intel Corporation. * * This file is provided under a dual BSD/GPLv2 license. When using or
@@ -206,8 +207,6 @@ static int hfi1_file_open(struct inode *inode, struct file *fp)spin_lock_init (&fd->tid_lock);spin_lock_init (&fd->invalid_lock); fd->rec_cpu_num =-1 ;/* no cpu affinity by default */ - fd->mm = current->mm; -mmgrab (fd->mm); fd->dd = dd; fp->private_data = fd;return 0 ;
@@ -711,7 +710,6 @@ static int hfi1_file_close(struct inode *inode, struct file *fp)deallocate_ctxt (uctxt); done: -mmdrop (fdata->mm);if (atomic_dec_and_test (&dd->user_refcount))complete (&dd->user_comp);
drivers/infiniband/hw/hfi1/hfi.h +1 -1
@@ -1,6 +1,7 @@#ifndef _HFI1_KERNEL_H#define _HFI1_KERNEL_H/* +* Copyright(c) 2020 Cornelis Networks, Inc. * Copyright(c) 2015-2020 Intel Corporation. * * This file is provided under a dual BSD/GPLv2 license. When using or
@@ -1451,7 +1452,6 @@ struct hfi1_filedata {u32 invalid_tid_idx;/* protect invalid_tids array and invalid_tid_idx */ spinlock_t invalid_lock; -struct mm_struct *mm; };extern struct xarray hfi1_dev_table;
drivers/infiniband/hw/hfi1/mmu_rb.c +35 -33
136 lines not on this page · raw message
drivers/infiniband/hw/hfi1/mmu_rb.h +15 -1
@@ -1,4 +1,5 @@/* +* Copyright(c) 2020 Cornelis Networks, Inc. * Copyright(c) 2016 Intel Corporation. * * This file is provided under a dual BSD/GPLv2 license. When using or
@@ -54,6 +55,7 @@ struct mmu_rb_node {unsigned long len;unsigned long __last;struct rb_node node; +struct mmu_rb_handler *handler;struct list_head list; };
@@ -71,7 +73,19 @@ struct mmu_rb_ops { void *evict_arg, bool *stop); }; -int hfi1_mmu_rb_register (void *ops_arg,struct mm_struct *mm, +struct mmu_rb_handler { +struct mmu_notifier mn; +struct rb_root_cached root; +void *ops_arg; +spinlock_t lock;/* protect the RB tree */ +struct mmu_rb_ops *ops; +struct list_head lru_list; +struct work_struct del_work; +struct list_head del_list; +struct workqueue_struct *wq; +}; + +int hfi1_mmu_rb_register (void *ops_arg,struct mmu_rb_ops *ops,struct workqueue_struct *wq,struct mmu_rb_handler **handler);
drivers/infiniband/hw/hfi1/user_exp_rcv.c +8 -4
@@ -1,4 +1,5 @@/* +* Copyright(c) 2020 Cornelis Networks, Inc. * Copyright(c) 2015-2018 Intel Corporation. * * This file is provided under a dual BSD/GPLv2 license. When using or
@@ -173,15 +174,18 @@ static void unpin_rcv_pages(struct hfi1_filedata *fd, {struct page **pages;struct hfi1_devdata *dd = fd->uctxt->dd; +struct mm_struct *mm;if (mapped) {pci_unmap_single (dd->pcidev, node->dma_addr, node->npages *PAGE_SIZE ,PCI_DMA_FROMDEVICE ); pages = &node->pages[idx]; + mm =mm_from_tid_node (node); }else { pages = &tidbuf->pages[idx]; + mm = current->mm; } -hfi1_release_user_pages (fd->mm, pages, npages, mapped); +hfi1_release_user_pages (mm, pages, npages, mapped); fd->tid_n_pinned -= npages; }
@@ -216,12 +220,12 @@ static int pin_rcv_pages(struct hfi1_filedata *fd, struct tid_user_buf *tidbuf)* pages, accept the amount pinned so far and program only that. * User space knows how to deal with partially programmed buffers. */ -if (!hfi1_can_pin_pages (dd, fd->mm, fd->tid_n_pinned, npages)) { +if (!hfi1_can_pin_pages (dd, current->mm, fd->tid_n_pinned, npages)) {kfree (pages);return -ENOMEM ; } - pinned =hfi1_acquire_user_pages (fd->mm, vaddr, npages, true, pages); + pinned =hfi1_acquire_user_pages (current->mm, vaddr, npages, true, pages);if (pinned <=0 ) {kfree (pages);return pinned;
@@ -756,7 +760,7 @@ static int set_rcvarray_entry(struct hfi1_filedata *fd,if (fd->use_mn) { ret =mmu_interval_notifier_insert ( - &node->notifier, fd->mm, + &node->notifier, current->mm, tbuf->vaddr + (pageidx *PAGE_SIZE ), npages *PAGE_SIZE , &tid_mn_ops);if (ret)
drivers/infiniband/hw/hfi1/user_exp_rcv.h +6 -0
@@ -1,6 +1,7 @@#ifndef _HFI1_USER_EXP_RCV_H#define _HFI1_USER_EXP_RCV_H/* +* Copyright(c) 2020 - Cornelis Networks, Inc. * Copyright(c) 2015 - 2017 Intel Corporation. * * This file is provided under a dual BSD/GPLv2 license. When using or
@@ -95,4 +96,9 @@ int hfi1_user_exp_rcv_clear(struct hfi1_filedata *fd,int hfi1_user_exp_rcv_invalid (struct hfi1_filedata *fd,struct hfi1_tid_info *tinfo); +static inline struct mm_struct *mm_from_tid_node (struct tid_rb_node *node) +{ +return node->notifier.mm; +} +#endif /* _HFI1_USER_EXP_RCV_H */
drivers/infiniband/hw/hfi1/user_sdma.c +7 -6
@@ -1,4 +1,5 @@/* +* Copyright(c) 2020 - Cornelis Networks, Inc. * Copyright(c) 2015 - 2018 Intel Corporation. * * This file is provided under a dual BSD/GPLv2 license. When using or
@@ -188,7 +189,6 @@ int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt,atomic_set (&pq->n_reqs,0 );init_waitqueue_head (&pq->wait);atomic_set (&pq->n_locked,0 ); - pq->mm = fd->mm;iowait_init (&pq->busy,0 ,NULL ,NULL , defer_packet_queue, activate_packet_queue,NULL ,NULL );
@@ -230,7 +230,7 @@ int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt, cq->nentries = hfi1_sdma_comp_ring_size; - ret =hfi1_mmu_rb_register (pq, pq->mm, &sdma_rb_ops, dd->pport->hfi1_wq, + ret =hfi1_mmu_rb_register (pq, &sdma_rb_ops, dd->pport->hfi1_wq, &pq->handler);if (ret) {dd_dev_err (dd,"Failed to register with MMU %d" , ret);
@@ -980,13 +980,13 @@ static int pin_sdma_pages(struct user_sdma_request *req, npages -= node->npages; retry: -if (!hfi1_can_pin_pages (pq->dd, pq->mm, +if (!hfi1_can_pin_pages (pq->dd, current->mm,atomic_read (&pq->n_locked), npages)) { cleared =sdma_cache_evict (pq, npages);if (cleared >= npages) goto retry; } - pinned =hfi1_acquire_user_pages (pq->mm, + pinned =hfi1_acquire_user_pages (current->mm, ((unsigned long )iovec->iov.iov_base + (node->npages *PAGE_SIZE )), npages,0 , pages + node->npages);
@@ -995,7 +995,7 @@ static int pin_sdma_pages(struct user_sdma_request *req,return pinned; }if (pinned != npages) { -unpin_vector_pages (pq->mm, pages, node->npages, pinned); +unpin_vector_pages (current->mm, pages, node->npages, pinned);return -EFAULT ; }kfree (node->pages);
@@ -1008,7 +1008,8 @@ static int pin_sdma_pages(struct user_sdma_request *req,static void unpin_sdma_pages (struct sdma_mmu_node *node) {if (node->npages) { -unpin_vector_pages (node->pq->mm, node->pages,0 , node->npages); +unpin_vector_pages (mm_from_sdma_node (node), node->pages,0 , + node->npages);atomic_sub (node->npages, &node->pq->n_locked); } }
drivers/infiniband/hw/hfi1/user_sdma.h +6 -1
@@ -1,6 +1,7 @@#ifndef _HFI1_USER_SDMA_H#define _HFI1_USER_SDMA_H/* +* Copyright(c) 2020 - Cornelis Networks, Inc. * Copyright(c) 2015 - 2018 Intel Corporation. * * This file is provided under a dual BSD/GPLv2 license. When using or
@@ -133,7 +134,6 @@ struct hfi1_user_sdma_pkt_q {unsigned long unpinned;struct mmu_rb_handler *handler;atomic_t n_locked; -struct mm_struct *mm; };struct hfi1_user_sdma_comp_q {
@@ -250,4 +250,9 @@ int hfi1_user_sdma_process_request(struct hfi1_filedata *fd, struct iovec *iovec, unsigned long dim, unsigned long *count); +static inline struct mm_struct *mm_from_sdma_node (struct sdma_mmu_node *node) +{ +return node->rb.handler->mn.mm; +} +#endif /* _HFI1_USER_SDMA_H */
Quoted 14 lines · Dennis Dalessandro
Two earlier bug fixes have created a security problem in the hfi1 driver. One fix aimed to solve an issue where
current->mmwas not valid when closing the hfi1 cdev. It attempted to do this by saving a cached value of thecurrent->mmpointer at file open time. This is a problem if another process with access to the FD calls in viawrite()orioctl()to pin pages via the hfi driver. The other fix tried to solve a use after free by taking a reference on the mm.To fix this correctly we use the existing cached value of the mm in the mmu notifier. Now we can check in the insert, evict, etc. routines that
current->mmmatched what the notifier was registered for. If not, then don't allow access. The register of the mmu notifier will save the mm pointer.
Quoted 5 lines
Note the check in the unregister is not needed in the event that
current->mmis empty. This means the tear down is happening due to a SigKill or OOM Killer, something along those lines. Ifcurrent->mmhas a value then it must be checked and only the task that did the register can do the unregister.
I deleted this paragraph, it doesn't apply any more
Quoted 12 lines
Since in
do_exit()theexit_mm()is called beforeexit_files(), which would call our close routine a reference is needed on the mm. We rely on the mmgrab done by the registration of the notifier, whereas before it was explicit. The mmu notifier deregistration happens when the user context is torn down, the creation of which triggered the registration.Also of note is we do not do any explicit work to protect the interval tree notifier. It doesn't seem that this is going to be needed since we aren't actually doing anything with
current->mm. The interval tree notifier stuff still has a FIXME noted from a previous commit that will be addressed in a follow on patch.
Quoted 8 lines
Fixes e0cf75deab81 ("IB/hfi1: Fix mm_struct use after free")Fixes 3faa3d9a308e ("IB/hfi1: Make use of mm consistent")Cc <[email protected]>Suggested-by Jann Horn <[email protected]>Reported-by Jason Gunthorpe <[email protected]>Reviewed-by Ira Weiny <[email protected]>Reviewed-by Mike Marciniszyn <[email protected]>Signed-off-by Dennis Dalessandro <[email protected]>
Applied to for-rc
Thanks,
Jason