]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: introduce unit_verify_contexts 33016/head
authorMike Yuan <me@yhndnzj.com>
Sat, 25 May 2024 20:43:53 +0000 (04:43 +0800)
committerMike Yuan <me@yhndnzj.com>
Sun, 26 May 2024 05:41:43 +0000 (13:41 +0800)
Refuse WorkingDirectory=~ both in that and exec_invoke()
when dynamic user is used.

src/core/exec-invoke.c
src/core/unit.c
src/test/test-execute.c

index ee62ff3dd42847c9c69777d5d0874cdf4d52c912..e88f524893cae5850d7e0d4464312df9baf43e63 100644 (file)
@@ -3600,6 +3600,9 @@ static int acquire_home(const ExecContext *c, const char **home, char **ret_buf)
         if (!c->working_directory_home)
                 return 0;
 
+        if (c->dynamic_user)
+                return -EADDRNOTAVAIL;
+
         r = get_home_dir(ret_buf);
         if (r < 0)
                 return r;
index 7698f601c742b05e5e4fbdb38a7c31cb204d85f2..1dea0db3fe2192a497e28123f1713561538c00e4 100644 (file)
@@ -4217,6 +4217,21 @@ static int user_from_unit_name(Unit *u, char **ret) {
         return 0;
 }
 
+static int unit_verify_contexts(const Unit *u, const ExecContext *ec) {
+        assert(u);
+
+        if (!ec)
+                return 0;
+
+        if (MANAGER_IS_USER(u->manager) && ec->dynamic_user)
+                return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "DynamicUser= enabled for user unit, which is not supported. Refusing.");
+
+        if (ec->dynamic_user && ec->working_directory_home)
+                return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "WorkingDirectory=~ is not allowed under DynamicUser=yes. Refusing.");
+
+        return 0;
+}
+
 int unit_patch_contexts(Unit *u) {
         CGroupContext *cc;
         ExecContext *ec;
@@ -4340,7 +4355,7 @@ int unit_patch_contexts(Unit *u) {
                 }
         }
 
-        return 0;
+        return unit_verify_contexts(u, ec);
 }
 
 ExecContext *unit_get_exec_context(const Unit *u) {
index 23eefcdf4b06cbd6aaf31671dac7a2ae897ab659..191741d97c2296e4f24eef292ec8c9f4ca7d6ee0 100644 (file)
@@ -987,6 +987,11 @@ static char* private_directory_bad(Manager *m) {
 }
 
 static void test_exec_dynamicuser(Manager *m) {
+        if (MANAGER_IS_USER(m)) {
+                log_notice("Skipping %s for user manager", __func__);
+                return;
+        }
+
         _cleanup_free_ char *bad = private_directory_bad(m);
         if (bad) {
                 log_warning("%s: %s has bad permissions, skipping test.", __func__, bad);
@@ -998,7 +1003,7 @@ static void test_exec_dynamicuser(Manager *m) {
                 return;
         }
 
-        int status = can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NAMESPACE : EXIT_GROUP;
+        int status = can_unshare ? 0 : EXIT_NAMESPACE;
 
         test(m, "exec-dynamicuser-fixeduser.service", status, CLD_EXITED);
         if (check_user_has_group_with_same_name("adm"))