use libcap for capability dropping

optional, must be enabled --with-capabilities=libcap
	will be extended to support --with-capabilities=libcap2
This commit is contained in:
Martin Willi
2008-08-29 09:24:14 +00:00
parent 8fa6f2dc66
commit eb3e27059b
7 changed files with 71 additions and 60 deletions
+17 -9
View File
@@ -138,6 +138,14 @@ AC_ARG_WITH(
[AC_SUBST(ipsecgroup, "root")] [AC_SUBST(ipsecgroup, "root")]
) )
dnl Will be extended to --with-capabilities=libcap|libcap2
AC_ARG_WITH(
[capabilities],
AS_HELP_STRING([--with-capabilities=libcap],[capability dropping using libcap. Currenlty only the value "libcap" is supported (default is NO).]),
[capabilities="$withval"],
[capabilities=no]
)
AC_ARG_ENABLE( AC_ARG_ENABLE(
[curl], [curl],
AS_HELP_STRING([--enable-curl],[enable CURL fetcher plugin to fetch files via libcurl (default is NO). Requires libcurl.]), AS_HELP_STRING([--enable-curl],[enable CURL fetcher plugin to fetch files via libcurl (default is NO). Requires libcurl.]),
@@ -628,15 +636,6 @@ AC_HAVE_LIBRARY(dl)
AC_CHECK_FUNCS(backtrace) AC_CHECK_FUNCS(backtrace)
AC_CHECK_FUNCS(dladdr) AC_CHECK_FUNCS(dladdr)
AC_MSG_CHECKING([capset() definition])
AC_TRY_COMPILE(
[#include <linux/capset.h>],
[
void *test = capset;
],
[AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no]); AC_DEFINE_UNQUOTED(NO_CAPSET_DEFINED, 1)]
)
if test x$gmp = xtrue; then if test x$gmp = xtrue; then
AC_HAVE_LIBRARY([gmp],[LIBS="$LIBS"],[AC_MSG_ERROR([GNU Multi Precision library gmp not found])]) AC_HAVE_LIBRARY([gmp],[LIBS="$LIBS"],[AC_MSG_ERROR([GNU Multi Precision library gmp not found])])
AC_MSG_CHECKING([gmp.h version >= 4.1.4]) AC_MSG_CHECKING([gmp.h version >= 4.1.4])
@@ -732,6 +731,11 @@ if test x$eap_gtc = xtrue; then
AC_CHECK_HEADER([security/pam_appl.h],,[AC_MSG_ERROR([PAM header security/pam_appl.h not found!])]) AC_CHECK_HEADER([security/pam_appl.h],,[AC_MSG_ERROR([PAM header security/pam_appl.h not found!])])
fi fi
if test x$capabilities = xlibcap; then
AC_HAVE_LIBRARY([cap],[LIBS="$LIBS"],[AC_MSG_ERROR([libcap library not found])])
AC_CHECK_HEADER([sys/capability.h],,[AC_MSG_ERROR([libcap header sys/capability.h not found!])])
fi
dnl ====================================== dnl ======================================
dnl collect all plugins for libstrongswan dnl collect all plugins for libstrongswan
dnl ====================================== dnl ======================================
@@ -850,6 +854,7 @@ AM_CONDITIONAL(USE_MANAGER, test x$manager = xtrue)
AM_CONDITIONAL(USE_ME, test x$me = xtrue) AM_CONDITIONAL(USE_ME, test x$me = xtrue)
AM_CONDITIONAL(USE_INTEGRITY_TEST, test x$integrity_test = xtrue) AM_CONDITIONAL(USE_INTEGRITY_TEST, test x$integrity_test = xtrue)
AM_CONDITIONAL(USE_SELF_TEST, test x$self_test = xtrue) AM_CONDITIONAL(USE_SELF_TEST, test x$self_test = xtrue)
AM_CONDITIONAL(USE_CAPABILITIES, test x$capabilities = xlibcap)
AM_CONDITIONAL(USE_PLUTO, test x$pluto = xtrue) AM_CONDITIONAL(USE_PLUTO, test x$pluto = xtrue)
AM_CONDITIONAL(USE_CHARON, test x$charon = xtrue) AM_CONDITIONAL(USE_CHARON, test x$charon = xtrue)
AM_CONDITIONAL(USE_TOOLS, test x$tools = xtrue) AM_CONDITIONAL(USE_TOOLS, test x$tools = xtrue)
@@ -864,6 +869,9 @@ dnl ==============================
if test x$me = xtrue; then if test x$me = xtrue; then
AC_DEFINE(ME) AC_DEFINE(ME)
fi fi
if test x$capabilities = xlibcap; then
AC_DEFINE(CAPABILITIES)
fi
dnl ============================== dnl ==============================
dnl build Makefiles dnl build Makefiles
+1 -1
View File
@@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
CFLAGS="-Wall -Wno-format -Wno-pointer-sign -Wno-strict-aliasing -g -O2" ./configure \ CFLAGS="-Wall -Wno-format -Wno-pointer-sign -Wno-strict-aliasing -g -O2" ./configure \
--sysconfdir=/etc --disable-tools --disable-pluto --enable-leak-detective --enable-dumm \ --sysconfdir=/etc --disable-tools --disable-pluto --enable-leak-detective --enable-dumm --with-capabilities=libcap \
$1 $2 $3 $4 $5 $1 $2 $3 $4 $5
+5
View File
@@ -129,6 +129,11 @@ if USE_SELF_TEST
AM_CFLAGS += -DSELF_TEST AM_CFLAGS += -DSELF_TEST
endif endif
if USE_CAPABILITIES
charon_LDADD += -lcap
endif
# build optional plugins # build optional plugins
######################## ########################
+22 -29
View File
@@ -22,7 +22,6 @@
#endif /* HAVE_DLADDR */ #endif /* HAVE_DLADDR */
#include <stdio.h> #include <stdio.h>
#include <linux/capability.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <signal.h> #include <signal.h>
#include <pthread.h> #include <pthread.h>
@@ -38,6 +37,9 @@
#ifdef HAVE_BACKTRACE #ifdef HAVE_BACKTRACE
# include <execinfo.h> # include <execinfo.h>
#endif /* HAVE_BACKTRACE */ #endif /* HAVE_BACKTRACE */
#ifdef CAPABILITIES
#include <sys/capability.h>
#endif /* CAPABILITIES */
#include "daemon.h" #include "daemon.h"
@@ -45,16 +47,6 @@
#include <config/traffic_selector.h> #include <config/traffic_selector.h>
#include <config/proposal.h> #include <config/proposal.h>
/* on some distros, a capset definition is missing */
#ifdef NO_CAPSET_DEFINED
extern int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
#endif /* NO_CAPSET_DEFINED */
/* missing on older kernel headers */
#ifndef CAP_AUDIT_WRITE
#define CAP_AUDIT_WRITE 29
#endif /* CAP_AUDIT_WRITE */
#ifdef INTEGRITY_TEST #ifdef INTEGRITY_TEST
#include <fips/fips.h> #include <fips/fips.h>
#include <fips/fips_signature.h> #include <fips/fips_signature.h>
@@ -81,10 +73,12 @@ struct private_daemon_t {
*/ */
pthread_t main_thread_id; pthread_t main_thread_id;
#ifdef CAPABILITIES
/** /**
* capabilities to keep * capabilities to keep
*/ */
u_int32_t keep; cap_t caps;
#endif /* CAPABILITIES */
}; };
/** /**
@@ -193,6 +187,9 @@ static void destroy(private_daemon_t *this)
} }
/* unload plugins to release threads */ /* unload plugins to release threads */
lib->plugins->unload(lib->plugins); lib->plugins->unload(lib->plugins);
#ifdef CAPABILITIES
cap_free(this->caps);
#endif /* CAPABILITIES */
DESTROY_IF(this->public.ike_sa_manager); DESTROY_IF(this->public.ike_sa_manager);
DESTROY_IF(this->public.kernel_interface); DESTROY_IF(this->public.kernel_interface);
DESTROY_IF(this->public.scheduler); DESTROY_IF(this->public.scheduler);
@@ -254,9 +251,6 @@ static void kill_daemon(private_daemon_t *this, char *reason)
*/ */
static void drop_capabilities(private_daemon_t *this) static void drop_capabilities(private_daemon_t *this)
{ {
struct __user_cap_header_struct hdr;
struct __user_cap_data_struct data;
prctl(PR_SET_KEEPCAPS, 1); prctl(PR_SET_KEEPCAPS, 1);
if (setgid(charon->gid) != 0) if (setgid(charon->gid) != 0)
@@ -268,20 +262,12 @@ static void drop_capabilities(private_daemon_t *this)
kill_daemon(this, "change to unprivileged user failed"); kill_daemon(this, "change to unprivileged user failed");
} }
/* we use the old capset version for now. For systems with version 2 #ifdef CAPABILITIES
* available, we specifiy version 1 excplicitly. */ if (cap_set_proc(this->caps) != 0)
#ifdef _LINUX_CAPABILITY_VERSION_1
hdr.version = _LINUX_CAPABILITY_VERSION_1;
#else
hdr.version = _LINUX_CAPABILITY_VERSION;
#endif
hdr.pid = 0;
data.inheritable = data.effective = data.permitted = this->keep;
if (capset(&hdr, &data))
{ {
kill_daemon(this, "unable to drop daemon capabilities"); kill_daemon(this, "unable to drop daemon capabilities");
} }
#endif /* CAPABILITIES */
} }
/** /**
@@ -289,7 +275,11 @@ static void drop_capabilities(private_daemon_t *this)
*/ */
static void keep_cap(private_daemon_t *this, u_int cap) static void keep_cap(private_daemon_t *this, u_int cap)
{ {
this->keep |= 1 << cap; #ifdef CAPABILITIES
cap_set_flag(this->caps, CAP_EFFECTIVE, 1, &cap, CAP_SET);
cap_set_flag(this->caps, CAP_INHERITABLE, 1, &cap, CAP_SET);
cap_set_flag(this->caps, CAP_PERMITTED, 1, &cap, CAP_SET);
#endif /* CAPABILITIES */
} }
/** /**
@@ -498,11 +488,14 @@ private_daemon_t *daemon_create(void)
this->public.gid = 0; this->public.gid = 0;
this->main_thread_id = pthread_self(); this->main_thread_id = pthread_self();
this->keep = 1<<CAP_NET_ADMIN; #ifdef CAPABILITIES
this->caps = cap_init();
keep_cap(this, CAP_NET_ADMIN);
if (lib->leak_detective) if (lib->leak_detective)
{ {
this->keep = 1<<CAP_SYS_NICE; keep_cap(this, CAP_SYS_NICE);
} }
#endif /* CAPABILITIES */
/* add handler for SEGV and ILL, /* add handler for SEGV and ILL,
* add handler for USR1 (cancellation). * add handler for USR1 (cancellation).
+2 -1
View File
@@ -21,7 +21,8 @@
#include <daemon.h> #include <daemon.h>
#include <linux/capability.h> /* missing in cababilities.h */
#define CAP_AUDIT_WRITE 29
/** /**
* Implementation of plugin_t.destroy * Implementation of plugin_t.destroy
+4
View File
@@ -139,3 +139,7 @@ if USE_SMARTCARD
AM_CFLAGS += -DSMARTCARD AM_CFLAGS += -DSMARTCARD
endif endif
if USE_CAPABILITIES
pluto_LDADD += -lcap
endif
+18 -18
View File
@@ -29,11 +29,14 @@
#include <resolv.h> #include <resolv.h>
#include <arpa/nameser.h> /* missing from <resolv.h> on old systems */ #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
#include <sys/queue.h> #include <sys/queue.h>
#include <linux/capability.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <pwd.h> #include <pwd.h>
#include <grp.h> #include <grp.h>
#ifdef CAPABILITIES
#include <sys/capability.h>
#endif /* CAPABILITIES */
#include <freeswan.h> #include <freeswan.h>
#include <pfkeyv2.h> #include <pfkeyv2.h>
@@ -68,11 +71,6 @@
#include "nat_traversal.h" #include "nat_traversal.h"
#include "virtual.h" #include "virtual.h"
/* on some distros, a capset() definition is missing */
#ifdef NO_CAPSET_DEFINED
extern int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
#endif /* NO_CAPSET_DEFINED */
static void static void
usage(const char *mess) usage(const char *mess)
{ {
@@ -236,8 +234,10 @@ main(int argc, char **argv)
bool force_keepalive = FALSE; bool force_keepalive = FALSE;
char *virtual_private = NULL; char *virtual_private = NULL;
int lockfd; int lockfd;
struct __user_cap_header_struct hdr; #ifdef CAPABILITIES
struct __user_cap_data_struct data; cap_t caps;
int keep[] = { CAP_NET_ADMIN, CAP_NET_BIND_SERVICE };
#endif /* CAPABILITIES */
/* handle arguments */ /* handle arguments */
for (;;) for (;;)
@@ -619,14 +619,6 @@ main(int argc, char **argv)
init_fetch(); init_fetch();
/* drop unneeded capabilities and change UID/GID */ /* drop unneeded capabilities and change UID/GID */
#ifdef _LINUX_CAPABILITY_VERSION_1
hdr.version = _LINUX_CAPABILITY_VERSION_1;
#else
hdr.version = _LINUX_CAPABILITY_VERSION;
#endif
hdr.pid = 0;
data.inheritable = data.effective = data.permitted =
1<<CAP_NET_ADMIN | 1<<CAP_NET_BIND_SERVICE;
prctl(PR_SET_KEEPCAPS, 1); prctl(PR_SET_KEEPCAPS, 1);
@@ -656,11 +648,19 @@ main(int argc, char **argv)
} }
} }
#endif #endif
if (capset(&hdr, &data))
#ifdef CAPABILITIES
caps = cap_init();
cap_set_flag(caps, CAP_EFFECTIVE, 2, keep, CAP_SET);
cap_set_flag(caps, CAP_INHERITABLE, 2, keep, CAP_SET);
cap_set_flag(caps, CAP_PERMITTED, 2, keep, CAP_SET);
if (cap_set_proc(caps) != 0)
{ {
plog("unable to drop root privileges"); plog("unable to drop daemon capabilities");
abort(); abort();
} }
cap_free(caps);
#endif /* CAPABILITIES */
/* loading X.509 CA certificates */ /* loading X.509 CA certificates */
load_authcerts("CA cert", CA_CERT_PATH, AUTH_CA); load_authcerts("CA cert", CA_CERT_PATH, AUTH_CA);