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 Workerallow gsid proc_cmdline:file r_file_perms; 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# liblp queries these block alignment properties. 59*e4a36f41SAndroid Build Coastguard Workerallowxperm gsid { userdata_block_device sdcard_block_device }:blk_file ioctl { 60*e4a36f41SAndroid Build Coastguard Worker BLKIOMIN 61*e4a36f41SAndroid Build Coastguard Worker BLKALIGNOFF 62*e4a36f41SAndroid Build Coastguard Worker}; 63*e4a36f41SAndroid Build Coastguard Worker 64*e4a36f41SAndroid Build Coastguard Worker# When installing images to an sdcard, gsid needs to be able to stat() the 65*e4a36f41SAndroid Build Coastguard Worker# block device. gsid also calls realpath() to remove symlinks. 66*e4a36f41SAndroid Build Coastguard Workerallow gsid mnt_media_rw_file:dir r_dir_perms; 67*e4a36f41SAndroid Build Coastguard Workerallow gsid mnt_media_rw_stub_file:dir r_dir_perms; 68*e4a36f41SAndroid Build Coastguard Worker 69*e4a36f41SAndroid Build Coastguard Worker# When installing images to an sdcard, gsid must bypass sdcardfs and install 70*e4a36f41SAndroid Build Coastguard Worker# directly to vfat, which supports the FIBMAP ioctl. 71*e4a36f41SAndroid Build Coastguard Workerallow gsid vfat:dir create_dir_perms; 72*e4a36f41SAndroid Build Coastguard Workerallow gsid vfat:file create_file_perms; 73*e4a36f41SAndroid Build Coastguard Workerallow gsid sdcard_block_device:blk_file r_file_perms; 74*e4a36f41SAndroid Build Coastguard Worker# This is needed for FIBMAP unfortunately. Oddly FIEMAP does not carry this 75*e4a36f41SAndroid Build Coastguard Worker# requirement, but the kernel does not implement FIEMAP support for VFAT. 76*e4a36f41SAndroid Build Coastguard Workerallow gsid self:global_capability_class_set sys_rawio; 77*e4a36f41SAndroid Build Coastguard Worker 78*e4a36f41SAndroid Build Coastguard Worker# Allow rules for gsi_tool. 79*e4a36f41SAndroid Build Coastguard Workeruserdebug_or_eng(` 80*e4a36f41SAndroid Build Coastguard Worker # gsi_tool passes the system image over the adb connection, via stdin. 81*e4a36f41SAndroid Build Coastguard Worker allow gsid adbd:fd use; 82*e4a36f41SAndroid Build Coastguard Worker # Needed when running gsi_tool through "su root" rather than adb root. 83*e4a36f41SAndroid Build Coastguard Worker allow gsid adbd:unix_stream_socket rw_socket_perms; 84*e4a36f41SAndroid Build Coastguard Worker # gsi_tool passes a FIFO to gsid if invoked with pipe redirection. 85*e4a36f41SAndroid Build Coastguard Worker allow gsid { shell su }:fifo_file r_file_perms; 86*e4a36f41SAndroid Build Coastguard Worker # Allow installing images from /storage/emulated/... 87*e4a36f41SAndroid Build Coastguard Worker allow gsid sdcard_type:file r_file_perms; 88*e4a36f41SAndroid Build Coastguard Worker') 89*e4a36f41SAndroid Build Coastguard Worker 90*e4a36f41SAndroid Build Coastguard Workerneverallow { 91*e4a36f41SAndroid Build Coastguard Worker domain 92*e4a36f41SAndroid Build Coastguard Worker -gsid 93*e4a36f41SAndroid Build Coastguard Worker -init 94*e4a36f41SAndroid Build Coastguard Worker -update_engine_common 95*e4a36f41SAndroid Build Coastguard Worker -recovery 96*e4a36f41SAndroid Build Coastguard Worker -fastbootd 97*e4a36f41SAndroid Build Coastguard Worker} gsid_prop:property_service set; 98*e4a36f41SAndroid Build Coastguard Worker 99*e4a36f41SAndroid Build Coastguard Worker# gsid needs to store images on /data, but cannot use file I/O. If it did, the 100*e4a36f41SAndroid Build Coastguard Worker# underlying blocks would be encrypted, and we couldn't mount the GSI image in 101*e4a36f41SAndroid Build Coastguard Worker# first-stage init. So instead of directly writing to /data, we: 102*e4a36f41SAndroid Build Coastguard Worker# 103*e4a36f41SAndroid Build Coastguard Worker# 1. fallocate a file large enough to hold the signed GSI 104*e4a36f41SAndroid Build Coastguard Worker# 2. extract its block layout with FIEMAP 105*e4a36f41SAndroid Build Coastguard Worker# 3. create a dm-linear device using the FIEMAP, targeting /dev/block/by-name/userdata 106*e4a36f41SAndroid Build Coastguard Worker# 4. write system_gsi into that dm device 107*e4a36f41SAndroid Build Coastguard Worker# 108*e4a36f41SAndroid Build Coastguard Worker# To make this process work, we need to unwrap the device-mapper stacking for 109*e4a36f41SAndroid Build Coastguard Worker# userdata to reach the underlying block device. To verify the result we use 110*e4a36f41SAndroid Build Coastguard Worker# stat(), which requires read access. 111*e4a36f41SAndroid Build Coastguard Workerallow gsid userdata_block_device:blk_file r_file_perms; 112*e4a36f41SAndroid Build Coastguard Worker 113*e4a36f41SAndroid Build Coastguard Worker# gsid uses /metadata/gsi to communicate GSI boot information to first-stage 114*e4a36f41SAndroid Build Coastguard Worker# init. It cannot use userdata since data cannot be decrypted during this 115*e4a36f41SAndroid Build Coastguard Worker# stage. 116*e4a36f41SAndroid Build Coastguard Worker# 117*e4a36f41SAndroid Build Coastguard Worker# gsid uses /metadata/gsi to store three files: 118*e4a36f41SAndroid Build Coastguard Worker# install_status - A short string indicating whether a GSI image is bootable. 119*e4a36f41SAndroid Build Coastguard Worker# lp_metadata - LpMetadata blob describing the block ranges on userdata 120*e4a36f41SAndroid Build Coastguard Worker# where system_gsi resides. 121*e4a36f41SAndroid Build Coastguard Worker# booted - An empty file that, if exists, indicates that a GSI is 122*e4a36f41SAndroid Build Coastguard Worker# currently running. 123*e4a36f41SAndroid Build Coastguard Worker# 124*e4a36f41SAndroid Build Coastguard Workerallow gsid metadata_file:dir { search getattr }; 125*e4a36f41SAndroid Build Coastguard Workerallow gsid { 126*e4a36f41SAndroid Build Coastguard Worker gsi_metadata_file_type 127*e4a36f41SAndroid Build Coastguard Worker}:dir create_dir_perms; 128*e4a36f41SAndroid Build Coastguard Worker 129*e4a36f41SAndroid Build Coastguard Workerallow gsid { 130*e4a36f41SAndroid Build Coastguard Worker ota_metadata_file 131*e4a36f41SAndroid Build Coastguard Worker}:dir rw_dir_perms; 132*e4a36f41SAndroid Build Coastguard Worker 133*e4a36f41SAndroid Build Coastguard Workerallow gsid { 134*e4a36f41SAndroid Build Coastguard Worker gsi_metadata_file_type 135*e4a36f41SAndroid Build Coastguard Worker ota_metadata_file 136*e4a36f41SAndroid Build Coastguard Worker}:file create_file_perms; 137*e4a36f41SAndroid Build Coastguard Worker 138*e4a36f41SAndroid Build Coastguard Worker# Allow restorecon to fix context of gsi_public_metadata_file. 139*e4a36f41SAndroid Build Coastguard Workerallow gsid file_contexts_file:file r_file_perms; 140*e4a36f41SAndroid Build Coastguard Workerallow gsid gsi_metadata_file:file relabelfrom; 141*e4a36f41SAndroid Build Coastguard Workerallow gsid gsi_public_metadata_file:file relabelto; 142*e4a36f41SAndroid Build Coastguard Worker 143*e4a36f41SAndroid Build Coastguard Workerallow gsid { 144*e4a36f41SAndroid Build Coastguard Worker gsi_data_file 145*e4a36f41SAndroid Build Coastguard Worker ota_image_data_file 146*e4a36f41SAndroid Build Coastguard Worker}:dir rw_dir_perms; 147*e4a36f41SAndroid Build Coastguard Workerallow gsid { 148*e4a36f41SAndroid Build Coastguard Worker gsi_data_file 149*e4a36f41SAndroid Build Coastguard Worker ota_image_data_file 150*e4a36f41SAndroid Build Coastguard Worker}:file create_file_perms; 151*e4a36f41SAndroid Build Coastguard Workerallowxperm gsid { 152*e4a36f41SAndroid Build Coastguard Worker gsi_data_file 153*e4a36f41SAndroid Build Coastguard Worker ota_image_data_file 154*e4a36f41SAndroid Build Coastguard Worker}:file ioctl { 155*e4a36f41SAndroid Build Coastguard Worker FS_IOC_FIEMAP 156*e4a36f41SAndroid Build Coastguard Worker FS_IOC_GETFLAGS 157*e4a36f41SAndroid Build Coastguard Worker}; 158*e4a36f41SAndroid Build Coastguard Worker 159*e4a36f41SAndroid Build Coastguard Workerallow gsid system_server:binder call; 160*e4a36f41SAndroid Build Coastguard Worker 161*e4a36f41SAndroid Build Coastguard Worker# Prevent most processes from writing to gsi_metadata_file_type, but allow 162*e4a36f41SAndroid Build Coastguard Worker# adding rules for path resolution of gsi_public_metadata_file and reading 163*e4a36f41SAndroid Build Coastguard Worker# gsi_public_metadata_file. 164*e4a36f41SAndroid Build Coastguard Workerneverallow { 165*e4a36f41SAndroid Build Coastguard Worker domain 166*e4a36f41SAndroid Build Coastguard Worker -init 167*e4a36f41SAndroid Build Coastguard Worker -gsid 168*e4a36f41SAndroid Build Coastguard Worker -fastbootd 169*e4a36f41SAndroid Build Coastguard Worker} gsi_metadata_file_type:dir no_w_dir_perms; 170*e4a36f41SAndroid Build Coastguard Worker 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 -gsi_public_metadata_file }:file_class_set *; 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_public_metadata_file:file_class_set ~{ r_file_perms }; 184*e4a36f41SAndroid Build Coastguard Worker 185*e4a36f41SAndroid Build Coastguard Worker# Prevent apps from accessing gsi_metadata_file_type. 186*e4a36f41SAndroid Build Coastguard Workerneverallow { 187*e4a36f41SAndroid Build Coastguard Worker appdomain 188*e4a36f41SAndroid Build Coastguard Worker -shell 189*e4a36f41SAndroid Build Coastguard Worker} gsi_metadata_file_type:dir_file_class_set *; 190*e4a36f41SAndroid Build Coastguard Worker 191*e4a36f41SAndroid Build Coastguard Workerneverallow { 192*e4a36f41SAndroid Build Coastguard Worker domain 193*e4a36f41SAndroid Build Coastguard Worker -init 194*e4a36f41SAndroid Build Coastguard Worker -gsid 195*e4a36f41SAndroid Build Coastguard Worker} gsi_data_file:dir_file_class_set *; 196*e4a36f41SAndroid Build Coastguard Worker 197*e4a36f41SAndroid Build Coastguard Workerneverallow { 198*e4a36f41SAndroid Build Coastguard Worker domain 199*e4a36f41SAndroid Build Coastguard Worker -gsid 200*e4a36f41SAndroid Build Coastguard Worker} gsi_data_file:file_class_set ~{ relabelto getattr }; 201