This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
/
thread.c
728 lines (585 loc) · 17.9 KB
/
thread.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#define __KERNEL__
#include <asm/fcntl.h>
#include <asm/mman.h>
#include <asm/unistd.h>
#include <errno.h>
#include <linux/fcntl.h>
#include <linux/stat.h>
#include "pal.h"
#include "pal_error.h"
#include "shim_fs.h"
#include "shim_handle.h"
#include "shim_internal.h"
#include "shim_lock.h"
#include "shim_process.h"
#include "shim_table.h"
#include "shim_thread.h"
#include "shim_utils.h"
#include "shim_vma.h"
static int parse_thread_name(const char* name, IDTYPE* pidptr, const char** next, size_t* next_len,
const char** nextnext) {
const char* p = name;
IDTYPE pid = 0;
if (*p == '/')
p++;
if (strstartswith(p, "self")) {
p += static_strlen("self");
if (*p && *p != '/')
return -ENOENT;
pid = get_cur_tid();
} else {
for (; *p && *p != '/'; p++) {
if (*p < '0' || *p > '9')
return -ENOENT;
pid = pid * 10 + *p - '0';
}
}
if (next) {
if (*(p++) == '/' && *p) {
*next = p;
if (next_len || nextnext)
for (; *p && *p != '/'; p++)
;
if (next_len)
*next_len = p - *next;
if (nextnext)
*nextnext = (*(p++) == '/' && *p) ? p : NULL;
} else {
*next = NULL;
}
}
if (pidptr)
*pidptr = pid;
return 0;
}
static int find_thread_link(const char* name, struct shim_qstr* link, struct shim_dentry** dentptr) {
const char* next;
const char* nextnext;
size_t next_len;
IDTYPE pid;
int ret = parse_thread_name(name, &pid, &next, &next_len, &nextnext);
if (ret < 0)
return ret;
struct shim_thread* thread = lookup_thread(pid);
struct shim_dentry* dent = NULL;
if (!thread)
return -ENOENT;
/* We just checked that a thread with this id exists and we do not need this handle anymore. */
put_thread(thread);
lock(&g_process.fs_lock);
if (next_len == static_strlen("root") && !memcmp(next, "root", next_len)) {
dent = g_process.root;
get_dentry(dent);
}
if (next_len == static_strlen("cwd") && !memcmp(next, "cwd", next_len)) {
dent = g_process.cwd;
get_dentry(dent);
}
if (next_len == static_strlen("exe") && !memcmp(next, "exe", next_len)) {
struct shim_handle* exec = g_process.exec;
if (!exec->dentry) {
unlock(&g_process.fs_lock);
ret = -EINVAL;
goto out;
}
dent = exec->dentry;
get_dentry(dent);
}
unlock(&g_process.fs_lock);
if (nextnext) {
struct shim_dentry* next_dent = NULL;
ret = path_lookupat(dent, nextnext, 0, &next_dent, dent->fs);
if (ret < 0)
goto out;
put_dentry(dent);
dent = next_dent;
}
if (link && !dentry_get_path_into_qstr(dent, link)) {
ret = -ENOMEM;
goto out;
}
if (dentptr) {
get_dentry(dent);
*dentptr = dent;
}
ret = 0;
out:
if (dent)
put_dentry(dent);
return ret;
}
static int proc_thread_link_open(struct shim_handle* hdl, const char* name, int flags) {
struct shim_dentry* dent;
int ret = find_thread_link(name, NULL, &dent);
if (ret < 0)
return ret;
if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
ret = -EACCES;
goto out;
}
ret = dent->fs->d_ops->open(hdl, dent, flags);
out:
put_dentry(dent);
return 0;
}
static int proc_thread_link_mode(const char* name, mode_t* mode) {
struct shim_dentry* dent;
int ret = find_thread_link(name, NULL, &dent);
if (ret < 0)
return ret;
if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
ret = -EACCES;
goto out;
}
ret = dent->fs->d_ops->mode(dent, mode);
out:
put_dentry(dent);
return ret;
}
static int proc_thread_link_stat(const char* name, struct stat* buf) {
struct shim_dentry* dent;
int ret = find_thread_link(name, NULL, &dent);
if (ret < 0)
return ret;
if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
ret = -EACCES;
goto out;
}
ret = dent->fs->d_ops->stat(dent, buf);
out:
put_dentry(dent);
return ret;
}
static int proc_thread_link_follow_link(const char* name, struct shim_qstr* link) {
return find_thread_link(name, link, NULL);
}
static const struct pseudo_fs_ops fs_thread_link = {
.open = &proc_thread_link_open,
.mode = &proc_thread_link_mode,
.stat = &proc_thread_link_stat,
.follow_link = &proc_thread_link_follow_link,
};
/* If *phdl is returned on success, the ref count is incremented */
static int parse_thread_fd(const char* name, const char** rest, struct shim_handle** phdl) {
const char* next;
const char* nextnext;
size_t next_len;
IDTYPE pid;
int ret = parse_thread_name(name, &pid, &next, &next_len, &nextnext);
if (ret < 0)
return ret;
if (!next || !nextnext || memcmp(next, "fd", next_len))
return -EINVAL;
const char* p = nextnext;
FDTYPE fd = 0;
for (; *p && *p != '/'; p++) {
if (*p < '0' || *p > '9')
return -ENOENT;
fd = fd * 10 + *p - '0';
if ((uint64_t)fd >= get_rlimit_cur(RLIMIT_NOFILE))
return -ENOENT;
}
struct shim_thread* thread = lookup_thread(pid);
if (!thread)
return -ENOENT;
struct shim_handle_map* handle_map = get_thread_handle_map(thread);
lock(&handle_map->lock);
if (fd >= handle_map->fd_top || handle_map->map[fd] == NULL ||
handle_map->map[fd]->handle == NULL) {
ret = -ENOENT;
goto out;
}
if (phdl) {
*phdl = handle_map->map[fd]->handle;
get_handle(*phdl);
}
if (rest)
*rest = *p ? p + 1 : NULL;
ret = 0;
out:
unlock(&handle_map->lock);
put_thread(thread);
return ret;
}
static int proc_match_thread_each_fd(const char* name) {
return parse_thread_fd(name, NULL, NULL) == 0 ? 1 : 0;
}
static int proc_list_thread_each_fd(const char* name, struct shim_dirent** buf, int count) {
const char* next;
size_t next_len;
IDTYPE pid;
int ret = parse_thread_name(name, &pid, &next, &next_len, NULL);
if (ret < 0)
return ret;
if (!next || memcmp(next, "fd", next_len))
return -EINVAL;
struct shim_thread* thread = lookup_thread(pid);
if (!thread)
return -ENOENT;
struct shim_handle_map* handle_map = get_thread_handle_map(thread);
int err = 0, bytes = 0;
struct shim_dirent* dirent = *buf;
struct shim_dirent** last = NULL;
lock(&handle_map->lock);
for (int i = 0; i < handle_map->fd_size; i++)
if (handle_map->map[i] && handle_map->map[i]->handle) {
int d = i, l = 0;
for (; d; d /= 10, l++)
;
l = l ?: 1;
bytes += sizeof(struct shim_dirent) + l + 1;
if (bytes > count) {
err = -ENOMEM;
break;
}
dirent->next = (void*)(dirent + 1) + l + 1;
dirent->ino = 1;
dirent->type = LINUX_DT_LNK;
dirent->name[0] = '0';
dirent->name[l--] = 0;
for (d = i; d; d /= 10) {
dirent->name[l--] = '0' + d % 10;
}
last = &dirent->next;
dirent = dirent->next;
}
unlock(&handle_map->lock);
put_thread(thread);
if (last)
*last = NULL;
*buf = dirent;
return err;
}
static const struct pseudo_name_ops nm_thread_each_fd = {
.match_name = &proc_match_thread_each_fd,
.list_name = &proc_list_thread_each_fd,
};
static int find_thread_each_fd(const char* name, struct shim_qstr* link,
struct shim_dentry** dentptr) {
const char* rest;
struct shim_handle* handle;
struct shim_dentry* dent = NULL;
int ret;
if ((ret = parse_thread_fd(name, &rest, &handle)) < 0)
return ret;
lock(&handle->lock);
if (handle->dentry) {
dent = handle->dentry;
get_dentry(dent);
}
unlock(&handle->lock);
if (!dent) {
ret = -ENOENT;
goto out;
}
if (rest) {
struct shim_dentry* next_dent = NULL;
ret = path_lookupat(dent, rest, 0, &next_dent, dent->fs);
if (ret < 0)
goto out;
put_dentry(dent);
dent = next_dent;
}
if (link && !dentry_get_path_into_qstr(dent, link)) {
ret = -ENOMEM;
goto out;
}
if (dentptr) {
get_dentry(dent);
*dentptr = dent;
}
out:
if (dent)
put_dentry(dent);
put_handle(handle);
return ret;
}
static int proc_thread_each_fd_open(struct shim_handle* hdl, const char* name, int flags) {
struct shim_dentry* dent;
int ret = find_thread_each_fd(name, NULL, &dent);
if (ret < 0)
return ret;
if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
ret = -EACCES;
goto out;
}
ret = dent->fs->d_ops->open(hdl, dent, flags);
out:
put_dentry(dent);
return 0;
}
static int proc_thread_each_fd_mode(const char* name, mode_t* mode) {
struct shim_dentry* dent;
int ret = find_thread_each_fd(name, NULL, &dent);
if (ret < 0)
return ret;
if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
ret = -EACCES;
goto out;
}
ret = dent->fs->d_ops->mode(dent, mode);
out:
put_dentry(dent);
return 0;
}
static int proc_thread_each_fd_stat(const char* name, struct stat* buf) {
struct shim_dentry* dent;
int ret = find_thread_each_fd(name, NULL, &dent);
if (ret < 0)
return ret;
if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
ret = -EACCES;
goto out;
}
ret = dent->fs->d_ops->stat(dent, buf);
out:
put_dentry(dent);
return 0;
}
static int proc_thread_each_fd_follow_link(const char* name, struct shim_qstr* link) {
return find_thread_each_fd(name, link, NULL);
}
static const struct pseudo_fs_ops fs_thread_each_fd = {
.open = &proc_thread_each_fd_open,
.mode = &proc_thread_each_fd_mode,
.stat = &proc_thread_each_fd_stat,
.follow_link = &proc_thread_each_fd_follow_link,
};
static const struct pseudo_dir dir_fd = {
.size = 1,
.ent =
{
{
.name_ops = &nm_thread_each_fd,
.fs_ops = &fs_thread_each_fd,
.type = LINUX_DT_LNK,
},
},
};
static int proc_thread_maps_open(struct shim_handle* hdl, const char* name, int flags) {
if (flags & (O_WRONLY | O_RDWR))
return -EACCES;
const char* next;
size_t next_len;
IDTYPE pid;
char* buffer = NULL;
int ret = parse_thread_name(name, &pid, &next, &next_len, NULL);
if (ret < 0)
return ret;
struct shim_thread* thread = lookup_thread(pid);
if (!thread)
return -ENOENT;
size_t count;
struct shim_vma_info* vmas = NULL;
ret = dump_all_vmas(&vmas, &count, /*include_unmapped=*/false);
if (ret < 0) {
goto err;
}
#define DEFAULT_VMA_BUFFER_SIZE 256
size_t buffer_size = DEFAULT_VMA_BUFFER_SIZE, offset = 0;
buffer = malloc(buffer_size);
if (!buffer) {
ret = -ENOMEM;
goto err;
}
for (struct shim_vma_info* vma = vmas; vma < vmas + count; vma++) {
size_t old_offset = offset;
uintptr_t start = (uintptr_t)vma->addr;
uintptr_t end = (uintptr_t)vma->addr + vma->length;
char pt[3] = {
(vma->prot & PROT_READ) ? 'r' : '-',
(vma->prot & PROT_WRITE) ? 'w' : '-',
(vma->prot & PROT_EXEC) ? 'x' : '-',
};
char pr = (vma->flags & MAP_PRIVATE) ? 'p' : 's';
#define ADDR_FMT(addr) ((addr) > 0xffffffff ? "%lx" : "%08lx")
#define EMIT(fmt...) \
do { \
offset += snprintf(buffer + offset, buffer_size - offset, fmt); \
} while (0)
retry_emit_vma:
if (vma->file) {
int dev_major = 0, dev_minor = 0;
unsigned long ino = vma->file->dentry ? vma->file->dentry->ino : 0;
const char* name = "[unknown]";
if (!qstrempty(&vma->file->path))
name = qstrgetstr(&vma->file->path);
EMIT(ADDR_FMT(start), start);
EMIT("-");
EMIT(ADDR_FMT(end), end);
EMIT(" %c%c%c%c %08lx %02d:%02d %lu %s\n", pt[0], pt[1], pt[2], pr, vma->file_offset,
dev_major, dev_minor, ino, name);
} else {
EMIT(ADDR_FMT(start), start);
EMIT("-");
EMIT(ADDR_FMT(end), end);
if (vma->comment[0])
EMIT(" %c%c%c%c 00000000 00:00 0 %s\n", pt[0], pt[1], pt[2], pr, vma->comment);
else
EMIT(" %c%c%c%c 00000000 00:00 0\n", pt[0], pt[1], pt[2], pr);
}
if (offset >= buffer_size) {
char* new_buffer = malloc(buffer_size * 2);
if (!new_buffer) {
ret = -ENOMEM;
goto err;
}
offset = old_offset;
memcpy(new_buffer, buffer, old_offset);
free(buffer);
buffer = new_buffer;
buffer_size *= 2;
goto retry_emit_vma;
}
}
struct shim_str_data* data = calloc(1, sizeof(struct shim_str_data));
if (!data) {
ret = -ENOMEM;
goto err;
}
data->str = buffer;
data->len = offset;
hdl->type = TYPE_STR;
hdl->flags = flags & ~O_RDONLY;
hdl->acc_mode = MAY_READ;
hdl->info.str.data = data;
ret = 0;
err:
if (ret < 0) {
free(buffer);
}
if (vmas) {
free_vma_info_array(vmas, count);
}
put_thread(thread);
return ret;
}
static int proc_thread_maps_mode(const char* name, mode_t* mode) {
// Only used by one file
__UNUSED(name);
*mode = 0400;
return 0;
}
static int proc_thread_maps_stat(const char* name, struct stat* buf) {
// Only used by one file
__UNUSED(name);
memset(buf, 0, sizeof(struct stat));
buf->st_dev = buf->st_ino = 1;
buf->st_mode = 0400 | S_IFREG;
buf->st_uid = 0;
buf->st_gid = 0;
buf->st_size = 0;
return 0;
}
static const struct pseudo_fs_ops fs_thread_maps = {
.open = &proc_thread_maps_open,
.mode = &proc_thread_maps_mode,
.stat = &proc_thread_maps_stat,
};
static int proc_thread_dir_open(struct shim_handle* hdl, const char* name, int flags) {
__UNUSED(hdl);
__UNUSED(name);
if (flags & (O_WRONLY | O_RDWR))
return -EISDIR;
// Don't really need to do any work here, but keeping as a placeholder,
// just in case.
return 0;
}
static int proc_thread_dir_mode(const char* name, mode_t* mode) {
const char* next;
size_t next_len;
IDTYPE pid;
int ret = parse_thread_name(name, &pid, &next, &next_len, NULL);
if (ret < 0)
return ret;
*mode = 0500;
return 0;
}
static int proc_thread_dir_stat(const char* name, struct stat* buf) {
const char* next;
size_t next_len;
IDTYPE pid;
int ret = parse_thread_name(name, &pid, &next, &next_len, NULL);
if (ret < 0)
return ret;
struct shim_thread* thread = lookup_thread(pid);
if (!thread)
return -ENOENT;
memset(buf, 0, sizeof(struct stat));
buf->st_dev = buf->st_ino = 1;
buf->st_mode = 0500 | S_IFDIR;
lock(&thread->lock);
buf->st_uid = thread->uid;
buf->st_gid = thread->gid;
unlock(&thread->lock);
buf->st_size = 4096;
put_thread(thread);
return 0;
}
static const struct pseudo_fs_ops fs_thread_fd = {
.mode = &proc_thread_dir_mode,
.stat = &proc_thread_dir_stat,
};
static int proc_match_thread(const char* name) {
IDTYPE pid;
if (parse_thread_name(name, &pid, NULL, NULL, NULL) < 0)
return 0;
struct shim_thread* thread = lookup_thread(pid);
if (thread) {
put_thread(thread);
return 1;
}
return 0;
}
struct walk_thread_arg {
struct shim_dirent *buf, *buf_end;
};
static int walk_cb(struct shim_thread* thread, void* arg) {
struct walk_thread_arg* args = (struct walk_thread_arg*)arg;
IDTYPE pid = thread->tid;
int p = pid, l = 0;
for (; p; p /= 10, l++)
;
if ((void*)(args->buf + 1) + l + 1 > (void*)args->buf_end)
return -ENOMEM;
struct shim_dirent* buf = args->buf;
buf->next = (void*)(buf + 1) + l + 1;
buf->ino = 1;
buf->type = LINUX_DT_DIR;
buf->name[l--] = 0;
for (p = pid; p; p /= 10) {
buf->name[l--] = p % 10 + '0';
}
args->buf = buf->next;
return 1;
}
static int proc_list_thread(const char* name, struct shim_dirent** buf, int len) {
__UNUSED(name); // We know this is for "/proc/self"
struct walk_thread_arg args = {
.buf = *buf,
.buf_end = (void*)*buf + len,
};
int ret = walk_thread_list(&walk_cb, &args, /*one_shot=*/false);
if (ret < 0)
return ret;
*buf = args.buf;
return 0;
}
const struct pseudo_name_ops nm_thread = {
.match_name = &proc_match_thread,
.list_name = &proc_list_thread,
};
const struct pseudo_fs_ops fs_thread = {
.open = &proc_thread_dir_open,
.mode = &proc_thread_dir_mode,
.stat = &proc_thread_dir_stat,
};
const struct pseudo_dir dir_thread = {
.size = 5,
.ent = {
{.name = "cwd", .fs_ops = &fs_thread_link, .type = LINUX_DT_LNK},
{.name = "exe", .fs_ops = &fs_thread_link, .type = LINUX_DT_LNK},
{.name = "root", .fs_ops = &fs_thread_link, .type = LINUX_DT_LNK},
{.name = "fd", .fs_ops = &fs_thread_fd, .dir = &dir_fd},
{.name = "maps", .fs_ops = &fs_thread_maps, .type = LINUX_DT_REG},
}};