Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support chroot as non-root on FreeBSD 14+ #41

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ __pycache__/
*.gcov
devel/a.out
*.gcno
devel/test.out.log
devel/test.out.stdout
devel/test.out.stderr
devel/test_make_safe_uri
devel/test_password_equal
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Features:
* Can serve 301 redirects based on Host header.
* Uses sendfile() on FreeBSD, Solaris and Linux.
* Can use acceptfilter on FreeBSD.
* Can use chroot as non-root on FreeBSD 14+.
* At some point worked on FreeBSD, Linux, OpenBSD, Solaris.
* ISC license.
* suckless.org says [darkhttpd sucks less](http://suckless.org/rocks/).
Expand Down
20 changes: 20 additions & 0 deletions darkhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ static const int debug = 1;
#include <time.h>
#include <unistd.h>

/* This is for non-root chroot support on FreeBSD 14.0+ */
/* Must set sysctl security.bsd.unprivileged_chroot=1 to allow this. */
#ifdef __FreeBSD__
# if __FreeBSD_version >= 1400000
# define HAVE_NON_ROOT_CHROOT
# endif
#endif

#ifdef HAVE_NON_ROOT_CHROOT
#include <sys/procctl.h>
#endif

#if defined(__has_feature)
# if __has_feature(memory_sanitizer)
# include <sanitizer/msan_interface.h>
Expand Down Expand Up @@ -2882,6 +2894,14 @@ int main(int argc, char **argv) {

/* security */
if (want_chroot) {
#ifdef HAVE_NON_ROOT_CHROOT
/* We run this even as root, which should never be a bad thing. */
int arg = PROC_NO_NEW_PRIVS_ENABLE;
int error = procctl(P_PID, (int)getpid(), PROC_NO_NEW_PRIVS_CTL, &arg);
if (error != 0)
err(1, "procctl");
#endif

tzset(); /* read /etc/localtime before we chroot */
if (chdir(wwwroot) == -1)
err(1, "chdir(%s)", wwwroot);
Expand Down
Loading