Merge pull request #27701 from poettering/switch-root-same-file
switch-root: use same_files() logic when checking whether new and old root dir are actually the same
This commit is contained in:
@@ -109,7 +109,7 @@ int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int
|
||||
/* Can't setns to your own userns, since then you could escalate from non-root to root in
|
||||
* your own namespace, so check if namespaces are equal before attempting to enter. */
|
||||
|
||||
r = files_same(FORMAT_PROC_FD_PATH(userns_fd), "/proc/self/ns/user", 0);
|
||||
r = inode_same_at(userns_fd, "", AT_FDCWD, "/proc/self/ns/user", AT_EMPTY_PATH);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r)
|
||||
|
||||
@@ -485,8 +485,8 @@ int path_compare(const char *a, const char *b) {
|
||||
}
|
||||
}
|
||||
|
||||
bool path_equal_or_files_same(const char *a, const char *b, int flags) {
|
||||
return path_equal(a, b) || files_same(a, b, flags) > 0;
|
||||
bool path_equal_or_inode_same(const char *a, const char *b, int flags) {
|
||||
return path_equal(a, b) || inode_same(a, b, flags) > 0;
|
||||
}
|
||||
|
||||
int path_compare_filename(const char *a, const char *b) {
|
||||
|
||||
@@ -77,7 +77,7 @@ static inline bool path_equal_filename(const char *a, const char *b) {
|
||||
return path_compare_filename(a, b) == 0;
|
||||
}
|
||||
|
||||
bool path_equal_or_files_same(const char *a, const char *b, int flags);
|
||||
bool path_equal_or_inode_same(const char *a, const char *b, int flags);
|
||||
|
||||
char* path_extend_internal(char **x, ...);
|
||||
#define path_extend(x, ...) path_extend_internal(x, __VA_ARGS__, POINTER_MAX)
|
||||
|
||||
@@ -946,7 +946,7 @@ int pid_from_same_root_fs(pid_t pid) {
|
||||
|
||||
root = procfs_file_alloca(pid, "root");
|
||||
|
||||
return files_same(root, "/proc/1/root", 0);
|
||||
return inode_same(root, "/proc/1/root", 0);
|
||||
}
|
||||
|
||||
bool is_main_thread(void) {
|
||||
|
||||
@@ -223,7 +223,7 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
|
||||
return false;
|
||||
|
||||
if (a->sockaddr.un.sun_path[0]) {
|
||||
if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
|
||||
if (!path_equal_or_inode_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
|
||||
return false;
|
||||
} else {
|
||||
if (a->size != b->size)
|
||||
|
||||
@@ -183,16 +183,18 @@ int path_is_read_only_fs(const char *path) {
|
||||
return fd_is_read_only_fs(fd);
|
||||
}
|
||||
|
||||
int files_same(const char *filea, const char *fileb, int flags) {
|
||||
int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags) {
|
||||
struct stat a, b;
|
||||
|
||||
assert(fda >= 0 || fda == AT_FDCWD);
|
||||
assert(filea);
|
||||
assert(fdb >= 0 || fdb == AT_FDCWD);
|
||||
assert(fileb);
|
||||
|
||||
if (fstatat(AT_FDCWD, filea, &a, flags) < 0)
|
||||
if (fstatat(fda, filea, &a, flags) < 0)
|
||||
return log_debug_errno(errno, "Cannot stat %s: %m", filea);
|
||||
|
||||
if (fstatat(AT_FDCWD, fileb, &b, flags) < 0)
|
||||
if (fstatat(fdb, fileb, &b, flags) < 0)
|
||||
return log_debug_errno(errno, "Cannot stat %s: %m", fileb);
|
||||
|
||||
return stat_inode_same(&a, &b);
|
||||
|
||||
@@ -37,7 +37,11 @@ static inline int null_or_empty_path(const char *fn) {
|
||||
|
||||
int path_is_read_only_fs(const char *path);
|
||||
|
||||
int files_same(const char *filea, const char *fileb, int flags);
|
||||
int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags);
|
||||
|
||||
static inline int inode_same(const char *filea, const char *fileb, int flags) {
|
||||
return inode_same_at(AT_FDCWD, filea, AT_FDCWD, fileb, flags);
|
||||
}
|
||||
|
||||
/* The .f_type field of struct statfs is really weird defined on
|
||||
* different archs. Let's give its type a name. */
|
||||
|
||||
+1
-1
@@ -891,7 +891,7 @@ int running_in_chroot(void) {
|
||||
if (getpid_cached() == 1)
|
||||
return false; /* We're PID 1, we can't be in a chroot. */
|
||||
|
||||
r = files_same("/proc/1/root", "/", 0);
|
||||
r = inode_same("/proc/1/root", "/", 0);
|
||||
if (r == -ENOENT) {
|
||||
r = proc_mounted();
|
||||
if (r == 0) {
|
||||
|
||||
Reference in New Issue
Block a user