All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Clemens Ladisch <clemens@ladisch.de>
Cc: alsa-devel@alsa-project.org
Subject: Re: [PATCH] ALSA: pcm: Fix possible inconsistent appl_ptr update via mmap
Date: Sun, 18 Jun 2017 21:07:27 +0200	[thread overview]
Message-ID: <s5ho9tlm7ww.wl-tiwai@suse.de> (raw)
In-Reply-To: <b5dbac0c-cbaa-b267-21ad-4d4c3aa3036c@ladisch.de>

On Sun, 18 Jun 2017 20:03:34 +0200,
Clemens Ladisch wrote:
> 
> Takashi Iwai wrote:
> > The ALSA PCM core refers to the appl_ptr value stored on the mmapped
> > page that is shared between kernel and user-space.  Although the
> > reference is performed in the PCM stream lock, it doesn't guarantee
> > the atomic access when the value gets updated concurrently from the
> > user-space on another CPU.
> >
> > In most of codes, this is no big problem, but still there are a few
> > places that may result in slight inconsistencies because they access
> > runtime->control->appl_ptr multiple times; that is, the second read
> > might be a different value from the first value.  It can be even
> > backward or jumping, as we have no control for it.  Hence, the
> > calculation may give an unexpected value.  Luckily, there is no
> > security vulnerability by that, as far as I've checked.  But still we
> > should address it.
> >
> > This patch tries to reduce such possible cases.  The fix is simple --
> > we just read once, store it to a local variable and use it for the
> > rest calculations.
> 
> It is likely that the compiler's optimizer already merged multiple
> accesses and used a temporary register.
>
> However, if the compiler can do this transformation, it can also do the
> reverse transformation.  So this new code does not actually guarantee
> that there is a single atomic access.  That can be enforced only with
> READ_ONCE():

Good point, we need to assure by that.
Will respin the patch.


thanks,

Takashi


> >  	if (runtime->silence_size < runtime->boundary) {
> >  		snd_pcm_sframes_t noise_dist, n;
> > -		if (runtime->silence_start != runtime->control->appl_ptr) {
> > -			n = runtime->control->appl_ptr - runtime->silence_start;
> > +		snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
> 
> 		snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
> etc.
> 
> 
> Regards,
> Clemens
> 

      reply	other threads:[~2017-06-18 19:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-16 20:40 [PATCH] ALSA: pcm: Fix possible inconsistent appl_ptr update via mmap Takashi Iwai
2017-06-18 10:49 ` Takashi Sakamoto
2017-06-18 17:23   ` Takashi Iwai
2017-06-19 12:10     ` Takashi Sakamoto
2017-06-19 12:24       ` Takashi Iwai
2017-06-18 18:03 ` Clemens Ladisch
2017-06-18 19:07   ` Takashi Iwai [this message]

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=s5ho9tlm7ww.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=clemens@ladisch.de \
    /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.