|
| 1 | +From db34ce8e5b917f08456e9e0d3f02a4d737c00441 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Kevin Lockwood <v-klockwood@microsoft.com> |
| 3 | +Date: Tue, 8 Apr 2025 16:56:52 -0700 |
| 4 | +Subject: [PATCH] [High] Patch qemu for CVE-2023-2861 |
| 5 | + |
| 6 | +Link: https://gitlab.com/qemu-project/qemu/-/commit/f6b0de53fb87ddefed348a39284c8e2f28dc4eda |
| 7 | +--- |
| 8 | + fsdev/virtfs-proxy-helper.c | 27 +++++++++++++++++++++++-- |
| 9 | + hw/9pfs/9p-util.h | 40 +++++++++++++++++++++++++++++++++++++ |
| 10 | + 2 files changed, 65 insertions(+), 2 deletions(-) |
| 11 | + |
| 12 | +diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c |
| 13 | +index 15c0e79b0..f9e4669a5 100644 |
| 14 | +--- a/fsdev/virtfs-proxy-helper.c |
| 15 | ++++ b/fsdev/virtfs-proxy-helper.c |
| 16 | +@@ -26,6 +26,7 @@ |
| 17 | + #include "qemu/xattr.h" |
| 18 | + #include "9p-iov-marshal.h" |
| 19 | + #include "hw/9pfs/9p-proxy.h" |
| 20 | ++#include "hw/9pfs/9p-util.h" |
| 21 | + #include "fsdev/9p-iov-marshal.h" |
| 22 | + |
| 23 | + #define PROGNAME "virtfs-proxy-helper" |
| 24 | +@@ -338,6 +339,28 @@ static void resetugid(int suid, int sgid) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | ++/* |
| 29 | ++ * Open regular file or directory. Attempts to open any special file are |
| 30 | ++ * rejected. |
| 31 | ++ * |
| 32 | ++ * returns file descriptor or -1 on error |
| 33 | ++ */ |
| 34 | ++static int open_regular(const char *pathname, int flags, mode_t mode) |
| 35 | ++{ |
| 36 | ++ int fd; |
| 37 | ++ |
| 38 | ++ fd = open(pathname, flags, mode); |
| 39 | ++ if (fd < 0) { |
| 40 | ++ return fd; |
| 41 | ++ } |
| 42 | ++ |
| 43 | ++ if (close_if_special_file(fd) < 0) { |
| 44 | ++ return -1; |
| 45 | ++ } |
| 46 | ++ |
| 47 | ++ return fd; |
| 48 | ++} |
| 49 | ++ |
| 50 | + /* |
| 51 | + * send response in two parts |
| 52 | + * 1) ProxyHeader |
| 53 | +@@ -682,7 +705,7 @@ static int do_create(struct iovec *iovec) |
| 54 | + if (ret < 0) { |
| 55 | + goto unmarshal_err_out; |
| 56 | + } |
| 57 | +- ret = open(path.data, flags, mode); |
| 58 | ++ ret = open_regular(path.data, flags, mode); |
| 59 | + if (ret < 0) { |
| 60 | + ret = -errno; |
| 61 | + } |
| 62 | +@@ -707,7 +730,7 @@ static int do_open(struct iovec *iovec) |
| 63 | + if (ret < 0) { |
| 64 | + goto err_out; |
| 65 | + } |
| 66 | +- ret = open(path.data, flags); |
| 67 | ++ ret = open_regular(path.data, flags, 0); |
| 68 | + if (ret < 0) { |
| 69 | + ret = -errno; |
| 70 | + } |
| 71 | +diff --git a/hw/9pfs/9p-util.h b/hw/9pfs/9p-util.h |
| 72 | +index 546f46dc7..23000e917 100644 |
| 73 | +--- a/hw/9pfs/9p-util.h |
| 74 | ++++ b/hw/9pfs/9p-util.h |
| 75 | +@@ -13,12 +13,16 @@ |
| 76 | + #ifndef QEMU_9P_UTIL_H |
| 77 | + #define QEMU_9P_UTIL_H |
| 78 | + |
| 79 | ++#include "qemu/error-report.h" |
| 80 | ++ |
| 81 | + #ifdef O_PATH |
| 82 | + #define O_PATH_9P_UTIL O_PATH |
| 83 | + #else |
| 84 | + #define O_PATH_9P_UTIL 0 |
| 85 | + #endif |
| 86 | + |
| 87 | ++#define qemu_fstat fstat |
| 88 | ++ |
| 89 | + static inline void close_preserve_errno(int fd) |
| 90 | + { |
| 91 | + int serrno = errno; |
| 92 | +@@ -26,6 +30,38 @@ static inline void close_preserve_errno(int fd) |
| 93 | + errno = serrno; |
| 94 | + } |
| 95 | + |
| 96 | ++/** |
| 97 | ++ * close_if_special_file() - Close @fd if neither regular file nor directory. |
| 98 | ++ * |
| 99 | ++ * @fd: file descriptor of open file |
| 100 | ++ * Return: 0 on regular file or directory, -1 otherwise |
| 101 | ++ * |
| 102 | ++ * CVE-2023-2861: Prohibit opening any special file directly on host |
| 103 | ++ * (especially device files), as a compromised client could potentially gain |
| 104 | ++ * access outside exported tree under certain, unsafe setups. We expect |
| 105 | ++ * client to handle I/O on special files exclusively on guest side. |
| 106 | ++ */ |
| 107 | ++static inline int close_if_special_file(int fd) |
| 108 | ++{ |
| 109 | ++ struct stat stbuf; |
| 110 | ++ |
| 111 | ++ if (qemu_fstat(fd, &stbuf) < 0) { |
| 112 | ++ close_preserve_errno(fd); |
| 113 | ++ return -1; |
| 114 | ++ } |
| 115 | ++ if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)) { |
| 116 | ++ error_report_once( |
| 117 | ++ "9p: broken or compromised client detected; attempt to open " |
| 118 | ++ "special file (i.e. neither regular file, nor directory)" |
| 119 | ++ ); |
| 120 | ++ close(fd); |
| 121 | ++ errno = ENXIO; |
| 122 | ++ return -1; |
| 123 | ++ } |
| 124 | ++ |
| 125 | ++ return 0; |
| 126 | ++} |
| 127 | ++ |
| 128 | + static inline int openat_dir(int dirfd, const char *name) |
| 129 | + { |
| 130 | + return openat(dirfd, name, |
| 131 | +@@ -56,6 +92,10 @@ again: |
| 132 | + return -1; |
| 133 | + } |
| 134 | + |
| 135 | ++ if (close_if_special_file(fd) < 0) { |
| 136 | ++ return -1; |
| 137 | ++ } |
| 138 | ++ |
| 139 | + serrno = errno; |
| 140 | + /* O_NONBLOCK was only needed to open the file. Let's drop it. We don't |
| 141 | + * do that with O_PATH since fcntl(F_SETFL) isn't supported, and openat() |
| 142 | +-- |
| 143 | +2.34.1 |
| 144 | + |
0 commit comments