Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 1 | # A service that sets up the chroot environment for Pre-reboot Dexopt. |
Jiakai Zhang | 817c49f | 2023-10-18 17:03:20 +0100 | [diff] [blame] | 2 | type dexopt_chroot_setup, domain, coredomain; |
| 3 | type dexopt_chroot_setup_exec, system_file_type, exec_type, file_type; |
| 4 | type dexopt_chroot_setup_tmpfs, file_type; |
| 5 | |
| 6 | # Allow dexopt_chroot_setup to publish a binder service and make binder calls. |
| 7 | binder_use(dexopt_chroot_setup) |
| 8 | add_service(dexopt_chroot_setup, dexopt_chroot_setup_service) |
| 9 | allow dexopt_chroot_setup dumpstate:fifo_file { getattr write }; |
| 10 | allow dexopt_chroot_setup dumpstate:fd use; |
| 11 | |
| 12 | init_daemon_domain(dexopt_chroot_setup) |
| 13 | |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 14 | # Use tmpfs_domain() which will give tmpfs files created by dexopt_chroot_setup |
| 15 | # their own label, which differs from other labels created by other processes. |
| 16 | # This allows to distinguish in policy files created by dexopt_chroot_setup vs |
| 17 | # other processes. |
Jiakai Zhang | 817c49f | 2023-10-18 17:03:20 +0100 | [diff] [blame] | 18 | tmpfs_domain(dexopt_chroot_setup) |
| 19 | |
| 20 | # libart (mark_compact.cc) has some intialization code that touches the cache |
| 21 | # info file and userfaultfd. |
| 22 | allow dexopt_chroot_setup apex_module_data_file:dir { getattr search }; |
| 23 | r_dir_file(dexopt_chroot_setup, apex_art_data_file) |
| 24 | userfaultfd_use(dexopt_chroot_setup) |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 25 | |
| 26 | # Allow getting root capabilities to bypass permission checks. |
| 27 | # - "sys_admin" is for performing mount and umount. |
| 28 | # - "sys_chroot" is for performing chroot. |
| 29 | allow dexopt_chroot_setup self:global_capability_class_set { sys_admin sys_chroot }; |
| 30 | |
| 31 | # Allow managing its own files. |
| 32 | # The root of the temp dir that dexopt_chroot_setup uses is labeled |
| 33 | # pre_reboot_dexopt_file. |
| 34 | allow dexopt_chroot_setup pre_reboot_dexopt_file:dir create_dir_perms; |
| 35 | allow dexopt_chroot_setup pre_reboot_dexopt_file:file create_file_perms; |
| 36 | |
| 37 | # Allow accessing /proc/filesystems. |
| 38 | allow dexopt_chroot_setup proc_filesystems:file r_file_perms; |
| 39 | |
| 40 | # Allow accessing block devices (/dev/block/...). |
| 41 | allow dexopt_chroot_setup block_device:dir { getattr search }; |
| 42 | |
| 43 | # Allow mounting file systems, to create a chroot environment. |
Jiakai Zhang | 413f44d | 2024-06-04 12:54:25 +0000 | [diff] [blame] | 44 | # We recursively bind-mount directories under /data, /mnt/expand, /proc, /sys, |
| 45 | # and /dev. We need some of them (e.g., incremental-fs directories for |
| 46 | # incremental apps in /data; /dev/cpuctl and /dev/blkio for task profiles), but |
| 47 | # not necessarily all of them. However, to avoid random crashes and silent |
| 48 | # fallbacks, we bind-mount all of them. Therefore, we need access to many of the |
| 49 | # fstypes. |
| 50 | |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 51 | allow dexopt_chroot_setup { |
| 52 | apex_mnt_dir |
Jiakai Zhang | 0a49ac3 | 2024-06-03 20:12:40 +0100 | [diff] [blame] | 53 | apk_data_file |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 54 | binderfs |
Jiakai Zhang | 672accd | 2024-06-26 14:39:00 +0000 | [diff] [blame] | 55 | binfmt_miscfs |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 56 | cgroup |
| 57 | cgroup_v2 |
Jiakai Zhang | 413f44d | 2024-06-04 12:54:25 +0000 | [diff] [blame] | 58 | userdebug_or_eng(debugfs) |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 59 | debugfs_tracing_debug |
| 60 | device |
| 61 | devpts |
| 62 | fs_bpf |
Jiakai Zhang | be2e719 | 2024-05-09 04:03:18 +0000 | [diff] [blame] | 63 | functionfs |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 64 | fusectlfs |
| 65 | linkerconfig_file |
| 66 | metadata_file |
| 67 | mnt_expand_file |
| 68 | pre_reboot_dexopt_file |
| 69 | proc |
| 70 | pstorefs |
| 71 | rootfs |
| 72 | selinuxfs |
| 73 | sysfs |
| 74 | system_data_file |
| 75 | system_data_root_file |
| 76 | system_file |
| 77 | tmpfs |
| 78 | vendor_file |
| 79 | }:dir mounton; |
| 80 | |
| 81 | allow dexopt_chroot_setup { tmpfs labeledfs }:filesystem mount; |
| 82 | |
| 83 | allow dexopt_chroot_setup { |
| 84 | binderfs |
Jiakai Zhang | 672accd | 2024-06-26 14:39:00 +0000 | [diff] [blame] | 85 | binfmt_miscfs |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 86 | cgroup |
| 87 | cgroup_v2 |
Jiakai Zhang | 413f44d | 2024-06-04 12:54:25 +0000 | [diff] [blame] | 88 | userdebug_or_eng(debugfs) |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 89 | debugfs_tracing_debug |
| 90 | devpts |
| 91 | fs_bpf |
Jiakai Zhang | be2e719 | 2024-05-09 04:03:18 +0000 | [diff] [blame] | 92 | functionfs |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 93 | fusectlfs |
| 94 | labeledfs |
| 95 | proc |
| 96 | pstorefs |
| 97 | selinuxfs |
| 98 | sysfs |
| 99 | tmpfs |
| 100 | }:filesystem unmount; |
| 101 | |
| 102 | # Allow reading /apex in chroot. |
| 103 | r_dir_file(dexopt_chroot_setup, apex_mnt_dir) |
| 104 | allow dexopt_chroot_setup apex_info_file:file r_file_perms; |
| 105 | |
| 106 | # Allow writing an empty linker config in chroot to suppress linker warnings. |
| 107 | # The empty linker config is used until linkerconfig has run. |
| 108 | # In chroot, we're reusing the type outside the chroot, to reuse all the rules |
| 109 | # for it for other domains, even though we're not changing the real linker |
| 110 | # config outside the chroot. |
| 111 | allow dexopt_chroot_setup linkerconfig_file:dir { write add_name }; |
| 112 | allow dexopt_chroot_setup linkerconfig_file:file { create write }; |
| 113 | |
| 114 | # Allow using the `rootcontext=` option when mounting tmpfs, so we can give the |
| 115 | # right labels to /apex, /linkerconfig, /mnt/artd_tmp in chroot. |
| 116 | # Combined with `allow file_type tmpfs:filesystem associate;`, this allows |
| 117 | # giving any labels to any tmpfs filesystems as soon as they are mounted. |
| 118 | # Note that those tmpfs filesystems are known to be empty at the time where the |
| 119 | # labels are given, and this rule doesn't allow relabeling any existing tmpfs. |
| 120 | allow dexopt_chroot_setup tmpfs:filesystem relabelfrom; |
| 121 | |
| 122 | # Allow executing art_exec_exec without a domain transition because it is a thin |
| 123 | # wrapper that executes other binaries on behalf of dexopt_chroot_setup. Domain |
| 124 | # transition will take place as soon as art_exec_exec executes other binaries. |
| 125 | allow dexopt_chroot_setup art_exec_exec:file rx_file_perms; |
| 126 | |
| 127 | # Allow running other binaries in their own domains. |
| 128 | domain_auto_trans(dexopt_chroot_setup, apexd_exec, apexd) |
| 129 | domain_auto_trans(dexopt_chroot_setup, linkerconfig_exec, linkerconfig) |
| 130 | |
| 131 | # Allow running snapshotctl through init, to map and unmap block devices. |
| 132 | set_prop(dexopt_chroot_setup, snapshotctl_prop) |
| 133 | |
Jiakai Zhang | 0a49ac3 | 2024-06-03 20:12:40 +0100 | [diff] [blame] | 134 | # Allow accessing /data/app/..., to bind-mount dirs for incremental apps. |
| 135 | allow dexopt_chroot_setup apk_data_file:dir { getattr search }; |
| 136 | |
Jiakai Zhang | 4acd073 | 2024-03-20 11:24:54 +0000 | [diff] [blame] | 137 | # Neverallow rules. |
| 138 | |
| 139 | # Never allow running other binaries without a domain transition. |
| 140 | # The exception for art_exec_exec is explained above. |
| 141 | neverallow dexopt_chroot_setup ~{art_exec_exec}:file execute_no_trans; |
| 142 | |
| 143 | # Given how powerful this domain is, it shouldn't be used for other purposes. |
| 144 | neverallow { domain -init } dexopt_chroot_setup:process transition; |
| 145 | neverallow * dexopt_chroot_setup:process dyntransition; |
| 146 | |
| 147 | # Never allow other processes to access the temp dirs for Pre-reboot Dexopt. |
| 148 | neverallow { |
| 149 | domain |
| 150 | -art_exec |
| 151 | -artd |
| 152 | -dexopt_chroot_setup |
| 153 | -init |
| 154 | -system_server |
| 155 | -vendor_init |
| 156 | } pre_reboot_dexopt_file:dir *; |