ä»åã¯ãWebãµã¼ãã®å®è£ ã«ä¾åãããã¨ãªããOSã¬ã¤ã¤ã§Webãµã¼ãã½ããã¦ã§ã¢ãèµ·åæã«å®è¡ããã§ãããã·ã¹ãã ã³ã¼ã«ãç£è¦ãã¦ããã®ã¿ã¤ãã³ã°ã§ããã»ã¹ãã¤ã¡ã¼ã¸åããæ¹æ³ï¼PoCï¼ã«ã¤ãã¦ç´¹ä»ãã¾ãã
ãã®åã«ãã¾ãã¯åæã®ä¸è´ã¨ãããã¨ã§ãåã¯ä»¥åãããWebãµã¼ãããã»ã¹ã®æ§è³ªã«ã¤ãã¦ãããã¢ã¯ãã£ãæ§ã¨ãªã¢ã¯ãã£ãæ§ã¨ããåé¡ã«ã¤ãã¦è¿°ã¹ã¦ãã¾ããã ããã¢ã¯ãã£ãæ§ã¨ãªã¢ã¯ãã£ãæ§ã«ã¤ãã¦ç°¡åã«ã¾ã¨ããã¨ã以ä¸ã®ããã«ãªãã¾ãã
Webãµã¼ãæ©è½ã®ããã¢ã¯ãã£ãæ§ã¨ãªã¢ã¯ãã£ãæ§
çªçºçãªã¢ã¯ã»ã¹éä¸ã®ãããªå¤åã«èãããã·ã¹ãã ãæ§ç¯ããããã«ã¯ï¼è² è·ã®ç¶æ ã«åºãã¦é©åãªã¤ã³ã¹ã¿ã³ã¹ã®æ°ã決å®ãï¼å¿ è¦ä»¥ä¸ã«ã³ã³ãã¥ã¼ã¿ãªã½ã¼ã¹ã使ç¨ããªãããã«è¨è¨ãããã¨ãéè¦ã§ããï¼ åä¸ã®ãµã¼ãã«é«éç©ã«ãã¹ããå容å¯è½ã§ããï¼ãã¹ãåä½ã§ã®ãªã½ã¼ã¹ç®¡çãé©åã«è¡ããªããï¼ã»ãã¥ãªãã£ã¨æ§è½ããã³è² è·ã«å¼·ãWebãã¹ãã£ã³ã°ç°å¢ãæ§ç¯ãããã¨ãç®çã¨ããå ´åï¼Webãµã¼ãæ©è½ãããã¢ã¯ãã£ãæ§ã¨ãªã¢ã¯ãã£ãæ§ã«åºãã¦åé¡ã§ããï¼ ä»¥ä¸ã«ï¼Webãµã¼ãæ©è½ã®ããã¢ã¯ãã£ãæ§ã¨ãªã¢ã¯ãã£ãæ§ãå®ç¾©ããï¼
ããã¢ã¯ãã£ãæ§ã¨ã¯ï¼Webãµã¼ãæ©è½ãæã¤ä»®æ³ãã·ã³ï¼ã³ã³ããããã³Webãµã¼ãããã»ã¹ãäºãèµ·åãã¦ããï¼ãªã¯ã¨ã¹ãã«å¿ãã¦ä»®æ³ãã·ã³ãã³ã³ããã®ç¶æ ãå³æå¤æ´ã§ããªããï¼å¸¸ã«èµ·åç¶æ ã§ããããï¼é«éã«ãªã¯ã¨ã¹ããå¦çã§ããæ§è³ªã¨ããï¼ ã¾ãï¼å¸¸æWebãµã¼ãæ©è½ã稼åããã¦ããå¿ è¦ãããããï¼ãªã½ã¼ã¹å¹çãæªãï¼ ããã¢ã¯ãã£ãæ§ããã£ããªã¼ãã¹ã±ã¼ã«ã¯äºåã«ã¢ã¯ã»ã¹é »åº¦ããäºæ¸¬ãè¡ãï¼äºæ¸¬ã«åºã¥ããæ°ã ãã¤ã³ã¹ã¿ã³ã¹ãèµ·åããã¦ãããããªã¢ããã¼ãã§ããï¼
ãªã¢ã¯ãã£ãæ§ã¨ã¯ï¼CGIãFastCGIã®ããã«ï¼ã¢ããªã±ã¼ã·ã§ã³ãå®ç¨ä¸ç¾å®çãªé度ã§èµ·åå¯è½ã§ãããã¨ãåæã«ï¼ãªã¯ã¨ã¹ãã«å¿ãã¦ã¢ããªã±ã¼ã·ã§ã³ãèµ·åããæ§è³ªã¨ããï¼ ãªã¢ã¯ãã£ãæ§ãæã¤Webãµã¼ãæ©è½ã¯ï¼èµ·åã¨åæ¢ã®ã³ã¹ãã¯çããããï¼æ§è½é¢ã¯ããã¢ã¯ãã£ãæ§ãæã¤Webãµã¼ãæ©è½ããå£ããï¼ãªã¯ã¨ã¹ããåä¿¡ããªãéãã¯ããã»ã¹ãèµ·åããªãããï¼ãªã½ã¼ã¹å¹çãè¯ãï¼ ã¾ãï¼ãªã¯ã¨ã¹ãã«å¿ãã¦è¤æ°èµ·åãããã¨ãã£ãå¤æ´ã«å¼·ãå¦çãå®è£ ãæãï¼ ä¸ä¾ã¨ãã¦ï¼FastCGIã¯ãªã½ã¼ã¹å¹çã¨æ§è½ã両ç«ããããã«ï¼ä¸å®æéèµ·åãã¦é£ç¶ãããªã¯ã¨ã¹ããé«éã«å¦çå¯è½ã¨ããã¢ã¼ããã¯ãã£ãã¨ã£ã¦ããï¼
ããããªããï¼CGIã®ãããªãªã¢ã¯ãã£ãæ§ã«åºã¥ãå¾æ¥ã®å¦çææ³ã¯æ§è½é¢ã®åé¡ãªã©ããå©ç¨ãããªããªã£ã¦ãã¦ããï¼ãªã¼ãã¹ã±ã¼ã«ã«ã¤ãã¦ãï¼ãªã¯ã¨ã¹ãåä½ã§ä»®æ³ãã·ã³ãã³ã³ãããé½åº¦èµ·åãããã³ã¹ããèæ ®ããã¨ï¼å®ç¨çãªæ§è½ãæºãããã¨ã¯å°é£ã§ããï¼
ref: æ¾æ¬ 亮ä», è¿è¤ å®æºæ, ä¸å® æ ä», åæ¦ å¥æ¬¡, æ æ å¥å¤ªé, FastContainer: Webã¢ããªã±ã¼ã·ã§ã³ã³ã³ããã®ç¶æ ããªã¢ã¯ãã£ãã«æ±ºå®ããã³ã³ãã管çã¢ã¼ããã¯ãã£, ç ç©¶å ±åã¤ã³ã¿ã¼ãããã¨éç¨æè¡ï¼IOTï¼, Vol.2017-IOT-38(14), pp.1-8, Jun 2017.
ä¸è¨ã§ãè¿°ã¹ãéãããªã¢ã¯ãã£ãæ§ããã¤Webãµã¼ãããã»ã¹ã¯ãå¤ãã¯å¾ æ©ã¡ã¢ãªç¯ç´ã®ããã®inetdãxinetdãæè¿ã§ã¯ãµã¼ãã¼ã¬ã¹ã¢ã¼ããã¯ãã£ã¨ãã£ãæ¹é¢ã§åã³æ³¨ç®ãæµ´ã³ã¦ãã¾ãã ãããããªã¢ã¯ãã£ãæ§ã®æ¬è³ªã¯ãªã½ã¼ã¹å¹çåã¨ããããã¯ãããããäºæ¸¬ã§ããªãå¤åã«å¯¾ãã¦ãªã¢ã¯ãã£ãã«ç¶æ ãå¤ããããã¨ããã«ããã¾ãã ãã®ç¹æ§ããçµæçã«å¤çè¦å ã®å¤åã«å¯¾ãã¦ãã·ã¹ãã å é¨ã®å®è³ªçãªãªã½ã¼ã¹ã®å¢æ¸ã ãã§ãªããæ§è½ãäºæ¸¬ã§ããªãå¤åã«ãã¾ããã£ãããããããã®ã§ãã
ä»å¾ããã©ãã·ã¥ã¯ã©ã¦ãã¨ãã£ãäºæ¸¬ã§ããªãçªçºçãªã¢ã¯ã»ã¹ã«å¯¾ãã¦ãããã¢ã¯ãã£ãã«è¦ç©ãã£ããäºæ¸¬çã«äºå対å¿ããããããå¤åã«å¼·ãåºç¤ãç¨æãã¦ãªã¢ã¯ãã£ãã«èªååã§å¯¾å¿ãããã¨ãéè¦ã«ãªãã¨èãã¾ãããã®ããã«ã¯ãä¾ãã°Webãµã¼ãã®æèã§èããæã«ã¯ãããã«Webãµã¼ãããã»ã¹ã®èµ·åãéãããããããã®ãå¤åã«å¼·ããªã¢ã¯ãã£ããªæ§è³ªããå¼·åããããã®ã¢ããã¼ãã«ãªãã§ãããã
ããã§ãCRIUã¨ããããã»ã¹ãã¤ã¡ã¼ã¸åããæè¡ã«ããã¦ãã注ç®ãã¦ããã®ã§ãããWebãµã¼ããèµ·åãã¦ãã¾ã£ã¦ããã¤ã¡ã¼ã¸åããã®ã¯ããããã¯ã¼ã¯ã¬ã¤ã¤ã¼ãèµ·åå¾ãªã©ã®ç¶æ ãæã£ãã¾ã¾ã¤ã¡ã¼ã¸åãããããããã®ãããªã¤ã¡ã¼ã¸ã¯ãã¢ãã¬ã¹ããã¼ãã®è¡çªã ã£ãããèµ·åå¾ã®ã³ã³ãã³ãã®ç¶æ ãæã£ã¦ãã¾ã£ãããOSã®å¯¾å¿ç¶æ³ãèããã¨ãèµ·åã®é«éåã¨ãã観ç¹ã§ã¯å®ç¨ä¸ä½¿ãã«ããã¨èãã¦ãã¾ããã
ä¸æ¹ã§ããããå種Webãµã¼ããã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã®èµ·åå¦çæã«ãsocketãä½ã£ã¦setsockopt()ãªãbind()ãlisten()ããç´åã®ã¿ã¤ãã³ã°ã§ããã»ã¹ãã¤ã¡ã¼ã¸åããã°ãä¸è¦ãªç¶æ ãæããªãã¦è¯ãã¨èãã¾ããããããã ã¨ãå種ãµã¼ãã½ããã¦ã§ã¢ã®åæåå¦çã«æãå ¥ãã¦ãåã ã«ä¾åããæ¡å¼µå®è£ ã追å ããå¿ è¦ããããæ±ç¨æ§ãããã¾ãä½ããªããªã¨æã£ã¦ãã¾ããã
ããããææ¥ã¯æµããè¤åãçºãã¦ããæã«ããã¥ã¤ã¼ã¼ã¼ã¼ã¼ã¼ã¼ã¼ã¼ã¼ã¼ãï¼ã¨ä½ããããã¦ãã¦ãããã ã£ãããåãµã¼ãã½ããã¦ã§ã¢ã®èµ·åå¦çãå¤é¨ããOSã¬ã¤ã¤ã§ç£è¦ãã¦ãsocketã®listenã«è³ãç´åã®å¦çã§å ±éãã¦å®è¡ããããããªã·ã¹ãã ã³ã¼ã«ï¼socket(), setsockopt(), bind(), listen(), accept()ãªã©ãªã©ï¼ãå®è¡ãããç´åã§ãã¤ã¡ã¼ã¸åãã¦ããã°ããããããã¨èãã¾ããã
ã»ã¨ãã©ã®ãµã¼ãã½ããã¦ã§ã¢ã¯ãåºæ¬çã«listenããã¿ã¤ãã³ã°ã§ã¯ã»ã¨ãã©ã®åæåå¦çãçµãã£ã¦ããã¯ãã§ããã®ã¿ã¤ãã³ã°ã§ã¤ã¡ã¼ã¸åã§ããã°ãç¶æ ãã»ã¨ãã©æã£ã¦ããªããããã®ã¤ã¡ã¼ã¸ãã復帰ãã¦listenããé度ã極ãã¦éããªãã ãããã¨ããèãæ¹ã§ããä¾ãã°railsã§é·ãã®åæåå¦çãçµãã£ã¦ãããlistenãã¦ãµã¼ãã¹ã¤ã³ã ï¼ã¨ããç¶æ ã®ç´åã§ã¤ã¡ã¼ã¸åã§ãããã¨ãæ³åããã¨ããã®ã¤ã¡ã¼ã¸ããã®ãªã¹ãã¢ã«ããèµ·åé度ã®å¹çæ§ã¯æ³åã«ããããã§ãããã
ãããã§ããã°ããµã¼ãã½ããã¦ã§ã¢ã®å®è£ ã«ä¾åãããå ¨ã¦OSã¬ã¤ã¤ã§å¤é¨ããããã»ã¹ã®è»éå¦çå®äºç´åã®æ®µéã§ã¤ã¡ã¼ã¸åãããã¨ãã§ãããã§ãã
ã¨ãããã¨ã§ããããå¯è½ããæ¨æ©ã´ãã§ã´ãã§ãã¾ãã¦ãçµè«ãã§ãããã§ãã
ãããå®ç¾ããããã®æ¹æ³ã¨PoCãç´¹ä»ãã¾ãã
seccompã¨ptraceã§ã·ã¹ãã ã³ã¼ã«ãç£è¦ãCRIUã§ã¤ã¡ã¼ã¸åãã
ä¸è¨ã®ãWebãµã¼ãã½ããã¦ã§ã¢ã®èµ·åå¦çå®äºã§ããã¤ãããã¯ã¼ã¯ãListenãã¦ããªãç¶æ ã®ããã»ã¹ãã¤ã¡ã¼ã¸ããããã¨ãç®æãã¾ãããã®ããã«ã¯ãseccompã§ç£è¦ããã·ã¹ãã ã³ã¼ã«ãè¨å®ãããã®ä¸ã§ã¤ã¡ã¼ã¸åããããµã¼ãããã»ã¹ãfork()ãã¦ããexecv()ãã¤ã¤ã親ããã»ã¹ãã対象ã®ãµã¼ãããã»ã¹ã®seccompã¤ãã³ããptrace()ã§ç£è¦ããã¤ãã³ããçºç«ãããªãã¡ãç£è¦ããã·ã¹ãã ã³ã¼ã«ãå®è¡ãããç´åã®æ®µéã§ããã®ããã»ã¹ãCRIUã§ã¤ã¡ã¼ã¸åãã¦ããã¾ãã
é½åã®è¯ããã¨ã«ãsecompã使ãmrbgemã®mruby-seccompãåããã«åº§ã£ã¦ãã @udzura ããããCRIUã§ããã»ã¹ãã¤ã¡ã¼ã¸åããmrbgemãåãæ¢ã«æä½ã£ã¦ããã®ã§ããããå°ãã ãå¼ã£ã¦ã以ä¸ã®ãããªã³ã¼ãã§ã¤ã¡ã¼ã¸åãããã¨ã«æåãã¾ããã
socket = "/var/run/criu_service.socket" images = "/tmp/dump_test" log = "dump.log" c = CRIU.new c.set_images_dir images c.set_service_address socket c.set_log_file log c.set_shell_job true pid = Process.fork do context = Seccomp.new(default: :allow) do |rule| rule.trace(:setsockopt, 0) end context.load exec '/home/ubuntu/DEV/mruby/bin/simpleserver/webserver', '/home/ubuntu/DEV/mruby/bin/simpleserver/server.conf' end ret = Seccomp.start_trace(pid) do |syscall, _pid, ud| name = Seccomp.syscall_to_name(syscall) puts "[#{_pid}]: syscall #{name}(##{syscall}) called. (ud: #{ud}), dump the process image." c.set_pid _pid c.dump puts "the pid of process image is #{_pid} into #{images} dir." end
ãã®ã³ã¼ãã§ãã£ã¦ãããã¨ã¯ãmruby製ã®ã·ã³ãã«ãªWebãµã¼ããexecã§å®è¡ãããã®Webãµã¼ãã®setsockopt()ã·ã¹ãã ã³ã¼ã«ãç£è¦ããsetsockopt()ã·ã¹ãã ã³ã¼ã«ãå®è¡ãããã¿ã¤ãã³ã°ãSeccomp.start_trace
ã§ãã¬ã¼ã¹ãã¾ãã
Seccomp.start_trace
ã®å®è£
èªä½ã¯ptrace()ã§seccompã¤ãã³ããç£è¦ããçºç«ããããããã¯ãå®è¡ãããããªå¦çã«ãªã£ã¦ãã¾ãã
ãã®ä¸ã§ããããã¯ã®ä¸ã§ã¯CRIUã®CAPIãbindingããmruby-criuã«ãã£ã¦ã対象ã®ããã»ã¹ãã¤ã¡ã¼ã¸ããã¾ãã
ãã®Webãµã¼ãã¯ã ãããèµ·åããã¨ã¡ã¢ãª1MBããããªã®ã§ããããããããã®ããã»ã¹ã§ã大ä½dumpã¨restoreã¯ããããæ°msecã§å®äºããã®ã§ãæ®éã«æåããèµ·åããã®ã¨æ¯è¼ãã¦ãããªãéãèµ·åã§ãããã§ãã ãã®ãããã®ã¤ã¡ã¼ã¸åã¨ããããã®ãªã¹ãã¢ã®æéããã¡ã¢ãªãµã¤ãºããã®ä»è¨å®ã«åºãã¦ã©ããããéããåºã¦ãããã¯ãä»å¾æ¤è¨¼äºå®ã§ãã
ãã®ã³ã¼ããåããããã«ããããã«ãmruby-seccompã®start_traceã¡ã½ããã«å°ãæãå ¥ãã¦ãä¸åº¦ã ããããã¯ãå®è¡ããå¦çã«æ¸ãæãã¾ããã ãã®ä¸ã§ãCRIUã®dumpã®å®è¡æã«ã¯ãCRIUèªä½ãptraceã§ä¸åº¦ããã»ã¹ã®å¦çãPTRACE_INTERRUPTã§ãµã¹ãã³ããã¦ããã¤ã¡ã¼ã¸åãããããã«ãCRIUã«ãæãå ¥ãã¦ãæ¢ã«ãµã¹ãã³ãæ¸ã¿ã®å ´åã¯æ¹ãã¦ptraceã§ãµã¹ãã³ããããªãããã«ãã¾ããã
ããã«ãCRIUã§ã¯seccomp mode 2ã«å¯¾å¿ããå¦çãå
¥ã£ã¦ããã®ã§ããããã®å¦çãéãã¨ããªããã¤ã¡ã¼ã¸ããããã»ã¹ãrestoreããå¾ã«ãããã»ã¹ãseccompã®å¦çã§è½ã¡ã¦ãã¾ãã¨ããåé¡ããã£ããããããã«ã¤ãã¦ãã復帰å¾ã¯ãããseccompã¯ãµã¹ãã³ããããã®ã§ãCRIUã®ä¸ã®dumpæã«seccomp mode 2ãDISABLEã«ãã¦ããä¿åããããã«æ¸ãæãã¾ããã
PTRACE_O_SUSPEND_SECCOMP
ã§ã¯CRIUå´ã§ãªããseccompãdisableã§ããªãã£ãã®ã§ãseccomp mode 2ã®å ´åã«ãããã¤ã¡ã¼ã¸ã«åæ ããå¦çãé¤å¤ãã¾ããã
ãã®ãããªèª¿æ´ã§ããã¾ãä¸è¨ã®ã³ã¼ãã§å®è¡ãããµã¼ãã½ããã¦ã§ã¢ã®å®è£ ã«ããããWebãµã¼ãã®ç¹æ§ä¸å¿ ãå®è¡ããããããªã·ã¹ãã ã³ã¼ã«ãOSã¬ã¤ã¤ã§ç£è¦ãã¦ããã®ã¿ã¤ãã³ã°ã§ããã»ã¹ãã¤ã¡ã¼ã¸ããããã¨ãã§ãã¾ããã
ãã§ãããã§ããã
ã¾ã¨ã
ä»æ¥ã¯Webãµã¼ããèµ·åæã«å ±éãã¦å®è¡ãããããªã·ã¹ãã ã³ã¼ã«ãseccompã¨ptraceã§ç£è¦ãã¦ãã·ã¹ãã ã³ã¼ã«å®è¡åã«criuã§ã¤ã¡ã¼ã¸åããPoCãæ¸ããããã®ããã«criuããããªãã«æ¸ãæãããã¨ã«ãªã£ããã©ãããã§Webãµã¼ãã®ç¨®é¡ã«ãããèµ·åå®äºç´åã®ããã»ã¹ãæ±ç¨çã«ã¤ã¡ã¼ã¸åã§ããã
— æ¾æ¬ äº®ä» / ã¾ã¤ãã¨ãã¼ (@matsumotory) 2018å¹´4æ23æ¥
criuã§ãããã¯ã¼ã¯ãTCPã»ãã·ã§ã³ãå«ãã¦ã¤ã¡ã¼ã¸åããã®ã¯ç¶æ ãæã¡éãã§æ±ãã«ãããä¸æ¹ãèµ·åæã®socket()å®è¡æã®ããã»ã¹ç¶æ ãã¤ã¡ã¼ã¸åããããã«ãè¤æ°ããWebãµã¼ããappãµã¼ãã®æ¡å¼µãæ¸ãã®ã¯å¤§å¤ãããã§ãããã»ã¹ãä»»æã®ã·ã¹ãã ã³ã¼ã«ãããã¯ããæã«criuããã°è¯ãã¨èãã
— æ¾æ¬ äº®ä» / ã¾ã¤ãã¨ãã¼ (@matsumotory) 2018å¹´4æ23æ¥
ã¨ãããã¨ã§ããµã¼ãã½ããã¦ã§ã¢ã®èµ·åããããã»ã©ãããã¯ã¼ã¯ã®ç¶æ ãæã¤ãã¨ãªãããã¨ãã£ã¦èµ·åå¦çã«ããã¦æéããããåå¦çãªã©ã¯çµãã£ã¦listenããç´åã®ããã»ã¹ã®ç¶æ ããå種ãµã¼ãã½ããã¦ã§ã¢ã®å®è£ ã«æãå ¥ããããããã¨ãªãå®ç¾ããããã®ã¤ã¡ã¼ã¸åææ³ãå®ç¾ã§ãã¾ããã
ãã®æ¹æ³ã使ãã°ãä»åã®ã±ã¼ã¹ã ãã§ãªããå é¨å®è£ ã«ãããã«OSã¬ã¤ã¤ããä»»æã®ã·ã¹ãã ã³ã¼ã«ãç£è¦ãã¦ãç¹å®ã®ã¿ã¤ãã³ã°ã§ããã»ã¹ã®ã¤ã¡ã¼ã¸åãå¯è½ã«ãªãã¾ãã
å¼ãç¶ããCRIUã®seccomp modeã®ä¸æãªåé¡ãããããå®ãããã¯ãã«å°å ¥ã§ããã¬ãã«ã¾ã§ãã©ãã·ã¥ã¢ãããã¦ããããã¨æãã¾ãã