Red Hatã®æ£®è¥ã§ãã
èªåã§systemdã®service unitãä½ãã¨ãã«ãèµ·åç¨ã®ããã¤ãã®ã³ãã³ããè¨è¿°ããã·ã§ã«ã¹ã¯ãªãããå¼ã¶äºã¯(çæ³çã§ã¯ãªãã§ãã)ãããã¨æãã¾ãã
ä»åã¯ãã®å ´åã«ãsudoãå©ç¨ããã¨ã¾ããçç±ã説æãã¦ããããã«setprivã使ãã»ããããã¨ãã話ã§ãã
- ä¾é¡ç¨ã®service
- å®è¡ãã¦ã¿ã
- å¥ã®cgroupã ã¨ä½ãã¾ããã®ã?
- 対çã¯setprivã³ãã³ã
ä¾é¡ç¨ã®service
sudoã«ããã¾ããåä½ã確èªããããã®ã§ããã ãåç´ãªä¾ã¨ãã¦ãhoge.service ãç¨æãã¾ãã
/opt/hoge/hoge.sh
#!/bin/bash sudo -u moriwaka sleep 5000
/etc/systemd/system/hoge.service
[Unit] Description=hoge [Service] Type=oneshot ExecStart=/opt/hoge/hoge.sh
å®è¡ãã¦ã¿ã
systemctl ã®ããã©ã«ãã ã¨hoge.shã®å®è¡çµäºãå¾
ã£ã¦ãã¾ãã®ã§ã --no-block
ãªãã·ã§ã³ãã¤ãã¦å®è¡ãã¾ãã
# systemctl --no-block start hoge.service
systemctl status
ã§ç¶æ
ãè¦ã¦ã¿ã¾ããExecStartã®ã¹ã¯ãªãããå®è¡ä¸ãªã®ã§activatingãªã®ã¯äºæ³éãã§ãããå®è¡ä¸ã®ã¯ãã® sleep ãCGroupã®ä¸ã«ãªããbashã ããåå¨ãã¦ãããã¨ããããã¾ãã
# systemctl status hoge â hoge.service - hoge Loaded: loaded (/etc/systemd/system/hoge.service; static) Active: activating (start) since Wed 2022-09-21 13:42:32 JST; 8min ago Main PID: 4110436 (hoge.sh) Tasks: 1 (limit: 37738) Memory: 1.7M CPU: 10ms CGroup: /system.slice/hoge.service ââ 4110436 /bin/bash /opt/hoge/hoge.sh Sep 21 13:42:32 turtle systemd[1]: Starting hoge.service - hoge... Sep 21 13:42:32 turtle sudo[4110437]: root : PWD=/ ; USER=moriwaka ; COMMAND=/usr/bin/sleep 5000
cgroupã®æ§åãã¿ãã¨ãã«ã¯systemd-cgls
ã使ãã¾ããsudoã¨sleepãããµã¼ãã¹ã«å¯¾å¿ãã /system.slice/hoge.service
ã§ã¯ãªã /user.slice/user-1000.slice/session-c4.scope
å
ã§å®è¡ããã¦ãããã¨ããããã¾ãã
ããã¯sudoã§æå®ããã¦ã¼ã¶(id: moriwaka, UID: 1000)ã®ã»ãã·ã§ã³ãc4ããä½æããããã®ä¸ã§å®è¡ããã¦ãããã¨ã示ãã¦ãã¾ãã
-.slice ââuser.slice (#1144) â â trusted.invocation_id: 1041b6b580784b47b41c08d97ab05e23 â ââuser-1000.slice (#5335) â â trusted.invocation_id: a9169cd85293489e9697b4d337962a86 â ââ[email protected] ⦠(#5423) (ä¸ç¥) â ââsession-c4.scope (#1663079) â â â trusted.invocation_id: d257de8a94d54a6eac713a35e228dc27 â â ââ 4110437 sudo -u moriwaka sleep 5000 â â ââ 4110439 sleep 5000 (以ä¸ç¥)
systemctlã§scopeã®ç¶æ ã表示ãããã¨ãã®ããã«è¡¨ç¤ºããã¾ãã sudoãpam_unixãçµç±ãã¦ã»ãã·ã§ã³ãä½æããæ¨ã®ãã°ã表示ããã¦ãã¾ãã
# systemctl status session-c4.scope â session-c4.scope - Session c4 of User moriwaka Loaded: loaded (/run/systemd/transient/session-c4.scope; transient) Transient: yes Active: active (running) since Wed 2022-09-21 13:42:32 JST; 13min ago Tasks: 2 Memory: 392.0K CPU: 2ms CGroup: /user.slice/user-1000.slice/session-c4.scope ââ 4110437 sudo -u moriwaka sleep 5000 ââ 4110439 sleep 5000 Sep 21 13:42:32 turtle systemd[1]: Started session-c4.scope - Session c4 of User moriwaka. Sep 21 13:42:32 turtle sudo[4110437]: pam_unix(sudo:session): session opened for user moriwaka(uid=1000) by (uid=0)
ããã§pstreeãå®è¡ããã¨ãããã»ã¹ã®è¦ªåé¢ä¿ã¨ãã¦ã¯systemdâhoge.shâsudoâsleep ã®ããã«æå³ã©ããã§ãããã¨ããããã¾ãã
# pstree systemdââ¬âModemManagerâââ3*[{ModemManager}] ââNetworkManagerâââ2*[{NetworkManager}] (ä¸ç¥) ââhoge.shâââsudoâââsleep (以ä¸ç¥)
å¥ã®cgroupã ã¨ä½ãã¾ããã®ã?
èµ·åã«æåãã¦ããããcgroupãå¥ã§ãããã¨ã¯ãããã¾ããã ããã§åé¡ãçºçããã®ã¯ã©ããªã¨ãã§ãããã?
- systemdã®hoge.serviceãããã»ã¹ã®ç£è¦ãã§ãã¦ããªããsystemdããµã¼ãã¹ã®ååãããµã¼ãã¹æ¬ä½ã®ããã»ã¹ãçºè¦ã§ããªããªãã¾ãããã®ãã systemctl kill ãã(ãã®ä¾ã«ã¯ããã¾ããã)Restartãã£ã¬ã¯ãã£ããªã©ã使ããã¨ããã¨ãã¾ãããã¾ãããdaemonizeããããã°ã©ã ã ã¨ãbashãåæ¢ãã¦ãåä½ãã¤ã¥ãã¾ããã管çãã§ããªããªãã¾ãã
- ã¦ã¼ã¶ã»ãã·ã§ã³(session-c4.scope)ã¨ãµã¼ãã¹(hoge.service)ã®éã«åå¾é¢ä¿ãªã©ã¯å®ç¾©ããã¾ãããã·ã¹ãã å ¨ä½ã®ã·ã£ãããã¦ã³æã«ã¯(ãã®ä¾ã§ã¯å®ç¾©ãã¦ãã¾ããã)ãµã¼ãã¹çµäºã³ãã³ããç¨æãã¦ãããã使ããã«ããã©ã«ãã®çµäºæ¹æ³(SIGTERMéä¿¡ã®ãã¨ã¿ã¤ã ã¢ã¦ãã¾ã¡ãã¦ããSIGKILLéä¿¡)ã§çµäºãããå ´åãããã¾ãã
- service unitã§ãªã½ã¼ã¹è¨å®ã権é管çãè¡ã£ã¦ãã¦ãããã®å¶éããã¾ãåæ ãããªãå ´åãããã¾ãã
- é¢é£ãããã°ãã¦ã¼ã¶ã»ãã·ã§ã³ã«æå±ããã®ã§journalããµã¼ãã¹ã§æ¤ç´¢ããã¨è¦éãå ´åãããã¾ãã
対çã¯setprivã³ãã³ã
sudoã¯ã»ãã·ã§ã³ãä½ãã®ã§ã¾ãããã¨ããããã¾ããã
ãã®ãããªã·ã§ã«ã¹ã¯ãªããã§sudoãå©ç¨ãããã®ã¯å ¸åçã«ã¯rootãããµã¼ãã¹ç¨ã¦ã¼ã¶ã¸ã®åãæ¿ãã®ããã§ãããã UID, GIDã®åãæ¿ãã ããè¡ãã»ãã·ã§ã³ã«ã¤ãã¦ã¯ä½ãããªã(PAMãã¤ãããªã)ããã¿ãããã¨é½åãããã§ãã ã¾ãã«ãã®ãããªã³ãã³ããsetprivã³ãã³ãã§ããsetprivã³ãã³ãã¯util-linuxããã±ã¼ã¸ã«å«ã¾ãã¦ãã¾ãã
ãã£ãã /opt/hoge/hoge.sh
ã®sudo ãsetprivã«ç½®ãæãã¾ã
/opt/hoge/hoge.sh
#!/bin/bash setpriv --reuid=1000 --regid=1000 --init-groups sleep 5000
ä»åãã¦ããsleepãæ¢ãã¾ãã(ãã®ä¾ã ã¨daemonizeãã¦ããªãã®ã§ãsystemctl stopã§bashã¸ã·ã°ãã«ãéããã¨ã§sleepãçµäºã§ãã¾ãã)
# systemctl stop hoge.service
å度 hoge.service ãèµ·åãã¦ã¿ã¾ããä»åº¦ã¯ cgroup /system.slice/hoge.service
ã®ä¸ã§sleepãå®è¡ããã¦ãããã¨ããããã¾ãã
# systemctl start --no-block hoge.service # systemctl status hoge.service â hoge.service - hoge Loaded: loaded (/etc/systemd/system/hoge.service; static) Active: activating (start) since Wed 2022-09-21 14:38:47 JST; 9s ago Main PID: 4115616 (hoge.sh) Tasks: 2 (limit: 37738) Memory: 540.0K CPU: 5ms CGroup: /system.slice/hoge.service ââ 4115616 /bin/bash /opt/hoge/hoge.sh ââ 4115617 sleep 5000 Sep 21 14:38:47 turtle systemd[1]: Starting hoge.service - hoge...
æå¾ ã©ããUID, GIDãå¤æ´ããã¦ãããã¨ã確èªãã¾ãã
# cat /proc/4115617/status |grep [UG]id Uid: 1000 1000 1000 1000 Gid: 1000 1000 1000 1000
ãã®ããã«setprivãå©ç¨ãã¦UID, GIDãå¤æ´ããã¨ã»ãã·ã§ã³ãä½æããªãã®ã§ãsystemdã«ãããµã¼ãã¹ç®¡çã®ä»çµã¿ã¨ç«¶åãã¾ããã