qrexec-agent-data.c 18 KB

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