Skip to contentSkip to conversation

latest busybox patch

PATCH Russell Coker 1 participant 1 message May 24, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

This patch is against the latest CVS version. It has SE Linux support for id, ls, ps, and login. It enables a new config option CONFIG_SELINUX which will soon be used for extra busybox applets such as avc_toggle.

This has been tested on an iPaQ 3950 running familiar, with this patched busybox the iPaQ can boot in enforcing mode and assign the correct domain and file type for the terminal when logging in through the patched login program.

To get this fully working for booting an iPaQ requires the busybox wrapper that I haven't yet released. But the current code on it's own will work for a recovery shell when you break shared libraries, or for other purposes of busybox.

Enjoy.

Signature · 4 lines
http://www.coker.com.au/selinux/   My NSA Security Enhanced Linux packages
http://www.coker.com.au/bonnie++/  Bonnie++ hard drive benchmark
http://www.coker.com.au/postal/    Postal SMTP/POP benchmark
http://www.coker.com.au/~russell/  My home page
Makefile +5 -0
diff -ru busybox.orig/Makefile busybox/Makefile
@@ -31,6 +31,11 @@
 	networking/libiproute networking/udhcp procps loginutils shell \
 	sysklogd util-linux libbb libpwdgrp coreutils/libcoreutils
 
+ifeq ($(strip $(CONFIG_SELINUX)),y)
+CFLAGS += -I/usr/include/selinux
+LIBRARIES += -lsecure
+endif
+
 ifeq ($(strip $(HAVE_DOT_CONFIG)),y)
 
 all: busybox busybox.links #doc
coreutils/id.c +29 -5
diff -ru busybox.orig/coreutils/id.c busybox/coreutils/id.c
@@ -28,9 +28,13 @@
 #include <getopt.h>
 #include <string.h>
 #include <sys/types.h>
+#ifdef CONFIG_SELINUX
+#include <proc_secure.h>
+#include <flask_util.h>
+#endif
 
-#define NO_GROUP          1
-#define NO_USER           2
+#define JUST_USER         1
+#define JUST_GROUP        2
 #define PRINT_REAL        4
 #define NAME_NOT_NUMBER   8
 
@@ -40,10 +44,13 @@
 	long pwnam, grnam;
 	int uid, gid;
 	int flags;
+#ifdef CONFIG_SELINUX
+	int is_flask_enabled_flag = is_flask_enabled();
+#endif
 	
 	flags = bb_getopt_ulflags(argc, argv, "ugrn");
 
-	if (((flags & (NO_USER | NO_GROUP)) == (NO_USER | NO_GROUP))
+	if (((flags & (JUST_USER | JUST_GROUP)) == (JUST_USER | JUST_GROUP))
 		|| (argc > optind + 1)
 	) {
 		bb_show_usage();
@@ -67,9 +74,9 @@
 	pwnam=my_getpwnam(user);
 	grnam=my_getgrnam(group);
 
-	if (flags & (NO_GROUP | NO_USER)) {
+	if (flags & (JUST_GROUP | JUST_USER)) {
 		char *s = group;
-		if (flags & NO_GROUP) {
+		if (flags & JUST_USER) {
 			s = user;
 			grnam = pwnam;
 		}
@@ -79,7 +86,24 @@
 			printf("%ld\n", grnam);
 		}
 	} else {
+#ifdef CONFIG_SELINUX
+		printf("uid=%ld(%s) gid=%ld(%s)", pwnam, user, grnam, group);
+		if(is_flask_enabled_flag)
+		{
+			security_id_t mysid = getsecsid();
+			char context[80];
+			int len = sizeof(context);
+			context[0] = '\0';
+			if(security_sid_to_context(mysid, context, &len))
+				strcpy(context, "unknown");
+			printf(" context=%s\n", context);
+		}
+		else
+			printf("\n");
+#else
 		printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group);
+#endif
+
 	}
 
 	bb_fflush_stdout_and_exit(0);
coreutils/ls.c +81 -13

176 lines not on this page · raw message

include/libbb.h +13 -2
diff -ru busybox.orig/include/libbb.h busybox/include/libbb.h
@@ -39,6 +39,9 @@
 #include <features.h>
 
 #include "config.h"
+#ifdef CONFIG_SELINUX
+#include <proc_secure.h>
+#endif
 
 #include "pwd_.h"
 #include "grp_.h"
@@ -376,7 +379,11 @@
 
 #define FAIL_DELAY    3
 extern void change_identity ( const struct passwd *pw );
-extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args );
+extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args
+#ifdef CONFIG_SELINUX
+	, security_id_t sid
+#endif
+);
 extern int run_parts(char **args, const unsigned char test_mode);
 extern int restricted_shell ( const char *shell );
 extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw );
@@ -407,7 +414,11 @@
 	char short_cmd[16];
 } procps_status_t;
 
-extern procps_status_t * procps_scan(int save_user_arg0);
+extern procps_status_t * procps_scan(int save_user_arg0
+#ifdef CONFIG_SELINUX
+	, int use_selinux, security_id_t *sid
+#endif
+);
 extern unsigned short compare_string_array(const char *string_array[], const char *key);
 
 extern int my_query_module(const char *name, int which, void **buf, size_t *bufsize, size_t *ret);
include/usage.h +11 -4
diff -ru busybox.orig/include/usage.h busybox/include/usage.h
@@ -878,6 +878,12 @@
 	"\t-u\tthe hardware clock is kept in coordinated universal time\n" \
 	"\t-l\tthe hardware clock is kept in local time"
 
+#ifdef CONFIG_SELINUX
+#define USAGE_SELINUX(a, b) a
+#else
+#define USAGE_SELINUX(a, b) b
+#endif
+
 #define id_trivial_usage \
 	"[OPTIONS]... [USERNAME]"
 #define id_full_usage \
@@ -885,6 +891,7 @@
 	"Options:\n" \
 	"\t-g\tprints only the group ID\n" \
 	"\t-u\tprints only the user ID\n" \
+	USAGE_SELINUX("\t-c\tprints only the security context\n", "") \
 	"\t-n\tprint a name instead of a number (with for -ug)\n" \
 	"\t-r\tprints the real user ID instead of the effective ID (with -ug)"
 #define id_example_usage \
@@ -1343,7 +1350,7 @@
   #define USAGE_AUTOWIDTH(a)
 #endif
 #define ls_trivial_usage \
-	"[-1Aa" USAGE_LS_TIMESTAMPS("c") "Cd" USAGE_LS_TIMESTAMPS("e") USAGE_LS_FILETYPES("F") "iln" USAGE_LS_FILETYPES("p") USAGE_LS_FOLLOWLINKS("L") USAGE_LS_RECURSIVE("R") USAGE_LS_SORTFILES("rS") "s" USAGE_AUTOWIDTH("T") USAGE_LS_TIMESTAMPS("tu") USAGE_LS_SORTFILES("v") USAGE_AUTOWIDTH("w") "x" USAGE_LS_SORTFILES("X") USAGE_HUMAN_READABLE("h") USAGE_NOT_HUMAN_READABLE("") "k] [filenames...]"
+	"[-1Aa" USAGE_LS_TIMESTAMPS("c") "Cd" USAGE_LS_TIMESTAMPS("e") USAGE_LS_FILETYPES("F") "iln" USAGE_LS_FILETYPES("p") USAGE_LS_FOLLOWLINKS("L") USAGE_LS_RECURSIVE("R") USAGE_LS_SORTFILES("rS") "s" USAGE_AUTOWIDTH("T") USAGE_LS_TIMESTAMPS("tu") USAGE_LS_SORTFILES("v") USAGE_AUTOWIDTH("w") "x" USAGE_LS_SORTFILES("X") USAGE_HUMAN_READABLE("h") USAGE_NOT_HUMAN_READABLE("") "k" USAGE_SELINUX("K", "") "] [filenames...]"
 #define ls_full_usage \
 	"List directory contents\n\n" \
 	"Options:\n" \
@@ -1373,8 +1380,7 @@
 	USAGE_LS_SORTFILES("\t-X\tsort the listing by extension\n") \
 	USAGE_HUMAN_READABLE( \
 	"\t-h\tprint sizes in human readable format (e.g., 1K 243M 2G )\n" \
-	"\t-k\tprint sizes in kilobytes(default)") USAGE_NOT_HUMAN_READABLE( \
-	"\t-k\tprint sizes in kilobytes(compatibility)") 
+	USAGE_SELINUX("\t-k\tprint security context\n\t-K\tprint security context in long format\n", "")
 
 #define lsmod_trivial_usage \
 	""
@@ -1775,7 +1781,8 @@
 	""
 #define ps_full_usage \
 	"Report process status\n" \
-	"\nThis version of ps accepts no options."
+	USAGE_SELINUX("\nOptions:\n\t-c\tshow SE Linux context", "\nThis version of ps accepts no options.")
+ 
 #define ps_example_usage \
 	"$ ps\n" \
 	"  PID  Uid      Gid State Command\n" \
libbb/find_pid_by_name.c +4 -0
diff -ru busybox.orig/libbb/find_pid_by_name.c busybox/libbb/find_pid_by_name.c
@@ -43,7 +43,11 @@
 	procps_status_t * p;
 
 	pidList = xmalloc(sizeof(long));
+#ifdef CONFIG_SELINUX
+	while ((p = procps_scan(0, 0, NULL)) != 0) {
+#else
 	while ((p = procps_scan(0)) != 0) {
+#endif
 		if (strcmp(p->short_cmd, pidName) == 0) {
 			pidList=xrealloc( pidList, sizeof(long) * (i+2));
 			pidList[i++]=p->pid;
libbb/procps.c +13 -1
diff -ru busybox.orig/libbb/procps.c busybox/libbb/procps.c
@@ -16,7 +16,11 @@
 
 #include "libbb.h"
 
-extern procps_status_t * procps_scan(int save_user_arg0)
+extern procps_status_t * procps_scan(int save_user_arg0
+#ifdef CONFIG_SELINUX
+	, int use_selinux , security_id_t *sid
+#endif
+	)
 {
 	static DIR *dir;
 	struct dirent *entry;
@@ -53,6 +57,14 @@
 		sprintf(status, "/proc/%d/stat", pid);
 		if((fp = fopen(status, "r")) == NULL)
 			continue;
+#ifdef CONFIG_SELINUX
+		if(use_selinux)
+		{
+			if(fstat_secure(fileno(fp), &sb, sid))
+				continue;
+		}
+		else
+#endif
 		if(fstat(fileno(fp), &sb))
 			continue;
 		my_getpwuid(curstatus.user, sb.st_uid);
libbb/run_shell.c +13 -2
diff -ru busybox.orig/libbb/run_shell.c busybox/libbb/run_shell.c
@@ -36,14 +36,20 @@
 #include <syslog.h>
 #include <ctype.h>
 #include "libbb.h"
-
+#ifdef CONFIG_SELINUX
+#include <proc_secure.h>
+#endif
 
 /* Run SHELL, or DEFAULT_SHELL if SHELL is empty.
    If COMMAND is nonzero, pass it to the shell with the -c option.
    If ADDITIONAL_ARGS is nonzero, pass it to the shell as more
    arguments.  */
 
-void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args )
+void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args
+#ifdef CONFIG_SELINUX
+	, security_id_t sid
+#endif
+)
 {
 	const char **args;
 	int argno = 1;
@@ -74,6 +80,11 @@
 			args [argno++] = *additional_args;
 	}
 	args [argno] = 0;
+#ifdef CONFIG_SELINUX
+	if(sid)
+		execve_secure(shell, (char **) args, environ, sid);
+	else
+#endif
 	execv ( shell, (char **) args );
 	bb_perror_msg_and_die ( "cannot run %s", shell );
 }
loginutils/login.c +45 -2

71 lines not on this page · raw message

loginutils/su.c +5 -1
diff -ru busybox.orig/loginutils/su.c busybox/loginutils/su.c
@@ -156,7 +156,11 @@
 
 	change_identity ( pw );	
 	setup_environment ( opt_shell, opt_loginshell, !opt_preserve, pw );
-	run_shell ( opt_shell, opt_loginshell, opt_command, (const char**)opt_args );
+	run_shell ( opt_shell, opt_loginshell, opt_command, (const char**)opt_args
+#ifdef CONFIG_SELINUX
+	, 0
+#endif
+	);
 	
 	return EXIT_FAILURE;
 }
procps/ps.c +33 -0
diff -ru busybox.orig/procps/ps.c busybox/procps/ps.c
@@ -31,6 +31,11 @@
 #include <termios.h>
 #include <sys/ioctl.h>
 #include "busybox.h"
+#ifdef CONFIG_SELINUX
+#include <fs_secure.h>
+#include <ss.h>
+#include <flask_util.h>          /* for is_flask_enabled() */
+#endif
 
 static const int TERMINAL_WIDTH = 79;      /* not 80 in case terminal has linefold bug */
 
@@ -47,6 +52,13 @@
 #define terminal_width  TERMINAL_WIDTH
 #endif
 
+#ifdef CONFIG_SELINUX
+	int use_selinux = 0;
+	security_id_t sid;
+	if(is_flask_enabled() && argv[1] && !strcmp(argv[1], "-c") )
+		use_selinux = 1;
+#endif
+
 
 #ifdef CONFIG_FEATURE_AUTOWIDTH
 		ioctl(fileno(stdout), TIOCGWINSZ, &win);
@@ -54,10 +66,31 @@
 			terminal_width = win.ws_col - 1;
 #endif
 
+#ifdef CONFIG_SELINUX
+	if(use_selinux)
+		printf("  PID Context                          Stat Command\n");
+	else
+#endif
 	printf("  PID  Uid     VmSize Stat Command\n");
+#ifdef CONFIG_SELINUX
+	while ((p = procps_scan(1, use_selinux, &sid)) != 0) {
+#else
 	while ((p = procps_scan(1)) != 0) {
+#endif
 		char *namecmd = p->cmd;
 
+#ifdef CONFIG_SELINUX
+		if(use_selinux)
+		{
+			char sbuf[128];
+			len = sizeof(sbuf);
+			if(security_sid_to_context(sid, (security_context_t)&sbuf, &len))
+				strcpy(sbuf, "unknown");
+
+			len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
+		}
+		else
+#endif
 		if(p->rss == 0)
 			len = printf("%5d %-8s        %s ", p->pid, p->user, p->state);
 		else
procps/top.c +4 -0
diff -ru busybox.orig/procps/top.c busybox/procps/top.c
@@ -501,7 +501,11 @@
 		/* read process IDs & status for all the processes */
 		procps_status_t * p;
 
+#ifdef CONFIG_SELINUX
+		while ((p = procps_scan(0, 0, NULL) ) != 0) {
+#else
 		while ((p = procps_scan(0)) != 0) {
+#endif
 			int n = ntop;
 
 			top = xrealloc(top, (++ntop)*sizeof(procps_status_t));
sysdeps/linux/Config.in +7 -0
diff -ru busybox.orig/sysdeps/linux/Config.in busybox/sysdeps/linux/Config.in
@@ -106,6 +106,13 @@
 	help
 	  Please submit a patch to add help text for this item.
 
+config CONFIG_SELINUX
+	bool "Support NSA Security Enhanced Linux"
+	default n
+	help
+	  Enable support for SE Linux in applets ls, ps, and id.  Also provide
+	  the option of compiling in SE Linux applets.
+
 endmenu
 
 menu 'Build Options'