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=-11.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 07626C432BE for ; Tue, 31 Aug 2021 10:22:56 +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 8A3AD604AC for ; Tue, 31 Aug 2021 10:22:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8A3AD604AC Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=tls.msk.ru Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:40950 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mL0us-0008Ny-Cg for qemu-devel@archiver.kernel.org; Tue, 31 Aug 2021 06:22:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59190) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mL0q1-0006dF-1w for qemu-devel@nongnu.org; Tue, 31 Aug 2021 06:17:53 -0400 Received: from isrv.corpit.ru ([86.62.121.231]:36145) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mL0pz-0000wv-1b for qemu-devel@nongnu.org; Tue, 31 Aug 2021 06:17:52 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id 4826C40838; Tue, 31 Aug 2021 13:17:46 +0300 (MSK) Received: from [192.168.177.130] (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with ESMTP id 9982123D; Tue, 31 Aug 2021 13:17:46 +0300 (MSK) Subject: Re: [PATCH] util: fix abstract socket path copy To: Peter Maydell References: <20210719130112.932069-1-marcandre.lureau@redhat.com> <12894fce-38cd-8d29-9c2b-fc2d8f73a42f@msgid.tls.msk.ru> <1706f18b-d6e3-e88c-ac12-decdfbd7d9b2@msgid.tls.msk.ru> <6dd4a9d9-c264-a747-0fb5-2d470f19ad16@msgid.tls.msk.ru> From: Michael Tokarev Message-ID: <314d03f8-1270-e146-3806-74752fe970f5@msgid.tls.msk.ru> Date: Tue, 31 Aug 2021 13:17:45 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Received-SPF: none client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -77 X-Spam_score: -7.8 X-Spam_bar: ------- X-Spam_report: (-7.8 / 5.0 requ) BAYES_00=-1.9, NICE_REPLY_A=-0.932, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham 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: xiaoqiang zhao , =?UTF-8?Q?Marc-Andr=c3=a9_Lureau?= , "Daniel P. Berrange" , QEMU Developers , Markus Armbruster Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 31.08.2021 12:53, Peter Maydell wrote: > On Mon, 30 Aug 2021 at 23:30, Michael Tokarev wrote: >> >> 31.08.2021 01:06, Michael Tokarev wrote: >> ... >>> And this is the value used to be returned in the getsockname/getpeername >>> calls. >>> >>> So this has nothing to do with socket being abstract or not. We asked for >>> larger storage for the sockaddr structure, and the kernel was able to build >>> one for us, including the trailing \0 byte. > >> diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c >> index f2f3676d1f..83926dc2bc 100644 >> --- a/util/qemu-sockets.c >> +++ b/util/qemu-sockets.c >> @@ -1345,8 +1345,9 @@ socket_sockaddr_to_address_unix(struct sockaddr_storage *sa, >> SocketAddress *addr; >> struct sockaddr_un *su = (struct sockaddr_un *)sa; >> >> + /* kernel might have added \0 terminator to non-abstract socket */ >> assert(salen >= sizeof(su->sun_family) + 1 && >> - salen <= sizeof(struct sockaddr_un)); >> + salen <= sizeof(struct sockaddr_un) + su->sun_path[0] ? 1 : 0); > > Q: Why are we imposing an upper limit on salen anyway? > We need the lower limit because salen is supposed to include > the whole of the 'struct sockaddr_un' and we assume that. > But what's the upper limit here protecting? It is not about protection really, it is about correctness. This is actually a grey area. This single trailing \0 byte depends on the implementation. Please read man 7 unix - especially the "Pathname sockets" and BUGS sections. > Q2: why does our required upper limit change depending on whether > there happens to be a string in the sun_path array or not ? Because for abstract sockets (the ones whos name starts with \0 byte) the sun_path is treated as a blob of given length, without the additional trailing \0, and neither the kernel nor userspace is trying to add the terminator, while for pathname sockets this is not the case and someone has to add the trailing \0 somewhere. > Q3: when we copy the sun_path, why do we skip the first character? > > addr->u.q_unix.path = g_strndup(su->sun_path + 1, > salen - sizeof(su->sun_family) - 1); Because this is the case of abstract socket. Actual name is a blob starting from the sun_path+1. I dunno if this is correct to use g_strndup there - will it terminate at first \0 encountered or not, but this is another question. The difference between this place and regular pathname socket below is this: addr->u.q_unix.abstract = true; We have the issue only whith regular traditional unix pathname sockets whose name is exactly 108 bytes long. I provided the code from the kernel in another thread showing how it increases the len by one in this case and returns it in getsockname() if userspace provided enough room. /mjt