From nobody Thu Sep 19 02:01:12 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 171680179341862.48050593756659; Mon, 27 May 2024 02:23:13 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 566D11AD1; Mon, 27 May 2024 05:23:12 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id D9F0D1B2C; Mon, 27 May 2024 05:19:32 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 1AC391B8B; Mon, 27 May 2024 05:19:23 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 064481939 for ; Mon, 27 May 2024 05:19:09 -0400 (EDT) Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-121-cNzOizD_Pg-CXjyD5lzflA-1; Mon, 27 May 2024 05:19:07 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 610FF1C0512D for ; Mon, 27 May 2024 09:19:06 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0CD8640005B for ; Mon, 27 May 2024 09:19:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716801549; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=njHVQ2PXrFFpnOOFEIytTf5gZySrlfAXOmLK4TRXXDM=; b=WxQxXyv4eaMyky65dEn8Qgk9+kkQIhXRPFl9afvnyABZvOYyrrVKSrXS+Hme26U3O+Rzsa u3rxWfAcBvUc/jtXUaQuJeP6Za/le2aeOTlBQqVc+4v+w4LGOy2cUFyHNRQB81HisnPdBE I1uIxTp9f2RWsFaw4CtXjEKG14jiecc= X-MC-Unique: cNzOizD_Pg-CXjyD5lzflA-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH 5/6] vsh: Restore original rl_line_buffer after completion Date: Mon, 27 May 2024 11:18:53 +0200 Message-ID: <734050628dc177d495c3dd3ebb99d8092b204504.1716801414.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: VD7OAVTZGF76IKSHII2HR7J34ZZVHVUD X-Message-ID-Hash: VD7OAVTZGF76IKSHII2HR7J34ZZVHVUD X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1716801793809100001 Content-Type: text/plain; charset="utf-8"; x-default="true" Problem with readline is its API. It's basically a bunch of global variables with no clear dependencies between them. In this specific case that I'm seeing: in interactive mode the cmdComplete() causes instant crash of virsh/virt-admin: =3D=3D27999=3D=3D Invalid write of size 1 =3D=3D27999=3D=3D at 0x516EF71: _rl_init_line_state (readline.c:742) =3D=3D27999=3D=3D by 0x5170054: rl_initialize (readline.c:1192) =3D=3D27999=3D=3D by 0x516E5E4: readline (readline.c:379) =3D=3D27999=3D=3D by 0x1B7024: vshReadline (vsh.c:3048) =3D=3D27999=3D=3D by 0x140DCF: main (virsh.c:905) =3D=3D27999=3D=3D Address 0x0 is not stack'd, malloc'd or (recently) free'd This is because readline keeps a copy of pointer to rl_line_buffer and the moment cmdComplete() returns and readline takes over, it accesses the copy which is now a dangling pointer. To fix this, just keep the original state of rl_line_buffer and restore it. Fixes: 41400ac1dda55b817388a4050aa823051bda2e05 Fixes: a0e1ada63c0afdc2af3b9405cbf637d8bd28700c Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- tools/vsh.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/vsh.c b/tools/vsh.c index de869248b4..c91d756885 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -3511,6 +3511,7 @@ cmdComplete(vshControl *ctl, const vshCmd *cmd) const vshClientHooks *hooks =3D ctl->hooks; const char *lastArg =3D NULL; const char **args =3D NULL; + char *old_rl_line_buffer =3D NULL; g_auto(GStrv) matches =3D NULL; char **iter; =20 @@ -3531,6 +3532,7 @@ cmdComplete(vshControl *ctl, const vshCmd *cmd) =20 vshReadlineInit(ctl); =20 + old_rl_line_buffer =3D g_steal_pointer(&rl_line_buffer); if (!(rl_line_buffer =3D g_strdup(vshCommandOptArgvString(cmd, "string= ")))) rl_line_buffer =3D g_strdup(""); =20 @@ -3540,6 +3542,7 @@ cmdComplete(vshControl *ctl, const vshCmd *cmd) =20 matches =3D vshReadlineCompletion(lastArg, 0, 0); g_clear_pointer(&rl_line_buffer, g_free); + rl_line_buffer =3D g_steal_pointer(&old_rl_line_buffer); =20 if (!matches) return false; --=20 2.44.1