qfile-unpacker.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_ROOT "/home/user/QubesIncoming"
  18. int prepare_creds_return_uid(const char *username)
  19. {
  20. const struct passwd *pwd;
  21. pwd = getpwnam(username);
  22. if (!pwd) {
  23. perror("getpwnam");
  24. exit(1);
  25. }
  26. setenv("HOME", pwd->pw_dir, 1);
  27. setenv("USER", username, 1);
  28. if (setgid(pwd->pw_gid) < 0)
  29. gui_fatal("Error setting group permissions");
  30. if (initgroups(username, 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_uid;
  35. }
  36. int main(int argc __attribute((__unused__)), char ** argv __attribute__((__unused__)))
  37. {
  38. char *incoming_dir;
  39. int uid, ret;
  40. pid_t pid;
  41. const char *remote_domain;
  42. char *procdir_path;
  43. int procfs_fd;
  44. uid = prepare_creds_return_uid("user");
  45. remote_domain = getenv("QREXEC_REMOTE_DOMAIN");
  46. if (!remote_domain) {
  47. gui_fatal("Cannot get remote domain name");
  48. exit(1);
  49. }
  50. mkdir(INCOMING_DIR_ROOT, 0700);
  51. if (asprintf(&incoming_dir, "%s/%s", INCOMING_DIR_ROOT, remote_domain) < 0)
  52. gui_fatal("Error allocating memory");
  53. mkdir(incoming_dir, 0700);
  54. if (chdir(incoming_dir))
  55. gui_fatal("Error chdir to %s", incoming_dir);
  56. if (mount(".", ".", NULL, MS_BIND | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL) < 0)
  57. gui_fatal("Failed to mount a directory %s", incoming_dir);
  58. /* parse the input in unprivileged child process, parent will hold root
  59. * access to unmount incoming dir */
  60. switch (pid=fork()) {
  61. case -1:
  62. gui_fatal("Failed to create new process");
  63. case 0:
  64. if (asprintf(&procdir_path, "/proc/%d/fd", getpid()) < 0) {
  65. gui_fatal("Error allocating memory");
  66. }
  67. procfs_fd = open(procdir_path, O_DIRECTORY | O_RDONLY);
  68. if (procfs_fd < 0)
  69. gui_fatal("Failed to open /proc");
  70. free(procdir_path);
  71. set_procfs_fd(procfs_fd);
  72. if (chroot("."))
  73. gui_fatal("Error chroot to %s", incoming_dir);
  74. if (setuid(uid) < 0) {
  75. /* no kdialog inside chroot */
  76. perror("setuid");
  77. exit(1);
  78. }
  79. return do_unpack();
  80. }
  81. if (waitpid(pid, &ret, 0) < 0) {
  82. gui_fatal("Failed to wait for child process");
  83. }
  84. if (umount2(".", MNT_DETACH) < 0)
  85. gui_fatal("Cannot umount incoming directory");
  86. return ret;
  87. }