os-posix.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * os-posix.c
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2010 Red Hat, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include <sys/wait.h>
  27. #include <pwd.h>
  28. #include <grp.h>
  29. #include <libgen.h>
  30. /* Needed early for CONFIG_BSD etc. */
  31. #include "net/slirp.h"
  32. #include "qemu/qemu-options.h"
  33. #include "qemu/error-report.h"
  34. #include "qemu/log.h"
  35. #include "sysemu/runstate.h"
  36. #include "qemu/cutils.h"
  37. #ifdef CONFIG_LINUX
  38. #include <sys/prctl.h>
  39. #endif
  40. /*
  41. * Must set all three of these at once.
  42. * Legal combinations are unset by name by uid
  43. */
  44. static struct passwd *user_pwd; /* NULL non-NULL NULL */
  45. static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */
  46. static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */
  47. static const char *chroot_dir;
  48. static int daemonize;
  49. static int daemon_pipe;
  50. void os_setup_early_signal_handling(void)
  51. {
  52. struct sigaction act;
  53. sigfillset(&act.sa_mask);
  54. act.sa_flags = 0;
  55. act.sa_handler = SIG_IGN;
  56. sigaction(SIGPIPE, &act, NULL);
  57. }
  58. static void termsig_handler(int signal, siginfo_t *info, void *c)
  59. {
  60. qemu_system_killed(info->si_signo, info->si_pid);
  61. }
  62. void os_setup_signal_handling(void)
  63. {
  64. struct sigaction act;
  65. memset(&act, 0, sizeof(act));
  66. act.sa_sigaction = termsig_handler;
  67. act.sa_flags = SA_SIGINFO;
  68. sigaction(SIGINT, &act, NULL);
  69. sigaction(SIGHUP, &act, NULL);
  70. sigaction(SIGTERM, &act, NULL);
  71. }
  72. void os_set_proc_name(const char *s)
  73. {
  74. #if defined(PR_SET_NAME)
  75. char name[16];
  76. if (!s)
  77. return;
  78. pstrcpy(name, sizeof(name), s);
  79. /* Could rewrite argv[0] too, but that's a bit more complicated.
  80. This simple way is enough for `top'. */
  81. if (prctl(PR_SET_NAME, name)) {
  82. error_report("unable to change process name: %s", strerror(errno));
  83. exit(1);
  84. }
  85. #else
  86. error_report("Change of process name not supported by your OS");
  87. exit(1);
  88. #endif
  89. }
  90. static bool os_parse_runas_uid_gid(const char *optarg)
  91. {
  92. unsigned long lv;
  93. const char *ep;
  94. uid_t got_uid;
  95. gid_t got_gid;
  96. int rc;
  97. rc = qemu_strtoul(optarg, &ep, 0, &lv);
  98. got_uid = lv; /* overflow here is ID in C99 */
  99. if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
  100. return false;
  101. }
  102. rc = qemu_strtoul(ep + 1, 0, 0, &lv);
  103. got_gid = lv; /* overflow here is ID in C99 */
  104. if (rc || got_gid != lv || got_gid == (gid_t)-1) {
  105. return false;
  106. }
  107. user_pwd = NULL;
  108. user_uid = got_uid;
  109. user_gid = got_gid;
  110. return true;
  111. }
  112. /*
  113. * Parse OS specific command line options.
  114. * return 0 if option handled, -1 otherwise
  115. */
  116. int os_parse_cmd_args(int index, const char *optarg)
  117. {
  118. switch (index) {
  119. case QEMU_OPTION_runas:
  120. user_pwd = getpwnam(optarg);
  121. if (user_pwd) {
  122. user_uid = -1;
  123. user_gid = -1;
  124. } else if (!os_parse_runas_uid_gid(optarg)) {
  125. error_report("User \"%s\" doesn't exist"
  126. " (and is not <uid>:<gid>)",
  127. optarg);
  128. exit(1);
  129. }
  130. break;
  131. case QEMU_OPTION_chroot:
  132. chroot_dir = optarg;
  133. break;
  134. case QEMU_OPTION_daemonize:
  135. daemonize = 1;
  136. break;
  137. #if defined(CONFIG_LINUX)
  138. case QEMU_OPTION_enablefips:
  139. warn_report("-enable-fips is deprecated, please build QEMU with "
  140. "the `libgcrypt` library as the cryptography provider "
  141. "to enable FIPS compliance");
  142. fips_set_state(true);
  143. break;
  144. #endif
  145. default:
  146. return -1;
  147. }
  148. return 0;
  149. }
  150. static void change_process_uid(void)
  151. {
  152. assert((user_uid == (uid_t)-1) || user_pwd == NULL);
  153. assert((user_uid == (uid_t)-1) ==
  154. (user_gid == (gid_t)-1));
  155. if (user_pwd || user_uid != (uid_t)-1) {
  156. gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid;
  157. uid_t intended_uid = user_pwd ? user_pwd->pw_uid : user_uid;
  158. if (setgid(intended_gid) < 0) {
  159. error_report("Failed to setgid(%d)", intended_gid);
  160. exit(1);
  161. }
  162. if (user_pwd) {
  163. if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
  164. error_report("Failed to initgroups(\"%s\", %d)",
  165. user_pwd->pw_name, user_pwd->pw_gid);
  166. exit(1);
  167. }
  168. } else {
  169. if (setgroups(1, &user_gid) < 0) {
  170. error_report("Failed to setgroups(1, [%d])",
  171. user_gid);
  172. exit(1);
  173. }
  174. }
  175. if (setuid(intended_uid) < 0) {
  176. error_report("Failed to setuid(%d)", intended_uid);
  177. exit(1);
  178. }
  179. if (setuid(0) != -1) {
  180. error_report("Dropping privileges failed");
  181. exit(1);
  182. }
  183. }
  184. }
  185. static void change_root(void)
  186. {
  187. if (chroot_dir) {
  188. if (chroot(chroot_dir) < 0) {
  189. error_report("chroot failed");
  190. exit(1);
  191. }
  192. if (chdir("/")) {
  193. error_report("not able to chdir to /: %s", strerror(errno));
  194. exit(1);
  195. }
  196. }
  197. }
  198. void os_daemonize(void)
  199. {
  200. if (daemonize) {
  201. pid_t pid;
  202. int fds[2];
  203. if (pipe(fds) == -1) {
  204. exit(1);
  205. }
  206. pid = fork();
  207. if (pid > 0) {
  208. uint8_t status;
  209. ssize_t len;
  210. close(fds[1]);
  211. do {
  212. len = read(fds[0], &status, 1);
  213. } while (len < 0 && errno == EINTR);
  214. /* only exit successfully if our child actually wrote
  215. * a one-byte zero to our pipe, upon successful init */
  216. exit(len == 1 && status == 0 ? 0 : 1);
  217. } else if (pid < 0) {
  218. exit(1);
  219. }
  220. close(fds[0]);
  221. daemon_pipe = fds[1];
  222. qemu_set_cloexec(daemon_pipe);
  223. setsid();
  224. pid = fork();
  225. if (pid > 0) {
  226. exit(0);
  227. } else if (pid < 0) {
  228. exit(1);
  229. }
  230. umask(027);
  231. signal(SIGTSTP, SIG_IGN);
  232. signal(SIGTTOU, SIG_IGN);
  233. signal(SIGTTIN, SIG_IGN);
  234. }
  235. }
  236. void os_setup_post(void)
  237. {
  238. int fd = 0;
  239. if (daemonize) {
  240. if (chdir("/")) {
  241. error_report("not able to chdir to /: %s", strerror(errno));
  242. exit(1);
  243. }
  244. TFR(fd = qemu_open_old("/dev/null", O_RDWR));
  245. if (fd == -1) {
  246. exit(1);
  247. }
  248. }
  249. change_root();
  250. change_process_uid();
  251. if (daemonize) {
  252. uint8_t status = 0;
  253. ssize_t len;
  254. dup2(fd, 0);
  255. dup2(fd, 1);
  256. /* In case -D is given do not redirect stderr to /dev/null */
  257. if (!qemu_log_enabled()) {
  258. dup2(fd, 2);
  259. }
  260. close(fd);
  261. do {
  262. len = write(daemon_pipe, &status, 1);
  263. } while (len < 0 && errno == EINTR);
  264. if (len != 1) {
  265. exit(1);
  266. }
  267. }
  268. }
  269. void os_set_line_buffering(void)
  270. {
  271. setvbuf(stdout, NULL, _IOLBF, 0);
  272. }
  273. bool is_daemonized(void)
  274. {
  275. return daemonize;
  276. }
  277. int os_set_daemonize(bool d)
  278. {
  279. daemonize = d;
  280. return 0;
  281. }
  282. int os_mlock(void)
  283. {
  284. #ifdef HAVE_MLOCKALL
  285. int ret = 0;
  286. ret = mlockall(MCL_CURRENT | MCL_FUTURE);
  287. if (ret < 0) {
  288. error_report("mlockall: %s", strerror(errno));
  289. }
  290. return ret;
  291. #else
  292. return -ENOSYS;
  293. #endif
  294. }