qrexec-agent-data.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. status = *(unsigned int *)buf;
  221. fprintf(stderr, "Remote service process exited with code %d\n", status);
  222. return -2;
  223. }
  224. }
  225. return 1;
  226. }
  227. void process_child_io(libvchan_t *data_vchan,
  228. int stdin_fd, int stdout_fd, int stderr_fd)
  229. {
  230. fd_set rdset, wrset;
  231. int vchan_fd;
  232. sigset_t selectmask;
  233. int child_process_status = -1;
  234. int ret, max_fd;
  235. struct timespec zero_timeout = { 0, 0 };
  236. sigemptyset(&selectmask);
  237. sigaddset(&selectmask, SIGCHLD);
  238. sigprocmask(SIG_BLOCK, &selectmask, NULL);
  239. sigemptyset(&selectmask);
  240. set_nonblock(stdout_fd);
  241. set_nonblock(stderr_fd);
  242. while (1) {
  243. if (child_exited) {
  244. pid_t pid;
  245. int status;
  246. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  247. if (pid == child_process_pid) {
  248. child_process_status = WEXITSTATUS(status);
  249. if (stdin_fd >= 0) {
  250. if (shutdown(stdin_fd, SHUT_WR) < 0) {
  251. if (errno == ENOTSOCK)
  252. close(stdin_fd);
  253. }
  254. stdin_fd = -1;
  255. }
  256. }
  257. }
  258. child_exited = 0;
  259. }
  260. /* if all done, exit the loop */
  261. if ((!child_process_pid || child_process_status > -1) &&
  262. stdin_fd == -1 && stdout_fd == -1 && stderr_fd == -1) {
  263. if (child_process_status > -1) {
  264. send_exit_code(data_vchan, child_process_status);
  265. }
  266. break;
  267. }
  268. /* child signaled desire to use single socket for both stdin and stdout */
  269. if (stdio_socket_requested) {
  270. if (stdout_fd != -1)
  271. close(stdout_fd);
  272. stdout_fd = stdin_fd;
  273. stdio_socket_requested = 0;
  274. }
  275. /* otherwise handle the events */
  276. FD_ZERO(&rdset);
  277. FD_ZERO(&wrset);
  278. max_fd = -1;
  279. vchan_fd = libvchan_fd_for_select(data_vchan);
  280. if (libvchan_buffer_space(data_vchan) > (int)sizeof(struct msg_header)) {
  281. if (stdout_fd >= 0) {
  282. FD_SET(stdout_fd, &rdset);
  283. if (stdout_fd > max_fd)
  284. max_fd = stdout_fd;
  285. }
  286. if (stderr_fd >= 0) {
  287. FD_SET(stderr_fd, &rdset);
  288. if (stderr_fd > max_fd)
  289. max_fd = stderr_fd;
  290. }
  291. }
  292. FD_SET(vchan_fd, &rdset);
  293. if (vchan_fd > max_fd)
  294. max_fd = vchan_fd;
  295. if (libvchan_data_ready(data_vchan) > 0) {
  296. /* check for other FDs, but exit immediately */
  297. ret = pselect(max_fd + 1, &rdset, &wrset, NULL, &zero_timeout, &selectmask);
  298. } else
  299. ret = pselect(max_fd + 1, &rdset, &wrset, NULL, NULL, &selectmask);
  300. if (ret < 0) {
  301. if (errno == EINTR)
  302. continue;
  303. else {
  304. perror("pselect");
  305. /* TODO */
  306. break;
  307. }
  308. }
  309. /* clear event pending flag */
  310. if (FD_ISSET(vchan_fd, &rdset)) {
  311. if (libvchan_wait(data_vchan) < 0)
  312. handle_vchan_error("wait");
  313. }
  314. if (stdout_fd >= 0 && FD_ISSET(stdout_fd, &rdset)) {
  315. switch (handle_input(data_vchan, stdout_fd, stdout_msg_type)) {
  316. case -1:
  317. handle_vchan_error("send");
  318. break;
  319. case 0:
  320. stdout_fd = -1;
  321. break;
  322. }
  323. }
  324. if (stderr_fd >= 0 && FD_ISSET(stderr_fd, &rdset)) {
  325. switch (handle_input(data_vchan, stderr_fd, MSG_DATA_STDERR)) {
  326. case -1:
  327. handle_vchan_error("send");
  328. break;
  329. case 0:
  330. stderr_fd = -1;
  331. break;
  332. }
  333. }
  334. /* handle_remote_data will check if any data is available */
  335. switch (handle_remote_data(data_vchan, stdin_fd)) {
  336. case -1:
  337. handle_vchan_error("read");
  338. break;
  339. case 0:
  340. stdin_fd = -1;
  341. break;
  342. case -2:
  343. /* remote process exited, no sense in sending more data to it */
  344. if (shutdown(stdout_fd, SHUT_RD) < 0) {
  345. if (errno == ENOTSOCK)
  346. close(stdout_fd);
  347. }
  348. stdout_fd = -1;
  349. close(stderr_fd);
  350. stderr_fd = -1;
  351. break;
  352. }
  353. }
  354. }
  355. pid_t handle_new_process(int type, int connect_domain, int connect_port,
  356. char *cmdline, int cmdline_len)
  357. {
  358. struct service_params *svc_params = (struct service_params*)cmdline;
  359. libvchan_t *data_vchan;
  360. pid_t pid;
  361. int stdin_fd, stdout_fd, stderr_fd;
  362. char pid_s[10];
  363. if (type == MSG_SERVICE_CONNECT) {
  364. if (cmdline_len != sizeof(*svc_params)) {
  365. fprintf(stderr, "Invalid MSG_SERVICE_CONNECT packet (cmdline len %d)\n", cmdline_len);
  366. return -1;
  367. }
  368. sscanf(cmdline, "%d %d %d", &stdin_fd, &stdout_fd, &stderr_fd);
  369. }
  370. switch (pid=fork()){
  371. case -1:
  372. perror("fork");
  373. return -1;
  374. case 0:
  375. break;
  376. default:
  377. if (type == MSG_SERVICE_CONNECT) {
  378. /* no longer needed in parent process */
  379. close(stdin_fd);
  380. close(stdout_fd);
  381. close(stderr_fd);
  382. }
  383. return pid;
  384. }
  385. /* child process */
  386. if (type == MSG_SERVICE_CONNECT) {
  387. data_vchan = libvchan_server_init(connect_domain, connect_port,
  388. VCHAN_BUFFER_SIZE, VCHAN_BUFFER_SIZE);
  389. if (data_vchan)
  390. libvchan_wait(data_vchan);
  391. } else {
  392. data_vchan = libvchan_client_init(connect_domain, connect_port);
  393. }
  394. if (!data_vchan) {
  395. fprintf(stderr, "Data vchan connection failed\n");
  396. exit(1);
  397. }
  398. handle_handshake(data_vchan);
  399. signal(SIGCHLD, sigchld_handler);
  400. signal(SIGUSR1, sigusr1_handler);
  401. snprintf(pid_s, sizeof(pid_s), "%d", getpid());
  402. setenv("QREXEC_AGENT_PID", pid_s, 1);
  403. /* TODO: use setresuid to allow child process to actually send the signal? */
  404. switch (type) {
  405. case MSG_JUST_EXEC:
  406. send_exit_code(data_vchan, handle_just_exec(cmdline));
  407. libvchan_close(data_vchan);
  408. break;
  409. case MSG_EXEC_CMDLINE:
  410. do_fork_exec(cmdline, &pid, &stdin_fd, &stdout_fd, &stderr_fd);
  411. fprintf(stderr, "executed %s pid %d\n", cmdline, pid);
  412. child_process_pid = pid;
  413. process_child_io(data_vchan, stdin_fd, stdout_fd, stderr_fd);
  414. break;
  415. case MSG_SERVICE_CONNECT:
  416. child_process_pid = 0;
  417. stdout_msg_type = MSG_DATA_STDIN;
  418. process_child_io(data_vchan, stdin_fd, stdout_fd, stderr_fd);
  419. break;
  420. }
  421. exit(0);
  422. /* suppress warning */
  423. return 0;
  424. }