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

Avoid potential buffer overflow/missing zero termination of string #36408

Merged
merged 5 commits into from
Jun 28, 2020

Conversation

martinholters
Copy link
Member

Ref 00abaf8#r39659418. Replace a strncpy(dest, src, strlen(src)) with strncpy(dest, src, DEST_SIZE); dest[DEST_SIZE-1]='\0'. The old code could overflow the destination if the source was too long (not sure that could actually happen), defeating the purpose of strncpy. Further, it failed to put the terminating zero in the destination, relying on it being properly initialized.

I have no idea about the context here, but this code path isn't exercised at all when I do make test. Is that somehow system specific? Does CI cover this?

@staticfloat
Copy link
Sponsor Member

This code path is only exercised when using dlopen("libname") after having run push!(Base.DL_LOAD_PATH, "@executable_path/path/to/libraries"). I've added a test case to this PR.

Copy link
Sponsor Member

@staticfloat staticfloat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works locally for me, and when running on the buildbots. Strange.

stdlib/Libdl/test/runtests.jl Outdated Show resolved Hide resolved
stdlib/Libdl/test/runtests.jl Outdated Show resolved Hide resolved
martinholters and others added 5 commits June 26, 2020 08:01
Replace a `strncpy(dest, src, strlen(src))` with `strncpy(dest, src,
DEST_SIZE); dest[DEST_SIZE-1]='\0'`. The old code could overflow the
destination if the source was too long, defeating the purpose of
`strncpy`. Further, it failed to put the terminating zero in the
destination, relying on it being properly initialized.
This exercises the replacement of `DL_LOAD_PATH` entries that start
with `@executable_path`.
...so that the test using the absolute path can be run unconditionally.
@martinholters
Copy link
Member Author

I've moved the test using the absolute path first so that it can run unconditionally (it can, right?). If this passes tests, I think it should be ready to go.

@staticfloat staticfloat merged commit 9d71d37 into master Jun 28, 2020
@staticfloat staticfloat deleted the mh/strncpy-fix branch June 28, 2020 00:38
@martinholters
Copy link
Member Author

Thanks for helping out with the tests here, Elliot. Funny how they turned out to be much more involved than the fix itself...

@JeffBezanson
Copy link
Sponsor Member

@staticfloat This seems to involve code that doesn't exist on 1.5 --- I don't see what can be backported?

@staticfloat
Copy link
Sponsor Member

My bad; I thought it was in 1.5 but I guess I was wrong. :)

simeonschaub pushed a commit to simeonschaub/julia that referenced this pull request Aug 11, 2020
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.

None yet

3 participants