thread.c 16 KB

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