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 X-Spam-Level: X-Spam-Status: No, score=-10.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA37DC432BE for ; Tue, 31 Aug 2021 19:23:49 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1673560234 for ; Tue, 31 Aug 2021 19:23:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 1673560234 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:60566 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mL9MJ-0005Zt-W2 for qemu-devel@archiver.kernel.org; Tue, 31 Aug 2021 15:23:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47202) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mL9Kg-0004Zm-2o for qemu-devel@nongnu.org; Tue, 31 Aug 2021 15:22:06 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:45866) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mL9Ka-00069w-Pe for qemu-devel@nongnu.org; Tue, 31 Aug 2021 15:22:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630437719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+qx9YjAbq9FTehpYBdDoW7AnFfQ6PWZUFgUsV2CO1Jw=; b=dHbk3QzPhiWh4noDyGj5atWP+VFakxxwNOLE5z+FU9LVgddFpbeVSbI67n19aEkPdkuRQm lGJHGPiJUEUods0Bh4iQjvLormHBLUDZ1A3v70b2s6cERwwtV9HM1Holn76hxAIdDzI8Qd 9ZhYh1IFCv31NNGvlRmdgQv3bJ9+emg= Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-422-gUGrWcaUN4WG0I3X6q4dsg-1; Tue, 31 Aug 2021 15:21:55 -0400 X-MC-Unique: gUGrWcaUN4WG0I3X6q4dsg-1 Received: by mail-pj1-f69.google.com with SMTP id m9-20020a17090ade09b029017903cc8d6cso1899260pjv.4 for ; Tue, 31 Aug 2021 12:21:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=+qx9YjAbq9FTehpYBdDoW7AnFfQ6PWZUFgUsV2CO1Jw=; b=mdVe5zEYzDmQs5ihfCe88mKHCduAw8WRjLOAUygbG8SFTeDrAOCrW67t2+0tW8hAhy fa1aNgWtc9nCIMY145CcEq5mLg7WrGoc1mIHkX0MOPFMKJ6bMU1FTPZIXg4ieWZ4di5P g/+9+K7e03XPTD/0n/FffT6eDrlQQxtXpbSAhyXMTQsPQK/g3XxeAoccze5+noDYJmB7 u56lBdWmBO0YPoVbrMMUP0ruze4AvDqtYztWCTHZ5oWIP+896yhWte9ZrBWcycYMRS5i 1RqMhoYYWXhIiuT/DAXnn+bB9E2moB70D/oPJ7CswvrayHlqHHhIIqriJUSi5vYUX7ri hjnw== X-Gm-Message-State: AOAM531nhYN2Vw/PSOOfUMaW6RY5b+X4VFk/pM4rB+6n9zb/C7U6jUUz 0DgELyEqZg4ezOQw+NtRgPkaQRsKk+UXZqkMXMW69WNBAo8u4z4rnDThbpyPvJjoil9SekoPlUL CH27tgkMLkzqLMqrOvGHYc/EeV735Apw= X-Received: by 2002:a17:902:c406:b0:138:c28f:a779 with SMTP id k6-20020a170902c40600b00138c28fa779mr6158604plk.1.1630437714528; Tue, 31 Aug 2021 12:21:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxuWXl/SLMlpqDpZMw8nWSEyxA404jGNk6nnFMeS46yuM2zaKyYx+laNKoiokax52f4snfif+pwGXDBO3sZOLI= X-Received: by 2002:a17:902:c406:b0:138:c28f:a779 with SMTP id k6-20020a170902c40600b00138c28fa779mr6158586plk.1.1630437714234; Tue, 31 Aug 2021 12:21:54 -0700 (PDT) MIME-Version: 1.0 References: <20210831182623.1792608-1-mjt@msgid.tls.msk.ru> In-Reply-To: <20210831182623.1792608-1-mjt@msgid.tls.msk.ru> From: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= Date: Tue, 31 Aug 2021 23:21:43 +0400 Message-ID: Subject: Re: [PATCH] qemu-sockets: fix unix socket path copy (again) To: Michael Tokarev Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlureau@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="000000000000c929e205cadfdc77" Received-SPF: pass client-ip=170.10.133.124; envelope-from=mlureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.391, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?Q?Daniel_P_=2E_Berrang=C3=A9?= , qemu-devel , qemu-stable Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --000000000000c929e205cadfdc77 Content-Type: text/plain; charset="UTF-8" Hi On Tue, Aug 31, 2021 at 10:26 PM Michael Tokarev wrote: > We test whenever the path of unix-domain socket > address is non-empty and strictly-less than > the length of the path buffer. Both these > conditions are wrong: the socket can be unnamed, > with empty path, or socket can have pathname > null-terminated _after_ the sun_path buffer, > since we provided more room when asking kernel. > > So on one side, allow empty, unnamed sockets > (and adjust the test for abstract socket too - > only do that if the socket is not unnamed), > and on another side, allow path length to be > up to our own maximum, - we have up to size > of sockaddr_storage there. > > While at it, fix the duplication of regular > pathname socket to not require trailing \0 > (since it can be missing for unnamed sockets). > > Fixes: 4cfd970ec188558daa6214f26203fe553fb1e01f (first in 6.1.0) > Fixes: http://bugs.debian.org/993145 > Signed-off-by: Michael Tokarev > Cc: qemu-stable@nongnu.org > -- > Two questions. > 1. Why do we store the name of the socket to start with? > 2. The code in the abstract socket case should not use > g_strndup but g_memdup instead, since the whole thing > is a blob of the given length, not a \0-terminated string. > --- > util/qemu-sockets.c | 15 +++++++++++---- > 1 file changed, 11 insertions(+), 4 deletions(-) > > diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c > index f2f3676d1f..7c83d81792 100644 > --- a/util/qemu-sockets.c > +++ b/util/qemu-sockets.c > @@ -1345,13 +1345,20 @@ socket_sockaddr_to_address_unix(struct > sockaddr_storage *sa, > SocketAddress *addr; > struct sockaddr_un *su = (struct sockaddr_un *)sa; > > - assert(salen >= sizeof(su->sun_family) + 1 && > - salen <= sizeof(struct sockaddr_un)); > + /* there's a corner case when trailing \0 does not fit into > + * sockaddr_un. Compare length with sizeof(sockaddr_storage), > + * not with sizeof(sockaddr_un), since this is what we actually > + * provide, to ensure we had no truncation and a room for > + * the trailing \0 which we add below. > + * When salen == sizeof(sun_family) it is unnamed socket, > + * and when first byte of sun_path is \0, it is abstract. */ > + assert(salen >= sizeof(su->sun_family) && > + salen <= sizeof(struct sockaddr_storage)); > Seems right to me, however there are some notes in libc bits/socket.h /* Structure large enough to hold any socket address (with the historical exception of AF_UNIX). */ And also this https://idea.popcount.org/2019-12-06-addressing/#fn:sockaddr_storage I must say I feel confused by those comments :) Is it large enough or not?? > addr = g_new0(SocketAddress, 1); > addr->type = SOCKET_ADDRESS_TYPE_UNIX; > #ifdef CONFIG_LINUX > - if (!su->sun_path[0]) { > + if (salen > sizeof(su->sun_family) && !su->sun_path[0]) { > /* Linux abstract socket */ > addr->u.q_unix.path = g_strndup(su->sun_path + 1, > salen - sizeof(su->sun_family) - > 1); > @@ -1363,7 +1370,7 @@ socket_sockaddr_to_address_unix(struct > sockaddr_storage *sa, > } > #endif > > - addr->u.q_unix.path = g_strndup(su->sun_path, sizeof(su->sun_path)); > + addr->u.q_unix.path = g_strndup(su->sun_path, salen - > sizeof(su->sun_family)); > looks good to me otherwise return addr; > } > #endif /* WIN32 */ > -- > 2.30.2 > > --000000000000c929e205cadfdc77 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

On Tue, Aug 31, 2021 at 10:26 PM Mi= chael Tokarev <mjt@tls.msk.ru> = wrote:
We test w= henever the path of unix-domain socket
address is non-empty and strictly-less than
the length of the path buffer. Both these
conditions are wrong: the socket can be unnamed,
with empty path, or socket can have pathname
null-terminated _after_ the sun_path buffer,
since we provided more room when asking kernel.

So on one side, allow empty, unnamed sockets
(and adjust the test for abstract socket too -
only do that if the socket is not unnamed),
and on another side, allow path length to be
up to our own maximum, - we have up to size
of sockaddr_storage there.

While at it, fix the duplication of regular
pathname socket to not require trailing \0
(since it can be missing for unnamed sockets).

Fixes: 4cfd970ec188558daa6214f26203fe553fb1e01f (first in 6.1.0)
Fixes: http://bugs.debian.org/993145
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Cc: qemu-stable= @nongnu.org
--
Two questions.
1. Why do we store the name of the socket to start with?
2. The code in the abstract socket case should not use
=C2=A0 =C2=A0g_strndup but g_memdup instead, since the whole thing
=C2=A0 =C2=A0is a blob of the given length, not a \0-terminated string.
---
=C2=A0util/qemu-sockets.c | 15 +++++++++++----
=C2=A01 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index f2f3676d1f..7c83d81792 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -1345,13 +1345,20 @@ socket_sockaddr_to_address_unix(struct sockaddr_sto= rage *sa,
=C2=A0 =C2=A0 =C2=A0SocketAddress *addr;
=C2=A0 =C2=A0 =C2=A0struct sockaddr_un *su =3D (struct sockaddr_un *)sa;
-=C2=A0 =C2=A0 assert(salen >=3D sizeof(su->sun_family) + 1 &&= ;
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0salen <=3D sizeof(struct socka= ddr_un));
+=C2=A0 =C2=A0 /* there's a corner case when trailing \0 does not fit i= nto
+=C2=A0 =C2=A0 =C2=A0* sockaddr_un. Compare length with sizeof(sockaddr_sto= rage),
+=C2=A0 =C2=A0 =C2=A0* not with sizeof(sockaddr_un), since this is what we = actually
+=C2=A0 =C2=A0 =C2=A0* provide, to ensure we had no truncation and a room f= or
+=C2=A0 =C2=A0 =C2=A0* the trailing \0 which we add below.
+=C2=A0 =C2=A0 =C2=A0* When salen =3D=3D sizeof(sun_family) it is unnamed s= ocket,
+=C2=A0 =C2=A0 =C2=A0* and when first byte of sun_path is \0, it is abstrac= t. */
+=C2=A0 =C2=A0 assert(salen >=3D sizeof(su->sun_family) && +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0salen <=3D sizeof(struct socka= ddr_storage));

Seems right to me, howev= er there are some notes in libc bits/socket.h
/* Structure la= rge enough to hold any socket address (with the historical
=C2=A0 =C2=A0= exception of AF_UNIX). =C2=A0*/

And also this
<= /div>

I must say I feel confused by those= comments :) Is it large enough or not??


=C2=A0 =C2=A0 =C2=A0addr =3D g_new0(SocketAddress, 1);
=C2=A0 =C2=A0 =C2=A0addr->type =3D SOCKET_ADDRESS_TYPE_UNIX;
=C2=A0#ifdef CONFIG_LINUX
-=C2=A0 =C2=A0 if (!su->sun_path[0]) {
+=C2=A0 =C2=A0 if (salen > sizeof(su->sun_family) && !su->= sun_path[0]) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/* Linux abstract socket */
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0addr->u.q_unix.path =3D g_strndup(su-&= gt;sun_path + 1,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sa= len - sizeof(su->sun_family) - 1);
@@ -1363,7 +1370,7 @@ socket_sockaddr_to_address_unix(struct sockaddr_stora= ge *sa,
=C2=A0 =C2=A0 =C2=A0}
=C2=A0#endif

-=C2=A0 =C2=A0 addr->u.q_unix.path =3D g_strndup(su->sun_path, sizeof= (su->sun_path));
+=C2=A0 =C2=A0 addr->u.q_unix.path =3D g_strndup(su->sun_path, salen = - sizeof(su->sun_family));

looks goo= d to me otherwise

=C2=A0 =C2=A0 =C2=A0return addr;
=C2=A0}
=C2=A0#endif /* WIN32 */
--
2.30.2

--000000000000c929e205cadfdc77--