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