qrexec-agent-data.c 18 KB

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