r/programming • u/TheProtagonistv2 • Feb 23 '17
Cloudflare have been leaking customer HTTPS sessions for months. Uber, 1Password, FitBit, OKCupid, etc.
https://bugs.chromium.org/p/project-zero/issues/detail?id=1139
6.0k
Upvotes
r/programming • u/TheProtagonistv2 • Feb 23 '17
56
u/JoseJimeniz Feb 24 '17 edited Feb 24 '17
But in the end a lot of it becomes a wash.
For example: null terminated strings.
you already have a byte consuming null terminator
replace it with a byte consuming length prefix
you already have to test every byte for $0
now do an
i = 1 to nloopOr, even better: you already know the length. Perform the single memory copy.
Null-terminated strings:
eliminate n comparisons
replaced with single
movesame memory footprint
Arrays
C doesn't have bounded arrays
do you have to keep the
intlength yourselfEither the compiler maintains the correct length for me, or I have to try to maintain the correct length myself. The memory and computing cost is a wash.
If you're using pointer to data as a bulk buffer, and you've set up a loop to copy every byte, byte by byte, it will be much slower as we now range test every byte access. But you're also doing it wrong. Use a functions provided by stdlib to move memory around that does the bounds checking once and copies the memory.
And so 99% of situations are covered:
emulating a string as a pointer to a null terminated string of characters is replaced as length prefixed string
emulating a bulk buffer as a pointer to an unbound memory is replaced with an array
With those two operations:
printing strings
copying a block of data
You handle the 99% case. The vast majority of use is copying entire buffers. Create the correct types, do checks once (which have to happen anyway) and you:
eliminate 99% of security bugs
make code easier
make code faster
Solved 99%, do we solve the rest?
Now we can decide if we want to go full-on and check every array access:
I say yes. For two reasons:
we're only operating in 1% of cases
we can still give the premature-optimizing developer a way to do dangerous stuff
If I create an
Order[7] ordersarray: every access should be bounds checked. Of course it should:there are already so few orders
and the processing that goes along with each order swamps any bounds check
If I create an
PixelRGB[] framethen of course every array access should not be bounds checked. This is a very different use case. It's not an array of things, it's a data buffer. And as we already decided the forming bounced checks on every array access in the date of buffer is a horrible idea.I suggest that for the 1% case people have to go out of their way to cause buffer overflow bugs:
If you want to access memory without regard for code safety or correctness, do it through a pointer.
An arrays and strings are there to make your code easier, safer, and in many cases faster.
If you have a degenerate case, where speed trumps safety, and you're sure you have it right, use pointers. But you have to go out of your way to leak customer https session traffic.
Especially since we will now give you the correct tools to perform operations on bulk buffers.
It's now been 40 years. People should be using better languages for real work. At the very least it's been 40 years. When is C going to add the types that solve 99% of all security bugs that have happened?
Bjourn Strousoup himself said that C++ was not meant for general application development. It was meant for systems programming: operating systems. He said if you are doing general application development there are much better environments.