qrexec-agent-data.c 19 KB

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