From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 2B5E9399C01F for ; Fri, 4 Jun 2021 19:30:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2B5E9399C01F Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-475-gCH2CAQTOFyRmAdvCqu_mg-1; Fri, 04 Jun 2021 15:30:37 -0400 X-MC-Unique: gCH2CAQTOFyRmAdvCqu_mg-1 Received: by mail-qt1-f198.google.com with SMTP id b20-20020ac87fd40000b02901e1370c5e12so5784225qtk.17 for ; Fri, 04 Jun 2021 12:30:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=bxN2o7wbH3GnV7ftA1PTWQac2j46MZW6gqENPezPc98=; b=DzAzI+vwKHsIJxuSQIEW7XlBhBF9+Xw0T2Y1jt37p/pv69QWYREPIefBr/XYU5tU+k jd4wl9GwGdAAdHypl07khR4bblVLZBPaLQQLOsyeoZyXQ39CQNTwr4OiKYwSm0xotzAT PR77U0IEHRiKjK0R9xSH+fqvUSSa1lGE6jkbv3wXnGAQz28xwtddykCpTK5YGF1jq+C4 5Cm1HYvx5Wg8On+GeMoVqtCqtxe9VCFZdu8F7et1rfDMEybM1deO28PKMTSSFWn4/E1W JLe3zYS4bC5K1+CIzZNSeL/Vxuad/dYbzKvD0B3l/R19leFll8yR4HQElu8d4dEIhhh+ Mlgg== X-Gm-Message-State: AOAM530BK90flTaMk74d5jiMhmv5XcMKwqIZbUN2iRZzJwRd9Zvhmo3y T0Xe2KXjEHYL5dwNOkOZJslT2NyFeC3kbTXt60yNbmn9pONpD10dHboGycjOBLaW6H3ecqphRuU VwCR0xDLrnlAR/MDdOfK3 X-Received: by 2002:ae9:c010:: with SMTP id u16mr5879510qkk.133.1622835036067; Fri, 04 Jun 2021 12:30:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxlVeRU6J7BAlJtN+JrgXC3VpajUYKYk36tTh+Z1XyNwPDPTmwCbTf9Id321qirMST+CDG28A== X-Received: by 2002:ae9:c010:: with SMTP id u16mr5879445qkk.133.1622835034890; Fri, 04 Jun 2021 12:30:34 -0700 (PDT) Received: from [192.168.1.16] (198-84-214-74.cpe.teksavvy.com. [198.84.214.74]) by smtp.gmail.com with ESMTPSA id l65sm4516490qke.7.2021.06.04.12.30.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 04 Jun 2021 12:30:34 -0700 (PDT) From: Carlos O'Donell Subject: Re: [PATCH v2 05/25] linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO To: Adhemerval Zanella , libc-alpha@sourceware.org References: <20210518205613.1487824-1-adhemerval.zanella@linaro.org> <20210518205613.1487824-6-adhemerval.zanella@linaro.org> Organization: Red Hat Message-ID: Date: Fri, 4 Jun 2021 15:30:33 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: <20210518205613.1487824-6-adhemerval.zanella@linaro.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Fri, 04 Jun 2021 19:30:50 -0000 On 5/18/21 4:55 PM, Adhemerval Zanella wrote: > The constant values will be changed for __TIMESIZE=64, so binaries built > with 64-bit time support might fail to work properly on old kernels. > Both {get,set}sockopt will retry the syscall with the old constant > values and the timeout value adjusted when kernel returns ENOTPROTOPT. > > It also adds an internal only SO_{RCV,SND}TIMEO where > COMPAT_SO_{RCV,SND}TIMEO_OLD indicates pre 32 bit time support and > COMPAT_SO_{RCV,SND}TIMEO_NEW indicates time64 support. It allows to > refer to constant independently of the time_t ABI and kernel version > used. > > Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15 > kernel). Requesting a v3 please. This needs fixing to match POSIX requirements for silent truncation. I agree with Florian, if POSIX requires it, and the Kernel truncates, then we should keep doing the same. There don't seem to be much better alternatives that don't themselves have corner cases that break existing code that expects these behaviours. > --- > sysdeps/unix/sysv/linux/getsockopt.c | 67 +++++++++++++++-- > .../sysv/linux/hppa/socket-constants-time64.h | 30 ++++++++ > .../sysv/linux/mips/socket-constants-time64.h | 30 ++++++++ > .../linux/powerpc/socket-constants-time64.h | 30 ++++++++ > sysdeps/unix/sysv/linux/setsockopt.c | 71 ++++++++++++++++--- > .../unix/sysv/linux/socket-constants-time64.h | 30 ++++++++ > .../linux/sparc/socket-constants-time64.h | 30 ++++++++ > 7 files changed, 273 insertions(+), 15 deletions(-) > create mode 100644 sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h > create mode 100644 sysdeps/unix/sysv/linux/mips/socket-constants-time64.h > create mode 100644 sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h > create mode 100644 sysdeps/unix/sysv/linux/socket-constants-time64.h > create mode 100644 sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h > > diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c > index 76ee8a94d6..c8e502d976 100644 > --- a/sysdeps/unix/sysv/linux/getsockopt.c > +++ b/sysdeps/unix/sysv/linux/getsockopt.c > @@ -15,16 +15,15 @@ > License along with the GNU C Library; if not, see > . */ > > -#include > -#include > #include > - > +#include > +#include > #include > -#include > -#include > +#include > > -int > -__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len) > +static int > +getsockopt_syscall (int fd, int level, int optname, void *optval, > + socklen_t *len) > { > #ifdef __ASSUME_GETSOCKOPT_SYSCALL > return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len); > @@ -32,4 +31,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len) > return SOCKETCALL (getsockopt, fd, level, optname, optval, len); > #endif > } > + > +#ifndef __ASSUME_TIME64_SYSCALLS > +static int > +getsockopt32 (int fd, int level, int optname, void *optval, > + socklen_t *len) > +{ > + int r = -1; > + > + if (level != SOL_SOCKET) > + return r; > + > + switch (optname) > + { > + case COMPAT_SO_RCVTIMEO_NEW: > + case COMPAT_SO_SNDTIMEO_NEW: > + { > + if (*len < sizeof (struct __timeval64)) > + { > + __set_errno (EINVAL); > + break; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html ~~~ If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. ~~~ > + } > + > + if (optname == COMPAT_SO_RCVTIMEO_NEW) > + optname = COMPAT_SO_RCVTIMEO_OLD; > + if (optname == COMPAT_SO_SNDTIMEO_NEW) > + optname = COMPAT_SO_SNDTIMEO_OLD; > + > + struct __timeval32 tv32; > + r = getsockopt_syscall (fd, level, optname, &tv32, > + (socklen_t[]) { sizeof tv32 }); > + if (r < 0) > + break; > + struct __timeval64 *tv64 = (struct __timeval64 *) optval; > + *tv64 = valid_timeval32_to_timeval64 (tv32); > + *len = sizeof (*tv64); > + } > + } > + > + return r; > +} > +#endif > + > +int > +__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len) > +{ > + int r = getsockopt_syscall (fd, level, optname, optval, len); > + > +#ifndef __ASSUME_TIME64_SYSCALLS > + if (r == -1 && errno == ENOPROTOOPT) > + r = getsockopt32 (fd, level, optname, optval, len); > +#endif > + > + return r; > +} > weak_alias (__getsockopt, getsockopt) > diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h > new file mode 100644 > index 0000000000..ea721e0fc2 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h > @@ -0,0 +1,30 @@ > +/* Compat socket constants used in 64 bit compat code. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#ifndef _SOCKET_CONSTANTS_TIME64_H > +#define _SOCKET_CONSTANTS_TIME64_H > + > +/* The compat code requires the SO_* constants used for both 32 and 64 bit > + time_t, however they were only added on v5.1 kernel. */ > + > +#define COMPAT_SO_RCVTIMEO_OLD 4102 > +#define COMPAT_SO_SNDTIMEO_OLD 4101 > +#define COMPAT_SO_RCVTIMEO_NEW 16448 > +#define COMPAT_SO_SNDTIMEO_NEW 16449 > + > +#endif OK. > diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h > new file mode 100644 > index 0000000000..ab8bd62853 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h > @@ -0,0 +1,30 @@ > +/* Compat socket constants used in 64 bit compat code. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#ifndef _SOCKET_CONSTANTS_TIME64_H > +#define _SOCKET_CONSTANTS_TIME64_H > + > +/* The compat code requires the SO_* constants used for both 32 and 64 bit > + time_t, however they were only added on v5.1 kernel. */ > + > +#define COMPAT_SO_RCVTIMEO_OLD 4102 > +#define COMPAT_SO_SNDTIMEO_OLD 4101 > +#define COMPAT_SO_RCVTIMEO_NEW 66 > +#define COMPAT_SO_SNDTIMEO_NEW 67 > + > +#endif OK. > diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h > new file mode 100644 > index 0000000000..1e48dcca8d > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h > @@ -0,0 +1,30 @@ > +/* Compat socket constants used in 64 bit compat code. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#ifndef _SOCKET_CONSTANTS_TIME64_H > +#define _SOCKET_CONSTANTS_TIME64_H > + > +/* The compat code requires the SO_* constants used for both 32 and 64 bit > + time_t, however they were only added on v5.1 kernel. */ > + > +#define COMPAT_SO_RCVTIMEO_OLD 18 > +#define COMPAT_SO_SNDTIMEO_OLD 19 > +#define COMPAT_SO_RCVTIMEO_NEW 66 > +#define COMPAT_SO_SNDTIMEO_NEW 67 OK. > + > +#endif > diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c > index 12fd7bdcde..6505202265 100644 > --- a/sysdeps/unix/sysv/linux/setsockopt.c > +++ b/sysdeps/unix/sysv/linux/setsockopt.c > @@ -15,21 +15,76 @@ > License along with the GNU C Library; if not, see > . */ > > -#include > -#include > #include > - > +#include > +#include > #include > -#include > -#include > +#include > > -int > -setsockopt (int fd, int level, int optname, const void *optval, socklen_t len) > +static int > +setsockopt_syscall (int fd, int level, int optname, const void *optval, > + socklen_t len) > { > #ifdef __ASSUME_SETSOCKOPT_SYSCALL > - return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len); > + return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len); > #else > return SOCKETCALL (setsockopt, fd, level, optname, optval, len); > #endif > } > + > +#ifndef __ASSUME_TIME64_SYSCALLS > +static int > +setsockopt32 (int fd, int level, int optname, const void *optval, > + socklen_t len) > +{ > + int r = -1; > + > + if (level != SOL_SOCKET) > + return r; > + > + switch (optname) > + { > + case COMPAT_SO_RCVTIMEO_NEW: > + case COMPAT_SO_SNDTIMEO_NEW: > + { > + if (len < sizeof (struct __timeval64)) > + { > + __set_errno (EINVAL); > + break; Same issue as above with size. Silent truncation required. > + } > + > + struct __timeval64 *tv64 = (struct __timeval64 *) optval; > + if (! in_time_t_range (tv64->tv_sec)) > + { > + __set_errno (EOVERFLOW); > + break; > + } > + > + if (optname == COMPAT_SO_RCVTIMEO_NEW) > + optname = COMPAT_SO_RCVTIMEO_OLD; > + if (optname == COMPAT_SO_SNDTIMEO_NEW) > + optname = COMPAT_SO_SNDTIMEO_OLD; > + > + struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64); > + > + r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32)); > + } > + } > + > + return r; > +} > +#endif > + > +int > +setsockopt (int fd, int level, int optname, const void *optval, socklen_t len) > +{ > + int r = setsockopt_syscall (fd, level, optname, optval, len); > + > +#ifndef __ASSUME_TIME64_SYSCALLS > + if (r == -1 && errno == ENOPROTOOPT) > + r = setsockopt32 (fd, level, optname, optval, len); > +#endif > + > + return r; OK. > +} > weak_alias (setsockopt, __setsockopt) > diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h > new file mode 100644 > index 0000000000..e5a3777f28 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h > @@ -0,0 +1,30 @@ > +/* Compat socket constants used in 64 bit compat code. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#ifndef _SOCKET_CONSTANTS_TIME64_H > +#define _SOCKET_CONSTANTS_TIME64_H > + > +/* The compat code requires the SO_* constants used for both 32 and 64 bit > + time_t, however they were only added on v5.1 kernel. */ > + > +#define COMPAT_SO_RCVTIMEO_OLD 20 > +#define COMPAT_SO_SNDTIMEO_OLD 21 OK. Verified 20, 21. > +#define COMPAT_SO_RCVTIMEO_NEW 66 > +#define COMPAT_SO_SNDTIMEO_NEW 67 OK. Verified 66, 67. > + > +#endif OK. > diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h > new file mode 100644 > index 0000000000..b137abdeea > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h > @@ -0,0 +1,30 @@ > +/* Compat socket constants used in 64 bit compat code. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#ifndef _SOCKET_CONSTANTS_TIME64_H > +#define _SOCKET_CONSTANTS_TIME64_H > + > +/* The compat code requires the SO_* constants used for both 32 and 64 bit > + time_t, however they were only added on v5.1 kernel. */ > + > +#define COMPAT_SO_RCVTIMEO_OLD 8192 > +#define COMPAT_SO_SNDTIMEO_OLD 16384 OK. Verified 0x2000, 0x4000. > +#define COMPAT_SO_RCVTIMEO_NEW 68 > +#define COMPAT_SO_SNDTIMEO_NEW 69 OK. Verified 0x44, 0x45. > + > +#endif OK. > -- Cheers, Carlos.