From nobody Wed Jul 15 04:55:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1783594663; cv=none; d=zohomail.com; s=zohoarc; b=SCmI772aKXcmECOBma4m3rQgYA3ut0+w9DLHedE/lvBeijarix9d6qYSRhkTOcLoVxUMmBT7M4qCU517k0MGBSU+PfbBIoSLGZXplyUAIeUnpN+/zsFhrWo6wBcnXkgsvTxkMlbdemvuR4lYbxYUgYiFlQeAsBin30RKhsV+4dE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783594663; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=HE5KFOnCzp23735KR1LCbTJaqRC1GzvULBi35G3mceQ=; b=WcSmO0+HTx9cre0NAEd1AYIDfZdyrPlgGdjUi94RfQVOmR8C8jAZPkEdFFK0h8rOSzUw/PpIKaCVeF7mYmQH7y1Hb/XnAlLAT69LGraPMA5BEDKZPhIBvbq2SJfa4hudWr18nD47+gF5TzgFv/W/Qc2RFI2WxgptiSzHQN2Ux9s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17835946633751020.1369171162681; Thu, 9 Jul 2026 03:57:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whmRW-0001Uq-P4; Thu, 09 Jul 2026 06:57:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whmRV-0001U0-37 for qemu-devel@nongnu.org; Thu, 09 Jul 2026 06:57:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whmRS-0004P2-RM for qemu-devel@nongnu.org; Thu, 09 Jul 2026 06:57:20 -0400 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-575-4mRrgQ29PxySnEfMkFMXkQ-1; Thu, 09 Jul 2026 06:57:13 -0400 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6DB261955F1E; Thu, 9 Jul 2026 10:57:12 +0000 (UTC) Received: from srv1.redhat.com (unknown [10.44.33.20]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 703221955D71; Thu, 9 Jul 2026 10:57:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1783594636; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=HE5KFOnCzp23735KR1LCbTJaqRC1GzvULBi35G3mceQ=; b=bEO24VLtvWwh1/nprInBE5mKAv6x1wIV0G+lNSsnfFr+YqU2ovvctzJWUnT4/OyBQi02O0 imPDIfCNr5RWQEJkQD9AqPWENFLTIKjRV/9C3owfTx6FHTiD9zQf/IYVhl3GIZ5WU/Ss2/ Hfm8YIg99Q3QrunqiPWwAc0FeND/QHg= X-MC-Unique: 4mRrgQ29PxySnEfMkFMXkQ-1 X-Mimecast-MFC-AGG-ID: 4mRrgQ29PxySnEfMkFMXkQ_1783594632 From: Kostiantyn Kostiuk To: qemu-devel@nongnu.org Cc: Valentino Paulon , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Yan Vugenfirer , Kostiantyn Kostiuk , Michael Roth Subject: [PATCH] qga: Do not follow symlink in guest-ssh-* commands Date: Thu, 9 Jul 2026 13:57:07 +0300 Message-ID: <20260709105707.91209-1-kkostiuk@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kkostiuk@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 8 X-Spam_score: 0.8 X-Spam_bar: / X-Spam_report: (0.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.445, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1783594665675158500 Content-Type: text/plain; charset="utf-8" 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 Fixes: CVE-2026-12080 Fixes: https://gitlab.com/qemu-project/qemu/-/work_items/3929 Reported-by: Valentino Paulon Signed-off-by: Kostiantyn Kostiuk --- qga/commands-posix-ssh.c | 53 ++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/qga/commands-posix-ssh.c b/qga/commands-posix-ssh.c index 661972e34e..4e717d8ae8 100644 --- a/qga/commands-posix-ssh.c +++ b/qga/commands-posix-ssh.c @@ -66,7 +66,7 @@ mkdir_for_user(const char *path, const struct passwd *p, return false; } =20 - if (chown(path, p->pw_uid, p->pw_gid) =3D=3D -1) { + if (lchown(path, p->pw_uid, p->pw_gid) =3D=3D -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; } =20 - if (chown(path, p->pw_uid, p->pw_gid) =3D=3D -1) { + if (lchown(path, p->pw_uid, p->pw_gid) =3D=3D -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 =3D NULL; strList *k; size_t nkeys, nauthkeys; + int fd; =20 reset =3D has_reset && reset; =20 @@ -138,15 +139,25 @@ qmp_guest_ssh_add_authorized_keys(const char *usernam= e, strList *keys, ssh_path =3D g_build_filename(p->pw_dir, ".ssh", NULL); authkeys_path =3D g_build_filename(ssh_path, "authorized_keys", NULL); =20 + fd =3D open(ssh_path, O_DIRECTORY | O_NOFOLLOW); + if (fd =3D=3D -1) { + if (errno !=3D ENOENT) { + error_setg_errno(errp, errno, "failed to open directory '%s'",= ssh_path); + return; + } + } + if (!reset) { authkeys =3D read_authkeys(authkeys_path, NULL); } if (authkeys =3D=3D NULL) { - if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) && - !mkdir_for_user(ssh_path, p, 0700, errp)) { + if (fd =3D=3D -1 && !mkdir_for_user(ssh_path, p, 0700, errp)) { return; } } + if (fd >=3D 0) { + close(fd); + } =20 nauthkeys =3D authkeys ? g_strv_length(authkeys) : 0; authkeys =3D g_realloc_n(authkeys, nauthkeys + nkeys + 1, sizeof(char = *)); @@ -167,11 +178,13 @@ qmp_guest_ssh_remove_authorized_keys(const char *user= name, strList *keys, Error **errp) { g_autofree struct passwd *p =3D NULL; + g_autofree char *ssh_path =3D NULL; g_autofree char *authkeys_path =3D NULL; g_autofree GStrv new_keys =3D NULL; /* do not own the strings */ g_auto(GStrv) authkeys =3D NULL; GStrv a; size_t nkeys =3D 0; + int fd; =20 if (!check_openssh_pub_keys(keys, NULL, errp)) { return; @@ -182,8 +195,19 @@ qmp_guest_ssh_remove_authorized_keys(const char *usern= ame, strList *keys, return; } =20 - authkeys_path =3D g_build_filename(p->pw_dir, ".ssh", - "authorized_keys", NULL); + ssh_path =3D g_build_filename(p->pw_dir, ".ssh", NULL); + authkeys_path =3D g_build_filename(ssh_path, "authorized_keys", NULL); + + fd =3D open(ssh_path, O_DIRECTORY | O_NOFOLLOW); + if (fd =3D=3D -1) { + if (errno !=3D 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 =3D NULL; + g_autofree char *ssh_path =3D NULL; g_autofree char *authkeys_path =3D NULL; g_auto(GStrv) authkeys =3D NULL; g_autoptr(GuestAuthorizedKeys) ret =3D NULL; int i; + int fd; =20 p =3D get_passwd_entry(username, errp); if (p =3D=3D NULL) { return NULL; } =20 - authkeys_path =3D g_build_filename(p->pw_dir, ".ssh", - "authorized_keys", NULL); + ssh_path =3D g_build_filename(p->pw_dir, ".ssh", NULL); + authkeys_path =3D g_build_filename(ssh_path, "authorized_keys", NULL); + + fd =3D open(ssh_path, O_DIRECTORY | O_NOFOLLOW); + if (fd =3D=3D -1) { + if (errno !=3D ENOENT) { + error_setg_errno(errp, errno, "failed to open directory '%s'",= ssh_path); + return NULL; + } + } else { + close(fd); + } + authkeys =3D read_authkeys(authkeys_path, errp); if (authkeys =3D=3D NULL) { return NULL; --=20 2.53.0