Skip to contentSkip to conversation

qga: Do not follow symlink in guest-ssh-* commands

PATCH In review Kostiantyn Kostiuk 3 participants 14 messages Jul 9 – Aug 10

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qga: Do not follow symlink in guest-ssh-* commands

Before this commit, when qmp_guest_ssh_add_authorized_keys adds an SSH key for an existing local user, the agent (running as root) decides whether to create the user's .ssh directory with a symlink-following directory test, and then writes and chowns the authorized_keys file. A local unprivileged user who owns their home directory can pre-stage their .ssh directory (or the authorized_keys file) as a symbolic link so that, when the host or operator triggers a key add for that user, the root agent follows the link and transfers ownership of an arbitrary root-owned file or directory to the unprivileged user, who can then rewrite it to obtain root

---
 qga/commands-posix-ssh.c | 53 ++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 8 deletions(-)
qga/commands-posix-ssh.c +45 -8
index 661972e34e..4e717d8ae8 100644
@@ -66,7 +66,7 @@ mkdir_for_user(const char *path, const struct passwd *p,
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory '%s'",
                          path);
@@ -96,7 +96,7 @@ write_authkeys(const char *path, const GStrv keys,
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory '%s'",
                          path);
@@ -123,6 +123,7 @@ qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
     g_auto(GStrv) authkeys = NULL;
     strList *k;
     size_t nkeys, nauthkeys;
+    int fd;
 
     reset = has_reset && reset;
 
@@ -138,15 +139,25 @@ qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
     ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
     authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
 
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory '%s'", ssh_path);
+            return;
+        }
+    }
+
     if (!reset) {
         authkeys = read_authkeys(authkeys_path, NULL);
     }
     if (authkeys == NULL) {
-        if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
-            !mkdir_for_user(ssh_path, p, 0700, errp)) {
+        if (fd == -1 && !mkdir_for_user(ssh_path, p, 0700, errp)) {
             return;
         }
     }
+    if (fd >= 0) {
+        close(fd);
+    }
 
     nauthkeys = authkeys ? g_strv_length(authkeys) : 0;
     authkeys = g_realloc_n(authkeys, nauthkeys + nkeys + 1, sizeof(char *));
@@ -167,11 +178,13 @@ qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,
                                      Error **errp)
 {
     g_autofree struct passwd *p = NULL;
+    g_autofree char *ssh_path = NULL;
     g_autofree char *authkeys_path = NULL;
     g_autofree GStrv new_keys = NULL; /* do not own the strings */
     g_auto(GStrv) authkeys = NULL;
     GStrv a;
     size_t nkeys = 0;
+    int fd;
 
     if (!check_openssh_pub_keys(keys, NULL, errp)) {
         return;
@@ -182,8 +195,19 @@ qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,
         return;
     }
 
-    authkeys_path = g_build_filename(p->pw_dir, ".ssh",
-                                     "authorized_keys", NULL);
+    ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
+    authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
+
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory '%s'", ssh_path);
+            return;
+        }
+    } else {
+        close(fd);
+    }
+
     if (!g_file_test(authkeys_path, G_FILE_TEST_EXISTS)) {
         return;
     }
@@ -215,18 +239,31 @@ GuestAuthorizedKeys *
 qmp_guest_ssh_get_authorized_keys(const char *username, Error **errp)
 {
     g_autofree struct passwd *p = NULL;
+    g_autofree char *ssh_path = NULL;
     g_autofree char *authkeys_path = NULL;
     g_auto(GStrv) authkeys = NULL;
     g_autoptr(GuestAuthorizedKeys) ret = NULL;
     int i;
+    int fd;
 
     p = get_passwd_entry(username, errp);
     if (p == NULL) {
         return NULL;
     }
 
-    authkeys_path = g_build_filename(p->pw_dir, ".ssh",
-                                     "authorized_keys", NULL);
+    ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
+    authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
+
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory '%s'", ssh_path);
+            return NULL;
+        }
+    } else {
+        close(fd);
+    }
+
     authkeys = read_authkeys(authkeys_path, errp);
     if (authkeys == NULL) {
         return NULL;
Signature · 1 line
2.53.0
#1 lore raw
Quoted 51 lines · Kostiantyn Kostiuk

Before this commit, when qmp_guest_ssh_add_authorized_keys adds an SSH key for an existing local user, the agent (running as root) decides whether to create the user's .ssh directory with a symlink-following directory test, and then writes and chowns the authorized_keys file. A local unprivileged user who owns their home directory can pre-stage their .ssh directory (or the authorized_keys file) as a symbolic link so that, when the host or operator triggers a key add for that user, the root agent follows the link and transfers ownership of an arbitrary root-owned file or directory to the unprivileged user, who can then rewrite it to obtain root

---
 qga/commands-posix-ssh.c | 53 ++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 8 deletions(-)
qga/commands-posix-ssh.c +3 -2
index 661972e34e..4e717d8ae8 100644
@@ -66,7 +66,7 @@ mkdir_for_user(const char *path, const struct passwd *p,
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory '%s'",
                          path);
@@ -96,7 +96,7 @@ write_authkeys(const char *path, const GStrv keys,
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory '%s'",
                          path);
@@ -123,6 +123,7 @@ qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
     g_auto(GStrv) authkeys = NULL;
     strList *k;
     size_t nkeys, nauthkeys;
+    int fd;
 
     reset = has_reset && reset;
 

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 148

Quoted 10 lines
qga/commands-posix-ssh.c +7 -0
@@ -138,15 +139,25 @@ qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
     ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
     authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
 
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory '%s'", ssh_path);
+            return;
+        }
+    }

IIUC, you're trying to protect against the possbility that /home/fred/.ssh is a symlink to some other privileged directory.

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 160

Quoted 13 lines
qga/commands-posix-ssh.c +4 -2
     if (!reset) {
         authkeys = read_authkeys(authkeys_path, NULL);
     }
     if (authkeys == NULL) {
-        if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
-            !mkdir_for_user(ssh_path, p, 0700, errp)) {
+        if (fd == -1 && !mkdir_for_user(ssh_path, p, 0700, errp)) {
             return;
         }
     }
+    if (fd >= 0) {
+        close(fd);
+    }

Does holding open an FD on a directory prevent that directory being altered ? Even if it prevents it being deleted, surely there's still a race where the dir could be renamed, andd .ssh turned back into a symlink ?

Rather than do these checks and switch chown->lchown, I wonder if we are better off having the agent simply change its effective UID/GID while it updates the SSH key files ? That way the agent would be confined just like the user would be and we don't need to implement special cases, nor would we have to think about race conditions.

Quoted 76 lines
qga/commands-posix-ssh.c +30 -4
     nauthkeys = authkeys ? g_strv_length(authkeys) : 0;
     authkeys = g_realloc_n(authkeys, nauthkeys + nkeys + 1, sizeof(char *));
@@ -167,11 +178,13 @@ qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,
                                      Error **errp)
 {
     g_autofree struct passwd *p = NULL;
+    g_autofree char *ssh_path = NULL;
     g_autofree char *authkeys_path = NULL;
     g_autofree GStrv new_keys = NULL; /* do not own the strings */
     g_auto(GStrv) authkeys = NULL;
     GStrv a;
     size_t nkeys = 0;
+    int fd;
 
     if (!check_openssh_pub_keys(keys, NULL, errp)) {
         return;
@@ -182,8 +195,19 @@ qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,
         return;
     }
 
-    authkeys_path = g_build_filename(p->pw_dir, ".ssh",
-                                     "authorized_keys", NULL);
+    ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
+    authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
+
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory '%s'", ssh_path);
+            return;
+        }
+    } else {
+        close(fd);
+    }
+
     if (!g_file_test(authkeys_path, G_FILE_TEST_EXISTS)) {
         return;
     }
@@ -215,18 +239,31 @@ GuestAuthorizedKeys *
 qmp_guest_ssh_get_authorized_keys(const char *username, Error **errp)
 {
     g_autofree struct passwd *p = NULL;
+    g_autofree char *ssh_path = NULL;
     g_autofree char *authkeys_path = NULL;
     g_auto(GStrv) authkeys = NULL;
     g_autoptr(GuestAuthorizedKeys) ret = NULL;
     int i;
+    int fd;
 
     p = get_passwd_entry(username, errp);
     if (p == NULL) {
         return NULL;
     }
 
-    authkeys_path = g_build_filename(p->pw_dir, ".ssh",
-                                     "authorized_keys", NULL);
+    ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
+    authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
+
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory '%s'", ssh_path);
+            return NULL;
+        }
+    } else {
+        close(fd);
+    }
+
     authkeys = read_authkeys(authkeys_path, errp);
     if (authkeys == NULL) {
         return NULL;
Signature · 1 line
2.53.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#2 lore raw
Quoted 86 lines · Daniel P. Berrangé
Quoted 9 lines · Kostiantyn Kostiuk

Before this commit, when qmp_guest_ssh_add_authorized_keys adds an SSH key for an existing local user, the agent (running as root) decides whether to create the user's .ssh directory with a symlink-following directory test, and then writes and chowns the authorized_keys file. A local unprivileged user who owns their home directory can pre-stage their .ssh directory (or the authorized_keys file) as a symbolic link so that, when the host or operator triggers a key add for that user, the root agent follows the link and transfers ownership of an arbitrary root-owned file or directory to the unprivileged user, who can then

rewrite

qga/commands-posix-ssh.c mkdir_for_user(const char *path, const struct passwd

Quoted 16 lines

it to obtain root

---
 qga/commands-posix-ssh.c | 53 ++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 8 deletions(-)
qga/commands-posix-ssh.c +0 -0
index 661972e34e..4e717d8ae8 100644
@@ -66,7 +66,7 @@ mkdir_for_user(const char *path, const struct passwd

*p,

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char

Quoted 18 lines
qga/commands-posix-ssh.c +2 -2
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory '%s'",
                          path);
@@ -96,7 +96,7 @@ write_authkeys(const char *path, const GStrv keys,
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory '%s'",
                          path);
@@ -123,6 +123,7 @@ qmp_guest_ssh_add_authorized_keys(const char

*username, strList *keys,

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char

Quoted 8 lines
qga/commands-posix-ssh.c +1 -0
     g_auto(GStrv) authkeys = NULL;
     strList *k;
     size_t nkeys, nauthkeys;
+    int fd;
 
     reset = has_reset && reset;
 
@@ -138,15 +139,25 @@ qmp_guest_ssh_add_authorized_keys(const char

*username, strList *keys,

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,

Quoted 7 lines
qga/commands-posix-ssh.c +4 -0
     ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
     authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
 
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory

'%s'", ssh_path);

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 148

Quoted 3 lines
qga/commands-posix-ssh.c +3 -0
+            return;
+        }
+    }

IIUC, you're trying to protect against the possbility that /home/fred/.ssh is
a symlink to some other privileged directory.

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 160

Quoted 13 lines
qga/commands-posix-ssh.c +4 -2
     if (!reset) {
         authkeys = read_authkeys(authkeys_path, NULL);
     }
     if (authkeys == NULL) {
-        if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
-            !mkdir_for_user(ssh_path, p, 0700, errp)) {
+        if (fd == -1 && !mkdir_for_user(ssh_path, p, 0700, errp)) {
             return;
         }
     }
+    if (fd >= 0) {
+        close(fd);
+    }
Quoted 5 lines

Does holding open an FD on a directory prevent that directory being altered ? Even if it prevents it being deleted, surely there's still a race where the dir could be renamed, andd .ssh turned back into a symlink ?

Yes, you are right; race is possible

Quoted 8 lines

Rather than do these checks and switch chown->lchown, I wonder if we are better off having the agent simply change its effective UID/GID while it updates the SSH key files ? That way the agent would be confined just like the user would be and we don't need to implement special cases, nor would we have to think about race conditions.

So, you propose to call seteuid/setegid before any I/O operation?

Best Regards,
Kostiantyn Kostiuk.

Quoted 90 lines

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char · line 161

Quoted 3 lines
qga/commands-posix-ssh.c +0 -0
     nauthkeys = authkeys ? g_strv_length(authkeys) : 0;
     authkeys = g_realloc_n(authkeys, nauthkeys + nkeys + 1, sizeof(char

*));

qga/commands-posix-ssh.c qmp_guest_ssh_remove_authorized_keys(const char

Quoted 1 line
qga/commands-posix-ssh.c +0 -0
@@ -167,11 +178,13 @@ qmp_guest_ssh_remove_authorized_keys(const char

*username, strList *keys,

qga/commands-posix-ssh.c qmp_guest_ssh_remove_authorized_keys(const char

Quoted 14 lines
qga/commands-posix-ssh.c +2 -0
                                      Error **errp)
 {
     g_autofree struct passwd *p = NULL;
+    g_autofree char *ssh_path = NULL;
     g_autofree char *authkeys_path = NULL;
     g_autofree GStrv new_keys = NULL; /* do not own the strings */
     g_auto(GStrv) authkeys = NULL;
     GStrv a;
     size_t nkeys = 0;
+    int fd;
 
     if (!check_openssh_pub_keys(keys, NULL, errp)) {
         return;
@@ -182,8 +195,19 @@ qmp_guest_ssh_remove_authorized_keys(const char

*username, strList *keys,

qga/commands-posix-ssh.c qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,

Quoted 12 lines
qga/commands-posix-ssh.c +7 -2
         return;
     }
 
-    authkeys_path = g_build_filename(p->pw_dir, ".ssh",
-                                     "authorized_keys", NULL);
+    ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
+    authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
+
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory

'%s'", ssh_path);

Quoted 24 lines
qga/commands-posix-ssh.c +8 -0
+            return;
+        }
+    } else {
+        close(fd);
+    }
+
     if (!g_file_test(authkeys_path, G_FILE_TEST_EXISTS)) {
         return;
     }
@@ -215,18 +239,31 @@ GuestAuthorizedKeys *
 qmp_guest_ssh_get_authorized_keys(const char *username, Error **errp)
 {
     g_autofree struct passwd *p = NULL;
+    g_autofree char *ssh_path = NULL;
     g_autofree char *authkeys_path = NULL;
     g_auto(GStrv) authkeys = NULL;
     g_autoptr(GuestAuthorizedKeys) ret = NULL;
     int i;
+    int fd;
 
     p = get_passwd_entry(username, errp);
     if (p == NULL) {
         return NULL;
     }

qga/commands-posix-ssh.c GuestAuthorizedKeys * · line 260

Quoted 10 lines
qga/commands-posix-ssh.c +7 -2
 
-    authkeys_path = g_build_filename(p->pw_dir, ".ssh",
-                                     "authorized_keys", NULL);
+    ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
+    authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
+
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory

'%s'", ssh_path);

Quoted 10 lines
qga/commands-posix-ssh.c +6 -1
+            return NULL;
+        }
+    } else {
+        close(fd);
+    }
+
     authkeys = read_authkeys(authkeys_path, errp);
     if (authkeys == NULL) {
         return NULL;
--
Quoted 2 lines

2.53.0

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#3 lore raw
Quoted 99 lines · Kostiantyn Kostiuk
Quoted 86 lines · Daniel P. Berrangé
Quoted 9 lines · Kostiantyn Kostiuk

Before this commit, when qmp_guest_ssh_add_authorized_keys adds an SSH key for an existing local user, the agent (running as root) decides whether to create the user's .ssh directory with a symlink-following directory test, and then writes and chowns the authorized_keys file. A local unprivileged user who owns their home directory can pre-stage their .ssh directory (or the authorized_keys file) as a symbolic link so that, when the host or operator triggers a key add for that user, the root agent follows the link and transfers ownership of an arbitrary root-owned file or directory to the unprivileged user, who can then

rewrite

qga/commands-posix-ssh.c mkdir_for_user(const char *path, const struct passwd

Quoted 16 lines

it to obtain root

---
 qga/commands-posix-ssh.c | 53 ++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 8 deletions(-)
qga/commands-posix-ssh.c +0 -0
index 661972e34e..4e717d8ae8 100644
@@ -66,7 +66,7 @@ mkdir_for_user(const char *path, const struct passwd

*p,

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char

Quoted 18 lines
qga/commands-posix-ssh.c +2 -2
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory '%s'",
                          path);
@@ -96,7 +96,7 @@ write_authkeys(const char *path, const GStrv keys,
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory '%s'",
                          path);
@@ -123,6 +123,7 @@ qmp_guest_ssh_add_authorized_keys(const char

*username, strList *keys,

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char

Quoted 8 lines
qga/commands-posix-ssh.c +1 -0
     g_auto(GStrv) authkeys = NULL;
     strList *k;
     size_t nkeys, nauthkeys;
+    int fd;
 
     reset = has_reset && reset;
 
@@ -138,15 +139,25 @@ qmp_guest_ssh_add_authorized_keys(const char

*username, strList *keys,

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,

Quoted 7 lines
qga/commands-posix-ssh.c +4 -0
     ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
     authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
 
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory

'%s'", ssh_path);

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 148

Quoted 3 lines
qga/commands-posix-ssh.c +3 -0
+            return;
+        }
+    }

IIUC, you're trying to protect against the possbility that /home/fred/.ssh is
a symlink to some other privileged directory.

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 160

Quoted 13 lines
qga/commands-posix-ssh.c +4 -2
     if (!reset) {
         authkeys = read_authkeys(authkeys_path, NULL);
     }
     if (authkeys == NULL) {
-        if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
-            !mkdir_for_user(ssh_path, p, 0700, errp)) {
+        if (fd == -1 && !mkdir_for_user(ssh_path, p, 0700, errp)) {
             return;
         }
     }
+    if (fd >= 0) {
+        close(fd);
+    }
Quoted 5 lines

Does holding open an FD on a directory prevent that directory being altered ? Even if it prevents it being deleted, surely there's still a race where the dir could be renamed, andd .ssh turned back into a symlink ?

Yes, you are right; race is possible

Quoted 1 line
Quoted 8 lines
Quoted 7 lines

Rather than do these checks and switch chown->lchown, I wonder if we are better off having the agent simply change its effective UID/GID while it updates the SSH key files ? That way the agent would be confined just like the user would be and we don't need to implement special cases, nor would we have to think about race conditions.

So, you propose to call seteuid/setegid before any I/O operation?

Yes, specifically for the SSH commands, because they're unusual in that we're doing stuff on behalf of an unprivileged user.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#4 lore raw
Quoted 4 lines
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O operation?

Yes, specifically for the SSH commands, because they're unusual in that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the outcome I was hoping for. It confines every step (read, mkdir, create, rename, chown) to what the target user could already do himself, which removes the whole TOCTOU class instead of chasing one instance of it.

A few implementation details that tend to bite with this pattern, in case they save a round-trip:

- drop the supplementary groups too, not just egid: initgroups() (or setgroups() with the user's list) while still root, otherwise the agent's effective access won't match the user's in either direction -- e.g. group-writable paths under the home.
- keep the real uid at 0 and change euid/egid only, so it stays reversible; setegid() before seteuid() on the way down, restore in reverse.
- effective ids are process-wide, so this assumes the SSH command is not doing its I/O from more than one thread concurrently -- worth a quick check against the agent's dispatch model.

The passwd lookup already runs before any of this, so it can stay outside the dropped region.

Happy to re-run my PoC against the reworked version once it's ready and confirm the window is closed.

Regards,
Valentino Paulon

Quoted 129 lines · Daniel P. Berrangé
Quoted 6 lines · Kostiantyn Kostiuk
Quoted 3 lines · Daniel P. Berrangé
Quoted 2 lines · Kostiantyn Kostiuk

Before this commit, when qmp_guest_ssh_add_authorized_keys adds an SSH key for an existing local user, the agent (running as root)

decides

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

whether to create the user's .ssh directory with a symlink-following directory test, and then writes and chowns the authorized_keys file. A local unprivileged user who owns their home directory can pre-stage their .ssh directory (or the authorized_keys file) as a symbolic link so that, when the host or operator triggers a key add for that user, the root agent follows the link and transfers ownership of an

arbitrary

Quoted 11 lines
Quoted 11 lines
Quoted 1 line

root-owned file or directory to the unprivileged user, who can then

rewrite

Quoted 9 lines

it to obtain root

---
 qga/commands-posix-ssh.c | 53

++++++++++++++++++++++++++++++++++------

Quoted 7 lines
Quoted 7 lines

qga/commands-posix-ssh.c mkdir_for_user(const char *path, const struct

Quoted 7 lines
 1 file changed, 45 insertions(+), 8 deletions(-)
qga/commands-posix-ssh.c +0 -0
index 661972e34e..4e717d8ae8 100644
@@ -66,7 +66,7 @@ mkdir_for_user(const char *path, const struct

passwd

Quoted 8 lines
Quoted 8 lines

*p,

Quoted 7 lines
qga/commands-posix-ssh.c +1 -1
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory

'%s'",

Quoted 9 lines
Quoted 9 lines

qga/commands-posix-ssh.c write_authkeys(const char *path, const GStrv keys, · line 101

Quoted 9 lines
qga/commands-posix-ssh.c +1 -1
                          path);
@@ -96,7 +96,7 @@ write_authkeys(const char *path, const GStrv keys,
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory

'%s'",

Quoted 8 lines
Quoted 8 lines

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char

Quoted 2 lines
qga/commands-posix-ssh.c +0 -0
                          path);
@@ -123,6 +123,7 @@ qmp_guest_ssh_add_authorized_keys(const char

*username, strList *keys,

Quoted 5 lines
qga/commands-posix-ssh.c +1 -0
     g_auto(GStrv) authkeys = NULL;
     strList *k;
     size_t nkeys, nauthkeys;
+    int fd;
 
Quoted 6 lines
Quoted 6 lines

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char

Quoted 3 lines
qga/commands-posix-ssh.c +0 -0
     reset = has_reset && reset;
 
@@ -138,15 +139,25 @@ qmp_guest_ssh_add_authorized_keys(const char

*username, strList *keys,

Quoted 2 lines
qga/commands-posix-ssh.c +0 -0
     ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
     authkeys_path = g_build_filename(ssh_path, "authorized_keys",

NULL);

Quoted 11 lines
Quoted 11 lines

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,

Quoted 5 lines
qga/commands-posix-ssh.c +4 -0
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory

'%s'", ssh_path);

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 148

Quoted 3 lines
qga/commands-posix-ssh.c +3 -0
+            return;
+        }
+    }

IIUC, you're trying to protect against the possbility that

/home/fred/.ssh

Quoted 27 lines
Quoted 17 lines

is
a symlink to some other privileged directory.

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 160

Quoted 13 lines
qga/commands-posix-ssh.c +4 -2
     if (!reset) {
         authkeys = read_authkeys(authkeys_path, NULL);
     }
     if (authkeys == NULL) {
-        if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
-            !mkdir_for_user(ssh_path, p, 0700, errp)) {
+        if (fd == -1 && !mkdir_for_user(ssh_path, p, 0700, errp)) {
             return;
         }
     }
+    if (fd >= 0) {
+        close(fd);
+    }
Quoted 5 lines

Does holding open an FD on a directory prevent that directory being altered ? Even if it prevents it being deleted, surely there's still a race where the dir could be renamed, andd .ssh turned back into a symlink ?

Yes, you are right; race is possible

Quoted 1 line
Quoted 8 lines
Quoted 7 lines

Rather than do these checks and switch chown->lchown, I wonder if we are better off having the agent simply change its effective UID/GID while it updates the SSH key files ? That way the agent would be confined just like the user would be and we don't need to implement special cases, nor would we have to think about race conditions.

So, you propose to call seteuid/setegid before any I/O operation?

Yes, specifically for the SSH commands, because they're unusual in that we're doing stuff on behalf of an unprivileged user.

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#5 lore raw

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 10 lines · Valentino Paulon
Quoted 4 lines
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O operation?

Yes, specifically for the SSH commands, because they're unusual in that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the outcome I was hoping for. It confines every step (read, mkdir, create, rename, chown) to what the target user could already do himself, which removes the whole TOCTOU class instead of chasing one instance of it.

Quoted 7 lines

A few implementation details that tend to bite with this pattern, in case they save a round-trip:

- drop the supplementary groups too, not just egid: initgroups() (or setgroups() with the user's list) while still root, otherwise the agent's effective access won't match the user's in either direction

Technically, there is no limitation to running QGA in unprivileged mode. Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller must have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do you think?

Quoted 8 lines

-- e.g. group-writable paths under the home.
- keep the real uid at 0 and change euid/egid only, so it stays reversible; setegid() before seteuid() on the way down, restore in reverse.
- effective ids are process-wide, so this assumes the SSH command is not doing its I/O from more than one thread concurrently -- worth a quick check against the agent's dispatch model.

No issue there. All QGA commands are synchronous.

Quoted 10 lines

The passwd lookup already runs before any of this, so it can stay outside the dropped region.

Happy to re-run my PoC against the reworked version once it's ready and confirm the window is closed.

Regards,
Valentino Paulon

Best Regards,
Kostiantyn Kostiuk.

Quoted 136 lines
Quoted 132 lines · Daniel P. Berrangé
Quoted 7 lines · Kostiantyn Kostiuk

On Tue, Jul 14, 2026 at 1:37 PM Daniel P. Berrangé <[email protected]

Quoted 3 lines · wrote:
Quoted 2 lines · Kostiantyn Kostiuk

Before this commit, when qmp_guest_ssh_add_authorized_keys adds an SSH key for an existing local user, the agent (running as root)

decides

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

whether to create the user's .ssh directory with a symlink-following directory test, and then writes and chowns the authorized_keys file. A local unprivileged user who owns their home directory can

pre-stage

Quoted 1 line
Quoted 1 line
Quoted 1 line

their .ssh directory (or the authorized_keys file) as a symbolic

link

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

so that, when the host or operator triggers a key add for that user, the root agent follows the link and transfers ownership of an

arbitrary

Quoted 11 lines
Quoted 11 lines
Quoted 1 line

root-owned file or directory to the unprivileged user, who can then

rewrite

Quoted 9 lines

it to obtain root

---
 qga/commands-posix-ssh.c | 53

++++++++++++++++++++++++++++++++++------

Quoted 7 lines
Quoted 7 lines

qga/commands-posix-ssh.c mkdir_for_user(const char *path, const struct

Quoted 7 lines
 1 file changed, 45 insertions(+), 8 deletions(-)
qga/commands-posix-ssh.c +0 -0
index 661972e34e..4e717d8ae8 100644
@@ -66,7 +66,7 @@ mkdir_for_user(const char *path, const struct

passwd

Quoted 8 lines
Quoted 8 lines

*p,

Quoted 7 lines
qga/commands-posix-ssh.c +1 -1
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory

'%s'",

Quoted 9 lines
Quoted 9 lines

qga/commands-posix-ssh.c write_authkeys(const char *path, const GStrv keys, · line 101

Quoted 9 lines
qga/commands-posix-ssh.c +1 -1
                          path);
@@ -96,7 +96,7 @@ write_authkeys(const char *path, const GStrv keys,
         return false;
     }
 
-    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+    if (lchown(path, p->pw_uid, p->pw_gid) == -1) {
         error_setg_errno(errp, errno,
                          "failed to set ownership of directory

'%s'",

Quoted 8 lines
Quoted 8 lines

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char

Quoted 2 lines
qga/commands-posix-ssh.c +0 -0
                          path);
@@ -123,6 +123,7 @@ qmp_guest_ssh_add_authorized_keys(const char

*username, strList *keys,

Quoted 5 lines
qga/commands-posix-ssh.c +1 -0
     g_auto(GStrv) authkeys = NULL;
     strList *k;
     size_t nkeys, nauthkeys;
+    int fd;
 
Quoted 6 lines
Quoted 6 lines

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char

Quoted 3 lines
qga/commands-posix-ssh.c +0 -0
     reset = has_reset && reset;
 
@@ -138,15 +139,25 @@ qmp_guest_ssh_add_authorized_keys(const char

*username, strList *keys,

Quoted 2 lines
qga/commands-posix-ssh.c +0 -0
     ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
     authkeys_path = g_build_filename(ssh_path, "authorized_keys",

NULL);

Quoted 11 lines
Quoted 11 lines

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,

Quoted 5 lines
qga/commands-posix-ssh.c +4 -0
+    fd = open(ssh_path, O_DIRECTORY | O_NOFOLLOW);
+    if (fd == -1) {
+        if (errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to open directory

'%s'", ssh_path);

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 148

Quoted 3 lines
qga/commands-posix-ssh.c +3 -0
+            return;
+        }
+    }

IIUC, you're trying to protect against the possbility that

/home/fred/.ssh

Quoted 27 lines
Quoted 17 lines

is
a symlink to some other privileged directory.

qga/commands-posix-ssh.c qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, · line 160

Quoted 13 lines
qga/commands-posix-ssh.c +4 -2
     if (!reset) {
         authkeys = read_authkeys(authkeys_path, NULL);
     }
     if (authkeys == NULL) {
-        if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
-            !mkdir_for_user(ssh_path, p, 0700, errp)) {
+        if (fd == -1 && !mkdir_for_user(ssh_path, p, 0700, errp)) {
             return;
         }
     }
+    if (fd >= 0) {
+        close(fd);
+    }
Quoted 5 lines

Does holding open an FD on a directory prevent that directory being altered ? Even if it prevents it being deleted, surely there's still a race where the dir could be renamed, andd .ssh turned back into a symlink ?

Yes, you are right; race is possible

Quoted 1 line
Quoted 8 lines
Quoted 7 lines

Rather than do these checks and switch chown->lchown, I wonder if we are better off having the agent simply change its effective UID/GID while it updates the SSH key files ? That way the agent would be confined just like the user would be and we don't need to implement special cases, nor would we have to think about race conditions.

So, you propose to call seteuid/setegid before any I/O operation?

Yes, specifically for the SSH commands, because they're unusual in that we're doing stuff on behalf of an unprivileged user.

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#6 lore raw
Quoted 26 lines · Kostiantyn Kostiuk

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 10 lines · Valentino Paulon
Quoted 4 lines
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O operation?

Yes, specifically for the SSH commands, because they're unusual in that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the outcome I was hoping for. It confines every step (read, mkdir, create, rename, chown) to what the target user could already do himself, which removes the whole TOCTOU class instead of chasing one instance of it.

Quoted 7 lines

A few implementation details that tend to bite with this pattern, in case they save a round-trip:

- drop the supplementary groups too, not just egid: initgroups() (or setgroups() with the user's list) while still root, otherwise the agent's effective access won't match the user's in either direction

Quoted 5 lines

Technically, there is no limitation to running QGA in unprivileged mode. Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller must have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do you think?

In any practical sense it is a system daemon given the set of commands it is exposing. Thus, IMHO, deploying as an unprivileged user should be considered a broken deployment. We could make that explicit by refusing to launch.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#7 lore raw
Quoted 33 lines · Daniel P. Berrangé
Quoted 26 lines · Kostiantyn Kostiuk

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 10 lines · Valentino Paulon
Quoted 4 lines
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O operation?

Yes, specifically for the SSH commands, because they're unusual in that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the outcome I was hoping for. It confines every step (read, mkdir, create, rename, chown) to what the target user could already do himself, which removes the whole TOCTOU class instead of chasing one instance of it.

Quoted 7 lines

A few implementation details that tend to bite with this pattern, in case they save a round-trip:

- drop the supplementary groups too, not just egid: initgroups() (or setgroups() with the user's list) while still root, otherwise the agent's effective access won't match the user's in either direction

Quoted 5 lines

Technically, there is no limitation to running QGA in unprivileged mode. Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller must have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do you think?

Quoted 5 lines

In any practical sense it is a system daemon given the set of commands it is exposing. Thus, IMHO, deploying as an unprivileged user should be considered a broken deployment. We could make that explicit by refusing to launch.

Makes sense, but we have one more issue with QGA unit tests in this case. They are called by an unprivileged user (build user), and initgroups broke it.
Is it possible to enter the namespace automatically by the test engine?

Quoted 8 lines

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#8 lore raw
Quoted 42 lines · Kostiantyn Kostiuk
Quoted 33 lines · Daniel P. Berrangé
Quoted 26 lines · Kostiantyn Kostiuk

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 10 lines · Valentino Paulon
Quoted 4 lines
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O operation?

Yes, specifically for the SSH commands, because they're unusual in that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the outcome I was hoping for. It confines every step (read, mkdir, create, rename, chown) to what the target user could already do himself, which removes the whole TOCTOU class instead of chasing one instance of it.

Quoted 7 lines

A few implementation details that tend to bite with this pattern, in case they save a round-trip:

- drop the supplementary groups too, not just egid: initgroups() (or setgroups() with the user's list) while still root, otherwise the agent's effective access won't match the user's in either direction

Quoted 5 lines

Technically, there is no limitation to running QGA in unprivileged mode. Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller must have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do you think?

Quoted 5 lines

In any practical sense it is a system daemon given the set of commands it is exposing. Thus, IMHO, deploying as an unprivileged user should be considered a broken deployment. We could make that explicit by refusing to launch.

Quoted 4 lines

Makes sense, but we have one more issue with QGA unit tests in this case. They are called by an unprivileged user (build user), and initgroups broke it.
Is it possible to enter the namespace automatically by the test engine?

We can't assume that is possible todo in all build/test environments.

Doing things as different user accounts feels pretty dubious for the scope of a unit test too - that is really functional or integration testing.

Can we somehow restructure the tests and/or agent code to avoid the changing groups stuff in unit tests.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#9 lore raw
Quoted 58 lines · Daniel P. Berrangé
Quoted 14 lines · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 11:20 AM Daniel P. Berrangé <[email protected]

Quoted 10 lines · wrote:
Quoted 9 lines · Kostiantyn Kostiuk

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 1 line · Valentino Paulon
Quoted 1 line
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O

operation?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Yes, specifically for the SSH commands, because they're unusual

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the

outcome I

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

was hoping for. It confines every step (read, mkdir, create,

rename,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

chown) to what the target user could already do himself, which

removes

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

the whole TOCTOU class instead of chasing one instance of it.

A few implementation details that tend to bite with this pattern,

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

case they save a round-trip:

- drop the supplementary groups too, not just egid: initgroups()

(or

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

setgroups() with the user's list) while still root, otherwise the agent's effective access won't match the user's in either direction

Technically, there is no limitation to running QGA in unprivileged

mode.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller must have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do you think?

Quoted 8 lines
Quoted 5 lines

In any practical sense it is a system daemon given the set of commands it is exposing. Thus, IMHO, deploying as an unprivileged user should be considered a broken deployment. We could make that explicit by refusing to launch.

Makes sense, but we have one more issue with QGA unit tests in this case. They are called by an unprivileged user (build user), and initgroups

broke

Quoted 2 lines

it.
Is it possible to enter the namespace automatically by the test engine?

Quoted 9 lines

We can't assume that is possible todo in all build/test environments.

Doing things as different user accounts feels pretty dubious for the scope of a unit test too - that is really functional or integration testing.

Can we somehow restructure the tests and/or agent code to avoid the changing groups stuff in unit tests.

There are two possible options:
1. add #ifdef QGA_BUILD_UNIT_TEST into qmp_guest_ssh_* functions
2. check the current uid and skip initgroups if it is zero (my initial question)

Quoted 9 lines

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#10 lore raw
Quoted 67 lines · Kostiantyn Kostiuk
Quoted 64 lines · Daniel P. Berrangé
Quoted 14 lines · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 11:20 AM Daniel P. Berrangé <[email protected]

Quoted 10 lines · wrote:
Quoted 9 lines · Kostiantyn Kostiuk

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 1 line · Valentino Paulon
Quoted 1 line
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O

operation?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Yes, specifically for the SSH commands, because they're unusual

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the

outcome I

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

was hoping for. It confines every step (read, mkdir, create,

rename,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

chown) to what the target user could already do himself, which

removes

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

the whole TOCTOU class instead of chasing one instance of it.

A few implementation details that tend to bite with this pattern,

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

case they save a round-trip:

- drop the supplementary groups too, not just egid: initgroups()

(or

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

setgroups() with the user's list) while still root, otherwise the agent's effective access won't match the user's in either direction

Technically, there is no limitation to running QGA in unprivileged

mode.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller must have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do you think?

Quoted 8 lines
Quoted 5 lines

In any practical sense it is a system daemon given the set of commands it is exposing. Thus, IMHO, deploying as an unprivileged user should be considered a broken deployment. We could make that explicit by refusing to launch.

Makes sense, but we have one more issue with QGA unit tests in this case. They are called by an unprivileged user (build user), and initgroups

broke

Quoted 2 lines

it.
Is it possible to enter the namespace automatically by the test engine?

We can't assume that is possible todo in all build/test environments.

Doing things as different user accounts feels pretty dubious for the scope of a unit test too - that is really functional or integration testing.

Quoted 8 lines
Quoted 3 lines

Can we somehow restructure the tests and/or agent code to avoid the changing groups stuff in unit tests.

There are two possible options:
1. add #ifdef QGA_BUILD_UNIT_TEST into qmp_guest_ssh_* functions
2. check the current uid and skip initgroups if it is zero (my initial question)

Surely you mean "if it is non-zero" here, as it is with uid!=0 that the UID changes will fail.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#11 lore raw
Quoted 85 lines · Daniel P. Berrangé
Quoted 6 lines · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 11:55 AM Daniel P. Berrangé <[email protected]

Quoted 2 lines · wrote:
Quoted 1 line · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 11:20 AM Daniel P. Berrangé <

[email protected]

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines · wrote:
Quoted 5 lines · Kostiantyn Kostiuk

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line · Valentino Paulon
Quoted 1 line
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O

operation?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Yes, specifically for the SSH commands, because they're

unusual

Quoted 4 lines
Quoted 4 lines

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the

Quoted 8 lines
Quoted 8 lines

outcome I

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

was hoping for. It confines every step (read, mkdir, create,

rename,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

chown) to what the target user could already do himself, which

removes

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

the whole TOCTOU class instead of chasing one instance of it.

A few implementation details that tend to bite with this

pattern,

Quoted 4 lines
Quoted 4 lines

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

case they save a round-trip:

- drop the supplementary groups too, not just egid:

initgroups()

Quoted 2 lines
Quoted 2 lines

(or

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

setgroups() with the user's list) while still root, otherwise

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

agent's effective access won't match the user's in either

direction

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Technically, there is no limitation to running QGA in

unprivileged

Quoted 5 lines
Quoted 5 lines

mode.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller must have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do you

think?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In any practical sense it is a system daemon given the set of

commands

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

it is exposing. Thus, IMHO, deploying as an unprivileged user

should

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

be considered a broken deployment. We could make that explicit by refusing to launch.

Makes sense, but we have one more issue with QGA unit tests in this

case.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

They are called by an unprivileged user (build user), and initgroups

broke

Quoted 2 lines

it.
Is it possible to enter the namespace automatically by the test

engine?

Quoted 11 lines
Quoted 10 lines

We can't assume that is possible todo in all build/test environments.

Doing things as different user accounts feels pretty dubious for the scope of a unit test too - that is really functional or integration testing.

Can we somehow restructure the tests and/or agent code to avoid the changing groups stuff in unit tests.

Quoted 8 lines
Quoted 4 lines

There are two possible options:
1. add #ifdef QGA_BUILD_UNIT_TEST into qmp_guest_ssh_* functions
2. check the current uid and skip initgroups if it is zero (my initial question)

Surely you mean "if it is non-zero" here, as it is with uid!=0 that the UID changes will fail.

yes. So what sounds better: "ifdef" or "uid!=0"?

Best Regards,
Kostiantyn Kostiuk.

Quoted 9 lines

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#12 lore raw
Quoted 93 lines · Kostiantyn Kostiuk
Quoted 90 lines · Daniel P. Berrangé
Quoted 6 lines · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 11:55 AM Daniel P. Berrangé <[email protected]

Quoted 2 lines · wrote:
Quoted 1 line · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 11:20 AM Daniel P. Berrangé <

[email protected]

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines · wrote:
Quoted 5 lines · Kostiantyn Kostiuk

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line · Valentino Paulon
Quoted 1 line
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O

operation?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Yes, specifically for the SSH commands, because they're

unusual

Quoted 4 lines
Quoted 4 lines

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the

Quoted 8 lines
Quoted 8 lines

outcome I

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

was hoping for. It confines every step (read, mkdir, create,

rename,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

chown) to what the target user could already do himself, which

removes

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

the whole TOCTOU class instead of chasing one instance of it.

A few implementation details that tend to bite with this

pattern,

Quoted 4 lines
Quoted 4 lines

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

case they save a round-trip:

- drop the supplementary groups too, not just egid:

initgroups()

Quoted 2 lines
Quoted 2 lines

(or

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

setgroups() with the user's list) while still root, otherwise

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

agent's effective access won't match the user's in either

direction

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Technically, there is no limitation to running QGA in

unprivileged

Quoted 5 lines
Quoted 5 lines

mode.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller must have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do you

think?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In any practical sense it is a system daemon given the set of

commands

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

it is exposing. Thus, IMHO, deploying as an unprivileged user

should

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

be considered a broken deployment. We could make that explicit by refusing to launch.

Makes sense, but we have one more issue with QGA unit tests in this

case.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

They are called by an unprivileged user (build user), and initgroups

broke

Quoted 2 lines

it.
Is it possible to enter the namespace automatically by the test

engine?

Quoted 15 lines
Quoted 10 lines

We can't assume that is possible todo in all build/test environments.

Doing things as different user accounts feels pretty dubious for the scope of a unit test too - that is really functional or integration testing.

Can we somehow restructure the tests and/or agent code to avoid the changing groups stuff in unit tests.

There are two possible options:
1. add #ifdef QGA_BUILD_UNIT_TEST into qmp_guest_ssh_* functions
2. check the current uid and skip initgroups if it is zero (my initial question)

Quoted 5 lines
Quoted 3 lines

Surely you mean "if it is non-zero" here, as it is with uid!=0 that the UID changes will fail.

yes. So what sounds better: "ifdef" or "uid!=0"?

I'd be inclined to #ifdef so we know it doesn't affect production deployments, only tests.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#13 lore raw

Version 2 with setegid/seteuid logic pushed
Please review and test

Quoted 127 lines · Daniel P. Berrangé
Quoted 6 lines · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 12:28 PM Daniel P. Berrangé <[email protected]

Quoted 2 lines · wrote:
Quoted 1 line · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 11:55 AM Daniel P. Berrangé <

[email protected]

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines · wrote:
Quoted 1 line · Kostiantyn Kostiuk

On Mon, Aug 10, 2026 at 11:20 AM Daniel P. Berrangé <

[email protected]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line · wrote:

On Mon, Aug 10, 2026 at 11:06:27AM +0300, Kostiantyn Kostiuk

Quoted 11 lines · wrote:
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Hi Valentino,

Sorry for the long delay.
I have a question for you

Quoted 1 line · Valentino Paulon
Quoted 1 line
Quoted 1 line

So, you propose to call seteuid/setegid before any I/O

operation?

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Yes, specifically for the SSH commands, because they're

unusual

Quoted 6 lines
Quoted 6 lines

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

that we're doing stuff on behalf of an unprivileged user.

+1 on the euid/egid direction -- as the reporter that's the

outcome I

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

was hoping for. It confines every step (read, mkdir,

create,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

rename,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

chown) to what the target user could already do himself,

which

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

removes

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the whole TOCTOU class instead of chasing one instance of

it.

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

A few implementation details that tend to bite with this

pattern,

Quoted 4 lines
Quoted 4 lines

in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

case they save a round-trip:

- drop the supplementary groups too, not just egid:

initgroups()

Quoted 2 lines
Quoted 2 lines

(or

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

setgroups() with the user's list) while still root,

otherwise

Quoted 10 lines
Quoted 10 lines

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

agent's effective access won't match the user's in either

direction

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Technically, there is no limitation to running QGA in

unprivileged

Quoted 3 lines
Quoted 3 lines

mode.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Normally, it is a system daemon, but this is not mandatory. In this case, we can not call initgroups because the caller

must

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

have CAP_SETGID.
We can call initgroups only when QGA runs as root. What do

you

Quoted 7 lines
Quoted 7 lines

think?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In any practical sense it is a system daemon given the set of

commands

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

it is exposing. Thus, IMHO, deploying as an unprivileged user

should

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

be considered a broken deployment. We could make that explicit

by

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

refusing to launch.

Makes sense, but we have one more issue with QGA unit tests in

this

Quoted 2 lines
Quoted 2 lines

case.

Quoted 1 line
Quoted 1 line
Quoted 1 line

They are called by an unprivileged user (build user), and

initgroups

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

broke

Quoted 2 lines

it.
Is it possible to enter the namespace automatically by the test

engine?

Quoted 2 lines
Quoted 2 lines

We can't assume that is possible todo in all build/test

environments.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Doing things as different user accounts feels pretty dubious for

the

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines

scope of a unit test too - that is really functional or integration testing.

Can we somehow restructure the tests and/or agent code to avoid the changing groups stuff in unit tests.

There are two possible options:
1. add #ifdef QGA_BUILD_UNIT_TEST into qmp_guest_ssh_* functions
2. check the current uid and skip initgroups if it is zero (my

initial

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

question)

Surely you mean "if it is non-zero" here, as it is with uid!=0 that the UID changes will fail.

yes. So what sounds better: "ifdef" or "uid!=0"?

I'd be inclined to #ifdef so we know it doesn't affect production deployments, only tests.

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|