thread.c 16 KB

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