1*e4a36f41SAndroid Build Coastguard Worker# gsid - Manager for GSI Installation 2*e4a36f41SAndroid Build Coastguard Worker 3*e4a36f41SAndroid Build Coastguard Workertype gsid, domain; 4*e4a36f41SAndroid Build Coastguard Workertype gsid_exec, exec_type, file_type, system_file_type; 5*e4a36f41SAndroid Build Coastguard Workertypeattribute gsid coredomain; 6*e4a36f41SAndroid Build Coastguard Worker 7*e4a36f41SAndroid Build Coastguard Workerinit_daemon_domain(gsid) 8*e4a36f41SAndroid Build Coastguard Worker 9*e4a36f41SAndroid Build Coastguard Workerbinder_use(gsid) 10*e4a36f41SAndroid Build Coastguard Workerbinder_service(gsid) 11*e4a36f41SAndroid Build Coastguard Workeradd_service(gsid, gsi_service) 12*e4a36f41SAndroid Build Coastguard Worker 13*e4a36f41SAndroid Build Coastguard Worker# Manage DSU metadata encryption key through vold. 14*e4a36f41SAndroid Build Coastguard Workerallow gsid vold_service:service_manager find; 15*e4a36f41SAndroid Build Coastguard Workerbinder_call(gsid, vold) 16*e4a36f41SAndroid Build Coastguard Worker 17*e4a36f41SAndroid Build Coastguard Workerset_prop(gsid, gsid_prop) 18*e4a36f41SAndroid Build Coastguard Worker 19*e4a36f41SAndroid Build Coastguard Worker# Needed to create/delete device-mapper nodes, and read/write to them. 20*e4a36f41SAndroid Build Coastguard Workerallow gsid dm_device:chr_file rw_file_perms; 21*e4a36f41SAndroid Build Coastguard Workerallow gsid dm_device:blk_file rw_file_perms; 22*e4a36f41SAndroid Build Coastguard Workerallow gsid self:global_capability_class_set sys_admin; 23*e4a36f41SAndroid Build Coastguard Workerdontaudit gsid self:global_capability_class_set dac_override; 24*e4a36f41SAndroid Build Coastguard Worker 25*e4a36f41SAndroid Build Coastguard Worker# On FBE devices (not using dm-default-key), gsid will use loop devices to map 26*e4a36f41SAndroid Build Coastguard Worker# images rather than device-mapper. 27*e4a36f41SAndroid Build Coastguard Workerallow gsid loop_control_device:chr_file rw_file_perms; 28*e4a36f41SAndroid Build Coastguard Workerallow gsid loop_device:blk_file rw_file_perms; 29*e4a36f41SAndroid Build Coastguard Workerallowxperm gsid loop_device:blk_file ioctl { 30*e4a36f41SAndroid Build Coastguard Worker LOOP_GET_STATUS64 31*e4a36f41SAndroid Build Coastguard Worker LOOP_SET_STATUS64 32*e4a36f41SAndroid Build Coastguard Worker LOOP_SET_FD 33*e4a36f41SAndroid Build Coastguard Worker LOOP_SET_BLOCK_SIZE 34*e4a36f41SAndroid Build Coastguard Worker LOOP_SET_DIRECT_IO 35*e4a36f41SAndroid Build Coastguard Worker LOOP_CLR_FD 36*e4a36f41SAndroid Build Coastguard Worker BLKFLSBUF 37*e4a36f41SAndroid Build Coastguard Worker}; 38*e4a36f41SAndroid Build Coastguard Worker 39*e4a36f41SAndroid Build Coastguard Worker# libfiemap_writer uses sysfs to derive the bottom of a device-mapper stacking. 40*e4a36f41SAndroid Build Coastguard Worker# This requires traversing /sys/block/dm-N/slaves/* and reading the list of 41*e4a36f41SAndroid Build Coastguard Worker# file names. 42*e4a36f41SAndroid Build Coastguard Workerr_dir_file(gsid, sysfs_dm) 43*e4a36f41SAndroid Build Coastguard Worker 44*e4a36f41SAndroid Build Coastguard Worker# libfiemap_writer needs to read /sys/fs/f2fs/<dev>/features to determine 45*e4a36f41SAndroid Build Coastguard Worker# whether pin_file support is enabled. 46*e4a36f41SAndroid Build Coastguard Workerr_dir_file(gsid, sysfs_fs_f2fs) 47*e4a36f41SAndroid Build Coastguard Worker 48*e4a36f41SAndroid Build Coastguard Worker# Needed to read fstab, which is used to validate that system verity does not 49*e4a36f41SAndroid Build Coastguard Worker# use check_once_at_most for sdcard installs. (Note: proc_cmdline is needed 50*e4a36f41SAndroid Build Coastguard Worker# to get the A/B slot suffix). 51*e4a36f41SAndroid Build Coastguard Workerread_fstab(gsid) 52*e4a36f41SAndroid Build Coastguard Workerallow gsid sysfs_dt_firmware_android:dir r_dir_perms; 53*e4a36f41SAndroid Build Coastguard Workerallow gsid sysfs_dt_firmware_android:file r_file_perms; 54*e4a36f41SAndroid Build Coastguard Worker 55*e4a36f41SAndroid Build Coastguard Worker# Needed to stat /data/gsi/* and realpath on /dev/block/by-name/* 56*e4a36f41SAndroid Build Coastguard Workerallow gsid block_device:dir r_dir_perms; 57*e4a36f41SAndroid Build Coastguard Worker 58*e4a36f41SAndroid Build Coastguard Worker# Allow querying the size of super_block_device_type. 59*e4a36f41SAndroid Build Coastguard Workerallow gsid super_block_device_type:blk_file r_file_perms; 60*e4a36f41SAndroid Build Coastguard Worker 61*e4a36f41SAndroid Build Coastguard Worker# liblp queries these block alignment properties. 62*e4a36f41SAndroid Build Coastguard Workerallowxperm gsid { 63*e4a36f41SAndroid Build Coastguard Worker userdata_block_device 64*e4a36f41SAndroid Build Coastguard Worker sdcard_block_device 65*e4a36f41SAndroid Build Coastguard Worker super_block_device_type 66*e4a36f41SAndroid Build Coastguard Worker}:blk_file ioctl { 67*e4a36f41SAndroid Build Coastguard Worker BLKIOMIN 68*e4a36f41SAndroid Build Coastguard Worker BLKALIGNOFF 69*e4a36f41SAndroid Build Coastguard Worker}; 70*e4a36f41SAndroid Build Coastguard Worker 71*e4a36f41SAndroid Build Coastguard Worker# When installing images to an sdcard, gsid needs to be able to stat() the 72*e4a36f41SAndroid Build Coastguard Worker# block device. gsid also calls realpath() to remove symlinks. 73*e4a36f41SAndroid Build Coastguard Workerallow gsid mnt_media_rw_file:dir r_dir_perms; 74*e4a36f41SAndroid Build Coastguard Workerallow gsid mnt_media_rw_stub_file:dir r_dir_perms; 75*e4a36f41SAndroid Build Coastguard Worker 76*e4a36f41SAndroid Build Coastguard Worker# When installing images to an sdcard, gsid must bypass sdcardfs and install 77*e4a36f41SAndroid Build Coastguard Worker# directly to vfat, which supports the FIBMAP ioctl. 78*e4a36f41SAndroid Build Coastguard Workerallow gsid vfat:dir create_dir_perms; 79*e4a36f41SAndroid Build Coastguard Workerallow gsid vfat:file create_file_perms; 80*e4a36f41SAndroid Build Coastguard Workerallow gsid sdcard_block_device:blk_file r_file_perms; 81*e4a36f41SAndroid Build Coastguard Worker# This is needed for FIBMAP unfortunately. Oddly FIEMAP does not carry this 82*e4a36f41SAndroid Build Coastguard Worker# requirement, but the kernel does not implement FIEMAP support for VFAT. 83*e4a36f41SAndroid Build Coastguard Workerallow gsid self:global_capability_class_set sys_rawio; 84*e4a36f41SAndroid Build Coastguard Worker 85*e4a36f41SAndroid Build Coastguard Worker# Allow rules for gsi_tool. 86*e4a36f41SAndroid Build Coastguard Workeruserdebug_or_eng(` 87*e4a36f41SAndroid Build Coastguard Worker # gsi_tool passes the system image over the adb connection, via stdin. 88*e4a36f41SAndroid Build Coastguard Worker allow gsid adbd:fd use; 89*e4a36f41SAndroid Build Coastguard Worker # Needed when running gsi_tool through "su root" rather than adb root. 90*e4a36f41SAndroid Build Coastguard Worker allow gsid adbd:unix_stream_socket rw_socket_perms; 91*e4a36f41SAndroid Build Coastguard Worker # gsi_tool passes a FIFO to gsid if invoked with pipe redirection. 92*e4a36f41SAndroid Build Coastguard Worker allow gsid { shell su }:fifo_file r_file_perms; 93*e4a36f41SAndroid Build Coastguard Worker # Allow installing images from /storage/emulated/... 94*e4a36f41SAndroid Build Coastguard Worker allow gsid { sdcard_type fuse }:file r_file_perms; 95*e4a36f41SAndroid Build Coastguard Worker') 96*e4a36f41SAndroid Build Coastguard Worker 97*e4a36f41SAndroid Build Coastguard Workerneverallow { 98*e4a36f41SAndroid Build Coastguard Worker domain 99*e4a36f41SAndroid Build Coastguard Worker -gsid 100*e4a36f41SAndroid Build Coastguard Worker -init 101*e4a36f41SAndroid Build Coastguard Worker -update_engine_common 102*e4a36f41SAndroid Build Coastguard Worker -recovery 103*e4a36f41SAndroid Build Coastguard Worker -fastbootd 104*e4a36f41SAndroid Build Coastguard Worker} gsid_prop:property_service set; 105*e4a36f41SAndroid Build Coastguard Worker 106*e4a36f41SAndroid Build Coastguard Worker# gsid needs to store images on /data, but cannot use file I/O. If it did, the 107*e4a36f41SAndroid Build Coastguard Worker# underlying blocks would be encrypted, and we couldn't mount the GSI image in 108*e4a36f41SAndroid Build Coastguard Worker# first-stage init. So instead of directly writing to /data, we: 109*e4a36f41SAndroid Build Coastguard Worker# 110*e4a36f41SAndroid Build Coastguard Worker# 1. fallocate a file large enough to hold the signed GSI 111*e4a36f41SAndroid Build Coastguard Worker# 2. extract its block layout with FIEMAP 112*e4a36f41SAndroid Build Coastguard Worker# 3. create a dm-linear device using the FIEMAP, targeting /dev/block/by-name/userdata 113*e4a36f41SAndroid Build Coastguard Worker# 4. write system_gsi into that dm device 114*e4a36f41SAndroid Build Coastguard Worker# 115*e4a36f41SAndroid Build Coastguard Worker# To make this process work, we need to unwrap the device-mapper stacking for 116*e4a36f41SAndroid Build Coastguard Worker# userdata to reach the underlying block device. To verify the result we use 117*e4a36f41SAndroid Build Coastguard Worker# stat(), which requires read access. 118*e4a36f41SAndroid Build Coastguard Workerallow gsid userdata_block_device:blk_file r_file_perms; 119*e4a36f41SAndroid Build Coastguard Worker 120*e4a36f41SAndroid Build Coastguard Worker# gsid uses /metadata/gsi to communicate GSI boot information to first-stage 121*e4a36f41SAndroid Build Coastguard Worker# init. It cannot use userdata since data cannot be decrypted during this 122*e4a36f41SAndroid Build Coastguard Worker# stage. 123*e4a36f41SAndroid Build Coastguard Worker# 124*e4a36f41SAndroid Build Coastguard Worker# gsid uses /metadata/gsi to store three files: 125*e4a36f41SAndroid Build Coastguard Worker# install_status - A short string indicating whether a GSI image is bootable. 126*e4a36f41SAndroid Build Coastguard Worker# lp_metadata - LpMetadata blob describing the block ranges on userdata 127*e4a36f41SAndroid Build Coastguard Worker# where system_gsi resides. 128*e4a36f41SAndroid Build Coastguard Worker# booted - An empty file that, if exists, indicates that a GSI is 129*e4a36f41SAndroid Build Coastguard Worker# currently running. 130*e4a36f41SAndroid Build Coastguard Worker# 131*e4a36f41SAndroid Build Coastguard Workerallow gsid metadata_file:dir { search getattr }; 132*e4a36f41SAndroid Build Coastguard Workerallow gsid { 133*e4a36f41SAndroid Build Coastguard Worker gsi_metadata_file_type 134*e4a36f41SAndroid Build Coastguard Worker}:dir create_dir_perms; 135*e4a36f41SAndroid Build Coastguard Worker 136*e4a36f41SAndroid Build Coastguard Workerallow gsid { 137*e4a36f41SAndroid Build Coastguard Worker ota_metadata_file 138*e4a36f41SAndroid Build Coastguard Worker}:dir rw_dir_perms; 139*e4a36f41SAndroid Build Coastguard Worker 140*e4a36f41SAndroid Build Coastguard Workerallow gsid { 141*e4a36f41SAndroid Build Coastguard Worker gsi_metadata_file_type 142*e4a36f41SAndroid Build Coastguard Worker ota_metadata_file 143*e4a36f41SAndroid Build Coastguard Worker}:file create_file_perms; 144*e4a36f41SAndroid Build Coastguard Worker 145*e4a36f41SAndroid Build Coastguard Worker# Allow restorecon to fix context of gsi_public_metadata_file. 146*e4a36f41SAndroid Build Coastguard Workerallow gsid file_contexts_file:file r_file_perms; 147*e4a36f41SAndroid Build Coastguard Workerallow gsid gsi_metadata_file:file relabelfrom; 148*e4a36f41SAndroid Build Coastguard Workerallow gsid gsi_public_metadata_file:file relabelto; 149*e4a36f41SAndroid Build Coastguard Worker 150*e4a36f41SAndroid Build Coastguard Workerallow gsid { 151*e4a36f41SAndroid Build Coastguard Worker gsi_data_file 152*e4a36f41SAndroid Build Coastguard Worker ota_image_data_file 153*e4a36f41SAndroid Build Coastguard Worker}:dir create_dir_perms; 154*e4a36f41SAndroid Build Coastguard Workerallow gsid { 155*e4a36f41SAndroid Build Coastguard Worker gsi_data_file 156*e4a36f41SAndroid Build Coastguard Worker ota_image_data_file 157*e4a36f41SAndroid Build Coastguard Worker}:file create_file_perms; 158*e4a36f41SAndroid Build Coastguard Workerallowxperm gsid { 159*e4a36f41SAndroid Build Coastguard Worker gsi_data_file 160*e4a36f41SAndroid Build Coastguard Worker ota_image_data_file 161*e4a36f41SAndroid Build Coastguard Worker}:file ioctl { 162*e4a36f41SAndroid Build Coastguard Worker FS_IOC_FIEMAP 163*e4a36f41SAndroid Build Coastguard Worker FS_IOC_GETFLAGS 164*e4a36f41SAndroid Build Coastguard Worker}; 165*e4a36f41SAndroid Build Coastguard Worker 166*e4a36f41SAndroid Build Coastguard Workerallow gsid system_server:binder call; 167*e4a36f41SAndroid Build Coastguard Worker 168*e4a36f41SAndroid Build Coastguard Worker# Prevent most processes from writing to gsi_metadata_file_type, but allow 169*e4a36f41SAndroid Build Coastguard Worker# adding rules for path resolution of gsi_public_metadata_file and reading 170*e4a36f41SAndroid Build Coastguard Worker# gsi_public_metadata_file. 171*e4a36f41SAndroid Build Coastguard Workerneverallow { 172*e4a36f41SAndroid Build Coastguard Worker domain 173*e4a36f41SAndroid Build Coastguard Worker -init 174*e4a36f41SAndroid Build Coastguard Worker -gsid 175*e4a36f41SAndroid Build Coastguard Worker -fastbootd 176*e4a36f41SAndroid Build Coastguard Worker} gsi_metadata_file_type:dir no_w_dir_perms; 177*e4a36f41SAndroid Build Coastguard Worker 178*e4a36f41SAndroid Build Coastguard Workerneverallow { 179*e4a36f41SAndroid Build Coastguard Worker domain 180*e4a36f41SAndroid Build Coastguard Worker -init 181*e4a36f41SAndroid Build Coastguard Worker -gsid 182*e4a36f41SAndroid Build Coastguard Worker -fastbootd 183*e4a36f41SAndroid Build Coastguard Worker} { gsi_metadata_file_type -gsi_public_metadata_file }:file_class_set *; 184*e4a36f41SAndroid Build Coastguard Worker 185*e4a36f41SAndroid Build Coastguard Workerneverallow { 186*e4a36f41SAndroid Build Coastguard Worker domain 187*e4a36f41SAndroid Build Coastguard Worker -init 188*e4a36f41SAndroid Build Coastguard Worker -gsid 189*e4a36f41SAndroid Build Coastguard Worker -fastbootd 190*e4a36f41SAndroid Build Coastguard Worker} gsi_public_metadata_file:file_class_set ~{ r_file_perms }; 191*e4a36f41SAndroid Build Coastguard Worker 192*e4a36f41SAndroid Build Coastguard Worker# Prevent apps from accessing gsi_metadata_file_type. 193*e4a36f41SAndroid Build Coastguard Workerneverallow { 194*e4a36f41SAndroid Build Coastguard Worker appdomain 195*e4a36f41SAndroid Build Coastguard Worker -shell 196*e4a36f41SAndroid Build Coastguard Worker} gsi_metadata_file_type:dir_file_class_set *; 197*e4a36f41SAndroid Build Coastguard Worker 198*e4a36f41SAndroid Build Coastguard Workerneverallow { 199*e4a36f41SAndroid Build Coastguard Worker domain 200*e4a36f41SAndroid Build Coastguard Worker -init 201*e4a36f41SAndroid Build Coastguard Worker -gsid 202*e4a36f41SAndroid Build Coastguard Worker} gsi_data_file:dir_file_class_set *; 203*e4a36f41SAndroid Build Coastguard Worker 204*e4a36f41SAndroid Build Coastguard Workerneverallow { 205*e4a36f41SAndroid Build Coastguard Worker domain 206*e4a36f41SAndroid Build Coastguard Worker -gsid 207*e4a36f41SAndroid Build Coastguard Worker} gsi_data_file:file_class_set ~{ relabelto getattr }; 208