1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
#define _GNU_SOURCE
#if __STDC_VERSION__ >= 199901L
/* C99 or later */
#else
#error conmon.c requires C99 or later
#endif
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <signal.h>
#include <sys/socket.h>
#include "cli.h" // opt_bundle_path
#include "utils.h"
#include "cmsg.h"
#ifdef USE_SECCOMP
#include <dlfcn.h>
#include <sys/sysmacros.h>
#include <linux/seccomp.h>
#include <seccomp.h>
#include "seccomp_notify.h"
#ifndef SECCOMP_USER_NOTIF_FLAG_CONTINUE
#define SECCOMP_USER_NOTIF_FLAG_CONTINUE (1UL << 0)
#endif
static struct seccomp_notify_context_s *seccomp_notify_ctx;
struct plugin {
void *handle;
void *opaque;
run_oci_seccomp_notify_handle_request_cb handle_request_cb;
};
struct seccomp_notify_context_s {
struct plugin *plugins;
size_t n_plugins;
struct seccomp_notif_resp *sresp;
struct seccomp_notif *sreq;
struct seccomp_notif_sizes sizes;
};
static inline void *xmalloc0(size_t size);
static void cleanup_seccomp_plugins();
static int seccomp_syscall(unsigned int op, unsigned int flags, void *args);
gboolean seccomp_cb(int fd, GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
{
if (condition & G_IO_IN) {
if (seccomp_notify_ctx == NULL)
return G_SOURCE_REMOVE;
int ret = seccomp_notify_plugins_event(seccomp_notify_ctx, fd);
return ret == 0 ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
gboolean seccomp_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
{
ndebugf("about to accept from seccomp_socket_fd: %d", fd);
int connfd = accept4(fd, NULL, NULL, SOCK_CLOEXEC);
if (connfd < 0) {
nwarn("Failed to accept console-socket connection");
return G_SOURCE_CONTINUE;
}
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");
return G_SOURCE_CONTINUE;
}
struct seccomp_notify_conf_s conf = {
.runtime_root_path = NULL,
.name = opt_name,
.bundle_path = opt_bundle_path,
.oci_config_path = oci_config_path,
};
int ret = seccomp_notify_plugins_load(&seccomp_notify_ctx, opt_seccomp_notify_plugins, &conf);
if (ret < 0) {
nwarn("Failed to initialize seccomp notify plugins");
return G_SOURCE_CONTINUE;
}
g_unix_set_fd_nonblocking(listener.fd, TRUE, NULL);
g_unix_fd_add(listener.fd, G_IO_IN | G_IO_HUP, seccomp_cb, NULL);
atexit(cleanup_seccomp_plugins);
return G_SOURCE_CONTINUE;
}
int seccomp_notify_plugins_load(struct seccomp_notify_context_s **out, const char *plugins, struct seccomp_notify_conf_s *conf)
{
cleanup_seccomp_notify_context struct seccomp_notify_context_s *ctx = xmalloc0(sizeof *ctx);
_cleanup_free_ char *b = NULL;
char *it, *saveptr;
size_t s;
if (seccomp_syscall(SECCOMP_GET_NOTIF_SIZES, 0, &ctx->sizes) < 0) {
pexit("Failed to get notifications size");
}
ctx->sreq = xmalloc0(ctx->sizes.seccomp_notif);
ctx->sresp = xmalloc0(ctx->sizes.seccomp_notif_resp);
ctx->n_plugins = 1;
for (it = b; it; it = strchr(it, ':'))
ctx->n_plugins++;
ctx->plugins = xmalloc0(sizeof(struct plugin) * (ctx->n_plugins + 1));
b = strdup(plugins);
if (b == NULL) {
pexit("Failed to strdup");
}
for (s = 0, it = strtok_r(b, ":", &saveptr); it; s++, it = strtok_r(NULL, ":", &saveptr)) {
run_oci_seccomp_notify_plugin_version_cb version_cb;
run_oci_seccomp_notify_start_cb start_cb;
void *opq = NULL;
ctx->plugins[s].handle = dlopen(it, RTLD_NOW);
if (ctx->plugins[s].handle == NULL) {
pexitf("cannot load `%s`: %s", it, dlerror());
}
version_cb = (run_oci_seccomp_notify_plugin_version_cb)dlsym(ctx->plugins[s].handle, "run_oci_seccomp_notify_version");
if (version_cb != NULL) {
int version;
version = version_cb();
if (version != 1) {
pexitf("invalid version supported by the plugin `%s`", it);
}
}
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);
}
start_cb = (run_oci_seccomp_notify_start_cb)dlsym(ctx->plugins[s].handle, "run_oci_seccomp_notify_start");
if (start_cb) {
int ret;
ret = start_cb(&opq, conf, sizeof(*conf));
if (ret != 0) {
pexitf("error loading `%s`", it);
}
}
ctx->plugins[s].opaque = opq;
}
/* Change ownership. */
*out = ctx;
ctx = NULL;
return 0;
}
int seccomp_notify_plugins_event(struct seccomp_notify_context_s *ctx, int seccomp_fd)
{
size_t i;
int ret;
bool handled = false;
memset(ctx->sreq, 0, ctx->sizes.seccomp_notif);
memset(ctx->sresp, 0, ctx->sizes.seccomp_notif_resp);
ret = ioctl(seccomp_fd, SECCOMP_IOCTL_NOTIF_RECV, ctx->sreq);
if (ret < 0) {
if (errno == ENOENT)
return 0;
nwarnf("Failed to read notification from %d", seccomp_fd);
return -1;
}
for (i = 0; i < ctx->n_plugins; i++) {
if (ctx->plugins[i].handle_request_cb) {
int resp_handled = 0;
int ret;
ret = ctx->plugins[i].handle_request_cb(ctx->plugins[i].opaque, &ctx->sizes, ctx->sreq, ctx->sresp, seccomp_fd,
&resp_handled);
if (ret != 0) {
nwarnf("Failed to handle seccomp notification from fd %d", seccomp_fd);
return -1;
}
switch (resp_handled) {
case RUN_OCI_SECCOMP_NOTIFY_HANDLE_NOT_HANDLED:
break;
case RUN_OCI_SECCOMP_NOTIFY_HANDLE_SEND_RESPONSE:
handled = true;
break;
/* The plugin will take care of it. */
case RUN_OCI_SECCOMP_NOTIFY_HANDLE_DELAYED_RESPONSE:
return 0;
case RUN_OCI_SECCOMP_NOTIFY_HANDLE_SEND_RESPONSE_AND_CONTINUE:
ctx->sresp->flags |= SECCOMP_USER_NOTIF_FLAG_CONTINUE;
handled = true;
break;
default:
pexitf("Unknown handler action specified %d", handled);
}
}
}
/* No plugin could handle the request. */
if (!handled) {
ctx->sresp->error = -ENOTSUP;
ctx->sresp->flags = 0;
}
ctx->sresp->id = ctx->sreq->id;
ret = ioctl(seccomp_fd, SECCOMP_IOCTL_NOTIF_SEND, ctx->sresp);
if (ret < 0) {
if (errno == ENOENT)
return 0;
nwarnf("Failed to send seccomp notification on fd %d", seccomp_fd);
return -errno;
}
return 0;
}
int seccomp_notify_plugins_free(struct seccomp_notify_context_s *ctx)
{
size_t i;
if (ctx == NULL) {
nwarnf("Invalid seccomp notification context");
return -1;
}
free(ctx->sreq);
free(ctx->sresp);
for (i = 0; i < ctx->n_plugins; i++) {
if (ctx->plugins && ctx->plugins[i].handle) {
run_oci_seccomp_notify_stop_cb cb;
cb = (run_oci_seccomp_notify_stop_cb)dlsym(ctx->plugins[i].handle, "run_oci_seccomp_notify_stop");
if (cb)
cb(ctx->plugins[i].opaque);
dlclose(ctx->plugins[i].handle);
}
}
free(ctx);
return 0;
}
static void cleanup_seccomp_plugins()
{
if (seccomp_notify_ctx) {
seccomp_notify_plugins_free(seccomp_notify_ctx);
seccomp_notify_ctx = NULL;
}
}
void cleanup_seccomp_notify_pluginsp(void *p)
{
struct seccomp_notify_context_s **pp = p;
if (*pp) {
seccomp_notify_plugins_free(*pp);
*pp = NULL;
}
}
static inline void *xmalloc0(size_t size)
{
void *res = calloc(1, size);
if (res == NULL)
pexitf("calloc");
return res;
}
static int seccomp_syscall(unsigned int op, unsigned int flags, void *args)
{
errno = 0;
return syscall(__NR_seccomp, op, flags, args);
}
#else
gboolean seccomp_accept_cb(G_GNUC_UNUSED int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
{
pexit("seccomp support not available");
return G_SOURCE_REMOVE;
}
#endif
|