Blame SOURCES/0066-su-clean-up-groups-initialization.patch

531551
From b2a41801904c4b281a717dde7f5e146cbd4500b3 Mon Sep 17 00:00:00 2001
531551
From: Karel Zak <kzak@redhat.com>
531551
Date: Mon, 15 Feb 2016 13:55:37 +0100
531551
Subject: [PATCH 66/84] su: clean up groups initialization
531551
531551
This patch does not change any su/runuser behaviour, code changes:
531551
531551
* don't use huge groups[NGROUPS_MAX]; the array has 256k, but we need
531551
  it only occasionally when -G/-g specified.
531551
531551
* the current code uses groups[0] for -g and the rest for -G, this patch adds
531551
  'gid' to remember -g argument to avoid memmove()
531551
531551
* add function add_supp_group() to simplify su_main()
531551
531551
* add note about -G and -g relation to the man pages (undocumented now)
531551
531551
Upstream: http://github.com/karelzak/util-linux/commit/c619d3d167115990e9228b27851e0cc2faa8f936
531551
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1304426
531551
Signed-off-by: Karel Zak <kzak@redhat.com>
531551
---
531551
 login-utils/runuser.1   |  5 ++--
531551
 login-utils/su-common.c | 68 +++++++++++++++++++++++++++----------------------
531551
 login-utils/su.1        |  5 ++--
531551
 3 files changed, 44 insertions(+), 34 deletions(-)
531551
531551
diff --git a/login-utils/runuser.1 b/login-utils/runuser.1
531551
index 7201ff0..d82dbb0 100644
531551
--- a/login-utils/runuser.1
531551
+++ b/login-utils/runuser.1
531551
@@ -75,8 +75,9 @@ shell.
531551
 \fB\-g\fR, \fB\-\-group\fR=\fIgroup\fR\fR
531551
 specify the primary group, this option is allowed for root user only
531551
 .TP
531551
-\fB\-G\fR, \fB\-\-supp-group\fR=\fIgroup\fR\fR
531551
-specify a supplemental group, this option is allowed for root user only
531551
+.BR \-G , " \-\-supp\-group" = \fIgroup
531551
+Specify a supplemental group.  This option is available to the root user only.  The first specified
531551
+supplementary group is also used as a primary group if the option \fB\-\-group\fR is unspecified.
531551
 .TP
531551
 \fB\-\fR, \fB\-l\fR, \fB\-\-login\fR
531551
 Starts the shell as login shell with an environment similar to a real
531551
diff --git a/login-utils/su-common.c b/login-utils/su-common.c
531551
index dd87804..d53d690 100644
531551
--- a/login-utils/su-common.c
531551
+++ b/login-utils/su-common.c
531551
@@ -535,7 +535,7 @@ modify_environment (const struct passwd *pw, const char *shell)
531551
 /* Become the user and group(s) specified by PW.  */
531551
 
531551
 static void
531551
-init_groups (const struct passwd *pw, gid_t *groups, int num_groups)
531551
+init_groups (const struct passwd *pw, gid_t *groups, size_t num_groups)
531551
 {
531551
   int retval;
531551
 
531551
@@ -707,6 +707,28 @@ evaluate_uid(void)
531551
   return (uid_t) 0 == ruid && ruid == euid ? 0 : 1;
531551
 }
531551
 
531551
+static gid_t
531551
+add_supp_group(const char *name, gid_t **groups, size_t *ngroups)
531551
+{
531551
+  struct group *gr;
531551
+
531551
+  if (*ngroups >= NGROUPS_MAX)
531551
+    errx(EXIT_FAILURE,
531551
+	P_("specifying more than %d supplemental group is not possible",
531551
+	   "specifying more than %d supplemental groups is not possible",
531551
+	     NGROUPS_MAX - 1), NGROUPS_MAX - 1);
531551
+
531551
+  gr = getgrnam(name);
531551
+  if (!gr)
531551
+    errx(EXIT_FAILURE, _("group %s does not exist"), name);
531551
+
531551
+  *groups = xrealloc(*groups, sizeof(gid_t) * (*ngroups + 1));
531551
+  (*groups)[*ngroups] = gr->gr_gid;
531551
+  (*ngroups)++;
531551
+
531551
+  return gr->gr_gid;
531551
+}
531551
+
531551
 int
531551
 su_main (int argc, char **argv, int mode)
531551
 {
531551
@@ -717,10 +739,12 @@ su_main (int argc, char **argv, int mode)
531551
   char *shell = NULL;
531551
   struct passwd *pw;
531551
   struct passwd pw_copy;
531551
-  struct group *gr;
531551
-  gid_t groups[NGROUPS_MAX];
531551
-  int num_supp_groups = 0;
531551
-  int use_gid = 0;
531551
+
531551
+  gid_t *groups = NULL;
531551
+  size_t ngroups = 0;
531551
+  bool use_supp = false;
531551
+  bool use_gid = false;
531551
+  gid_t gid = 0;
531551
 
531551
   static const struct option longopts[] = {
531551
     {"command", required_argument, NULL, 'c'},
531551
@@ -765,23 +789,13 @@ su_main (int argc, char **argv, int mode)
531551
 	  break;
531551
 
531551
 	case 'g':
531551
-	  gr = getgrnam(optarg);
531551
-	  if (!gr)
531551
-	    errx(EXIT_FAILURE, _("group %s does not exist"), optarg);
531551
-	  use_gid = 1;
531551
-	  groups[0] = gr->gr_gid;
531551
+	  use_gid = true;
531551
+	  gid = add_supp_group(optarg, &groups, &ngroups);
531551
 	  break;
531551
 
531551
 	case 'G':
531551
-	  num_supp_groups++;
531551
-	  if (num_supp_groups >= NGROUPS_MAX)
531551
-	     errx(EXIT_FAILURE,
531551
-		  _("can't specify more than %d supplemental groups"),
531551
-		  NGROUPS_MAX - 1);
531551
-	  gr = getgrnam(optarg);
531551
-	  if (!gr)
531551
-	    errx(EXIT_FAILURE, _("group %s does not exist"), optarg);
531551
-	  groups[num_supp_groups] = gr->gr_gid;
531551
+	  use_supp = true;
531551
+	  add_supp_group(optarg, &groups, &ngroups);
531551
 	  break;
531551
 
531551
 	case 'l':
531551
@@ -852,7 +866,7 @@ su_main (int argc, char **argv, int mode)
531551
     break;
531551
   }
531551
 
531551
-  if ((num_supp_groups || use_gid) && restricted)
531551
+  if ((use_supp || use_gid) && restricted)
531551
     errx(EXIT_FAILURE, _("only root can specify alternative groups"));
531551
 
531551
   logindefs_load_defaults = load_config;
531551
@@ -878,16 +892,10 @@ su_main (int argc, char **argv, int mode)
531551
 			  : DEFAULT_SHELL);
531551
   endpwent ();
531551
 
531551
-  if (num_supp_groups && !use_gid)
531551
-  {
531551
-    pw->pw_gid = groups[1];
531551
-    memmove (groups, groups + 1, sizeof(gid_t) * num_supp_groups);
531551
-  }
531551
-  else if (use_gid)
531551
-  {
531551
+  if (use_supp && !use_gid)
531551
     pw->pw_gid = groups[0];
531551
-    num_supp_groups++;
531551
-  }
531551
+  else if (use_gid)
531551
+    pw->pw_gid = gid;
531551
 
531551
   authenticate (pw);
531551
 
531551
@@ -912,7 +920,7 @@ su_main (int argc, char **argv, int mode)
531551
     shell = xstrdup (shell ? shell : pw->pw_shell);
531551
   }
531551
 
531551
-  init_groups (pw, groups, num_supp_groups);
531551
+  init_groups (pw, groups, ngroups);
531551
 
531551
   if (!simulate_login || command)
531551
     suppress_pam_info = 1;		/* don't print PAM info messages */
531551
diff --git a/login-utils/su.1 b/login-utils/su.1
531551
index eab1a6f..1f69868 100644
531551
--- a/login-utils/su.1
531551
+++ b/login-utils/su.1
531551
@@ -62,8 +62,9 @@ shell.
531551
 \fB\-g\fR, \fB\-\-group\fR=\fIgroup\fR\fR
531551
 specify the primary group, this option is allowed for root user only
531551
 .TP
531551
-\fB\-G\fR, \fB\-\-supp-group\fR=\fIgroup\fR\fR
531551
-specify a supplemental group, this option is allowed for root user only
531551
+.BR \-G , " \-\-supp\-group" = \fIgroup
531551
+Specify a supplemental group.  This option is available to the root user only.  The first specified
531551
+supplementary group is also used as a primary group if the option \fB\-\-group\fR is unspecified.
531551
 .TP
531551
 \fB\-\fR, \fB\-l\fR, \fB\-\-login\fR
531551
 Starts the shell as login shell with an environment similar to a real
531551
-- 
531551
2.7.4
531551