Skip to content

Commit

Permalink
Check for chroot() and setgroups()
Browse files Browse the repository at this point in the history
  • Loading branch information
roehling committed Aug 6, 2023
1 parent 6a0d194 commit bb9073e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ check_include_file(sys/wait.h HAVE_SYS_WAIT_H)
check_include_file(syslog.h HAVE_SYSLOG_H)
check_include_file(time.h HAVE_TIME_H)
check_include_file(unistd.h HAVE_UNISTD_H)
check_symbol_exists(chroot unistd.h HAVE_CHROOT)
check_symbol_exists(close_range unistd.h HAVE_CLOSE_RANGE)
check_symbol_exists(setgroups grp.h HAVE_SETGROUPS)
check_symbol_exists(strcasecmp strings.h HAVE_STRCASECMP)
check_symbol_exists(_stricmp string.h HAVE__STRICMP)
check_symbol_exists(strncasecmp strings.h HAVE_STRNCASECMP)
Expand Down
7 changes: 6 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static bool drop_privileges(cfg_t* cfg)
}
if (chroot_dir && *chroot_dir)
{
#ifdef HAVE_CHROOT
if (chdir(chroot_dir) < 0)
{
log_perror(errno,
Expand All @@ -97,10 +98,14 @@ static bool drop_privileges(cfg_t* cfg)
log_perror(errno, "cannot drop privileges: chroot");
return false;
}
#else
log_error("chroot is not supported on this system");
return false;
#endif
}
if (target_uid != 0 || target_gid != 0)
{
#ifdef HAVE_GRP_H
#ifdef HAVE_SETGROUPS
if (setgroups(0, NULL) < 0)
{
log_perror(errno, "cannot drop privileges: setgroups");
Expand Down
2 changes: 2 additions & 0 deletions src/postsrsd_build_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
#cmakedefine WITH_SQLITE 1

#cmakedefine HAVE_BIG_ENDIAN 1
#cmakedefine HAVE_CHROOT 1
#cmakedefine HAVE_CLOSE_RANGE 1
#cmakedefine HAVE_CLOSE_RANGE_GNU 1
#cmakedefine HAVE_SETGROUPS 1
#cmakedefine HAVE_STRCASECMP 1
#cmakedefine HAVE__STRICMP 1
#cmakedefine HAVE_STRNCASECMP 1
Expand Down

0 comments on commit bb9073e

Please sign in to comment.