upvote
I am confused why you make out 32 bit int as being rare. Almost all devices that you'd reasonably connect to the internet at this point have int as 32 bits
reply
> It's base64. 4/3 expansion; round it up and add padding. What's "nontrivial" about that?

If you mean ceil(inlen * 4/3) + 1 (even if you use exact arithmetic), then you are passing too small a buffer and you will overflow. Maybe your “add padding” part fixes it? In any case, I think that the fact that this confusion is even possible proves my point.

You should not need to read the manpage of a function you are calling in order to determine whether you have passed a large enough buffer to avoid overflow.

Reading the manpage to get the function to not return an error would be a different story.

reply
If you mean ceil(inlen * 4/3) + 1 (even if you use exact arithmetic), then you are passing too small a buffer and you will overflow.

If you let untrusted input reach inlen and overflow that computation, you have already failed:

https://news.ycombinator.com/item?id=50570319

reply
inlen isn't an untrusted input and it's not this calculation that's overflowing (integer overflow). It's the buffer that oveflows when you allow the operation to proceed based on faulty validation.

The caller shouldn't be responsible for enforcing preconditions of your function. They should just provide the input buffer, output buffer, their sizes and your implementation should verify whether it's safe to proceed.

reply
Are you saying that any code that base64’s a string of attacker-controlled length is a mistake?
reply
I think userbinator thought that you meant that the calculation would overflow, while I think you meant that the calculation, even if not overflowing, would return the wrong value under some conditions and that subsequent code would then overflow the buffer. (As for what I think about the actual issue, I’m too sleepy to even try. Which is probably an indication of something. =) )
reply
The question can be reduce to "who do the length validation".

In my case, if i write a public-facing API, i never trust the caller will do the correct thing, thus the caller must provide all the critical parameters, and i must validate all of them.

reply
The question can be reduce to "who do the length validation".

At the boundary where untrusted input becomes trusted. This is commonly known as the sanitisation/validation layer.

reply