Explorar o código

s-s-d: Properly set the supplementary groups on --chuid

Set the supplementary groups if the real user or group are different than
the ones we should switch to.

Closes: #462075
Guillem Jover %!s(int64=18) %!d(string=hai) anos
pai
achega
2a7fb2bfef
Modificáronse 3 ficheiros con 28 adicións e 29 borrados
  1. 6 0
      ChangeLog
  2. 5 0
      debian/changelog
  3. 17 29
      utils/start-stop-daemon.c

+ 6 - 0
ChangeLog

@@ -1,3 +1,9 @@
+2008-01-22  Guillem Jover  <guillem@debian.org>
+
+	* utils/start-stop-daemon.c (gid_in_current_groups): Remove function.
+	(main): Call initgroups if the real user or group are different than
+	the ones we should switch to. Call setgid before initgroups.
+
 2008-01-22  Raphael Hertzog  <hertzog@debian.org>
 
 	* scripts/dpkg-genchanges.pl, scripts/dpkg-gencontrol.pl,

+ 5 - 0
debian/changelog

@@ -1,9 +1,14 @@
 dpkg (1.14.16.3) UNRELEASED; urgency=low
 
+  [ Raphael Hertzog ]
   * Remove the ":utf8" layer that utf8-encodes already valid utf8.
     Closes: #462098
   * Disable variable substitution in dpkg-genchanges. Closes: #462079, #462089
 
+  [ Guillem Jover ]
+  * Make start-stop-daemon set the supplementary groups if the real user or
+    group are different than the ones we should switch to. Closes: #462075
+
  -- Raphael Hertzog <hertzog@debian.org>  Tue, 22 Jan 2008 18:15:42 +0100
 
 dpkg (1.14.16.2) unstable; urgency=low

+ 17 - 29
utils/start-stop-daemon.c

@@ -322,27 +322,6 @@ clear(struct pid_list **list)
 	*list = NULL;
 }
 
-static int
-gid_in_current_groups(gid_t gid)
-{
-	gid_t *gids;
-	int i, ngroups;
-
-	ngroups = getgroups(0, NULL);
-	gids = xmalloc(ngroups * sizeof(gid_t));
-	getgroups(ngroups, gids);
-
-	for (i = 0; i < ngroups; i++) {
-		if (gid == gids[i]) {
-			free(gids);
-			return 1;
-		}
-	}
-
-	free(gids);
-	return 0;
-}
-
 static void
 do_help(void)
 {
@@ -1285,6 +1264,8 @@ int
 main(int argc, char **argv)
 {
 	int devnull_fd = -1;
+	gid_t rgid;
+	uid_t ruid;
 #ifdef HAVE_TIOCNOTTY
 	int tty_fd = -1;
 #endif
@@ -1413,18 +1394,25 @@ main(int argc, char **argv)
 	if (chdir(changedir) < 0)
 		fatal("Unable to chdir() to %s", changedir);
 
-	if (changegroup != NULL && *changegroup != '\0' &&
-	    getgid() != (gid_t)runas_gid) {
-		if (!gid_in_current_groups(runas_gid))
+	rgid = getgid();
+	ruid = getuid();
+	if (changegroup != NULL) {
+		if (rgid != (gid_t)runas_gid)
+			if (setgid(runas_gid))
+				fatal("Unable to set gid to %d", runas_gid);
+
+		/* We assume that if our real user and group are the same as
+		 * the ones we should switch to, the supplementary groups
+		 * will be already in place. */
+		if (rgid != (gid_t)runas_gid || ruid != (uid_t)runas_uid)
 			if (initgroups(changeuser, runas_gid))
 				fatal("Unable to set initgroups() with gid %d",
 				      runas_gid);
-		if (setgid(runas_gid))
-			fatal("Unable to set gid to %d", runas_gid);
 	}
-	if (changeuser != NULL && getuid() != (uid_t)runas_uid) {
-		if (setuid(runas_uid))
-			fatal("Unable to set uid to %s", changeuser);
+	if (changeuser != NULL) {
+		if (ruid != (uid_t)runas_uid)
+			if (setuid(runas_uid))
+				fatal("Unable to set uid to %s", changeuser);
 	}
 
 	if (background) {