qrexec-agent-data.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * The Qubes OS Project, http://www.qubes-os.org
  3. *
  4. * Copyright (C) 2013 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <signal.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <sys/wait.h>
  30. #include <sys/select.h>
  31. #include <sys/socket.h>
  32. #include <fcntl.h>
  33. #include <libvchan.h>
  34. #include <assert.h>
  35. #include "qrexec.h"
  36. #include "libqrexec-utils.h"
  37. #include "qrexec-agent.h"
  38. #define VCHAN_BUFFER_SIZE 65536
  39. static volatile int child_exited;
  40. static volatile int stdio_socket_requested;
  41. int stdout_msg_type = MSG_DATA_STDOUT;
  42. pid_t child_process_pid;
  43. int remote_process_status = 0;
  44. static void sigchld_handler(int __attribute__((__unused__))x)
  45. {
  46. child_exited = 1;
  47. }
  48. static void sigusr1_handler(int __attribute__((__unused__))x)
  49. {
  50. stdio_socket_requested = 1;
  51. signal(SIGUSR1, SIG_IGN);
  52. }
  53. void prepare_child_env() {
  54. char pid_s[10];
  55. signal(SIGCHLD, sigchld_handler);
  56. signal(SIGUSR1, sigusr1_handler);
  57. snprintf(pid_s, sizeof(pid_s), "%d", getpid());
  58. setenv("QREXEC_AGENT_PID", pid_s, 1);
  59. }
  60. int handle_handshake(libvchan_t *ctrl)
  61. {
  62. struct msg_header hdr;
  63. struct peer_info info;
  64. int actual_version;
  65. /* send own HELLO */
  66. hdr.type = MSG_HELLO;
  67. hdr.len = sizeof(info);
  68. info.version = QREXEC_PROTOCOL_VERSION;
  69. if (libvchan_send(ctrl, &hdr, sizeof(hdr)) != sizeof(hdr)) {
  70. fprintf(stderr, "Failed to send HELLO hdr to agent\n");
  71. return -1;
  72. }
  73. if (libvchan_send(ctrl, &info, sizeof(info)) != sizeof(info)) {
  74. fprintf(stderr, "Failed to send HELLO hdr to agent\n");
  75. return -1;
  76. }
  77. /* receive MSG_HELLO from remote */
  78. if (libvchan_recv(ctrl, &hdr, sizeof(hdr)) != sizeof(hdr)) {
  79. fprintf(stderr, "Failed to read agent HELLO hdr\n");
  80. return -1;
  81. }
  82. if (hdr.type != MSG_HELLO || hdr.len != sizeof(info)) {
  83. fprintf(stderr, "Invalid HELLO packet received: type %d, len %d\n", hdr.type, hdr.len);
  84. return -1;
  85. }
  86. if (libvchan_recv(ctrl, &info, sizeof(info)) != sizeof(info)) {
  87. fprintf(stderr, "Failed to read agent HELLO body\n");
  88. return -1;
  89. }
  90. actual_version = info.version < QREXEC_PROTOCOL_VERSION ? info.version : QREXEC_PROTOCOL_VERSION;
  91. if (actual_version != QREXEC_PROTOCOL_VERSION) {
  92. fprintf(stderr, "Incompatible agent protocol version (remote %d, local %d)\n", info.version, QREXEC_PROTOCOL_VERSION);
  93. return -1;
  94. }
  95. return actual_version;
  96. }
  97. int handle_just_exec(char *cmdline)
  98. {
  99. int fdn, pid;
  100. switch (pid = fork()) {
  101. case -1:
  102. perror("fork");
  103. return -1;
  104. case 0:
  105. fdn = open("/dev/null", O_RDWR);
  106. fix_fds(fdn, fdn, fdn);
  107. do_exec(cmdline);
  108. perror("execl");
  109. exit(1);
  110. default:;
  111. }
  112. fprintf(stderr, "executed (nowait) %s pid %d\n", cmdline, pid);
  113. return 0;
  114. }
  115. void send_exit_code(libvchan_t *data_vchan, int status)
  116. {
  117. struct msg_header hdr;
  118. hdr.type = MSG_DATA_EXIT_CODE;
  119. hdr.len = sizeof(status);
  120. if (libvchan_send(data_vchan, &hdr, sizeof(hdr)) < 0)
  121. handle_vchan_error("write hdr");
  122. if (libvchan_send(data_vchan, &status, sizeof(status)) < 0)
  123. handle_vchan_error("write status");
  124. fprintf(stderr, "send exit code %d\n", status);
  125. }
  126. /* handle data from specified FD and send over vchan link
  127. * Return:
  128. * -1 - vchan error occurred
  129. * 0 - EOF received, do not attempt to access this FD again
  130. * 1 - some data processed, call it again when buffer space and more data
  131. * available
  132. */
  133. int handle_input(libvchan_t *vchan, int fd, int msg_type)
  134. {
  135. char buf[MAX_DATA_CHUNK];
  136. int len;
  137. struct msg_header hdr;
  138. hdr.type = msg_type;
  139. while (libvchan_buffer_space(vchan) > (int)sizeof(struct msg_header)) {
  140. len = libvchan_buffer_space(vchan)-sizeof(struct msg_header);
  141. if (len > (int)sizeof(buf))
  142. len = sizeof(buf);
  143. len = read(fd, buf, len);
  144. if (len < 0) {
  145. if (errno == EAGAIN || errno == EWOULDBLOCK)
  146. return 1;
  147. else
  148. return -1;
  149. }
  150. hdr.len = len;
  151. if (libvchan_send(vchan, &hdr, sizeof(hdr)) < 0)
  152. return -1;
  153. if (len && !write_vchan_all(vchan, buf, len))
  154. return -1;
  155. if (len == 0) {
  156. /* restore flags */
  157. set_block(fd);
  158. if (shutdown(fd, SHUT_RD) < 0) {
  159. if (errno == ENOTSOCK)
  160. close(fd);
  161. }
  162. return 0;
  163. }
  164. }
  165. return 1;
  166. }
  167. /* handle data from vchan and send it to specified FD
  168. * Return:
  169. * -2 - remote process terminated, do not send more data to it
  170. * in this case "status" will be set
  171. * -1 - vchan error occurred
  172. * 0 - EOF received, do not attempt to access this FD again
  173. * 1 - maybe some data processed, call it again when buffer space and more data
  174. * available
  175. */
  176. int handle_remote_data(libvchan_t *data_vchan, int stdin_fd, int *status,
  177. struct buffer *stdin_buf)
  178. {
  179. struct msg_header hdr;
  180. char buf[MAX_DATA_CHUNK];
  181. /* do not receive any data if we have something already buffered */
  182. switch (flush_client_data(stdin_fd, stdin_buf)) {
  183. case WRITE_STDIN_OK:
  184. break;
  185. case WRITE_STDIN_BUFFERED:
  186. return 1;
  187. case WRITE_STDIN_ERROR:
  188. perror("write");
  189. return 0;
  190. }
  191. while (libvchan_data_ready(data_vchan) > 0) {
  192. if (libvchan_recv(data_vchan, &hdr, sizeof(hdr)) < 0)
  193. return -1;
  194. if (hdr.len > MAX_DATA_CHUNK) {
  195. fprintf(stderr, "Too big data chunk received: %d > %d\n",
  196. hdr.len, MAX_DATA_CHUNK);
  197. return -1;
  198. }
  199. if (!read_vchan_all(data_vchan, buf, hdr.len))
  200. return -1;
  201. switch (hdr.type) {
  202. /* handle both directions because this can be either server or client
  203. * of VM-VM connection */
  204. case MSG_DATA_STDIN:
  205. case MSG_DATA_STDOUT:
  206. if (stdin_fd < 0)
  207. /* discard the data */
  208. continue;
  209. if (hdr.len == 0) {
  210. /* restore flags */
  211. set_block(stdin_fd);
  212. if (!child_process_pid || stdin_fd == 1 ||
  213. (shutdown(stdin_fd, SHUT_WR) == -1 &&
  214. errno == ENOTSOCK)) {
  215. close(stdin_fd);
  216. }
  217. stdin_fd = -1;
  218. return 0;
  219. } else {
  220. switch (write_stdin(stdin_fd, buf, hdr.len, stdin_buf)) {
  221. case WRITE_STDIN_OK:
  222. break;
  223. case WRITE_STDIN_BUFFERED:
  224. return 1;
  225. case WRITE_STDIN_ERROR:
  226. if (errno == EPIPE || errno == ECONNRESET) {
  227. if (!child_process_pid || stdin_fd == 1 ||
  228. (shutdown(stdin_fd, SHUT_WR) == -1 &&
  229. errno == ENOTSOCK)) {
  230. close(stdin_fd);
  231. }
  232. stdin_fd = -1;
  233. } else {
  234. perror("write");
  235. }
  236. return 0;
  237. }
  238. }
  239. break;
  240. case MSG_DATA_STDERR:
  241. /* stderr of remote service, log locally */
  242. if (!write_all(2, buf, hdr.len)) {
  243. perror("write");
  244. /* only log the error */
  245. }
  246. break;
  247. case MSG_DATA_EXIT_CODE:
  248. /* remote process exited, so there is no sense to send any data
  249. * to it */
  250. if (hdr.len < sizeof(*status))
  251. *status = 255;
  252. else
  253. memcpy(status, buf, sizeof(*status));
  254. return -2;
  255. }
  256. }
  257. return 1;
  258. }
  259. int process_child_io(libvchan_t *data_vchan,
  260. int stdin_fd, int stdout_fd, int stderr_fd)
  261. {
  262. fd_set rdset, wrset;
  263. int vchan_fd;
  264. sigset_t selectmask;
  265. int child_process_status = -1;
  266. int remote_process_status = -1;
  267. int ret, max_fd;
  268. struct timespec zero_timeout = { 0, 0 };
  269. struct timespec normal_timeout = { 10, 0 };
  270. struct buffer stdin_buf;
  271. sigemptyset(&selectmask);
  272. sigaddset(&selectmask, SIGCHLD);
  273. sigprocmask(SIG_BLOCK, &selectmask, NULL);
  274. sigemptyset(&selectmask);
  275. set_nonblock(stdin_fd);
  276. set_nonblock(stdout_fd);
  277. set_nonblock(stderr_fd);
  278. buffer_init(&stdin_buf);
  279. while (1) {
  280. if (child_exited) {
  281. int status;
  282. if (child_process_pid &&
  283. waitpid(child_process_pid, &status, WNOHANG) > 0) {
  284. if (WIFSIGNALED(status))
  285. child_process_status = 128 + WTERMSIG(status);
  286. else
  287. child_process_status = WEXITSTATUS(status);
  288. if (stdin_fd >= 0) {
  289. /* restore flags */
  290. set_block(stdin_fd);
  291. if (!child_process_pid || stdin_fd == 1 ||
  292. (shutdown(stdin_fd, SHUT_WR) == -1 &&
  293. errno == ENOTSOCK)) {
  294. close(stdin_fd);
  295. }
  296. stdin_fd = -1;
  297. }
  298. }
  299. child_exited = 0;
  300. }
  301. /* if all done, exit the loop */
  302. if ((!child_process_pid || child_process_status > -1) &&
  303. (child_process_pid || remote_process_status > -1) &&
  304. stdin_fd == -1 && stdout_fd == -1 && stderr_fd == -1) {
  305. if (child_process_status > -1) {
  306. send_exit_code(data_vchan, child_process_status);
  307. }
  308. break;
  309. }
  310. /* also if vchan is disconnected (and we processed all the data), there
  311. * is no sense of processing further data */
  312. if (!libvchan_data_ready(data_vchan) &&
  313. !libvchan_is_open(data_vchan) &&
  314. !buffer_len(&stdin_buf)) {
  315. break;
  316. }
  317. /* child signaled desire to use single socket for both stdin and stdout */
  318. if (stdio_socket_requested) {
  319. if (stdout_fd != -1 && stdout_fd != stdin_fd)
  320. close(stdout_fd);
  321. stdout_fd = stdin_fd;
  322. stdio_socket_requested = 0;
  323. }
  324. /* otherwise handle the events */
  325. FD_ZERO(&rdset);
  326. FD_ZERO(&wrset);
  327. max_fd = -1;
  328. vchan_fd = libvchan_fd_for_select(data_vchan);
  329. if (libvchan_buffer_space(data_vchan) > (int)sizeof(struct msg_header)) {
  330. if (stdout_fd >= 0) {
  331. FD_SET(stdout_fd, &rdset);
  332. if (stdout_fd > max_fd)
  333. max_fd = stdout_fd;
  334. }
  335. if (stderr_fd >= 0) {
  336. FD_SET(stderr_fd, &rdset);
  337. if (stderr_fd > max_fd)
  338. max_fd = stderr_fd;
  339. }
  340. }
  341. FD_SET(vchan_fd, &rdset);
  342. if (vchan_fd > max_fd)
  343. max_fd = vchan_fd;
  344. /* if we have something buffered for the child process, wake also on
  345. * writable stdin */
  346. if (stdin_fd > -1 && buffer_len(&stdin_buf)) {
  347. FD_SET(stdin_fd, &wrset);
  348. if (stdin_fd > max_fd)
  349. max_fd = stdin_fd;
  350. }
  351. if (!buffer_len(&stdin_buf) && libvchan_data_ready(data_vchan) > 0) {
  352. /* check for other FDs, but exit immediately */
  353. ret = pselect(max_fd + 1, &rdset, &wrset, NULL, &zero_timeout, &selectmask);
  354. } else
  355. ret = pselect(max_fd + 1, &rdset, &wrset, NULL, &normal_timeout, &selectmask);
  356. if (ret < 0) {
  357. if (errno == EINTR)
  358. continue;
  359. else {
  360. perror("pselect");
  361. /* TODO */
  362. break;
  363. }
  364. }
  365. /* clear event pending flag */
  366. if (FD_ISSET(vchan_fd, &rdset)) {
  367. if (libvchan_wait(data_vchan) < 0)
  368. handle_vchan_error("wait");
  369. }
  370. /* handle_remote_data will check if any data is available */
  371. switch (handle_remote_data(data_vchan, stdin_fd, &remote_process_status, &stdin_buf)) {
  372. case -1:
  373. handle_vchan_error("read");
  374. break;
  375. case 0:
  376. stdin_fd = -1;
  377. break;
  378. case -2:
  379. /* remote process exited, no sense in sending more data to it;
  380. * be careful to not shutdown socket inherited from parent */
  381. if (!child_process_pid || stdout_fd == 0 ||
  382. (shutdown(stdout_fd, SHUT_RD) == -1 &&
  383. errno == ENOTSOCK)) {
  384. close(stdout_fd);
  385. }
  386. stdout_fd = -1;
  387. close(stderr_fd);
  388. stderr_fd = -1;
  389. /* if we do not care for any local process, return remote process code */
  390. if (child_process_pid == 0)
  391. return remote_process_status;
  392. break;
  393. }
  394. if (stdout_fd >= 0 && FD_ISSET(stdout_fd, &rdset)) {
  395. switch (handle_input(data_vchan, stdout_fd, stdout_msg_type)) {
  396. case -1:
  397. handle_vchan_error("send");
  398. break;
  399. case 0:
  400. stdout_fd = -1;
  401. break;
  402. }
  403. }
  404. if (stderr_fd >= 0 && FD_ISSET(stderr_fd, &rdset)) {
  405. switch (handle_input(data_vchan, stderr_fd, MSG_DATA_STDERR)) {
  406. case -1:
  407. handle_vchan_error("send");
  408. break;
  409. case 0:
  410. stderr_fd = -1;
  411. break;
  412. }
  413. }
  414. }
  415. /* make sure that all the pipes/sockets are closed, so the child process
  416. * (if any) will know that the connection is terminated */
  417. if (stdout_fd != -1) {
  418. /* restore flags */
  419. set_block(stdout_fd);
  420. /* be careful to not shutdown socket inherited from parent */
  421. if (!child_process_pid || stdout_fd == 0 ||
  422. (shutdown(stdout_fd, SHUT_RD) == -1 && errno == ENOTSOCK)) {
  423. close(stdout_fd);
  424. }
  425. stdout_fd = -1;
  426. }
  427. if (stdin_fd != -1) {
  428. /* restore flags */
  429. set_block(stdin_fd);
  430. /* be careful to not shutdown socket inherited from parent */
  431. if (!child_process_pid || stdin_fd == 1 ||
  432. (shutdown(stdin_fd, SHUT_WR) == -1 && errno == ENOTSOCK)) {
  433. close(stdin_fd);
  434. }
  435. stdin_fd = -1;
  436. }
  437. if (stderr_fd != -1) {
  438. /* restore flags */
  439. set_block(stderr_fd);
  440. close(stderr_fd);
  441. stderr_fd = -1;
  442. }
  443. if (child_process_pid == 0)
  444. return remote_process_status;
  445. return child_process_status;
  446. }
  447. /* Behaviour depends on type parameter:
  448. * MSG_SERVICE_CONNECT - create vchan server, pass the data to/from given FDs
  449. * (stdin_fd, stdout_fd, stderr_fd), then return remote process exit code
  450. * MSG_JUST_EXEC - connect to vchan server, fork+exec process given by cmdline
  451. * parameter, send artificial exit code "0" (local process can still be
  452. * running), then return 0
  453. * MSG_EXEC_CMDLINE - connect to vchan server, fork+exec process given by
  454. * cmdline parameter, pass the data to/from that process, then return local
  455. * process exit code
  456. *
  457. * buffer_size is about vchan buffer allocated (only for vchan server cases),
  458. * use 0 to use built-in default (64k); needs to be power of 2
  459. */
  460. int handle_new_process_common(int type, int connect_domain, int connect_port,
  461. char *cmdline, int cmdline_len, /* MSG_JUST_EXEC and MSG_EXEC_CMDLINE */
  462. int stdin_fd, int stdout_fd, int stderr_fd /* MSG_SERVICE_CONNECT */,
  463. int buffer_size)
  464. {
  465. libvchan_t *data_vchan;
  466. int exit_code = 0;
  467. pid_t pid;
  468. if (type != MSG_SERVICE_CONNECT) {
  469. assert(cmdline != NULL);
  470. cmdline[cmdline_len-1] = 0;
  471. }
  472. if (buffer_size == 0)
  473. buffer_size = VCHAN_BUFFER_SIZE;
  474. if (type == MSG_SERVICE_CONNECT) {
  475. data_vchan = libvchan_server_init(connect_domain, connect_port,
  476. buffer_size, buffer_size);
  477. if (data_vchan)
  478. libvchan_wait(data_vchan);
  479. } else {
  480. data_vchan = libvchan_client_init(connect_domain, connect_port);
  481. }
  482. if (!data_vchan) {
  483. fprintf(stderr, "Data vchan connection failed\n");
  484. exit(1);
  485. }
  486. handle_handshake(data_vchan);
  487. prepare_child_env();
  488. /* TODO: use setresuid to allow child process to actually send the signal? */
  489. switch (type) {
  490. case MSG_JUST_EXEC:
  491. send_exit_code(data_vchan, handle_just_exec(cmdline));
  492. break;
  493. case MSG_EXEC_CMDLINE:
  494. do_fork_exec(cmdline, &pid, &stdin_fd, &stdout_fd, &stderr_fd);
  495. fprintf(stderr, "executed %s pid %d\n", cmdline, pid);
  496. child_process_pid = pid;
  497. exit_code = process_child_io(data_vchan, stdin_fd, stdout_fd, stderr_fd);
  498. fprintf(stderr, "pid %d exited with %d\n", pid, exit_code);
  499. break;
  500. case MSG_SERVICE_CONNECT:
  501. child_process_pid = 0;
  502. stdout_msg_type = MSG_DATA_STDIN;
  503. exit_code = process_child_io(data_vchan, stdin_fd, stdout_fd, stderr_fd);
  504. break;
  505. }
  506. libvchan_close(data_vchan);
  507. return exit_code;
  508. }
  509. /* Returns PID of data processing process */
  510. pid_t handle_new_process(int type, int connect_domain, int connect_port,
  511. char *cmdline, int cmdline_len)
  512. {
  513. int exit_code;
  514. pid_t pid;
  515. assert(type != MSG_SERVICE_CONNECT);
  516. switch (pid=fork()){
  517. case -1:
  518. perror("fork");
  519. return -1;
  520. case 0:
  521. break;
  522. default:
  523. return pid;
  524. }
  525. /* child process */
  526. exit_code = handle_new_process_common(type, connect_domain, connect_port,
  527. cmdline, cmdline_len,
  528. -1, -1, -1, 0);
  529. exit(exit_code);
  530. /* suppress warning */
  531. return 0;
  532. }
  533. /* Returns exit code of remote process */
  534. int handle_data_client(int type, int connect_domain, int connect_port,
  535. int stdin_fd, int stdout_fd, int stderr_fd, int buffer_size)
  536. {
  537. int exit_code;
  538. assert(type == MSG_SERVICE_CONNECT);
  539. exit_code = handle_new_process_common(type, connect_domain, connect_port,
  540. NULL, 0, stdin_fd, stdout_fd, stderr_fd, buffer_size);
  541. return exit_code;
  542. }