Skip to content

Commit

Permalink
freebsd: add capsicum sandbox
Browse files Browse the repository at this point in the history
Add support for capsicum on freebsd:

    EPCAP_SANDBOX=capsicum rebar3 do clean, compile, ct

This change adds some ifdef's to use process descriptors (why don't all
unix'es support process descriptors? looking at you linux) because after
capability mode is entered the supervisor process will not be able to
signal the child process by PID.
  • Loading branch information
msantos committed Dec 31, 2017
1 parent b603118 commit 8967f4b
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 2 deletions.
56 changes: 54 additions & 2 deletions c_src/epcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#include <stdint.h>
#include <ei.h>

#ifdef EPCAP_SANDBOX_capsicum
#include <sys/param.h>
#include <sys/procdesc.h>
#endif

#include "epcap.h"

static int epcap_open(EPCAP_STATE *);
Expand All @@ -57,7 +62,6 @@ main(int argc, char *argv[])
int ch = 0;
int fd = 0;


ep = calloc(1, sizeof(EPCAP_STATE));

if (ep == NULL)
Expand Down Expand Up @@ -188,7 +192,12 @@ main(int argc, char *argv[])

signal(SIGCHLD, gotsig);

switch (pid = fork()) {
#ifdef EPCAP_SANDBOX_capsicum
pid = pdfork(&ep->pdfd, 0);
#else
pid = fork();
#endif
switch (pid) {
case -1:
exit(errno);
case 0:
Expand Down Expand Up @@ -220,7 +229,11 @@ main(int argc, char *argv[])
epcap_send(ep);

CLEANUP:
#ifdef EPCAP_SANDBOX_capsicum
(void)pdkill(ep->pdfd, SIGTERM);
#else
(void)kill(pid, SIGTERM);
#endif
break;
}

Expand All @@ -232,14 +245,53 @@ main(int argc, char *argv[])
epcap_send(EPCAP_STATE *ep)
{
const int fd = STDIN_FILENO;
int maxfd = 0;
ssize_t n = 0;
unsigned char buf[SNAPLEN] = {0};
u_int16_t len = 0;

int rv = 0;

for ( ; ; ) {
fd_set rfds;

if (child_exited)
return;

FD_ZERO(&rfds);
FD_SET(fd, &rfds);

#ifdef EPCAP_SANDBOX_capsicum
FD_SET(ep->pdfd, &rfds);
maxfd = MAX(fd, ep->pdfd) + 1;
#else
maxfd = fd + 1;
#endif

rv = select(maxfd, &rfds, NULL, NULL, NULL);

switch (rv) {
case 0:
continue;

case -1:
if (errno == EINTR)
continue;

return;

default:
break;
}

#ifdef EPCAP_SANDBOX_capsicum
if (FD_ISSET(ep->pdfd, &rfds))
return;
#endif

if (!FD_ISSET(fd, &rfds))
continue;

n = read_exact(fd, buf, sizeof(len));

if (n != sizeof(len)) {
Expand Down
3 changes: 3 additions & 0 deletions c_src/epcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ enum {

typedef struct {
pcap_t *p; /* pcap handle */
#ifdef EPCAP_SANDBOX_capsicum
int pdfd; /* process descriptor for pcap process */
#endif
int datalink; /* dlt */
int opt; /* options */
int verbose; /* debug messages */
Expand Down
126 changes: 126 additions & 0 deletions c_src/epcap_sandbox_capsicum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* Copyright (c) 2017, Michael Santos <michael.santos@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef EPCAP_SANDBOX_capsicum

#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/capability.h>

#include <errno.h>

#include <pcap/pcap.h>
#include "epcap.h"

int
epcap_sandbox_pcap()
{
struct rlimit rl = {0};
cap_rights_t policy_read;
cap_rights_t policy_null;
cap_rights_t policy_write;

int fd = -1;

(void)cap_rights_init(&policy_read, CAP_READ, CAP_EVENT);
(void)cap_rights_init(&policy_null);
(void)cap_rights_init(&policy_write, CAP_WRITE);

if (cap_rights_limit(STDIN_FILENO, &policy_null) < 0)
return -1;

if (cap_rights_limit(STDOUT_FILENO, &policy_write) < 0)
return -1;

if (cap_rights_limit(STDERR_FILENO, &policy_write) < 0)
return -1;

if (setrlimit(RLIMIT_NPROC, &rl) != 0)
return -1;

if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
return -1;

for (fd = STDERR_FILENO+1; fd < rl.rlim_cur; fd++) {
if (fcntl(fd, F_GETFD, 0) < 0)
continue;

if (cap_rights_limit(fd, &policy_read) < 0)
return -1;
}

return cap_enter();
}

int
epcap_sandbox_erl()
{
struct rlimit rl = {0};
cap_rights_t policy_read;
cap_rights_t policy_write;
cap_rights_t policy_null;

int fd = -1;

(void)cap_rights_init(&policy_read, CAP_READ, CAP_EVENT);
(void)cap_rights_init(&policy_write, CAP_WRITE, CAP_EVENT);
(void)cap_rights_init(&policy_null);

if (cap_rights_limit(STDIN_FILENO, &policy_read) < 0)
return -1;

if (cap_rights_limit(STDOUT_FILENO, &policy_null) < 0)
return -1;

if (cap_rights_limit(STDERR_FILENO, &policy_write) < 0)
return -1;

if (setrlimit(RLIMIT_NPROC, &rl) != 0)
return -1;

if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
return -1;

for (fd = STDERR_FILENO+1; fd < rl.rlim_cur; fd++) {
if (fcntl(fd, F_GETFD, 0) < 0)
continue;

if (cap_rights_limit(fd, &policy_write) < 0)
return -1;
}

return cap_enter();
}
#endif

0 comments on commit 8967f4b

Please sign in to comment.