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

Add non capsicum build option #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ netdumpd.debug
netdumpd.8.gz

.ccls-cache

client/netdump-client
client/netdump-client.debug
client/netdump-client.full
19 changes: 15 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
PROG= netdumpd

.if defined(WITHOUT_CAPSICUM)

LDADD+= -lnv -lutil
SRCS= netdumpd.c \
nocap_handler.c \
nocap_herald.c

.else

CFLAGS+= -DWITH_CASPER -DWITH_CAPSICUM
LDADD+= -lcasper -lnv -lutil
SRCS= netdumpd.c \
cap_dns.c \
cap_handler.c \
cap_herald.c
MAN= netdumpd.8
BINDIR= /usr/sbin

LDADD+= -lcasper -lnv -lutil
.endif

CFLAGS+= -DWITH_CASPER
MAN= netdumpd.8
BINDIR= /usr/sbin

WARNS?= 6

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Summary
Netdump is a daemon that accepts network core dumps

# How to build
## With Capsicum
```
make
```

## Without Capsicum
```
# Requires FreeBSD 13 or higher
make WITHOUT_CAPSICUM=1
```
1 change: 1 addition & 0 deletions client/netdump-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/netdump/netdump.h>
#include <net/debugnet.h>

#include <err.h>
#include <fcntl.h>
Expand Down
76 changes: 67 additions & 9 deletions netdumpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SDPX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2005-2011 Sandvine Incorporated. All rights reserved.
* Copyright (c) 2016-2018 Dell EMC
* Copyright (c) 2016-2023 Dell EMC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand All @@ -27,12 +27,11 @@
*/

#include <sys/param.h>
#include <sys/capsicum.h>

#include <sys/endian.h>
#include <sys/errno.h>
#include <sys/event.h>
#include <sys/kerneldump.h>
#include <sys/nv.h>
#include <sys/procdesc.h>
#include <sys/queue.h>
#include <sys/socket.h>
Expand All @@ -44,7 +43,14 @@
#include <netinet/in.h>

#include <assert.h>

#ifdef WITH_CAPSICUM
#include <sys/capsicum.h>
#include <sys/nv.h>
#include <capsicum_helpers.h>
#include "cap_dns.h"
#endif

#include <err.h>
#include <fcntl.h>
#include <libgen.h>
Expand All @@ -62,7 +68,6 @@

#include <libutil.h>

#include "cap_dns.h"
#include "netdumpd.h"
#include "kerneldump_compat.h"

Expand Down Expand Up @@ -108,8 +113,10 @@ struct netdump_client {
/* Clients list. */
static LIST_HEAD(, netdump_client) g_clients = LIST_HEAD_INITIALIZER(g_clients);

#ifdef WITH_CAPSICUM
/* Capabilities. */
static cap_channel_t *g_capdns, *g_caphandler, *g_capherald;
#endif

/* Program arguments handlers. */
static char g_dumpdir[MAXPATHLEN];
Expand Down Expand Up @@ -192,7 +199,7 @@ read_index(struct netdump_client *client, const char *dir)
return (-1);
}
fd = openat(g_dumpdir_fd, bounds,
O_RDONLY | O_CREAT | O_CLOEXEC, 0600);
O_RDONLY | O_CREAT | O_CLOEXEC | O_RESOLVE_BENEATH, 0600);
if (fd < 0) {
LOGERR("openat(%s): %s\n", bounds, strerror(errno));
return (-1);
Expand Down Expand Up @@ -244,7 +251,7 @@ write_index(struct netdump_client *client)
LOGERR("Truncated bounds file path: '%s'\n", bounds);
return (-1);
}
fd = openat(g_dumpdir_fd, bounds, O_WRONLY | O_CLOEXEC);
fd = openat(g_dumpdir_fd, bounds, O_WRONLY | O_CLOEXEC | O_RESOLVE_BENEATH);
if (fd < 0) {
LOGERR("openat(%s): %s\n", bounds, strerror(errno));
return (-1);
Expand Down Expand Up @@ -277,7 +284,7 @@ open_info_file(struct netdump_client *client, const char *dir, int idx)
}

fd = openat(g_dumpdir_fd, client->infofilename,
O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_RESOLVE_BENEATH, 0600);
if (fd == -1 && errno != EEXIST)
LOGERR("openat(\"%s\"): %s\n",
client->infofilename, strerror(errno));
Expand Down Expand Up @@ -330,7 +337,7 @@ open_client_files(struct netdump_client *client, const char *dir)
return (-1);
}
fd = openat(g_dumpdir_fd, client->corefilename,
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_RESOLVE_BENEATH, 0600);
if (fd == -1) {
error = errno;
/* Failed. Keep the numbers in sync. */
Expand Down Expand Up @@ -370,6 +377,7 @@ alloc_client(int sd, struct sockaddr_in *saddr, char *path)
client->last_msg = g_now;
client->ip = saddr->sin_addr;

#ifdef WITH_CAPSICUM
error = cap_getnameinfo(g_capdns, (struct sockaddr *)saddr,
saddr->sin_len, client->hostname, sizeof(client->hostname),
NULL, 0, NI_NAMEREQD);
Expand All @@ -388,6 +396,26 @@ alloc_client(int sd, struct sockaddr_in *saddr, char *path)
if (firstdot)
*firstdot = '\0';
}
#else
error = getnameinfo((struct sockaddr *)saddr,
saddr->sin_len, client->hostname, sizeof(client->hostname),
NULL, 0, NI_NAMEREQD);
if (error != 0) {
/* Can't resolve, try with a numeric IP. */
error = getnameinfo((struct sockaddr *)saddr,
saddr->sin_len, client->hostname, sizeof(client->hostname),
NULL, 0, 0);
if (error != 0) {
LOGERR("getnameinfo(): %s\n", gai_strerror(error));
goto error_out;
}
} else {
/* Strip off the domain name. */
firstdot = strchr(client->hostname, '.');
if (firstdot)
*firstdot = '\0';
}
#endif
Copy link
Owner

Choose a reason for hiding this comment

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

You should be able to do something like

#define cap_getnameinfo(channel, ...) getnameinfo(__VA_ARGS__)

in the non-capsicum case, and then everything ought to work without any code duplication.


/* It should be enough to hold approximatively twice the chunk size. */
bufsz = 128 * 1024;
Expand Down Expand Up @@ -474,12 +502,24 @@ exec_handler(struct netdump_client *client, const char *reason)
struct kevent ev;
int pd;

#ifdef WITH_CAPSICUM
if (g_caphandler == NULL)
return;
pd = netdump_cap_handler(g_caphandler, reason, client_ntoa(client),
client->hostname, client->infofilename, client->corefilename);
if (pd < 0)
LOGERR("netdump_cap_handler(): %m");

#else
if(g_handler_script == NULL)
return;
pd = netdump_handler(g_handler_script, reason, client_ntoa(client),
client->hostname, client->infofilename, client->corefilename);
if (pd < 0)
LOGERR("netdump_handler(): %m");

#endif

EV_SET(&ev, pd, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
if (kevent(g_kq, &ev, 1, NULL, 0, NULL) != 0) {
LOGERR_PERROR("kevent(procdesc)");
Expand Down Expand Up @@ -721,7 +761,7 @@ handle_ekcd_key(struct netdump_client *client, struct netdump_pkt *pkt)
}

fd = openat(g_dumpdir_fd, keyfile,
O_WRONLY | O_CREAT | O_CLOEXEC, 0400);
O_WRONLY | O_CREAT | O_CLOEXEC | O_RESOLVE_BENEATH, 0400);
if (fd < 0) {
LOGERR_PERROR("openat()");
return;
Expand Down Expand Up @@ -841,12 +881,21 @@ server_event(void)
uint32_t seqno;
int error, sd;

#ifdef WITH_CAPSICUM
error = netdump_cap_herald(g_capherald, &sd, &saddr, &seqno, &path);
if (error != 0) {
LOGERR("netdump_cap_herald(): %s\n", strerror(error));
return;
}

#else
error = netdump_herald(g_sock, &sd, &saddr, &seqno, &path);
if (error != 0) {
LOGERR("netdump_herald(): %s\n", strerror(error));
return;
}

#endif
LIST_FOREACH(client, &g_clients, iter) {
if (client->ip.s_addr == saddr.sin_addr.s_addr)
break;
Expand Down Expand Up @@ -1023,6 +1072,7 @@ get_script_option(void)
return (script);
}

#ifdef WITH_CAPSICUM
/*
* Enter capability mode. This effectively sandboxes netdumpd by restricting
* its ability to acquire new rights. In particular, capability mode enforces
Expand Down Expand Up @@ -1116,6 +1166,7 @@ init_cap_mode(void)
cap_close(capcasper);
return (error);
}
#endif

static int
init_kqueue(void)
Expand Down Expand Up @@ -1181,6 +1232,7 @@ init_server_socket(void)
bindaddr.sin_family = AF_INET;
bindaddr.sin_addr.s_addr = g_bindip.s_addr;
bindaddr.sin_port = htons(NETDUMP_PORT);

if (bind(g_sock, (struct sockaddr *)&bindaddr, sizeof(bindaddr))) {
LOGERR_PERROR("bind()");
return (1);
Expand Down Expand Up @@ -1303,9 +1355,13 @@ main(int argc, char **argv)
goto cleanup;
if (init_kqueue())
goto cleanup;

#ifdef WITH_CAPSICUM
if (init_cap_mode())
goto cleanup;
#endif

// Loop here until service is stopped
Copy link
Owner

Choose a reason for hiding this comment

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

Please use C-style comments, just to keep things consistent.

exit_code = eventloop();

cleanup:
Expand All @@ -1315,9 +1371,11 @@ main(int argc, char **argv)
free(g_handler_script);
if (g_sock != -1)
close(g_sock);
#ifdef WITH_CAPSICUM
if (g_capherald != NULL)
cap_close(g_capherald);
if (g_capdns != NULL)
cap_close(g_capdns);
#endif
return (exit_code);
}
8 changes: 8 additions & 0 deletions netdumpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@
struct cap_channel;
struct sockaddr_in;

#ifdef WITH_CAPSICUM
int netdump_cap_handler(struct cap_channel *, const char *, const char *,
const char *, const char *, const char *);
int netdump_cap_herald(struct cap_channel *, int *, struct sockaddr_in *,
uint32_t *, char **);

Copy link
Owner

Choose a reason for hiding this comment

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

Extra newline here.

#else
int netdump_handler(const char *, const char *, const char *,
const char *, const char *, const char *);
int netdump_herald(int g_sock, int *, struct sockaddr_in *,
uint32_t *, char **);
#endif

#define NETDUMP_DATASIZE 4096
#define NETDUMP_PORT 20023
#define NETDUMP_ACKPORT 20024
Expand Down
76 changes: 76 additions & 0 deletions nocap_handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*-
* Copyright (c) 2023 Dell EMC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/

#include <sys/param.h>
#include <sys/dnv.h>
#include <sys/nv.h>
#include <sys/procdesc.h>

#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "netdumpd.h"

/*
* The handler capability lets us invoke a script upon completion (successful or
* otherwise) of a netdump. The script is executed with cwd set to the dumpdir.
*/

int
netdump_handler(const char *script, const char *reason, const char *ip,
const char *hostname, const char *infofile, const char *corefile)
{
int pd;
const char *argv[7];
pid_t pid;

// Starting with linux unfriendly code (pdfork)
// ?? errno?
if ((pid = pdfork(&pd, PD_CLOEXEC)) < 0)
Copy link
Owner

Choose a reason for hiding this comment

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

pdfork() is roughly equivalent to fork()+pidfd_open() on Linux, I believe.

return (errno);
if (pid == 0) {
argv[0] = script;
argv[1] = reason;
argv[2] = ip;
argv[3] = hostname;
argv[4] = infofile;
argv[5] = corefile;
argv[6] = NULL;
(void)execve(script, __DECONST(char *const *, argv), NULL);
_exit(1);
}

// ?? is this correct?
// if (pd != -1) {
// (void)close(pd);
// pd = -1;
// }
// return (pd);
return pd;
}
Loading