richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
923a60
From ddad59f50e67a5e36cd9c40e774d28240a6a7c0c Mon Sep 17 00:00:00 2001
923a60
From: Filipe Brandenburger <filbranden@google.com>
923a60
Date: Wed, 11 Nov 2015 09:24:34 -0800
923a60
Subject: [PATCH] test-execute: Clarify interaction of PassEnvironment= and
923a60
 MANAGER_USER
923a60
923a60
@evverx brought up that test-execute runs under MANAGER_USER which
923a60
forwards all its environment variables to the services. It turns out it
923a60
only forwards those that were in the environment at the time of manager
923a60
creation, so this test was still working.
923a60
923a60
It was still possible to attack it by running something like:
923a60
  $ sudo VAR1=a VAR2=b VAR3=c ./test-execute
923a60
923a60
Prevent that attack by unsetting the three variables explicitly before
923a60
creating the manager for the test case.
923a60
923a60
Also add comments explaining the interactions with MANAGER_USER and,
923a60
while it has some caveats, this tests are still valid in that context.
923a60
923a60
Tested by checking that the test running with the variables set from the
923a60
external environment will still pass.
923a60
923a60
(cherry picked from commit e1abca2ee42e5938ee1f2542c3eba9e70edb0be2)
923a60
923a60
Related: #1426214
923a60
---
923a60
 src/test/test-execute.c | 21 +++++++++++++++++++++
923a60
 1 file changed, 21 insertions(+)
923a60
923a60
diff --git a/src/test/test-execute.c b/src/test/test-execute.c
923a60
index 8def1946dc..6e5567c3ed 100644
923a60
--- a/src/test/test-execute.c
923a60
+++ b/src/test/test-execute.c
923a60
@@ -143,6 +143,17 @@ static void test_exec_environment(Manager *m) {
923a60
 }
923a60
 
923a60
 static void test_exec_passenvironment(Manager *m) {
923a60
+        /* test-execute runs under MANAGER_USER which, by default, forwards all
923a60
+         * variables present in the environment, but only those that are
923a60
+         * present _at the time it is created_!
923a60
+         *
923a60
+         * So these PassEnvironment checks are still expected to work, since we
923a60
+         * are ensuring the variables are not present at manager creation (they
923a60
+         * are unset explicitly in main) and are only set here.
923a60
+         *
923a60
+         * This is still a good approximation of how a test for MANAGER_SYSTEM
923a60
+         * would work.
923a60
+         */
923a60
         assert_se(setenv("VAR1", "word1 word2", 1) == 0);
923a60
         assert_se(setenv("VAR2", "word3", 1) == 0);
923a60
         assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
923a60
@@ -199,6 +210,16 @@ int main(int argc, char *argv[]) {
923a60
         assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
923a60
         assert_se(set_unit_path(TEST_DIR ":") >= 0);
923a60
 
923a60
+        /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
923a60
+         * cases, otherwise (and if they are present in the environment),
923a60
+         * `manager_default_environment` will copy them into the default
923a60
+         * environment which is passed to each created job, which will make the
923a60
+         * tests that expect those not to be present to fail.
923a60
+         */
923a60
+        assert_se(unsetenv("VAR1") == 0);
923a60
+        assert_se(unsetenv("VAR2") == 0);
923a60
+        assert_se(unsetenv("VAR3") == 0);
923a60
+
923a60
         r = manager_new(SYSTEMD_USER, true, &m);
923a60
         if (IN_SET(r, -EPERM, -EACCES, -EADDRINUSE, -EHOSTDOWN, -ENOENT)) {
923a60
                 printf("Skipping test: manager_new: %s", strerror(-r));