qemu/0057-virtiofsd-use-proc-self-fd-O_PATH-file-descriptor.patch

375 lines
11 KiB
Diff
Raw Normal View History

From: Stefan Hajnoczi <stefanha@redhat.com>
Date: Mon, 27 Jan 2020 19:01:26 +0000
Subject: [PATCH] virtiofsd: use /proc/self/fd/ O_PATH file descriptor
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Sandboxing will remove /proc from the mount namespace so we can no
longer build string paths into "/proc/self/fd/...".
Keep an O_PATH file descriptor so we can still re-open fds via
/proc/self/fd.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
(cherry picked from commit 9f59d175e2ca96f0b87f534dba69ea547dd35945)
---
tools/virtiofsd/passthrough_ll.c | 130 ++++++++++++++++++++++++-------
1 file changed, 103 insertions(+), 27 deletions(-)
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
index e3d65c3676..e2e2211ea1 100644
--- a/tools/virtiofsd/passthrough_ll.c
+++ b/tools/virtiofsd/passthrough_ll.c
@@ -110,6 +110,9 @@ struct lo_data {
struct lo_map ino_map; /* protected by lo->mutex */
struct lo_map dirp_map; /* protected by lo->mutex */
struct lo_map fd_map; /* protected by lo->mutex */
+
+ /* An O_PATH file descriptor to /proc/self/fd/ */
+ int proc_self_fd;
};
static const struct fuse_opt lo_opts[] = {
@@ -379,9 +382,9 @@ static int lo_parent_and_name(struct lo_data *lo, struct lo_inode *inode,
int res;
retry:
- sprintf(procname, "/proc/self/fd/%i", inode->fd);
+ sprintf(procname, "%i", inode->fd);
- res = readlink(procname, path, PATH_MAX);
+ res = readlinkat(lo->proc_self_fd, procname, path, PATH_MAX);
if (res < 0) {
fuse_log(FUSE_LOG_WARNING, "%s: readlink failed: %m\n", __func__);
goto fail_noretry;
@@ -477,9 +480,9 @@ static int utimensat_empty(struct lo_data *lo, struct lo_inode *inode,
}
return res;
}
- sprintf(path, "/proc/self/fd/%i", inode->fd);
+ sprintf(path, "%i", inode->fd);
- return utimensat(AT_FDCWD, path, tv, 0);
+ return utimensat(lo->proc_self_fd, path, tv, 0);
fallback:
res = lo_parent_and_name(lo, inode, path, &parent);
@@ -535,8 +538,8 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
if (fi) {
res = fchmod(fd, attr->st_mode);
} else {
- sprintf(procname, "/proc/self/fd/%i", ifd);
- res = chmod(procname, attr->st_mode);
+ sprintf(procname, "%i", ifd);
+ res = fchmodat(lo->proc_self_fd, procname, attr->st_mode, 0);
}
if (res == -1) {
goto out_err;
@@ -552,11 +555,23 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
}
}
if (valid & FUSE_SET_ATTR_SIZE) {
+ int truncfd;
+
if (fi) {
- res = ftruncate(fd, attr->st_size);
+ truncfd = fd;
} else {
- sprintf(procname, "/proc/self/fd/%i", ifd);
- res = truncate(procname, attr->st_size);
+ sprintf(procname, "%i", ifd);
+ truncfd = openat(lo->proc_self_fd, procname, O_RDWR);
+ if (truncfd < 0) {
+ goto out_err;
+ }
+ }
+
+ res = ftruncate(truncfd, attr->st_size);
+ if (!fi) {
+ saverr = errno;
+ close(truncfd);
+ errno = saverr;
}
if (res == -1) {
goto out_err;
@@ -874,9 +889,9 @@ static int linkat_empty_nofollow(struct lo_data *lo, struct lo_inode *inode,
return res;
}
- sprintf(path, "/proc/self/fd/%i", inode->fd);
+ sprintf(path, "%i", inode->fd);
- return linkat(AT_FDCWD, path, dfd, name, AT_SYMLINK_FOLLOW);
+ return linkat(lo->proc_self_fd, path, dfd, name, AT_SYMLINK_FOLLOW);
fallback:
res = lo_parent_and_name(lo, inode, path, &parent);
@@ -1404,8 +1419,8 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
fi->flags &= ~O_APPEND;
}
- sprintf(buf, "/proc/self/fd/%i", lo_fd(req, ino));
- fd = open(buf, fi->flags & ~O_NOFOLLOW);
+ sprintf(buf, "%i", lo_fd(req, ino));
+ fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW);
if (fd == -1) {
return (void)fuse_reply_err(req, errno);
}
@@ -1458,7 +1473,6 @@ static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
struct fuse_file_info *fi)
{
int res;
- (void)ino;
int fd;
char *buf;
@@ -1466,12 +1480,14 @@ static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
(void *)fi);
if (!fi) {
- res = asprintf(&buf, "/proc/self/fd/%i", lo_fd(req, ino));
+ struct lo_data *lo = lo_data(req);
+
+ res = asprintf(&buf, "%i", lo_fd(req, ino));
if (res == -1) {
return (void)fuse_reply_err(req, errno);
}
- fd = open(buf, O_RDWR);
+ fd = openat(lo->proc_self_fd, buf, O_RDWR);
free(buf);
if (fd == -1) {
return (void)fuse_reply_err(req, errno);
@@ -1587,11 +1603,13 @@ static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
size_t size)
{
+ struct lo_data *lo = lo_data(req);
char *value = NULL;
char procname[64];
struct lo_inode *inode;
ssize_t ret;
int saverr;
+ int fd = -1;
inode = lo_inode(req, ino);
if (!inode) {
@@ -1616,7 +1634,11 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
goto out;
}
- sprintf(procname, "/proc/self/fd/%i", inode->fd);
+ sprintf(procname, "%i", inode->fd);
+ fd = openat(lo->proc_self_fd, procname, O_RDONLY);
+ if (fd < 0) {
+ goto out_err;
+ }
if (size) {
value = malloc(size);
@@ -1624,7 +1646,7 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
goto out_err;
}
- ret = getxattr(procname, name, value, size);
+ ret = fgetxattr(fd, name, value, size);
if (ret == -1) {
goto out_err;
}
@@ -1635,7 +1657,7 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
fuse_reply_buf(req, value, ret);
} else {
- ret = getxattr(procname, name, NULL, 0);
+ ret = fgetxattr(fd, name, NULL, 0);
if (ret == -1) {
goto out_err;
}
@@ -1644,6 +1666,10 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
}
out_free:
free(value);
+
+ if (fd >= 0) {
+ close(fd);
+ }
return;
out_err:
@@ -1655,11 +1681,13 @@ out:
static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
{
+ struct lo_data *lo = lo_data(req);
char *value = NULL;
char procname[64];
struct lo_inode *inode;
ssize_t ret;
int saverr;
+ int fd = -1;
inode = lo_inode(req, ino);
if (!inode) {
@@ -1683,7 +1711,11 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
goto out;
}
- sprintf(procname, "/proc/self/fd/%i", inode->fd);
+ sprintf(procname, "%i", inode->fd);
+ fd = openat(lo->proc_self_fd, procname, O_RDONLY);
+ if (fd < 0) {
+ goto out_err;
+ }
if (size) {
value = malloc(size);
@@ -1691,7 +1723,7 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
goto out_err;
}
- ret = listxattr(procname, value, size);
+ ret = flistxattr(fd, value, size);
if (ret == -1) {
goto out_err;
}
@@ -1702,7 +1734,7 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
fuse_reply_buf(req, value, ret);
} else {
- ret = listxattr(procname, NULL, 0);
+ ret = flistxattr(fd, NULL, 0);
if (ret == -1) {
goto out_err;
}
@@ -1711,6 +1743,10 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
}
out_free:
free(value);
+
+ if (fd >= 0) {
+ close(fd);
+ }
return;
out_err:
@@ -1724,9 +1760,11 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
const char *value, size_t size, int flags)
{
char procname[64];
+ struct lo_data *lo = lo_data(req);
struct lo_inode *inode;
ssize_t ret;
int saverr;
+ int fd = -1;
inode = lo_inode(req, ino);
if (!inode) {
@@ -1751,21 +1789,31 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
goto out;
}
- sprintf(procname, "/proc/self/fd/%i", inode->fd);
+ sprintf(procname, "%i", inode->fd);
+ fd = openat(lo->proc_self_fd, procname, O_RDWR);
+ if (fd < 0) {
+ saverr = errno;
+ goto out;
+ }
- ret = setxattr(procname, name, value, size, flags);
+ ret = fsetxattr(fd, name, value, size, flags);
saverr = ret == -1 ? errno : 0;
out:
+ if (fd >= 0) {
+ close(fd);
+ }
fuse_reply_err(req, saverr);
}
static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
{
char procname[64];
+ struct lo_data *lo = lo_data(req);
struct lo_inode *inode;
ssize_t ret;
int saverr;
+ int fd = -1;
inode = lo_inode(req, ino);
if (!inode) {
@@ -1789,12 +1837,20 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
goto out;
}
- sprintf(procname, "/proc/self/fd/%i", inode->fd);
+ sprintf(procname, "%i", inode->fd);
+ fd = openat(lo->proc_self_fd, procname, O_RDWR);
+ if (fd < 0) {
+ saverr = errno;
+ goto out;
+ }
- ret = removexattr(procname, name);
+ ret = fremovexattr(fd, name);
saverr = ret == -1 ? errno : 0;
out:
+ if (fd >= 0) {
+ close(fd);
+ }
fuse_reply_err(req, saverr);
}
@@ -1887,12 +1943,25 @@ static void print_capabilities(void)
printf("}\n");
}
+static void setup_proc_self_fd(struct lo_data *lo)
+{
+ lo->proc_self_fd = open("/proc/self/fd", O_PATH);
+ if (lo->proc_self_fd == -1) {
+ fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n");
+ exit(1);
+ }
+}
+
int main(int argc, char *argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
struct fuse_session *se;
struct fuse_cmdline_opts opts;
- struct lo_data lo = { .debug = 0, .writeback = 0 };
+ struct lo_data lo = {
+ .debug = 0,
+ .writeback = 0,
+ .proc_self_fd = -1,
+ };
struct lo_map_elem *root_elem;
int ret = -1;
@@ -2003,6 +2072,9 @@ int main(int argc, char *argv[])
fuse_daemonize(opts.foreground);
+ /* Must be after daemonize to get the right /proc/self/fd */
+ setup_proc_self_fd(&lo);
+
/* Block until ctrl+c or fusermount -u */
ret = virtio_loop(se);
@@ -2018,6 +2090,10 @@ err_out1:
lo_map_destroy(&lo.dirp_map);
lo_map_destroy(&lo.ino_map);
+ if (lo.proc_self_fd >= 0) {
+ close(lo.proc_self_fd);
+ }
+
if (lo.root.fd >= 0) {
close(lo.root.fd);
}