Skip to content

Commit 0d28e58

Browse files
committed
seccomp: fix n_plugins calculation
and make sure we do not leak resources on errors. Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent b75d7e4 commit 0d28e58

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/libcrun/seccomp_notify.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ libcrun_load_seccomp_notify_plugins (struct seccomp_notify_context_s **out, cons
9595
ctx->sreq = xmalloc (ctx->sizes.seccomp_notif);
9696
ctx->sresp = xmalloc (ctx->sizes.seccomp_notif_resp);
9797

98+
b = xstrdup (plugins);
99+
98100
ctx->n_plugins = 1;
99-
for (it = b; it; it = strchr (it, ':'))
100-
ctx->n_plugins++;
101+
for (it = b; *it; it++)
102+
if (*it == ':')
103+
ctx->n_plugins++;
101104

102105
ctx->plugins = xmalloc0 (sizeof (struct plugin) * (ctx->n_plugins + 1));
103-
104-
b = xstrdup (plugins);
105106
for (s = 0, it = strtok_r (b, ":", &saveptr); it; s++, it = strtok_r (NULL, ":", &saveptr))
106107
{
107108
run_oci_seccomp_notify_plugin_version_cb version_cb;
@@ -254,6 +255,7 @@ libcrun_free_seccomp_notify_plugins (struct seccomp_notify_context_s *ctx, libcr
254255
dlclose (ctx->plugins[i].handle);
255256
}
256257

258+
free (ctx->plugins);
257259
free (ctx);
258260

259261
return 0;

tests/tests_libcrun_seccomp_notify.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,27 @@ test_load_nonexistent_plugin ()
114114
return 0;
115115
}
116116

117+
/* Test load with multiple non-existent plugins to verify cleanup of n_plugins. */
118+
static int
119+
test_load_multiple_nonexistent_plugins ()
120+
{
121+
libcrun_error_t err = NULL;
122+
struct seccomp_notify_context_s *ctx = NULL;
123+
int ret;
124+
125+
ret = libcrun_load_seccomp_notify_plugins (&ctx, "/nonexistent/p1.so:/nonexistent/p2.so:/nonexistent/p3.so", NULL, &err);
126+
if (ret >= 0)
127+
{
128+
if (ctx)
129+
libcrun_free_seccomp_notify_plugins (ctx, &err);
130+
crun_error_release (&err);
131+
return -1;
132+
}
133+
134+
crun_error_release (&err);
135+
return 0;
136+
}
137+
117138
/* Test seccomp_notify_plugins returns error without seccomp support */
118139
static int
119140
test_notify_no_seccomp ()
@@ -167,11 +188,12 @@ int
167188
main ()
168189
{
169190
int id = 1;
170-
printf ("1..5\n");
191+
printf ("1..6\n");
171192
RUN_TEST (test_cleanup_null);
172193
RUN_TEST (test_free_null_context);
173194
RUN_TEST (test_load_invalid_path);
174195
RUN_TEST (test_load_nonexistent_plugin);
196+
RUN_TEST (test_load_multiple_nonexistent_plugins);
175197
RUN_TEST (test_notify_no_seccomp);
176198
return 0;
177199
}

0 commit comments

Comments
 (0)