thread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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);
  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. 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. unlock(handle_map->lock);
  211. if (rest)
  212. *rest = *p ? p + 1 : NULL;
  213. return 0;
  214. }
  215. static int proc_match_thread_each_fd (const char * name)
  216. {
  217. return parse_thread_fd(name, NULL, NULL) == 0 ? 1 : 0;
  218. }
  219. static int proc_list_thread_each_fd (const char * name,
  220. struct shim_dirent ** buf, int count)
  221. {
  222. const char * next;
  223. int next_len;
  224. int pid = parse_thread_name(name, &next, &next_len, NULL);
  225. if (!pid)
  226. return pid;
  227. if (!next || memcmp(next, "fd", next_len))
  228. return -EINVAL;
  229. struct shim_thread * thread = lookup_thread(pid);
  230. if (!thread)
  231. return -ENOENT;
  232. struct shim_handle_map * handle_map = get_cur_handle_map(thread);
  233. int err = 0, bytes = 0;
  234. struct shim_dirent * dirent = *buf, ** last = NULL;
  235. lock(handle_map->lock);
  236. for (int i = 0 ; i < handle_map->fd_size ; i++)
  237. if (handle_map->map[i] &&
  238. handle_map->map[i]->handle) {
  239. int d = i, l = 0;
  240. for ( ; d ; d /= 10, l++);
  241. l = l ? : 1;
  242. bytes += sizeof(struct shim_dirent) + l + 1;
  243. if (bytes > count) {
  244. err = -ENOMEM;
  245. break;
  246. }
  247. dirent->next = (void *) (dirent + 1) + l + 1;
  248. dirent->ino = 1;
  249. dirent->type = LINUX_DT_LNK;
  250. dirent->name[0] = '0';
  251. dirent->name[l--] = 0;
  252. for (d = i ; d ; d /= 10)
  253. dirent->name[l--] = '0' + d % 10;
  254. last = &dirent->next;
  255. dirent = dirent->next;
  256. }
  257. unlock(handle_map->lock);
  258. put_thread(thread);
  259. if (last)
  260. *last = NULL;
  261. *buf = dirent;
  262. return err;
  263. }
  264. static const struct proc_nm_ops nm_thread_each_fd = {
  265. .match_name = &proc_match_thread_each_fd,
  266. .list_name = &proc_list_thread_each_fd,
  267. };
  268. static int find_thread_each_fd (const char * name, struct shim_qstr * link,
  269. struct shim_dentry ** dentptr)
  270. {
  271. const char * rest;
  272. struct shim_handle * handle;
  273. struct shim_dentry * dent = NULL;
  274. int ret;
  275. if ((ret = parse_thread_fd(name, &rest, &handle)) < 0)
  276. return ret;
  277. lock(handle->lock);
  278. if (handle->dentry) {
  279. dent = handle->dentry;
  280. get_dentry(dent);
  281. }
  282. unlock(handle->lock);
  283. if (!dent) {
  284. ret = -ENOENT;
  285. goto out;
  286. }
  287. if (rest) {
  288. struct shim_dentry * next_dent = NULL;
  289. ret = path_lookupat(dent, rest, 0, &next_dent);
  290. if (ret < 0)
  291. goto out;
  292. put_dentry(dent);
  293. dent = next_dent;
  294. }
  295. if (link) {
  296. int size;
  297. char * path = dentry_get_path(dent, true, &size);
  298. qstrsetstr(link, path, size);
  299. }
  300. if (dentptr) {
  301. get_dentry(dent);
  302. *dentptr = dent;
  303. }
  304. out:
  305. if (dent)
  306. put_dentry(dent);
  307. put_handle(handle);
  308. return ret;
  309. }
  310. static int proc_thread_each_fd_open (struct shim_handle * hdl,
  311. const char * name, int flags)
  312. {
  313. struct shim_dentry * dent;
  314. int ret = find_thread_each_fd(name, NULL, &dent);
  315. if (ret < 0)
  316. return ret;
  317. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  318. ret = -EACCES;
  319. goto out;
  320. }
  321. ret = dent->fs->d_ops->open(hdl, dent, flags);
  322. out:
  323. put_dentry(dent);
  324. return 0;
  325. }
  326. static int proc_thread_each_fd_mode (const char * name, mode_t * mode)
  327. {
  328. struct shim_dentry * dent;
  329. int ret = find_thread_each_fd(name, NULL, &dent);
  330. if (ret < 0)
  331. return ret;
  332. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
  333. ret = -EACCES;
  334. goto out;
  335. }
  336. ret = dent->fs->d_ops->mode(dent, mode, true);
  337. out:
  338. put_dentry(dent);
  339. return 0;
  340. }
  341. static int proc_thread_each_fd_stat (const char * name, struct stat * buf)
  342. {
  343. struct shim_dentry * dent;
  344. int ret = find_thread_each_fd(name, NULL, &dent);
  345. if (ret < 0)
  346. return ret;
  347. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  348. ret = -EACCES;
  349. goto out;
  350. }
  351. ret = dent->fs->d_ops->stat(dent, buf);
  352. out:
  353. put_dentry(dent);
  354. return 0;
  355. }
  356. static int proc_thread_each_fd_follow_link (const char * name,
  357. struct shim_qstr * link)
  358. {
  359. return find_thread_each_fd(name, link, NULL);
  360. }
  361. static const struct proc_fs_ops fs_thread_each_fd = {
  362. .open = &proc_thread_each_fd_open,
  363. .mode = &proc_thread_each_fd_mode,
  364. .stat = &proc_thread_each_fd_stat,
  365. .follow_link = &proc_thread_each_fd_follow_link,
  366. };
  367. static const struct proc_dir dir_fd = { .size = 1, .ent = { {
  368. .nm_ops = &nm_thread_each_fd, .fs_ops = &fs_thread_each_fd,
  369. }, }, };
  370. static int proc_thread_maps_open (struct shim_handle * hdl,
  371. const char * name, int flags)
  372. {
  373. if (flags & (O_WRONLY|O_RDWR))
  374. return -EACCES;
  375. const char * next;
  376. int next_len;
  377. int pid = parse_thread_name(name, &next, &next_len, NULL);
  378. if (pid < 0)
  379. return pid;
  380. struct shim_thread * thread = lookup_thread(pid);
  381. if (!thread)
  382. return -ENOENT;
  383. int size = DEFAULT_BUFFER_SIZE;
  384. char * strbuf = malloc(size);
  385. int ret = 0, len = 0;
  386. if (!strbuf) {
  387. ret = -ENOMEM;
  388. goto out;
  389. }
  390. retry:
  391. ret = dump_all_vmas(thread, strbuf, size);
  392. if (ret == -EOVERFLOW) {
  393. char * newbuf = malloc(size * 2);
  394. if (!newbuf) {
  395. ret = -ENOMEM;
  396. goto err;
  397. }
  398. free(strbuf);
  399. strbuf = newbuf;
  400. size *= 2;
  401. goto retry;
  402. }
  403. if (ret < 0)
  404. goto err;
  405. len = ret;
  406. struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
  407. if (!data) {
  408. ret = -ENOMEM;
  409. goto err;
  410. }
  411. memset(data, 0, sizeof(struct shim_str_data));
  412. data->str = strbuf;
  413. data->len = len;
  414. hdl->type = TYPE_STR;
  415. hdl->flags = flags & ~O_RDONLY;
  416. hdl->acc_mode = MAY_READ;
  417. hdl->info.str.data = data;
  418. ret = 0;
  419. out:
  420. put_thread(thread);
  421. return ret;
  422. err:
  423. free(strbuf);
  424. goto out;
  425. }
  426. static int proc_thread_maps_mode (const char * name, mode_t * mode)
  427. {
  428. *mode = 0400;
  429. return 0;
  430. }
  431. static int proc_thread_maps_stat (const char * name, struct stat * buf)
  432. {
  433. memset(buf, 0, sizeof(struct stat));
  434. buf->st_dev = buf->st_ino = 1;
  435. buf->st_mode = 0400|S_IFREG;
  436. buf->st_uid = 0;
  437. buf->st_gid = 0;
  438. buf->st_size = 0;
  439. return 0;
  440. }
  441. static const struct proc_fs_ops fs_thread_maps = {
  442. .open = &proc_thread_maps_open,
  443. .mode = &proc_thread_maps_mode,
  444. .stat = &proc_thread_maps_stat,
  445. };
  446. static int proc_thread_dir_mode (const char * name, mode_t * mode)
  447. {
  448. const char * next;
  449. int next_len;
  450. int pid = parse_thread_name(name, &next, &next_len, NULL);
  451. if (pid < 0)
  452. return pid;
  453. *mode = 0500;
  454. return 0;
  455. }
  456. static int proc_thread_dir_stat (const char * name, struct stat * buf)
  457. {
  458. const char * next;
  459. int next_len;
  460. int pid = parse_thread_name(name, &next, &next_len, NULL);
  461. if (pid < 0)
  462. return pid;
  463. struct shim_thread * thread = lookup_thread(pid);
  464. if (!thread)
  465. return -ENOENT;
  466. memset(buf, 0, sizeof(struct stat));
  467. buf->st_dev = buf->st_ino = 1;
  468. buf->st_mode = 0500|S_IFDIR;
  469. lock(thread->lock);
  470. buf->st_uid = thread->uid;
  471. buf->st_gid = thread->gid;
  472. unlock(thread->lock);
  473. buf->st_size = 4096;
  474. return 0;
  475. }
  476. static const struct proc_fs_ops fs_thread_fd = {
  477. .mode = &proc_thread_dir_mode,
  478. .stat = &proc_thread_dir_stat,
  479. };
  480. static int proc_match_thread (const char * name)
  481. {
  482. int pid = parse_thread_name(name, NULL, NULL, NULL);
  483. if (pid < 0)
  484. return 0;
  485. struct shim_thread * thread = lookup_thread(pid);
  486. return thread ? 1 : 0;
  487. }
  488. struct walk_thread_arg {
  489. struct shim_dirent * buf, * buf_end;
  490. };
  491. static int walk_cb (struct shim_thread * thread, void * arg, bool * unlocked)
  492. {
  493. struct walk_thread_arg * args = (struct walk_thread_arg *) arg;
  494. IDTYPE pid = thread->tid;
  495. int p = pid, l = 0;
  496. for ( ; p ; p /= 10, l++);
  497. if ((void *) (args->buf + 1) + l + 1 > (void *) args->buf_end)
  498. return -ENOBUFS;
  499. struct shim_dirent * buf = args->buf;
  500. buf->next = (void *) (buf + 1) + l + 1;
  501. buf->ino = 1;
  502. buf->type = LINUX_DT_DIR;
  503. buf->name[l--] = 0;
  504. for (p = pid ; p ; p /= 10)
  505. buf->name[l--] = p % 10 + '0';
  506. args->buf = buf->next;
  507. return 1;
  508. }
  509. static int proc_list_thread (const char * name, struct shim_dirent ** buf,
  510. int len)
  511. {
  512. struct walk_thread_arg args =
  513. { .buf = *buf, .buf_end = (void *) *buf + len, };
  514. int ret = walk_thread_list(&walk_cb, &args, false);
  515. if (ret < 0)
  516. return ret;
  517. *buf = args.buf;
  518. return 0;
  519. }
  520. const struct proc_nm_ops nm_thread = {
  521. .match_name = &proc_match_thread,
  522. .list_name = &proc_list_thread,
  523. };
  524. const struct proc_fs_ops fs_thread = {
  525. .mode = &proc_thread_dir_mode,
  526. .stat = &proc_thread_dir_stat,
  527. };
  528. const struct proc_dir dir_thread = { .size = 5, .ent = {
  529. { .name = "cwd", .fs_ops = &fs_thread_link, },
  530. { .name = "exe", .fs_ops = &fs_thread_link, },
  531. { .name = "root", .fs_ops = &fs_thread_link, },
  532. { .name = "fd", .dir = &dir_fd, .fs_ops = &fs_thread_fd, },
  533. { .name = "maps", .fs_ops = &fs_thread_maps, },
  534. }, };