[PATCH v5 2/3] linux: Fix ancillary 64-bit time timestamp conversion (BZ #28349, BZ#28350)

Szabolcs Nagy szabolcs.nagy@arm.com
Fri Sep 30 10:47:22 GMT 2022


The 01/27/2022 17:15, Adhemerval Zanella via Libc-alpha wrote:
> 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.
> 
> The test checks if the extra ancillary space is correctly handled
> 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.
> 
> Checked on x86_64-linux and on i686-linux-gnu on both 5.11 and
> 4.15 kernel.
> 
> Co-authored-by: Fabian Vogt <fvogt@suse.de>

note: the time64 recvmsg test started to fail on 32bit
arm after i updated my aarch64 kernel to 5.18

FAIL: socket/tst-socket-timestamp-time64

../sysdeps/unix/sysv/linux/tst-socket-timestamp.c:136: numeric comparison failure
   left: 0 (0x0); from: timestamp
  right: 1 (0x1); from: exp_timestamp


> +static void
> +do_recvmsg_slack_ancillary (bool use_multi_call, int s, void *cmsg,
> +			    size_t slack, size_t tsize, int exp_payload)
> +{
> +  int payload;
> +  struct iovec iov =
> +    {
> +      .iov_base = &payload,
> +      .iov_len = sizeof (payload)
> +    };
> +  size_t msg_controllen = CMSG_SPACE (tsize) + slack;
> +  char *msg_control = cmsg - msg_controllen;
> +  memset (msg_control, 0x55, msg_controllen);
> +  struct mmsghdr mmhdr =
> +    {
> +      .msg_hdr =
> +      {
> +        .msg_name = NULL,
> +        .msg_namelen = 0,
> +        .msg_iov = &iov,
> +        .msg_iovlen = 1,
> +        .msg_control = msg_control,
> +        .msg_controllen = msg_controllen
> +      },
> +    };
> +
> +  int r;
> +  if (use_multi_call)
> +    {
> +      r = recvmmsg (s, &mmhdr, 1, 0, NULL);
> +      if (r >= 0)
> +	r = mmhdr.msg_len;
> +    }
> +  else
> +    r = recvmsg (s, &mmhdr.msg_hdr, 0);
> +  TEST_COMPARE (r, sizeof (int));
> +  TEST_COMPARE (payload, exp_payload);
> +
> +  if (cmsg == NULL)
> +    return;
> +
> +  /* A timestamp is expected if 32-bit timestamp are used (support in every
> +     configuration) or if underlying kernel support 64-bit timestamps.
> +     Otherwise recvmsg will need extra space do add the 64-bit timestamp.  */
> +  bool exp_timestamp;
> +  if (sizeof (time_t) == 4 || support_64_timestamp)
> +    exp_timestamp = true;
> +   else
> +    exp_timestamp = slack >= CMSG_SPACE (tsize);
> +
> +  bool timestamp = false;
> +  for (struct cmsghdr *cmsg = CMSG_FIRSTHDR (&mmhdr.msg_hdr);
> +       cmsg != NULL;
> +       cmsg = CMSG_NXTHDR (&mmhdr.msg_hdr, cmsg))
> +    {
> +      if (cmsg->cmsg_level != SOL_SOCKET)
> +	continue;
> +      if (cmsg->cmsg_type == SCM_TIMESTAMP
> +	  && cmsg->cmsg_len == CMSG_LEN (sizeof (struct timeval)))


in strace i see

recvmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\0\0\0\0", iov_len=4}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_TIMESTAMP, cmsg_data={tv_sec=1664533412, tv_usec=56794}}], msg_controllen=20, msg_flags=0}, 0) = 4

i.e. cmsg->cmsg_len == 20, but CMSG_LEN (sizeof (struct timeval)) is 28.

in the loop sometimes i also see a second cmsg where
cmsg->cmsg_type == SO_TIMESTAMP_NEW and cmsg->cmsg_len == 28,
but then the type does not match.

same behaviour with recvmmsg_time64 (but my strace is too
old to show that).

not sure how to debug this further.

> +	{
> +	  struct timeval tv;
> +	  memcpy (&tv, CMSG_DATA (cmsg), sizeof (tv));
> +	  if (test_verbose)
> +	    printf ("SCM_TIMESTAMP:   {%jd, %jd}\n", (intmax_t)tv.tv_sec,
> +		    (intmax_t)tv.tv_usec);
> +	  timestamp = true;
> +	}
> +      else if (cmsg->cmsg_type == SCM_TIMESTAMPNS
> +	       && cmsg->cmsg_len == CMSG_LEN (sizeof (struct timespec)))
> +	{
> +	  struct timespec ts;
> +	  memcpy (&ts, CMSG_DATA (cmsg), sizeof (ts));
> +	  if (test_verbose)
> +	    printf ("SCM_TIMESTAMPNS: {%jd, %jd}\n", (intmax_t)ts.tv_sec,
> +		    (intmax_t)ts.tv_nsec);
> +	  timestamp = true;
> +	}
> +    }
> +
> +  TEST_COMPARE (timestamp, exp_timestamp);

this fails with false != true.


More information about the Libc-alpha mailing list