ryantimwilson / rpms / systemd

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