Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array index past end of array in NSData+Base64 #96

Open
newacct opened this issue Oct 21, 2011 · 7 comments
Open

Array index past end of array in NSData+Base64 #96

newacct opened this issue Oct 21, 2011 · 7 comments

Comments

@newacct
Copy link

newacct commented Oct 21, 2011

In NSData+Base64.m, in the initWithBase64EncodedString: method, the inbuf array is declared with size 3, yet it is indexed with indexes 0 thru 3, which is a buffer overflow. Curiously, the outbuf array is declared with size 4, yet is indexed with indexes 0 thru 2. Perhaps their sizes are switched.

@danielsonchris
Copy link

Found this issue as well. The code fix for this is trivial. Please also take into account that you need to memset your buffers before operating on them as well unless you want entropy laden artifacts.
unsigned char inbuf[4];
memset(&inbuf, 0x00, 4);
//while we are at it.
memset(&outbuf, 0x00, 4);

-Chris

@tanapon
Copy link

tanapon commented Jan 3, 2012

I'm also getting a warning at
outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );
that says
Array index of '3' indexes past the end of an array (that contains 3 elements)
so do I have to change this > "unsigned char inbuf[3], outbuf[4];"
to this? > "unsigned char inbuf[4], outbuf[3];" or "unsigned char inbuf[4], outbuf[4];"

I have no knowledge of memset. so do I have to add memset(&inbuf, 0x00, 4); and memset(&outbuf, 0x00, 4); as well?
thanks.

@danielsonchris
Copy link

Change to: "unsigned char inbuf[4], outbuf[3];"

Memset is great for setting the buffer to a known value before you operate on that buffer. When you operate with memory in C, it should never be assumed that it is properly zero'd out or assigned to a particular set of default values. You have to do this manually and memset helps you quickly do this. Otherwise, you might have junk in your buffer. This is especially a problem if you are doing some bit flipping on that buffer without pre-assigning values to the proper offset.

From the terminal type in "man memset" You'll see more great information on it.

Kind Regards,
Chris

@newacct
Copy link
Author

newacct commented Jan 3, 2012

"Change to: "unsigned char inbuf[4], outbuf[4];""

Why not "unsigned char inbuf[4], outbuf[3];"?

@newacct
Copy link
Author

newacct commented Jan 4, 2012

Yes it is large enough. All uses of outbuf in that function are index 0, 1, or 2. Maybe you are looking at a different function. Plus the same fix was applied upstream (where this file came from) several years ago.

@danielsonchris
Copy link

Yes, I meant inbuf wasn't large enough. In the version I'm holding here it has on line 79:

outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );

@newacct Just read my original post at the top.

@Jusung
Copy link

Jusung commented Sep 14, 2012

Thank you guys! @newacct and @danielsonchris you guys conversation helped me to resolve my same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants