New issue
Advanced search Search tips
Starred by 1 user
Status: WontFix
Owner:
Closed: Dec 2016
Cc:



Sign in to add a comment
Android: Code loading bypasses in system_server
Project Member Reported by laginimaineb@google.com, Sep 27 2016 Back to list
As of Android Nougat,  a new set of SELinux rules have been added which are designed to prevent system_server from loading arbitrary code into its address-space. This has been enforced by adding the following rules to system_server's SELinux policy:

neverallow system_server self:process execmem;
neverallow system_server ashmem_device:chr_file execute;
neverallow system_server system_server_tmpfs:file execute;

However, as system_server is extremely privileged, there are a few vectors through which it may still load arbitrary code, thus bypassing the mitigation mentioned above.

1. The system user has read-write access to /data/app/*/oat/*. However, this directory and all files created within it have the SELinux context:

u:object_r:dalvikcache_data_file:s0

(see http://androidxref.com/7.0.0_r1/xref/system/sepolicy/file_contexts#252)

Since system_server is required to execute dalvik-cache files, its SELinux policy explicitly allows this by adding the rule:

allow system_server dalvikcache_data_file:file execute;

This means that system_server may create a file under the aforementioned path and mmap it with PROT_EXEC in order to load arbitrary code.

2. Much in the same way, system_server has read-write access to /data/app, including /data/app/vmdl*.tmp/*/oat/*. However, this directory and all files created within it have the SELinux context:

u:object_r:dalvikcache_data_file:s0

(see http://androidxref.com/7.0.0_r1/xref/system/sepolicy/file_contexts#254)

This allows the attacker to follow the same steps as in (1.) in order to map in arbitrary code.


Note that removing RW access to the oat directories is not always sufficient to fix this issue. For example, an attacker can also choose to store his code within the his application (i.e., in a large byte[] within the source code). This in turn will be compiled into the ODEX file under the "oat" directory, and can then be used to map in arbitrary code once more into system_server.

This bug is subject to a 90 day disclosure deadline. If 90 days elapse without a broadly available patch, then the bug report will automatically become visible to the public.
 
Project Member Comment 1 by laginimaineb@google.com, Sep 30 2016
Just wanted to add a small update to this issue - Nick pointed out an SELinux rule I missed which prevents direct "write" access to files with the "dalvikcache_data_file" context:

# Only authorized processes should be writing to files in /data/dalvik-cache
neverallow {
  domain
  -init # TODO: limit init to relabelfrom for files
  -zygote
  -installd
  -postinstall_dexopt
  -cppreopts
  -dex2oat
  -otapreopt_slot
} dalvikcache_data_file:file no_w_file_perms;

However, an attacker can still create files with the "dalvikcache_data_file" context from system_server, with the aid of the "installd" daemon. This can be achieved by following two short steps:

 1. Write an APK file to /data/app
 2. Write the "dexopt" command to installd's socket, resulting in the creation of an OAT file with the context "dalvikcache_data_file"
    (see http://androidxref.com/7.0.0_r1/xref/frameworks/native/cmds/installd/commands.cpp#1391)

(Since this is so little logic, it can probably be written comfortably in a ROP chain).
Project Member Comment 2 by laginimaineb@google.com, Dec 2 2016
Labels: -Restrict-View-Commit
Status: WontFix
Android responded on November 1st: 

"In Android N, we started rolling out a security feature with the eventual goal of preventing sensitive system components such as system_server from executing code that isn’t on the verified-boot protected system image. Because of Android’s use of updateable system apps and saving memory by JIT-ing rarely used code, fulling disallowing code loading requires significantly more work and is considered a long-term goal. In spite of that, there are immediate security benefits of a partial rollout. In N we removed the execmem permission from system_server. If an attacker gains control of execution in system_server, they can no longer drop shell-code in memory. They must now go through a more complex path to get arbitrary code running.

We consider this issue to be a combination of discovering the limits of a mitigation introduced in N and a feature request for additional mitigations to be implemented. We appreciate the research and will use this information to improve Android security in the future. However, since this is not a vulnerability or a bug in the current implementation, it can be publicly disclosed at any time and no embargo is required before disclosure."
Sign in to add a comment