ãšã³ããªãŒ
ä»æ¥ã¯ãã³ã¢ããå°ãäžã®ã¬ãã«ã§ã»ãã¥ãªãã£ãæ€èšãç¶ããŸãã 第2éšã§ã¯ãsystem.imgãuserdata.imgãcache.imgãã©ãããæ¥ãã®ããããã³ãã€ãã£ããŠãŒã¶ãŒç©ºéã§ã»ãã¥ãªãã£ãã©ã®ããã«æäŸãããããèŠãŠãããŸãã
èå³ã®ããæ¹ãããããïŒ
èšäºäžèŠ§
ãã®ãããã¯ããã®ç§ã®èšäºãžã®ãªã³ã¯ã¯æ¬¡ã®ãšããã§ãã- Androidãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ã»ãã¥ãªãã£ã®åºæ¬ã ã³ã¢ã¬ãã«
- Androidãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ã»ãã¥ãªãã£ã®åºæ¬ã ãã€ãã£ããŠãŒã¶ãŒã¹ããŒã¹ãããŒã1
- Androidãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ã»ãã¥ãªãã£ã®åºæ¬ã ãã€ãã£ããŠãŒã¶ãŒã¹ããŒã¹ãããŒã2
- Androidãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ã»ãã¥ãªãã£ã®åºæ¬ã Application Frameworkã¬ãã«ã§ã®ã»ãã¥ãªãã£ã ãã€ã³ããŒIPC
ãã€ãã£ããŠãŒã¶ãŒã¹ããŒã¹ã®æå³
ãã€ãã£ããŠãŒã¶ãŒã¹ããŒã¹ãšã¯ãDalvikä»®æ³ãã·ã³ã®å€éšã§å®è¡ãããLinuxã«ãŒãã«ã®äžéšã§ã¯ãªããã¹ãŠã®ãŠãŒã¶ãŒã¹ããŒã¹ã³ã³ããŒãã³ããæããŸãã ãã€ãã£ããŠãŒã¶ãŒã¹ããŒã¹ã¯ãç¹å®ã®ã¢ãŒããã¯ãã£çšã«ã³ã³ãã€ã«ãããå®è¡å¯èœãã¡ã€ã«ã§ãã ãããã«ã¯ãinitã¹ã¯ãªããããèªåçã«ãŸãã¯ã€ãã³ãã®ã€ãã³ãã§èµ·åãããå®è¡å¯èœãã¡ã€ã«ãããŒã«ããã¯ã¹ãŠãŒãã£ãªãã£ãããã³ãŠãŒã¶ãŒãã·ã§ã«ã®äžããå®è¡ã§ããå®è¡å¯èœãã¡ã€ã«ãå«ãŸããŸããéå§ãã
æåã®éšåã§è¿°ã¹ãããã«ãAndroidã¯Linuxã«ãŒãã«ã«åºã¥ããŠããŸãã ãã¹ãŠã®Linuxã·ã¹ãã ãšåæ§ã«ãAndroidã®ã»ãã¥ãªãã£ã¯ã¢ã¯ã»ã¹å¶åŸ¡ã«åºã¥ããŠããŸãã ã€ãŸã åãªãœãŒã¹ïŒãã¡ã€ã«ãªã©ïŒã«ã¯ããã®ãã¡ã€ã«ã®äœæè ïŒææè ïŒææè ïŒïŒããã³ææè ïŒææè ïŒã«å±ããã¡ã€ã³ã°ã«ãŒãïŒææè ã°ã«ãŒãïŒã«é¢ããã¡ã¿æ å ±ãå«ãŸããŠããŸãã åããã»ã¹ã¯ãäžéšã®ãŠãŒã¶ãŒïŒãŠãŒã¶ãŒïŒã«ä»£ãã£ãŠèµ·åãããŸãã åãŠãŒã¶ãŒã«ã¯ã³ã¢ã°ã«ãŒãããããŸãã ããã«ã圌ã¯ä»ã®ã°ã«ãŒãã®ã¡ã³ããŒã§ããå¯èœæ§ããããŸãã ãããã£ãŠããªãœãŒã¹ïŒãã¡ã€ã«ãªã©ïŒã®èªã¿åã/æžã蟌ã¿/å®è¡ãå¯èœãªãŠãŒã¶ãŒã«é¢ããæ å ±ãïŒrwxrwxrwx圢åŒã§ïŒåãªãœãŒã¹ã«æ·»ä»ãããšããã®ãã¡ã€ã«ãžã®ã¢ã¯ã»ã¹ãå¶åŸ¡ã§ããŸãã ããšãã°ããã¡ã€ã«ã«ã¢ã¯ã»ã¹èš±å¯ãå²ãåœãŠãããšãã§ããŸãããã®ãã¡ã€ã«ã®ææè ïŒææè ïŒããã®ãã¡ã€ã«ã§ã§ããããšã ææè ã°ã«ãŒãã®äžéšã§ãããŠãŒã¶ãŒãã§ããããšã 誰ããã§ããããšã ãã㧠ãããã«ã€ããŠè©³ããèªãããšãã§ããŸãã
ãã ããAndroidã«ã¯ããã€ãã®éãããããŸãã ãŸããAndroidã¯æåã¯æºåž¯é»è©±çšã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ãããåç¥ã®ããã«ãããã¯éåžžã«å人çãªãã®ã«å±ããééã£ãæã«æž¡ããããªããã®ã§ãã ã€ãŸãããŠãŒã¶ãŒã1人ã ãã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ãšããŠèããããŠããŸããã ãããã£ãŠãã»ãã¥ãªãã£ã®ããã«ããŸããŸãªLinuxãŠãŒã¶ãŒã䜿çšããããšã決å®ãããŸããïŒã¢ããªã±ãŒã·ã§ã³ããšã«-æåã®èšäºã§æ¢ã«è¿°ã¹ãããã«ãåå¥ã®ãŠãŒã¶ãŒïŒã 第äºã«ãAndroidã§ã¯ãäžéšã®ãŠãŒã¶ãŒïŒãŠãŒã¶ãŒïŒãšãã®UIDïŒèå¥åïŒãã·ã¹ãã ã«ããŒãã³ãŒãã£ã³ã°ãããŠãããããã»ãã¥ãªãã£ã«é¢ä¿ãã人ã ããå€ãã®èŠæ ãå¯ããããŠããŸãïŒæ£çŽãªãšããããã®ã¢ãããŒãããªãæ¹å€ãããŠããã®ãæ¬åœã«ããããŸããïŒã ãããã®ãŠãŒã¶ãŒã¯ããã¡ã€ã«ã·ã¹ãã /core/include/private/android_filesystem_config.hã§æ¢ã«èŠãŠããŸããããšãã°ã rootã®èå¥åã¯0ã§ã system - 1000ã§ãã
ãã§ã«è¿°ã¹ãããã«ãããã»ã¹ã¯ããã®æ°ããããã»ã¹ãéå§ããããã»ã¹ãšåããŠãŒã¶ãŒïŒUIDïŒã«ä»£ãã£ãŠéå§ãããŸãã UIDïŒcalling_processïŒ== UIDïŒcalled_processïŒã Androidã§å®è¡ãããæåã®ããã»ã¹-init-ã¯ã«ãŒããšããŠå®è¡ãããŸã ïŒUID == 0ïŒã ãããã£ãŠãçè«äžããã¹ãŠã®ããã»ã¹ã¯åããŠãŒã¶ãŒã«ä»£ãã£ãŠéå§ããå¿ èŠããããŸãã ããããããã ã£ãã§ãããã ãã ããæåã«ãç¹æš©ãŠãŒã¶ãŒïŒããã³ç¹å®ã®æ©èœãæã€ãŠãŒã¶ãŒïŒã«ä»£ãã£ãŠå®è¡ãããããã»ã¹ã¯ãUIDãç¹æš©ã®äœããŠãŒã¶ãŒã«å€æŽã§ããŸãã 次ã«ãAndroidã§ã¯ã init.rcã¹ã¯ãªããã§ããŒã¢ã³ãèµ·åãããšãã«ããã®ããã»ã¹ãéå§ãããŠãŒã¶ãŒãšã°ã«ãŒãã®ç¹æš©ãæå®ããããšãã§ããŸãã
... service console /system/bin/sh class core console disabled user shell group log ... service servicemanager /system/bin/servicemanager class core user system group system critical onrestart restart zygote onrestart restart media onrestart restart surfaceflinger onrestart restart drm ... service media /system/bin/mediaserver class main user media group audio camera inet net_bt net_bt_admin net_bw_acct drmrpc ioprio rt 4 ...
ãããã®ããŒã¢ã³ãä»ããŠèµ·åããããã¹ãŠã®ããã»ã¹ã«ã¯ãã«ãŒãæš©éããªããªããŸãã
ã·ã¹ãã ãããŒã¿ããã£ãã·ã¥
ç§ã¯ãã®ãããã¯ãäœåºŠãçºè¡šããŠããã®ã§ãéåžžã«è€éã§æ··ä¹±ããŠãããšæãã§ãããã å®éãããã¯ããã§ã¯ãããŸããã System.img ã userdata.imgããã³cache.imgã¯ãAndroidãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ãã³ã³ãã€ã«ããçµæã§ãã ã€ãŸããã·ã¹ãã ãçµã¿ç«ãŠãçµæããããã®3ã€ã®ãã¡ã€ã«ãååŸãããããã€ã¹ã«æžã蟌ãŸããŸãã
ããããæãéèŠãªããšã¯ããã§ã¯ãããŸããã Androidã·ã¹ãã ã§ã¯ãã·ã¹ãã ãŠãŒã¶ãŒã®ãŠãŒã¶ãŒåãšUIDãããŒãã³ãŒãã£ã³ã°ãããŠããããããã§ã«ã³ã³ãã€ã«æ®µéã§ããããã®ã€ã¡ãŒãžã®ããŸããŸãªãã£ã¬ã¯ããªã«å¯ŸããããŸããŸãªã·ã¹ãã ãŠãŒã¶ãŒã®ã¢ã¯ã»ã¹æš©ã決å®ã§ããŸãã ãããã®æš©éã¯ãæåã®èšäºã§ãã§ã«èª¬æããsystem / core / include / private / android_filesystem_config.hãã¡ã€ã«ã§æå®ãããŠããŸã ã ã¢ã¯ã»ã¹æš©ã¯ã次ã®ããã«ãã£ã¬ã¯ããªïŒandroid_dirs []ïŒããã³ãã¡ã€ã«ïŒandroid_files []ïŒã«å¯ŸããŠå¥ã ã«å®çŸ©ãããŸãã
... struct fs_path_config { unsigned mode; unsigned uid; unsigned gid; uint64_t capabilities; const char *prefix; }; /* Rules for directories. ** These rules are applied based on "first match", so they ** should start with the most specific path and work their ** way up to the root. */ static const struct fs_path_config android_dirs[] = { { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" }, { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" }, { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" }, { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/dalvik-cache" }, { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" }, { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" }, { 00771, AID_SHELL, AID_SHELL, 0, "data/local" }, { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" }, { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" }, { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" }, { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" }, { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" }, { 00750, AID_ROOT, AID_SHELL, 0, "sbin" }, { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" }, { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" }, { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" }, { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" }, { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" }, { 00755, AID_ROOT, AID_ROOT, 0, 0 }, }; /* Rules for files. ** These rules are applied based on "first match", so they ** should start with the most specific path and work their ** way up to the root. Prefixes ending in * denotes wildcard ** and will allow partial matches. */ static const struct fs_path_config android_files[] = { { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" }, { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" }, { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.trout.rc" }, { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" }, { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.testmenu" }, { 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" }, { 00440, AID_BLUETOOTH, AID_BLUETOOTH, 0, "system/etc/dbus.conf" }, { 00444, AID_RADIO, AID_AUDIO, 0, "system/etc/AudioPara4.csv" }, { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" }, { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" }, { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" }, { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" }, { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" }, { 00644, AID_APP, AID_APP, 0, "data/data/*" }, { 00755, AID_ROOT, AID_ROOT, 0, "system/bin/ping" }, /* the following file is INTENTIONALLY set-gid and not set-uid. * Do not change. */ { 02750, AID_ROOT, AID_INET, 0, "system/bin/netcfg" }, /* the following five files are INTENTIONALLY set-uid, but they * are NOT included on user builds. */ { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/su" }, { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" }, { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" }, { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" }, { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/tcpdump" }, { 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" }, /* the following file has enhanced capabilities and IS included in user builds. */ { 00750, AID_ROOT, AID_SHELL, (1 << CAP_SETUID) | (1 << CAP_SETGID), "system/bin/run-as" }, { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" }, { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" }, { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" }, { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" }, { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" }, { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" }, { 00750, AID_ROOT, AID_SHELL, 0, "init*" }, { 00750, AID_ROOT, AID_SHELL, 0, "charger*" }, { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" }, { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" }, { 00644, AID_ROOT, AID_ROOT, 0, 0 }, }; ...
ãŸãããã®ãã¡ã€ã«ã®åŸåã§å®çŸ©ãããŠããéçã€ã³ã©ã€ã³void fs_configé¢æ°ïŒconst char * pathãint dirãunsigned * uidãunsigned * gidãunsigned * modeãuint64_t * capabilitiesïŒã¯ãææè ãææè ã°ã«ãŒããæ©èœãããã³ã¢ã¯ã»ã¹æš©ã®èšå®ãæ åœããŸãã ãã®é¢æ°ã¯ã ã€ã¡ãŒãžã®ã¢ã»ã³ããªäžã«åŒã³åºãããŸã ã
äžè¬ã«ãäžéšã®ãã¡ã€ã«ã«èš±å¯ãã©ã° ïŒsetuidããã³setgidïŒãèšå®ããããšãé€ããŠãããã®ãã¹ãŠã¯å€ããå°ãªããæ確ã§ãªããã°ãªããŸããïŒããšãã°ããsystem / xbin / suãã®èš±å¯ã¯06755ãšããŠå®çŸ©ãããæåã®6ã¯ãã©ã°ãèšå®ãããããšãæå³ããŸãïŒãŠãŒã¶ãŒIDïŒ4ïŒãèšå®ããã°ã«ãŒãIDãèšå®ããããã®ãã©ã°ïŒ2ïŒãèšå®ããŸãïŒã ãããã®ãã©ã°ãèšå®ãããšããŠãŒã¶ãŒã¯èµ·åãããããã»ã¹ã®æš©éããã¡ã€ã«ãŸãã¯ææè ã°ã«ãŒãïŒææè ã°ã«ãŒãïŒã®ææè ïŒææè ïŒã®ã¬ãã«ãŸã§å¢ããããšãã§ããŸãã Androidã®å Žåãåã¢ããªã±ãŒã·ã§ã³ã¯ç¬èªã®UIDãšGIDãæã€ãŠãŒã¶ãŒã§ãã ãããã£ãŠãããã©ã«ãã§ã¯ãã¢ããªã±ãŒã·ã§ã³ãããã€ãã£ãå®è¡å¯èœãã¡ã€ã«ãå®è¡ãããšããããåŒã³åºããã¢ããªã±ãŒã·ã§ã³ãšåãUIDããã³GIDã§å®è¡ãããŸãã ãããã®èš±å¯ãã©ã°ãèšå®ãããšãææè æš©éã§ãã€ãã£ãå®è¡å¯èœãã¡ã€ã«ãå®è¡ã§ããŸãã ãã®äŸã§ã¯ãææè ã¯AID_ROOTïŒã«ãŒãïŒã§ãã ããã¯ã次ã®system / extras / su / su.cã§çºçããŸãã
int main(int argc, char **argv) { ... int uid, gid, myuid; ... if(setgid(gid) || setuid(uid)) { ... } ... }
ã€ãŸã setuidããã³setgidé¢æ°ãåŒã³åºãããŸãã ãã®å Žåããããã®æ©èœãæ£åžžã«å®äºãããšãããã»ã¹ã¯ãã®ãã¡ã€ã«ã®ææè ãšææè ã°ã«ãŒãã«ä»£ãã£ãŠäœæ¥ãéå§ããŸãã ãã®äŸã§ã¯ããã®ããã»ã¹ã¯ã¹ãŒããŒãŠãŒã¶ãŒã®æš©å©ãåãåããŸãã 圌ã¯èªåã®ããããããšã¯äœã§ãã§ããŸã:)ãã®ãããªç¡ç§©åºã¯åžžã«æ£åœåããããšã¯éããªãã®ã§ãLinuxã¯èœåã®æŠå¿µãå°å ¥ããŸãã run-asã¢ããªã±ãŒã·ã§ã³ã¯ãã¹ãŠã®ã¹ãŒããŒãŠãŒã¶ãŒæš©éãå¿ èŠãšããªããããããŸããŸãªãŠãŒã¶ãŒã«ä»£ãã£ãŠã¢ããªã±ãŒã·ã§ã³ãå®è¡ããããã«èå¥åãå€æŽã§ããå¿ èŠããããŸãã ã¡ãªã¿ã«ãæ©èœã¯æè¿ç»å Žããããã§ã-Android 2.3.xã§ã¯ãããã¯èŠãŠããŸããã
å®å šæ§
ç¹æš©ããã°ã©ã ïŒsuãªã©ïŒã®å Žåããããã®ããã°ã©ã ãåŒã³åºãããšãã§ããã¢ããªã±ãŒã·ã§ã³ã®ç¯å²ãå¶éããå¿ èŠããããŸãã ãã以å€ã®å Žåãã¢ããªã±ãŒã·ã§ã³ã¯ã¹ãŒããŒãŠãŒã¶ãŒæš©éãååŸã§ããŸãã ãããã£ãŠãéåžžã«å€ãã®å ŽåãUIDæ€èšŒã¯ãã®ãããªããã°ã©ã ã«çµã¿èŸŒãŸããŠããŸãã
... #include <private/android_filesystem_config.h> ... int main(int argc, char **argv) { struct passwd *pw; int uid, gid, myuid; /* Until we have something better, only root and the shell can use su. */ myuid = getuid(); if (myuid != AID_ROOT && myuid != AID_SHELL) { fprintf(stderr,"su: uid %d not allowed to su\n", myuid); return 1; } ... }
ã€ãŸã ããã°ã©ã ã¯ã getuidïŒïŒé¢æ°ã䜿çšããŠãåŒã³åºãããã»ã¹ãéå§ããã人ãæåã«ãã§ãã¯ããŸãã ãããŠããããã®å€ãã·ã¹ãã ã«ããŒãã³ãŒããããå€ãšæ¯èŒããŸãã ãã®å ŽåããŠãŒã¶ãŒãsystemãããã³ãrootãã«ä»£ãã£ãŠå®è¡ãããããã»ã¹ã®ã¿ãsuã䜿çšããæš©å©ãæã¡ãŸãã
ãããã«
ãã®èšäºã§ã¯ããã€ãã£ããŠãŒã¶ãŒç©ºéã¬ãã«ã§ã®ã»ãã¥ãªãã£ã®æäŸæ¹æ³ã®å解ãçµäºããŸããã 次ã®èšäºã§ã¯ãèš±å¯ã®ä»çµã¿ãåæããäºå®ã§ãããçŸåšã®è² è·ã倧ããããããã€æžã蟌ã¿ãéå§ããã®ãããããŸããã ãã€ãã®ããã«ãç§ã¯è¿œå ãšä¿®æ£ã«éåžžã«æºè¶³ããŠããŸãã
PS DevConf @ mobiã®ã¬ããŒãã®äœæã«æåŸ ãããŸãããã¢ããªã±ãŒã·ã§ã³éçºè ã«çŠç¹ãåœãŠãäŒè°ã§ãèŽè¡ã¯Androidãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ã»ãã¥ãªãã£ã«é¢ããã¬ããŒãã«èå³ããããšæããŸããïŒ
åç §è³æ
- Karim Yaghmourã«ãããçµã¿èŸŒã¿Androidã
- Marko Gargentaã«ããAndroidã»ãã¥ãªãã£ã®åºç€
- LinuxãŠãŒã¶ãŒãšã°ã«ãŒã
- ã€ã¡ãŒãžæ§æ
- Suid
- æ©èœã®æŠèŠ