linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@arndb.de>
To: Hans Verkuil <hverkuil@xs4all.nl>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>,
	Linux Media Mailing List <linux-media@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	y2038 Mailman List <y2038@lists.linaro.org>
Subject: Re: [PATCH v4 2/8] media: v4l2: abstract timeval handling in v4l2_buffer
Date: Tue, 26 Nov 2019 13:42:19 +0100	[thread overview]
Message-ID: <CAK8P3a3FjD4wYv4NNQrKaGCEOte6k5gtOghWuAJRhUk5rdDxPw@mail.gmail.com> (raw)
In-Reply-To: <efdeef8a-5ce7-bbe5-8def-e4eec31f13ab@xs4all.nl>

On Tue, Nov 26, 2019 at 12:43 PM Hans Verkuil <hverkuil@xs4all.nl> wrote:
> On 11/26/19 12:34 PM, Arnd Bergmann wrote:
> > On Mon, Nov 25, 2019 at 4:52 PM Hans Verkuil <hverkuil@xs4all.nl> wrote:
> >>>
> >>> +static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)
> >>> +{
> >>> +     return buf->timestamp.tv_sec * NSEC_PER_SEC +
> >>> +            (u32)buf->timestamp.tv_usec * NSEC_PER_USEC;
> >>
> >> Why the (u32) cast?
> >
> > Simple question, long answer:
> >
> > on 32-bit architectures, the tv_usec member may be 32-bit wide plus
> > padding in user space when interpreted as a regular 'struct timeval',
> > but the kernel implementation now sees it as a 64-bit member,
> > with half of it being possibly uninitialized user space data.
> >
> > The 32-bit cast avoids that uninitialized data and ensures user space
> > passing garbage in the upper half gets ignored, as it has to be on 32-bit
> > user space.
>
> But that's only valid for little endian 32 bit systems, right?
> Is this only an issue for x86 platforms?

Uninitialized data is an issue on all 32-bit architectures. The layout
of the new timeval is such that the low 32 bits of tv_sec are in the
same place on both 32-bit and 64-bit architectures of the same
endianess, but if an application initializes the fields individually
without a memset before it, it may still pass invalid data.

> > On 64-bit native user space, the tv_usec field is always 64 bit wide,
> > so this is a change in behavior for denormalized timeval data
> > with tv_usec > U32_MAX, but the current behavior does not appear
> > worth preserving either.
> >
> > The correct way would probably be to return an error for
> >  tv_usec >USEC_PER_SEC, but as the code never did that, this
> > would risk a regression for user space that relies on passing
> > invalid timestamps without getting an error.
>
> This long answer needs to be added to a comment to that function.
> Because otherwise someone will come along later and remove that
> seemingly unnecessary cast.
>
> It's OK if it is a long comment, it's a non-trivial reason.

Added this comment now:

        /*
         * When the timestamp comes from 32-bit user space, there may be
         * uninitialized data in tv_usec, so cast it to u32.
         * Otherwise allow invalid input for backwards compatibility.
         */

Let me know if you prefer a more elaborate version.

> >> so media/v4l2-common.h would be a good place.
> >
> > Ok, sounds good. I wasn't sure where to put it, and ended up
> > with include/linux/videodev2.h as the best replacement for
> > include/uapi/linux/videodev2.h, changed it to
> > include/media/v4l2-common.h now.
>
> Never use include/linux/videodev2.h. It's just a wrapper around
> the uapi header and should not contain any 'real' code.
>
> It's also why I missed that you modified that header since we never
> touch it.

Ok, got it. I now tried to remove this file completely, hoping that the
include <linux/time.h> is no longer needed after my series, but
it seems we still need it.

       Arnd

  reply	other threads:[~2019-11-26 12:42 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-11 20:38 [PATCH v4 0/8] y2038 safety in v4l2 Arnd Bergmann
2019-11-11 20:38 ` [PATCH v4 1/8] media: documentation: fix video_event description Arnd Bergmann
2019-11-11 20:38 ` [PATCH v4 2/8] media: v4l2: abstract timeval handling in v4l2_buffer Arnd Bergmann
2019-11-25 15:52   ` Hans Verkuil
2019-11-26 11:34     ` Arnd Bergmann
2019-11-26 11:43       ` Hans Verkuil
2019-11-26 12:42         ` Arnd Bergmann [this message]
2019-11-11 20:38 ` [PATCH v4 3/8] media: v4l2-core: compat: ignore native command codes Arnd Bergmann
2019-11-11 20:38 ` [PATCH v4 4/8] media: v4l2-core: split out data copy from video_usercopy Arnd Bergmann
2019-11-11 20:38 ` [PATCH v4 5/8] media: v4l2-core: fix VIDIOC_DQEVENT for time64 ABI Arnd Bergmann
2019-11-25 14:40   ` Hans Verkuil
2019-11-26 14:43     ` Arnd Bergmann
2019-11-26 15:10       ` Hans Verkuil
2019-11-26 15:19         ` Arnd Bergmann
2019-11-11 20:38 ` [PATCH v4 6/8] media: v4l2-core: fix v4l2_buffer handling " Arnd Bergmann
2019-11-25 14:57   ` Hans Verkuil
2019-11-26 13:50     ` Arnd Bergmann
2019-11-26 14:15       ` Hans Verkuil
2019-11-26 15:17         ` Arnd Bergmann
2019-11-26 15:24           ` Hans Verkuil
2019-11-11 20:38 ` [PATCH v4 7/8] media: v4l2-core: fix compat VIDIOC_DQEVENT " Arnd Bergmann
2019-11-11 20:38 ` [PATCH v4 8/8] media: v4l2-core: fix compat v4l2_buffer handling " Arnd Bergmann
2019-11-25 16:02 ` [PATCH v4 0/8] y2038 safety in v4l2 Hans Verkuil
2019-11-26 11:13   ` Arnd Bergmann

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=CAK8P3a3FjD4wYv4NNQrKaGCEOte6k5gtOghWuAJRhUk5rdDxPw@mail.gmail.com \
    --to=arnd@arndb.de \
    --cc=hverkuil@xs4all.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=y2038@lists.linaro.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).