thread.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <shim_internal.h>
  4. #include <shim_table.h>
  5. #include <shim_thread.h>
  6. #include <shim_handle.h>
  7. #include <shim_fs.h>
  8. #include <shim_utils.h>
  9. #include <pal.h>
  10. #include <pal_error.h>
  11. #include <errno.h>
  12. #include <linux/stat.h>
  13. #include <linux/fcntl.h>
  14. #include <asm/fcntl.h>
  15. #include <asm/mman.h>
  16. #include <asm/unistd.h>
  17. #include <asm/prctl.h>
  18. static int parse_thread_name (const char * name,
  19. const char ** next, int * next_len,
  20. const char ** nextnext)
  21. {
  22. const char * p = name;
  23. int pid = 0;
  24. if (*p == '/')
  25. p++;
  26. if (strpartcmp_static(p, "self")) {
  27. p += static_strlen("self");
  28. if (*p && *p != '/')
  29. return -ENOENT;
  30. pid = get_cur_tid();
  31. } else {
  32. for ( ; *p && *p != '/' ; p++) {
  33. if (*p < '0' || *p > '9')
  34. return -ENOENT;
  35. pid = pid * 10 + *p - '0';
  36. }
  37. }
  38. if (next) {
  39. if (*(p++) == '/' && *p) {
  40. *next = p;
  41. if (next_len || nextnext)
  42. for ( ; *p && *p != '/' ; p++);
  43. if (next_len)
  44. *next_len = p - *next;
  45. if (nextnext)
  46. *nextnext = (*(p++) == '/' && *p) ? p : NULL;
  47. } else {
  48. *next = NULL;
  49. }
  50. }
  51. return pid;
  52. }
  53. static int find_thread_link (const char * name, struct shim_qstr * link,
  54. struct shim_dentry ** dentptr,
  55. struct shim_thread ** threadptr)
  56. {
  57. const char * next, * nextnext;
  58. int next_len;
  59. int pid = parse_thread_name(name, &next, &next_len, &nextnext);
  60. if (pid < 0)
  61. return pid;
  62. struct shim_thread * thread = lookup_thread(pid);
  63. struct shim_dentry * dent = NULL;
  64. int ret = 0;
  65. if (!thread)
  66. return -ENOENT;
  67. if (!thread->in_vm) {
  68. ret = -ENOENT;
  69. goto out;
  70. }
  71. lock(thread->lock);
  72. if (next_len == static_strlen("root") && !memcmp(next, "root", next_len)) {
  73. dent = thread->root;
  74. get_dentry(dent);
  75. }
  76. if (next_len == static_strlen("cwd") && !memcmp(next, "cwd", next_len)) {
  77. dent = thread->cwd;
  78. get_dentry(dent);
  79. }
  80. if (next_len == static_strlen("exe") && !memcmp(next, "exe", next_len)) {
  81. struct shim_handle * exec = thread->exec;
  82. if (!exec->dentry) {
  83. unlock(thread->lock);
  84. ret = -EINVAL;
  85. goto out;
  86. }
  87. dent = exec->dentry;
  88. get_dentry(dent);
  89. }
  90. unlock(thread->lock);
  91. if (nextnext) {
  92. struct shim_dentry * next_dent = NULL;
  93. ret = path_lookupat(dent, nextnext, 0, &next_dent, dent->fs);
  94. if (ret < 0)
  95. goto out;
  96. put_dentry(dent);
  97. dent = next_dent;
  98. }
  99. if (link) {
  100. int size;
  101. char * path = dentry_get_path(dent, true, &size);
  102. qstrsetstr(link, path, size);
  103. }
  104. if (dentptr) {
  105. get_dentry(dent);
  106. *dentptr = dent;
  107. }
  108. if (threadptr) {
  109. get_thread(thread);
  110. *threadptr = thread;
  111. }
  112. ret = 0;
  113. out:
  114. if (dent)
  115. put_dentry(dent);
  116. if (thread)
  117. put_thread(thread);
  118. return ret;
  119. }
  120. static int proc_thread_link_open (struct shim_handle * hdl,
  121. const char * name, int flags)
  122. {
  123. struct shim_dentry * dent;
  124. int ret = find_thread_link(name, NULL, &dent, NULL);
  125. if (ret < 0)
  126. return ret;
  127. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  128. ret = -EACCES;
  129. goto out;
  130. }
  131. ret = dent->fs->d_ops->open(hdl, dent, flags);
  132. out:
  133. put_dentry(dent);
  134. return 0;
  135. }
  136. static int proc_thread_link_mode (const char * name, mode_t * mode)
  137. {
  138. struct shim_dentry * dent;
  139. int ret = find_thread_link(name, NULL, &dent, NULL);
  140. if (ret < 0)
  141. return ret;
  142. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
  143. ret = -EACCES;
  144. goto out;
  145. }
  146. ret = dent->fs->d_ops->mode(dent, mode, true);
  147. out:
  148. put_dentry(dent);
  149. return ret;
  150. }
  151. static int proc_thread_link_stat (const char * name, struct stat * buf)
  152. {
  153. struct shim_dentry * dent;
  154. int ret = find_thread_link(name, NULL, &dent, NULL);
  155. if (ret < 0)
  156. return ret;
  157. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  158. ret = -EACCES;
  159. goto out;
  160. }
  161. ret = dent->fs->d_ops->stat(dent, buf);
  162. out:
  163. put_dentry(dent);
  164. return ret;
  165. }
  166. static int proc_thread_link_follow_link (const char * name,
  167. struct shim_qstr * link)
  168. {
  169. return find_thread_link(name, link, NULL, NULL);
  170. }
  171. static const struct proc_fs_ops fs_thread_link = {
  172. .open = &proc_thread_link_open,
  173. .mode = &proc_thread_link_mode,
  174. .stat = &proc_thread_link_stat,
  175. .follow_link = &proc_thread_link_follow_link,
  176. };
  177. /* If *phdl is returned on success, the ref count is incremented */
  178. static int parse_thread_fd (const char * name, const char ** rest,
  179. struct shim_handle ** phdl)
  180. {
  181. const char * next, * nextnext;
  182. int next_len;
  183. int pid = parse_thread_name(name, &next, &next_len, &nextnext);
  184. if (!pid)
  185. return pid;
  186. if (!next || !nextnext || memcmp(next, "fd", next_len))
  187. return -EINVAL;
  188. const char * p = nextnext;
  189. int fd = 0;
  190. for ( ; *p && *p != '/' ; p++) {
  191. if (*p < '0' || *p > '9')
  192. return -ENOENT;
  193. fd = fd * 10 + *p - '0';
  194. if (fd >= max_fds)
  195. return -ENOENT;
  196. }
  197. struct shim_thread * thread = lookup_thread(pid);
  198. if (!thread)
  199. return -ENOENT;
  200. struct shim_handle_map * handle_map = get_cur_handle_map(thread);
  201. lock(handle_map->lock);
  202. if (fd >= handle_map->fd_top ||
  203. handle_map->map[fd] == NULL ||
  204. handle_map->map[fd]->handle == NULL) {
  205. unlock(handle_map->lock);
  206. return -ENOENT;
  207. }
  208. if (phdl) {
  209. *phdl = handle_map->map[fd]->handle;
  210. get_handle(*phdl);
  211. }
  212. unlock(handle_map->lock);
  213. if (rest)
  214. *rest = *p ? p + 1 : NULL;
  215. return 0;
  216. }
  217. static int proc_match_thread_each_fd (const char * name)
  218. {
  219. return parse_thread_fd(name, NULL, NULL) == 0 ? 1 : 0;
  220. }
  221. static int proc_list_thread_each_fd (const char * name,
  222. struct shim_dirent ** buf, int count)
  223. {
  224. const char * next;
  225. int next_len;
  226. int pid = parse_thread_name(name, &next, &next_len, NULL);
  227. if (!pid)
  228. return pid;
  229. if (!next || memcmp(next, "fd", next_len))
  230. return -EINVAL;
  231. struct shim_thread * thread = lookup_thread(pid);
  232. if (!thread)
  233. return -ENOENT;
  234. struct shim_handle_map * handle_map = get_cur_handle_map(thread);
  235. int err = 0, bytes = 0;
  236. struct shim_dirent * dirent = *buf, ** last = NULL;
  237. lock(handle_map->lock);
  238. for (int i = 0 ; i < handle_map->fd_size ; i++)
  239. if (handle_map->map[i] &&
  240. handle_map->map[i]->handle) {
  241. int d = i, l = 0;
  242. for ( ; d ; d /= 10, l++);
  243. l = l ? : 1;
  244. bytes += sizeof(struct shim_dirent) + l + 1;
  245. if (bytes > count) {
  246. err = -ENOMEM;
  247. break;
  248. }
  249. dirent->next = (void *) (dirent + 1) + l + 1;
  250. dirent->ino = 1;
  251. dirent->type = LINUX_DT_LNK;
  252. dirent->name[0] = '0';
  253. dirent->name[l--] = 0;
  254. for (d = i ; d ; d /= 10)
  255. dirent->name[l--] = '0' + d % 10;
  256. last = &dirent->next;
  257. dirent = dirent->next;
  258. }
  259. unlock(handle_map->lock);
  260. put_thread(thread);
  261. if (last)
  262. *last = NULL;
  263. *buf = dirent;
  264. return err;
  265. }
  266. static const struct proc_nm_ops nm_thread_each_fd = {
  267. .match_name = &proc_match_thread_each_fd,
  268. .list_name = &proc_list_thread_each_fd,
  269. };
  270. static int find_thread_each_fd (const char * name, struct shim_qstr * link,
  271. struct shim_dentry ** dentptr)
  272. {
  273. const char * rest;
  274. struct shim_handle * handle;
  275. struct shim_dentry * dent = NULL;
  276. int ret;
  277. if ((ret = parse_thread_fd(name, &rest, &handle)) < 0)
  278. return ret;
  279. lock(handle->lock);
  280. if (handle->dentry) {
  281. dent = handle->dentry;
  282. get_dentry(dent);
  283. }
  284. unlock(handle->lock);
  285. if (!dent) {
  286. ret = -ENOENT;
  287. goto out;
  288. }
  289. if (rest) {
  290. struct shim_dentry * next_dent = NULL;
  291. ret = path_lookupat(dent, rest, 0, &next_dent, dent->fs);
  292. if (ret < 0)
  293. goto out;
  294. put_dentry(dent);
  295. dent = next_dent;
  296. }
  297. if (link) {
  298. int size;
  299. char * path = dentry_get_path(dent, true, &size);
  300. qstrsetstr(link, path, size);
  301. }
  302. if (dentptr) {
  303. get_dentry(dent);
  304. *dentptr = dent;
  305. }
  306. out:
  307. if (dent)
  308. put_dentry(dent);
  309. put_handle(handle);
  310. return ret;
  311. }
  312. static int proc_thread_each_fd_open (struct shim_handle * hdl,
  313. const char * name, int flags)
  314. {
  315. struct shim_dentry * dent;
  316. int ret = find_thread_each_fd(name, NULL, &dent);
  317. if (ret < 0)
  318. return ret;
  319. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  320. ret = -EACCES;
  321. goto out;
  322. }
  323. ret = dent->fs->d_ops->open(hdl, dent, flags);
  324. out:
  325. put_dentry(dent);
  326. return 0;
  327. }
  328. static int proc_thread_each_fd_mode (const char * name, mode_t * mode)
  329. {
  330. struct shim_dentry * dent;
  331. int ret = find_thread_each_fd(name, NULL, &dent);
  332. if (ret < 0)
  333. return ret;
  334. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
  335. ret = -EACCES;
  336. goto out;
  337. }
  338. ret = dent->fs->d_ops->mode(dent, mode, true);
  339. out:
  340. put_dentry(dent);
  341. return 0;
  342. }
  343. static int proc_thread_each_fd_stat (const char * name, struct stat * buf)
  344. {
  345. struct shim_dentry * dent;
  346. int ret = find_thread_each_fd(name, NULL, &dent);
  347. if (ret < 0)
  348. return ret;
  349. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  350. ret = -EACCES;
  351. goto out;
  352. }
  353. ret = dent->fs->d_ops->stat(dent, buf);
  354. out:
  355. put_dentry(dent);
  356. return 0;
  357. }
  358. static int proc_thread_each_fd_follow_link (const char * name,
  359. struct shim_qstr * link)
  360. {
  361. return find_thread_each_fd(name, link, NULL);
  362. }
  363. static const struct proc_fs_ops fs_thread_each_fd = {
  364. .open = &proc_thread_each_fd_open,
  365. .mode = &proc_thread_each_fd_mode,
  366. .stat = &proc_thread_each_fd_stat,
  367. .follow_link = &proc_thread_each_fd_follow_link,
  368. };
  369. static const struct proc_dir dir_fd = { .size = 1, .ent = { {
  370. .nm_ops = &nm_thread_each_fd, .fs_ops = &fs_thread_each_fd,
  371. }, }, };
  372. static int proc_thread_maps_open (struct shim_handle * hdl,
  373. const char * name, int flags)
  374. {
  375. if (flags & (O_WRONLY|O_RDWR))
  376. return -EACCES;
  377. const char * next;
  378. int next_len;
  379. int pid = parse_thread_name(name, &next, &next_len, NULL);
  380. int ret = 0;
  381. if (pid < 0)
  382. return pid;
  383. struct shim_thread * thread = lookup_thread(pid);
  384. if (!thread)
  385. return -ENOENT;
  386. size_t count = DEFAULT_VMA_COUNT;
  387. struct shim_vma_val * vmas = malloc(sizeof(struct shim_vma_val) * count);
  388. if (!vmas) {
  389. ret = -ENOMEM;
  390. goto out;
  391. }
  392. retry_dump_vmas:
  393. ret = dump_all_vmas(vmas, count);
  394. if (ret == -EOVERFLOW) {
  395. struct shim_vma_val * new_vmas
  396. = malloc(sizeof(struct shim_vma_val) * count * 2);
  397. if (!new_vmas) {
  398. ret = -ENOMEM;
  399. goto err;
  400. }
  401. free(vmas);
  402. vmas = new_vmas;
  403. count *= 2;
  404. goto retry_dump_vmas;
  405. }
  406. if (ret < 0)
  407. goto err;
  408. #define DEFAULT_VMA_BUFFER_SIZE 256
  409. count = ret;
  410. size_t buffer_size = DEFAULT_VMA_BUFFER_SIZE, offset = 0;
  411. char * buffer = malloc(buffer_size);
  412. if (!buffer) {
  413. ret = -ENOMEM;
  414. goto err;
  415. }
  416. for (struct shim_vma_val * vma = vmas ; vma < vmas + count ; vma++) {
  417. size_t old_offset = offset;
  418. uint64_t start = (uint64_t) vma->addr;
  419. uint64_t end = (uint64_t) vma->addr + vma->length;
  420. char pt[3] = {
  421. (vma->prot & PROT_READ) ? 'r' : '-',
  422. (vma->prot & PROT_WRITE) ? 'w' : '-',
  423. (vma->prot & PROT_EXEC) ? 'x' : '-',
  424. };
  425. char pr = (vma->flags & MAP_PRIVATE) ? 'p' : 's';
  426. #define ADDR_FMT(addr) ((addr) > 0xffffffff ? "%lx" : "%08x")
  427. #define EMIT(fmt ...) \
  428. do { \
  429. offset += snprintf(buffer + offset, buffer_size - offset, \
  430. fmt); \
  431. } while (0)
  432. retry_emit_vma:
  433. if (vma->file) {
  434. int dev_major = 0, dev_minor = 0;
  435. unsigned long ino = vma->file->dentry ? vma->file->dentry->ino : 0;
  436. const char * name = "[unknown]";
  437. if (!qstrempty(&vma->file->path))
  438. name = qstrgetstr(&vma->file->path);
  439. EMIT(ADDR_FMT(start), start);
  440. EMIT("-");
  441. EMIT(ADDR_FMT(end), end);
  442. EMIT(" %c%c%c%c %08lx %02d:%02d %u %s\n", pt[0], pt[1], pt[2], pr,
  443. vma->offset, dev_major, dev_minor, ino, name);
  444. } else {
  445. EMIT(ADDR_FMT(start), start);
  446. EMIT("-");
  447. EMIT(ADDR_FMT(end), end);
  448. if (vma->comment[0])
  449. EMIT(" %c%c%c%c 00000000 00:00 0 %s\n", pt[0], pt[1], pt[2], pr,
  450. vma->comment);
  451. else
  452. EMIT(" %c%c%c%c 00000000 00:00 0\n", pt[0], pt[1], pt[2], pr);
  453. }
  454. if (offset >= buffer_size) {
  455. char * new_buffer = malloc(buffer_size * 2);
  456. if (!new_buffer) {
  457. ret = -ENOMEM;
  458. goto err;
  459. }
  460. offset = old_offset;
  461. memcpy(new_buffer, buffer, old_offset);
  462. free(buffer);
  463. buffer = new_buffer;
  464. buffer_size *= 2;
  465. goto retry_emit_vma;
  466. }
  467. }
  468. struct shim_str_data * data = calloc(1, sizeof(struct shim_str_data));
  469. if (!data) {
  470. ret = -ENOMEM;
  471. goto err;
  472. }
  473. data->str = buffer;
  474. data->len = offset;
  475. hdl->type = TYPE_STR;
  476. hdl->flags = flags & ~O_RDONLY;
  477. hdl->acc_mode = MAY_READ;
  478. hdl->info.str.data = data;
  479. ret = 0;
  480. out:
  481. put_thread(thread);
  482. if (vmas)
  483. free_vma_val_array(vmas, count);
  484. return ret;
  485. err:
  486. if (buffer)
  487. free(buffer);
  488. goto out;
  489. }
  490. static int proc_thread_maps_mode (const char * name, mode_t * mode)
  491. {
  492. *mode = 0400;
  493. return 0;
  494. }
  495. static int proc_thread_maps_stat (const char * name, struct stat * buf)
  496. {
  497. memset(buf, 0, sizeof(struct stat));
  498. buf->st_dev = buf->st_ino = 1;
  499. buf->st_mode = 0400|S_IFREG;
  500. buf->st_uid = 0;
  501. buf->st_gid = 0;
  502. buf->st_size = 0;
  503. return 0;
  504. }
  505. static const struct proc_fs_ops fs_thread_maps = {
  506. .open = &proc_thread_maps_open,
  507. .mode = &proc_thread_maps_mode,
  508. .stat = &proc_thread_maps_stat,
  509. };
  510. static int proc_thread_dir_mode (const char * name, mode_t * mode)
  511. {
  512. const char * next;
  513. int next_len;
  514. int pid = parse_thread_name(name, &next, &next_len, NULL);
  515. if (pid < 0)
  516. return pid;
  517. *mode = 0500;
  518. return 0;
  519. }
  520. static int proc_thread_dir_stat (const char * name, struct stat * buf)
  521. {
  522. const char * next;
  523. int next_len;
  524. int pid = parse_thread_name(name, &next, &next_len, NULL);
  525. if (pid < 0)
  526. return pid;
  527. struct shim_thread * thread = lookup_thread(pid);
  528. if (!thread)
  529. return -ENOENT;
  530. memset(buf, 0, sizeof(struct stat));
  531. buf->st_dev = buf->st_ino = 1;
  532. buf->st_mode = 0500|S_IFDIR;
  533. lock(thread->lock);
  534. buf->st_uid = thread->uid;
  535. buf->st_gid = thread->gid;
  536. unlock(thread->lock);
  537. buf->st_size = 4096;
  538. return 0;
  539. }
  540. static const struct proc_fs_ops fs_thread_fd = {
  541. .mode = &proc_thread_dir_mode,
  542. .stat = &proc_thread_dir_stat,
  543. };
  544. static int proc_match_thread (const char * name)
  545. {
  546. int pid = parse_thread_name(name, NULL, NULL, NULL);
  547. if (pid < 0)
  548. return 0;
  549. struct shim_thread * thread = lookup_thread(pid);
  550. return thread ? 1 : 0;
  551. }
  552. struct walk_thread_arg {
  553. struct shim_dirent * buf, * buf_end;
  554. };
  555. static int walk_cb (struct shim_thread * thread, void * arg, bool * unlocked)
  556. {
  557. struct walk_thread_arg * args = (struct walk_thread_arg *) arg;
  558. IDTYPE pid = thread->tid;
  559. int p = pid, l = 0;
  560. for ( ; p ; p /= 10, l++);
  561. if ((void *) (args->buf + 1) + l + 1 > (void *) args->buf_end)
  562. return -ENOBUFS;
  563. struct shim_dirent * buf = args->buf;
  564. buf->next = (void *) (buf + 1) + l + 1;
  565. buf->ino = 1;
  566. buf->type = LINUX_DT_DIR;
  567. buf->name[l--] = 0;
  568. for (p = pid ; p ; p /= 10)
  569. buf->name[l--] = p % 10 + '0';
  570. args->buf = buf->next;
  571. return 1;
  572. }
  573. static int proc_list_thread (const char * name, struct shim_dirent ** buf,
  574. int len)
  575. {
  576. struct walk_thread_arg args =
  577. { .buf = *buf, .buf_end = (void *) *buf + len, };
  578. int ret = walk_thread_list(&walk_cb, &args, false);
  579. if (ret < 0)
  580. return ret;
  581. *buf = args.buf;
  582. return 0;
  583. }
  584. const struct proc_nm_ops nm_thread = {
  585. .match_name = &proc_match_thread,
  586. .list_name = &proc_list_thread,
  587. };
  588. const struct proc_fs_ops fs_thread = {
  589. .mode = &proc_thread_dir_mode,
  590. .stat = &proc_thread_dir_stat,
  591. };
  592. const struct proc_dir dir_thread = { .size = 5, .ent = {
  593. { .name = "cwd", .fs_ops = &fs_thread_link, },
  594. { .name = "exe", .fs_ops = &fs_thread_link, },
  595. { .name = "root", .fs_ops = &fs_thread_link, },
  596. { .name = "fd", .dir = &dir_fd, .fs_ops = &fs_thread_fd, },
  597. { .name = "maps", .fs_ops = &fs_thread_maps, },
  598. }, };