1. 01 9月, 2022 1 次提交
    • C
      binder: fix alloc->vma_vm_mm null-ptr dereference · 1da52815
      Carlos Llamas 提交于
      Syzbot reported a couple issues introduced by commit 44e602b4
      ("binder_alloc: add missing mmap_lock calls when using the VMA"), in
      which we attempt to acquire the mmap_lock when alloc->vma_vm_mm has not
      been initialized yet.
      
      This can happen if a binder_proc receives a transaction without having
      previously called mmap() to setup the binder_proc->alloc space in [1].
      Also, a similar issue occurs via binder_alloc_print_pages() when we try
      to dump the debugfs binder stats file in [2].
      
      Sample of syzbot's crash report:
        ==================================================================
        KASAN: null-ptr-deref in range [0x0000000000000128-0x000000000000012f]
        CPU: 0 PID: 3755 Comm: syz-executor229 Not tainted 6.0.0-rc1-next-20220819-syzkaller #0
        syz-executor229[3755] cmdline: ./syz-executor2294415195
        Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/22/2022
        RIP: 0010:__lock_acquire+0xd83/0x56d0 kernel/locking/lockdep.c:4923
        [...]
        Call Trace:
         <TASK>
         lock_acquire kernel/locking/lockdep.c:5666 [inline]
         lock_acquire+0x1ab/0x570 kernel/locking/lockdep.c:5631
         down_read+0x98/0x450 kernel/locking/rwsem.c:1499
         mmap_read_lock include/linux/mmap_lock.h:117 [inline]
         binder_alloc_new_buf_locked drivers/android/binder_alloc.c:405 [inline]
         binder_alloc_new_buf+0xa5/0x19e0 drivers/android/binder_alloc.c:593
         binder_transaction+0x242e/0x9a80 drivers/android/binder.c:3199
         binder_thread_write+0x664/0x3220 drivers/android/binder.c:3986
         binder_ioctl_write_read drivers/android/binder.c:5036 [inline]
         binder_ioctl+0x3470/0x6d00 drivers/android/binder.c:5323
         vfs_ioctl fs/ioctl.c:51 [inline]
         __do_sys_ioctl fs/ioctl.c:870 [inline]
         __se_sys_ioctl fs/ioctl.c:856 [inline]
         __x64_sys_ioctl+0x193/0x200 fs/ioctl.c:856
         do_syscall_x64 arch/x86/entry/common.c:50 [inline]
         do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80
         entry_SYSCALL_64_after_hwframe+0x63/0xcd
         [...]
        ==================================================================
      
      Fix these issues by setting up alloc->vma_vm_mm pointer during open()
      and caching directly from current->mm. This guarantees we have a valid
      reference to take the mmap_lock during scenarios described above.
      
      [1] https://syzkaller.appspot.com/bug?extid=f7dc54e5be28950ac459
      [2] https://syzkaller.appspot.com/bug?extid=a75ebe0452711c9e56d9
      
      Fixes: 44e602b4 ("binder_alloc: add missing mmap_lock calls when using the VMA")
      Cc: <stable@vger.kernel.org> # v5.15+
      Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
      Reported-by: syzbot+f7dc54e5be28950ac459@syzkaller.appspotmail.com
      Reported-by: syzbot+a75ebe0452711c9e56d9@syzkaller.appspotmail.com
      Reviewed-by: NLiam R. Howlett <Liam.Howlett@oracle.com>
      Acked-by: NTodd Kjos <tkjos@google.com>
      Signed-off-by: NCarlos Llamas <cmllamas@google.com>
      Link: https://lore.kernel.org/r/20220829201254.1814484-2-cmllamas@google.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      1da52815
  2. 29 8月, 2022 1 次提交
  3. 19 8月, 2022 1 次提交
  4. 18 8月, 2022 1 次提交
  5. 30 7月, 2022 2 次提交
    • L
      android: binder: fix lockdep check on clearing vma · b0cab80e
      Liam Howlett 提交于
      When munmapping a vma, the mmap_lock can be degraded to a write before
      calling close() on the file handle.  The binder close() function calls
      binder_alloc_set_vma() to clear the vma address, which now has a lock dep
      check for writing on the mmap_lock.  Change the lockdep check to ensure
      the reading lock is held while clearing and keep the write check while
      writing.
      
      Link: https://lkml.kernel.org/r/20220627151857.2316964-1-Liam.Howlett@oracle.com
      Fixes: 472a68df605b ("android: binder: stop saving a pointer to the VMA")
      Signed-off-by: NLiam R. Howlett <Liam.Howlett@oracle.com>
      Reported-by: syzbot+da54fa8d793ca89c741f@syzkaller.appspotmail.com
      Acked-by: NTodd Kjos <tkjos@google.com>
      Cc: "Arve Hjønnevåg" <arve@android.com>
      Cc: Christian Brauner (Microsoft) <brauner@kernel.org>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Hridya Valsaraju <hridya@google.com>
      Cc: Joel Fernandes <joel@joelfernandes.org>
      Cc: Martijn Coenen <maco@android.com>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
      b0cab80e
    • L
      android: binder: stop saving a pointer to the VMA · a43cfc87
      Liam R. Howlett 提交于
      Do not record a pointer to a VMA outside of the mmap_lock for later use. 
      This is unsafe and there are a number of failure paths *after* the
      recorded VMA pointer may be freed during setup.  There is no callback to
      the driver to clear the saved pointer from generic mm code.  Furthermore,
      the VMA pointer may become stale if any number of VMA operations end up
      freeing the VMA so saving it was fragile to being with.
      
      Instead, change the binder_alloc struct to record the start address of the
      VMA and use vma_lookup() to get the vma when needed.  Add lockdep
      mmap_lock checks on updates to the vma pointer to ensure the lock is held
      and depend on that lock for synchronization of readers and writers - which
      was already the case anyways, so the smp_wmb()/smp_rmb() was not
      necessary.
      
      [akpm@linux-foundation.org: fix drivers/android/binder_alloc_selftest.c]
      Link: https://lkml.kernel.org/r/20220621140212.vpkio64idahetbyf@revolver
      Fixes: da1b9564 ("android: binder: fix the race mmap and alloc_new_buf_locked")
      Reported-by: syzbot+58b51ac2b04e388ab7b0@syzkaller.appspotmail.com
      Signed-off-by: NLiam R. Howlett <Liam.Howlett@oracle.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Christian Brauner (Microsoft) <brauner@kernel.org>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Hridya Valsaraju <hridya@google.com>
      Cc: Joel Fernandes <joel@joelfernandes.org>
      Cc: Martijn Coenen <maco@android.com>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Cc: Todd Kjos <tkjos@android.com>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
      a43cfc87
  6. 04 7月, 2022 1 次提交
    • R
      mm: shrinkers: provide shrinkers with names · e33c267a
      Roman Gushchin 提交于
      Currently shrinkers are anonymous objects.  For debugging purposes they
      can be identified by count/scan function names, but it's not always
      useful: e.g.  for superblock's shrinkers it's nice to have at least an
      idea of to which superblock the shrinker belongs.
      
      This commit adds names to shrinkers.  register_shrinker() and
      prealloc_shrinker() functions are extended to take a format and arguments
      to master a name.
      
      In some cases it's not possible to determine a good name at the time when
      a shrinker is allocated.  For such cases shrinker_debugfs_rename() is
      provided.
      
      The expected format is:
          <subsystem>-<shrinker_type>[:<instance>]-<id>
      For some shrinkers an instance can be encoded as (MAJOR:MINOR) pair.
      
      After this change the shrinker debugfs directory looks like:
        $ cd /sys/kernel/debug/shrinker/
        $ ls
          dquota-cache-16     sb-devpts-28     sb-proc-47       sb-tmpfs-42
          mm-shadow-18        sb-devtmpfs-5    sb-proc-48       sb-tmpfs-43
          mm-zspool:zram0-34  sb-hugetlbfs-17  sb-pstore-31     sb-tmpfs-44
          rcu-kfree-0         sb-hugetlbfs-33  sb-rootfs-2      sb-tmpfs-49
          sb-aio-20           sb-iomem-12      sb-securityfs-6  sb-tracefs-13
          sb-anon_inodefs-15  sb-mqueue-21     sb-selinuxfs-22  sb-xfs:vda1-36
          sb-bdev-3           sb-nsfs-4        sb-sockfs-8      sb-zsmalloc-19
          sb-bpf-32           sb-pipefs-14     sb-sysfs-26      thp-deferred_split-10
          sb-btrfs:vda2-24    sb-proc-25       sb-tmpfs-1       thp-zero-9
          sb-cgroup2-30       sb-proc-39       sb-tmpfs-27      xfs-buf:vda1-37
          sb-configfs-23      sb-proc-41       sb-tmpfs-29      xfs-inodegc:vda1-38
          sb-dax-11           sb-proc-45       sb-tmpfs-35
          sb-debugfs-7        sb-proc-46       sb-tmpfs-40
      
      [roman.gushchin@linux.dev: fix build warnings]
        Link: https://lkml.kernel.org/r/Yr+ZTnLb9lJk6fJO@castleReported-by: Nkernel test robot <lkp@intel.com>
      Link: https://lkml.kernel.org/r/20220601032227.4076670-4-roman.gushchin@linux.devSigned-off-by: NRoman Gushchin <roman.gushchin@linux.dev>
      Cc: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
      Cc: Dave Chinner <dchinner@redhat.com>
      Cc: Hillf Danton <hdanton@sina.com>
      Cc: Kent Overstreet <kent.overstreet@gmail.com>
      Cc: Muchun Song <songmuchun@bytedance.com>
      Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
      e33c267a
  7. 26 4月, 2022 3 次提交
  8. 04 2月, 2022 1 次提交
  9. 21 12月, 2021 1 次提交
    • T
      binder: fix async_free_space accounting for empty parcels · cfd0d84b
      Todd Kjos 提交于
      In 4.13, commit 74310e06 ("android: binder: Move buffer out of area shared with user space")
      fixed a kernel structure visibility issue. As part of that patch,
      sizeof(void *) was used as the buffer size for 0-length data payloads so
      the driver could detect abusive clients sending 0-length asynchronous
      transactions to a server by enforcing limits on async_free_size.
      
      Unfortunately, on the "free" side, the accounting of async_free_space
      did not add the sizeof(void *) back. The result was that up to 8-bytes of
      async_free_space were leaked on every async transaction of 8-bytes or
      less.  These small transactions are uncommon, so this accounting issue
      has gone undetected for several years.
      
      The fix is to use "buffer_size" (the allocated buffer size) instead of
      "size" (the logical buffer size) when updating the async_free_space
      during the free operation. These are the same except for this
      corner case of asynchronous transactions with payloads < 8 bytes.
      
      Fixes: 74310e06 ("android: binder: Move buffer out of area shared with user space")
      Signed-off-by: NTodd Kjos <tkjos@google.com>
      Cc: stable@vger.kernel.org # 4.14+
      Link: https://lore.kernel.org/r/20211220190150.2107077-1-tkjos@google.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      cfd0d84b
  10. 10 4月, 2021 1 次提交
  11. 09 12月, 2020 1 次提交
  12. 16 9月, 2020 1 次提交
  13. 04 9月, 2020 2 次提交
  14. 29 7月, 2020 1 次提交
  15. 23 7月, 2020 1 次提交
  16. 10 6月, 2020 2 次提交
  17. 14 11月, 2019 3 次提交
  18. 22 10月, 2019 1 次提交
  19. 17 10月, 2019 1 次提交
    • J
      binder: Don't modify VMA bounds in ->mmap handler · 45d02f79
      Jann Horn 提交于
      binder_mmap() tries to prevent the creation of overly big binder mappings
      by silently truncating the size of the VMA to 4MiB. However, this violates
      the API contract of mmap(). If userspace attempts to create a large binder
      VMA, and later attempts to unmap that VMA, it will call munmap() on a range
      beyond the end of the VMA, which may have been allocated to another VMA in
      the meantime. This can lead to userspace memory corruption.
      
      The following sequence of calls leads to a segfault without this commit:
      
      int main(void) {
        int binder_fd = open("/dev/binder", O_RDWR);
        if (binder_fd == -1) err(1, "open binder");
        void *binder_mapping = mmap(NULL, 0x800000UL, PROT_READ, MAP_SHARED,
                                    binder_fd, 0);
        if (binder_mapping == MAP_FAILED) err(1, "mmap binder");
        void *data_mapping = mmap(NULL, 0x400000UL, PROT_READ|PROT_WRITE,
                                  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
        if (data_mapping == MAP_FAILED) err(1, "mmap data");
        munmap(binder_mapping, 0x800000UL);
        *(char*)data_mapping = 1;
        return 0;
      }
      
      Cc: stable@vger.kernel.org
      Signed-off-by: NJann Horn <jannh@google.com>
      Acked-by: NTodd Kjos <tkjos@google.com>
      Acked-by: NChristian Brauner <christian.brauner@ubuntu.com>
      Link: https://lore.kernel.org/r/20191016150119.154756-1-jannh@google.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      45d02f79
  20. 10 10月, 2019 1 次提交
  21. 01 7月, 2019 1 次提交
  22. 05 6月, 2019 1 次提交
  23. 25 4月, 2019 1 次提交
  24. 21 3月, 2019 1 次提交
  25. 19 2月, 2019 1 次提交
    • M
      binder: reduce mmap_sem write-side lock · 3013bf62
      Minchan Kim 提交于
      binder has used write-side mmap_sem semaphore to release memory
      mapped at address space of the process. However, right lock to
      release pages is down_read, not down_write because page table lock
      already protects the race for parallel freeing.
      
      Please do not use mmap_sem write-side lock which is well known
      contented lock.
      
      Cc: Todd Kjos <tkjos@google.com>
      Cc: Martijn Coenen <maco@android.com>
      Cc: Arve Hjønnevåg <arve@android.com>
      Signed-off-by: NMinchan Kim <minchan@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      3013bf62
  26. 12 2月, 2019 5 次提交
  27. 27 11月, 2018 2 次提交
    • T
      binder: fix sparse warnings on locking context · 324fa64c
      Todd Kjos 提交于
      Add __acquire()/__release() annnotations to fix warnings
      in sparse context checking
      
      There is one case where the warning was due to a lack of
      a "default:" case in a switch statement where a lock was
      being released in each of the cases, so the default
      case was added.
      Signed-off-by: NTodd Kjos <tkjos@google.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      324fa64c
    • T
      binder: fix race that allows malicious free of live buffer · 7bada55a
      Todd Kjos 提交于
      Malicious code can attempt to free buffers using the BC_FREE_BUFFER
      ioctl to binder. There are protections against a user freeing a buffer
      while in use by the kernel, however there was a window where
      BC_FREE_BUFFER could be used to free a recently allocated buffer that
      was not completely initialized. This resulted in a use-after-free
      detected by KASAN with a malicious test program.
      
      This window is closed by setting the buffer's allow_user_free attribute
      to 0 when the buffer is allocated or when the user has previously freed
      it instead of waiting for the caller to set it. The problem was that
      when the struct buffer was recycled, allow_user_free was stale and set
      to 1 allowing a free to go through.
      Signed-off-by: NTodd Kjos <tkjos@google.com>
      Acked-by: NArve Hjønnevåg <arve@android.com>
      Cc: stable <stable@vger.kernel.org> # 4.14
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      7bada55a
  28. 12 9月, 2018 1 次提交
    • M
      android: binder: fix the race mmap and alloc_new_buf_locked · da1b9564
      Minchan Kim 提交于
      There is RaceFuzzer report like below because we have no lock to close
      below the race between binder_mmap and binder_alloc_new_buf_locked.
      To close the race, let's use memory barrier so that if someone see
      alloc->vma is not NULL, alloc->vma_vm_mm should be never NULL.
      
      (I didn't add stable mark intentionallybecause standard android
      userspace libraries that interact with binder (libbinder & libhwbinder)
      prevent the mmap/ioctl race. - from Todd)
      
      "
      Thread interleaving:
      CPU0 (binder_alloc_mmap_handler)              CPU1 (binder_alloc_new_buf_locked)
      =====                                         =====
      // drivers/android/binder_alloc.c
      // #L718 (v4.18-rc3)
      alloc->vma = vma;
                                                    // drivers/android/binder_alloc.c
                                                    // #L346 (v4.18-rc3)
                                                    if (alloc->vma == NULL) {
                                                        ...
                                                        // alloc->vma is not NULL at this point
                                                        return ERR_PTR(-ESRCH);
                                                    }
                                                    ...
                                                    // #L438
                                                    binder_update_page_range(alloc, 0,
                                                            (void *)PAGE_ALIGN((uintptr_t)buffer->data),
                                                            end_page_addr);
      
                                                    // In binder_update_page_range() #L218
                                                    // But still alloc->vma_vm_mm is NULL here
                                                    if (need_mm && mmget_not_zero(alloc->vma_vm_mm))
      alloc->vma_vm_mm = vma->vm_mm;
      
      Crash Log:
      ==================================================================
      BUG: KASAN: null-ptr-deref in __atomic_add_unless include/asm-generic/atomic-instrumented.h:89 [inline]
      BUG: KASAN: null-ptr-deref in atomic_add_unless include/linux/atomic.h:533 [inline]
      BUG: KASAN: null-ptr-deref in mmget_not_zero include/linux/sched/mm.h:75 [inline]
      BUG: KASAN: null-ptr-deref in binder_update_page_range+0xece/0x18e0 drivers/android/binder_alloc.c:218
      Write of size 4 at addr 0000000000000058 by task syz-executor0/11184
      
      CPU: 1 PID: 11184 Comm: syz-executor0 Not tainted 4.18.0-rc3 #1
      Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.8.2-0-g33fbe13 by qemu-project.org 04/01/2014
      Call Trace:
       __dump_stack lib/dump_stack.c:77 [inline]
       dump_stack+0x16e/0x22c lib/dump_stack.c:113
       kasan_report_error mm/kasan/report.c:352 [inline]
       kasan_report+0x163/0x380 mm/kasan/report.c:412
       check_memory_region_inline mm/kasan/kasan.c:260 [inline]
       check_memory_region+0x140/0x1a0 mm/kasan/kasan.c:267
       kasan_check_write+0x14/0x20 mm/kasan/kasan.c:278
       __atomic_add_unless include/asm-generic/atomic-instrumented.h:89 [inline]
       atomic_add_unless include/linux/atomic.h:533 [inline]
       mmget_not_zero include/linux/sched/mm.h:75 [inline]
       binder_update_page_range+0xece/0x18e0 drivers/android/binder_alloc.c:218
       binder_alloc_new_buf_locked drivers/android/binder_alloc.c:443 [inline]
       binder_alloc_new_buf+0x467/0xc30 drivers/android/binder_alloc.c:513
       binder_transaction+0x125b/0x4fb0 drivers/android/binder.c:2957
       binder_thread_write+0xc08/0x2770 drivers/android/binder.c:3528
       binder_ioctl_write_read.isra.39+0x24f/0x8e0 drivers/android/binder.c:4456
       binder_ioctl+0xa86/0xf34 drivers/android/binder.c:4596
       vfs_ioctl fs/ioctl.c:46 [inline]
       do_vfs_ioctl+0x154/0xd40 fs/ioctl.c:686
       ksys_ioctl+0x94/0xb0 fs/ioctl.c:701
       __do_sys_ioctl fs/ioctl.c:708 [inline]
       __se_sys_ioctl fs/ioctl.c:706 [inline]
       __x64_sys_ioctl+0x43/0x50 fs/ioctl.c:706
       do_syscall_64+0x167/0x4b0 arch/x86/entry/common.c:290
       entry_SYSCALL_64_after_hwframe+0x49/0xbe
      "
      Signed-off-by: NTodd Kjos <tkjos@google.com>
      Signed-off-by: NMinchan Kim <minchan@kernel.org>
      Reviewed-by: NMartijn Coenen <maco@android.com>
      Cc: stable <stable@vger.kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      da1b9564