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

Fix for buffer overflow on read failure #11

Merged
merged 1 commit into from
Sep 2, 2013
Merged

Fix for buffer overflow on read failure #11

merged 1 commit into from
Sep 2, 2013

Conversation

langholz
Copy link
Collaborator

On linux, whenever a connection is already established and the read operation on the file descriptor is unsuccessful (e.g. the connection was reset or other), the application crashes with a segmentation fault (e.g. Program received signal SIGSEGV, Segmentation fault). Debugging a little bit more through gdb, I discovered the following...

Problem:
In function BTSerialPortBinding::EIO_Read for src/linux/BTSerialPortBinding.cc:163, we assign the number of bytes read by the read(...) to baton->size. However, upon the operation failing the bytes read return is -1. A couple of lines below in the same function, src/linux/BTSerialPortBinding.cc:169, we copy the data read and stored in buf into baton->result. However, in the case the read operation failed baton->size == -1. This causes memcpy to be invoked with a -1 as its count value. The count parameter is actually defined as a size_t while baton->size happens to be an int. Passing the -1 (int) into memcpy causes the count (size_t is actually unsigned) value to wrap around and become a really large value (integer overflow). In turn, when the copy function refers to an index outside either buffers we overflow because we are referring to a memory location larger than 1024 (which is the max size for either).

Solution:
Luckily the solution is simple. We just need to add a check to determine if the size is non-negative. If it is not, then we proceed to copy. Otherwise, we don't. I tried this fix with the application that I used to uncover this issues and confirmed that this works as expected.

@eelcocramer
Copy link
Owner

Thank a lot for your PR and throurough explanation. I'll look into it after the weekend.

@ghost ghost assigned eelcocramer Sep 2, 2013
eelcocramer added a commit that referenced this pull request Sep 2, 2013
Fix for buffer overflow on read failure
@eelcocramer eelcocramer merged commit 7a77676 into eelcocramer:master Sep 2, 2013
@eelcocramer
Copy link
Owner

Pulled. Good find & fix :-)

Thanks for the PR. I will publish a new version to npm later today.

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

Successfully merging this pull request may close these issues.

2 participants