From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id 8105D3858406 for ; Mon, 24 Jan 2022 12:07:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8105D3858406 Received: by mail-oi1-x22f.google.com with SMTP id bf5so25143247oib.4 for ; Mon, 24 Jan 2022 04:07:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=8gljYx5XVU2XtNm8K8/4+XI8ZqI8b59fWZc0PjJIgqA=; b=QsSONiKJ5BG6MPwah09RWs1o9ANMho6mcLuhUMwlj2Skvjv1pgz+tGRBYuJfcVpq0y IHgkW63Dp0XXpwxT1OCVuxXhHQMpKOyYf1hZKk9Q+eMIQTTEZTcBKoH4AO1ILDq4mRmm a/xnZymvlFggSTbSjn8UjpMcB6byQdvgl5ClsBvKuzDtc1h0q/4YssgPPQBmsiqMbK01 3gX9ZVA/bxdBKIRlWtQzAInoY3TExtjyvD9DLWXVfoEfWPV5mn4/y+LyfhuhDPx4ZtAv aNFYoMbg042XBWNv+IArHeNcBYCWvIq6/y0e37TEG5iiKB3qeVMOLXhff15FreGb86rb Foaw== X-Gm-Message-State: AOAM530Rjzu6D0efLsA0EIHzGEDzZH/RkFIqLgesj8mpjut+1MGfaMLW ig5l/l3lvYDcggs/IkK6LSF5CChLBdk5ZA== X-Google-Smtp-Source: ABdhPJy57VDQkAR/mdAMyldsoWlKVq46ZuLjrkDryyyz9D8TrOzcjr6cGP28ww1JlCFvF26kbHGBjQ== X-Received: by 2002:aca:3842:: with SMTP id f63mr1093544oia.68.1643026031711; Mon, 24 Jan 2022 04:07:11 -0800 (PST) Received: from ?IPV6:2804:431:c7cb:27f8:c505:63c0:de61:b716? ([2804:431:c7cb:27f8:c505:63c0:de61:b716]) by smtp.gmail.com with ESMTPSA id bb32sm5254906oib.11.2022.01.24.04.07.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Jan 2022 04:07:11 -0800 (PST) Message-ID: <372ae1ee-0163-7bf4-be1c-cc1f13b1a940@linaro.org> Date: Mon, 24 Jan 2022 09:07:09 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH v4] linux: Fix ancillary 64-bit time timestamp conversion (BZ #28349, BZ #28350) Content-Language: en-US To: Florian Weimer , Adhemerval Zanella via Libc-alpha References: <20211222185239.1088511-1-adhemerval.zanella@linaro.org> <8735lh6vqo.fsf@oldenburg.str.redhat.com> From: Adhemerval Zanella In-Reply-To: <8735lh6vqo.fsf@oldenburg.str.redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Jan 2022 12:07:14 -0000 On 21/01/2022 10:53, Florian Weimer wrote: > * Adhemerval Zanella via Libc-alpha: > >> The __convert_scm_timestamps() only updates the control message last >> pointer for SOL_SOCKET type, so if the message control buffer contains >> multiple ancillary message types the converted timestamp one might >> overwrite a valid message. > > s/()/ function/? Ack. > >> The test check if the extra ancillary space is correctly handled > > typo: the test check[s] Ack. > >> by recvmsg/recvmmsg, where if there is no extra space for the 64-bit >> time_t converted message the control buffer should be marked with >> MSG_TRUNC. It also check if recvmsg/recvmmsg handle correctly multiple >> ancillary data. > > typo: MSG_[C]TRUNC > > (I think this MSG_CTRUNC result is a remaining bug we need to fix > separately for time32 mode.) Do you mean not returning the MSG_CTRUNC? > >> diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c >> index 00c934c413..36976c276f 100644 >> --- a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c >> +++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c >> @@ -54,6 +54,8 @@ __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) >> cmsg != NULL; >> cmsg = CMSG_NXTHDR (msg, cmsg)) >> { >> + last = cmsg; >> + >> if (cmsg->cmsg_level != SOL_SOCKET) >> continue; >> >> @@ -75,8 +77,6 @@ __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) >> tvts[1] = tmp[1]; >> break; >> } >> - >> - last = cmsg; >> } >> >> if (last == NULL || type == 0) > > I think the last == NULL check is now redundant. It's probably clearer > to remove it. Indeed. > >> @@ -88,10 +88,11 @@ __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) >> return; >> } > > (The necessary length check is not visible in the patch, but it is > there.) > >> + /* Zero memory for the new cmsghdr, required by CMSG_NXTHDR. */ > > This is specifically about the cmsg_len field, right? Maybe mention > this in the comment. I changed to /* Zero memory for the new cmsghdr, so reading cmsg_len field by CMSG_NXTHDR does not trigger UB. */ > >> + memset (msg->msg_control + msg->msg_controllen, 0, >> + CMSG_SPACE (sizeof tvts)); >> msg->msg_controllen += CMSG_SPACE (sizeof tvts); >> - cmsg = CMSG_NXTHDR(msg, last); >> - if (cmsg == NULL) >> - return; >> + cmsg = CMSG_NXTHDR (msg, last); >> cmsg->cmsg_level = SOL_SOCKET; >> cmsg->cmsg_type = type; >> cmsg->cmsg_len = CMSG_LEN (sizeof tvts); > > CMSG_NXTHDR cannot be NULL anymore because of the previous length check > and cmsg_len set to zero. Okay. > >> diff --git a/sysdeps/unix/sysv/linux/tst-socket-timestamp.c b/sysdeps/unix/sysv/linux/tst-socket-timestamp.c >> new file mode 100644 >> index 0000000000..3854c46bad >> --- /dev/null >> +++ b/sysdeps/unix/sysv/linux/tst-socket-timestamp.c >> @@ -0,0 +1,344 @@ > >> +/* Some extra space added for ancillary data, it might be used to convert >> + 32-bit timestamp to 64-bit for _TIME_BITS=64. */ >> +enum { slack_max_size = 64 }; >> +static const int slack[] = { 0, 4, 8, 16, 32, slack_max_size }; > > I was worried whether 4 is okay here, but we read the cmsg buffer via > memcpy, so we always have sufficient alignment. > >> + >> +static bool support_64_timestamp; >> +/* AF_INET socket and address used to send and receive data. */ >> +static int srv; >> +static struct sockaddr_in srv_addr; > > I think the comment should mention receiving only. Ack. > >> + >> +static int >> +do_sendto (const struct sockaddr_in *addr, int nmsgs) >> +{ >> + int s = xsocket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); >> + xconnect (s, (const struct sockaddr *) addr, sizeof (*addr)); >> + >> + for (int i = 0; i < nmsgs; i++) >> + xsendto (s, &i, sizeof (i), 0, (const struct sockaddr *) addr, >> + sizeof (*addr)); >> + >> + return 0; >> +} > > Missing xclose (s). Ack. > >> +static void >> +do_recvmsg_slack_ancillary (bool use_multi_call, int s, void *cmsg, >> + size_t slack, size_t tsize, int exp_msg) >> +{ >> + int msg; > > (This is actually the payload.) > >> + /* If there is not timestamp in the ancilliary data, recvmsg should set >> + the flag inidcating it. */ > > typo: in[di]cating Ack. > >> + if (exp_timestamp && !timestamp) >> + TEST_VERIFY (mmhdr.msg_hdr.msg_flags & MSG_TRUNC); > > Shouldn't this be MSG_CTRUNC? I expect this is actually dead code. Indeed, I think this is a leftover when I was writing the patch. I will remove it. > >> +static void >> +do_test_slack_space (void) >> +{ >> + /* Setup the ancillary data buffer with an extra page with PROT_NONE to >> + check the possible timestamp conversion on some systems. */ >> + struct support_next_to_fault nf = >> + support_next_to_fault_allocate (slack_max_size); >> + void *msgbuf = nf.buffer + slack_max_size; >> + >> + /* Enable the timestamp using struct timeval precision. */ >> + { >> + int r = setsockopt (srv, SOL_SOCKET, SO_TIMESTAMP, &(int){1}, >> + sizeof (int)); >> + TEST_VERIFY_EXIT (r != -1); >> + } >> + /* Check recvmsg. */ >> + do_sendto (&srv_addr, array_length (slack)); >> + for (int s = 0; s < array_length (slack); s++) { > > style: { should be on its own line (repeated below). Ack. > >> +/* Check if the converted 64-bit timestamp is correctly appended when there >> + are multiple ancillary messages. */ >> +static void >> +do_recvmsg_multiple_ancillary (bool use_multi_call, int s, void *cmsg, >> + size_t cmsgsize, int exp_msg) >> +{ > >> + /* If there is no timestamp in the ancillary data, recvmsg should set >> + the flag to indicate it. */ >> + if (!timestamp) >> + TEST_VERIFY (mmhdr.msg_hdr.msg_flags & MSG_TRUNC); > > MSG_CTRUNC? (see above) Ack, I will remove it.