Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
02171f5
container: add json_gen helper macros to reduce boilerplate
giuseppe Aug 13, 2026
c7cbb8f
container: extract gen_annotations helper
giuseppe Aug 13, 2026
4dbcc96
container: extract apply_seccomp_for_exec helper
giuseppe Aug 13, 2026
91d1fab
container: extract write_pid_file helper
giuseppe Aug 13, 2026
beae492
container: extract read_status_require_running helper
giuseppe Aug 13, 2026
5bcea2c
container: reuse read_container_config_from_state for config loads
giuseppe Aug 13, 2026
0a158b4
container: extract write_error_to_pipe helper
giuseppe Aug 13, 2026
a67e280
container: use cleanup attribute for gen in do_hooks
giuseppe Aug 13, 2026
9cdba43
linux: extract write_id_map helper
giuseppe Aug 13, 2026
e72cb49
linux: extract preopen_needed_devs_and_notify helper
giuseppe Aug 13, 2026
141136e
linux: extract apply_propagation_flags from do_mount
giuseppe Aug 13, 2026
6676d3a
linux: extract do_mount_replace_rootfs from do_mount
giuseppe Aug 13, 2026
ceeb426
linux: extract apply_selinux_xattr from do_mount
giuseppe Aug 13, 2026
c6f4dd5
linux: extract schedule_or_do_remount from do_mount
giuseppe Aug 13, 2026
f5997b7
container: fix resource leak in libcrun_container_update
giuseppe Aug 13, 2026
ebbb929
libcrun: move json_gen helper macros to a shared header
giuseppe Aug 13, 2026
db3035f
status: use json_gen helper macros
giuseppe Aug 13, 2026
afb93ee
status: reuse libcrun_get_state_directory for the status file path
giuseppe Aug 13, 2026
cb23573
status: extract get_exec_fifo_path helper
giuseppe Aug 13, 2026
f489b00
cgroup-systemd: remove dead NULL check in append_devices
giuseppe Aug 13, 2026
5fd8004
seccomp: extract open_seccomp_bpf_dirfd helper
giuseppe Aug 13, 2026
5bfc947
exec: extract append_to_string_array helper
giuseppe Aug 13, 2026
45333f0
cgroup-systemd: extract compute_finalized_path helper
giuseppe Aug 13, 2026
698b18e
cgroup-systemd: extract cleanup_sd_bus_and_return helper
giuseppe Aug 13, 2026
48924c7
krun: extract spec_has_device helper
giuseppe Aug 13, 2026
d6bffde
krun: extract stat_optional_device helper
giuseppe Aug 13, 2026
87c9c7f
criu: extract setup_criu_work_dir helper
giuseppe Aug 13, 2026
320bc72
criu: extract parse_cgroup_subsystem helper
giuseppe Aug 13, 2026
88e554f
exec: unify numeric parsing into parse_id_or_fail
giuseppe Aug 13, 2026
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
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ EXTRA_DIST = COPYING COPYING.libcrun README.md NEWS SECURITY.md rpm/crun.spec au
src/libcrun/linux.h src/libcrun/utils.h src/libcrun/error.h src/libcrun/criu.h \
src/libcrun/scheduler.h src/libcrun/mempolicy.h src/libcrun/status.h src/libcrun/terminal.h \
src/libcrun/mount_flags.h src/libcrun/intelrdt.h src/libcrun/ring_buffer.h src/libcrun/string_map.h \
src/libcrun/json_gen_utils.h \
src/libcrun/net_device.h src/libcrun/spec.h \
src/libcrun/syscalls.h \
crun.1.md crun.1 libcrun.lds \
Expand Down
2 changes: 1 addition & 1 deletion src/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
break;

case OPTION_PRESERVE_FDS:
crun_context.preserve_fds = parse_int_or_fail (argp_mandatory_argument (arg, state), "preserve-fds");
crun_context.preserve_fds = parse_id_or_fail (argp_mandatory_argument (arg, state), NULL, "preserve-fds");
break;

case OPTION_NO_SUBREAPER:
Expand Down
25 changes: 17 additions & 8 deletions src/crun.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,20 +389,29 @@ argp_mandatory_argument (char *arg, struct argp_state *state)
return state->argv[state->next++];
}

/* Parse a numeric value from STR, aborting on an out-of-range or malformed
value. KIND names the field for error messages. If ENDPTR is not NULL it
is set past the parsed number (so the caller can continue parsing, e.g. a
"UID:GID" pair); otherwise the whole string must be a valid number. */
int
parse_int_or_fail (const char *str, const char *kind)
parse_id_or_fail (const char *str, char **endptr, const char *kind)
{
char *endptr = NULL;
char *end = NULL;
long long l;

errno = 0;
l = strtoll (str, &endptr, 10);
if (errno != 0)
libcrun_fail_with_error (errno, "invalid value for `%s`", kind);
if (endptr != NULL && *endptr != '\0')
libcrun_fail_with_error (EINVAL, "invalid value for `%s`", kind);
l = strtoll (str, &end, 10);
if (end == str)
libcrun_fail_with_error (0, "invalid %s specified", kind);
if (errno == ERANGE)
libcrun_fail_with_error (0, "invalid %s specified", kind);
if (l < INT_MIN || l > INT_MAX)
libcrun_fail_with_error (ERANGE, "invalid value for `%s`", kind);
libcrun_fail_with_error (0, "invalid %s specified", kind);

if (endptr)
*endptr = end;
else if (*end != '\0')
libcrun_fail_with_error (0, "invalid %s specified", kind);

return (int) l;
}
Expand Down
2 changes: 1 addition & 1 deletion src/crun.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct crun_global_arguments
};

char *argp_mandatory_argument (char *arg, struct argp_state *state);
int parse_int_or_fail (const char *str, const char *kind);
int parse_id_or_fail (const char *str, char **endptr, const char *kind);
int init_libcrun_context (libcrun_context_t *con, const char *id, struct crun_global_arguments *glob,
libcrun_error_t *err);
void crun_assert_n_args (int n, int min, int max);
Expand Down
46 changes: 14 additions & 32 deletions src/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,25 @@ static struct argp_option options[]

static char args_doc[] = "exec CONTAINER cmd";

static void
append_to_string_array (char ***arr, size_t *size, const char *arg)
{
*arr = xrealloc (*arr, (*size + 2) * sizeof (**arr));
(*arr)[*size + 1] = NULL;
(*arr)[*size] = xstrdup (arg);
(*size)++;
}

static void
append_env (const char *arg)
{
exec_options.env = realloc (exec_options.env, (exec_options.env_size + 2) * sizeof (*exec_options.env));
if (exec_options.env == NULL)
error (EXIT_FAILURE, errno, "cannot allocate memory");
exec_options.env[exec_options.env_size + 1] = NULL;
exec_options.env[exec_options.env_size] = xstrdup (arg);
exec_options.env_size++;
append_to_string_array (&exec_options.env, &exec_options.env_size, arg);
}

static void
append_cap (const char *arg)
{
exec_options.cap = realloc (exec_options.cap, (exec_options.cap_size + 2) * sizeof (*exec_options.cap));
if (exec_options.cap == NULL)
error (EXIT_FAILURE, errno, "cannot allocate memory");
exec_options.cap[exec_options.cap_size + 1] = NULL;
exec_options.cap[exec_options.cap_size] = xstrdup (arg);
exec_options.cap_size++;
append_to_string_array (&exec_options.cap, &exec_options.cap_size, arg);
}

static char **
Expand Down Expand Up @@ -152,7 +151,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
break;

case OPTION_PRESERVE_FDS:
exec_options.preserve_fds = parse_int_or_fail (argp_mandatory_argument (arg, state), "preserve-fds");
exec_options.preserve_fds = parse_id_or_fail (argp_mandatory_argument (arg, state), NULL, "preserve-fds");
break;

case OPTION_CGROUP:
Expand Down Expand Up @@ -205,38 +204,21 @@ make_oci_process_user (const char *userspec)
runtime_spec_schema_config_schema_process_user *u;
char *endptr = NULL;
char *gidstr = NULL;
long long l;

if (userspec == NULL)
return NULL;

u = xmalloc0 (sizeof (runtime_spec_schema_config_schema_process_user));
errno = 0;
l = strtoll (userspec, &endptr, 10);
if (endptr == userspec)
libcrun_fail_with_error (0, "invalid UID specified");
if (errno == ERANGE)
libcrun_fail_with_error (0, "invalid UID specified");
if (l < INT_MIN || l > INT_MAX)
libcrun_fail_with_error (0, "invalid UID specified");
u->uid = (int) l;
u->uid = parse_id_or_fail (userspec, &endptr, "UID");
if (*endptr == '\0')
return u;
if (*endptr != ':')
libcrun_fail_with_error (0, "invalid USERSPEC specified");

errno = 0;
gidstr = endptr + 1;
l = strtoll (gidstr, &endptr, 10);
if (endptr == gidstr)
libcrun_fail_with_error (0, "invalid GID specified");
if (errno == ERANGE)
libcrun_fail_with_error (0, "invalid GID specified");
if (l < INT_MIN || l > INT_MAX)
libcrun_fail_with_error (0, "invalid GID specified");
u->gid = parse_id_or_fail (gidstr, &endptr, "GID");
if (*endptr != '\0')
libcrun_fail_with_error (0, "invalid USERSPEC specified");
u->gid = (int) l;
return u;
}

Expand Down
79 changes: 36 additions & 43 deletions src/libcrun/cgroup-systemd.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,18 @@ setup_cpuset_for_systemd_v1 (runtime_spec_schema_config_linux_resources *resourc
return 0;
}

/* Build PATH from the cgroup slice FROM, appending SUFFIX when set. */
static int
compute_finalized_path (char **path, const char *from, const char *suffix, libcrun_error_t *err)
{
if (suffix == NULL)
{
*path = xstrdup (from);
return 0;
}
return append_paths (path, err, from, suffix, NULL);
}

static int
systemd_finalize (struct libcrun_cgroup_args *args, char **path_out,
int cgroup_mode, const char *suffix, libcrun_error_t *err)
Expand Down Expand Up @@ -344,15 +356,10 @@ systemd_finalize (struct libcrun_cgroup_args *args, char **path_out,
if (UNLIKELY (to == NULL))
return crun_make_error (err, 0, "cannot parse `%s`", PROC_SELF_CGROUP);
*to = '\0';
if (suffix == NULL)
path = xstrdup (from);
else
{
ret = append_paths (&path, err, from, suffix, NULL);
if (UNLIKELY (ret < 0))
return ret;
}
ret = compute_finalized_path (&path, from, suffix, err);
*to = '\n';
if (UNLIKELY (ret < 0))
return ret;

if (geteuid ())
return 0;
Expand Down Expand Up @@ -411,15 +418,10 @@ systemd_finalize (struct libcrun_cgroup_args *args, char **path_out,
if (UNLIKELY (to == NULL))
return crun_make_error (err, 0, "cannot parse `%s`", PROC_SELF_CGROUP);
*to = '\0';
if (suffix == NULL)
path = xstrdup (from);
else
{
ret = append_paths (&path, err, from, suffix, NULL);
if (UNLIKELY (ret < 0))
return ret;
}
ret = compute_finalized_path (&path, from, suffix, err);
*to = '\n';
if (UNLIKELY (ret < 0))
return ret;

ret = append_paths (&dir, err, CGROUP_ROOT, path, NULL);
if (UNLIKELY (ret < 0))
Expand Down Expand Up @@ -1326,9 +1328,6 @@ append_devices (sd_bus_message *m,
return ret;
}

if (resources == NULL)
return 0;

for (i = find_first_rule_no_default (resources->devices, resources->devices_len); i < resources->devices_len; i++)
{
runtime_spec_schema_defs_linux_device_cgroup *d = resources->devices[i];
Expand Down Expand Up @@ -1729,6 +1728,21 @@ verify_ebpf_device_filter_installed (const char *cgroup_path, libcrun_error_t *e
return 0;
}

/* Release the sd-bus objects used by a D-Bus call and return RET. */
static int
cleanup_sd_bus_and_return (sd_bus *bus, sd_bus_message *m, sd_bus_message *reply,
sd_bus_error *error, int ret)
{
if (bus)
sd_bus_unref (bus);
if (m)
sd_bus_message_unref (m);
if (reply)
sd_bus_message_unref (reply);
sd_bus_error_free (error);
return ret;
}

static int
enter_systemd_cgroup_scope (runtime_spec_schema_config_linux_resources *resources,
int cgroup_mode,
Expand Down Expand Up @@ -1933,14 +1947,7 @@ enter_systemd_cgroup_scope (runtime_spec_schema_config_linux_resources *resource
ret = systemd_check_job_status (bus, &job_data, object, "creating", err);

exit:
if (bus)
sd_bus_unref (bus);
if (m)
sd_bus_message_unref (m);
if (reply)
sd_bus_message_unref (reply);
sd_bus_error_free (&error);
return ret;
return cleanup_sd_bus_and_return (bus, m, reply, &error, ret);
}

static int
Expand Down Expand Up @@ -1999,14 +2006,7 @@ libcrun_destroy_systemd_cgroup_scope (struct libcrun_cgroup_status *cgroup_statu
reset_failed_unit (bus, scope);

exit:
if (bus)
sd_bus_unref (bus);
if (m)
sd_bus_message_unref (m);
if (reply)
sd_bus_message_unref (reply);
sd_bus_error_free (&error);
return ret;
return cleanup_sd_bus_and_return (bus, m, reply, &error, ret);
}

static const char *
Expand Down Expand Up @@ -2263,14 +2263,7 @@ libcrun_update_resources_systemd (struct libcrun_cgroup_status *cgroup_status,
ret = 0;

exit:
if (bus)
sd_bus_unref (bus);
if (m)
sd_bus_message_unref (m);
if (reply)
sd_bus_message_unref (reply);
sd_bus_error_free (&error);
return ret;
return cleanup_sd_bus_and_return (bus, m, reply, &error, ret);
}

#else
Expand Down
Loading
Loading