Skip to content

Commit

Permalink
Rework pledge support after seccomp support has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
bigio authored and dormando committed Nov 4, 2017
1 parent 7f4e024 commit 105064c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ if BUILD_LINUX_PRIVS
memcached_SOURCES += linux_priv.c
endif

if BUILD_OPENBSD_PRIVS
memcached_SOURCES += openbsd_priv.c
endif

if ENABLE_SASL
memcached_SOURCES += sasl_defs.c
endif
Expand Down
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,17 @@ AS_IF([test "x$enable_seccomp" = "xyes" ], [
], [])
])

AC_CHECK_FUNCS(pledge, [
AC_CHECK_HEADER(unistd.h, [
AC_DEFINE([HAVE_DROP_PRIVILEGES], 1,
[Define this if you have an implementation of drop_privileges()])
build_openbsd_privs=yes
], [])
],[])

AM_CONDITIONAL([BUILD_SOLARIS_PRIVS],[test "$build_solaris_privs" = "yes"])
AM_CONDITIONAL([BUILD_LINUX_PRIVS],[test "$build_linux_privs" = "yes"])
AM_CONDITIONAL([BUILD_OPENBSD_PRIVS],[test "$build_openbsd_privs" = "yes"])

AC_CHECK_HEADER(umem.h, [
AC_DEFINE([HAVE_UMEM_H], 1,
Expand Down
28 changes: 28 additions & 0 deletions openbsd_priv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "memcached.h"

/*
* this section of code will drop all (OpenBSD) privileges including
* those normally granted to all userland process (basic privileges). The
* effect of this is that after running this code, the process will not able
* to fork(), exec(), etc. See pledge(2) for more information.
*/
void drop_privileges() {
extern char *__progname;

if (settings.socketpath != NULL) {
if (pledge("stdio unix", NULL) == -1) {
fprintf(stderr, "%s: pledge: %s\n", __progname, strerror(errno));
exit(EXIT_FAILURE);
}
} else {
if (pledge("stdio inet", NULL) == -1) {
fprintf(stderr, "%s: pledge: %s\n", __progname, strerror(errno));
exit(EXIT_FAILURE);
}
}
}

0 comments on commit 105064c

Please sign in to comment.