Live data from Hacker News

Exploiting the Math.expm1 typing bug in V8

abiondo.me

21–30 of 47 posts

Re: Exploiting the Math.expm1 typing bug in V8

#21
post #14

Earlier quoted context omitted.

> The fundamental bug here is really slick. The static analyzer in the JIT incorrectly believes Math.expm1(x) can't return -0. I can't get past this part. When a Googler "believes" Union(PlainNumber, NaN) represents the set of possible return values for a math function and commits that in the code , how is there not an automated set of tests that use one out of every other IEEE754-associated type as input to then che…

I don't know, to me, this sounds like one of the more subtle examples of the kinds of mistakes that lead to security failures. Like, it might be an almost archetypical example of the "all bugs are security vulnerabilities" hypothesis. They got code execution from expm1! But if you believe that this is an example of wanton abuse at Google, you can trade on that belief, and in a sense put your money where your mouth is…

If they just didnt discard the OutOfBounds check, the V8 JS engine would be a lot safer...

Re: Exploiting the Math.expm1 typing bug in V8

#22
post #3

The fundamental bug here is really slick. The static analyzer in the JIT incorrectly believes Math.expm1(x) can't return -0. But if x is -0, it can. That, in turn, means it believes Object.is(Math.expm(x), -0) must always be false. But if x is -0, it's true, not false. That, in turn, means that the JIT believes array[Object.is(Math.expm(x), -0) * INDEX] must be array[0], no matter what INDEX is. But if x is -0, it'll…

Here's what I don't get. Why isn't Math.expm1 just implemented in JavaScript? Math.expm1 = (x) => Math.pow(Math.E, (x)) - 1; With this monkey patched version, Math.expm1(-0) now returns 0. I've been writing JavaScript for a long time and I didn't even know Math.expm1 was a thing. Was it really necessary to implement this inside V8?

Because you lose all your precision there due to how floating point math eorjsy. expm1 is for very small x that where exp (x) is close to 1.

Re: Exploiting the Math.expm1 typing bug in V8

#23
post #3

The fundamental bug here is really slick. The static analyzer in the JIT incorrectly believes Math.expm1(x) can't return -0. But if x is -0, it can. That, in turn, means it believes Object.is(Math.expm(x), -0) must always be false. But if x is -0, it's true, not false. That, in turn, means that the JIT believes array[Object.is(Math.expm(x), -0) * INDEX] must be array[0], no matter what INDEX is. But if x is -0, it'll…

Here's what I don't get. Why isn't Math.expm1 just implemented in JavaScript? Math.expm1 = (x) => Math.pow(Math.E, (x)) - 1; With this monkey patched version, Math.expm1(-0) now returns 0. I've been writing JavaScript for a long time and I didn't even know Math.expm1 was a thing. Was it really necessary to implement this inside V8?

expm1 avoids catastrophic cancellation (due to finite precision floating point), and so is more accurate for x close to zero than naive(x) = exp(x) - 1, e.g. expm1(1e-100) != 0, but naive(1e-100) = 0.

Re: Exploiting the Math.expm1 typing bug in V8

#24
post #14

Earlier quoted context omitted.

I don't know, to me, this sounds like one of the more subtle examples of the kinds of mistakes that lead to security failures. Like, it might be an almost archetypical example of the "all bugs are security vulnerabilities" hypothesis. They got code execution from expm1! But if you believe that this is an example of wanton abuse at Google, you can trade on that belief, and in a sense put your money where your mouth is…

If they just didnt discard the OutOfBounds check, the V8 JS engine would be a lot safer...

...and probably a lot slower. Google loves to make claims about V8's performance, but not so much about security.

Re: Exploiting the Math.expm1 typing bug in V8

#25
post #14

Earlier quoted context omitted.

> The fundamental bug here is really slick. The static analyzer in the JIT incorrectly believes Math.expm1(x) can't return -0. I can't get past this part. When a Googler "believes" Union(PlainNumber, NaN) represents the set of possible return values for a math function and commits that in the code , how is there not an automated set of tests that use one out of every other IEEE754-associated type as input to then che…

I don't know, to me, this sounds like one of the more subtle examples of the kinds of mistakes that lead to security failures. Like, it might be an almost archetypical example of the "all bugs are security vulnerabilities" hypothesis. They got code execution from expm1! But if you believe that this is an example of wanton abuse at Google, you can trade on that belief, and in a sense put your money where your mouth is…

> it might be an almost archetypical example of the "all bugs are security vulnerabilities" hypothesis

This article will be my new go-to example when someone handwaves a bug away with a complacent “it’ll never happen” and “it’s not that big of a deal”. Yes it will, and yes it is.

Re: Exploiting the Math.expm1 typing bug in V8

#26
post #8
post #2

Interesting how at the end, after acquiring out-of-bounds write access, that it was easiest to leverage the WebAssembly infrastructure to execute code than to build a ROP chain. Apparently WebAssembly heap memory storing generated code is not write protected at all . I guess whatever architecture they have for managing typed memory chunks doesn't make it sufficiently easy to manipulate protection bits dynamically, an…

Hi, TLM of the WebAssembly runtime in V8 here. TLDR: it's asm.js's fault. And yes, complexity. The reason that WebAssembly JIT code memory is still RMW (for now) is actually really unfortunate. As you might know, V8's JIT code memory for JS is only writable when the application is quiesced (i.e. JS is not running) and the JIT is either finishing a function or the garbage collector is moving JITted code. It's read-exe…

Why doesn't it at least do double-mapping if it can't do W^X flipping?

Re: Exploiting the Math.expm1 typing bug in V8

#27
post #3

The fundamental bug here is really slick. The static analyzer in the JIT incorrectly believes Math.expm1(x) can't return -0. But if x is -0, it can. That, in turn, means it believes Object.is(Math.expm(x), -0) must always be false. But if x is -0, it's true, not false. That, in turn, means that the JIT believes array[Object.is(Math.expm(x), -0) * INDEX] must be array[0], no matter what INDEX is. But if x is -0, it'll…

Here's what I don't get. Why isn't Math.expm1 just implemented in JavaScript? Math.expm1 = (x) => Math.pow(Math.E, (x)) - 1; With this monkey patched version, Math.expm1(-0) now returns 0. I've been writing JavaScript for a long time and I didn't even know Math.expm1 was a thing. Was it really necessary to implement this inside V8?

Even good old C includes several functions that seem "redundant" at first glance [0] but are necessary for edge cases. I suppose this is similar to mechanics like fused multiply-add [1].

[0]: https://www.johndcook.com/blog/2010/06/07/math-library-funct...

[1]: https://en.wikipedia.org/wiki/Multiply%E2%80%93accumulate_op...

Re: Exploiting the Math.expm1 typing bug in V8

#28
post #3

The fundamental bug here is really slick. The static analyzer in the JIT incorrectly believes Math.expm1(x) can't return -0. But if x is -0, it can. That, in turn, means it believes Object.is(Math.expm(x), -0) must always be false. But if x is -0, it's true, not false. That, in turn, means that the JIT believes array[Object.is(Math.expm(x), -0) * INDEX] must be array[0], no matter what INDEX is. But if x is -0, it'll…

> The fundamental bug here is really slick. The static analyzer in the JIT incorrectly believes Math.expm1(x) can't return -0. I can't get past this part. When a Googler "believes" Union(PlainNumber, NaN) represents the set of possible return values for a math function and commits that in the code , how is there not an automated set of tests that use one out of every other IEEE754-associated type as input to then che…

[deleted]

Re: Exploiting the Math.expm1 typing bug in V8

#29

Earlier quoted context omitted.

If they just didnt discard the OutOfBounds check, the V8 JS engine would be a lot safer...

...and probably a lot slower. Google loves to make claims about V8's performance, but not so much about security.

certainly V8 tries to be correct and this bug will be fixed. Chromium's position is "security in depth". They know it's impossible to have zero bugs therefore the entire architure assumes there will be bugs and tries to prevent them from causing any harm. This is also why there are roughly 10x less code execution bugs in chrome vs other browsers. same number of bugs overall but most lead nowhere

Re: Exploiting the Math.expm1 typing bug in V8

#30

Earlier quoted context omitted.

...and probably a lot slower. Google loves to make claims about V8's performance, but not so much about security.

certainly V8 tries to be correct and this bug will be fixed. Chromium's position is "security in depth". They know it's impossible to have zero bugs therefore the entire architure assumes there will be bugs and tries to prevent them from causing any harm. This is also why there are roughly 10x less code execution bugs in chrome vs other browsers. same number of bugs overall but most lead nowhere

> Chromium's position is "security in depth".

Yet the exploit in WebSQL a few weeks ago gave RCE from a component directly reachable by page scripts.

Post reply on HN