qrexec-agent-data.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 "qrexec.h"
  35. #include "libqrexec-utils.h"
  36. #include "qrexec-agent.h"
  37. #define VCHAN_BUFFER_SIZE 65536
  38. static volatile int child_exited;
  39. static volatile int stdio_socket_requested;
  40. int stdout_msg_type = MSG_DATA_STDOUT;
  41. pid_t child_process_pid;
  42. static void sigchld_handler(int __attribute__((__unused__))x)
  43. {
  44. child_exited = 1;
  45. signal(SIGCHLD, sigchld_handler);
  46. }
  47. static void sigusr1_handler(int __attribute__((__unused__))x)
  48. {
  49. stdio_socket_requested = 1;
  50. signal(SIGUSR1, SIG_IGN);
  51. }
  52. int handle_handshake(libvchan_t *ctrl)
  53. {
  54. struct msg_header hdr;
  55. struct peer_info info;
  56. /* send own HELLO */
  57. hdr.type = MSG_HELLO;
  58. hdr.len = sizeof(info);
  59. info.version = QREXEC_PROTOCOL_VERSION;
  60. if (libvchan_send(ctrl, &hdr, sizeof(hdr)) != sizeof(hdr)) {
  61. fprintf(stderr, "Failed to send HELLO hdr to agent\n");
  62. return -1;
  63. }
  64. if (libvchan_send(ctrl, &info, sizeof(info)) != sizeof(info)) {
  65. fprintf(stderr, "Failed to send HELLO hdr to agent\n");
  66. return -1;
  67. }
  68. /* receive MSG_HELLO from remote */
  69. if (libvchan_recv(ctrl, &hdr, sizeof(hdr)) != sizeof(hdr)) {
  70. fprintf(stderr, "Failed to read agent HELLO hdr\n");
  71. return -1;
  72. }
  73. if (hdr.type != MSG_HELLO || hdr.len != sizeof(info)) {
  74. fprintf(stderr, "Invalid HELLO packet received: type %d, len %d\n", hdr.type, hdr.len);
  75. return -1;
  76. }
  77. if (libvchan_recv(ctrl, &info, sizeof(info)) != sizeof(info)) {
  78. fprintf(stderr, "Failed to read agent HELLO body\n");
  79. return -1;
  80. }
  81. if (info.version != QREXEC_PROTOCOL_VERSION) {
  82. fprintf(stderr, "Incompatible agent protocol version (remote %d, local %d)\n", info.version, QREXEC_PROTOCOL_VERSION);
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. int handle_just_exec(char *cmdline)
  88. {
  89. int fdn, pid;
  90. switch (pid = fork()) {
  91. case -1:
  92. perror("fork");
  93. return -1;
  94. case 0:
  95. fdn = open("/dev/null", O_RDWR);
  96. fix_fds(fdn, fdn, fdn);
  97. do_exec(cmdline);
  98. perror("execl");
  99. exit(1);
  100. default:;
  101. }
  102. fprintf(stderr, "executed (nowait) %s pid %d\n", cmdline, pid);
  103. return 0;
  104. }
  105. void send_exit_code(libvchan_t *data_vchan, int status)
  106. {
  107. struct msg_header hdr;
  108. hdr.type = MSG_DATA_EXIT_CODE;
  109. hdr.len = sizeof(status);
  110. if (libvchan_send(data_vchan, &hdr, sizeof(hdr)) < 0)
  111. handle_vchan_error("write hdr");
  112. if (libvchan_send(data_vchan, &status, sizeof(status)) < 0)
  113. handle_vchan_error("write status");
  114. fprintf(stderr, "send exit code %d\n", status);
  115. }
  116. /* handle data from specified FD and send over vchan link
  117. * Return:
  118. * -1 - vchan error occurred
  119. * 0 - EOF received, do not attempt to access this FD again
  120. * 1 - some data processed, call it again when buffer space and more data
  121. * available
  122. */
  123. int handle_input(libvchan_t *vchan, int fd, int msg_type)
  124. {
  125. char buf[MAX_DATA_CHUNK];
  126. int len;
  127. struct msg_header hdr;
  128. hdr.type = msg_type;
  129. while (libvchan_buffer_space(vchan) > (int)sizeof(struct msg_header)) {
  130. len = libvchan_buffer_space(vchan)-sizeof(struct msg_header);
  131. if (len > (int)sizeof(buf))
  132. len = sizeof(buf);
  133. len = read(fd, buf, len);
  134. if (len < 0) {
  135. if (errno == EAGAIN || errno == EWOULDBLOCK)
  136. return 1;
  137. else
  138. return -1;
  139. }
  140. hdr.len = len;
  141. if (libvchan_send(vchan, &hdr, sizeof(hdr)) < 0)
  142. return -1;
  143. if (len && !write_vchan_all(vchan, buf, len))
  144. return -1;
  145. if (len == 0) {
  146. if (shutdown(fd, SHUT_RD) < 0) {
  147. if (errno == ENOTSOCK)
  148. close(fd);
  149. }
  150. return 0;
  151. }
  152. }
  153. return 1;
  154. }
  155. /* handle data from vchan and send it to specified FD
  156. * Return:
  157. * -2 - remote process terminated, do not send more data to it
  158. * -1 - vchan error occurred
  159. * 0 - EOF received, do not attempt to access this FD again
  160. * 1 - some data processed, call it again when buffer space and more data
  161. * available
  162. */
  163. int handle_remote_data(libvchan_t *data_vchan, int stdin_fd)
  164. {
  165. struct msg_header hdr;
  166. char buf[MAX_DATA_CHUNK];
  167. int status;
  168. /* TODO: set stdin_fd to non-blocking mode and handle its buffering */
  169. while (libvchan_data_ready(data_vchan) > 0) {
  170. if (libvchan_recv(data_vchan, &hdr, sizeof(hdr)) < 0)
  171. return -1;
  172. if (hdr.len > MAX_DATA_CHUNK) {
  173. fprintf(stderr, "Too big data chunk received: %d > %d\n",
  174. hdr.len, MAX_DATA_CHUNK);
  175. return -1;
  176. }
  177. if (!read_vchan_all(data_vchan, buf, hdr.len))
  178. return -1;
  179. switch (hdr.type) {
  180. /* handle both directions because this can be either server or client
  181. * of VM-VM connection */
  182. case MSG_DATA_STDIN:
  183. case MSG_DATA_STDOUT:
  184. if (stdin_fd < 0)
  185. /* discard the data */
  186. continue;
  187. if (hdr.len == 0) {
  188. if (shutdown(stdin_fd, SHUT_WR) < 0) {
  189. if (errno == ENOTSOCK)
  190. close(stdin_fd);
  191. }
  192. stdin_fd = -1;
  193. return 0;
  194. } else {
  195. /* FIXME: use buffered write here to prevent deadlock */
  196. if (!write_all(stdin_fd, buf, hdr.len)) {
  197. if (errno == EPIPE || errno == ECONNRESET) {
  198. if (shutdown(stdin_fd, SHUT_WR) < 0) {
  199. if (errno == ENOTSOCK)
  200. close(stdin_fd);
  201. }
  202. stdin_fd = -1;
  203. } else {
  204. perror("write");
  205. }
  206. return 0;
  207. }
  208. }
  209. break;
  210. case MSG_DATA_STDERR:
  211. /* stderr of remote service, log locally */
  212. if (!write_all(2, buf, hdr.len)) {
  213. perror("write");
  214. /* only log the error */
  215. }
  216. break;
  217. case MSG_DATA_EXIT_CODE:
  218. /* remote process exited, so there is no sense to send any data
  219. * to it */
  220. if (hdr.len < sizeof(status))
  221. status = 255;
  222. else
  223. memcpy(&status, buf, sizeof(status));
  224. fprintf(stderr, "Remote service process exited with code %d\n", status);
  225. return -2;
  226. }
  227. }
  228. return 1;
  229. }
  230. void process_child_io(libvchan_t *data_vchan,
  231. int stdin_fd, int stdout_fd, int stderr_fd)
  232. {
  233. fd_set rdset, wrset;
  234. int vchan_fd;
  235. sigset_t selectmask;
  236. int child_process_status = -1;
  237. int ret, max_fd;
  238. struct timespec zero_timeout = { 0, 0 };
  239. sigemptyset(&selectmask);
  240. sigaddset(&selectmask, SIGCHLD);
  241. sigprocmask(SIG_BLOCK, &selectmask, NULL);
  242. sigemptyset(&selectmask);
  243. set_nonblock(stdout_fd);
  244. set_nonblock(stderr_fd);
  245. while (1) {
  246. if (child_exited) {
  247. pid_t pid;
  248. int status;
  249. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  250. if (pid == child_process_pid) {
  251. child_process_status = WEXITSTATUS(status);
  252. if (stdin_fd >= 0) {
  253. if (shutdown(stdin_fd, SHUT_WR) < 0) {
  254. if (errno == ENOTSOCK)
  255. close(stdin_fd);
  256. }
  257. stdin_fd = -1;
  258. }
  259. }
  260. }
  261. child_exited = 0;
  262. }
  263. /* if all done, exit the loop */
  264. if ((!child_process_pid || child_process_status > -1) &&
  265. stdin_fd == -1 && stdout_fd == -1 && stderr_fd == -1) {
  266. if (child_process_status > -1) {
  267. send_exit_code(data_vchan, child_process_status);
  268. }
  269. break;
  270. }
  271. /* child signaled desire to use single socket for both stdin and stdout */
  272. if (stdio_socket_requested) {
  273. if (stdout_fd != -1)
  274. close(stdout_fd);
  275. stdout_fd = stdin_fd;
  276. stdio_socket_requested = 0;
  277. }
  278. /* otherwise handle the events */
  279. FD_ZERO(&rdset);
  280. FD_ZERO(&wrset);
  281. max_fd = -1;
  282. vchan_fd = libvchan_fd_for_select(data_vchan);
  283. if (libvchan_buffer_space(data_vchan) > (int)sizeof(struct msg_header)) {
  284. if (stdout_fd >= 0) {
  285. FD_SET(stdout_fd, &rdset);
  286. if (stdout_fd > max_fd)
  287. max_fd = stdout_fd;
  288. }
  289. if (stderr_fd >= 0) {
  290. FD_SET(stderr_fd, &rdset);
  291. if (stderr_fd > max_fd)
  292. max_fd = stderr_fd;
  293. }
  294. }
  295. FD_SET(vchan_fd, &rdset);
  296. if (vchan_fd > max_fd)
  297. max_fd = vchan_fd;
  298. if (libvchan_data_ready(data_vchan) > 0) {
  299. /* check for other FDs, but exit immediately */
  300. ret = pselect(max_fd + 1, &rdset, &wrset, NULL, &zero_timeout, &selectmask);
  301. } else
  302. ret = pselect(max_fd + 1, &rdset, &wrset, NULL, NULL, &selectmask);
  303. if (ret < 0) {
  304. if (errno == EINTR)
  305. continue;
  306. else {
  307. perror("pselect");
  308. /* TODO */
  309. break;
  310. }
  311. }
  312. /* clear event pending flag */
  313. if (FD_ISSET(vchan_fd, &rdset)) {
  314. if (libvchan_wait(data_vchan) < 0)
  315. handle_vchan_error("wait");
  316. }
  317. if (stdout_fd >= 0 && FD_ISSET(stdout_fd, &rdset)) {
  318. switch (handle_input(data_vchan, stdout_fd, stdout_msg_type)) {
  319. case -1:
  320. handle_vchan_error("send");
  321. break;
  322. case 0:
  323. stdout_fd = -1;
  324. break;
  325. }
  326. }
  327. if (stderr_fd >= 0 && FD_ISSET(stderr_fd, &rdset)) {
  328. switch (handle_input(data_vchan, stderr_fd, MSG_DATA_STDERR)) {
  329. case -1:
  330. handle_vchan_error("send");
  331. break;
  332. case 0:
  333. stderr_fd = -1;
  334. break;
  335. }
  336. }
  337. /* handle_remote_data will check if any data is available */
  338. switch (handle_remote_data(data_vchan, stdin_fd)) {
  339. case -1:
  340. handle_vchan_error("read");
  341. break;
  342. case 0:
  343. stdin_fd = -1;
  344. break;
  345. case -2:
  346. /* remote process exited, no sense in sending more data to it */
  347. if (shutdown(stdout_fd, SHUT_RD) < 0) {
  348. if (errno == ENOTSOCK)
  349. close(stdout_fd);
  350. }
  351. stdout_fd = -1;
  352. close(stderr_fd);
  353. stderr_fd = -1;
  354. break;
  355. }
  356. }
  357. }
  358. pid_t handle_new_process(int type, int connect_domain, int connect_port,
  359. char *cmdline, int cmdline_len)
  360. {
  361. struct service_params *svc_params = (struct service_params*)cmdline;
  362. libvchan_t *data_vchan;
  363. pid_t pid;
  364. int stdin_fd, stdout_fd, stderr_fd;
  365. char pid_s[10];
  366. if (type == MSG_SERVICE_CONNECT) {
  367. if (cmdline_len != sizeof(*svc_params)) {
  368. fprintf(stderr, "Invalid MSG_SERVICE_CONNECT packet (cmdline len %d)\n", cmdline_len);
  369. return -1;
  370. }
  371. sscanf(cmdline, "%d %d %d", &stdin_fd, &stdout_fd, &stderr_fd);
  372. }
  373. switch (pid=fork()){
  374. case -1:
  375. perror("fork");
  376. return -1;
  377. case 0:
  378. break;
  379. default:
  380. if (type == MSG_SERVICE_CONNECT) {
  381. /* no longer needed in parent process */
  382. close(stdin_fd);
  383. close(stdout_fd);
  384. close(stderr_fd);
  385. }
  386. return pid;
  387. }
  388. /* child process */
  389. if (type == MSG_SERVICE_CONNECT) {
  390. data_vchan = libvchan_server_init(connect_domain, connect_port,
  391. VCHAN_BUFFER_SIZE, VCHAN_BUFFER_SIZE);
  392. if (data_vchan)
  393. libvchan_wait(data_vchan);
  394. } else {
  395. data_vchan = libvchan_client_init(connect_domain, connect_port);
  396. }
  397. if (!data_vchan) {
  398. fprintf(stderr, "Data vchan connection failed\n");
  399. exit(1);
  400. }
  401. handle_handshake(data_vchan);
  402. signal(SIGCHLD, sigchld_handler);
  403. signal(SIGUSR1, sigusr1_handler);
  404. snprintf(pid_s, sizeof(pid_s), "%d", getpid());
  405. setenv("QREXEC_AGENT_PID", pid_s, 1);
  406. /* TODO: use setresuid to allow child process to actually send the signal? */
  407. switch (type) {
  408. case MSG_JUST_EXEC:
  409. send_exit_code(data_vchan, handle_just_exec(cmdline));
  410. libvchan_close(data_vchan);
  411. break;
  412. case MSG_EXEC_CMDLINE:
  413. do_fork_exec(cmdline, &pid, &stdin_fd, &stdout_fd, &stderr_fd);
  414. fprintf(stderr, "executed %s pid %d\n", cmdline, pid);
  415. child_process_pid = pid;
  416. process_child_io(data_vchan, stdin_fd, stdout_fd, stderr_fd);
  417. break;
  418. case MSG_SERVICE_CONNECT:
  419. child_process_pid = 0;
  420. stdout_msg_type = MSG_DATA_STDIN;
  421. process_child_io(data_vchan, stdin_fd, stdout_fd, stderr_fd);
  422. break;
  423. }
  424. exit(0);
  425. /* suppress warning */
  426. return 0;
  427. }