Please Implement "--chroot /to/dir" in hhvm
As in php-fpm (config file command: chroot), allow chrooting the hhvm-server to a subdirectory, too.
php-fpm.conf.default says: "Note: chrooting is a great security feature and should be used whenever possible."
Thanks for reporting this. Feel free to submit a PR if you (or someone else reading this) gets to this before we do.
chroot is not a security feature. Please see https://securityblog.redhat.com/2013/03/27/is-chroot-a-security-feature/ for more information.
The security aspect is totally valid and postulating it as a security mechanism is a farce. However, using chroot as a "performance mechanism" can make sense in some cases to squeeze out the last percent of the system.
What do I mean by that. If your disk is kinda slow, you can save a lot of stat() calls when you encounter a lot of includes and very long paths. I discovered this when I implemented some new features on PHP ( http://www.xarg.org/2011/06/php-hacking/ ). APC in the PHP world only helped when stat calls were completely turned off.
Closing as WONTFIX. Slow disk is covered by inotify to reduce useless stats calls as well as repo mode.
I prefere to use the php-fpm chroot feature because it hardens the web installation (that is also stated by the page that LiraNuna linked). A php-fpm chroot installation can be so minimal that it offers only very minimal libraries, no headers and (if you want) no command line tools for the attackers to work with. It also does not need /proc and /dev, which means that it hides critical systeminformations like running processes, config files and logged in users. That IS a feature, as this prevents some/many local root exploits found in the wild to work, and without root they cant break out. It may be true that a genious hacker still can work around all that, but all attacks i had so far were script kids with public exploits, and for them chroot does really the trick!
LiraNuna could you please reopen this issue? This feature is needed as it is crucial to virtual hosting environments. Security aspect of chroot is perfectly valid in virtual environments, especially with grsecurity extensions, saying its not is completely insane.
cc @paulbiss and @fredemmott
Why does this need to be part of HHVM? start-stop-daemon and upstart both support providing a chroot path, and you can just run chroot directly too.
Agree with @simonwelsh, I'm unclear on why this needs to be part of the hhvm binary.
I think chrooting via chroot or start-stop-daemon is more difficult as then you have to copy all needed files (lib, dev, etc/resolv.conf) in the to-be-chrooted subdirectory which can be a bit annoying and another security risk when you later forget to update the libraries in the chroot-subdir.
So a chroot by hhvm itself after start-up can be a convenient option.
I'm missing php-fpm chroot like feature, where I could isolate php environments in virtual hosting in very efficient way - each php thread could run as a different user, have its own resource and security policy, it works really well, I could create small chrooted enviroments which were consistent with shell and php. If it could be achieved without integrating chroot feature into binary, i'd like to see such implementation with nginx.
@lutel I'm a little confused, you can't chroot a thread, or run two threads from the same process as different users.
@Thomas12 makes sense. I'm still not totally sold on this feature, but I'd be willing to look at a PR if there's enough interest in adding it. We do have support for setuid so I suppose it's not without precedent.
@paulbiss in php-fpm each thread has it's own chrooted directory (eg. per one customer), an it is own by customer user. In php-fpm you can define separate pools, and chroot each of them.
@lutel: php-fpm uses multiple processes, not multiple threads. Multiple threads can not be chrooted differently. There can only be one chroot per process, which dramatically limits the value of this for HHVM.
@fredemmott indeed, its per process. Why is this limitation for HHVM? In terms of resource usage these are almost the same...
xxxx.pl 13285 0.0 0.4 653604 80252 ? S 06:17 0:01 php-fpm: pool xxxx.pl hhvm 15013 0.0 0.5 608292 88728 ? Ssl Jan11 0:00 /usr/bin/hhvm -m server -c /etc/hhvm/php.ini -c /etc/hhvm/server.ini -u hhvm -vPidFile=/var/run/hhvm/hhvm.pid -vLog.File=/var/log/hhvm/hhvm_error.log -vRepo.Central.Path=/var/lib/hhvm/hhvm.hhbc -vServer.Type=fastcgi -vServer.IP=127.0.0.1 -vServer.Port=9000
@lutel: hhvm is a jit, and many of the performance benefits come from compiling machine code translations of php and caching them in a translation cache. As each request needs access to the translated machine code and associated data structures we run requests in threads rather than separate processes.
@paulbiss yes, but wouldn't be possible to create mechanism to chroot different hhvm processes (not threads), like we have in php-fpm? This feature of php-fpm that you can define multiple pools and chroot each of them is very convenient in virtual hosting. I'd be interested to see if it is already possible to chroot hhvm like @simonwelsh mentioned.
The problem is that there aren't multiple processes handling requests within hhvm, it's all done in a single multithreaded process. Short of substantially re-architecting both the server and the runtime I don't think a multiprocess solution is going to be possible.
If you want to serve requests from different source trees in separate chroot jails you can always just spin up multiple servers and point them each at the source tree you want them to serve. This is something that should be done in a wrapper script, not implemented in hhvm.
It is possible to run hhvm in a chroot, but as @Thomas12 mentioned you'll need to have the jail setup to contain all of the resources that hhvm will need to start, libraries, shells, etc.
@paulbiss with php-fpm implementation, jailing is also required, but it is only what php requires in terms of libs/shells etc - which is perfectly fine. Looking at both php-fpm and hhvm in terms of chroot, i don't see they are that fart apart - it is only that php-fpm supports in its config chrooting, and does chrooting in according to whats configured in pools. Still the process would be isolated to one directory, it doesn't seem like big architecture change... As you said, it could be achieved with wrapper script, i'll try to work on it.
@lutel: supporting a chroot jail would be quite doable, in either a wrapper or within hhvm itself. Allowing hhvm to run requests in different processes and chroot them each individually would be a much larger undertaking, that was my point.