From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D5CAAC43458 for ; Tue, 14 Jul 2026 11:30:38 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wjbLE-0001Fw-5t; Tue, 14 Jul 2026 07:30:24 -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 1wjbLC-0001Eg-TW for qemu-devel@nongnu.org; Tue, 14 Jul 2026 07:30:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wjbL9-0002Fi-Sp for qemu-devel@nongnu.org; Tue, 14 Jul 2026 07:30:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784028618; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ipdRXt74Byl8QPOWcWJmRT9z57ql/fUfWwgW5tA1NXQ=; b=Bv+9YgDTe+hGXVU1owQU7QWl7P2e6DOvat8gLqD/+qIQs6OnQnhhFV42Xmrnm88BkOW3z6 KYdNaDKBPMG9/6RGvYzr/+zg6hm1wuMRySJwjbzTM6SKcTbwMoPC735TDgW4iPhcAqP0Wr mKsocdlU/SQhwSAOD5BFhOcxXmY2zDw= Received: from mx-prod-mc-05.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-510-4gFOkwypONW9gvRs11tkYA-1; Tue, 14 Jul 2026 07:30:17 -0400 X-MC-Unique: 4gFOkwypONW9gvRs11tkYA-1 X-Mimecast-MFC-AGG-ID: 4gFOkwypONW9gvRs11tkYA_1784028616 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 39A8D195608B; Tue, 14 Jul 2026 11:30:16 +0000 (UTC) Received: from redhat.com (unknown [10.44.33.163]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 413E130001A1; Tue, 14 Jul 2026 11:30:13 +0000 (UTC) Date: Tue, 14 Jul 2026 12:30:08 +0100 From: Daniel =?utf-8?B?UC4gQmVycmFuZ8Op?= To: Kostiantyn Kostiuk Cc: qemu-devel@nongnu.org, Valentino Paulon , Yan Vugenfirer , Michael Roth Subject: Re: [PATCH] qga: Do not follow symlink in guest-ssh-* commands Message-ID: References: <20260709105707.91209-1-kkostiuk@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/2.4.0 (2026-06-19) X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: permerror client-ip=170.10.133.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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.01, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, T_SPF_PERMERROR=0.01 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: , Reply-To: Daniel =?utf-8?B?UC4gQmVycmFuZ8Op?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org On Tue, Jul 14, 2026 at 02:28:35PM +0300, Kostiantyn Kostiuk wrote: > On Tue, Jul 14, 2026 at 1:37 PM Daniel P. Berrangé > wrote: > > > On Thu, Jul 09, 2026 at 01:57:07PM +0300, Kostiantyn Kostiuk wrote: > > > 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; > > > } > > > > > > - 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; > > > + } > > > + } > > > > IIUC, you're trying to protect against the possbility that /home/fred/.ssh > > is > > a symlink to some other privileged directory. > > > > > 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 ? > > > > Yes, you are right; race is possible > > > > > > 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 :|