Run shell script at boot in AOSP

柔情痞子 提交于 2020-07-22 06:07:09

问题


I am using iMX 8 Mini EVK for my Project. I build Android 9.0 from AOSP for this board. Now I want to run a script at boot. I did following changes in files but still, I am facing an issue.

File : Android_AOSP_build/device/fsl/imx8m/evk_8mm/init.rc

service gea3appservice /vendor/bin/sh /vendor/bin/run.sh 
  class late_start
  user root system
  group root system
  oneshot

File : Android_AOSP_build/device/fsl/imx8m/evk_8mm/sepolicy/gea3appservice.te

type gea3appservice, domain;
type gea3appservice_exec, exec_type, vendor_file_type, file_type;

init_daemon_domain(gea3appservice)

domain_auto_trans(init, vendor_shell_exec, gea3appservice)

File : Android_AOSP_build/device/fsl/imx8m/evk_8mm/sepolicy/file_contexts

/vendor/bin/run.sh   u:object_r:gea3appservice_exec:s0

When I manually run service I get following error :

[ 134.010656] type=1400 audit(1564667688.236:3740): avc: denied { dac_read_search } for pid=1 comm="init" capability=2 scontext=u:r:init:s0 tcontext=u:r:init:s0 tclass=capability permissive=1

Does anyone know this issue?

I tried with the approach suggested by the Android developer site

https://source.android.com/security/selinux/device-policy

But I get the following error

libsepol.report_failure: neverallow on line 1002 of system/sepolicy/public/domain.te (or line 11242 of policy.conf) violated by allow gea3appservice gea3appservice_exec:file { execute entrypoint };


回答1:


The failed neverallow rule could indicate that your script tried to run a binary in /system, which is not allowed by a script located in /vendor according to the rule in system/sepolicy/public/domain.te.

Do not allow vendor components to execute files from system except for the ones whitelist here.




回答2:


this works for me

in init.mydevice.rc i have

on property:sys.boot_completed=1
    start init-myservice-sh

service init-myservice-sh /vendor/bin/init.myscript.sh
    class main
    user root
    group root system
    disabled
    oneshot

and this is init.myscript.sh

#!/system/bin/sh

echo '#################  It works  ##################'
cd /system/app
ls -hal

in device/myvendor/mydevice/sepolicy folder i have file_contexts with

/vendor/bin/init\.myscript\.sh      u:object_r:init-myservice_exec:s0

and init-myservice.te

type init-myservice, domain;
type init-myservice_exec, exec_type, vendor_file_type, file_type;

init_daemon_domain(init-myservice)

allow init-myservice vendor_shell_exec:file rx_file_perms;
allow init-myservice vendor_toolbox_exec:file rx_file_perms;

of course you have to copy your script to the bin dir

PRODUCT_COPY_FILES += \
 $(LOCAL_PATH)/init.myscript.sh:$(TARGET_COPY_OUT_VENDOR)/bin/init.myscript.sh

and in BoardConfig.mk

BOARD_SEPOLICY_DIRS := device/myvendor/mydevice/sepolicy

in my console i can see this

console:/ $ dmesg | grep myservice                                             
[   21.098013] init: starting service 'init-myservice-sh'...
[   21.148562] init: Command 'start init-myservice-sh' action=sys.boot_completed=1 (/vendor/etc/init/hw/init.mydevice.rc:66) took 51ms and succeeded

and try this

console:/ $ init.myscript.sh
#################  It works  ##################

for moor details see this article https://source.android.com/security/selinux/device-policy#label_new_services_and_address_denials



来源:https://stackoverflow.com/questions/57370029/run-shell-script-at-boot-in-aosp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!