Skip to content

Commit 801d6e8

Browse files
authored
Merge pull request #1647 from giuseppe/status-validate-container-id
status: validate container id
2 parents cdc907b + 432a66d commit 801d6e8

File tree

10 files changed

+193
-92
lines changed

10 files changed

+193
-92
lines changed

lua/lua_crun.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,10 @@ luacrun_ctx_status_container (lua_State *S)
512512
cleanup_container libcrun_container_t *container = NULL;
513513
cleanup_free char *dir = NULL;
514514

515-
dir = libcrun_get_state_directory (state_root, id);
516-
if (dir == NULL)
515+
ret = libcrun_get_state_directory (&dir, state_root, id, &crun_err);
516+
if (UNLIKELY (ret < 0))
517517
{
518+
libcrun_error_release (&crun_err);
518519
lua_pushnil (S);
519520
lua_pushstring (S, "cannot get state directory");
520521
return 2;
@@ -526,6 +527,7 @@ luacrun_ctx_status_container (lua_State *S)
526527
lua_pop (S, 1);
527528
if (container == NULL)
528529
{
530+
libcrun_error_release (&crun_err);
529531
lua_pushnil (S);
530532
lua_pushstring (S, "error loading config.json");
531533
return 2;

src/crun.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,18 @@ static struct argp_option options[] = { { "debug", OPTION_DEBUG, 0, 0, "produce
239239
static void
240240
print_version (FILE *stream, struct argp_state *state arg_unused)
241241
{
242-
cleanup_free char *rundir = libcrun_get_state_directory (arguments.root, NULL);
242+
libcrun_error_t err = NULL;
243+
cleanup_free char *rundir = NULL;
244+
int ret;
245+
246+
ret = libcrun_get_state_directory (&rundir, arguments.root, NULL, &err);
247+
if (UNLIKELY (ret < 0))
248+
{
249+
libcrun_error_release (&err);
250+
fprintf (stderr, "Failed to get state directory\n");
251+
exit (EXIT_FAILURE);
252+
}
253+
243254
fprintf (stream, "%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
244255
fprintf (stream, "commit: %s\n", GIT_VERSION);
245256
fprintf (stream, "rundir: %s\n", rundir);

src/libcrun/cgroup-systemd.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,9 @@ enter_systemd_cgroup_scope (runtime_spec_schema_config_linux_resources *resource
15511551

15521552
*can_retry = false;
15531553

1554-
state_dir = libcrun_get_state_directory (state_root, NULL);
1554+
ret = libcrun_get_state_directory (&state_dir, state_root, NULL, err);
1555+
if (UNLIKELY (ret < 0))
1556+
return ret;
15551557

15561558
i = 0;
15571559
boolean_opts[i++] = "Delegate";
@@ -1937,7 +1939,9 @@ libcrun_update_resources_systemd (struct libcrun_cgroup_status *cgroup_status,
19371939
int sd_err, ret;
19381940
int cgroup_mode;
19391941

1940-
state_dir = libcrun_get_state_directory (state_root, NULL);
1942+
ret = libcrun_get_state_directory (&state_dir, state_root, NULL, err);
1943+
if (UNLIKELY (ret < 0))
1944+
return ret;
19411945

19421946
cgroup_mode = libcrun_get_cgroup_mode (err);
19431947
if (UNLIKELY (cgroup_mode < 0))

src/libcrun/container.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,9 +1604,9 @@ read_container_config_from_state (libcrun_container_t **container, const char *s
16041604

16051605
*container = NULL;
16061606

1607-
dir = libcrun_get_state_directory (state_root, id);
1608-
if (UNLIKELY (dir == NULL))
1609-
return crun_make_error (err, 0, "cannot get state directory from `%s`", state_root);
1607+
ret = libcrun_get_state_directory (&dir, state_root, id, err);
1608+
if (UNLIKELY (ret < 0))
1609+
return ret;
16101610

16111611
ret = append_paths (&config_file, err, dir, "config.json", NULL);
16121612
if (UNLIKELY (ret < 0))
@@ -2041,9 +2041,9 @@ wait_for_process (struct wait_for_process_args *args, libcrun_error_t *err)
20412041
struct libcrun_load_seccomp_notify_conf_s conf;
20422042
memset (&conf, 0, sizeof conf);
20432043

2044-
state_root = libcrun_get_state_directory (args->context->state_root, args->context->id);
2045-
if (UNLIKELY (state_root == NULL))
2046-
return crun_make_error (err, 0, "cannot get state directory");
2044+
ret = libcrun_get_state_directory (&state_root, args->context->state_root, args->context->id, err);
2045+
if (UNLIKELY (ret < 0))
2046+
return ret;
20472047

20482048
ret = append_paths (&oci_config_path, err, state_root, "config.json", NULL);
20492049
if (UNLIKELY (ret < 0))
@@ -2777,9 +2777,9 @@ libcrun_copy_config_file (const char *id, const char *state_root, libcrun_contai
27772777
cleanup_free char *buffer = NULL;
27782778
size_t len;
27792779

2780-
dir = libcrun_get_state_directory (state_root, id);
2781-
if (UNLIKELY (dir == NULL))
2782-
return crun_make_error (err, 0, "cannot get state directory");
2780+
ret = libcrun_get_state_directory (&dir, state_root, id, err);
2781+
if (UNLIKELY (ret < 0))
2782+
return ret;
27832783

27842784
ret = append_paths (&dest_path, err, dir, "config.json", NULL);
27852785
if (UNLIKELY (ret < 0))
@@ -3259,12 +3259,9 @@ libcrun_container_state (libcrun_context_t *context, const char *id, FILE *out,
32593259
cleanup_container libcrun_container_t *container = NULL;
32603260
cleanup_free char *dir = NULL;
32613261

3262-
dir = libcrun_get_state_directory (state_root, id);
3263-
if (UNLIKELY (dir == NULL))
3264-
{
3265-
ret = crun_make_error (err, 0, "cannot get state directory");
3266-
goto exit;
3267-
}
3262+
ret = libcrun_get_state_directory (&dir, state_root, id, err);
3263+
if (UNLIKELY (ret < 0))
3264+
goto exit;
32683265

32693266
ret = append_paths (&config_file, err, dir, "config.json", NULL);
32703267
if (UNLIKELY (ret < 0))
@@ -3598,9 +3595,9 @@ libcrun_container_exec_with_options (libcrun_context_t *context, const char *id,
35983595
return ret;
35993596
container_status = ret;
36003597

3601-
dir = libcrun_get_state_directory (state_root, id);
3602-
if (UNLIKELY (dir == NULL))
3603-
return crun_make_error (err, 0, "cannot get state directory");
3598+
ret = libcrun_get_state_directory (&dir, state_root, id, err);
3599+
if (UNLIKELY (ret < 0))
3600+
return ret;
36043601

36053602
ret = append_paths (&config_file, err, dir, "config.json", NULL);
36063603
if (UNLIKELY (ret < 0))
@@ -4474,9 +4471,9 @@ libcrun_container_update_intel_rdt (libcrun_context_t *context, const char *id,
44744471
cleanup_free char *dir = NULL;
44754472
int ret;
44764473

4477-
dir = libcrun_get_state_directory (context->state_root, id);
4478-
if (UNLIKELY (dir == NULL))
4479-
return crun_make_error (err, 0, "cannot get state directory");
4474+
ret = libcrun_get_state_directory (&dir, context->state_root, id, err);
4475+
if (UNLIKELY (ret < 0))
4476+
return ret;
44804477

44814478
ret = append_paths (&config_file, err, dir, "config.json", NULL);
44824479
if (UNLIKELY (ret < 0))

src/libcrun/handlers/krun.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ libkrun_configure_container (void *cookie, enum handler_configure_phase phase,
195195
cleanup_free char *config = NULL;
196196
size_t config_size;
197197

198-
state_dir = libcrun_get_state_directory (context->state_root, context->id);
199-
if (UNLIKELY (state_dir == NULL))
200-
return crun_make_error (err, 0, "could not retrieve the state directory");
198+
ret = libcrun_get_state_directory (&state_dir, context->state_root, context->id, err);
199+
if (UNLIKELY (ret < 0))
200+
return ret;
201201

202202
ret = append_paths (&origin_config_path, err, state_dir, "config.json", NULL);
203203
if (UNLIKELY (ret < 0))

src/libcrun/linux.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,9 @@ get_notify_fd (libcrun_context_t *context, libcrun_container_t *container, int *
23622362

23632363
if (host_path == NULL)
23642364
{
2365-
state_dir = libcrun_get_state_directory (context->state_root, context->id);
2365+
ret = libcrun_get_state_directory (&state_dir, context->state_root, context->id, err);
2366+
if (UNLIKELY (ret < 0))
2367+
return ret;
23662368

23672369
ret = append_paths (&host_notify_socket_path, err, state_dir, "notify/notify", NULL);
23682370
if (UNLIKELY (ret < 0))
@@ -2406,14 +2408,18 @@ do_notify_socket (libcrun_container_t *container, const char *rootfs, libcrun_er
24062408
const char *notify_socket = container->context->notify_socket;
24072409
cleanup_free char *host_notify_socket_path = NULL;
24082410
cleanup_free char *container_notify_socket_path = NULL;
2409-
cleanup_free char *state_dir = libcrun_get_state_directory (container->context->state_root, container->context->id);
2411+
cleanup_free char *state_dir = NULL;
24102412
uid_t container_root_uid = -1;
24112413
gid_t container_root_gid = -1;
24122414
int notify_socket_tree_fd;
24132415

24142416
if (notify_socket == NULL)
24152417
return 0;
24162418

2419+
ret = libcrun_get_state_directory (&state_dir, container->context->state_root, container->context->id, err);
2420+
if (UNLIKELY (ret < 0))
2421+
return ret;
2422+
24172423
ret = append_paths (&container_notify_socket_path, err, rootfs, notify_socket, "notify", NULL);
24182424
if (UNLIKELY (ret < 0))
24192425
return ret;
@@ -4284,9 +4290,9 @@ prepare_and_send_dev_mounts (libcrun_container_t *container, int sync_socket_hos
42844290
if (! has_userns || is_empty_string (container->context->id) || geteuid () > 0)
42854291
return send_mounts (sync_socket_host, dev_fds, how_many, def->linux->devices_len, err);
42864292

4287-
state_dir = libcrun_get_state_directory (container->context->state_root, container->context->id);
4288-
if (state_dir == NULL)
4289-
return send_mounts (sync_socket_host, dev_fds, how_many, def->linux->devices_len, err);
4293+
ret = libcrun_get_state_directory (&state_dir, container->context->state_root, container->context->id, err);
4294+
if (UNLIKELY (ret < 0))
4295+
return ret;
42904296

42914297
ret = append_paths (&devs_path, err, state_dir, "devs", NULL);
42924298
if (UNLIKELY (ret < 0))

src/libcrun/seccomp.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,11 @@ open_rundir_dirfd (const char *state_root, libcrun_error_t *err)
456456
{
457457
cleanup_free char *dir = NULL;
458458
int dirfd;
459+
int ret;
459460

460-
dir = libcrun_get_state_directory (state_root, NULL);
461-
if (UNLIKELY (dir == NULL))
462-
return crun_make_error (err, 0, "cannot get state directory");
461+
ret = libcrun_get_state_directory (&dir, state_root, NULL, err);
462+
if (UNLIKELY (ret < 0))
463+
return ret;
463464

464465
dirfd = TEMP_FAILURE_RETRY (open (dir, O_PATH | O_DIRECTORY | O_CLOEXEC));
465466
if (UNLIKELY (dirfd < 0))

0 commit comments

Comments
 (0)