Skip to contentSkip to conversation

busybox -- SELinux option support for coreutils

PATCH In review Yuichi Nakamura 1 participant 2 messages Feb 8, 2007 – Feb 9, 2007 1 message not in the archive SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 2/6 busybox -- SELinux option support for coreutils

[2/6] busybox-coreutils-02-copy.patch
- cp: -Z,-c option support.
-c option: security context is preserved during file copy. -Z option: security context can be set during file copy.
- mv
In SELinux, it is recommended to preserve security context when file is moved. By this patch, file context is preserved during file move.
- install
When file is copied by install, security context of installed file becomes different from value configured in file_contexts file. By this patch, security context is set according to file_contexts file.

libbb.h +7 -1
Index: include/libbb.h
===================================================================
@@ -743,9 +743,15 @@
 	FILEUTILS_INTERACTIVE = 0x10,
 	FILEUTILS_MAKE_HARDLINK = 0x20,
 	FILEUTILS_MAKE_SOFTLINK = 0x40,
+#if ENABLE_SELINUX
+	FILEUTILS_PRESERVE_SECURITY_CONTEXT = 0x80,
+	FILEUTILS_SET_SECURITY_CONTEXT = 0x100
+#endif
+
 };
-#define FILEUTILS_CP_OPTSTR "pdRfils"
 
+#define FILEUTILS_CP_OPTSTR "pdRfils" USE_SELINUX("cZ:")
+
 extern const char *applet_name;
 extern const char BB_BANNER[];
 
cp.c +19 -2
Index: coreutils/cp.c
===================================================================
@@ -3,6 +3,7 @@
  * Mini cp implementation for busybox
  *
  * Copyright (C) 2000 by Matt Kraai <[email protected]>
+ * SELinux support by Yuichi Nakamura <[email protected]>
  *
  * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  */
@@ -28,6 +29,9 @@
 	int d_flags;
 	int flags;
 	int status = 0;
+#if ENABLE_SELINUX
+ 	char *context_str = NULL;
+#endif
 	enum {
 		OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
 		OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
@@ -40,8 +44,8 @@
 	// -P and -d are the same (-P is POSIX, -d is GNU)
 	// -r and -R are the same
 	// -a = -pdR
-	opt_complementary = "?:l--s:s--l:Pd:rR:apdR";
-	flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL");
+	opt_complementary = "?:l--s:s--l:Pd:rR:apdR" USE_SELINUX(":c--Z:Z--c");
+	flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL" USE_SELINUX(,&context_str));
 	/* Default behavior of cp is to dereference, so we don't have to do
 	 * anything special when we are given -L.
 	 * The behavior of -H is *almost* like -L, but not quite, so let's
@@ -50,6 +54,19 @@
 	if (flags & OPT_H) ... // deref command-line params only
 	*/
 
+#if ENABLE_SELINUX 
+	if (flags & FILEUTILS_SET_SECURITY_CONTEXT) {
+		if(is_selinux_enabled() == 0) {
+			fprintf( stderr, "Warning:  ignoring --context (-Z). "
+					 "It requires a SELinux enabled kernel.\n" );
+		}else{
+			if ( setfscreatecon(context_str) < 0 ) {
+				bb_error_msg_and_die("cannot set default security context %s\n", context_str);
+			}
+		}
+	}
+#endif
+
 	flags ^= FILEUTILS_DEREFERENCE;		/* The sense of this flag was reversed. */
 
 	if (optind + 2 > argc) {
mv.c +7 -2
Index: coreutils/mv.c
===================================================================
@@ -3,6 +3,7 @@
  * Mini mv implementation for busybox
  *
  * Copyright (C) 2000 by Matt Kraai <[email protected]>
+ * SELinux support by Yuichi Nakamura <[email protected]>
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
@@ -44,6 +45,7 @@
 	unsigned long flags;
 	int dest_exists;
 	int status = 0;
+	int copy_flag = 0;
 
 #if ENABLE_FEATURE_MV_LONG_OPTIONS
 	applet_long_options = mv_long_options;
@@ -113,8 +115,11 @@
 						goto RET_1;
 					}
 				}
-				if ((copy_file(*argv, dest,
-					FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
+				copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
+#if ENABLE_SELINUX
+				copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
+#endif				
+				if ((copy_file(*argv, dest,	copy_flag) >= 0) &&
 					(remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
 					goto RET_0;
 				}
install.c +39 -1
Index: coreutils/install.c
===================================================================
@@ -25,6 +25,41 @@
 };
 #endif
 
+
+#if ENABLE_SELINUX
+static int use_default_selinux_context = 1;
+
+static void setdefaultfilecon(const char *path) {
+	struct stat s;
+	security_context_t scontext = NULL;
+
+	if (!is_selinux_enabled()){
+		return;
+	}	
+	if (lstat(path, &s) != 0){
+		return;
+	}
+
+	if (matchpathcon(path, s.st_mode, &scontext) < 0){
+		return;
+	}
+	if (strcmp(scontext, "<<none>>") == 0){
+		freecon(scontext);
+		return;
+	}
+
+	if (lsetfilecon(path, scontext) < 0) {
+		if (errno != ENOTSUP) {
+			bb_perror_msg("warning: failed to change context of %s to %s", path, scontext);
+		}
+	}
+
+	freecon(scontext);
+	return;
+}
+
+#endif
+
 int install_main(int argc, char **argv);
 int install_main(int argc, char **argv)
 {
@@ -117,7 +152,10 @@
 			bb_perror_msg("cannot change permissions of %s", dest);
 			ret = EXIT_FAILURE;
 		}
-
+#if ENABLE_SELINUX
+		if (use_default_selinux_context)
+			setdefaultfilecon(dest);
+#endif
 		/* Set the user and group id */
 		if ((flags & (OPT_OWNER|OPT_GROUP))
 		 && lchown(dest, uid, gid) == -1
copy_file.c +41 -1

61 lines not on this page · raw message

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 32 lines · On Thu, 8 Feb 2007 23:49:08 +0100 Denis Vlasenko wrote:
Quoted 13 lines · Yuichi Nakamura

[2/6] busybox-coreutils-02-copy.patch
- cp: -Z,-c option support.
-c option: security context is preserved during file copy. -Z option: security context can be set during file copy.
- mv
In SELinux, it is recommended to preserve security context when file is moved. By this patch, file context is preserved during file move.
- install
When file is copied by install, security context of installed file becomes different from value configured in file_contexts file. By this patch, security context is set according to file_contexts file.

Quoted 1 line

Index: include/libbb.h
===================================================================

libbb.h +5 -0
@@ -743,9 +743,15 @@
 	FILEUTILS_INTERACTIVE = 0x10,
 	FILEUTILS_MAKE_HARDLINK = 0x20,
 	FILEUTILS_MAKE_SOFTLINK = 0x40,
+#if ENABLE_SELINUX
+	FILEUTILS_PRESERVE_SECURITY_CONTEXT = 0x80,
+	FILEUTILS_SET_SECURITY_CONTEXT = 0x100
+#endif
+
 };
 
Quoted 1 line

This empty line after #endif - why?

removed this empty line.

libbb.h

Quoted 13 lines
libbb.h +12 -0
+#if ENABLE_SELINUX 
+	if (flags & FILEUTILS_SET_SECURITY_CONTEXT) {
+		if(is_selinux_enabled() == 0) {
+			fprintf( stderr, "Warning:  ignoring --context (-Z). "
+					 "It requires a SELinux enabled kernel.\n" );
+		}else{
+			if ( setfscreatecon(context_str) < 0 ) {
+				bb_error_msg_and_die("cannot set default security context %s\n", context_str);
+			}
+		}
+	}
+#endif

This part is removed because upstream coreutils does not have -Z option for cp.

Quoted 5 lines

The style is not consistent. Should be "if ()", "} else {".
"Warning: ignoring" has extra space for no reason.
fprintf(stderr) can be probably replaced by bb_error_msg:
bb_error_msg("warning: ignoring --context (-Z), it requires a SELinux enabled kernel");

fixed.

Quoted 5 lines

+static int use_default_selinux_context = 1;

You never change it, it is always 1. - ?!

It is used in current patch.

Quoted 3 lines

--
vda

Other changes are following:
* Removed -Z option from cp
* Added --preserve-context, -Z options to install

Signature · 3 lines
Yuichi Nakamura
Hitachi Software Engineering Co., Ltd.
SELinux Policy Editor: http://seedit.sourceforge.net/
libbb.h +7 -2
Index: include/libbb.h
===================================================================
@@ -735,7 +735,7 @@
 uint32_t *crc32_filltable(int endian);
 
 
-enum {	/* DO NOT CHANGE THESE VALUES!  cp.c depends on them. */
+enum {	/* DO NOT CHANGE THESE VALUES!  cp.c, mv.c, install.c depends on them. */
 	FILEUTILS_PRESERVE_STATUS = 1,
 	FILEUTILS_DEREFERENCE = 2,
 	FILEUTILS_RECUR = 4,
@@ -743,9 +743,14 @@
 	FILEUTILS_INTERACTIVE = 0x10,
 	FILEUTILS_MAKE_HARDLINK = 0x20,
 	FILEUTILS_MAKE_SOFTLINK = 0x40,
+#if ENABLE_SELINUX
+	FILEUTILS_PRESERVE_SECURITY_CONTEXT = 0x80,
+	FILEUTILS_SET_SECURITY_CONTEXT = 0x100
+#endif
 };
-#define FILEUTILS_CP_OPTSTR "pdRfils"
 
+#define FILEUTILS_CP_OPTSTR "pdRfils" USE_SELINUX("c\b")
+
 extern const char *applet_name;
 extern const char BB_BANNER[];
 
cp.c +7 -0
Index: coreutils/cp.c
===================================================================
@@ -3,6 +3,7 @@
  * Mini cp implementation for busybox
  *
  * Copyright (C) 2000 by Matt Kraai <[email protected]>
+ * SELinux support by Yuichi Nakamura <[email protected]>
  *
  * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  */
@@ -50,6 +51,12 @@
 	if (flags & OPT_H) ... // deref command-line params only
 	*/
 
+#if ENABLE_SELINUX 
+	if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
+		selinux_or_die();
+	}
+#endif
+
 	flags ^= FILEUTILS_DEREFERENCE;		/* The sense of this flag was reversed. */
 
 	if (optind + 2 > argc) {
mv.c +7 -2
Index: coreutils/mv.c
===================================================================
@@ -3,6 +3,7 @@
  * Mini mv implementation for busybox
  *
  * Copyright (C) 2000 by Matt Kraai <[email protected]>
+ * SELinux support by Yuichi Nakamura <[email protected]>
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
@@ -44,6 +45,7 @@
 	unsigned long flags;
 	int dest_exists;
 	int status = 0;
+	int copy_flag = 0;
 
 #if ENABLE_FEATURE_MV_LONG_OPTIONS
 	applet_long_options = mv_long_options;
@@ -113,8 +115,11 @@
 						goto RET_1;
 					}
 				}
-				if ((copy_file(*argv, dest,
-					FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
+				copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
+#if ENABLE_SELINUX
+				copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
+#endif				
+				if ((copy_file(*argv, dest,	copy_flag) >= 0) &&
 					(remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
 					goto RET_0;
 				}
install.c +77 -4

116 lines not on this page · raw message

copy_file.c +41 -1

61 lines not on this page · raw message