[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 047/109] virtiofsd: use /proc/self/fd/ O_PATH file descriptor
From: |
Dr. David Alan Gilbert (git) |
Subject: |
[PATCH v2 047/109] virtiofsd: use /proc/self/fd/ O_PATH file descriptor |
Date: |
Tue, 21 Jan 2020 12:23:31 +0000 |
From: Stefan Hajnoczi <address@hidden>
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 <address@hidden>
Reviewed-by: Philippe Mathieu-Daudé <address@hidden>
---
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 7a16e6b4d6..322205ba3a 100644
--- a/tools/virtiofsd/passthrough_ll.c
+++ b/tools/virtiofsd/passthrough_ll.c
@@ -109,6 +109,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[] = {
@@ -378,9 +381,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;
@@ -476,9 +479,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);
@@ -534,8 +537,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;
@@ -551,11 +554,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;
@@ -873,9 +888,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);
@@ -1403,8 +1418,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);
}
@@ -1457,7 +1472,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;
@@ -1465,12 +1479,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);
@@ -1586,11 +1602,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) {
@@ -1615,7 +1633,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);
@@ -1623,7 +1645,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;
}
@@ -1634,7 +1656,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;
}
@@ -1643,6 +1665,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:
@@ -1654,11 +1680,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) {
@@ -1682,7 +1710,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);
@@ -1690,7 +1722,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;
}
@@ -1701,7 +1733,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;
}
@@ -1710,6 +1742,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:
@@ -1723,9 +1759,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) {
@@ -1750,21 +1788,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) {
@@ -1788,12 +1836,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);
}
@@ -1886,12 +1942,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;
@@ -2002,6 +2071,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);
@@ -2017,6 +2089,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);
}
--
2.24.1
- [PATCH v2 037/109] virtiofsd: passthrough_ll: add fd_map to hide file descriptors, (continued)
- [PATCH v2 037/109] virtiofsd: passthrough_ll: add fd_map to hide file descriptors, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 038/109] virtiofsd: passthrough_ll: add fallback for racy ops, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 039/109] virtiofsd: validate path components, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 040/109] virtiofsd: Plumb fuse_bufvec through to do_write_buf, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 041/109] virtiofsd: Pass write iov's all the way through, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 042/109] virtiofsd: add fuse_mbuf_iter API, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 043/109] virtiofsd: validate input buffer sizes in do_write_buf(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 045/109] virtiofsd: prevent ".." escape in lo_do_lookup(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 044/109] virtiofsd: check input buffer size in fuse_lowlevel.c ops, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 046/109] virtiofsd: prevent ".." escape in lo_do_readdir(), Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 047/109] virtiofsd: use /proc/self/fd/ O_PATH file descriptor,
Dr. David Alan Gilbert (git) <=
- [PATCH v2 048/109] virtiofsd: sandbox mount namespace, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 049/109] virtiofsd: move to an empty network namespace, Dr. David Alan Gilbert (git), 2020/01/21
- [PATCH v2 051/109] virtiofsd: add seccomp whitelist, Dr. David Alan Gilbert (git), 2020/01/21
- Re: [PATCH v2 051/109] virtiofsd: add seccomp whitelist, Florian Weimer, 2020/01/24