quark-noroot-20191003-3c7049e.diff 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. From d91f68b56a4fd673786e9e4df0088642f3b186ff Mon Sep 17 00:00:00 2001
  2. From: codesoap <codesoap@mailbox.org>
  3. Date: Thu, 3 Oct 2019 17:00:49 +0200
  4. Subject: [PATCH] patch: noroot
  5. Don't require or allow root to run quark.
  6. ---
  7. main.c | 50 ++------------------------------------------------
  8. quark.1 | 13 +------------
  9. sock.c | 7 +------
  10. sock.h | 2 +-
  11. 4 files changed, 5 insertions(+), 67 deletions(-)
  12. diff --git a/main.c b/main.c
  13. index c1ff489..583e343 100644
  14. --- a/main.c
  15. +++ b/main.c
  16. @@ -1,9 +1,7 @@
  17. /* See LICENSE file for copyright and license details. */
  18. #include <errno.h>
  19. -#include <grp.h>
  20. #include <limits.h>
  21. #include <netinet/in.h>
  22. -#include <pwd.h>
  23. #include <regex.h>
  24. #include <signal.h>
  25. #include <sys/resource.h>
  26. @@ -163,7 +161,7 @@ err:
  27. static void
  28. usage(void)
  29. {
  30. - const char *opts = "[-u user] [-g group] [-n num] [-d dir] [-l] "
  31. + const char *opts = "[-n num] [-d dir] [-l] "
  32. "[-i file] [-v vhost] ... [-m map] ...";
  33. die("usage: %s -h host -p port %s\n"
  34. @@ -174,8 +172,6 @@ usage(void)
  35. int
  36. main(int argc, char *argv[])
  37. {
  38. - struct group *grp = NULL;
  39. - struct passwd *pwd = NULL;
  40. struct rlimit rlim;
  41. struct sockaddr_storage in_sa;
  42. pid_t cpid, wpid, spid;
  43. @@ -188,8 +184,6 @@ main(int argc, char *argv[])
  44. /* defaults */
  45. int maxnprocs = 512;
  46. char *servedir = ".";
  47. - char *user = "nobody";
  48. - char *group = "nogroup";
  49. s.host = s.port = NULL;
  50. s.vhost = NULL;
  51. @@ -202,9 +196,6 @@ main(int argc, char *argv[])
  52. case 'd':
  53. servedir = EARGF(usage());
  54. break;
  55. - case 'g':
  56. - group = EARGF(usage());
  57. - break;
  58. case 'h':
  59. s.host = EARGF(usage());
  60. break;
  61. @@ -241,9 +232,6 @@ main(int argc, char *argv[])
  62. case 'U':
  63. udsname = EARGF(usage());
  64. break;
  65. - case 'u':
  66. - user = EARGF(usage());
  67. - break;
  68. case 'v':
  69. if (spacetok(EARGF(usage()), tok, 4) || !tok[0] || !tok[1] ||
  70. !tok[2]) {
  71. @@ -291,25 +279,13 @@ main(int argc, char *argv[])
  72. die("setrlimit RLIMIT_NPROC:");
  73. }
  74. - /* validate user and group */
  75. - errno = 0;
  76. - if (user && !(pwd = getpwnam(user))) {
  77. - die("getpwnam '%s': %s", user, errno ? strerror(errno) :
  78. - "Entry not found");
  79. - }
  80. - errno = 0;
  81. - if (group && !(grp = getgrnam(group))) {
  82. - die("getgrnam '%s': %s", group, errno ? strerror(errno) :
  83. - "Entry not found");
  84. - }
  85. -
  86. /* Open a new process group */
  87. setpgid(0,0);
  88. handlesignals(sigcleanup);
  89. /* bind socket */
  90. - insock = udsname ? sock_get_uds(udsname, pwd->pw_uid, grp->gr_gid) :
  91. + insock = udsname ? sock_get_uds(udsname) :
  92. sock_get_ips(s.host, s.port);
  93. switch (cpid = fork()) {
  94. @@ -329,24 +305,9 @@ main(int argc, char *argv[])
  95. eunveil(servedir, "r");
  96. eunveil(NULL, NULL);
  97. - /* chroot */
  98. if (chdir(servedir) < 0) {
  99. die("chdir '%s':", servedir);
  100. }
  101. - if (chroot(".") < 0) {
  102. - die("chroot .:");
  103. - }
  104. -
  105. - /* drop root */
  106. - if (grp && setgroups(1, &(grp->gr_gid)) < 0) {
  107. - die("setgroups:");
  108. - }
  109. - if (grp && setgid(grp->gr_gid) < 0) {
  110. - die("setgid:");
  111. - }
  112. - if (pwd && setuid(pwd->pw_uid) < 0) {
  113. - die("setuid:");
  114. - }
  115. if (udsname) {
  116. epledge("stdio rpath proc unix", NULL);
  117. @@ -354,13 +315,6 @@ main(int argc, char *argv[])
  118. epledge("stdio rpath proc inet", NULL);
  119. }
  120. - if (getuid() == 0) {
  121. - die("Won't run as root user", argv0);
  122. - }
  123. - if (getgid() == 0) {
  124. - die("Won't run as root group", argv0);
  125. - }
  126. -
  127. /* accept incoming connections */
  128. while (1) {
  129. in_sa_len = sizeof(in_sa);
  130. diff --git a/quark.1 b/quark.1
  131. index ce315b5..e45140c 100644
  132. --- a/quark.1
  133. +++ b/quark.1
  134. @@ -35,13 +35,8 @@ is a simple HTTP GET/HEAD-only web server for static content.
  135. .It Fl d Ar dir
  136. Serve
  137. .Ar dir
  138. -after chrooting into it.
  139. +after changing into it.
  140. The default is ".".
  141. -.It Fl g Ar group
  142. -Set group ID when dropping privileges, and in socket mode the group of the
  143. -socket file, to the ID of
  144. -.Ar group .
  145. -The default is "nogroup".
  146. .It Fl h Ar host
  147. Use
  148. .Ar host
  149. @@ -86,12 +81,6 @@ redirects on non-standard ports.
  150. Create the UNIX-domain socket
  151. .Ar file ,
  152. listen on it for incoming connections and remove it on exit.
  153. -.It Fl u Ar user
  154. -Set user ID when dropping privileges,
  155. -and in socket mode the user of the socket file,
  156. -to the ID of
  157. -.Ar user .
  158. -The default is "nobody".
  159. .It Fl v Ar vhost
  160. Add the virtual host specified by
  161. .Ar vhost ,
  162. diff --git a/sock.c b/sock.c
  163. index 7000738..31960c5 100644
  164. --- a/sock.c
  165. +++ b/sock.c
  166. @@ -68,7 +68,7 @@ sock_rem_uds(const char *udsname)
  167. }
  168. int
  169. -sock_get_uds(const char *udsname, uid_t uid, gid_t gid)
  170. +sock_get_uds(const char *udsname)
  171. {
  172. struct sockaddr_un addr = {
  173. .sun_family = AF_UNIX,
  174. @@ -99,11 +99,6 @@ sock_get_uds(const char *udsname, uid_t uid, gid_t gid)
  175. die("chmod:");
  176. }
  177. - if (chown(udsname, uid, gid) < 0) {
  178. - sock_rem_uds(udsname);
  179. - die("chown:");
  180. - }
  181. -
  182. return insock;
  183. }
  184. diff --git a/sock.h b/sock.h
  185. index a39aec9..4f790f6 100644
  186. --- a/sock.h
  187. +++ b/sock.h
  188. @@ -8,7 +8,7 @@
  189. int sock_get_ips(const char *, const char *);
  190. void sock_rem_uds(const char *);
  191. -int sock_get_uds(const char *, uid_t, gid_t);
  192. +int sock_get_uds(const char *);
  193. int sock_set_timeout(int, int);
  194. int sock_get_inaddr_str(struct sockaddr_storage *, char *, size_t);
  195. --
  196. 2.21.0