[PATCH] qga: keep a trailing newline in authorized_keys

Valentinas Čirba posted 1 patch 2 weeks, 3 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261105114242.35394-1-fizikiukas@gmail.com
Maintainers: Michael Roth <michael.roth@amd.com>, Kostiantyn Kostiuk <kkostiuk@redhat.com>
qga/commands-common-ssh.c  | 12 ++++++++++-
qga/commands-posix-ssh.c   | 41 +++++++++++++++++++++++++++++++++-----
qga/commands-windows-ssh.c |  7 ++++++-
3 files changed, 53 insertions(+), 7 deletions(-)
[PATCH] qga: keep a trailing newline in authorized_keys
Posted by Valentinas Čirba 2 weeks, 3 days ago
From: Valentinas Čirba <valentinas.cirba@hostinger.com>

After guest-ssh-add-authorized-keys the file has an empty line before the
new key and no final newline, so a key appended later with ">>" is
merged into the previous line.

read_authkeys() / g_strsplit leaves a trailing empty element;
write_authkeys() / g_strjoinv adds no final newline.

Both write_authkeys() functions, in qga/commands-posix-ssh.c
and qga/commands-windows-ssh.c, now end non-empty contents
with \n. An empty key list is still written as "".

Also also changes read_authkeys() in qga/commands-common-ssh.c,
so it drops the trailing empty element.

Fixes: 8d769ec777dc ("qga: add ssh-{add,remove}-authorized-keys")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4803
Cc: qemu-stable@nongnu.org
Signed-off-by: Valentinas Čirba <valentinas.cirba@hostinger.com>
---
 qga/commands-common-ssh.c  | 12 ++++++++++-
 qga/commands-posix-ssh.c   | 41 +++++++++++++++++++++++++++++++++-----
 qga/commands-windows-ssh.c |  7 ++++++-
 3 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/qga/commands-common-ssh.c b/qga/commands-common-ssh.c
index 537869fb98..5f5b631689 100644
--- a/qga/commands-common-ssh.c
+++ b/qga/commands-common-ssh.c
@@ -11,13 +11,23 @@ GStrv read_authkeys(const char *path, Error **errp)
 {
     g_autoptr(GError) err = NULL;
     g_autofree char *contents = NULL;
+    GStrv lines;
+    guint len;
 
     if (!g_file_get_contents(path, &contents, NULL, &err)) {
         error_setg(errp, "failed to read '%s': %s", path, err->message);
         return NULL;
     }
 
-    return g_strsplit(contents, "\n", -1);
+    lines = g_strsplit(contents, "\n", -1);
+
+    len = g_strv_length(lines);
+    if (len > 0 && lines[len - 1][0] == '\0') {
+        g_free(lines[len - 1]);
+        lines[len - 1] = NULL;
+    }
+
+    return lines;
 }
 
 bool check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp)
diff --git a/qga/commands-posix-ssh.c b/qga/commands-posix-ssh.c
index aa3411049f..924c5bdeb0 100644
--- a/qga/commands-posix-ssh.c
+++ b/qga/commands-posix-ssh.c
@@ -113,7 +113,12 @@ write_authkeys(const char *path, const GStrv keys,
     g_autofree char *contents = NULL;
     g_autoptr(GError) err = NULL;
 
-    contents = g_strjoinv("\n", keys);
+    if (keys[0] != NULL) {
+        g_autofree char *joined = g_strjoinv("\n", keys);
+        contents = g_strconcat(joined, "\n", NULL);
+    } else {
+        contents = g_strdup("");
+    }
     if (!g_file_set_contents(path, contents, -1, &err)) {
         error_setg(errp, "failed to write to '%s': %s", path, err->message);
         return false;
@@ -402,7 +407,7 @@ test_add_keys(void)
                                       &err);
     g_assert(err == NULL);
 
-    test_authorized_keys_equal("algo key2 comments");
+    test_authorized_keys_equal("algo key2 comments\n");
 
     qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
                                       (strList *)&test_key1_2,
@@ -412,7 +417,7 @@ test_add_keys(void)
 
     /*  key2 came first, and shouldn't be duplicated */
     test_authorized_keys_equal("algo key2 comments\n"
-                               "algo key1 comments");
+                               "algo key1 comments\n");
 }
 
 static void
@@ -428,7 +433,7 @@ test_add_reset_keys(void)
 
     /* reset with key2 only */
     test_authorized_keys_equal("algo key1 comments\n"
-                               "algo key2 comments");
+                               "algo key2 comments\n");
 
     qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
                                       (strList *)&test_key2,
@@ -436,7 +441,7 @@ test_add_reset_keys(void)
                                       &err);
     g_assert(err == NULL);
 
-    test_authorized_keys_equal("algo key2 comments");
+    test_authorized_keys_equal("algo key2 comments\n");
 
     /* empty should clear file */
     qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
@@ -448,6 +453,30 @@ test_add_reset_keys(void)
     test_authorized_keys_equal("");
 }
 
+static void
+test_add_keys_trailing_newline(void)
+{
+    Error *err = NULL;
+
+    test_authorized_keys_set("algo key1 comments\n");
+    qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
+                                      (strList *)&test_key2,
+                                      FALSE, FALSE,
+                                      &err);
+    g_assert(err == NULL);
+    test_authorized_keys_equal("algo key1 comments\n"
+                               "algo key2 comments\n");
+
+    test_authorized_keys_set("algo key1 comments");
+    qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
+                                      (strList *)&test_key2,
+                                      FALSE, FALSE,
+                                      &err);
+    g_assert(err == NULL);
+    test_authorized_keys_equal("algo key1 comments\n"
+                               "algo key2 comments\n");
+}
+
 static void
 test_remove_keys(void)
 {
@@ -507,6 +536,8 @@ int main(int argc, char *argv[])
     g_test_add_func("/qga/ssh/invalid_key", test_invalid_key);
     g_test_add_func("/qga/ssh/add_keys", test_add_keys);
     g_test_add_func("/qga/ssh/add_reset_keys", test_add_reset_keys);
+    g_test_add_func("/qga/ssh/add_keys_trailing_newline",
+                    test_add_keys_trailing_newline);
     g_test_add_func("/qga/ssh/remove_keys", test_remove_keys);
     g_test_add_func("/qga/ssh/get_keys", test_get_keys);
 
diff --git a/qga/commands-windows-ssh.c b/qga/commands-windows-ssh.c
index df45c17b75..f5cf30cc88 100644
--- a/qga/commands-windows-ssh.c
+++ b/qga/commands-windows-ssh.c
@@ -431,7 +431,12 @@ static bool write_authkeys(WindowsUserInfo *userInfo, GStrv authkeys,
     g_autofree char *contents = NULL;
     g_autoptr(GError) err = NULL;
 
-    contents = g_strjoinv("\n", authkeys);
+    if (authkeys[0] != NULL) {
+        g_autofree char *joined = g_strjoinv("\n", authkeys);
+        contents = g_strconcat(joined, "\n", NULL);
+    } else {
+        contents = g_strdup("");
+    }
 
     if (!g_file_set_contents(userInfo->authorizedKeyFile, contents, -1, &err)) {
         error_setg(errp, "failed to write to '%s': %s",
-- 
2.54.0 (Apple Git-157)