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

RFC: Capabilities #185

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
db3d867
Initial attempt at libcap - untested!
mhaas Mar 15, 2015
6a2e306
Fix: wifidog requires CAP_NET_RAW
mhaas Mar 16, 2015
5f71eb7
Cherry-pick SOCK_PACKET -> SOCK_RAW
mhaas Mar 16, 2015
9326edf
Fix: drop group first, regain privileges
mhaas Mar 16, 2015
d280fb5
Merge branch 'master' of https://github.com/mhaas/wifidog-gateway int…
mhaas Mar 25, 2015
78b537f
Merge branch 'master' of https://github.com/wifidog/wifidog-gateway i…
mhaas Mar 25, 2015
93c94db
Merge branch 'coverity-fixes' into HEAD
mhaas Mar 25, 2015
6f6f526
Move drop_privileges to capabilities.c
mhaas Mar 25, 2015
c0ef73c
Add debug output
mhaas Mar 25, 2015
6c18daf
Error handling in capabilities.c
mhaas Mar 25, 2015
fbf3556
Fix compilation
mhaas Mar 25, 2015
2a54bdf
Merge branch 'master' of https://github.com/wifidog/wifidog-gateway i…
mhaas Apr 3, 2015
c0d284c
Tabs -> spaces
mhaas Apr 3, 2015
bd19dc8
Capabilities: Fix unnecessary calls
mhaas Apr 3, 2015
1d324c5
Add user/group config option
mhaas Apr 3, 2015
2131aae
Fix: CAP_NET_ADMIN is actually required, add back
mhaas Apr 3, 2015
05af333
Add TODO - need to check return values!
mhaas Apr 3, 2015
6302dda
Capabilities: error handling, comment
mhaas Apr 3, 2015
a0a78a8
Refactor capabilities.c and hook up fw_iptables.c
mhaas Apr 3, 2015
8ad25bc
Remove TODO
mhaas Apr 3, 2015
6f6cdac
Add license header, fix formatting
mhaas Apr 3, 2015
be3e019
Fix typo: open -> popen
mhaas Apr 3, 2015
ef379f3
Fix help string for --enable-libcap
mhaas Apr 3, 2015
79e60f1
Fix alignment of configure help
acv Apr 3, 2015
9af8215
OCD realignment
acv Apr 3, 2015
05c04c7
Rename build type cyassl -> full
mhaas Apr 8, 2015
9c26326
Respect $CFLAGS in travis configure wrapper
mhaas Apr 8, 2015
58984c3
Travis: build libcap
mhaas Apr 8, 2015
b750717
Also build libattr as a libcap dependency
mhaas Apr 8, 2015
f507078
Refactor travis_configure_wrapper
mhaas Apr 8, 2015
5dee3cf
Reset CFLAGS, libattr fails due to -Werror
mhaas Apr 8, 2015
3e26b42
Free memory, use thread-safe getpwnam_r
mhaas Apr 8, 2015
3cc6aca
Fix unprivileged group example
mhaas Apr 8, 2015
142d289
Merge branch 'master' of https://github.com/wifidog/wifidog-gateway i…
mhaas Apr 8, 2015
426a1c1
Merge pull request #3 from acv/feature-capabilities-3
mhaas Apr 8, 2015
fcde454
Merge branch 'feature-capabilities-3' of https://github.com/mhaas/wif…
mhaas Apr 8, 2015
b471afb
Re-order declarations
mhaas Apr 8, 2015
e445b38
Fix missing variable in travis configure wrapper
mhaas Apr 8, 2015
87c5572
Fix libcap include paths
mhaas Apr 8, 2015
56c8fd2
Fix libcap install location
mhaas Apr 8, 2015
c78e25c
Fix prefix for libcap
mhaas Apr 8, 2015
d03694d
Merge branch 'master' of https://github.com/wifidog/wifidog-gateway i…
mhaas Apr 8, 2015
3031faa
Free some memory
mhaas Apr 8, 2015
a83394c
Run indent
mhaas Apr 8, 2015
070435a
Fix superfluous newlines in capabilities.h decl
mhaas Apr 8, 2015
42e5341
Fix typo: , -> ;
mhaas Apr 8, 2015
897a695
Remove erroneous comment
mhaas Apr 9, 2015
7a24ddb
Fix typo
mhaas Apr 9, 2015
d64cd9f
Merge branch 'master' into feature-capabilities-3
mhaas Jul 26, 2015
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
15 changes: 15 additions & 0 deletions configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ AC_SUBST(enable_latex_docs)
# Acutally perform the doxygen check
BB_ENABLE_DOXYGEN

# Enable capabilities?
AC_DEFUN([BB_LIBCAP],
[
AC_ARG_ENABLE(libcap, [ --enable-libcap enable privilege dropping with capabilities (no)], [], [enable_libcap=no])
if test "x$enable_libcap" = xyes; then
AC_CHECK_HEADERS(sys/capability.h)
AC_SEARCH_LIBS([cap_get_proc], [cap], [], [
AC_MSG_ERROR([unable to find the cap_get_proc function.])
])
AC_DEFINE(USE_LIBCAP,, "Compile with libcap support")
fi
])
# Actually perform the libcap check
BB_LIBCAP

# Enable cyassl?
AC_DEFUN([BB_CYASSL],
[
Expand Down
3 changes: 3 additions & 0 deletions contrib/load-tester/wifidog-mock.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
User nobody
Group nobody

ExternalInterface eth0
GatewayInterface internal0

Expand Down
6 changes: 4 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ libgateway_a_SOURCES = commandline.c \
safe.c \
httpd_thread.c \
simple_http.c \
pstring.c
pstring.c \
capabilities.c

noinst_HEADERS = commandline.h \
common.h \
Expand All @@ -53,7 +54,8 @@ noinst_HEADERS = commandline.h \
safe.h \
httpd_thread.h \
simple_http.h \
pstring.h
pstring.h \
capabilities.h

wdctl_LDADD = libgateway.a

Expand Down
239 changes: 239 additions & 0 deletions src/capabilities.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/* vim: set et sw=4 ts=4 sts=4 : */
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA [email protected] *
* *
\********************************************************************/

/** @file capabilities.c
@author Copyright (C) 2015 Michael Haas <[email protected]>
*/

#include "../config.h"

#ifdef USE_LIBCAP

#include <errno.h>

#include <syslog.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <unistd.h>
/* FILE and popen */
#include <stdio.h>
/* For strerror */
#include <string.h>
/* For exit */
#include <stdlib.h>
/* For getpwnam */
#include <pwd.h>
/* For getgrnam */
#include <grp.h>

#include "capabilities.h"
#include "debug.h"

/**
* Switches to non-privileged user and drops unneeded capabilities.
*
* Wifidog does not need to run as root. The only capabilities required
* are:
* - CAP_NET_RAW: get IP addresses from sockets, ICMP ping
* - CAP_NET_ADMIN: modify firewall rules
*
* This function drops all other capabilities. As only the effective
* user id is set, it is theoretically possible for an attacker to
* regain root privileges.
* Any processes started with execve will
* have UID0. This is a convenient side effect to allow for proper
* operation of iptables.
*
* Any error is considered fatal and exit() is called.
*
* @param user Non-privileged user
* @param group Non-privileged group
*/
void
drop_privileges(const char *user, const char *group)
{
int ret = 0;
debug(LOG_DEBUG, "Entered drop_privileges");

/*
* We are about to drop our effective UID to a non-privileged user.
* This clears the EFFECTIVE capabilities set, so we later re-enable
* re-enable these. We can do that because they are not cleared from
* the PERMITTED set.
* Note: if we used setuid() instead of seteuid(), we would have lost the
* PERMITTED set as well. In this case, we would need to call prctl
* with PR_SET_KEEPCAPS.
*/
set_user_group(user, group);
/* The capabilities we want.
* CAP_NET_RAW is used for our socket handling.
* CAP_NET_ADMIN is not used directly by iptables which
* is called by Wifidog
*/
const int num_caps = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declaring variables all over is a bit of bad form in C, usually you declare them at start of scope (i.e.: after an {), this is however legal as of C99. I'm just a crufty curmudgeon. It's probably safe to ignore me ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

cap_value_t cap_values[] = { CAP_NET_RAW, CAP_NET_ADMIN };
cap_t caps;

caps = cap_get_proc();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allocates ram therefore could return a NULL. Same comment apply to all subsequent cap_get_proc() calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes.

debug(LOG_DEBUG, "Current capabilities: %s", cap_to_text(caps, NULL));
/* Clear all caps and then set the caps we desire */
cap_clear(caps);
cap_set_flag(caps, CAP_PERMITTED, num_caps, cap_values, CAP_SET);
ret = cap_set_proc(caps);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not cap_free() for this cap_t, my reading of the man page is that cap_set_proc() does not release the memory used by the cap_t.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed all occurences.

if (ret == -1) {
debug(LOG_ERR, "Could not set capabilities!");
exit(1);
}
caps = cap_get_proc();
debug(LOG_DEBUG, "Dropped caps, now: %s", cap_to_text(caps, NULL));
cap_free(caps);
caps = cap_get_proc();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a cap_free() after this cap_get_proc()

debug(LOG_DEBUG, "Current capabilities: %s", cap_to_text(caps, NULL));
debug(LOG_DEBUG, "Regaining capabilities.");
/* Re-gain privileges */
cap_set_flag(caps, CAP_EFFECTIVE, num_caps, cap_values, CAP_SET);
cap_set_flag(caps, CAP_INHERITABLE, num_caps, cap_values, CAP_SET);
/*
* Note that we keep CAP_INHERITABLE empty. In theory, CAP_INHERITABLE
* would be useful to execve iptables as non-root. In practice, Wifidog
* often runs on embedded systems (OpenWrt) where the required file-based
* capabilities are not available as the underlying file system does not
* support extended attributes.
*
* The linux capabilities implementation requires that the executable is
* specifically marked as being able to inherit capabilities from a calling
* process. This can be done by setting the Inheritable+Effective file
* capabilities on the executable. Alas, it's not relevant here.
*
* This is also the main reason why we only seteuid() instead of setuid():
* When an executable is called as root (real UID == 0), the INHERITABLE
* and PERMITTED file capability sets are implicitly marked as enabled.
*/
ret = cap_set_proc(caps);
if (ret == -1) {
debug(LOG_ERR, "Could not set capabilities!");
exit(1);
}
caps = cap_get_proc();
debug(LOG_INFO, "Final capabilities: %s", cap_to_text(caps, NULL));
cap_free(caps);
}


/**
* Switches the effective user ID to 0 (root).
*
* If the underlying seteuid call fails, an error message is logged.
* No other error handling is performed.
*
*/
void switch_to_root() {
int ret = 0;
ret = seteuid(0);
/* Not being able to raise privileges is not fatal. */
if (ret != 0) {
debug(LOG_ERR, "execute: Could not seteuid(0): %s", strerror(errno));
}
ret = setegid(0);
if (ret != 0) {
debug(LOG_ERR, "execute: Could not setegid(0): %s", strerror(errno));
}
debug(LOG_DEBUG, "execute: Switched to UID 0!");;
}


/**
* Switches user and group, typically to a non-privileged user.
*
* If either user or group switching fails, this is considered fatal
* and exit() is called.
*
* @param user name of the user
* @param group name of the group
*
*/
void set_user_group(const char* user, const char* group) {
debug(LOG_DEBUG, "Switching to group %s", group);
/* don't free grp, see getpwnam() for details */
struct group *grp = getgrnam(group);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getgrnam and getpwnam are not thread safe. Use the re-entrant versions getpwnam_r and getgrnam_r.

I might be over cautious here as this might only ever be called from gw_main before threads are spawned... But I don't like future land mines either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

if (NULL == grp) {
debug(LOG_ERR, "Failed to look up GID for group %s: %s", group, strerror(errno));
exit(1);
}
gid_t gid = grp->gr_gid;
struct passwd *pwd = getpwnam(user);
if (NULL == pwd) {
debug(LOG_ERR, "Failed to look up UID for user %s", user);
exit(1);
}
uid_t uid = pwd->pw_uid;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you need this line or the similar line for gid, what's wrong with:

set_uid_gid(pwd->pw_uid, grp->gr_gid);

Why force the C compiler to allocate a variable in the stack? (Actually at -O > 1, gcc will just drop the variable altogether.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

set_uid_gid(uid, gid);

}

/**
* Switches user ID and group ID, typically to a non-privileged user.
*
* If either user or group switching fails, this is considered fatal
* and exit() is called.
*
* @param uid the ID of the user
* @param gid the ID of the group
*
*/
void set_uid_gid(uid_t uid, gid_t gid) {
int ret;
ret = setegid(gid);
if (ret != 0) {
debug(LOG_ERR, "Failed to setegid() %s", strerror(errno));
exit(1);
}
ret = seteuid(uid);
if (ret != 0) {
debug(LOG_ERR, "Failed to seteuid(): %s", strerror(errno));
exit(1);
}
}


/**
* Calls popen with root privileges.
*
* This method is a wrapper around popen(). The effective
* user and group IDs of the current process are temporarily set
* to 0 (root) and then reset to the original, typically non-privileged,
* values before returning.
*
* @param command First popen parameter
* @param type Second popen parameter
* @returns File handle pointer returned by popen
*/
FILE *popen_as_root(const char *command, const char *type) {
FILE *p = NULL;
uid_t uid = getuid();
gid_t gid = getgid();
switch_to_root();
p = popen(command, type);
set_uid_gid(uid, gid);
return p;
}

#endif /* USE_LIBCAP */
50 changes: 50 additions & 0 deletions src/capabilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* vim: set et sw=4 ts=4 sts=4 : */
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA [email protected] *
* *
\********************************************************************/

/** @file capabilities.h
@author Copyright (C) 2015 Michael Haas <[email protected]>
*/

#include "../config.h"

#ifdef USE_LIBCAP

#ifndef _CAPABILITIES_H_
#define _CAPABILITIES_H_

void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declarations in .h should be all on one line. In .c, they should be broken up with return on the previous line. Exactly the reverse of the capabilities.[ch]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

drop_privileges(const char*, const char*);

void
switch_to_root();

FILE*
popen_as_root(const char*, const char*);

void
set_user_group(const char*, const char*);

void
set_uid_gid(uid_t, gid_t);

#endif /* _CAPABILITIES_H_ */

#endif /* USE_LIBCAP */
22 changes: 22 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ typedef enum {
oSSLPeerVerification,
oSSLCertPath,
oSSLAllowedCipherList,
oUser,
oGroup,
} OpCodes;

/** @internal
Expand Down Expand Up @@ -145,6 +147,8 @@ static const struct {
"sslpeerverification", oSSLPeerVerification}, {
"sslcertpath", oSSLCertPath}, {
"sslallowedcipherlist", oSSLAllowedCipherList}, {
"user", oUser}, {
"group", oGroup}, {
NULL, oBadOption},};

static void config_notnull(const void *parm, const char *parmname);
Expand Down Expand Up @@ -197,6 +201,8 @@ config_init(void)
config.ssl_verify = DEFAULT_AUTHSERVSSLPEERVER;
config.ssl_cipher_list = NULL;
config.arp_table_path = safe_strdup(DEFAULT_ARPTABLE);
config.user = safe_strdup(DEFAULT_USER);
config.group = safe_strdup(DEFAULT_GROUP);
}

/**
Expand Down Expand Up @@ -764,6 +770,22 @@ config_read(const char *filename)
debug(LOG_WARNING, "SSLAllowedCipherList is set but no SSL compiled in. Ignoring!");
#endif
break;
case oUser:
#ifndef USE_LIBCAP
debug(LOG_WARNING, "Non-privileged user is set but not compiled with libcap. Bailing out!");
exit(1);
#endif
free(config.user);
config.user = safe_strdup(p1);
break;
case oGroup:
#ifndef USE_LIBCAP
debug(LOG_WARNING, "Non-privileged group is set but not compiled with libcap. Bailing out!");
exit(1);
#endif
free(config.group);
config.group = safe_strdup(p1);
break;
case oBadOption:
/* FALL THROUGH */
default:
Expand Down
Loading