qfile-unpacker.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. perror("Failed to open /proc");
  70. else
  71. set_procfs_fd(procfs_fd);
  72. free(procdir_path);
  73. if (chroot("."))
  74. gui_fatal("Error chroot to %s", incoming_dir);
  75. if (setuid(uid) < 0) {
  76. /* no kdialog inside chroot */
  77. perror("setuid");
  78. exit(1);
  79. }
  80. return do_unpack();
  81. }
  82. if (waitpid(pid, &ret, 0) < 0) {
  83. gui_fatal("Failed to wait for child process");
  84. }
  85. if (umount2(".", MNT_DETACH) < 0)
  86. gui_fatal("Cannot umount incoming directory");
  87. if (!WIFEXITED(ret)) {
  88. gui_fatal("Child process exited abnormally");
  89. }
  90. return WEXITSTATUS(ret);
  91. }