From nobody Mon Nov 23 04:28:09 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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1793885064; cv=none; d=zohomail.com; s=zohoarc; b=P/ZoEmBLExsPPO7wve5gjOzDMDDdBZM87v01uKh2HvU1UsYsFViOEqtJ2+xMSdkMzJ9fc/0OpBblYJwnt264WcTkrtgVG/JrRFFXj3yaPYa+ojljCeM+Zd6rh5fATT/GuuMRAGllTVVZ8JjnLbNQe8Vep2Ew41tKT+QvztsBb8s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793885064; h=Content-Type: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=3gok3RHX0hdU8OwhiFU7ML1Z2ViviNh+l7K4BQ51h44=; b=WfIsOMR3DvsYoSQwfsPscfB1wcnbSm8N9T16qSRQmxK4/nL8iSAslt+t28xcoQ/37BI1Ppwkqq9UlV0Gg8E5aLCoteviTquLTaM01wLiuWw04y12Sz+1H1Hm1uYlr/L9pPELXZM5cZXW3iDTd45mKmNLEqSU+nohjposJ3634uw= 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=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179388506457141.36713652376989; Thu, 5 Nov 2026 05:24:24 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOxRZ-0001wu-4b; Thu, 05 Nov 2026 08:23:53 -0500 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 1xOvs1-0003v5-3u for qemu-devel@nongnu.org; Thu, 05 Nov 2026 06:43:05 -0500 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOvrz-0005X6-4R for qemu-devel@nongnu.org; Thu, 05 Nov 2026 06:43:04 -0500 Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-c434376dbebso130609966b.1 for ; Thu, 05 Nov 2026 03:43:02 -0800 (PST) Received: from Mac-FVFFP2HZQ05N ([86.106.20.24]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c4d0e4a7797sm161770866b.87.2026.11.05.03.42.59 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 05 Nov 2026 03:42:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793878981; x=1794483781; darn=nongnu.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=3gok3RHX0hdU8OwhiFU7ML1Z2ViviNh+l7K4BQ51h44=; b=EDUXTZQwbqN97fgZZfm6A5lwy7+N+UtvK0XhcdXL+mKA4lKodl261yFOjEHoWjJsnn sP+QyCt8lp/H89iHqYT9xTnJhbrCw7GYZ212mVp/t7dhVsP82cwBTC3iyD2SO9gGBiDx wohRVJJ96l0T8dVZ1ho1cbDkmiq2OvnRSuwUIq9GCkEMq6Ue7NGbLksSyF8ugC8/VMn8 HJGtkJN1tRfDdhda180r4QOt8xIlAXB0+8IX+Cqa4GouRk4xBnirQ1hF/kOXLXLZc+L5 zhl5v/WChXCyH/pfdThYDe2yrRSC1CfY74RNFroyMM1WZ90xAJEBhoonjL0UMn2lIpST RDBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793878981; x=1794483781; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=3gok3RHX0hdU8OwhiFU7ML1Z2ViviNh+l7K4BQ51h44=; b=2Bk7trBiCVq5H/Wv1f1IQ8EkXAnf7QilfuSZ/6wLP89G5QgwIsnABKmzFSPhouj3JH vPBy2bvMp5zZHs3h3Cq8Yjw6b8eTQOvgUcU9lS+d7Rzw+o+QvkzGe+2115Q9RQmG6lWu 2niyoFCQZMl5BU+fp5jtDi5fppe0yQ23lAw87duTYJVIuHrJ3X18sfBkiCcuvbDieTDb M5GgwqR43A0pA9ZkiRzPnCcl1+USAzmBtfc3No/Aw2zKCR6lnxjx2cGrEJW5NB1a+A5L 3F+uJr9KYc2H9V4XPyg4SfEqjU8NVWpCe+NYjTpca/rAu954zbtWte7ANDUsmf5/j4oZ livA== X-Gm-Message-State: APuU4Z945zdX+9bQKM1pN+8V8HqWPv3vPgndytDr8X7LEGhDjCB8GgxH hjJhHCOg9REZ1ObnUUut2dGmovjyt9F6nwMV+10Txm0gVrjuGjDrmWu5HrilwAmZ X-Gm-Gg: AYBFou1vZ5N/PsEFHJmTcEl9nITO9cqQNe6n59pACaOVtoZT2XZRChvG9uVdrpNl2Dd gU8762bLIoTifPHAhT4pv8ZbKpnYZbNIOfW6VJ7yKZ0GUuXB3Anw6fLVGlMgFCqg0/gd4P4Q14k tHWKnIVvS/qVlKPTvv0GdHm9Ax/hYoG/3oBH3/+fYRPm/T09v4wLI4IE+hcf6KVf5JifEN4/eOg x+4bTeH9c5upCWa7C0DaHadGWT5+ZerBeDLAqz0JhRacPkPKthZquCUrIBhMDI3AHdms0XuU7Cl Rl5S5naHPNqGTkou9LTUdOlFvcse8DYKgRribalepe8VlF7lVmbFjbei0Q9A+jYtzJ/pNJYHrel iaXDiMki7IP+OgfFHF8PlaY63uzRrzQpRyS6clzPxhJFQI0k9WZsRdTxvGgWtXJC7fruC/w+CRs zk5Qx6bb2EkO8I5H90VBDLAOpjJnVijoozR2077IET1evOAOrJsL67h3J9jEM8u9yXCQOg8RTCI 20gs53GID2xxWO+O0HxskIPfAgNILNyq7MrclD/zoMa8GvzrSFQZfNc5DF35SjXkr3+ArSsXboU 1E46dYqKDFFH8Ts0hJoxooLHOWQu7SuVKNHl/MBq8mFzd3iiMuYALKYa7Vdx4VTjrC1fyHTQcIS 0Vf0PLLG1o+ArBASn0aJoz2xmQDmG44h5Mq5CDz4/3oh0F/JyJr27+sxRod1b/HuIWA== X-Received: by 2002:a17:907:3d94:b0:c2e:902e:5208 with SMTP id a640c23a62f3a-c4d204bdf35mr204555466b.5.1793878980814; Thu, 05 Nov 2026 03:43:00 -0800 (PST) From: =?UTF-8?q?Valentinas=20=C4=8Cirba?= To: qemu-devel@nongnu.org Cc: Michael Roth , Kostiantyn Kostiuk , qemu-stable@nongnu.org, =?UTF-8?q?Valentinas=20=C4=8Cirba?= Subject: [PATCH] qga: keep a trailing newline in authorized_keys Date: Thu, 5 Nov 2026 13:41:50 +0200 Message-ID: <20261105114242.35394-1-fizikiukas@gmail.com> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=2a00:1450:4864:20::631; envelope-from=fizikiukas@gmail.com; helo=mail-ej1-x631.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 05 Nov 2026 08:23:50 -0500 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 @gmail.com) X-ZM-MESSAGEID: 1793885066870158500 From: Valentinas =C4=8Cirba After guest-ssh-add-authorized-keys the file has an empty line before the new key and no final newline, so a key appended later with ">>" is merged into the previous line. read_authkeys() / g_strsplit leaves a trailing empty element; write_authkeys() / g_strjoinv adds no final newline. Both write_authkeys() functions, in qga/commands-posix-ssh.c and qga/commands-windows-ssh.c, now end non-empty contents with \n. An empty key list is still written as "". Also also changes read_authkeys() in qga/commands-common-ssh.c, so it drops the trailing empty element. Fixes: 8d769ec777dc ("qga: add ssh-{add,remove}-authorized-keys") Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4803 Cc: qemu-stable@nongnu.org Signed-off-by: Valentinas =C4=8Cirba --- qga/commands-common-ssh.c | 12 ++++++++++- qga/commands-posix-ssh.c | 41 +++++++++++++++++++++++++++++++++----- qga/commands-windows-ssh.c | 7 ++++++- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/qga/commands-common-ssh.c b/qga/commands-common-ssh.c index 537869fb98..5f5b631689 100644 --- a/qga/commands-common-ssh.c +++ b/qga/commands-common-ssh.c @@ -11,13 +11,23 @@ GStrv read_authkeys(const char *path, Error **errp) { g_autoptr(GError) err =3D NULL; g_autofree char *contents =3D NULL; + GStrv lines; + guint len; =20 if (!g_file_get_contents(path, &contents, NULL, &err)) { error_setg(errp, "failed to read '%s': %s", path, err->message); return NULL; } =20 - return g_strsplit(contents, "\n", -1); + lines =3D g_strsplit(contents, "\n", -1); + + len =3D g_strv_length(lines); + if (len > 0 && lines[len - 1][0] =3D=3D '\0') { + g_free(lines[len - 1]); + lines[len - 1] =3D NULL; + } + + return lines; } =20 bool check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp) diff --git a/qga/commands-posix-ssh.c b/qga/commands-posix-ssh.c index aa3411049f..924c5bdeb0 100644 --- a/qga/commands-posix-ssh.c +++ b/qga/commands-posix-ssh.c @@ -113,7 +113,12 @@ write_authkeys(const char *path, const GStrv keys, g_autofree char *contents =3D NULL; g_autoptr(GError) err =3D NULL; =20 - contents =3D g_strjoinv("\n", keys); + if (keys[0] !=3D NULL) { + g_autofree char *joined =3D g_strjoinv("\n", keys); + contents =3D g_strconcat(joined, "\n", NULL); + } else { + contents =3D g_strdup(""); + } if (!g_file_set_contents(path, contents, -1, &err)) { error_setg(errp, "failed to write to '%s': %s", path, err->message= ); return false; @@ -402,7 +407,7 @@ test_add_keys(void) &err); g_assert(err =3D=3D NULL); =20 - test_authorized_keys_equal("algo key2 comments"); + test_authorized_keys_equal("algo key2 comments\n"); =20 qmp_guest_ssh_add_authorized_keys(g_get_user_name(), (strList *)&test_key1_2, @@ -412,7 +417,7 @@ test_add_keys(void) =20 /* key2 came first, and shouldn't be duplicated */ test_authorized_keys_equal("algo key2 comments\n" - "algo key1 comments"); + "algo key1 comments\n"); } =20 static void @@ -428,7 +433,7 @@ test_add_reset_keys(void) =20 /* reset with key2 only */ test_authorized_keys_equal("algo key1 comments\n" - "algo key2 comments"); + "algo key2 comments\n"); =20 qmp_guest_ssh_add_authorized_keys(g_get_user_name(), (strList *)&test_key2, @@ -436,7 +441,7 @@ test_add_reset_keys(void) &err); g_assert(err =3D=3D NULL); =20 - test_authorized_keys_equal("algo key2 comments"); + test_authorized_keys_equal("algo key2 comments\n"); =20 /* empty should clear file */ qmp_guest_ssh_add_authorized_keys(g_get_user_name(), @@ -448,6 +453,30 @@ test_add_reset_keys(void) test_authorized_keys_equal(""); } =20 +static void +test_add_keys_trailing_newline(void) +{ + Error *err =3D NULL; + + test_authorized_keys_set("algo key1 comments\n"); + qmp_guest_ssh_add_authorized_keys(g_get_user_name(), + (strList *)&test_key2, + FALSE, FALSE, + &err); + g_assert(err =3D=3D NULL); + test_authorized_keys_equal("algo key1 comments\n" + "algo key2 comments\n"); + + test_authorized_keys_set("algo key1 comments"); + qmp_guest_ssh_add_authorized_keys(g_get_user_name(), + (strList *)&test_key2, + FALSE, FALSE, + &err); + g_assert(err =3D=3D NULL); + test_authorized_keys_equal("algo key1 comments\n" + "algo key2 comments\n"); +} + static void test_remove_keys(void) { @@ -507,6 +536,8 @@ int main(int argc, char *argv[]) g_test_add_func("/qga/ssh/invalid_key", test_invalid_key); g_test_add_func("/qga/ssh/add_keys", test_add_keys); g_test_add_func("/qga/ssh/add_reset_keys", test_add_reset_keys); + g_test_add_func("/qga/ssh/add_keys_trailing_newline", + test_add_keys_trailing_newline); g_test_add_func("/qga/ssh/remove_keys", test_remove_keys); g_test_add_func("/qga/ssh/get_keys", test_get_keys); =20 diff --git a/qga/commands-windows-ssh.c b/qga/commands-windows-ssh.c index df45c17b75..f5cf30cc88 100644 --- a/qga/commands-windows-ssh.c +++ b/qga/commands-windows-ssh.c @@ -431,7 +431,12 @@ static bool write_authkeys(WindowsUserInfo *userInfo, = GStrv authkeys, g_autofree char *contents =3D NULL; g_autoptr(GError) err =3D NULL; =20 - contents =3D g_strjoinv("\n", authkeys); + if (authkeys[0] !=3D NULL) { + g_autofree char *joined =3D g_strjoinv("\n", authkeys); + contents =3D g_strconcat(joined, "\n", NULL); + } else { + contents =3D g_strdup(""); + } =20 if (!g_file_set_contents(userInfo->authorizedKeyFile, contents, -1, &e= rr)) { error_setg(errp, "failed to write to '%s': %s", --=20 2.54.0 (Apple Git-157)