qfile-unpacker.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #define _GNU_SOURCE
  2. #include <grp.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pwd.h>
  7. #include <sys/stat.h>
  8. #include <sys/mount.h>
  9. #include <sys/wait.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/fsuid.h>
  14. #include <gui-fatal.h>
  15. #include <errno.h>
  16. #include <libqubes-rpc-filecopy.h>
  17. #define INCOMING_DIR_NAME "QubesIncoming"
  18. char *prepare_creds_return_dir(int uid)
  19. {
  20. const struct passwd *pwd;
  21. pwd = getpwuid(uid);
  22. if (!pwd) {
  23. perror("getpwuid");
  24. exit(1);
  25. }
  26. setenv("HOME", pwd->pw_dir, 1);
  27. setenv("USER", pwd->pw_name, 1);
  28. if (setgid(pwd->pw_gid) < 0)
  29. gui_fatal("Error setting group permissions");
  30. if (initgroups(pwd->pw_name, pwd->pw_gid) < 0)
  31. gui_fatal("Error initializing groups");
  32. if (setfsuid(pwd->pw_uid) < 0)
  33. gui_fatal("Error setting filesystem level permissions");
  34. return pwd->pw_dir;
  35. }
  36. int main(int argc __attribute((__unused__)), char ** argv __attribute__((__unused__)))
  37. {
  38. char *home_dir;
  39. char *incoming_dir_root;
  40. char *incoming_dir;
  41. int uid, ret;
  42. pid_t pid;
  43. const char *remote_domain;
  44. char *procdir_path;
  45. int procfs_fd;
  46. uid = getuid();
  47. home_dir = prepare_creds_return_dir(uid);
  48. remote_domain = getenv("QREXEC_REMOTE_DOMAIN");
  49. if (!remote_domain) {
  50. gui_fatal("Cannot get remote domain name");
  51. }
  52. if (asprintf(&incoming_dir_root, "%s/%s", home_dir, INCOMING_DIR_NAME) < 0) {
  53. gui_fatal("Error allocating memory");
  54. }
  55. mkdir(incoming_dir_root, 0700);
  56. if (asprintf(&incoming_dir, "%s/%s", incoming_dir_root, remote_domain) < 0)
  57. gui_fatal("Error allocating memory");
  58. mkdir(incoming_dir, 0700);
  59. if (chdir(incoming_dir))
  60. gui_fatal("Error chdir to %s", incoming_dir);
  61. if (mount(".", ".", NULL, MS_BIND | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL) < 0)
  62. gui_fatal("Failed to mount a directory %s", incoming_dir);
  63. /* parse the input in unprivileged child process, parent will hold root
  64. * access to unmount incoming dir */
  65. switch (pid=fork()) {
  66. case -1:
  67. gui_fatal("Failed to create new process");
  68. case 0:
  69. if (asprintf(&procdir_path, "/proc/%d/fd", getpid()) < 0) {
  70. gui_fatal("Error allocating memory");
  71. }
  72. procfs_fd = open(procdir_path, O_DIRECTORY | O_RDONLY);
  73. if (procfs_fd < 0)
  74. perror("Failed to open /proc");
  75. else
  76. set_procfs_fd(procfs_fd);
  77. free(procdir_path);
  78. if (chroot("."))
  79. gui_fatal("Error chroot to %s", incoming_dir);
  80. if (setuid(uid) < 0) {
  81. /* no kdialog inside chroot */
  82. perror("setuid");
  83. exit(1);
  84. }
  85. return do_unpack();
  86. }
  87. if (waitpid(pid, &ret, 0) < 0) {
  88. gui_fatal("Failed to wait for child process");
  89. }
  90. if (umount2(".", MNT_DETACH) < 0)
  91. gui_fatal("Cannot umount incoming directory");
  92. if (!WIFEXITED(ret)) {
  93. gui_fatal("Child process exited abnormally");
  94. }
  95. return WEXITSTATUS(ret);
  96. }