All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philippe Gerum <rpm@xenomai.org>
To: Florian Bezdeka <florian.bezdeka@siemens.com>
Cc: xenomai@xenomai.org, jan.kiszka@siemens.com, chensong@kylinos.cn
Subject: Re: [RFC PATCH v2 1/4] y2038: Fixing the sem_timedwait syscall for 32 bit systems
Date: Tue, 09 Mar 2021 10:46:49 +0100	[thread overview]
Message-ID: <87wnugirqu.fsf@xenomai.org> (raw)
In-Reply-To: <20210308170206.313181-2-florian.bezdeka@siemens.com>


Florian Bezdeka <florian.bezdeka@siemens.com> writes:

> On systems using 32 bit for time_t the sem_timedwait syscall was broken
> because the function used for copying the timeout value from userspace
> to kernel (=sem_fetch_timeout()) was always copying
> sizeof(struct timespec64).
>
> A 32 bit application (or more specific an application with 4 byte
> time_t) would only provide sizeof(struct old_timespec32).
>
> Notable changes:
>   - The copy operation from userspace to kernel is now already done in
>     the syscall handler. So it is always done. Previously it was copied
>     over and validated before the first use (when used at all).
>     So we have some additional instructions now that may be
>     unnecessary, but that simplifies the code.
>
>   - Validation: Switched to timespec64_valid() instead of our own
>     check.
>
> Fixes: 8043eccd232d ("cobalt/kernel: y2038: convert struct timespec to timespec64")
> Signed-off-by: Florian Bezdeka <florian.bezdeka@siemens.com>
> ---
>  kernel/cobalt/posix/sem.c       | 40 +++++++++++++++------------------
>  kernel/cobalt/posix/sem.h       |  6 ++---
>  kernel/cobalt/posix/syscall32.c | 10 +++++++--
>  kernel/cobalt/posix/syscall32.h |  2 +-
>  4 files changed, 29 insertions(+), 29 deletions(-)
>
> diff --git a/kernel/cobalt/posix/sem.c b/kernel/cobalt/posix/sem.c
> index 467a9b7dd..827a4751a 100644
> --- a/kernel/cobalt/posix/sem.c
> +++ b/kernel/cobalt/posix/sem.c
> @@ -267,20 +267,11 @@ out:
>  	return ret;
>  }
>  
> -static inline int sem_fetch_timeout(struct timespec64 *ts,
> -				    const void __user *u_ts)
> -{
> -	return u_ts == NULL ? -EFAULT :
> -		cobalt_copy_from_user(ts, u_ts, sizeof(*ts));
> -}
> -
>  int __cobalt_sem_timedwait(struct cobalt_sem_shadow __user *u_sem,
> -			   const void __user *u_ts,
> -			   int (*fetch_timeout)(struct timespec64 *ts,
> -						const void __user *u_ts))
> +			   const struct timespec64 *ts)
>  {
> -	struct timespec64 ts = { .tv_sec = 0, .tv_nsec = 0 };
> -	int pull_ts = 1, ret, info;
> +	int ret, info;
> +	bool validate_ts = true;
>  	struct cobalt_sem *sem;
>  	xnhandle_t handle;
>  	xntmode_t tmode;
> @@ -304,24 +295,23 @@ int __cobalt_sem_timedwait(struct cobalt_sem_shadow __user *u_sem,
>  		 * it's actually more complex, to keep some
>  		 * applications ported to Linux happy.
>  		 */
> -		if (pull_ts) {
> +		if (validate_ts) {
>  			atomic_inc(&sem->state->value);
> -			xnlock_put_irqrestore(&nklock, s);
> -			ret = fetch_timeout(&ts, u_ts);
> -			xnlock_get_irqsave(&nklock, s);
> -			if (ret)

As mentioned in a previous comment on this series, this type of patch is
subtly changing where the core currently stands with respect to a
peculiar POSIX compliance issue. Checking the content of a valid
timespec struct is currently postponed until the timeout is needed, but
the validity of the timespec pointer referring to that information is
checked as late as possible too.

If the code now pre-loads the timespec struct early on in the syscall
path, before the timed services are called, the pointer is explicitly
checked for validity before we can decide if that timeout information is
going to be used.

e.g.

struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000000 };
sem_init(&sem, 0, 0);
sem_post(&sem);
sem_timedwait(&sem, &ts); /* should not fail, and won't as expected. */

but,

sem_init(&sem, 0, 0);
sem_post(&sem);
sem_timedwait(&sem, (void *)0xdeadbeefUL); /* should not fail, but will. */

Since the standard does not mandates such behavior but seems to tag it
as an implementation-dependent option ("The validity of the abstime need
not be checked if..."), the change would still be acceptable POSIX-wise
I believe. However, I'm pretty sure that there are POSIX compliance test
suites around which would start reporting failures due to this
change.

-- 
Philippe.


  parent reply	other threads:[~2021-03-09  9:46 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-20 15:18 [PATCH 1/5] cobalt/kernel: y2038: convert struct timeval to __kernel_old_timeval Philippe Gerum
2021-02-20 15:18 ` [PATCH 2/5] cobalt/kernel: y2038: convert struct timespec to timespec64 Philippe Gerum
2021-02-21  2:17   ` chensong
2021-02-21 15:27     ` Philippe Gerum
2021-02-22  1:04       ` chensong
2021-02-22  8:21       ` florian.bezdeka
2021-02-22  9:08         ` Philippe Gerum
2021-03-08 12:19           ` Jan Kiszka
2021-03-08 14:01             ` Philippe Gerum
2021-03-08 14:03               ` Jan Kiszka
2021-03-03 10:05   ` florian.bezdeka
2021-03-04  9:35     ` Philippe Gerum
2021-03-04  9:49       ` florian.bezdeka
2021-03-04  9:55         ` Philippe Gerum
2021-03-04  9:59           ` florian.bezdeka
2021-03-04 10:08           ` Philippe Gerum
2021-03-04 11:36             ` [y2038][RFC PATCH 0/2] Pattern for implementing y2038 syscalls Florian Bezdeka
2021-03-04 11:36               ` [y2038][RFC PATCH 1/2] y2038: Fixing the sem_timedwait syscall for 32 bit systems Florian Bezdeka
2021-03-04 15:11                 ` Philippe Gerum
2021-03-04 15:22                   ` florian.bezdeka
2021-03-04 15:42                     ` Philippe Gerum
2021-03-08 17:02                       ` [RFC PATCH v2 0/4] Pattern for implementing y2038 syscalls Florian Bezdeka
2021-03-08 17:02                         ` [RFC PATCH v2 1/4] y2038: Fixing the sem_timedwait syscall for 32 bit systems Florian Bezdeka
2021-03-08 17:12                           ` Jan Kiszka
2021-03-08 18:11                             ` florian.bezdeka
2021-03-08 18:22                               ` Jan Kiszka
2021-03-09  9:46                           ` Philippe Gerum [this message]
2021-03-09 11:08                             ` florian.bezdeka
2021-03-08 17:02                         ` [RFC PATCH v2 2/4] y2038: Adding sem_timedwait_time64 Florian Bezdeka
2021-03-08 18:28                           ` Jan Kiszka
2021-03-09  7:53                             ` florian.bezdeka
2021-03-10  7:51                               ` florian.bezdeka
2021-03-10  9:46                                 ` chensong
2021-03-08 17:02                         ` [RFC PATCH v2 3/4] y2038: Add tests for the sc_cobalt_sem_timedwait_time64 syscall Florian Bezdeka
2021-03-08 18:35                           ` Jan Kiszka
2021-03-09  8:00                             ` florian.bezdeka
2021-03-08 17:02                         ` [RFC PATCH v2 4/4] y2038: lib/cobalt: Add support of sc_cobalt_sem_timedwait_time64 Florian Bezdeka
2021-03-10 13:09                           ` [y2038][PATCH v3 0/4] Pattern for implementing y2038 syscalls Florian Bezdeka
2021-03-10 13:09                             ` [y2038][PATCH v3 1/4] y2038: Fixing the sem_timedwait syscall for 32 bit systems Florian Bezdeka
2021-03-10 13:09                             ` [y2038][PATCH v3 2/4] y2038: Adding sem_timedwait64 Florian Bezdeka
2021-03-10 13:09                             ` [y2038][PATCH v3 3/4] y2038: Add tests for the sc_cobalt_sem_timedwait64 syscall Florian Bezdeka
2021-03-10 13:09                             ` [y2038][PATCH v3 4/4] y2038: lib/cobalt: Add support of sc_cobalt_sem_timedwait64 Florian Bezdeka
2021-03-11  2:38                               ` chensong
2021-03-04 11:36               ` [y2038][RFC PATCH 2/2] y2038: Adding sem_timedwait_time64 Florian Bezdeka
2021-03-05  5:49                 ` chensong
2021-03-05  6:36                   ` florian.bezdeka
2021-03-05  7:42                     ` chensong
2021-03-05  7:53                       ` florian.bezdeka
2021-03-08 12:59             ` [PATCH 2/5] cobalt/kernel: y2038: convert struct timespec to timespec64 Jan Kiszka
2021-03-10 17:52             ` florian.bezdeka
2021-03-10 18:14               ` Philippe Gerum
2021-03-03 14:36   ` florian.bezdeka
2021-03-04  9:44     ` Philippe Gerum
2021-03-04  9:57       ` florian.bezdeka
2021-02-20 15:18 ` [PATCH 3/5] lib: y2038: convert to internal timespec type Philippe Gerum
2021-02-20 15:18 ` [PATCH 4/5] cobalt/kernel: y2038: convert struct itimerspec to itimerspec64 Philippe Gerum
2021-02-20 15:18 ` [PATCH 5/5] cobalt/kernel: y2038: convert struct timex to __kernel_timex Philippe Gerum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wnugirqu.fsf@xenomai.org \
    --to=rpm@xenomai.org \
    --cc=chensong@kylinos.cn \
    --cc=florian.bezdeka@siemens.com \
    --cc=jan.kiszka@siemens.com \
    --cc=xenomai@xenomai.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.