Optimization-unstable code
Optimization-unstable code
Posted Dec 5, 2013 8:35 UTC (Thu) by jezuch (subscriber, #52988)Parent article: Optimization-unstable code
> unsigned int len = ...;
>
> if (buf + len < buf) /* overflow check */
> ...
> On some architectures, according to the paper, that's no great loss as the test doesn't work. But on other architectures, it does protect against a too large value of len.
My take on this is: if you invent TRICKSY code like this, it's your damn fault if it breaks.
> I think the only answer to those lines is to advise you to not use any programs written in C.
Haha only serious ;)
Posted Dec 5, 2013 12:42 UTC (Thu)
by Felix.Braun (guest, #3032)
[Link] (7 responses)
if (buf + len >= &len) ...
Posted Dec 5, 2013 13:42 UTC (Thu)
by HelloWorld (guest, #56129)
[Link]
Posted Dec 5, 2013 14:01 UTC (Thu)
by mathstuf (subscriber, #69389)
[Link] (2 responses)
Maybe I'm missing something, but what are you supposed to do if the check fails? Complain that malloc is giving you bad pointers? If there's a bug serious enough that malloc is giving you a block that wraps around the end of the address space, I'd almost it rather crash to get away from that libc ASAP ;) . Or you're not asking for a big enough block, but that's an easy fix too.
Posted Dec 5, 2013 14:44 UTC (Thu)
by pbonzini (subscriber, #60935)
[Link] (1 responses)
The compiler can also reuse memory for variables with overlapping lifetimes.
Posted Dec 8, 2013 14:20 UTC (Sun)
by nix (subscriber, #2304)
[Link]
Posted Dec 12, 2013 11:36 UTC (Thu)
by etrusco (guest, #4227)
[Link] (2 responses)
if (buf + len >= (char *) len) ...
?
Posted Dec 12, 2013 12:03 UTC (Thu)
by mathstuf (subscriber, #69389)
[Link] (1 responses)
Posted Dec 12, 2013 14:07 UTC (Thu)
by mathstuf (subscriber, #69389)
[Link]
Optimization-unstable code
The way I see it, buf + len can only be < buf if len is sufficiently big to wrap around the address space. Wouldn't it be much more useful to check for a buffer overflow like this instead?
char *buf = ...;
unsigned int len = ...;
if (buf + len < buf) /* overflow check */
That makes no sense. Why would there be any relationship between the buffer's bounds and len's address?
You do have a point though: the check quoted in the LWN article is insufficient, and in fact, the check in the paper is different:
Optimization-unstable code
char *buf = ...;
char *buf_end = ...;
unsigned int len = ...;
if (buf + len >= buf_end)
return; /* len too large */
if (buf + len < buf)
return; /* overflow, buf+len wrapped around */
/* write to buf[0..len-1] */
Optimization-unstable code
Optimization-unstable code
Optimization-unstable code
The compiler can also reuse memory for variables with overlapping lifetimes.
I hope you meant 'non-overlapping'!
Optimization-unstable code
Optimization-unstable code
Optimization-unstable code