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

improve receive of terminal fd #411

Merged
merged 5 commits into from
Apr 28, 2023
Merged
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
6 changes: 3 additions & 3 deletions src/cmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@

#define error(s) \
do { \
fprintf(stderr, "nsenter: %s %s\n", s, strerror(errno)); \
fprintf(stderr, "conmon: %s %s\n", s, strerror(errno)); \
errno = ECOMM; \
goto err; /* return value */ \
} while (0)

#define errorf(fmt, ...) \
do { \
fprintf(stderr, "nsenter: " fmt ": %s\n", ##__VA_ARGS__, strerror(errno)); \
fprintf(stderr, "conmon: " fmt ": %s\n", ##__VA_ARGS__, strerror(errno)); \
errno = ECOMM; \
goto err; /* return value */ \
} while (0)
Expand Down Expand Up @@ -162,5 +162,5 @@ struct file_t recvfd(int sockfd)
olderrno = errno;
free(file.name);
errno = olderrno;
return (struct file_t){0};
return (struct file_t){.name = NULL, .fd = -1};
}
13 changes: 10 additions & 3 deletions src/ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ gboolean terminal_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC
{

ndebugf("about to accept from console_socket_fd: %d", fd);
int connfd = accept4(fd, NULL, NULL, SOCK_CLOEXEC);

int connfd;
do {
connfd = accept4(fd, NULL, NULL, SOCK_CLOEXEC);
} while (connfd < 0 && errno == EINTR);
if (connfd < 0) {
nwarn("Failed to accept console-socket connection");
return G_SOURCE_CONTINUE;
pexit("Failed to accept console-socket connection");
}

/* Not accepting anything else. */
Expand All @@ -43,6 +46,10 @@ gboolean terminal_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC
ndebugf("about to recvfd from connfd: %d", connfd);
struct file_t console = recvfd(connfd);

if (console.fd < 0) {
pexit("Failed to receive console file descriptor");
}

ndebugf("console = {.name = '%s'; .fd = %d}", console.name, console.fd);
free(console.name);

Expand Down
11 changes: 4 additions & 7 deletions src/seccomp_notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ gboolean seccomp_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_
struct file_t listener = recvfd(connfd);
close(connfd);

if (listener.fd < 0) {
pexit("Failed to receive socket listener file descriptor");
}

_cleanup_free_ char *oci_config_path = g_strdup_printf("%s/config.json", opt_bundle_path);
if (oci_config_path == NULL) {
nwarn("Failed to allocate memory");
Expand Down Expand Up @@ -110,7 +114,6 @@ int seccomp_notify_plugins_load(struct seccomp_notify_context_s **out, const cha

if (seccomp_syscall(SECCOMP_GET_NOTIF_SIZES, 0, &ctx->sizes) < 0) {
pexit("Failed to get notifications size");
return -1;
}

ctx->sreq = xmalloc0(ctx->sizes.seccomp_notif);
Expand All @@ -125,7 +128,6 @@ int seccomp_notify_plugins_load(struct seccomp_notify_context_s **out, const cha
b = strdup(plugins);
if (b == NULL) {
pexit("Failed to strdup");
return -1;
}
for (s = 0, it = strtok_r(b, ":", &saveptr); it; s++, it = strtok_r(NULL, ":", &saveptr)) {
run_oci_seccomp_notify_plugin_version_cb version_cb;
Expand All @@ -135,7 +137,6 @@ int seccomp_notify_plugins_load(struct seccomp_notify_context_s **out, const cha
ctx->plugins[s].handle = dlopen(it, RTLD_NOW);
if (ctx->plugins[s].handle == NULL) {
pexitf("cannot load `%s`: %s", it, dlerror());
return -1;
}

version_cb = (run_oci_seccomp_notify_plugin_version_cb)dlsym(ctx->plugins[s].handle, "run_oci_seccomp_notify_version");
Expand All @@ -145,15 +146,13 @@ int seccomp_notify_plugins_load(struct seccomp_notify_context_s **out, const cha
version = version_cb();
if (version != 1) {
pexitf("invalid version supported by the plugin `%s`", it);
return -1;
}
}

ctx->plugins[s].handle_request_cb =
(run_oci_seccomp_notify_handle_request_cb)dlsym(ctx->plugins[s].handle, "run_oci_seccomp_notify_handle_request");
if (ctx->plugins[s].handle_request_cb == NULL) {
pexitf("plugin `%s` doesn't export `run_oci_seccomp_notify_handle_request`", it);
return -1;
}

start_cb = (run_oci_seccomp_notify_start_cb)dlsym(ctx->plugins[s].handle, "run_oci_seccomp_notify_start");
Expand All @@ -163,7 +162,6 @@ int seccomp_notify_plugins_load(struct seccomp_notify_context_s **out, const cha
ret = start_cb(&opq, conf, sizeof(*conf));
if (ret != 0) {
pexitf("error loading `%s`", it);
return -1;
}
}
ctx->plugins[s].opaque = opq;
Expand Down Expand Up @@ -223,7 +221,6 @@ int seccomp_notify_plugins_event(struct seccomp_notify_context_s *ctx, int secco

default:
pexitf("Unknown handler action specified %d", handled);
return -1;
}
}
}
Expand Down