qga: Change effective user/group ID in guest-ssh-* commands
PATCH v3 1/1 qga: Change effective user/group ID 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.
v1: https://patchew.org/QEMU/[email protected]/
v2 -> v1:
Change effective user/group ID instead of checking for symlinks and changing ownership of the file.
v2: https://patchew.org/QEMU/[email protected]
v3 -> v2:
Deduplicate code.
Fail daemon when can't rollback effective user/group ID.
--- qga/commands-posix-ssh.c | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
qga/commands-posix-ssh.c +66 -0
@@ -8,11 +8,29 @@#include <glib/gstdio.h> #include <locale.h> #include <pwd.h> +#include <grp.h> #include "commands-common-ssh.h" #include "qapi/error.h" #include "qga-qapi-commands.h" +typedef struct EffectiveUserInfo { +uid_t uid; +gid_t gid; +}EffectiveUserInfo ; + +typedef EffectiveUserInfo *PEffectiveUserInfo ; + +static void rollback_effective_info (PEffectiveUserInfo info) +{ +if (info) { +assert (seteuid (info->uid) ==0 ); +assert (setegid (info->gid) ==0 ); + } +} + +G_DEFINE_AUTO_CLEANUP_FREE_FUNC (PEffectiveUserInfo, rollback_effective_info,NULL ); +#ifdef QGA_BUILD_UNIT_TEST static struct passwd *test_get_passwd_entry (const gchar *user_name,GError **error)
@@ -112,6 +130,36 @@ write_authkeys(const char *path, const GStrv keys,return true; } +static PEffectiveUserInfo set_privileges_to_user (const struct passwd *p,Error **errp) +{ +g_auto (PEffectiveUserInfo) info =g_new0 (EffectiveUserInfo,1 ); + + info->uid =geteuid (); + info->gid =getegid (); + +#ifndef QGA_BUILD_UNIT_TEST +/* The initgroups requires CAP_SETGID. During build time unit tests, we can't do this. */ +if (initgroups (p->pw_name, p->pw_gid) ==-1 ) { +error_setg_errno (errp, errno,"failed to set group for user '%s'" , + p->pw_name); +return NULL ; + } +#endif + +if (setegid (p->pw_gid) ==-1 ) { +error_setg_errno (errp, errno,"failed to set effective group ID for user '%s'" , + p->pw_name); +return NULL ; + } +if (seteuid (p->pw_uid) ==-1 ) { +error_setg_errno (errp, errno,"failed to set effective user ID for user '%s'" , + p->pw_name); +return NULL ; + } + +return g_steal_pointer (&info); +} + void qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, bool has_reset, bool reset,
@@ -123,6 +171,7 @@ qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,g_auto (GStrv) authkeys =NULL ;strList *k;size_t nkeys, nauthkeys; +g_auto (PEffectiveUserInfo) effective_user_info =NULL ; reset = has_reset && reset;
@@ -135,6 +184,11 @@ qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,return ; } + effective_user_info =set_privileges_to_user (p, errp); +if (effective_user_info ==NULL ) { +return ; + } + ssh_path =g_build_filename (p->pw_dir,".ssh" ,NULL ); authkeys_path =g_build_filename (ssh_path,"authorized_keys" ,NULL );
@@ -172,6 +226,7 @@ qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,g_auto (GStrv) authkeys =NULL ;GStrv a;size_t nkeys =0 ; +g_auto (PEffectiveUserInfo) effective_user_info =NULL ;if (!check_openssh_pub_keys (keys,NULL , errp)) {return ;
@@ -182,6 +237,11 @@ qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,return ; } + effective_user_info =set_privileges_to_user (p, errp); +if (effective_user_info ==NULL ) { +return ; + } + authkeys_path =g_build_filename (p->pw_dir,".ssh" ,"authorized_keys" ,NULL );if (!g_file_test (authkeys_path,G_FILE_TEST_EXISTS )) {
@@ -219,12 +279,18 @@ qmp_guest_ssh_get_authorized_keys(const char *username, Error **errp)g_auto (GStrv) authkeys =NULL ;g_autoptr (GuestAuthorizedKeys) ret =NULL ;int i; +g_auto (PEffectiveUserInfo) effective_user_info =NULL ; p =get_passwd_entry (username, errp);if (p ==NULL ) {return NULL ; } + effective_user_info =set_privileges_to_user (p, errp); +if (effective_user_info ==NULL ) { +return NULL ; + } + authkeys_path =g_build_filename (p->pw_dir,".ssh" ,"authorized_keys" ,NULL ); authkeys =read_authkeys (authkeys_path, errp);
Signature · 1 line
2.55.0
Quoted 24 lines · Kostiantyn Kostiuk
Before this commit, when
qmp_guest_ssh_add_authorized_keysadds 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 theauthorized_keysfile. A local unprivileged user who owns their home directory can pre-stage their .ssh directory (or theauthorized_keysfile) 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.Fixes CVE-2026-12080v1: https://patchew.org/QEMU/[email protected]/
v2 -> v1:
Change effective user/group ID instead of checking for symlinks and changing ownership of the file.v2: https://patchew.org/QEMU/[email protected]
v3 -> v2:
Deduplicate code.
Fail daemon when can't rollback effective user/group ID.
Quoted 5 lines
Reported-by Valentino Paulon <[email protected]>Signed-off-by Kostiantyn Kostiuk <[email protected]>--- qga/commands-posix-ssh.c | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
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 :|
Quoted 47 lines · Kostiantyn Kostiuk
Before this commit, when
qmp_guest_ssh_add_authorized_keysadds 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 theauthorized_keysfile. A local unprivileged user who owns their home directory can pre-stage their .ssh directory (or theauthorized_keysfile) 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.Fixes CVE-2026-12080v1: https://patchew.org/QEMU/[email protected]/
v2 -> v1:
Change effective user/group ID instead of checking for symlinks and changing ownership of the file.v2: https://patchew.org/QEMU/[email protected]
v3 -> v2:
Deduplicate code.
Fail daemon when can't rollback effective user/group ID.Reported-by Valentino Paulon <[email protected]>Signed-off-by Kostiantyn Kostiuk <[email protected]>--- qga/commands-posix-ssh.c | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)qga/commands-posix-ssh.c +3
-0@@ -8,11 +8,29 @@#include <glib/gstdio.h> #include <locale.h> #include <pwd.h> +#include <grp.h> #include "commands-common-ssh.h" #include "qapi/error.h" #include "qga-qapi-commands.h" +typedef struct EffectiveUserInfo { + uid_t uid;
qga/commands-posix-ssh.c line 28
Quoted 10 lines
qga/commands-posix-ssh.c +10
-0+gid_t gid; +} EffectiveUserInfo; + +typedef EffectiveUserInfo *PEffectiveUserInfo ; + +static void rollback_effective_info (PEffectiveUserInfo info) +{ +if (info) { +assert (seteuid (info->uid) ==0 ); +assert (setegid (info->gid) ==0 );
A brief comment here about why we're happy to assert() here would be helpful.
qga/commands-posix-ssh.c line 32
Quoted 4 lines
qga/commands-posix-ssh.c +4
-0+ } +} + +G_DEFINE_AUTO_CLEANUP_FREE_FUNC (PEffectiveUserInfo, rollback_effective_info,NULL );
Don't we also need to g_free(info) in the cleanup function?
thanks
-- PMM
Quoted 48 lines · Peter Maydell
Quoted 10 lines · Kostiantyn Kostiuk
Before this commit, when
qmp_guest_ssh_add_authorized_keysadds 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 theauthorized_keysfile. A local unprivileged user who owns their home directory can pre-stage their .ssh directory (or theauthorized_keysfile) 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 thenrewrite
Quoted 35 lines
it to obtain root.
Fixes CVE-2026-12080v1: https://patchew.org/QEMU/[email protected]/
v2 -> v1:
Change effective user/group ID instead of checking for symlinks and changing ownership of the file.v2: https://patchew.org/QEMU/[email protected]
v3 -> v2:
Deduplicate code.
Fail daemon when can't rollback effective user/group ID.Reported-by Valentino Paulon <[email protected]>Signed-off-by Kostiantyn Kostiuk <[email protected]>--- qga/commands-posix-ssh.c | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)qga/commands-posix-ssh.c +1
-0@@ -8,11 +8,29 @@#include <glib/gstdio.h> #include <locale.h> #include <pwd.h> +#include <grp.h> #include "commands-common-ssh.h" #include "qapi/error.h" #include "qga-qapi-commands.h"
Quoted 16 lines
qga/commands-posix-ssh.c line 28
Quoted 12 lines
qga/commands-posix-ssh.c +12
-0+typedef struct EffectiveUserInfo { +uid_t uid; +gid_t gid; +}EffectiveUserInfo ; + +typedef EffectiveUserInfo *PEffectiveUserInfo ; + +static void rollback_effective_info (PEffectiveUserInfo info) +{ +if (info) { +assert (seteuid (info->uid) ==0 ); +assert (setegid (info->gid) ==0 );A brief comment here about why we're happy to
assert()here would be helpful.
Done
Quoted 9 lines
qga/commands-posix-ssh.c
Quoted 5 lines
qga/commands-posix-ssh.c +4
-0+ } +} + +G_DEFINE_AUTO_CLEANUP_FREE_FUNC(PEffectiveUserInfo,
rollback_effective_info, NULL);Don't we also need to
g_free(info)in the cleanup function?
You are right
Quoted 5 lines
thanks
-- PMM