Passenger åãã« AppArmor ãè¨å®ãã
2013/01/06
2013å¹´æåã®ãRailséæãã®ãã¼ãã¯ãAppArmorã§ããUbuntu Serverä¸ã§Passengerãå©ç¨ãã¦Railsã¢ããªã±ã¼ã·ã§ã³ãéç¨ããå ´åã«AppArmorãã©ãè¨å®ãã¹ãããèãã¦ã¿ããã¨æãã¾ãã
æ¬ç¨¿ã対象ã¨ããUbuntu Serverã®ãã¼ã¸ã§ã³ã¯12.04 LTSã§ãããã§ã«Apacheã¨Passengerã使ã£ã¦Railsã¢ããªã±ã¼ã·ã§ã³ãéç¨ãã¦ãããã¨ããåæã§ã話ããã¾ããRubyã®ãã¼ã¸ã§ã³ã¯1.9.3ã§ã/usr/local/bin
ã«ã¤ã³ã¹ãã¼ã«ããã¦ãã¾ãã
æ¬ç¨¿ã®å 容ãå®éã«è©¦ãã¦ã¿ããæ¹ã¯ãRailsæ¬çªç°å¢æ§ç¯ã¬ã¤ããåèã«ãã¦ç°å¢ãæ´ãã¦ãã ããã
AppArmorã®åºç¤ç¥è
AppArmorã¯ãLinuxã«ã¼ãã«ã®ã»ãã¥ãªãã£ã¢ã¸ã¥ã¼ã«ã®ä¸ç¨®ã§ããåæ§ã®ã¢ã¸ã¥ã¼ã«ã¨ãã¦ã¯SELinuxããããããããã¯ãã¡ãã®æ¹ãæåã§ãããããã®ã»ãã¥ãªãã£ã¢ã¸ã¥ã¼ã«ã¯Linux Security Modules(LSM)ã¨å¼ã°ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãéãã¦ãLinuxã«ã¼ãã«ã«çµã¿è¾¼ã¾ãã¾ããåºæ¬çã«ã¯ãã»ãã¥ãªãã£ã¢ã¸ã¥ã¼ã«ã¯1åããLinuxã«ã¼ãã«ã«çµã¿è¾¼ãã¾ããã®ã§ãLinuxã®ãã£ã¹ããªãã¥ã¼ã·ã§ã³ãã¨ã«ããã©ã«ãã®ã»ãã¥ãªãã£ã¢ã¸ã¥ã¼ã«ãç°ãªãã¾ããCentOSã¯SELinuxããUbuntuã¯AppArmorãæ¡ç¨ãã¦ãã¾ãã
AppArmorã¯ãSELinuxã¨åæ§ã«å¼·å¶ã¢ã¯ã»ã¹å¶å¾¡(MAC)ã¨å¼ã°ããä»çµã¿ãLinuxã«ã¼ãã«ã«ããããã¾ããææè ã¨ã°ã«ã¼ãã®æ¦å¿µã«åºç¤ãç½®ãä¼çµ±çãªLinuxã®ã¢ã¯ã»ã¹å¶å¾¡ï¼ä»»æã¢ã¯ã»ã¹å¶å¾¡ï¼ã¨ã®éããã¾ã¨ããã¨æ¬¡ã®ããã«ãªãã¾ãï¼
- 誰ãä½ã«å¯¾ãã¦ã©ããªå¦çãè¡ããã®ããç´°ããå¶å¾¡ã§ããã
- ã»ãã¥ãªãã£ããªã·ã¼ã¯ç®¡çè ã«ãã£ã¦ä¸å çã«ç®¡çãããã
- 管çè 以å¤ã®ã¦ã¼ã¶ã¼ã¯ãèªåãææãããã¡ã¤ã«ã§ãã£ã¦ãã»ãã¥ãªãã£ããªã·ã¼ã®ç¯å²ãè¶ ãã¦ã¢ã¯ã»ã¹å¶éãç·©åã§ããªãã
Webãµã¤ããéå¶ããè¦ç¹ã§ã¯ãApacheãMySQLãªã©ã®ãµã¼ãã¹ã«å¯¾ãã¦ã¢ã¯ã»ã¹å¶éãããããããã¨ãéè¦ã§ããä»»æã¢ã¯ã»ã¹å¶å¾¡ã®ãã¨ã§ã¯ãæ»æè ããããã®ãµã¼ãã¹ã®ä¹ã£åãã«æåããã¨ãäºå®ä¸ä½ã§ãããããæ¾é¡ã ã£ãã®ã§ãããå¼·å¶ã¢ã¯ã»ã¹å¶å¾¡ãé©ç¨ããã¦ããã°ãæ»æè ã¯ã»ãã¥ãªãã£ããªã·ã¼ã®ç¯å²å ã§ããåããªããã¨ãããã¨ã«ãªãã¾ããã¤ã¾ããå¼·å¶ã¢ã¯ã»ã¹å¶å¾¡ãå©ç¨ããã¨ããµã¼ãã¹ãéããããããã¨ãã§ããã®ã§ãã
AppArmorã§ã¯ããµã¼ãã¹ãã¨ã«ãããã¡ã¤ã«ã¨å¼ã°ããè¨å®ãã¡ã¤ã«ãä½ãããããAppArmorã«ãã¼ããããã¨ã§å¯¾è±¡ã¨ãªããµã¼ãã¹ãAppArmorã®ç®¡çä¸ã«å ¥ãã¾ãããã®éããµã¼ãã¹ãã¨ã«å¼·å¶ã¢ã¼ã(Enforce mode)ã¨å¦ç¿ã¢ã¼ã(Complain mode)ã®2ã¤ã®ã¢ã¼ãã®ãããããé¸æã§ãã¾ãããµã¼ãã¹ãã»ãã¥ãªãã£ããªã·ã¼ã«éåããã¢ã¯ã»ã¹ãè¡ã£ãã¨ãã®çµæãç°ãªãã¾ããåè ã§ã¯åç´ã«æå¦ããã¾ããå¾è ã§ã¯ã¢ã¯ã»ã¹èªä½ã¯å¯è½ã§ãããç£æ»ãã°ã«è¨é²ãæ®ãã¾ãã
AppArmorã«é¢ããæ å ±ã¯ãhttp://wiki.apparmor.net ã詳ããã§ãï¼è±èªï¼ããã®è¨äºã®å·çã«ããã£ã¦ã¯ãç¹ã« Mod apparmor example ãåç §ãã¾ããã
AppArmorã®ç¶æ ã確èªãã
ã§ã¯ãAppArmorã®ç¶æ ã確èªããã¨ããããä½æ¥ãå§ãã¾ãããã
Ubuntu Serverã®ã¤ã³ã¹ãã¼ã«ããããã·ã³ã«SSHã§ãã°ã¤ã³ãã¦ãapparmor
ãµã¼ãã¹ã® status ã表示ãã¾ãã
$ sudo service apparmor status apparmor module is loaded. 6 profiles are loaded. 6 profiles are in enforce mode. /sbin/dhclient /usr/lib/NetworkManager/nm-dhcp-client.action /usr/lib/connman/scripts/dhclient-script /usr/sbin/mysqld /usr/sbin/ntpd /usr/sbin/tcpdump 0 profiles are in complain mode. 3 processes have profiles defined. 3 processes are in enforce mode. /sbin/dhclient (874) /usr/sbin/mysqld (1093) /usr/sbin/ntpd (1363) 0 processes are in complain mode. 0 processes are unconfined but have a profile defined.
注ç®ãã¹ã㯠3 processes are in enforce mode.
ã«ç¶ã3è¡ã§ããããã«åæããã¦ããããã°ã©ã ãå¼·å¶ã¢ã¼ãã§AppArmorã®ç®¡çã«å
¥ã£ã¦ãã¾ãã1åç®ã¯DHCPï¼åçãã¹ãè¨å®ãããã³ã«ï¼ã®ã¯ã©ã¤ã¢ã³ãã2åç®ã¯MySQLãµã¼ããNTPï¼æå»åæãããã³ã«ï¼ãµã¼ãã§ããApacheãå
¥ã£ã¦ãã¾ããããã¤ã¾ããApacheã¯å¼·å¶ã¢ã¯ã»ã¹å¶å¾¡ã«ãã£ã¦éãããããã¦ããªããã¨ããããã§ãã
mod_apparmorã®å°å ¥
Apacheã«å¼·å¶ã¢ã¯ã»ã¹å¶å¾¡ãå°å
¥ããã«ã¯ãApacheã¢ã¸ã¥ã¼ã«ã® mod_apparmor
ãå©ç¨ããã®ã簡便ã§ãã
apt-get
ã§ããã±ã¼ã¸ libapache2-mod-apparmor
ãã¤ã³ã¹ãã¼ã«ãã¾ãããã
$ sudo apt-get install libapache2-mod-apparmor
ç¶ãã¦ãa2enmod
ã³ãã³ã㧠mod_apparmor
ãApacheã«çµã¿è¾¼ã¿ã¾ãã
$ sudo a2enmod apparmor
ããã¨ã/etc/apparmor.d
ãã£ã¬ã¯ããªã«ãApacheç¨ã®ãããã¡ã¤ã« usr.lib.apache2.mpm-prefork.apache2
ãçæããã¾ããåæç¶æ
ã§ã®ä¸èº«ã¯æ¬¡ã®éãã§ãï¼ã³ã¡ã³ãè¡ã¨ä½åãªç©ºè¡ãé¤å¤ãã¦ããã¾ãï¼ã
#include <tunables/global> /usr/lib/apache2/mpm-prefork/apache2 flags=(complain) { #include <abstractions/base> #include <abstractions/nameservice> capability kill, capability net_bind_service, capability setgid, capability setuid, capability sys_tty_config, / rw, /** mrwlkix, ^DEFAULT_URI flags=(complain) { #include <abstractions/base> #include <abstractions/nameservice> / rw, /** mrwlkix, } ^HANDLING_UNTRUSTED_INPUT flags=(complain) { #include <abstractions/nameservice> / rw, /** mrwlkix, } #include <apache2.d> #include <local/usr.lib.apache2.mpm-prefork.apache2> }
Apacheåãã®ãããã¡ã¤ã«ãä¿®æ£
/etc/apparmor.d/usr.lib.apache2.mpm-prefork.apache2
ãã¨ãã£ã¿ã§éããcapability sys_tty_config,
ã®ç´å¾ã«ã«ä»¥ä¸ã®è¨è¿°ã追å ãã¾ãã
capability chown, capability dac_override, capability fowner, capability fsetid, capability sys_ptrace, capability sys_resource,
ãã¼ãã£ã«ãã¹ãã®è¨å®å¤æ´
次ã«ããã¼ãã£ã«ãã¹ãã®è¨å®ãã¡ã¤ã«ãä¿®æ£ãã¾ããç¾å¨ã®å 容ã¯æ¬¡ã®ããã«ãªã£ã¦ãã¾ãã
<VirtualHost *:80> ServerName example.com AADefaultHatName passenger DocumentRoot /home/kuroda/example/current/public <Directory /home/kuroda/example/current/public> AllowOverride all Options -MultiViews </Directory> (çç¥) </VirtualHost>
ããã§ãAllowOverride all
ã®ä¸ã«æ¬¡ã®è¨è¿°ãæ¿å
¥ãã¦ãã ããã
AAHatName passenger
Passengeråãã®ãµããããã¡ã¤ã«ãä½æ
ç¶ãã¦ãPassengeråãã®ãµããããã¡ã¤ã«ãä½æãã¾ãããã£ã¬ã¯ã㪠/etc/apparmor.d/apache2.d
ã®ç´ä¸ã«æ°è¦ãã¡ã¤ã« passenger
ãä½æãã以ä¸ã®å
容ãæ¸ãè¾¼ãã§ãã ããã
^passenger { #include <abstractions/apache2-common> #include <abstractions/base> #include <abstractions/nameservice> /usr/local/bin/ruby rix, /usr/local/lib/ruby/**/*.so m, /usr/local/lib/ruby/** r, /usr/local/lib/ruby/gems/1.9.1/gems/passenger-*/agents/** rix, /var/log/apache2/*.log w, /var/log/apache2/*.log w, /home/kuroda/example/current/** r, /home/kuroda/example/current/public/log/*.log a, }
æå¾ã®2è¡ã¯ãå®éã®ãã£ã¬ã¯ããªæ§æã«å¾ã£ã¦å¤æ´ãã¦ãã ããã
ãããã¡ã¤ã«ã®æå¹å
aa-enforce
ã³ãã³ãã§ãããã¡ã¤ã«ãæå¹ã«ãã¾ãã
$ sudo aa-enforce /etc/apparmor.d/usr.lib.apache2.mpm-prefork.apache2
Apacheãåèµ·åãã¾ãã
$ sudo service apache2 restart
ãã©ã¦ã¶ã§Webãµã¤ãã«ã¢ã¯ã»ã¹ãã¦ãRailsã¢ããªã±ã¼ã·ã§ã³ãæ£å¸¸ã«æ©è½ããã°OKã§ãããã¾ãè¡ããªãå ´åã¯ãã¾ãã¯ãããã¡ã¤ã«ã®æ¸ãééããçã£ã¦ãã ããã誤ããããã°ä¿®æ£ãã次ã®ã³ãã³ãã§ãããã¡ã¤ã«ããªãã¼ããã¦ãã ããã
$ sudo apparmor_parser -r /etc/apparmor.d/usr.lib.apache2.mpm-prefork.apache2
ããããããã¡ã¤ã«ã«ééãããªãã®ã«Railsã¢ããªã±ã¼ã·ã§ã³ãæ£å¸¸ã«åä½ããªãå ´åã¯ãaa-genprof
ã¦ã¼ãã£ãªãã£ãç¨ãã¦ãããã¡ã¤ã«ãä¿®æ£ããã®ã§ãããããã«ã¤ãã¦ã¯ã¾ãã®æ©ä¼ã«ã