diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2018-05-30 10:02:47 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2018-05-30 10:02:47 +0200 |
commit | d5ea8f4ae6db7a198fdd139417c08b346b8834b5 (patch) | |
tree | 17f09e55b70a16f1a92b69b2f15dffa4990b5983 | |
parent | ac567dfe566d582fb59297487822a3afadff1974 (diff) |
POTD skeleton #81.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/pevent.c | 54 | ||||
-rw-r--r-- | src/pevent.h | 4 | ||||
-rw-r--r-- | src/pseccomp.c | 4 | ||||
-rw-r--r-- | src/redirector.c | 10 |
4 files changed, 52 insertions, 20 deletions
diff --git a/src/pevent.c b/src/pevent.c index 7ed9b55..ea29825 100644 --- a/src/pevent.c +++ b/src/pevent.c @@ -9,6 +9,23 @@ #include "pevent.h" #include "log.h" +static int epoll_event_string(uint32_t event, char *buf, size_t siz); + + +static int epoll_event_string(uint32_t event, char *buf, size_t siz) +{ + if (event & EPOLLERR) { + snprintf(buf, siz, "%s", "EPOLLERR"); + } else if (event & EPOLLHUP) { + snprintf(buf, siz, "%s", "EPOLLHUP"); + } else if (event & EPOLLRDHUP) { + snprintf(buf, siz, "%s", "EPOLLRDHUP"); + } else if (event & EPOLLIN) { + snprintf(buf, siz, "%s", "EPOLLIN"); + } else return 1; + + return 0; +} void event_init(event_ctx **ctx) { @@ -75,20 +92,23 @@ int event_add_fd(event_ctx *ctx, int fd) int event_loop(event_ctx *ctx, on_event_cb on_event, void *user_data) { - int n, i; + int n, i, saved_errno; + char ev_err[16]; sigset_t eset; assert(ctx && on_event); sigemptyset(&eset); ctx->active = 1; + ctx->has_error = 0; - while (ctx->active) { + while (ctx->active && !ctx->has_error) { errno = 0; n = epoll_pwait(ctx->epoll_fd, ctx->events, POTD_MAXEVENTS, -1, &eset); + saved_errno = errno; if (errno == EINTR) continue; if (n < 0) { - ctx->active = 0; + ctx->has_error = 1; break; } @@ -100,21 +120,29 @@ int event_loop(event_ctx *ctx, on_event_cb on_event, void *user_data) (ctx->events[i].events & EPOLLRDHUP) || (!(ctx->events[i].events & EPOLLIN))) { - E_STRERR("Event epoll for descriptor %d", - ctx->events[i].data.fd); - ctx->active = 0; + if (epoll_event_string(ctx->events[i].events, ev_err, sizeof ev_err)) { + errno = saved_errno; + E_STRERR("Event for descriptor %d", + ctx->events[i].data.fd); + } else { + errno = saved_errno; + E_STRERR("Event [%s] for descriptor %d", + ev_err, ctx->events[i].data.fd); + } + + ctx->has_error = 1; } else { - if (!on_event(ctx, ctx->events[i].data.fd, user_data)) + if (!on_event(ctx, ctx->events[i].data.fd, user_data) && !ctx->has_error) W2("Event callback failed: [fd: %d , npoll: %d]", ctx->events[i].data.fd, n); } - if (!ctx->active) + if (!ctx->active || ctx->has_error) break; } } - return ctx->active == 0; + return ctx->has_error != 0; } forward_state @@ -150,6 +178,7 @@ event_forward_connection(event_ctx *ctx, int dest_fd, on_data_cb on_data, switch (siz) { case -1: E_STRERR("Client read from fd %d", ev->data.fd); + ctx->has_error = 1; rc = CON_IN_ERROR; break; case 0: @@ -177,6 +206,7 @@ event_forward_connection(event_ctx *ctx, int dest_fd, on_data_cb on_data, switch (siz) { case -1: + ctx->has_error = 1; rc = CON_OUT_ERROR; break; case 0: @@ -195,10 +225,8 @@ event_forward_connection(event_ctx *ctx, int dest_fd, on_data_cb on_data, D2("Connection state: %d", rc); if (rc != CON_OK) { - if (shutdown(ev->data.fd, SHUT_RDWR)) - E_STRERR("Shutdown source socket fd %d", ev->data.fd); - if (shutdown(dest_fd, SHUT_RDWR)) - E_STRERR("Shutdown dest socket fd %d", dest_fd); + shutdown(ev->data.fd, SHUT_RDWR); + shutdown(dest_fd, SHUT_RDWR); } return rc; } diff --git a/src/pevent.h b/src/pevent.h index d6927f6..5f9b4fd 100644 --- a/src/pevent.h +++ b/src/pevent.h @@ -14,8 +14,10 @@ typedef enum forward_state { } forward_state; typedef struct event_ctx { - int epoll_fd; int active; + int has_error; + + int epoll_fd; struct epoll_event events[POTD_MAXEVENTS]; int current_event; } event_ctx; diff --git a/src/pseccomp.c b/src/pseccomp.c index 376bbc6..23198bd 100644 --- a/src/pseccomp.c +++ b/src/pseccomp.c @@ -39,7 +39,7 @@ static const int default_allowed_syscalls[] = { SCMP_SYS(lseek), SCMP_SYS(stat), SCMP_SYS(readlink), SCMP_SYS(getcwd), SCMP_SYS(lstat), SCMP_SYS(sysinfo), SCMP_SYS(setuid), SCMP_SYS(setgid), - SCMP_SYS(setreuid), SCMP_SYS(setregid), + SCMP_SYS(setresuid), SCMP_SYS(setresgid), SCMP_SYS(getuid), SCMP_SYS(geteuid), SCMP_SYS(getgid), SCMP_SYS(getegid), SCMP_SYS(getgroups), SCMP_SYS(getdents), SCMP_SYS(getpgrp), SCMP_SYS(setpgid), SCMP_SYS(getpid), SCMP_SYS(kill), @@ -72,7 +72,7 @@ static const int jail_allowed_syscalls[] = { SCMP_SYS(lseek), SCMP_SYS(stat), SCMP_SYS(readlink), SCMP_SYS(getcwd), SCMP_SYS(lstat), SCMP_SYS(sysinfo), SCMP_SYS(setuid), SCMP_SYS(setgid), - SCMP_SYS(setreuid), SCMP_SYS(setregid), + SCMP_SYS(setresuid), SCMP_SYS(setresgid), SCMP_SYS(getuid), SCMP_SYS(geteuid), SCMP_SYS(getgid), SCMP_SYS(getegid), SCMP_SYS(getgroups), SCMP_SYS(getdents), SCMP_SYS(getpgrp), SCMP_SYS(setpgid), SCMP_SYS(getpid), SCMP_SYS(kill), diff --git a/src/redirector.c b/src/redirector.c index 664aa9a..be3802f 100644 --- a/src/redirector.c +++ b/src/redirector.c @@ -317,7 +317,8 @@ client_mainloop(void *arg) args->rdr_ctx->sock.fd); goto finish; } - N("Forwarding connection to %s:%s forward fd %d", + N("Forwarding connection from %s:%s to %s:%s forward fd %d", + args->host_buf, args->service_buf, args->rdr_ctx->fwd_ctx.host_buf, args->rdr_ctx->fwd_ctx.service_buf, fwd.fd); @@ -348,10 +349,11 @@ client_mainloop(void *arg) ev_cli.client_args = args; ev_cli.fwd_sock = &fwd; - if (event_loop(ev_ctx, client_io, &ev_cli)) - E_STRERR("Forward connection data to %s:%s forward fd %d", + if (event_loop(ev_ctx, client_io, &ev_cli) && ev_ctx->has_error) + E_STRERR("Forward connection data from %s:%s to %s:%s", + args->host_buf, args->service_buf, args->rdr_ctx->fwd_ctx.host_buf, - args->rdr_ctx->fwd_ctx.service_buf, fwd.fd); + args->rdr_ctx->fwd_ctx.service_buf); finish: event_free(&ev_ctx); |