Skip to content

exec: apply the process OOM score - #2425

Merged
kolyshkin merged 2 commits into
containers:mainfrom
giuseppe:set-oom-score-exec
Oct 30, 2026
Merged

kolyshkin merged 2 commits into
containers:mainfrom
giuseppe:set-oom-score-exec

Conversation

@giuseppe

Copy link
Copy Markdown
Member

Alternative to: #2424

The oomScoreAdj specified in the process passed to "crun exec" was
ignored.  Set it on crun itself before the dumpable flag is dropped,
so that the exec process inherits it, as already done for the rlimits.
Once crun is not dumpable, /proc/self/oom_score_adj is owned by root
and cannot be written by rootless users.

Signed-off-by: Giuseppe Scrivano <giuseppe@scrivano.org>
If the exec process doesn't specify oomScoreAdj, use the value from
the container configuration, as already done for other process
attributes.

Signed-off-by: Giuseppe Scrivano <giuseppe@scrivano.org>
@kolyshkin

kolyshkin commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

I took the liberty of removing the extra libcrun_set_oom_score_adj() and instead added process argument to libcrun_set_oom(). This is how libcrun_set_apparmor_profile() and libcrun_set_selinux_label() already work. The second commit is unchanged.

The change (f92f9993..f67cf174):

diff --git a/src/libcrun/container.c b/src/libcrun/container.c
index 2eeda861..e88619d5 100644
--- a/src/libcrun/container.c
+++ b/src/libcrun/container.c
@@ -4478,7 +4478,7 @@ libcrun_container_exec_with_options (libcrun_context_t *context, const char *id,
 
   /* The exec process inherits it from us.  This must be done before we drop
      the dumpable flag, which makes /proc/self/oom_score_adj owned by root.  */
-  ret = libcrun_set_oom_score_adj (container, process, err);
+  ret = libcrun_set_oom (container, process, err);
   if (UNLIKELY (ret < 0))
     return ret;
 
diff --git a/src/libcrun/linux.c b/src/libcrun/linux.c
index 53cd59fe..6db4cc69 100644
--- a/src/libcrun/linux.c
+++ b/src/libcrun/linux.c
@@ -5307,8 +5307,8 @@ libcrun_set_domainname (libcrun_container_t *container, libcrun_error_t *err)
 }
 
 int
-libcrun_set_oom_score_adj (libcrun_container_t *container, runtime_spec_schema_config_schema_process *process,
-                           libcrun_error_t *err)
+libcrun_set_oom (libcrun_container_t *container, runtime_spec_schema_config_schema_process *process,
+                 libcrun_error_t *err)
 {
   cleanup_close int fd = -1;
   int ret;
@@ -5330,12 +5330,6 @@ libcrun_set_oom_score_adj (libcrun_container_t *container, runtime_spec_schema_c
   return 0;
 }
 
-int
-libcrun_set_oom (libcrun_container_t *container, libcrun_error_t *err)
-{
-  return libcrun_set_oom_score_adj (container, container->container_def->process, err);
-}
-
 const char *sysctlRequiringIPC[] = {
   "kernel/msgmax",
   "kernel/msgmnb",
@@ -6828,7 +6822,7 @@ init_container (libcrun_container_t *container, int sync_socket_container, struc
         return ret;
     }
 
-  ret = libcrun_set_oom (container, err);
+  ret = libcrun_set_oom (container, container->container_def->process, err);
   if (UNLIKELY (ret < 0))
     return ret;
 
@@ -7131,7 +7125,7 @@ libcrun_run_linux_container (libcrun_container_t *container, container_entrypoin
         return ret;
     }
 
-  ret = libcrun_set_oom (container, err);
+  ret = libcrun_set_oom (container, container->container_def->process, err);
   if (UNLIKELY (ret < 0))
     return ret;
 
diff --git a/src/libcrun/linux.h b/src/libcrun/linux.h
index 926ca413..bd7f71f6 100644
--- a/src/libcrun/linux.h
+++ b/src/libcrun/linux.h
@@ -81,9 +81,8 @@ int libcrun_set_selinux_label (libcrun_container_t *container, runtime_spec_sche
 int libcrun_set_apparmor_profile (libcrun_container_t *container, runtime_spec_schema_config_schema_process *proc, bool now, libcrun_error_t *err);
 int libcrun_set_hostname (libcrun_container_t *container, libcrun_error_t *err);
 int libcrun_set_domainname (libcrun_container_t *container, libcrun_error_t *err);
-int libcrun_set_oom (libcrun_container_t *container, libcrun_error_t *err);
-int libcrun_set_oom_score_adj (libcrun_container_t *container, runtime_spec_schema_config_schema_process *process,
-                               libcrun_error_t *err);
+int libcrun_set_oom (libcrun_container_t *container, runtime_spec_schema_config_schema_process *process,
+                     libcrun_error_t *err);
 int libcrun_set_sysctl (libcrun_container_t *container, libcrun_error_t *err);
 int libcrun_set_terminal (libcrun_container_t *container, libcrun_error_t *err);
 int libcrun_join_process (libcrun_context_t *context, libcrun_container_t *container, pid_t pid_to_join,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants