Merge pull request #27064 from DaanDeMeyer/chase-fixes

Chase fixes
This commit is contained in:
Daan De Meyer
2023-03-30 11:44:33 +02:00
committed by GitHub
2 changed files with 26 additions and 105 deletions
+25 -104
View File
@@ -72,13 +72,7 @@ static int log_prohibited_symlink(int fd, ChaseFlags flags) {
strna(n1));
}
int chaseat(
int dir_fd,
const char *path,
ChaseFlags flags,
char **ret_path,
int *ret_fd) {
int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int *ret_fd) {
_cleanup_free_ char *buffer = NULL, *done = NULL;
_cleanup_close_ int fd = -EBADF, root_fd = -EBADF;
unsigned max_follow = CHASE_MAX; /* how many symlinks to follow before giving up and returning ELOOP */
@@ -482,13 +476,7 @@ chased_one:
return 0;
}
int chase(
const char *path,
const char *original_root,
ChaseFlags flags,
char **ret_path,
int *ret_fd) {
int chase(const char *path, const char *original_root, ChaseFlags flags, char **ret_path, int *ret_fd) {
_cleanup_free_ char *root = NULL, *absolute = NULL, *p = NULL;
_cleanup_close_ int fd = -EBADF, pfd = -EBADF;
int r;
@@ -572,13 +560,7 @@ int chase(
return r;
}
int chase_and_open(
const char *path,
const char *root,
ChaseFlags chase_flags,
int open_flags,
char **ret_path) {
int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, int open_flags, char **ret_path) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL, *fname = NULL;
mode_t mode = open_flags & O_DIRECTORY ? 0755 : 0644;
@@ -603,14 +585,13 @@ int chase_and_open(
if (isempty(q))
q = ".";
r = path_extract_filename(q, &fname);
if (r < 0 && r != -EADDRNOTAVAIL)
return r;
if (!FLAGS_SET(chase_flags, CHASE_PARENT)) {
r = path_extract_filename(q, &fname);
if (r < 0 && r != -EADDRNOTAVAIL)
return r;
}
if (FLAGS_SET(chase_flags, CHASE_PARENT) || r == -EADDRNOTAVAIL)
r = fd_reopen(path_fd, open_flags);
else
r = xopenat(path_fd, fname, open_flags|O_NOFOLLOW, mode);
r = xopenat(path_fd, strempty(fname), open_flags|O_NOFOLLOW, mode);
if (r < 0)
return r;
@@ -620,13 +601,7 @@ int chase_and_open(
return r;
}
int chase_and_opendir(
const char *path,
const char *root,
ChaseFlags chase_flags,
char **ret_path,
DIR **ret_dir) {
int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
DIR *d;
@@ -662,13 +637,7 @@ int chase_and_opendir(
return 0;
}
int chase_and_stat(
const char *path,
const char *root,
ChaseFlags chase_flags,
char **ret_path,
struct stat *ret_stat) {
int chase_and_stat(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
int r;
@@ -697,13 +666,7 @@ int chase_and_stat(
return 0;
}
int chase_and_access(
const char *path,
const char *root,
ChaseFlags chase_flags,
int access_mode,
char **ret_path) {
int chase_and_access(const char *path, const char *root, ChaseFlags chase_flags, int access_mode, char **ret_path) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
int r;
@@ -767,13 +730,7 @@ int chase_and_fopen_unlocked(
return 0;
}
int chase_and_unlink(
const char *path,
const char *root,
ChaseFlags chase_flags,
int unlink_flags,
char **ret_path) {
int chase_and_unlink(const char *path, const char *root, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
_cleanup_free_ char *p = NULL, *fname = NULL;
_cleanup_close_ int fd = -EBADF;
int r;
@@ -810,13 +767,7 @@ int chase_and_open_parent(const char *path, const char *root, ChaseFlags chase_f
return pfd;
}
int chase_and_openat(
int dir_fd,
const char *path,
ChaseFlags chase_flags,
int open_flags,
char **ret_path) {
int chase_and_openat(int dir_fd, const char *path, ChaseFlags chase_flags, int open_flags, char **ret_path) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL, *fname = NULL;
mode_t mode = open_flags & O_DIRECTORY ? 0755 : 0644;
@@ -835,14 +786,13 @@ int chase_and_openat(
if (r < 0)
return r;
r = path_extract_filename(p, &fname);
if (r < 0 && r != -EDESTADDRREQ)
return r;
if (!FLAGS_SET(chase_flags, CHASE_PARENT)) {
r = path_extract_filename(p, &fname);
if (r < 0 && r != -EADDRNOTAVAIL)
return r;
}
if (FLAGS_SET(chase_flags, CHASE_PARENT) || r == -EDESTADDRREQ)
r = fd_reopen(path_fd, open_flags);
else
r = xopenat(path_fd, fname, open_flags|O_NOFOLLOW, mode);
r = xopenat(path_fd, strempty(fname), open_flags|O_NOFOLLOW, mode);
if (r < 0)
return r;
@@ -852,13 +802,7 @@ int chase_and_openat(
return r;
}
int chase_and_opendirat(
int dir_fd,
const char *path,
ChaseFlags chase_flags,
char **ret_path,
DIR **ret_dir) {
int chase_and_opendirat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
DIR *d;
@@ -894,13 +838,7 @@ int chase_and_opendirat(
return 0;
}
int chase_and_statat(
int dir_fd,
const char *path,
ChaseFlags chase_flags,
char **ret_path,
struct stat *ret_stat) {
int chase_and_statat(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
int r;
@@ -929,13 +867,7 @@ int chase_and_statat(
return 0;
}
int chase_and_accessat(
int dir_fd,
const char *path,
ChaseFlags chase_flags,
int access_mode,
char **ret_path) {
int chase_and_accessat(int dir_fd, const char *path, ChaseFlags chase_flags, int access_mode, char **ret_path) {
_cleanup_close_ int path_fd = -EBADF;
_cleanup_free_ char *p = NULL;
int r;
@@ -999,13 +931,7 @@ int chase_and_fopenat_unlocked(
return 0;
}
int chase_and_unlinkat(
int dir_fd,
const char *path,
ChaseFlags chase_flags,
int unlink_flags,
char **ret_path) {
int chase_and_unlinkat(int dir_fd, const char *path, ChaseFlags chase_flags, int unlink_flags, char **ret_path) {
_cleanup_free_ char *p = NULL, *fname = NULL;
_cleanup_close_ int fd = -EBADF;
int r;
@@ -1030,12 +956,7 @@ int chase_and_unlinkat(
return 0;
}
int chase_and_open_parent_at(
int dir_fd,
const char *path,
ChaseFlags chase_flags,
char **ret_filename) {
int chase_and_open_parent_at(int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_filename) {
int pfd, r;
assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
+1 -1
View File
@@ -1106,7 +1106,7 @@ int xopenat(int dir_fd, const char *path, int flags, mode_t mode) {
if (isempty(path)) {
assert(!FLAGS_SET(flags, O_CREAT|O_EXCL));
return fd_reopen(dir_fd, flags);
return fd_reopen(dir_fd, flags & ~O_NOFOLLOW);
}
if (FLAGS_SET(flags, O_DIRECTORY|O_CREAT)) {