-
Notifications
You must be signed in to change notification settings - Fork 58
/
fd.c
584 lines (488 loc) · 13.4 KB
/
fd.c
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include <dlfcn.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#ifdef __linux__
#include <linux/major.h>
#include <sys/sysmacros.h>
#endif
#include <xf86drm.h>
#include "internal.h"
#include "macros.h"
#include "fd.h"
#include "tty.h"
#include "logind.h"
#ifndef EVIOCREVOKE
# define EVIOCREVOKE _IOW('E', 0x91, int)
#endif
#define DRM_MAJOR 226
struct msg_request_fd_open {
char path[32];
int flags;
enum wlc_fd_type type;
};
struct msg_request_fd_close {
dev_t st_dev;
ino_t st_ino;
};
struct msg_request_activate_vt {
int vt;
};
enum msg_type {
TYPE_CHECK,
TYPE_FD_OPEN,
TYPE_FD_CLOSE,
TYPE_ACTIVATE,
TYPE_DEACTIVATE,
TYPE_ACTIVATE_VT,
};
struct msg_request {
enum msg_type type;
union {
struct msg_request_fd_open fd_open;
struct msg_request_fd_close fd_close;
struct msg_request_activate_vt vt_activate;
};
};
struct msg_response {
enum msg_type type;
union {
bool activate;
bool deactivate;
};
};
static struct {
struct wlc_fd {
dev_t st_dev;
ino_t st_ino;
int fd;
enum wlc_fd_type type;
} fds[32];
int socket;
pid_t child;
bool has_logind;
} wlc;
static ssize_t
write_fd(int sock, int fd, const void *buffer, ssize_t buffer_size)
{
char control[CMSG_SPACE(sizeof(int))];
memset(control, 0, sizeof(control));
struct msghdr message = {
.msg_name = NULL,
.msg_namelen = 0,
.msg_iov = &(struct iovec){
.iov_base = (void*)buffer,
.iov_len = buffer_size
},
.msg_iovlen = 1,
.msg_control = NULL,
.msg_controllen = 0,
};
if (fd >= 0) {
message.msg_control = control;
message.msg_controllen = sizeof(control);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&message);
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
}
return sendmsg(sock, &message, 0);
}
static ssize_t
recv_fd(int sock, int *out_fd, void *out_buffer, ssize_t buffer_size)
{
assert(out_fd && out_buffer);
*out_fd = -1;
char control[CMSG_SPACE(sizeof(int))];
struct msghdr message = {
.msg_name = NULL,
.msg_namelen = 0,
.msg_iov = &(struct iovec){
.iov_base = out_buffer,
.iov_len = buffer_size
},
.msg_iovlen = 1,
.msg_control = &control,
.msg_controllen = sizeof(control)
};
struct cmsghdr *cmsg;
errno = 0;
ssize_t read;
if ((read = recvmsg(sock, &message, 0)) < 0)
return read;
if (message.msg_controllen == 0)
return read;
if (!(cmsg = CMSG_FIRSTHDR(&message)))
return read;
if (cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
return read;
memcpy(out_fd, CMSG_DATA(cmsg), sizeof(int));
} else if (cmsg->cmsg_len == CMSG_LEN(0)) {
*out_fd = -1;
}
return read;
}
static int
fd_open(const char *path, int flags, enum wlc_fd_type type)
{
assert(path);
struct wlc_fd *pfd = NULL;
for (uint32_t i = 0; i < LENGTH(wlc.fds); ++i) {
if (wlc.fds[i].fd < 0) {
pfd = &wlc.fds[i];
break;
}
}
if (!pfd) {
wlc_log(WLC_LOG_ERROR, "Maximum number of fds opened");
return -1;
}
/* we will only open allowed paths */
#ifdef __linux__
#define FILTER(x, m) { x, (sizeof(x) > 32 ? 32 : sizeof(x)) - 1, m }
static struct {
const char *base;
const size_t size;
uint32_t major;
} allow[WLC_FD_LAST] = {
FILTER("/dev/input/", INPUT_MAJOR), // WLC_FD_INPUT
FILTER("/dev/dri/card", DRM_MAJOR), // WLC_FD_DRM
};
#undef FILTER
if (type > WLC_FD_LAST || memcmp(path, allow[type].base, allow[type].size)) {
wlc_log(WLC_LOG_WARN, "Denying open from: %s", path);
return -1;
}
#endif
struct stat st;
if (stat(path, &st) < 0)
return -1;
#ifdef __linux__
if (major(st.st_rdev) != allow[type].major)
return -1;
#endif
int fd;
if ((fd = open(path, flags | O_CLOEXEC)) < 0)
return fd;
pfd->fd = fd;
pfd->type = type;
pfd->st_dev = st.st_dev;
pfd->st_ino = st.st_ino;
if (pfd->type == WLC_FD_DRM)
drmSetMaster(pfd->fd);
if (pfd->fd < 0)
wlc_log(WLC_LOG_WARN, "Error opening (%m): %s", path);
return pfd->fd;
}
static void
fd_close(dev_t st_dev, ino_t st_ino)
{
struct wlc_fd *pfd = NULL;
for (uint32_t i = 0; i < LENGTH(wlc.fds); ++i) {
if (wlc.fds[i].st_dev == st_dev && wlc.fds[i].st_ino == st_ino) {
pfd = &wlc.fds[i];
break;
}
}
if (!pfd) {
wlc_log(WLC_LOG_WARN, "Tried to close fd that we did not open: (%zu, %zu)", (size_t)st_dev, (size_t)st_ino);
return;
}
if (pfd->type == WLC_FD_DRM)
drmDropMaster(pfd->fd);
close(pfd->fd);
pfd->fd = -1;
}
static bool
activate(void)
{
for (uint32_t i = 0; i < LENGTH(wlc.fds); ++i) {
if (wlc.fds[i].fd < 0)
continue;
switch (wlc.fds[i].type) {
case WLC_FD_DRM:
if (drmSetMaster(wlc.fds[i].fd)) {
wlc_log(WLC_LOG_WARN, "Could not set master for drm fd (%d)", wlc.fds[i].fd);
return false;
}
break;
case WLC_FD_INPUT:
case WLC_FD_LAST:
break;
}
}
return wlc_tty_activate();
}
static bool
deactivate(void)
{
// try drop drm fds first before we kill input
for (uint32_t i = 0; i < LENGTH(wlc.fds); ++i) {
if (wlc.fds[i].fd < 0 || wlc.fds[i].type != WLC_FD_DRM)
continue;
if (drmDropMaster(wlc.fds[i].fd)) {
wlc_log(WLC_LOG_WARN, "Could not drop master for drm fd (%d)", wlc.fds[i].fd);
return false;
}
}
for (uint32_t i = 0; i < LENGTH(wlc.fds); ++i) {
if (wlc.fds[i].fd < 0)
continue;
switch (wlc.fds[i].type) {
case WLC_FD_INPUT:
if (ioctl(wlc.fds[i].fd, EVIOCREVOKE, 0) == -1) {
wlc_log(WLC_LOG_WARN, "Kernel does not support EVIOCREVOKE, can not revoke input devices");
return false;
}
close(wlc.fds[i].fd);
wlc.fds[i].fd = -1;
break;
case WLC_FD_DRM:
case WLC_FD_LAST:
break;
}
}
return wlc_tty_deactivate();
}
static void
handle_request(int sock, int fd, const struct msg_request *request)
{
struct msg_response response;
memset(&response, 0, sizeof(response));
response.type = request->type;
switch (request->type) {
case TYPE_CHECK:
write_fd(sock, fd, &response, sizeof(response));
break;
case TYPE_FD_OPEN:
fd = fd_open(request->fd_open.path, request->fd_open.flags, request->fd_open.type);
write_fd(sock, fd, &response, sizeof(response));
break;
case TYPE_FD_CLOSE:
/* we will only close file descriptors opened by us. */
fd_close(request->fd_close.st_dev, request->fd_close.st_ino);
break;
case TYPE_ACTIVATE:
response.activate = activate();
write_fd(sock, fd, &response, sizeof(response));
break;
case TYPE_DEACTIVATE:
response.deactivate = deactivate();
write_fd(sock, fd, &response, sizeof(response));
break;
case TYPE_ACTIVATE_VT:
response.activate = wlc_tty_activate_vt(request->vt_activate.vt);
write_fd(sock, fd, &response, sizeof(response));
}
}
static void
communicate(int sock, pid_t parent)
{
memset(wlc.fds, -1, sizeof(wlc.fds));
do {
int fd = -1;
struct msg_request request;
while (recv_fd(sock, &fd, &request, sizeof(request)) == sizeof(request))
handle_request(sock, fd, &request);
} while (kill(parent, 0) == 0);
// Close all open fds
for (uint32_t i = 0; i < LENGTH(wlc.fds); ++i) {
if (wlc.fds[i].fd < 0)
continue;
if (wlc.fds[i].type == WLC_FD_DRM)
drmDropMaster(wlc.fds[i].fd);
close(wlc.fds[i].fd);
}
wlc_log(WLC_LOG_INFO, "Parent exit (%u)", parent);
wlc_cleanup();
}
static bool
read_response(int sock, int *out_fd, struct msg_response *response, enum msg_type expected_type)
{
if (out_fd)
*out_fd = -1;
memset(response, 0, sizeof(struct msg_response));
fd_set set;
FD_ZERO(&set);
FD_SET(sock, &set);
struct timeval timeout;
memset(&timeout, 0, sizeof(timeout));
timeout.tv_sec = 1;
if (select(sock + 1, &set, NULL, NULL, &timeout) != 1)
return false;
int fd = -1;
ssize_t ret = 0;
do {
ret = recv_fd(sock, &fd, response, sizeof(struct msg_response));
} while (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK));
if (out_fd)
*out_fd = fd;
return (ret == sizeof(struct msg_response) && response->type == expected_type);
}
static void
write_or_die(int sock, int fd, const void *buffer, ssize_t size)
{
ssize_t wrt;
if ((wrt = write_fd(sock, fd, buffer, size)) != size)
die("Failed to write %zi bytes to socket (wrote %zi)", size, wrt);
}
static bool
check_socket(int sock)
{
struct msg_request request;
memset(&request, 0, sizeof(request));
write_or_die(sock, -1, &request, sizeof(request));
struct msg_response response;
return read_response(sock, NULL, &response, TYPE_CHECK);
}
WLC_PURE static void
signal_handler(int signal)
{
if (wlc.has_logind && (signal == SIGTERM || signal == SIGINT))
_exit(EXIT_SUCCESS);
// For non-logind no-op all catched signals
// we might need to restore tty
}
int
wlc_fd_open(const char *path, int flags, enum wlc_fd_type type)
{
#ifdef HAS_LOGIND
if (wlc.has_logind)
return wlc_logind_open(path, flags);
#endif
struct msg_request request;
memset(&request, 0, sizeof(request));
request.type = TYPE_FD_OPEN;
strncpy(request.fd_open.path, path, sizeof(request.fd_open.path));
request.fd_open.flags = flags;
request.fd_open.type = type;
write_or_die(wlc.socket, -1, &request, sizeof(request));
int fd = -1;
struct msg_response response;
if (!read_response(wlc.socket, &fd, &response, TYPE_FD_OPEN))
return -1;
return fd;
}
void
wlc_fd_close(int fd)
{
#ifdef HAS_LOGIND
if (wlc.has_logind) {
wlc_logind_close(fd);
goto close;
}
#endif
struct stat st;
if (fstat(fd, &st) == 0) {
struct msg_request request;
memset(&request, 0, sizeof(request));
request.type = TYPE_FD_CLOSE;
request.fd_close.st_dev = st.st_dev;
request.fd_close.st_ino = st.st_ino;
write_or_die(wlc.socket, -1, &request, sizeof(request));
}
#ifdef HAS_LOGIND
close:
#endif
close(fd);
}
bool
wlc_fd_activate(void)
{
#ifdef HAS_LOGIND
if (wlc.has_logind)
return wlc_tty_activate();
#endif
struct msg_response response;
struct msg_request request;
memset(&request, 0, sizeof(request));
request.type = TYPE_ACTIVATE;
write_or_die(wlc.socket, -1, &request, sizeof(request));
return read_response(wlc.socket, NULL, &response, TYPE_ACTIVATE) && response.activate;
}
bool
wlc_fd_deactivate(void)
{
#ifdef HAS_LOGIND
if (wlc.has_logind)
return wlc_tty_deactivate();
#endif
struct msg_response response;
struct msg_request request;
memset(&request, 0, sizeof(request));
request.type = TYPE_DEACTIVATE;
write_or_die(wlc.socket, -1, &request, sizeof(request));
return read_response(wlc.socket, NULL, &response, TYPE_DEACTIVATE) && response.deactivate;
}
bool
wlc_fd_activate_vt(int vt)
{
#ifdef HAS_LOGIND
if (wlc.has_logind)
return wlc_tty_activate_vt(vt);
#endif
struct msg_response response;
struct msg_request request;
memset(&request, 0, sizeof(request));
request.type = TYPE_ACTIVATE_VT;
request.vt_activate.vt = vt;
write_or_die(wlc.socket, -1, &request, sizeof(request));
return read_response(wlc.socket, NULL, &response, TYPE_ACTIVATE_VT) && response.activate;
}
void
wlc_fd_terminate(void)
{
if (wlc.child >= 0) {
if (wlc.has_logind) {
// XXX: We need fd passer to cleanup sessionless tty
kill(wlc.child, SIGTERM);
}
wlc.child = 0;
}
memset(&wlc, 0, sizeof(wlc));
}
void
wlc_fd_init(bool has_logind)
{
wlc.has_logind = has_logind;
int sock[2];
if (socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sock) != 0)
die("Failed to create fd passing unix domain socket pair: %m");
if ((wlc.child = fork()) == 0) {
close(sock[0]);
if (clearenv() != 0)
die("Failed to clear environment");
struct sigaction action = {
.sa_handler = signal_handler
};
sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
communicate(sock[1], getppid());
_exit(EXIT_SUCCESS);
} else if (wlc.child < 0) {
die("Fork failed");
} else {
close(sock[1]);
if (getuid() != geteuid() || getgid() != getegid())
wlc_log(WLC_LOG_INFO, "Work done, dropping permissions and checking communication");
if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
die("Could not drop permissions: %m");
if (kill(wlc.child, 0) != 0)
die("Child process died");
if (!check_socket(sock[0]))
die("Communication between parent and child process seems to be broken");
wlc.socket = sock[0];
}
}