HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/basic/fd-util.c
Git blob: 5c07983782f0a96e623df96d13679dbd82d1b5e8
Lines 1001–1209 of 1,209
int flags, unexpected_flags;
/* Check if an extrinsic fd is safe to work on (by a privileged service). This ensures that clients
* can't trick a privileged service into giving access to a file the client doesn't already have
* access to (especially via something like O_PATH).
*
* O_NOFOLLOW: For some reason the kernel will return this flag from fcntl(); it doesn't go away
* immediately after open(). It should have no effect whatsoever to an already-opened FD,
* and since we refuse O_PATH it should be safe.
*
* RAW_O_LARGEFILE: glibc secretly sets this and neglects to hide it from us if we call fcntl.
* See comment in src/basic/include/fcntl.h for more details about this.
*
* If 'extra_flags' is specified as non-zero the included flags are also allowed.
*/
assert(fd >= 0);
flags = fcntl(fd, F_GETFL);
if (flags < 0)
return -errno;
unexpected_flags = flags & ~(O_ACCMODE_STRICT|O_NOFOLLOW|RAW_O_LARGEFILE|extra_flags);
if (unexpected_flags != 0)
return log_debug_errno(SYNTHETIC_ERRNO(EREMOTEIO),
"Unexpected flags set for extrinsic fd: 0%o",
(unsigned) unexpected_flags);
return flags & (O_ACCMODE_STRICT | extra_flags); /* return the flags variable, but remove the noise */
}
unsigned read_nr_open(void) {
unsigned v;
int r;
/* Returns the kernel's current fd limit, either by reading it of /proc/sys if that works, or using the
* hard-coded default compiled-in value of current kernels (1M) if not. This call will never fail. */
r = sysctl_read_unsigned("fs/nr_open", &v);
if (r < 0)
log_debug_errno(r, "Failed to read fs.nr_open sysctl, ignoring: %m");
else
return v;
/* If we fail, fall back to the hard-coded kernel limit of 1024 * 1024. */
return NR_OPEN_DEFAULT;
}
int fd_get_diskseq(int fd, uint64_t *ret) {
uint64_t diskseq;
assert(fd >= 0);
assert(ret);
if (ioctl(fd, BLKGETDISKSEQ, &diskseq) < 0) {
/* Note that the kernel is weird: non-existing ioctls currently return EINVAL
* rather than ENOTTY on loopback block devices. They should fix that in the kernel,
* but in the meantime we accept both here. */
if (!ERRNO_IS_NOT_SUPPORTED(errno) && errno != EINVAL)
return -errno;
return -EOPNOTSUPP;
}
*ret = diskseq;
return 0;
}
static bool is_literal_root(const char *p) {
if (!p)
return false;
/* Check if string consists of at least one '/', and possibly more, but nothing else */
size_t n = strspn(p, "/");
return n >= 1 && p[n] == 0;
}
int path_is_root_at(int dir_fd, const char *path) {
assert(wildcard_fd_is_valid(dir_fd));
if (dir_fd == XAT_FDROOT && isempty(path))
return true;
if (IN_SET(dir_fd, XAT_FDROOT, AT_FDCWD) && is_literal_root(path))
return true;
_cleanup_close_ int fd = -EBADF;
if (!isempty(path)) {
fd = xopenat(dir_fd, path, O_PATH|O_DIRECTORY|O_CLOEXEC);
if (fd == -ENOTDIR)
return false; /* the root dir must be a dir */
if (fd < 0)
return fd;
dir_fd = fd;
}
/* Even if the root directory has the same inode as our fd, the fd may not point to the root
* directory "/", and we also need to check that the mount ids are the same. Otherwise, a construct
* like the following could be used to trick us:
*
* $ mkdir /tmp/x
* $ mount --bind / /tmp/x
*/
return fds_inode_and_mount_same(dir_fd, XAT_FDROOT);
}
int fds_inode_and_mount_same(int fd1, int fd2) {
struct statx sx1, sx2;
int r;
assert(wildcard_fd_is_valid(fd1));
assert(wildcard_fd_is_valid(fd2));
r = xstatx(fd1, /* path = */ NULL, AT_EMPTY_PATH,
STATX_TYPE|STATX_INO|STATX_MNT_ID,
&sx1);
if (r < 0)
return r;
if (fd1 == fd2) /* Shortcut things if fds are the same (only after validating the fd) */
return true;
r = xstatx(fd2, /* path = */ NULL, AT_EMPTY_PATH,
STATX_TYPE|STATX_INO|STATX_MNT_ID,
&sx2);
if (r < 0)
return r;
r = statx_mount_same(&sx1, &sx2);
if (r <= 0)
return r;
return statx_inode_same(&sx1, &sx2);
}
int resolve_xat_fdroot(int *fd, const char **path, char **ret_buffer) {
assert(fd);
assert(path);
assert(ret_buffer);
if (*fd != XAT_FDROOT) {
*ret_buffer = NULL;
return 0;
}
if (isempty(*path)) {
*path = "/";
*ret_buffer = NULL;
} else if (!path_is_absolute(*path)) {
char *p = strjoin("/", *path);
if (!p)
return -ENOMEM;
*path = *ret_buffer = p;
}
*fd = AT_FDCWD;
return 1;
}
char* format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
assert(buf);
assert(fd >= 0);
assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd));
return buf;
}
const char* accmode_to_string(int flags) {
switch (flags & O_ACCMODE_STRICT) {
case O_RDONLY:
return "ro";
case O_WRONLY:
return "wo";
case O_RDWR:
return "rw";
default:
return NULL;
}
}
char* format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd) {
assert(buf);
assert(fd >= 0);
assert(pid >= 0);
assert_se(snprintf_ok(buf, PROC_PID_FD_PATH_MAX, "/proc/" PID_FMT "/fd/%i", pid == 0 ? getpid_cached() : pid, fd));
return buf;
}
int proc_fd_enoent_errno(void) {
int r;
/* When ENOENT is returned during the use of FORMAT_PROC_FD_PATH, it can mean two things:
* that the fd does not exist or that /proc/ is not mounted.
* Let's make things debuggable and figure out the most appropriate errno. */
r = proc_mounted();
if (r == 0)
return -ENOSYS; /* /proc/ is not available or not set up properly, we're most likely
in some chroot environment. */
if (r > 0)
return -EBADF; /* If /proc/ is definitely around then this means the fd is not valid. */
return -ENOENT; /* Otherwise let's propagate the original ENOENT. */
}