qrexec-agent.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * The Qubes OS Project, http://www.qubes-os.org
  3. *
  4. * Copyright (C) 2010 Rafal Wojtczuk <rafal@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. #define _GNU_SOURCE
  22. #include <sys/select.h>
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <signal.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <sys/wait.h>
  31. #include <fcntl.h>
  32. #include <string.h>
  33. #include <pwd.h>
  34. #include <grp.h>
  35. #include <sys/stat.h>
  36. #include <assert.h>
  37. #include "qrexec.h"
  38. #include <libvchan.h>
  39. #include "libqrexec-utils.h"
  40. #include "qrexec-agent.h"
  41. struct _connection_info {
  42. int pid; /* pid of child process handling the data */
  43. int fd; /* socket to process handling the data (wait for EOF here) */
  44. int connect_domain;
  45. int connect_port;
  46. };
  47. int max_process_fd = -1;
  48. /* */
  49. struct _connection_info connection_info[MAX_FDS];
  50. libvchan_t *ctrl_vchan;
  51. int trigger_fd;
  52. int meminfo_write_started = 0;
  53. void no_colon_in_cmd()
  54. {
  55. fprintf(stderr,
  56. "cmdline is supposed to be in user:command form\n");
  57. exit(1);
  58. }
  59. /* Start program requested by dom0 in already prepared process
  60. * (stdin/stdout/stderr already set, etc)
  61. * Called in two cases:
  62. * MSG_JUST_EXEC - from qrexec-agent-data.c:handle_new_process_common->handle_just_exec
  63. * MSG_EXEC_CMDLINE - from
  64. * qrexec-agent-data.c:handle_new_process_common->do_fork_exec (callback
  65. * registerd with register_exec_func in init() here)
  66. *
  67. * cmd parameter came from dom0 (MSG_JUST_EXEC or MSG_EXEC_CMDLINE messages), so
  68. * is trusted. Even in VM-VM service request, the command here is controlled by
  69. * dom0 - it will be in form:
  70. * RPC_REQUEST_COMMAND " " service_name " " source_vm_name
  71. * where service_name is already validated against Qrexec RPC policy
  72. *
  73. * If dom0 sends overly long cmd, it will probably crash qrexec-agent (unless
  74. * process can allocate up to 4GB on both stack and heap), sorry.
  75. */
  76. void do_exec(const char *cmd)
  77. {
  78. char buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - RPC_REQUEST_COMMAND_LEN + 1];
  79. char *realcmd = index(cmd, ':');
  80. if (!realcmd)
  81. no_colon_in_cmd();
  82. /* mark end of username and move to command */
  83. *realcmd = 0;
  84. realcmd++;
  85. /* ignore "nogui:" prefix in linux agent */
  86. if (strncmp(realcmd, NOGUI_CMD_PREFIX, NOGUI_CMD_PREFIX_LEN) == 0)
  87. realcmd += NOGUI_CMD_PREFIX_LEN;
  88. /* replace magic RPC cmd with RPC multiplexer path */
  89. if (strncmp(realcmd, RPC_REQUEST_COMMAND " ", RPC_REQUEST_COMMAND_LEN+1)==0) {
  90. strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH);
  91. strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), realcmd + RPC_REQUEST_COMMAND_LEN);
  92. realcmd = buf;
  93. }
  94. signal(SIGCHLD, SIG_DFL);
  95. signal(SIGPIPE, SIG_DFL);
  96. execl("/bin/su", "su", "-", cmd, "-c", realcmd, NULL);
  97. perror("execl");
  98. exit(1);
  99. }
  100. void handle_vchan_error(const char *op)
  101. {
  102. fprintf(stderr, "Error while vchan %s, exiting\n", op);
  103. exit(1);
  104. }
  105. void init()
  106. {
  107. mode_t old_umask;
  108. /* FIXME: This 0 is remote domain ID */
  109. ctrl_vchan = libvchan_server_init(0, VCHAN_BASE_PORT, 4096, 4096);
  110. if (!ctrl_vchan)
  111. handle_vchan_error("server_init");
  112. if (handle_handshake(ctrl_vchan) < 0)
  113. exit(1);
  114. old_umask = umask(0);
  115. trigger_fd = get_server_socket(QREXEC_AGENT_TRIGGER_PATH);
  116. umask(old_umask);
  117. register_exec_func(do_exec);
  118. /* wait for qrexec daemon */
  119. while (!libvchan_is_open(ctrl_vchan))
  120. libvchan_wait(ctrl_vchan);
  121. }
  122. void wake_meminfo_writer()
  123. {
  124. FILE *f;
  125. int pid;
  126. if (meminfo_write_started)
  127. /* wake meminfo-writer only once */
  128. return;
  129. f = fopen(MEMINFO_WRITER_PIDFILE, "r");
  130. if (f == NULL) {
  131. /* no meminfo-writer found, ignoring */
  132. return;
  133. }
  134. if (fscanf(f, "%d", &pid) < 1) {
  135. fclose(f);
  136. /* no meminfo-writer found, ignoring */
  137. return;
  138. }
  139. fclose(f);
  140. if (pid <= 1 || pid > 0xffff) {
  141. /* check within acceptable range */
  142. return;
  143. }
  144. if (kill(pid, SIGUSR1) < 0) {
  145. /* Can't send signal */
  146. return;
  147. }
  148. meminfo_write_started = 1;
  149. }
  150. int try_fork_server(int type, int connect_domain, int connect_port,
  151. char *cmdline, int cmdline_len) {
  152. char username[cmdline_len];
  153. char *colon;
  154. char *fork_server_socket_path;
  155. int s, len;
  156. struct sockaddr_un remote;
  157. struct qrexec_cmd_info info;
  158. strncpy(username, cmdline, cmdline_len);
  159. colon = index(username, ':');
  160. if (!colon)
  161. return -1;
  162. *colon = '\0';
  163. if (asprintf(&fork_server_socket_path, QREXEC_FORK_SERVER_SOCKET, username) < 0) {
  164. fprintf(stderr, "Memory allocation failed\n");
  165. return -1;
  166. }
  167. remote.sun_family = AF_UNIX;
  168. strncpy(remote.sun_path, fork_server_socket_path,
  169. sizeof(remote.sun_path));
  170. free(fork_server_socket_path);
  171. if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  172. perror("socket");
  173. return -1;
  174. }
  175. len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  176. if (connect(s, (struct sockaddr *) &remote, len) == -1) {
  177. if (errno != ECONNREFUSED && errno != ENOENT)
  178. perror("connect");
  179. close(s);
  180. return -1;
  181. }
  182. info.type = type;
  183. info.connect_domain = connect_domain;
  184. info.connect_port = connect_port;
  185. info.cmdline_len = cmdline_len-(strlen(username)+1);
  186. if (!write_all(s, &info, sizeof(info))) {
  187. perror("write");
  188. close(s);
  189. return -1;
  190. }
  191. if (!write_all(s, colon+1, info.cmdline_len)) {
  192. perror("write");
  193. close(s);
  194. return -1;
  195. }
  196. return s;
  197. }
  198. void register_vchan_connection(pid_t pid, int fd, int domain, int port)
  199. {
  200. int i;
  201. for (i = 0; i < MAX_FDS; i++) {
  202. if (connection_info[i].pid == 0) {
  203. connection_info[i].pid = pid;
  204. connection_info[i].fd = fd;
  205. connection_info[i].connect_domain = domain;
  206. connection_info[i].connect_port = port;
  207. return;
  208. }
  209. }
  210. fprintf(stderr, "No free slot for child %d (connection to %d:%d)\n", pid, domain, port);
  211. }
  212. /* hdr parameter is received from dom0, so it is trusted */
  213. void handle_server_exec_request(struct msg_header *hdr)
  214. {
  215. struct exec_params params;
  216. int buf_len = hdr->len-sizeof(params);
  217. char buf[buf_len];
  218. pid_t child_agent;
  219. int client_fd;
  220. assert(hdr->len >= sizeof(params));
  221. if (libvchan_recv(ctrl_vchan, &params, sizeof(params)) < 0)
  222. handle_vchan_error("read exec params");
  223. if (libvchan_recv(ctrl_vchan, buf, buf_len) < 0)
  224. handle_vchan_error("read exec cmd");
  225. if ((hdr->type == MSG_EXEC_CMDLINE || hdr->type == MSG_JUST_EXEC) &&
  226. !strstr(buf, ":nogui:")) {
  227. int child_socket = try_fork_server(hdr->type,
  228. params.connect_domain, params.connect_port,
  229. buf, buf_len);
  230. if (child_socket >= 0) {
  231. register_vchan_connection(-1, child_socket,
  232. params.connect_domain, params.connect_port);
  233. return;
  234. }
  235. }
  236. if (hdr->type == MSG_SERVICE_CONNECT && sscanf(buf, "SOCKET%d", &client_fd)) {
  237. /* FIXME: Maybe add some check if client_fd is really FD to some
  238. * qrexec-client-vm process; but this data comes from qrexec-daemon
  239. * (which sends back what it got from us earlier), so it isn't critical.
  240. */
  241. if (write(client_fd, &params, sizeof(params)) < 0) {
  242. /* ignore */
  243. }
  244. /* No need to send request_id (buf) - the client don't need it, there
  245. * is only meaningless (for the client) socket FD */
  246. /* Register connection even if there was an error sending params to
  247. * qrexec-client-vm. This way the mainloop will clean the things up
  248. * (close socket, send MSG_CONNECTION_TERMINATED) when qrexec-client-vm
  249. * will close the socket (terminate itself). */
  250. register_vchan_connection(-1, client_fd,
  251. params.connect_domain, params.connect_port);
  252. return;
  253. }
  254. /* No fork server case */
  255. child_agent = handle_new_process(hdr->type,
  256. params.connect_domain, params.connect_port,
  257. buf, buf_len);
  258. register_vchan_connection(child_agent, -1,
  259. params.connect_domain, params.connect_port);
  260. }
  261. void handle_service_refused(struct msg_header *hdr)
  262. {
  263. struct service_params params;
  264. int socket_fd;
  265. if (hdr->len != sizeof(params)) {
  266. fprintf(stderr, "Invalid msg 0x%x length (%d)\n", MSG_SERVICE_REFUSED, hdr->len);
  267. exit(1);
  268. }
  269. if (libvchan_recv(ctrl_vchan, &params, sizeof(params)) < 0)
  270. handle_vchan_error("read exec params");
  271. if (sscanf(params.ident, "SOCKET%d", &socket_fd))
  272. close(socket_fd);
  273. else
  274. fprintf(stderr, "Received REFUSED for unknown service request '%s'\n", params.ident);
  275. }
  276. void handle_server_cmd()
  277. {
  278. struct msg_header s_hdr;
  279. if (libvchan_recv(ctrl_vchan, &s_hdr, sizeof(s_hdr)) < 0)
  280. handle_vchan_error("read s_hdr");
  281. // fprintf(stderr, "got %x %x %x\n", s_hdr.type, s_hdr.client_id,
  282. // s_hdr.len);
  283. switch (s_hdr.type) {
  284. case MSG_EXEC_CMDLINE:
  285. case MSG_JUST_EXEC:
  286. case MSG_SERVICE_CONNECT:
  287. wake_meminfo_writer();
  288. handle_server_exec_request(&s_hdr);
  289. break;
  290. case MSG_SERVICE_REFUSED:
  291. handle_service_refused(&s_hdr);
  292. break;
  293. default:
  294. fprintf(stderr, "msg type from daemon is %d ?\n",
  295. s_hdr.type);
  296. exit(1);
  297. }
  298. }
  299. volatile int child_exited;
  300. void sigchld_handler(int x __attribute__((__unused__)))
  301. {
  302. child_exited = 1;
  303. signal(SIGCHLD, sigchld_handler);
  304. }
  305. int find_connection(int pid)
  306. {
  307. int i;
  308. for (i = 0; i < MAX_FDS; i++)
  309. if (connection_info[i].pid == pid)
  310. return i;
  311. return -1;
  312. }
  313. void release_connection(int id) {
  314. struct msg_header hdr;
  315. struct exec_params params;
  316. hdr.type = MSG_CONNECTION_TERMINATED;
  317. hdr.len = sizeof(struct exec_params);
  318. params.connect_domain = connection_info[id].connect_domain;
  319. params.connect_port = connection_info[id].connect_port;
  320. if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
  321. handle_vchan_error("send");
  322. if (libvchan_send(ctrl_vchan, &params, sizeof(params)) < 0)
  323. handle_vchan_error("send");
  324. connection_info[id].pid = 0;
  325. }
  326. void reap_children()
  327. {
  328. int status;
  329. int pid;
  330. int id;
  331. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  332. id = find_connection(pid);
  333. if (id < 0)
  334. continue;
  335. release_connection(id);
  336. }
  337. child_exited = 0;
  338. }
  339. int fill_fds_for_select(fd_set * rdset, fd_set * wrset)
  340. {
  341. int max = -1;
  342. int i;
  343. FD_ZERO(rdset);
  344. FD_ZERO(wrset);
  345. FD_SET(trigger_fd, rdset);
  346. if (trigger_fd > max)
  347. max = trigger_fd;
  348. for (i = 0; i < MAX_FDS; i++) {
  349. if (connection_info[i].pid != 0 && connection_info[i].fd != -1) {
  350. FD_SET(connection_info[i].fd, rdset);
  351. if (connection_info[i].fd > max)
  352. max = connection_info[i].fd;
  353. }
  354. }
  355. return max;
  356. }
  357. void handle_trigger_io()
  358. {
  359. struct msg_header hdr;
  360. struct trigger_service_params params;
  361. int ret;
  362. int client_fd;
  363. client_fd = do_accept(trigger_fd);
  364. if (client_fd < 0)
  365. return;
  366. hdr.len = sizeof(params);
  367. ret = read(client_fd, &params, sizeof(params));
  368. if (ret == sizeof(params)) {
  369. hdr.type = MSG_TRIGGER_SERVICE;
  370. snprintf(params.request_id.ident, sizeof(params.request_id), "SOCKET%d", client_fd);
  371. if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
  372. handle_vchan_error("write hdr");
  373. if (libvchan_send(ctrl_vchan, &params, sizeof(params)) < 0)
  374. handle_vchan_error("write params");
  375. }
  376. if (ret <= 0) {
  377. close(client_fd);
  378. }
  379. /* do not close client_fd - we'll need it to send the connection details
  380. * later (when dom0 accepts the request) */
  381. }
  382. void handle_terminated_fork_client(fd_set *rdset) {
  383. int i, ret;
  384. char buf[2];
  385. for (i = 0; i < MAX_FDS; i++) {
  386. if (connection_info[i].pid && connection_info[i].fd >= 0 &&
  387. FD_ISSET(connection_info[i].fd, rdset)) {
  388. ret = read(connection_info[i].fd, buf, sizeof(buf));
  389. if (ret == 0 || (ret == -1 && errno == ECONNRESET)) {
  390. close(connection_info[i].fd);
  391. release_connection(i);
  392. } else {
  393. fprintf(stderr, "Unexpected read on fork-server connection: %d(%s)\n", ret, strerror(errno));
  394. close(connection_info[i].fd);
  395. release_connection(i);
  396. }
  397. }
  398. }
  399. }
  400. int main()
  401. {
  402. fd_set rdset, wrset;
  403. int max;
  404. sigset_t chld_set;
  405. init();
  406. signal(SIGCHLD, sigchld_handler);
  407. signal(SIGPIPE, SIG_IGN);
  408. sigemptyset(&chld_set);
  409. sigaddset(&chld_set, SIGCHLD);
  410. for (;;) {
  411. sigprocmask(SIG_BLOCK, &chld_set, NULL);
  412. if (child_exited)
  413. reap_children();
  414. max = fill_fds_for_select(&rdset, &wrset);
  415. if (libvchan_buffer_space(ctrl_vchan) <=
  416. (int)sizeof(struct msg_header))
  417. FD_ZERO(&rdset);
  418. wait_for_vchan_or_argfd(ctrl_vchan, max, &rdset, &wrset);
  419. sigprocmask(SIG_UNBLOCK, &chld_set, NULL);
  420. while (libvchan_data_ready(ctrl_vchan))
  421. handle_server_cmd();
  422. if (FD_ISSET(trigger_fd, &rdset))
  423. handle_trigger_io();
  424. handle_terminated_fork_client(&rdset);
  425. }
  426. }