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