1 |
commit: 382f70b8d93d012648edc7a42087a6d4d5a103eb |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri Nov 5 10:23:34 2021 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Nov 5 10:23:34 2021 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=382f70b8 |
7 |
|
8 |
libsandbox/libsbutil: use faccessat for file-existence tests |
9 |
|
10 |
This is faster than using stat since it doesn't have to gather all |
11 |
the metadata, and should avoid LFS issues as a result. |
12 |
|
13 |
Bug: https://bugs.gentoo.org/583282 |
14 |
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org> |
15 |
|
16 |
libsandbox/pre_check_openat.c | 15 +++------------ |
17 |
libsandbox/wrapper-funcs/fopen_pre_check.c | 3 +-- |
18 |
libsbutil/src/file.c | 14 +------------- |
19 |
3 files changed, 5 insertions(+), 27 deletions(-) |
20 |
|
21 |
diff --git a/libsandbox/pre_check_openat.c b/libsandbox/pre_check_openat.c |
22 |
index 8cf8133..8fd3b23 100644 |
23 |
--- a/libsandbox/pre_check_openat.c |
24 |
+++ b/libsandbox/pre_check_openat.c |
25 |
@@ -12,24 +12,15 @@ |
26 |
|
27 |
bool sb_openat_pre_check(const char *func, const char *pathname, int dirfd, int flags) |
28 |
{ |
29 |
- /* If we're not trying to create, fail normally if |
30 |
- * file does not stat |
31 |
- */ |
32 |
+ /* If we're not trying to create, fail normally if file does not stat */ |
33 |
if (flags & O_CREAT) |
34 |
return true; |
35 |
|
36 |
save_errno(); |
37 |
|
38 |
- /* Check incoming args against common *at issues */ |
39 |
- char dirfd_path[SB_PATH_MAX]; |
40 |
- if (!sb_common_at_pre_check(func, &pathname, dirfd, dirfd_path, sizeof(dirfd_path))) |
41 |
- return false; |
42 |
- |
43 |
/* Doesn't exist -> skip permission checks */ |
44 |
- struct stat st; |
45 |
- if (((flags & O_NOFOLLOW) ? lstat(pathname, &st) : stat(pathname, &st)) == -1) { |
46 |
- sb_debug_dyn("EARLY FAIL: %s(%s): %s\n", |
47 |
- func, pathname, strerror(errno)); |
48 |
+ if (faccessat(dirfd, pathname, F_OK, (flags & O_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0) == -1) { |
49 |
+ sb_debug_dyn("EARLY FAIL: %s(%s): %s\n", func, pathname, strerror(errno)); |
50 |
return false; |
51 |
} |
52 |
|
53 |
|
54 |
diff --git a/libsandbox/wrapper-funcs/fopen_pre_check.c b/libsandbox/wrapper-funcs/fopen_pre_check.c |
55 |
index 765526e..95108e0 100644 |
56 |
--- a/libsandbox/wrapper-funcs/fopen_pre_check.c |
57 |
+++ b/libsandbox/wrapper-funcs/fopen_pre_check.c |
58 |
@@ -11,8 +11,7 @@ bool sb_fopen_pre_check(const char *func, const char *pathname, const char *mode |
59 |
save_errno(); |
60 |
|
61 |
/* If we're trying to read, fail normally if file does not stat */ |
62 |
- struct stat st; |
63 |
- if (-1 == stat(pathname, &st)) { |
64 |
+ if (faccessat(AT_FDCWD, pathname, F_OK, 0) == -1) { |
65 |
sb_debug_dyn("EARLY FAIL: %s(%s): %s\n", |
66 |
func, pathname, strerror(errno)); |
67 |
return false; |
68 |
|
69 |
diff --git a/libsbutil/src/file.c b/libsbutil/src/file.c |
70 |
index 4542ae5..a1a4a0e 100644 |
71 |
--- a/libsbutil/src/file.c |
72 |
+++ b/libsbutil/src/file.c |
73 |
@@ -15,19 +15,7 @@ |
74 |
bool |
75 |
rc_file_exists (const char *pathname) |
76 |
{ |
77 |
- struct stat buf; |
78 |
- int retval; |
79 |
- |
80 |
- if (!check_str (pathname)) |
81 |
- return false; |
82 |
- |
83 |
- retval = lstat (pathname, &buf); |
84 |
- if (-1 != retval) |
85 |
- retval = true; |
86 |
- else |
87 |
- retval = false; |
88 |
- |
89 |
- return retval; |
90 |
+ return faccessat(AT_FDCWD, pathname, F_OK, AT_SYMLINK_NOFOLLOW) == 0; |
91 |
} |
92 |
|
93 |
bool |