-
Notifications
You must be signed in to change notification settings - Fork 242
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
Add get_current_dir_name from musl to fix building on Bionic libc #580
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* Copyright © 2005-2020 Rich Felker, et al., 2023 Dallas Strouse | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <limits.h> | ||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
|
||
static char *get_current_dir_name(void) { | ||
struct stat a, b; | ||
char *res = getenv("PWD"); | ||
if (res && *res && !stat(res, &a) && !stat(".", &b) | ||
&& (a.st_dev == b.st_dev) && (a.st_ino == b.st_ino)) | ||
return strdup(res); | ||
return getcwd(0, 0); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This relies on a behaviour of
getcwd()
that is documented to be a GNU extension.getcwd(3)
on my (GNU/)Linux system says:meaning that it's considered perfectly OK under POSIX for
getcwd (NULL, 0)
to crash, or even to be a security vulnerability. On GNU systems (and presumably also musl and bionic systems), as an extension,getcwd (NULL, 0)
is documented to allocate a large enough buffer as if viamalloc()
, to be freed by the caller - but there is nothing to stop someone from deciding that musl and bionic are "too bloated" and writing a new libc that doesn't implement that GNU extension, in which case bubblewrap's behaviour would become undefined if we used this code on that libc.Undefined behaviour in bubblewrap is scary, because it's sometimes setuid root, making it security-sensitive.
I see you're using this under
#ifdef __BIONIC__
rather than#ifndef HAVE_GET_CURRENT_DIR_NAME
, which means that as long as Bionic documents the GNU-likegetcwd(NULL, 0)
extension, it's safe to use. Does it document that behaviour?I think that's subtle enough to deserve comments. Perhaps above the
__BIONIC__
check, something like:(except with appropriate line wrapping); and above this call to
getcwd(0, 0)
,(assuming that's true of course).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the Bionic implementation: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/getcwd.cpp
I don't know much about the details of these, I'm mainly a Rust gal, and don't touch C often if at all. If needed, I'll add more comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see it implements the GNU-style extension, but does it document the extension (or a general API stability policy) so that it would be considered to be an incompatible change to remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick Google search doesn't turn anything up, I'll search some more and ask some Android devs if they can comment on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading the following files, I think it's safe to assume they won't change getcwd on us, as they have it listed (and i assume supported because of that) on some of their docs. Can't find any specific details on getcwd though. Would the source code be enough to reference?
https://android.googlesource.com/platform/bionic/+/ics-mr1-release/libc/docs/CHANGES.TXT
https://android.googlesource.com/platform/bionic/+/refs/heads/master/docs/status.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't see anything by following those links that documents that their
getcwd (NULL, 0)
is intended to have GNU-compatible semantics.