thread.c 16 KB

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