Closed
Bug 1007223
Opened 11 years ago
Closed 11 years ago
AudioContext::DecodeAudioData passes ArrayBuffer data across threads without transferring it
Categories
(Core :: Web Audio, defect)
Core
Web Audio
Tracking
()
VERIFIED
FIXED
mozilla32
People
(Reporter: Waldo, Assigned: padenot)
References
Details
(Keywords: sec-high, Whiteboard: [adv-main30+])
Attachments
(5 files, 3 obsolete files)
4.43 KB,
patch
|
ehsan.akhgari
:
review+
|
Details | Diff | Splinter Review |
2.15 KB,
patch
|
Details | Diff | Splinter Review | |
1.67 KB,
patch
|
ehsan.akhgari
:
review+
|
Details | Diff | Splinter Review |
6.54 KB,
patch
|
ehsan.akhgari
:
review+
bholley
:
review+
lsblakk
:
approval-mozilla-aurora+
lsblakk
:
approval-mozilla-beta+
|
Details | Diff | Splinter Review |
7.88 KB,
patch
|
ehsan.akhgari
:
review+
|
Details | Diff | Splinter Review |
void
AudioContext::DecodeAudioData(const ArrayBuffer& aBuffer,
DecodeSuccessCallback& aSuccessCallback,
const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback)
{
// Sniff the content of the media.
// Failed type sniffing will be handled by AsyncDecodeMedia.
nsAutoCString contentType;
NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr,
aBuffer.Data(), aBuffer.Length(),
contentType);
nsRefPtr<DecodeErrorCallback> failureCallback;
if (aFailureCallback.WasPassed()) {
failureCallback = &aFailureCallback.Value();
}
nsRefPtr<WebAudioDecodeJob> job(
new WebAudioDecodeJob(contentType, this, aBuffer,
&aSuccessCallback, failureCallback));
mDecoder.AsyncDecodeMedia(contentType.get(),
aBuffer.Data(), aBuffer.Length(), *job);
// Transfer the ownership to mDecodeJobs
mDecodeJobs.AppendElement(job);
}
When |aBuffer| is passed to |WebAudioDecodeJob|, the underlying object is cached in the decode job. We pass in the length and the raw data from the ArrayBuffer into AsyncDecodeMedia. In there we create a |new MediaDecodeTask(aContentType, aBuffer, aLength, aDecodeJob, mThreadPool);|, then |CreateReader()| on it. That creates a |BufferMediaResource| passing in the raw buffer pointer. And |BufferMediaResource| does all sorts of stuff to what's in the pointer/length pair. Boo-urns. Or am I misreading this somehow?
(Of course, this is all problematic because the buffer passed into decodeAudioData is user-controlled and can be neutered at any point after the call completes [but before the decoding task stuff completes]. Or the data could be moved around there's sufficiently little of it and someone does something that requests a *stable* address for the data, causing the data to be copied into heap-allocated permanent [modulo neutering] storage. Just to absolutely clear to any readers not fully on top of this.)
Comment 1•11 years ago
|
||
Looking at the spec at https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#dfn-decodeAudioData it looks like we're supposed to neuter the arraybuffer, no?
We're also supposed to un-neuter.... but that seems lossy, since neutering will affect all the views, no? I think this spec is bunk. :(
Flags: needinfo?(ehsan)
Comment 2•11 years ago
|
||
I guess http://webaudio.github.io/web-audio-api/#widl-AudioContext-decodeAudioData-Promise-AudioBuffer--ArrayBuffer-audioData-DecodeSuccessCallback-successCallback-DecodeErrorCallback-errorCallback is the actual current version, but it has the same issue.
Comment 3•11 years ago
|
||
Ah, yes... We should neuter the buffer here indeed. Paul, can you take this please?
Flags: needinfo?(ehsan) → needinfo?(paul)
Comment 4•11 years ago
|
||
The point is, the spec says the buffer should be "un-neutered" in some cases, but there is no such thing. Once you neuter a buffer, it stays neutered forever. So the spec needs to either copy, or neuter and move on.
Flags: needinfo?(ehsan)
Comment 5•11 years ago
|
||
Oh, sorry, I read the bug too fast.
So I tried to remember how we got to the status quo. This goes back to October 2012 when I brought up the original issue: <http://lists.w3.org/Archives/Public/public-audio/2012OctDec/0419.html>. There we convinced Chris Rogers to change the spec, but the only thing being discussed in that thread was neutering the typed array of the AudioBuffer that gets passed to decodeAudioData. We finally agreed and I filed this spec bug <https://www.w3.org/Bugs/Public/show_bug.cgi?id=20161> where Chris changed the spec, but it seemed like he perhaps mistakenly added the prose about restoring. But then I filed bug 832098 (which this bug is a dupe of) and apparently didn't pay too much attention to the actual text that went into the spec, and I apologize for that. :(
I think what we need to do here is comment 3, and change the spec to remove the prose about restoring the typed array.
Flags: needinfo?(ehsan)
Assignee | ||
Comment 7•11 years ago
|
||
Ehsan, here is the patch with a test. To me, it seems to work, but I'm not super confident I got it right, because it's kind of my first time doing this kind of stuff.
In particular, I'm not sure if neutering the ArrayBuffer affects the lifetime of the underlying memory: should we free it ourself, or is there some gc magic that'll do it for us?
Also, the spec bug is file here: https://github.com/WebAudio/web-audio-api/issues/319
Attachment #8421013 -
Flags: review?(ehsan)
Flags: needinfo?(paul)
Comment 8•11 years ago
|
||
Comment on attachment 8421013 [details] [diff] [review]
neuter
Review of attachment 8421013 [details] [diff] [review]:
-----------------------------------------------------------------
::: content/media/webaudio/AudioContext.cpp
@@ +446,5 @@
> + AutoPushJSContext cx(GetJSContext());
> +
> + size_t length = aBuffer.Length();
> + JS::RootedObject obj(cx, aBuffer.Obj());
> + uint8_t* data = static_cast<uint8_t*>(JS_StealArrayBufferContents(cx, obj));
This takes ownership over the underlying buffer, you need to free it using JS_Free() once the WebAudioDecodeJob object is done with this.
::: content/media/webaudio/MediaBufferDecoder.h
@@ +31,5 @@
> // You may omit both the success and failure callback, or you must pass both.
> // The callbacks are only necessary for asynchronous operation.
> WebAudioDecodeJob(const nsACString& aContentType,
> dom::AudioContext* aContext,
> + JSObject* aBuffer,
I _think_ not using a HandleObject here is fine because you're sticking the value inside a JS::Heap<> immediately, but please make sure that the rooting analysis bot on the try server agrees with me. ;-)
Attachment #8421013 -
Flags: review?(ehsan) → review-
Reporter | ||
Comment 9•11 years ago
|
||
Comment on attachment 8421013 [details] [diff] [review]
neuter
Review of attachment 8421013 [details] [diff] [review]:
-----------------------------------------------------------------
::: content/media/webaudio/MediaBufferDecoder.cpp
@@ +492,3 @@
> MOZ_COUNT_CTOR(WebAudioDecodeJob);
>
> + mArrayBuffer = aBuffer;
Erm. Why should, or does, this class even accept and store a JSObject* in the first place? Why can't we just remove it entirely?
There's something fairly scary, seems to me, about storing pointers to JS heap stuff in objects that are accessed on different threads, as here (if memory serves). Just remove the argument and member field entirely.
::: content/media/webaudio/test/test_decodeAudioDataNeuter.html
@@ +24,5 @@
> + ok(xhr.response.byteLength == 0, "The source ArrayBuffer is neutered after decoding.");
> + SimpleTest.finish();
> + }, function() {
> + ok(false, "We could not decode the file.");
> + });
Given the neutering is "supposed" to happen sync, you should test byteLength immediately after the decodeAudioData call as well as in the success/fail continuations.
Note that because of this being a security bug, this test shouldn't land with the patch. I typically post patch and test as separate attachments with separate review requests, so make clear that sec-approvals and landing requests and such don't conflate the two.
Assignee | ||
Comment 10•11 years ago
|
||
The test, split off.
Assignee | ||
Updated•11 years ago
|
Assignee: nobody → paul
Assignee | ||
Comment 11•11 years ago
|
||
I went with Waldo's suggestion, since we don't need for JS to know what we do
with the array anymore, and we free it by hand when we are done.
I could not find JS_Free. js_free and JS_free both exist, I went with the latter
because JS_* are more commonly used in Gecko, passing a nullptr as a first arg,
because that's allowed.
Attachment #8421650 -
Flags: review?(ehsan)
Assignee | ||
Comment 12•11 years ago
|
||
Rollup of the initial patch + the interdiff.
Assignee | ||
Updated•11 years ago
|
Attachment #8421013 -
Attachment is obsolete: true
Assignee | ||
Comment 13•11 years ago
|
||
forgot to qref
Assignee | ||
Updated•11 years ago
|
Attachment #8421641 -
Attachment is obsolete: true
Assignee | ||
Updated•11 years ago
|
Attachment #8421654 -
Attachment description: Neuter the ArrayBuffer passed to decodeAudioData. r= → rollup
Updated•11 years ago
|
Attachment #8421650 -
Flags: review?(ehsan) → review+
Comment 14•11 years ago
|
||
(In reply to Paul Adenot (:padenot) from comment #11)
> I could not find JS_Free. js_free and JS_free both exist, I went with the
> latter
> because JS_* are more commonly used in Gecko, passing a nullptr as a first
> arg,
> because that's allowed.
Sorry, JS_Free was a typo. Blame it on the Shift key.
(FWIW, JS_ prefix means external, js_ means internal.)
Assignee | ||
Comment 15•11 years ago
|
||
Comment on attachment 8421654 [details] [diff] [review]
rollup
[Security approval request comment]
How easily could an exploit be constructed based on the patch?
No idea. Waldo, do you mind to quickly elaborate on that?
Do comments in the patch, the check-in comment, or tests included in the patch paint a bulls-eye on the security problem?
I split off the tests from the patch. There is a one line comment that explains what the new code is doing, we can probably remove it.
Which older supported branches are affected by this flaw?
All of them, I guess, back to Firefox 25
If not all supported branches, which bug introduced the flaw?
N/A
Do you have backports for the affected branches? If not, how different, hard to create, and risky will they be?
This code did not move much, I would expect that the patch applies on all branches.
How likely is this patch to cause regressions; how much testing does it need?
There is a change in behavior, we now aligns with the spec. I would not expect regressions.
Attachment #8421654 -
Flags: sec-approval?
Flags: needinfo?(jwalden+bmo)
Reporter | ||
Comment 16•11 years ago
|
||
(In reply to Paul Adenot (:padenot) from comment #15)
> [Security approval request comment]
> How easily could an exploit be constructed based on the patch?
> No idea. Waldo, do you mind to quickly elaborate on that?
No idea. Depends on exactly what audio decoding entails, and that I haven't the slightest idea about. Once I determine something is memory-unsafe, I tend not to investigate much beyond that point. If I just want to fix bugs, there's just not much reason to deeply consider weaponization feasibility, except for a new intellectual challenge.
> How likely is this patch to cause regressions; how much testing does it need?
> There is a change in behavior, we now aligns with the spec. I would not
> expect regressions.
There could be pages expecting the passed-in buffer to not be neutered, given no implementation neuters now (per a skim of WebKit/Blink code). Pages that expect non-neutering are Doing It Wrong. I have no idea how likely it is any would be doing this. If we're super-super-paranoid, we could decode a copy of the data in the ArrayBuffer and not neuter, leaving the neutering fix to trunk. Such a hackaround would double memory consumption; it's probably not worth the extra effort, given Web Audio is still bleeding-edge tech.
Flags: needinfo?(jwalden+bmo)
Assignee | ||
Comment 17•11 years ago
|
||
(In reply to Jeff Walden [:Waldo] (remove +bmo to email) from comment #16)
> > How likely is this patch to cause regressions; how much testing does it need?
> > There is a change in behavior, we now aligns with the spec. I would not
> > expect regressions.
>
> There could be pages expecting the passed-in buffer to not be neutered,
> given no implementation neuters now (per a skim of WebKit/Blink code).
> Pages that expect non-neutering are Doing It Wrong. I have no idea how
> likely it is any would be doing this. If we're super-super-paranoid, we
> could decode a copy of the data in the ArrayBuffer and not neuter, leaving
> the neutering fix to trunk. Such a hackaround would double memory
> consumption; it's probably not worth the extra effort, given Web Audio is
> still bleeding-edge tech.
Note that Chrome people agreed that neutering should be done (It was a google person merging my patch to the spec). They just haven't written the code yet.
I don't think we need to implement the work around you mention. I can't really think of a use case for that.
Updated•11 years ago
|
status-firefox29:
--- → wontfix
status-firefox30:
--- → affected
status-firefox31:
--- → affected
status-firefox32:
--- → affected
status-firefox-esr24:
--- → unaffected
tracking-firefox30:
--- → +
tracking-firefox31:
--- → +
tracking-firefox32:
--- → +
Comment 18•11 years ago
|
||
Comment on attachment 8421654 [details] [diff] [review]
rollup
sec-approval+ for trunk. We should get beta and aurora patches made and nominated once it is in trunk.
Attachment #8421654 -
Flags: sec-approval? → sec-approval+
Assignee | ||
Comment 19•11 years ago
|
||
I think the only thing that relied on this behaviour was our test :-)
Attachment #8423985 -
Flags: review?(ehsan)
Updated•11 years ago
|
Group: media-core-security
Comment 20•11 years ago
|
||
Comment on attachment 8423985 [details] [diff] [review]
imported patch fix-test
Review of attachment 8423985 [details] [diff] [review]:
-----------------------------------------------------------------
Haha, this is embarrassing. Sorry! ;-)
Attachment #8423985 -
Flags: review?(ehsan) → review+
Assignee | ||
Comment 21•11 years ago
|
||
JSAPI changes bitrot the patch :-(
Attachment #8426168 -
Flags: review?(ehsan)
Assignee | ||
Comment 22•11 years ago
|
||
To be specific, this just changes the way to get the context and to choose the compartment.
Comment 23•11 years ago
|
||
Comment on attachment 8426168 [details] [diff] [review]
patch
Review of attachment 8426168 [details] [diff] [review]:
-----------------------------------------------------------------
::: content/media/webaudio/AudioContext.cpp
@@ +431,5 @@
> const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback)
> {
> + AutoJSAPI jsapi;
> + JSContext* cx = jsapi.cx();
> + JSAutoCompartment comp(cx, aBuffer.Obj());
Boris can you please double check this?
Attachment #8426168 -
Flags: review?(ehsan)
Attachment #8426168 -
Flags: review?(bzbarsky)
Attachment #8426168 -
Flags: review+
Comment 24•11 years ago
|
||
Comment on attachment 8426168 [details] [diff] [review]
patch
I _think_ it's right, but this stuff is pretty new... Bobby, is this how it's meant to be used?
Attachment #8426168 -
Flags: review?(bzbarsky) → review?(bobbyholley)
Comment 25•11 years ago
|
||
Comment on attachment 8426168 [details] [diff] [review]
patch
Review of attachment 8426168 [details] [diff] [review]:
-----------------------------------------------------------------
::: content/media/webaudio/AudioContext.cpp
@@ +431,5 @@
> const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback)
> {
> + AutoJSAPI jsapi;
> + JSContext* cx = jsapi.cx();
> + JSAutoCompartment comp(cx, aBuffer.Obj());
Please call the JSAutoCompartment |ac|. AutoJSAPI usage looks good otherwise.
Attachment #8426168 -
Flags: review?(bobbyholley) → review+
Assignee | ||
Comment 26•11 years ago
|
||
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla32
Comment 28•11 years ago
|
||
Let's please get an uplift nomination asap, this needs to get into next week's Beta.
Flags: needinfo?(paul)
Assignee | ||
Comment 29•11 years ago
|
||
Comment on attachment 8426168 [details] [diff] [review]
patch
[Approval Request Comment]
Bug caused by (feature/regressing bug #): Landing of web audio (or maybe shortly after)
User impact if declined: not sure
Testing completed (on m-c, etc.): m-c, locally, has test to be landed separately, but green everywhere
Risk to taking this patch (and alternatives if risky): little behavioral change, other implementers have agreed to change their behavior as well.
String or IDL/UUID changes made by this patch: none
Attachment #8426168 -
Flags: approval-mozilla-beta?
Attachment #8426168 -
Flags: approval-mozilla-aurora?
Flags: needinfo?(paul)
Updated•11 years ago
|
Attachment #8426168 -
Flags: approval-mozilla-beta?
Attachment #8426168 -
Flags: approval-mozilla-beta+
Attachment #8426168 -
Flags: approval-mozilla-aurora?
Attachment #8426168 -
Flags: approval-mozilla-aurora+
Comment 30•11 years ago
|
||
https://hg.mozilla.org/releases/mozilla-aurora/rev/15ec20e99da2
https://hg.mozilla.org/releases/mozilla-beta/rev/5ecccdf09f33
status-b2g-v1.2:
--- → affected
status-b2g-v1.3:
--- → affected
status-b2g-v1.4:
--- → affected
status-b2g-v2.0:
--- → fixed
Comment 31•11 years ago
|
||
Backed out from beta for bustage.
https://hg.mozilla.org/releases/mozilla-beta/rev/91e40e5e4d26
https://tbpl.mozilla.org/php/getParsedLog.php?id=40470548&tree=Mozilla-Beta
Assignee | ||
Comment 32•11 years ago
|
||
(In reply to Ryan VanderMeulen [:RyanVM UTC-4] from comment #31)
> Backed out from beta for bustage.
> https://hg.mozilla.org/releases/mozilla-beta/rev/91e40e5e4d26
>
> https://tbpl.mozilla.org/php/getParsedLog.php?id=40470548&tree=Mozilla-Beta
Can you land the patch name "rollup" on beta? The patch got bitrotted while I was writing it, but "rollup" has the right code for beta, I believe.
Comment 33•11 years ago
|
||
Comment 34•11 years ago
|
||
Assignee | ||
Comment 35•11 years ago
|
||
Well, that's sad. I'll give you a patch that builds, and push it if you want. Sorry to waste your time with that.
Comment 36•11 years ago
|
||
No problem, it happens :). Feel free to push yourself if you want.
Assignee | ||
Comment 37•11 years ago
|
||
This is the backport for beta. It changes enough that I think it needs another
review.
Attachment #8430299 -
Flags: review?(ehsan)
Assignee | ||
Updated•11 years ago
|
Attachment #8421654 -
Attachment is obsolete: true
Updated•11 years ago
|
Attachment #8430299 -
Flags: review?(ehsan) → review+
Comment 38•11 years ago
|
||
Comment 39•11 years ago
|
||
Bustage follow-up:
https://hg.mozilla.org/releases/mozilla-beta/rev/8260d1fb1c34
Comment 40•11 years ago
|
||
(In reply to Ryan VanderMeulen [:RyanVM UTC-4] from comment #39)
> Bustage follow-up:
> https://hg.mozilla.org/releases/mozilla-beta/rev/8260d1fb1c34
post-landing r?padenot
Flags: needinfo?(paul)
Comment 41•11 years ago
|
||
https://hg.mozilla.org/releases/mozilla-b2g28_v1_3/rev/5dea846ef683
This is getting to be a hairy enough backport that I vote wontfix for b2g26. It's EOL in less than 2 weeks anyway.
Comment 42•11 years ago
|
||
Updated•11 years ago
|
status-b2g-v1.3T:
--- → fixed
Updated•11 years ago
|
Group: media-core-security
Updated•11 years ago
|
Whiteboard: [adv-main30+]
Comment 44•11 years ago
|
||
Confirmed problem on Fx29 using Paul's test.
Verified fixed using builds of Fx30, Fx31 and Fx32 from 2014-06-03.
Status: RESOLVED → VERIFIED
Whiteboard: [adv-main30+] → [qa-]
Updated•11 years ago
|
Whiteboard: [qa-]
Updated•11 years ago
|
Whiteboard: [adv-main30+]
Comment 45•10 years ago
|
||
not able to easily transplant, wontfix on SeaMonkey 2.26.1 (Gecko 29 based)
status-seamonkey2.26:
--- → wontfix
Updated•9 years ago
|
Group: core-security
You need to log in
before you can comment on or make changes to this bug.
Description
•