[patch] minor shrinking of allnoconfig with suid handling enabled

Bernhard Fischer rep.nop at aon.at
Wed Mar 22 17:26:19 UTC 2006


Hi,

attached patch saves a very few bytes off allnoconfig with SUID and
SUID_CONFIGFILE enabled while also using proper style (whitespace
cleanup).

   text    data     bss     dec     hex filename
   5864     388      32    6284    188c busybox.oorig-4.0
   5860     388      32    6280    1888 busybox

Opinions?

PS: only compile-tested.
PPS: landley, i know that you'll dislike that this patch still uses the
preprocessor to filter out some stuff.. :)
PPPS: the ...no-whitespace.diff is just there for easier review.
-------------- next part --------------
Index: applets/applets.c
===================================================================
--- applets/applets.c	(revision 14597)
+++ applets/applets.c	(working copy)
@@ -54,7 +54,7 @@ static struct BB_applet *applet_using;
 const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
 
 
-#ifdef CONFIG_FEATURE_SUID_CONFIG
+#if ENABLE_FEATURE_SUID_CONFIG
 
 #include <sys/stat.h>
 #include <ctype.h>
@@ -144,7 +144,7 @@ static void parse_config_file(void)
 	char *s;
 	char *e;
 	int i, lc, section;
-	char buffer[256];
+	RESERVE_CONFIG_BUFFER(buffer, 256);
 	struct stat st;
 
 	assert(!suid_config);		/* Should be set to NULL by bss init. */
@@ -155,7 +155,7 @@ static void parse_config_file(void)
 		|| (st.st_mode & (S_IWGRP | S_IWOTH))	/* Writable by non-root? */
 		|| !(f = fopen(config_file, "r"))		/* Can not open? */
 		) {
-		return;
+		goto out;
 	}
 
 	suid_cfg_readable = 1;
@@ -171,7 +171,7 @@ static void parse_config_file(void)
 			}
 			fclose(f);
 			suid_config = sct_head;	/* Success, so set the pointer. */
-			return;
+			goto out;
 		}
 
 		lc++;					/* Got a (partial) line. */
@@ -256,12 +256,11 @@ static void parse_config_file(void)
 				e = (char *) bb_skip_whitespace(e+1);
 
 				for (i=0 ; i < 3 ; i++) {
-					const char *q;
-					if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
+					if (!*(err = strchrnul(mode_chars + 5*i, *e++))) {
 						parse_error("mode");
 					}
 					/* Adjust by -i to account for nul. */
-					sct->m_mode |= mode_mask[(q - mode_chars) - i];
+					sct->m_mode |= mode_mask[(err - mode_chars) - i];
 				}
 
 				/* Now get the the user/group info. */
@@ -326,6 +325,8 @@ static void parse_config_file(void)
 		free(sct_head);
 		sct_head = sct;
 	}
+out:
+	RELEASE_CONFIG_BUFFER(buffer);
 	return;
 }
 
@@ -333,65 +334,64 @@ static void parse_config_file(void)
 #define parse_config_file()
 #endif /* CONFIG_FEATURE_SUID_CONFIG */
 
-#ifdef CONFIG_FEATURE_SUID
+#if ENABLE_FEATURE_SUID
 static void check_suid (struct BB_applet *applet)
 {
   uid_t ruid = getuid ();               /* real [ug]id */
   uid_t rgid = getgid ();
 
-#ifdef CONFIG_FEATURE_SUID_CONFIG
-  if (suid_cfg_readable) {
-	struct BB_suid_config *sct;
+  if (ENABLE_FEATURE_SUID_CONFIG) {
+	  if (suid_cfg_readable) {
+		struct BB_suid_config *sct;
 
-	for (sct = suid_config; sct; sct = sct->m_next) {
-	  if (sct->m_applet == applet)
-		break;
-	}
-	if (sct) {
-	  mode_t m = sct->m_mode;
+		for (sct = suid_config; sct; sct = sct->m_next) {
+		  if (sct->m_applet == applet)
+			break;
+		}
+		if (sct) {
+		  mode_t m = sct->m_mode;
 
-	  if (sct->m_uid == ruid)       /* same uid */
-		m >>= 6;
-	  else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid))  /* same group / in group */
-		m >>= 3;
+		  if (sct->m_uid == ruid)       /* same uid */
+			m >>= 6;
+		  else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid))
+			/* same group / in group */
+			m >>= 3;
 
-	  if (!(m & S_IXOTH))           /* is x bit not set ? */
-		bb_error_msg_and_die ("You have no permission to run this applet!");
+		  if (!(m & S_IXOTH))           /* is x bit not set ? */
+			bb_error_msg_and_die ("You have no permission to run this applet!");
 
-	  if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {     /* *both* have to be set for sgid */
-		if (setegid (sct->m_gid))
-		  bb_error_msg_and_die
-			("BusyBox binary has insufficient rights to set proper GID for applet!");
-	  } else
-		setgid (rgid);                  /* no sgid -> drop */
+		  if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {     /* *both* have to be set for sgid */
+			if (setegid (sct->m_gid))
+			  bb_error_msg_and_die
+				("BusyBox binary has insufficient rights to set proper GID for applet!");
+		  } else
+			setgid (rgid);                  /* no sgid -> drop */
 
-	  if (sct->m_mode & S_ISUID) {
-		if (seteuid (sct->m_uid))
-		  bb_error_msg_and_die
-			("BusyBox binary has insufficient rights to set proper UID for applet!");
-	  } else
-		setuid (ruid);                  /* no suid -> drop */
-	} else {
-		/* default: drop all priviledges */
-	  setgid (rgid);
-	  setuid (ruid);
-	}
-	return;
-  } else {
-#ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
-	static int onetime = 0;
+		  if (sct->m_mode & S_ISUID) {
+			if (seteuid (sct->m_uid))
+			  bb_error_msg_and_die
+				("BusyBox binary has insufficient rights to set proper UID for applet!");
+		  } else
+			setuid (ruid);                  /* no suid -> drop */
+		} else {
+			/* default: drop all priviledges */
+		  setgid (rgid);
+		  setuid (ruid);
+		}
+		return;
+	  } else if (!ENABLE_FEATURE_SUID_CONFIG_QUIET) {
+		static int onetime = 0;
 
-	if (!onetime) {
-	  onetime = 1;
-	  fprintf (stderr, "Using fallback suid method\n");
-	}
-#endif
+		if (!onetime) {
+		  onetime = 1;
+		  fprintf (stderr, "Using fallback suid method\n");
+		}
+	  }
   }
-#endif
 
   if (applet->need_suid == _BB_SUID_ALWAYS) {
 	if (geteuid () != 0)
-	  bb_error_msg_and_die ("This applet requires root priviledges!");
+	  bb_error_msg_and_die ("This applet requires root privileges!");
   } else if (applet->need_suid == _BB_SUID_NEVER) {
 	setgid (rgid);                          /* drop all priviledges */
 	setuid (ruid);
@@ -399,7 +399,7 @@ static void check_suid (struct BB_applet
 }
 #else
 #define check_suid(x)
-#endif /* CONFIG_FEATURE_SUID */
+#endif /* ENABLE_FEATURE_SUID */
 
 
 
@@ -444,15 +444,19 @@ struct BB_applet *find_applet_by_name (c
 
 void run_applet_by_name (const char *name, int argc, char **argv)
 {
-	if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file ();
+	if (ENABLE_FEATURE_SUID_CONFIG)
+		parse_config_file ();
 
-	if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
+	if (!strncmp(name, "busybox", 7))
+		busybox_main(argc, argv);
 	/* Do a binary search to find the applet entry given the name. */
 	applet_using = find_applet_by_name(name);
-	if(applet_using) {
+	if (applet_using) {
 		bb_applet_name = applet_using->name;
-		if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage ();
-		if(ENABLE_FEATURE_SUID) check_suid (applet_using);
+		if (argc==2 && !strcmp(argv[1], "--help"))
+			bb_show_usage ();
+		if (ENABLE_FEATURE_SUID)
+			check_suid (applet_using);
 		exit ((*(applet_using->main)) (argc, argv));
 	}
 }
-------------- next part --------------
Index: applets/applets.c
===================================================================
--- applets/applets.c	(revision 14597)
+++ applets/applets.c	(working copy)
@@ -54,7 +54,7 @@ static struct BB_applet *applet_using;
 const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
 
 
-#ifdef CONFIG_FEATURE_SUID_CONFIG
+#if ENABLE_FEATURE_SUID_CONFIG
 
 #include <sys/stat.h>
 #include <ctype.h>
@@ -144,7 +144,7 @@ static void parse_config_file(void)
 	char *s;
 	char *e;
 	int i, lc, section;
-	char buffer[256];
+	RESERVE_CONFIG_BUFFER(buffer, 256);
 	struct stat st;
 
 	assert(!suid_config);		/* Should be set to NULL by bss init. */
@@ -155,7 +155,7 @@ static void parse_config_file(void)
 		|| (st.st_mode & (S_IWGRP | S_IWOTH))	/* Writable by non-root? */
 		|| !(f = fopen(config_file, "r"))		/* Can not open? */
 		) {
-		return;
+		goto out;
 	}
 
 	suid_cfg_readable = 1;
@@ -171,7 +171,7 @@ static void parse_config_file(void)
 			}
 			fclose(f);
 			suid_config = sct_head;	/* Success, so set the pointer. */
-			return;
+			goto out;
 		}
 
 		lc++;					/* Got a (partial) line. */
@@ -256,12 +256,11 @@ static void parse_config_file(void)
 				e = (char *) bb_skip_whitespace(e+1);
 
 				for (i=0 ; i < 3 ; i++) {
-					const char *q;
-					if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
+					if (!*(err = strchrnul(mode_chars + 5*i, *e++))) {
 						parse_error("mode");
 					}
 					/* Adjust by -i to account for nul. */
-					sct->m_mode |= mode_mask[(q - mode_chars) - i];
+					sct->m_mode |= mode_mask[(err - mode_chars) - i];
 				}
 
 				/* Now get the the user/group info. */
@@ -326,6 +325,8 @@ static void parse_config_file(void)
 		free(sct_head);
 		sct_head = sct;
 	}
+out:
+	RELEASE_CONFIG_BUFFER(buffer);
 	return;
 }
 
@@ -333,13 +334,13 @@ static void parse_config_file(void)
 #define parse_config_file()
 #endif /* CONFIG_FEATURE_SUID_CONFIG */
 
-#ifdef CONFIG_FEATURE_SUID
+#if ENABLE_FEATURE_SUID
 static void check_suid (struct BB_applet *applet)
 {
   uid_t ruid = getuid ();               /* real [ug]id */
   uid_t rgid = getgid ();
 
-#ifdef CONFIG_FEATURE_SUID_CONFIG
+  if (ENABLE_FEATURE_SUID_CONFIG) {
   if (suid_cfg_readable) {
 	struct BB_suid_config *sct;
 
@@ -352,7 +353,8 @@ static void check_suid (struct BB_applet
 
 	  if (sct->m_uid == ruid)       /* same uid */
 		m >>= 6;
-	  else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid))  /* same group / in group */
+		  else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid))
+			/* same group / in group */
 		m >>= 3;
 
 	  if (!(m & S_IXOTH))           /* is x bit not set ? */
@@ -377,21 +379,19 @@ static void check_suid (struct BB_applet
 	  setuid (ruid);
 	}
 	return;
-  } else {
-#ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
+	  } else if (!ENABLE_FEATURE_SUID_CONFIG_QUIET) {
 	static int onetime = 0;
 
 	if (!onetime) {
 	  onetime = 1;
 	  fprintf (stderr, "Using fallback suid method\n");
 	}
-#endif
   }
-#endif
+  }
 
   if (applet->need_suid == _BB_SUID_ALWAYS) {
 	if (geteuid () != 0)
-	  bb_error_msg_and_die ("This applet requires root priviledges!");
+	  bb_error_msg_and_die ("This applet requires root privileges!");
   } else if (applet->need_suid == _BB_SUID_NEVER) {
 	setgid (rgid);                          /* drop all priviledges */
 	setuid (ruid);
@@ -399,7 +399,7 @@ static void check_suid (struct BB_applet
 }
 #else
 #define check_suid(x)
-#endif /* CONFIG_FEATURE_SUID */
+#endif /* ENABLE_FEATURE_SUID */
 
 
 
@@ -444,15 +444,19 @@ struct BB_applet *find_applet_by_name (c
 
 void run_applet_by_name (const char *name, int argc, char **argv)
 {
-	if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file ();
+	if (ENABLE_FEATURE_SUID_CONFIG)
+		parse_config_file ();
 
-	if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
+	if (!strncmp(name, "busybox", 7))
+		busybox_main(argc, argv);
 	/* Do a binary search to find the applet entry given the name. */
 	applet_using = find_applet_by_name(name);
-	if(applet_using) {
+	if (applet_using) {
 		bb_applet_name = applet_using->name;
-		if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage ();
-		if(ENABLE_FEATURE_SUID) check_suid (applet_using);
+		if (argc==2 && !strcmp(argv[1], "--help"))
+			bb_show_usage ();
+		if (ENABLE_FEATURE_SUID)
+			check_suid (applet_using);
 		exit ((*(applet_using->main)) (argc, argv));
 	}
 }


More information about the busybox mailing list