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

libsbutil: add sb_exists function #7

Merged
merged 1 commit into from Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion libsandbox/pre_check_openat.c
Expand Up @@ -19,7 +19,7 @@ bool sb_openat_pre_check(const char *func, const char *pathname, int dirfd, int
save_errno();

/* Doesn't exist -> skip permission checks */
if (faccessat(dirfd, pathname, F_OK, (flags & O_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0) == -1) {
if (sb_exists(dirfd, pathname, (flags & O_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0) == -1) {
sb_debug_dyn("EARLY FAIL: %s(%s): %s\n", func, pathname, strerror(errno));
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion libsandbox/wrapper-funcs/fopen_pre_check.c
Expand Up @@ -11,7 +11,7 @@ bool sb_fopen_pre_check(const char *func, const char *pathname, const char *mode
save_errno();

/* If we're trying to read, fail normally if file does not stat */
if (faccessat(AT_FDCWD, pathname, F_OK, 0) == -1) {
if (sb_exists(AT_FDCWD, pathname, 0) == -1) {
sb_debug_dyn("EARLY FAIL: %s(%s): %s\n",
func, pathname, strerror(errno));
return false;
Expand Down
1 change: 1 addition & 0 deletions libsbutil/local.mk
Expand Up @@ -16,6 +16,7 @@ noinst_LTLIBRARIES += %D%/libsbutil.la
%D%/environment.c \
%D%/sb_backtrace.c \
%D%/sb_efuncs.c \
%D%/sb_exists.c \
%D%/sb_gdb.c \
%D%/sb_method.c \
%D%/sb_open.c \
Expand Down
24 changes: 24 additions & 0 deletions libsbutil/sb_exists.c
@@ -0,0 +1,24 @@
/*
* Copyright 2023 Gentoo Authors
* Distributed under the terms of the GNU General Public License v2
*/

#include "headers.h"
#include "sbutil.h"

/* Wrapper for faccessat to work around buggy behavior on musl */
int sb_exists(int dirfd, const char *pathname, int flags)
{
struct stat64 buf;

if (faccessat(dirfd, pathname, F_OK, flags) == 0)
return 0;

/* musl's faccessat gives EINVAL when the kernel does not support
* faccessat2 and AT_SYMLINK_NOFOLLOW is set.
* https://www.openwall.com/lists/musl/2023/06/19/1 */
if (errno != EINVAL)
return -1;

return fstatat64(dirfd, pathname, &buf, flags);
}
1 change: 1 addition & 0 deletions libsbutil/sbutil.h
Expand Up @@ -111,6 +111,7 @@ size_t sb_write(int fd, const void *buf, size_t count);
int sb_close(int fd);
void sb_close_all_fds(void);
int sb_copy_file_to_fd(const char *file, int ofd);
int sb_exists(int dirfd, const char *pathname, int flags);

/* Reliable output */
__printf(1, 2) void sb_printf(const char *format, ...);
Expand Down
2 changes: 1 addition & 1 deletion libsbutil/src/file.c
Expand Up @@ -15,7 +15,7 @@
bool
rc_file_exists (const char *pathname)
{
return faccessat(AT_FDCWD, pathname, F_OK, AT_SYMLINK_NOFOLLOW) == 0;
return sb_exists(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW) == 0;
}

bool
Expand Down