mod_process_security icon indicating copy to clipboard operation
mod_process_security copied to clipboard

chroot機能の追加

Open matsumotory opened this issue 12 years ago • 2 comments

スレッドで権限分離をする際に、chroot機能をオプションで使えるように実装したい。Capabilityにuid、gid、groups等の権限変更に特権に加えて、chrootの特権を追加する必要もある。

matsumotory avatar Jan 17 '14 03:01 matsumotory

ref: http://blog.konbu.link/2017/01/18/chroot-in-thread/

動きとしては、子プロセスにCAP_CHROOTを持たせて、スレッド作ってchrootしてからCAP_CHROOTをdropし、コンテンツを実行したらスレッドを破棄した上で、子プロセスが再度元の場所にchrootするようにすれば良い。

matsumotory avatar Jan 18 '17 01:01 matsumotory

POC

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>

void run_php_dso_program(char *path) {
  DIR *dir;
  struct dirent *dp;

  dir=opendir(path);
  for(dp=readdir(dir); dp!=NULL; dp=readdir(dir)){
    printf("%s\n", dp->d_name);
  }
  closedir(dir);
}

int main()
{
  int x;
  const char *dir = "/Users/matsumotory/DEV/haconiwa-image-php-tester";

  // create mod_process_security thread
  // chroot into vhost docmentroot
  chdir(dir);
  chroot(dir);

  // drop cap_chroot from mod_process_security thread
  // run php program on mod_process_security thread
  run_php_dso_program("/");
  // finish php program on mod_process_security thread
  // dstroy mod_process_security thread

  // child process go back to real host root
  chroot("/tmp");
  for (x = 0; x < 1024; x++) {
    chdir("..");
  }
  chroot(".");

  // real host root
  run_php_dso_program("/");

  return 0;
}

matsumotory avatar Apr 14 '17 06:04 matsumotory