From 4265a66a5e84137d1f874c413b2961f363c8f358 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 23 Sep 2020 18:19:21 +0200 Subject: [PATCH] fs-util: check for /proc mounted in access_fd() It's a very basic function very similar to other cases where we go via /proc/self/fd/, hence do the explicit proc_mounted() check here too. --- src/basic/fs-util.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index fb74501105..13e7b1b163 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -1298,16 +1298,25 @@ int chase_symlinks_and_stat( int access_fd(int fd, int mode) { char p[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1]; - int r; /* Like access() but operates on an already open fd */ xsprintf(p, "/proc/self/fd/%i", fd); - r = access(p, mode); - if (r < 0) - return -errno; + if (access(p, mode) < 0) { + if (errno != ENOENT) + return -errno; - return r; + /* ENOENT can mean two things: that the fd does not exist or that /proc is not mounted. Let's + * make things debuggable and distinguish the two. */ + + if (proc_mounted() == 0) + return -ENOSYS; /* /proc is not available or not set up properly, we're most likely in some chroot + * environment. */ + + return -EBADF; /* The directory exists, hence it's the fd that doesn't. */ + } + + return 0; } void unlink_tempfilep(char (*p)[]) {