Safely chroot the current process and drop privileges
Safely chroot the current process and drop privileges.
Bind a TCP server to a privileged port, chroot and drop privileges to "wwwuser".
var net = require'net';var chroot = require'chroot';var server = netcreateServer;serverlisten80if err throw err;trychroot'/var/empty' 'wwwuser';console.log'changed root to "/var/empty" and user to "wwwuser"';catcherrconsole.error'changing root or user failed' err;processexit1;;
Note: the process must be started as the super user.
$ npm install chroot
Change the root directory of the current process. A normal user must be provided since changing root without dropping privileges makes no sense from a security point of view.
child_process.fork()to accomplish that.
Chrooting a program is not a security solution. It is only one aspect of the much broader principle of least privilege. When done right it can be used as a mitigation to seriously hinder a compromised process in further compromising the system. Keep the following things in mind when setting up and using a chroot:
Copyright (c) 2014, 2015 Tim Kuijsten
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.