qrexec-agent-data.c 13 KB

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