-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
qsb-089-2023.txt
236 lines (202 loc) · 10.5 KB
/
qsb-089-2023.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
---===[ Qubes Security Bulletin 089 ]===---
2023-05-11
Qrexec: Memory corruption in service request handling
User action required
---------------------
Users must install the following specific packages in order to address
the issues discussed in this bulletin:
For Qubes 4.1, in dom0:
- qrexec packages, version 4.1.21
These packages will migrate from the security-testing repository to the
current (stable) repository over the next two weeks after being tested
by the community. [1] Once available, the packages are to be installed
via the Qubes Update tool or its command-line equivalents. [2]
Summary
--------
Due to a bug in qrexec [3], a malicious qube can cause memory corruption
in the qrexec-daemon. The Qubes Security Team is not aware of any way to
exploit this vulnerability in an attack (not even in a denial-of-service
attack that only crashes the process). However, we cannot completely
rule out such a possibility.
Impact
-------
While we consider the successful exploitation of this vulnerability to
be very unlikely, an attacker could theoretically use it to crash the
qrexec-daemon or execute arbitrary code in dom0.
Discussion
-----------
Qubes OS features a framework known as "qrexec," which allows different
qubes to communicate with each other in a controlled manner. [3][4]
These interactions are restricted by the system's RPC policies. [5] In
particular, qrexec can be used to allow less trusted qubes to
communicate with more trusted qubes, including dom0.
Incoming RPC calls are handled by the qrexec-daemon process. Qubes OS
4.1 introduced a new qrexec message type, `MSG_TRIGGER_SERVICE3`, which
allows much larger requests (theoretically up to 65000 bytes, compared
to 64 bytes in earlier versions). This message type uses a
dynamically-allocated buffer for the message body based on the request
length. The code used to validate the request length has an off-by-one
error that can cause memory corruption, as described below.
First, the incoming message is validated in the
`sanitize_message_from_agent()` function:
1177 static void sanitize_message_from_agent(struct msg_header *untrusted_header)
1178 {
1179 switch (untrusted_header->type) {
...
1191 case MSG_TRIGGER_SERVICE3:
1192 if (protocol_version < QREXEC_PROTOCOL_V3) {
1193 LOG(ERROR, "agent sent (new) MSG_TRIGGER_SERVICE3 "
1194 "although it uses protocol %d", protocol_version);
1195 exit(1);
1196 }
1197 if (untrusted_header->len < sizeof(struct trigger_service_params3)) {
1198 LOG(ERROR, "agent sent invalid MSG_TRIGGER_SERVICE3 packet");
1199 exit(1);
1200 }
1201 if (untrusted_header->len - sizeof(struct trigger_service_params3)
1202 > MAX_SERVICE_NAME_LEN) {
1203 LOG(ERROR, "agent sent too large MSG_TRIGGER_SERVICE3 packet");
1204 exit(1);
1205 }
1206 break;
...
The second condition, on line 1197, verifies that the message sent by
the qrexec-agent (from a VM) is not shorter than the message header
defined in `struct trigger_service_params3`. However, it fails to
account for a mandatory NUL character at the end of the message payload
(the service name and its argument). Later on, the
`handle_message_from_agent()` function processes the message as follows:
1222 static void handle_message_from_agent(void)
1223 {
1224 struct msg_header hdr, untrusted_hdr;
1225 struct trigger_service_params untrusted_params, params;
1226 struct trigger_service_params3 untrusted_params3, params3;
1227 char *untrusted_service_name = NULL, *service_name = NULL;
1228 size_t service_name_len;
1229
1230 if (libvchan_recv(vchan, &untrusted_hdr, sizeof(untrusted_hdr))
1231 != sizeof(untrusted_hdr))
1232 handle_vchan_error("recv hdr");
1233 /* sanitize start */
1234 sanitize_message_from_agent(&untrusted_hdr);
1235 hdr = untrusted_hdr;
1236 /* sanitize end */
...
1241 switch (hdr.type) {
...
1262 case MSG_TRIGGER_SERVICE3:
1263 service_name_len = hdr.len - sizeof(untrusted_params3);
1264 untrusted_service_name = malloc(service_name_len);
1265 if (!untrusted_service_name)
1266 handle_vchan_error("malloc(service_name)");
1267
1268 if (libvchan_recv(vchan, &untrusted_params3, sizeof(untrusted_params3))
1269 != sizeof(untrusted_params3))
1270 handle_vchan_error("recv params3");
1271 if (libvchan_recv(vchan, untrusted_service_name, service_name_len)
1272 != (int)service_name_len)
1273 handle_vchan_error("recv params3(service_name)");
1274
1275 /* sanitize start */
1276 ENSURE_NULL_TERMINATED(untrusted_params3.target_domain);
1277 ENSURE_NULL_TERMINATED(untrusted_params3.request_id.ident);
1278 untrusted_service_name[service_name_len-1] = 0;
1279 sanitize_name(untrusted_params3.target_domain, "@:");
1280 sanitize_name(untrusted_params3.request_id.ident, " ");
1281 sanitize_name(untrusted_service_name, "+");
1282 params3 = untrusted_params3;
1283 service_name = untrusted_service_name;
1284 untrusted_service_name = NULL;
1285 /* sanitize end */
...
The initial call to `sanitize_message_from_agent()` is visible in line
1234. Then, the function calculates the expected service name length in
line 1263, allocates memory for it in line 1264, and receives both the
rest of the header and its payload in lines 1268-1273. Since
`sanitize_message_from_agent()` allows the `hdr.len` to be equal to
`sizeof(untrusted_params3)`, `service_name_len` can be zero. This means
that adding the terminating NUL character in line 1278 can write outside
of the allocated buffer. Furthermore, in such a case, the
`untrusted_service_name` buffer is allocated with a size of zero, and
the `sanitize_name()` call in line 1281 can write beyond the buffer too.
The `sanitize_name()` function, listed below, replaces disallowed
characters with underscores (byte 0x5f) until it finds the terminating
NUL character:
759 static void sanitize_name(char * untrusted_s_signed, char *extra_allowed_chars)
760 {
761 unsigned char * untrusted_s;
762 for (untrusted_s=(unsigned char*)untrusted_s_signed; *untrusted_s; untrusted_s++) {
763 if (*untrusted_s >= 'a' && *untrusted_s <= 'z')
764 continue;
765 if (*untrusted_s >= 'A' && *untrusted_s <= 'Z')
766 continue;
767 if (*untrusted_s >= '0' && *untrusted_s <= '9')
768 continue;
769 if (*untrusted_s == '_' ||
770 *untrusted_s == '-' ||
771 *untrusted_s == '.')
772 continue;
773 if (extra_allowed_chars && strchr(extra_allowed_chars, *untrusted_s))
774 continue;
775 *untrusted_s = '_';
776 }
777 }
This code runs in dom0. In Qubes OS 4.1, dom0 is based on Fedora 32
x86_64, which uses the GNU libc 2.31 library. In this implementation
[5], the `malloc()` call (line 1264) always allocates at least 32 bytes,
even if the requested size is 0. This means that the returned pointer is
not NULL in this case (which would be allowed by the specification for
zero-sized allocations), so the error handling in line 1266 does not
interrupt the processing. Consequently, the NUL character written in
line 1278 hits part of the malloc metadata, specifically the most
significant byte of the allocation size. Given that the size is small
(between the 32-byte `MIN_CHUNK_SIZE` and the 1024-byte
`MIN_LARGE_SIZE`), that byte is always zero already, so this write is
harmless.
The remaining concern is about the `sanitize_name()` function. For the
bug to be harmful, it must overwrite something beyond the allocated
buffer (at least 32 bytes). This means that there cannot be a zero byte
in this area. In our analysis, this is very unlikely to occur, for the
following reasons:
1. The `malloc()` call in `handle_message_from_agent()` is the one and
only call that occurs outside of startup and connection setup. No
other malloc call occurs in that process, not even an indirect one.
The connection setup allocations can be redone when the qrexec-agent
disconnects and connects again, but the points below also apply to
this reconnection sequence. (Perhaps the only exception is handling
an error message just before exit(1), but if the attacker hasn't
corrupted malloc metadata up to this point, the attacker won't have a
chance to do it after this point either.)
2. The qrexec-daemon handles only a single request at a time. At no
point are multiple buffers for `service_name` allocated in the
process at the same time. All remaining handling occurs in a separate
process (which also handles only a single request).
3. Due to how GNU libc's malloc works, if there is a small chunk
available, it will be used. It won't be split out of a bigger chunk.
And, due to point 2 above, if there was a small chunk initially, it
will get used again.
4. The allocations done during startup and reconnection leave a few
small free chunks that are interleaved with allocated areas (the
libvchan_t structure, something from libxengnttab, libxentoollog, and
a few others). These memory chunks can't be merged due to their
layout.
5. Every small allocation includes a NUL byte somewhere in the payload
before being freed. This applies to both initial areas as well as to
the buffers used for `service_name`.
Given all of these considerations, successfully exploiting this
vulnerability in an attack is very unlikely. However, due to the
complexity of the memory allocator, we cannot completely rule out such a
possibility.
Credits
--------
This issue was discovered by Demi Marie Obenour.
References
-----------
[1] https://www.qubes-os.org/doc/testing/
[2] https://www.qubes-os.org/doc/how-to-update/
[3] https://www.qubes-os.org/doc/qrexec/
[4] https://www.qubes-os.org/doc/qrexec-internals/
[5] https://sourceware.org/glibc/wiki/MallocInternals
--
The Qubes Security Team
https://www.qubes-os.org/security/