Skip to content
Snippets Groups Projects
  1. Oct 29, 2020
    • Todd Kjos's avatar
      binder: fix UAF when releasing todo list · d4c49b67
      Todd Kjos authored
      
      commit f3277cbf upstream.
      
      When releasing a thread todo list when tearing down
      a binder_proc, the following race was possible which
      could result in a use-after-free:
      
      1.  Thread 1: enter binder_release_work from binder_thread_release
      2.  Thread 2: binder_update_ref_for_handle() -> binder_dec_node_ilocked()
      3.  Thread 2: dec nodeA --> 0 (will free node)
      4.  Thread 1: ACQ inner_proc_lock
      5.  Thread 2: block on inner_proc_lock
      6.  Thread 1: dequeue work (BINDER_WORK_NODE, part of nodeA)
      7.  Thread 1: REL inner_proc_lock
      8.  Thread 2: ACQ inner_proc_lock
      9.  Thread 2: todo list cleanup, but work was already dequeued
      10. Thread 2: free node
      11. Thread 2: REL inner_proc_lock
      12. Thread 1: deref w->type (UAF)
      
      The problem was that for a BINDER_WORK_NODE, the binder_work element
      must not be accessed after releasing the inner_proc_lock while
      processing the todo list elements since another thread might be
      handling a deref on the node containing the binder_work element
      leading to the node being freed.
      
      Signed-off-by: default avatarTodd Kjos <tkjos@google.com>
      Link: https://lore.kernel.org/r/20201009232455.4054810-1-tkjos@google.com
      
      
      Cc: <stable@vger.kernel.org> # 4.14, 4.19, 5.4, 5.8
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      d4c49b67
  2. Aug 21, 2020
    • Jann Horn's avatar
      binder: Prevent context manager from incrementing ref 0 · f40f289b
      Jann Horn authored
      commit 4b836a14 upstream.
      
      Binder is designed such that a binder_proc never has references to
      itself. If this rule is violated, memory corruption can occur when a
      process sends a transaction to itself; see e.g.
      <https://syzkaller.appspot.com/bug?extid=09e05aba06723a94d43d
      
      >.
      
      There is a remaining edgecase through which such a transaction-to-self
      can still occur from the context of a task with BINDER_SET_CONTEXT_MGR
      access:
      
       - task A opens /dev/binder twice, creating binder_proc instances P1
         and P2
       - P1 becomes context manager
       - P2 calls ACQUIRE on the magic handle 0, allocating index 0 in its
         handle table
       - P1 dies (by closing the /dev/binder fd and waiting a bit)
       - P2 becomes context manager
       - P2 calls ACQUIRE on the magic handle 0, allocating index 1 in its
         handle table
         [this triggers a warning: "binder: 1974:1974 tried to acquire
         reference to desc 0, got 1 instead"]
       - task B opens /dev/binder once, creating binder_proc instance P3
       - P3 calls P2 (via magic handle 0) with (void*)1 as argument (two-way
         transaction)
       - P2 receives the handle and uses it to call P3 (two-way transaction)
       - P3 calls P2 (via magic handle 0) (two-way transaction)
       - P2 calls P2 (via handle 1) (two-way transaction)
      
      And then, if P2 does *NOT* accept the incoming transaction work, but
      instead closes the binder fd, we get a crash.
      
      Solve it by preventing the context manager from using ACQUIRE on ref 0.
      There shouldn't be any legitimate reason for the context manager to do
      that.
      
      Additionally, print a warning if someone manages to find another way to
      trigger a transaction-to-self bug in the future.
      
      Cc: stable@vger.kernel.org
      Fixes: 457b9a6f ("Staging: android: add binder driver")
      Acked-by: default avatarTodd Kjos <tkjos@google.com>
      Signed-off-by: default avatarJann Horn <jannh@google.com>
      Reviewed-by: default avatarMartijn Coenen <maco@android.com>
      Link: https://lore.kernel.org/r/20200727120424.1627555-1-jannh@google.com
      
      
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      f40f289b
  3. Aug 04, 2019
  4. Jul 31, 2019
  5. Jul 21, 2019
  6. Dec 05, 2018
    • Todd Kjos's avatar
      binder: fix race that allows malicious free of live buffer · fd6cc33d
      Todd Kjos authored
      
      commit 7bada55a upstream.
      
      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: default avatarTodd Kjos <tkjos@google.com>
      Acked-by: default avatarArve Hjønnevåg <arve@android.com>
      Cc: stable <stable@vger.kernel.org> # 4.14
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      
      fd6cc33d
  7. May 01, 2018
  8. Feb 25, 2018
    • Todd Kjos's avatar
      binder: replace "%p" with "%pK" · b46af094
      Todd Kjos authored
      
      commit 8ca86f16 upstream.
      
      The format specifier "%p" can leak kernel addresses. Use
      "%pK" instead. There were 4 remaining cases in binder.c.
      
      Signed-off-by: default avatarTodd Kjos <tkjos@google.com>
      Cc: stable <stable@vger.kernel.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      b46af094
    • Eric Biggers's avatar
      binder: check for binder_thread allocation failure in binder_poll() · 047ba51a
      Eric Biggers authored
      
      commit f8898267 upstream.
      
      If the kzalloc() in binder_get_thread() fails, binder_poll()
      dereferences the resulting NULL pointer.
      
      Fix it by returning POLLERR if the memory allocation failed.
      
      This bug was found by syzkaller using fault injection.
      
      Reported-by: default avatarsyzbot <syzkaller@googlegroups.com>
      Fixes: 457b9a6f ("Staging: android: add binder driver")
      Cc: stable@vger.kernel.org
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      047ba51a
    • Martijn Coenen's avatar
      ANDROID: binder: synchronize_rcu() when using POLLFREE. · 441b5d10
      Martijn Coenen authored
      
      commit 5eeb2ca0 upstream.
      
      To prevent races with ep_remove_waitqueue() removing the
      waitqueue at the same time.
      
      Reported-by: default avatar <syzbot+a2a3c4909716e271487e@syzkaller.appspotmail.com>
      Signed-off-by: default avatarMartijn Coenen <maco@android.com>
      Cc: stable <stable@vger.kernel.org> # 4.14+
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      441b5d10
    • Todd Kjos's avatar
      ANDROID: binder: remove WARN() for redundant txn error · 129926c3
      Todd Kjos authored
      
      commit e46a3b3b upstream.
      
      binder_send_failed_reply() is called when a synchronous
      transaction fails. It reports an error to the thread that
      is waiting for the completion. Given that the transaction
      is synchronous, there should never be more than 1 error
      response to that thread -- this was being asserted with
      a WARN().
      
      However, when exercising the driver with syzbot tests, cases
      were observed where multiple "synchronous" requests were
      sent without waiting for responses, so it is possible that
      multiple errors would be reported to the thread. This testing
      was conducted with panic_on_warn set which forced the crash.
      
      This is easily reproduced by sending back-to-back
      "synchronous" transactions without checking for any
      response (eg, set read_size to 0):
      
          bwr.write_buffer = (uintptr_t)&bc1;
          bwr.write_size = sizeof(bc1);
          bwr.read_buffer = (uintptr_t)&br;
          bwr.read_size = 0;
          ioctl(fd, BINDER_WRITE_READ, &bwr);
          sleep(1);
          bwr2.write_buffer = (uintptr_t)&bc2;
          bwr2.write_size = sizeof(bc2);
          bwr2.read_buffer = (uintptr_t)&br;
          bwr2.read_size = 0;
          ioctl(fd, BINDER_WRITE_READ, &bwr2);
          sleep(1);
      
      The first transaction is sent to the servicemanager and the reply
      fails because no VMA is set up by this client. After
      binder_send_failed_reply() is called, the BINDER_WORK_RETURN_ERROR
      is sitting on the thread's todo list since the read_size was 0 and
      the client is not waiting for a response.
      
      The 2nd transaction is sent and the BINDER_WORK_RETURN_ERROR has not
      been consumed, so the thread's reply_error.cmd is still set (normally
      cleared when the BINDER_WORK_RETURN_ERROR is handled). Therefore
      when the servicemanager attempts to reply to the 2nd failed
      transaction, the error is already set and it triggers this warning.
      
      This is a user error since it is not waiting for the synchronous
      transaction to complete. If it ever does check, it will see an
      error.
      
      Changed the WARN() to a pr_warn().
      
      Signed-off-by: default avatarTodd Kjos <tkjos@android.com>
      Reported-by: default avatarsyzbot <syzkaller@googlegroups.com>
      Cc: stable <stable@vger.kernel.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      129926c3
  9. Feb 03, 2018
  10. Jan 02, 2018
    • Todd Kjos's avatar
      binder: fix proc->files use-after-free · d87f1bc7
      Todd Kjos authored
      
      commit 7f3dc008 upstream.
      
      proc->files cleanup is initiated by binder_vma_close. Therefore
      a reference on the binder_proc is not enough to prevent the
      files_struct from being released while the binder_proc still has
      a reference. This can lead to an attempt to dereference the
      stale pointer obtained from proc->files prior to proc->files
      cleanup. This has been seen once in task_get_unused_fd_flags()
      when __alloc_fd() is called with a stale "files".
      
      The fix is to protect proc->files with a mutex to prevent cleanup
      while in use.
      
      Signed-off-by: default avatarTodd Kjos <tkjos@google.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      d87f1bc7
  11. Dec 10, 2017
  12. Oct 20, 2017
  13. Oct 04, 2017
    • Todd Kjos's avatar
      binder: fix use-after-free in binder_transaction() · 512cf465
      Todd Kjos authored
      
      User-space normally keeps the node alive when creating a transaction
      since it has a reference to the target. The local strong ref keeps it
      alive if the sending process dies before the target process processes
      the transaction. If the source process is malicious or has a reference
      counting bug, this can fail.
      
      In this case, when we attempt to decrement the node in the failure
      path, the node has already been freed.
      
      This is fixed by taking a tmpref on the node while constructing
      the transaction. To avoid re-acquiring the node lock and inner
      proc lock to increment the proc's tmpref, a helper is used that
      does the ref increments on both the node and proc.
      
      Signed-off-by: default avatarTodd Kjos <tkjos@google.com>
      Cc: stable <stable@vger.kernel.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      512cf465
  14. Sep 18, 2017
    • Xu YiPing's avatar
      binder: fix memory corruption in binder_transaction binder · d53bebdf
      Xu YiPing authored
      
      commit 7a4408c6 ("binder: make sure accesses to proc/thread are
      safe") made a change to enqueue tcomplete to thread->todo before
      enqueuing the transaction. However, in err_dead_proc_or_thread case,
      the tcomplete is directly freed, without dequeued. It may cause the
      thread->todo list to be corrupted.
      
      So, dequeue it before freeing.
      
      Fixes: 7a4408c6 ("binder: make sure accesses to proc/thread are safe")
      Signed-off-by: default avatarXu YiPing <xuyiping@hisilicon.com>
      Signed-off-by: default avatarTodd Kjos <tkjos@google.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      d53bebdf
    • Xu YiPing's avatar
      binder: fix an ret value override · 52b81611
      Xu YiPing authored
      
      commit 372e3147 ("binder: guarantee txn complete / errors delivered
      in-order") incorrectly defined a local ret value.  This ret value will
      be invalid when out of the if block
      
      Fixes: 372e3147 ("binder: refactor binder ref inc/dec for thread safety")
      Signed-off-by: default avatarXu YiPing <xuyiping@hislicon.com>
      Signed-off-by: default avatarTodd Kjos <tkjos@google.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      52b81611
    • Arnd Bergmann's avatar
      android: binder: fix type mismatch warning · 1c363eae
      Arnd Bergmann authored
      
      Allowing binder to expose the 64-bit API on 32-bit kernels caused a
      build warning:
      
      drivers/android/binder.c: In function 'binder_transaction_buffer_release':
      drivers/android/binder.c:2220:15: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
          fd_array = (u32 *)(parent_buffer + fda->parent_offset);
                     ^
      drivers/android/binder.c: In function 'binder_translate_fd_array':
      drivers/android/binder.c:2445:13: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
        fd_array = (u32 *)(parent_buffer + fda->parent_offset);
                   ^
      drivers/android/binder.c: In function 'binder_fixup_parent':
      drivers/android/binder.c:2511:18: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
      
      This adds extra type casts to avoid the warning.
      
      However, there is another problem with the Kconfig option: turning
      it on or off creates two incompatible ABI versions, a kernel that
      has this enabled cannot run user space that was built without it
      or vice versa. A better solution might be to leave the option hidden
      until the binder code is fixed to deal with both ABI versions.
      
      Fixes: e8d2ed7d ("Revert "staging: Fix build issues with new binder API"")
      Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      1c363eae
  15. Sep 01, 2017
  16. Aug 28, 2017
    • Sherry Yang's avatar
      android: binder: Add global lru shrinker to binder · f2517eb7
      Sherry Yang authored
      
      Hold on to the pages allocated and mapped for transaction
      buffers until the system is under memory pressure. When
      that happens, use linux shrinker to free pages. Without
      using shrinker, patch "android: binder: Move buffer out
      of area shared with user space" will cause a significant
      slow down for small transactions that fit into the first
      page because free list buffer header used to be inlined
      with buffer data.
      
      In addition to prevent the performance regression for
      small transactions, this patch improves the performance
      for transactions that take up more than one page.
      
      Modify alloc selftest to work with the shrinker change.
      
      Test: Run memory intensive applications (Chrome and Camera)
      to trigger shrinker callbacks. Binder frees memory as expected.
      Test: Run binderThroughputTest with high memory pressure
      option enabled.
      
      Signed-off-by: default avatarSherry Yang <sherryy@android.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      f2517eb7
    • Sherry Yang's avatar
      android: binder: Add allocator selftest · 4175e2b4
      Sherry Yang authored
      
      binder_alloc_selftest tests that alloc_new_buf handles page allocation and
      deallocation properly when allocate and free buffers. The test allocates 5
      buffers of various sizes to cover all possible page alignment cases, and
      frees the buffers using a list of exhaustive freeing order.
      
      Test: boot the device with ANDROID_BINDER_IPC_SELFTEST config option
      enabled. Allocator selftest passes.
      
      Signed-off-by: default avatarSherry Yang <sherryy@android.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      4175e2b4
  17. Aug 23, 2017
  18. Jul 17, 2017
Loading