3 ms·
> Wasn't the heartbleed issue that you could trick it into reading past the memory it had allocated? No. Heartbleed is this: 1. malloc an input and an output
by masklinn 12y ago
> Wasn't the heartbleed issue that you could trick it into reading past the memory it had allocated?
No.
Heartbleed is this:
1. malloc an input and an output buffer of the size specified by the caller (16 bits, up to 65536 bytes)
2. copy input data into input buffer (as little as 1 byte)
3. copy input buffer to output buffer
4. send output buffer to caller
Neither malloc nor free zero-out their stuff, so when you malloc chances are the garbage you get before filling your buffer is the result of previous memory writes. In heartbleed, each call would return up to 65535 bytes of these likely-previous-memory-writes to the caller.
This was made even more likely because OpenSSL includes an anti-mitigation framework in the form of freelists: while malloc doesn't zero memory by default, various OS have added mitigation techniques e.g. BSD's malloc.conf can wipe allocated buffers. However OpenSSL does its own memory management using freelists, so it reuses previously-allocated buffers for new allocations making it even more likely heartbleed would leak interesting data.
Anyway one of the things which don't occur at any point is reading or writing past a buffer.
- ajanuary 12y agoThank you for clarifying.
- eddyb 12y agoThe equivalent would be `Vec::with_capacity` which allocates the right size but does not provide safe access to uninitialized memory - in other words, you can only read what you wrote.
- rbehrends 12y agoThis isn't how I remember it nor what the code looks like [1]. Briefly: 1. tls1_process_heartbeat() retrieves type and 16-bit length from the SSL record. 2. It stores a pointer to the payload (which is after the two length bytes). 3. For the response, it allocates an output buffer and memcpy()s from the payload pointer to the output buffer. 4. If the specified payload length (the two bytes preceding the payload) is less than the actual length of the payload, memcpy() will read past the actual payload in the SSL record and will copy arbitrary memory contents to the response. This looks like a pretty traditional buffer overrun to me. As far as I can tell, nowhere in the code was the input ever extended to match the specified rather than the actual payload length and the fix was to insert a sanity check to abort if there's a mismatch. [1] http://git.openssl.org/gitweb/?p=openssl.git;a=blob;f=ssl/t1_lib.c;h=a2e2475d136f33fa26958fd192b8ace158c4899d#l3964 http://git.openssl.org/gitweb/?p=openssl.git;a=blob;f=ssl/t1...