dcavalca / rpms / systemd

Forked from rpms/systemd 4 months ago
Clone
594167
From 75e45d84d9391e29d49f6ec05272c3fb9a92bbd8 Mon Sep 17 00:00:00 2001
594167
From: Lennart Poettering <lennart@poettering.net>
594167
Date: Sat, 19 Feb 2022 00:08:39 +0100
594167
Subject: [PATCH] env-util: replace unsetenv_erase() by new
594167
 getenv_steal_erase() helper
594167
594167
The new helper combines a bunch of steps every invocation of
594167
unsetenv_erase() did so far: getenv() + strdup() + unsetenv_erase().
594167
Let's unify this into one helper that is harder to use incorrectly. It's
594167
in inspired by TAKE_PTR() in a way: get the env var out and invalidate
594167
where it was before.
594167
594167
(cherry picked from commit e99ca1474145f7fad38bb0255d344f4ad7717ef5)
594167
594167
Related: #2087652
594167
---
594167
 src/basic/env-util.c                   | 27 ++++++++++---
594167
 src/basic/env-util.h                   |  2 +
594167
 src/cryptenroll/cryptenroll-password.c | 15 ++-----
594167
 src/cryptenroll/cryptenroll.c          | 20 ++++------
594167
 src/cryptsetup/cryptsetup-fido2.c      | 12 +++---
594167
 src/cryptsetup/cryptsetup.c            | 15 ++++---
594167
 src/home/homectl.c                     | 55 ++++++++++++--------------
594167
 src/shared/pkcs11-util.c               | 11 +++---
594167
 src/test/test-env-util.c               | 16 +++++---
594167
 9 files changed, 89 insertions(+), 84 deletions(-)
594167
594167
diff --git a/src/basic/env-util.c b/src/basic/env-util.c
594167
index 455f5d76f5..b60c9f9fdc 100644
594167
--- a/src/basic/env-util.c
594167
+++ b/src/basic/env-util.c
594167
@@ -857,19 +857,36 @@ int getenv_path_list(const char *name, char ***ret_paths) {
594167
         return 1;
594167
 }
594167
 
594167
-int unsetenv_erase(const char *name) {
594167
-        char *p;
594167
+int getenv_steal_erase(const char *name, char **ret) {
594167
+        _cleanup_(erase_and_freep) char *a = NULL;
594167
+        char *e;
594167
 
594167
         assert(name);
594167
 
594167
-        p = getenv(name);
594167
-        if (!p)
594167
+        /* Reads an environment variable, makes a copy of it, erases its memory in the environment block and removes
594167
+         * it from there. Usecase: reading passwords from the env block (which is a bad idea, but useful for
594167
+         * testing, and given that people are likely going to misuse this, be thorough) */
594167
+
594167
+        e = getenv(name);
594167
+        if (!e) {
594167
+                if (ret)
594167
+                        *ret = NULL;
594167
                 return 0;
594167
+        }
594167
 
594167
-        string_erase(p);
594167
+        if (ret) {
594167
+                a = strdup(e);
594167
+                if (!a)
594167
+                        return -ENOMEM;
594167
+        }
594167
+
594167
+        string_erase(e);
594167
 
594167
         if (unsetenv(name) < 0)
594167
                 return -errno;
594167
 
594167
+        if (ret)
594167
+                *ret = TAKE_PTR(a);
594167
+
594167
         return 1;
594167
 }
594167
diff --git a/src/basic/env-util.h b/src/basic/env-util.h
594167
index 38bfc8a3f2..e4084af9a5 100644
594167
--- a/src/basic/env-util.h
594167
+++ b/src/basic/env-util.h
594167
@@ -70,3 +70,5 @@ int setenv_systemd_exec_pid(bool update_only);
594167
 int getenv_path_list(const char *name, char ***ret_paths);
594167
 
594167
 int unsetenv_erase(const char *name);
594167
+
594167
+int getenv_steal_erase(const char *name, char **ret);
594167
diff --git a/src/cryptenroll/cryptenroll-password.c b/src/cryptenroll/cryptenroll-password.c
594167
index 1775912d8e..9b7c8b5400 100644
594167
--- a/src/cryptenroll/cryptenroll-password.c
594167
+++ b/src/cryptenroll/cryptenroll-password.c
594167
@@ -17,20 +17,13 @@ int enroll_password(
594167
         _cleanup_free_ char *error = NULL;
594167
         const char *node;
594167
         int r, keyslot;
594167
-        char *e;
594167
 
594167
         assert_se(node = crypt_get_device_name(cd));
594167
 
594167
-        e = getenv("NEWPASSWORD");
594167
-        if (e) {
594167
-
594167
-                new_password = strdup(e);
594167
-                if (!new_password)
594167
-                        return log_oom();
594167
-
594167
-                assert_se(unsetenv_erase("NEWPASSWORD") >= 0);
594167
-
594167
-        } else {
594167
+        r = getenv_steal_erase("NEWPASSWORD", &new_password);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire password from environment: %m");
594167
+        if (r == 0) {
594167
                 _cleanup_free_ char *disk_path = NULL;
594167
                 unsigned i = 5;
594167
                 const char *id;
594167
diff --git a/src/cryptenroll/cryptenroll.c b/src/cryptenroll/cryptenroll.c
594167
index ed19f3f8f4..2e11ffe291 100644
594167
--- a/src/cryptenroll/cryptenroll.c
594167
+++ b/src/cryptenroll/cryptenroll.c
594167
@@ -422,8 +422,8 @@ static int prepare_luks(
594167
                 size_t *ret_volume_key_size) {
594167
 
594167
         _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
594167
+        _cleanup_(erase_and_freep) char *envpw = NULL;
594167
         _cleanup_(erase_and_freep) void *vk = NULL;
594167
-        char *e = NULL;
594167
         size_t vks;
594167
         int r;
594167
 
594167
@@ -458,23 +458,17 @@ static int prepare_luks(
594167
         if (!vk)
594167
                 return log_oom();
594167
 
594167
-        e = getenv("PASSWORD");
594167
-        if (e) {
594167
-                _cleanup_(erase_and_freep) char *password = NULL;
594167
-
594167
-                password = strdup(e);
594167
-                if (!password)
594167
-                        return log_oom();
594167
-
594167
-                assert_se(unsetenv_erase("PASSWORD") >= 0);
594167
-
594167
+        r = getenv_steal_erase("PASSWORD", &envpw);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire password from environment: %m");
594167
+        if (r > 0) {
594167
                 r = crypt_volume_key_get(
594167
                                 cd,
594167
                                 CRYPT_ANY_SLOT,
594167
                                 vk,
594167
                                 &vks,
594167
-                                password,
594167
-                                strlen(password));
594167
+                                envpw,
594167
+                                strlen(envpw));
594167
                 if (r < 0)
594167
                         return log_error_errno(r, "Password from environment variable $PASSWORD did not work.");
594167
         } else {
594167
diff --git a/src/cryptsetup/cryptsetup-fido2.c b/src/cryptsetup/cryptsetup-fido2.c
594167
index 35d5dbe007..74053b8ce3 100644
594167
--- a/src/cryptsetup/cryptsetup-fido2.c
594167
+++ b/src/cryptsetup/cryptsetup-fido2.c
594167
@@ -30,12 +30,12 @@ int acquire_fido2_key(
594167
                 size_t *ret_decrypted_key_size,
594167
                 AskPasswordFlags ask_password_flags) {
594167
 
594167
+        _cleanup_(erase_and_freep) char *envpw = NULL;
594167
         _cleanup_strv_free_erase_ char **pins = NULL;
594167
         _cleanup_free_ void *loaded_salt = NULL;
594167
         bool device_exists = false;
594167
         const char *salt;
594167
         size_t salt_size;
594167
-        char *e;
594167
         int r;
594167
 
594167
         ask_password_flags |= ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_ACCEPT_CACHED;
594167
@@ -66,13 +66,13 @@ int acquire_fido2_key(
594167
                 salt = loaded_salt;
594167
         }
594167
 
594167
-        e = getenv("PIN");
594167
-        if (e) {
594167
-                pins = strv_new(e);
594167
+        r = getenv_steal_erase("PIN", &envpw);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire password from environment: %m");
594167
+        if (r > 0) {
594167
+                pins = strv_new(envpw);
594167
                 if (!pins)
594167
                         return log_oom();
594167
-
594167
-                assert_se(unsetenv_erase("PIN") >= 0);
594167
         }
594167
 
594167
         for (;;) {
594167
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
594167
index fc1f37730f..692e1d137b 100644
594167
--- a/src/cryptsetup/cryptsetup.c
594167
+++ b/src/cryptsetup/cryptsetup.c
594167
@@ -829,20 +829,19 @@ static bool libcryptsetup_plugins_support(void) {
594167
 
594167
 #if HAVE_LIBCRYPTSETUP_PLUGINS
594167
 static int acquire_pins_from_env_variable(char ***ret_pins) {
594167
-        char *e;
594167
+        _cleanup_(erase_and_freep) char *envpin = NULL;
594167
         _cleanup_strv_free_erase_ char **pins = NULL;
594167
+        int r;
594167
 
594167
         assert(ret_pins);
594167
 
594167
-        e = getenv("PIN");
594167
-        if (e) {
594167
-                pins = strv_new(e);
594167
+        r = getenv_steal_erase("PIN", &envpin);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
594167
+        if (r > 0) {
594167
+                pins = strv_new(envpin);
594167
                 if (!pins)
594167
                         return log_oom();
594167
-
594167
-                string_erase(e);
594167
-                if (unsetenv("PIN") < 0)
594167
-                        return log_error_errno(errno, "Failed to unset $PIN: %m");
594167
         }
594167
 
594167
         *ret_pins = TAKE_PTR(pins);
594167
diff --git a/src/home/homectl.c b/src/home/homectl.c
594167
index 4aeaaf6b9a..f0d1dac6ab 100644
594167
--- a/src/home/homectl.c
594167
+++ b/src/home/homectl.c
594167
@@ -201,24 +201,25 @@ static int acquire_existing_password(
594167
                 AskPasswordFlags flags) {
594167
 
594167
         _cleanup_(strv_free_erasep) char **password = NULL;
594167
+        _cleanup_(erase_and_freep) char *envpw = NULL;
594167
         _cleanup_free_ char *question = NULL;
594167
-        char *e;
594167
         int r;
594167
 
594167
         assert(user_name);
594167
         assert(hr);
594167
 
594167
-        e = getenv("PASSWORD");
594167
-        if (e) {
594167
+        r = getenv_steal_erase("PASSWORD", &envpw);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire password from environment: %m");
594167
+        if (r > 0) {
594167
                 /* People really shouldn't use environment variables for passing passwords. We support this
594167
                  * only for testing purposes, and do not document the behaviour, so that people won't
594167
                  * actually use this outside of testing. */
594167
 
594167
-                r = user_record_set_password(hr, STRV_MAKE(e), true);
594167
+                r = user_record_set_password(hr, STRV_MAKE(envpw), true);
594167
                 if (r < 0)
594167
                         return log_error_errno(r, "Failed to store password: %m");
594167
 
594167
-                assert_se(unsetenv_erase("PASSWORD") >= 0);
594167
                 return 1;
594167
         }
594167
 
594167
@@ -261,24 +262,25 @@ static int acquire_recovery_key(
594167
                 AskPasswordFlags flags) {
594167
 
594167
         _cleanup_(strv_free_erasep) char **recovery_key = NULL;
594167
+        _cleanup_(erase_and_freep) char *envpw = NULL;
594167
         _cleanup_free_ char *question = NULL;
594167
-        char *e;
594167
         int r;
594167
 
594167
         assert(user_name);
594167
         assert(hr);
594167
 
594167
-        e = getenv("RECOVERY_KEY");
594167
-        if (e) {
594167
+        r = getenv_steal_erase("PASSWORD", &envpw);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire password from environment: %m");
594167
+        if (r > 0) {
594167
                 /* People really shouldn't use environment variables for passing secrets. We support this
594167
                  * only for testing purposes, and do not document the behaviour, so that people won't
594167
                  * actually use this outside of testing. */
594167
 
594167
-                r = user_record_set_password(hr, STRV_MAKE(e), true); /* recovery keys are stored in the record exactly like regular passwords! */
594167
+                r = user_record_set_password(hr, STRV_MAKE(envpw), true); /* recovery keys are stored in the record exactly like regular passwords! */
594167
                 if (r < 0)
594167
                         return log_error_errno(r, "Failed to store recovery key: %m");
594167
 
594167
-                assert_se(unsetenv_erase("RECOVERY_KEY") >= 0);
594167
                 return 1;
594167
         }
594167
 
594167
@@ -318,20 +320,21 @@ static int acquire_token_pin(
594167
                 AskPasswordFlags flags) {
594167
 
594167
         _cleanup_(strv_free_erasep) char **pin = NULL;
594167
+        _cleanup_(erase_and_freep) char *envpin = NULL;
594167
         _cleanup_free_ char *question = NULL;
594167
-        char *e;
594167
         int r;
594167
 
594167
         assert(user_name);
594167
         assert(hr);
594167
 
594167
-        e = getenv("PIN");
594167
-        if (e) {
594167
-                r = user_record_set_token_pin(hr, STRV_MAKE(e), false);
594167
+        r = getenv_steal_erase("PIN", &envpin);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
594167
+        if (r > 0) {
594167
+                r = user_record_set_token_pin(hr, STRV_MAKE(envpin), false);
594167
                 if (r < 0)
594167
                         return log_error_errno(r, "Failed to store token PIN: %m");
594167
 
594167
-                assert_se(unsetenv_erase("PIN") >= 0);
594167
                 return 1;
594167
         }
594167
 
594167
@@ -1147,33 +1150,25 @@ static int acquire_new_password(
594167
                 bool suggest,
594167
                 char **ret) {
594167
 
594167
+        _cleanup_(erase_and_freep) char *envpw = NULL;
594167
         unsigned i = 5;
594167
-        char *e;
594167
         int r;
594167
 
594167
         assert(user_name);
594167
         assert(hr);
594167
 
594167
-        e = getenv("NEWPASSWORD");
594167
-        if (e) {
594167
-                _cleanup_(erase_and_freep) char *copy = NULL;
594167
-
594167
+        r = getenv_steal_erase("NEWPASSWORD", &envpw);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire password from environment: %m");
594167
+        if (r > 0) {
594167
                 /* As above, this is not for use, just for testing */
594167
 
594167
-                if (ret) {
594167
-                        copy = strdup(e);
594167
-                        if (!copy)
594167
-                                return log_oom();
594167
-                }
594167
-
594167
-                r = user_record_set_password(hr, STRV_MAKE(e), /* prepend = */ true);
594167
+                r = user_record_set_password(hr, STRV_MAKE(envpw), /* prepend = */ true);
594167
                 if (r < 0)
594167
                         return log_error_errno(r, "Failed to store password: %m");
594167
 
594167
-                assert_se(unsetenv_erase("NEWPASSWORD") >= 0);
594167
-
594167
                 if (ret)
594167
-                        *ret = TAKE_PTR(copy);
594167
+                        *ret = TAKE_PTR(envpw);
594167
 
594167
                 return 0;
594167
         }
594167
diff --git a/src/shared/pkcs11-util.c b/src/shared/pkcs11-util.c
594167
index 4f9ec1fbd6..80feeb1fae 100644
594167
--- a/src/shared/pkcs11-util.c
594167
+++ b/src/shared/pkcs11-util.c
594167
@@ -275,15 +275,16 @@ int pkcs11_token_login(
594167
 
594167
         for (unsigned tries = 0; tries < 3; tries++) {
594167
                 _cleanup_strv_free_erase_ char **passwords = NULL;
594167
-                char *e;
594167
+                _cleanup_(erase_and_freep) char *envpin = NULL;
594167
 
594167
-                e = getenv("PIN");
594167
-                if (e) {
594167
-                        passwords = strv_new(e);
594167
+                r = getenv_steal_erase("PIN", &envpin);
594167
+                if (r < 0)
594167
+                        return log_error_errno(r, "Failed to acquire PIN from environment: %m");
594167
+                if (r > 0) {
594167
+                        passwords = strv_new(envpin);
594167
                         if (!passwords)
594167
                                 return log_oom();
594167
 
594167
-                        assert_se(unsetenv_erase("PIN") >= 0);
594167
                 } else if (headless)
594167
                         return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the 'PIN' environment variable.");
594167
                 else {
594167
diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c
594167
index 4d5f39b5b7..cc37d96327 100644
594167
--- a/src/test/test-env-util.c
594167
+++ b/src/test/test-env-util.c
594167
@@ -406,16 +406,16 @@ TEST(setenv_systemd_exec_pid) {
594167
         assert_se(set_unset_env("SYSTEMD_EXEC_PID", saved, 1) >= 0);
594167
 }
594167
 
594167
-TEST(unsetenv_erase) {
594167
+TEST(getenv_steal_erase) {
594167
         int r;
594167
 
594167
-        r = safe_fork("(sd-unsetenverase)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
594167
+        r = safe_fork("(sd-getenvstealerase)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
594167
         if (r == 0) {
594167
                 _cleanup_strv_free_ char **l = NULL;
594167
 
594167
                 /* child */
594167
 
594167
-                assert_se(unsetenv_erase("thisenvvardefinitelywontexist") == 0);
594167
+                assert_se(getenv_steal_erase("thisenvvardefinitelywontexist", NULL) == 0);
594167
 
594167
                 l = strv_new("FOO=BAR", "QUUX=PIFF", "ONE=TWO", "A=B");
594167
                 assert_se(strv_length(l) == 4);
594167
@@ -423,7 +423,7 @@ TEST(unsetenv_erase) {
594167
                 environ = l;
594167
 
594167
                 STRV_FOREACH(e, environ) {
594167
-                        _cleanup_free_ char *n = NULL;
594167
+                        _cleanup_free_ char *n = NULL, *copy1 = NULL, *copy2 = NULL;
594167
                         char *eq;
594167
 
594167
                         eq = strchr(*e, '=');
594167
@@ -433,9 +433,13 @@ TEST(unsetenv_erase) {
594167
                         n = strndup(*e, eq - *e);
594167
                         assert_se(n);
594167
 
594167
-                        assert_se(streq_ptr(getenv(n), eq + 1));
594167
+                        copy1 = strdup(eq + 1);
594167
+                        assert_se(copy1);
594167
+
594167
+                        assert_se(streq_ptr(getenv(n), copy1));
594167
                         assert_se(getenv(n) == eq + 1);
594167
-                        assert_se(unsetenv_erase(n) > 0);
594167
+                        assert_se(getenv_steal_erase(n, &copy2) > 0);
594167
+                        assert_se(streq_ptr(copy1, copy2));
594167
                         assert_se(isempty(eq + 1));
594167
                         assert_se(!getenv(n));
594167
                 }