Skip to content

Commit a1c0ef1

Browse files
authored
Merge pull request #627 from giuseppe/seccomp-notifications-oci
seccomp: support notify listener
2 parents add0730 + 9aa382b commit a1c0ef1

File tree

25 files changed

+836
-89
lines changed

25 files changed

+836
-89
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ TESTS = tests/test_capabilities.py \
129129
tests/test_resources.py \
130130
tests/test_start.py \
131131
tests/test_exec.py \
132+
tests/test_seccomp.py \
132133
$(UNIT_TESTS)
133134

134135
.version:

contrib/seccomp-receiver/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CFLAGS = -I../..
2+
seccomp-receiver: seccomp-receiver.c
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* crun - OCI runtime written in C
3+
*
4+
* Copyright (C) 2021 Giuseppe Scrivano <[email protected]>
5+
* crun is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* crun is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with crun. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#define _GNU_SOURCE
20+
21+
#include <config.h>
22+
#include <stdarg.h>
23+
#include <stdlib.h>
24+
#include <sys/un.h>
25+
#include <sys/socket.h>
26+
#include <errno.h>
27+
#include <unistd.h>
28+
#include <fcntl.h>
29+
#include <sys/ioctl.h>
30+
#include <termios.h>
31+
#include <stdio.h>
32+
33+
#define error(status, errno, fmt, ...) do { \
34+
if (errno) \
35+
fprintf (stderr, "crun: " fmt, ##__VA_ARGS__); \
36+
else \
37+
fprintf (stderr, "crun: %s:" fmt, strerror (errno), ##__VA_ARGS__); \
38+
if (status) \
39+
exit (status); \
40+
} while(0)
41+
42+
static int
43+
open_unix_domain_socket (const char *path)
44+
{
45+
struct sockaddr_un addr = {};
46+
int ret;
47+
int fd = socket (AF_UNIX, SOCK_STREAM, 0);
48+
if (fd < 0)
49+
error (EXIT_FAILURE, errno, "error creating UNIX socket");
50+
51+
strcpy (addr.sun_path, path);
52+
addr.sun_family = AF_UNIX;
53+
ret = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
54+
if (ret < 0)
55+
error (EXIT_FAILURE, errno, "error binding UNIX socket");
56+
57+
ret = listen (fd, 1);
58+
if (ret < 0)
59+
error (EXIT_FAILURE, errno, "listen");
60+
61+
return fd;
62+
}
63+
64+
static void
65+
print_payload (int from)
66+
{
67+
int fd = -1;
68+
struct iovec iov[1];
69+
struct msghdr msg = {};
70+
char ctrl_buf[2048] = {};
71+
char data[2048];
72+
int ret;
73+
struct cmsghdr *cmsg;
74+
75+
iov[0].iov_base = data;
76+
iov[0].iov_len = sizeof (data) - 1;
77+
78+
msg.msg_name = NULL;
79+
msg.msg_namelen = 0;
80+
msg.msg_iov = iov;
81+
msg.msg_iovlen = 1;
82+
msg.msg_controllen = CMSG_SPACE (sizeof (int));
83+
msg.msg_control = ctrl_buf;
84+
85+
do
86+
ret = recvmsg (from, &msg, 0);
87+
while (ret < 0 && errno == EINTR);
88+
if (ret < 0)
89+
{
90+
error (0, errno, "recvmsg");
91+
return;
92+
}
93+
94+
data[iov[0].iov_len] = '\0';
95+
puts (data);
96+
97+
cmsg = CMSG_FIRSTHDR (&msg);
98+
if (cmsg == NULL)
99+
{
100+
error (0, 0, "no msg received");
101+
return;
102+
}
103+
memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
104+
close (fd);
105+
}
106+
107+
int
108+
main (int argc, char **argv)
109+
{
110+
char buf[8192];
111+
int ret, fd, socket;
112+
if (argc < 2)
113+
error (EXIT_FAILURE, 0, "usage %s PATH\n", argv[0]);
114+
115+
unlink (argv[1]);
116+
117+
socket = open_unix_domain_socket (argv[1]);
118+
while (1)
119+
{
120+
struct termios tset;
121+
int conn;
122+
123+
do
124+
conn = accept (socket, NULL, NULL);
125+
while (conn < 0 && errno == EINTR);
126+
if (conn < 0)
127+
error (EXIT_FAILURE, errno, "accept");
128+
129+
print_payload (conn);
130+
}
131+
132+
return 0;
133+
}

src/create.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static struct argp run_argp = { options, parse_opt, args_doc, doc, NULL, NULL, N
114114
int
115115
crun_command_create (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err)
116116
{
117-
int first_arg, ret;
117+
int first_arg = 0, ret;
118118
cleanup_container libcrun_container_t *container = NULL;
119119
cleanup_free char *bundle_cleanup = NULL;
120120
cleanup_free char *config_file_cleanup = NULL;
@@ -138,7 +138,9 @@ crun_command_create (struct crun_global_arguments *global_args, int argc, char *
138138
}
139139

140140
/* Make sure the bundle is an absolute path. */
141-
if (bundle)
141+
if (bundle == NULL)
142+
bundle = bundle_cleanup = getcwd (NULL, 0);
143+
else
142144
{
143145
if (bundle[0] != '/')
144146
{
@@ -160,7 +162,7 @@ crun_command_create (struct crun_global_arguments *global_args, int argc, char *
160162
if (container == NULL)
161163
libcrun_fail_with_error (0, "error loading config.json");
162164

163-
crun_context.bundle = bundle ? bundle : ".";
165+
crun_context.bundle = bundle;
164166
if (getenv ("LISTEN_FDS"))
165167
crun_context.preserve_fds += strtoll (getenv ("LISTEN_FDS"), NULL, 10);
166168

src/crun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ int
297297
main (int argc, char **argv)
298298
{
299299
libcrun_error_t err = NULL;
300-
int ret, first_argument;
300+
int ret, first_argument = 0;
301301

302302
argp_program_version_hook = print_version;
303303

src/delete.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static struct argp run_argp = { options, parse_opt, args_doc, doc, NULL, NULL, N
8686
int
8787
crun_command_delete (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err)
8888
{
89-
int first_arg, ret;
89+
int first_arg = 0, ret;
9090

9191
libcrun_context_t crun_context = {
9292
0,

src/exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ make_oci_process_user (const char *userspec)
202202
int
203203
crun_command_exec (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err)
204204
{
205-
int first_arg, ret = 0;
205+
int first_arg = 0, ret = 0;
206206
libcrun_context_t crun_context = {
207207
0,
208208
};

src/kill.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static struct argp run_argp = { options, parse_opt, args_doc, doc, NULL, NULL, N
8787
int
8888
crun_command_kill (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err)
8989
{
90-
int first_arg, signal, ret;
90+
int first_arg = 0, signal, ret;
9191

9292
libcrun_context_t crun_context = {
9393
0,

src/libcrun/cgroup.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,9 @@ read_pids_cgroup (int dfd, bool recurse, pid_t **pids, size_t *n_pids, size_t *a
18191819
if (UNLIKELY (ret < 0))
18201820
return ret;
18211821

1822+
if (len == 0)
1823+
return 0;
1824+
18221825
for (n_new_pids = 0, it = buffer; it; it = strchr (it + 1, '\n'))
18231826
n_new_pids++;
18241827

0 commit comments

Comments
 (0)