[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: [PATCH 2/8] busybox -- SELinux option support for coreutils: ver3
From:       Yuichi Nakamura <ynakam () hitachisoft ! jp>
Date:       2007-02-26 1:52:49
Message-ID: 20070226105249.98bf6e55.ynakam () hitachisoft ! jp
[Download RAW message or body]

On Sat, 24 Feb 2007 16:01:14 +0100
Denis Vlasenko  wrote:
> On Friday 23 February 2007 09:47, Yuichi Nakamura wrote:
> > [2/8] busybox-coreutils-02-copy.v3.patch
> >   - cp: -Z,-c option support. 
> >       -c option: security context is preserved 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.
> >     -Z option: security context can be set during file copy.
> > 
> > Signed-off-by: Yuichi Nakamura <ynakam@hitachisoft.jp>
> 
>         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;
> ...
>         { "owner",               0, NULL, 'o' },
> +#if ENABLE_SELINUX
> +       { "context",             1, NULL, 'Z' },
> +       { "preserve_context",    0, NULL, '\b'},
> +       { "preserve-context",    0, NULL, '\b'},
> +
> +#endif
>         { 0, 0, 0, 0 }
> 
> Hmmm... we typically use high ascii values for this kind
> of "fake" option chars. Example in wget.c:
> 
>         static const struct option wget_long_options[] = {
> ...
>                 { "user-agent",       required_argument, NULL, 'U' },
>                 { "passive-ftp",      no_argument, NULL, 0xff },
>                 { "header",           required_argument, NULL, 0xfe },
>                 { 0, 0, 0, 0 }
>         };
>         applet_long_options = wget_long_options;
> #endif
>         opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
>         opt = getopt32(argc, argv, "cqO:P:Y:U:",   ...);
> 
> Notice that in this example we avoid giving "strange" chars to getopt32
> *at all*, preventing our applets from having "hidden" options a-la
> "wget $'-\xff' ftp://kernel.org/".
> 
> Can you avoid passing '\b' to getopt32 in install etc?
Fixed. Using "0xff" instead.


> 
> 
> +                       bb_error_msg("warning: ignoring --preserve-context. "
> +                                        "The kernel is not SELinux-enabled.\n" );
> 
> bb_error_msg don't need trailing '\n'.
> I already mentioned that in the previous round of review.
> 
> 
> +#if ENABLE_SELINUX
> +       if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0){
> +               security_context_t con;
> +               if (lgetfilecon (source, &con) >= 0){
> +                       if (setfscreatecon(con) < 0) {
> +                               bb_perror_msg ("cannot set setfscreatecon %s", con);
> +                               freecon(con);
> +                               return -1;
> +                       }
> +               }else{
> +                       if( errno == ENOTSUP || errno == ENODATA ) {
> +                               setfscreatecon(NULL);
> +                       } else {
> +                               bb_perror_msg ("cannot  lgetfilecon %s", source);
> +                               return -1;
> +                       }
> +               }
> +       }
> +#endif
> 
> Usage of whitespace is very different from the rest of busybox code here.
Fixed. And I found other bad usage of white space, and fixed.

> 
> --
> vda

Attached is revised patch(v4).

--
Yuichi Nakamura
Hitachi Software Engineering Co., Ltd.
SELinux Policy Editor: http://seedit.sourceforge.net/


["busybox-coreutils-copy-02.v4.patch" (application/octet-stream)]

Index: include/libbb.h
===================================================================
--- include/libbb.h	(revision 17961)
+++ include/libbb.h	(working copy)
@@ -737,7 +737,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,
@@ -745,9 +745,13 @@
 	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")
 extern const char *applet_name;
 extern const char BB_BANNER[];
 
Index: coreutils/cp.c
===================================================================
--- coreutils/cp.c	(revision 17961)
+++ coreutils/cp.c	(working copy)
@@ -3,6 +3,7 @@
  * Mini cp implementation for busybox
  *
  * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
+ * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  *
  * 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) {
Index: coreutils/mv.c
===================================================================
--- coreutils/mv.c	(revision 17961)
+++ coreutils/mv.c	(working copy)
@@ -3,6 +3,7 @@
  * Mini mv implementation for busybox
  *
  * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
+ * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  *
  * 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;
 				}
Index: coreutils/install.c
===================================================================
--- coreutils/install.c	(revision 17961)
+++ coreutils/install.c	(working copy)
@@ -21,10 +21,50 @@
 	{ "group",               0, NULL, 'g' },
 	{ "mode",                0, NULL, 'm' },
 	{ "owner",               0, NULL, 'o' },
+#if ENABLE_SELINUX
+	{ "context",             1, NULL, 'Z' },
+	{ "preserve_context",    0, NULL, 0xff},
+	{ "preserve-context",    0, NULL, 0xff},
+#endif
 	{ 0, 0, 0, 0 }
 };
 #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)
 {
@@ -37,7 +77,9 @@
 	const char *mode_str;
 	int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
 	int ret = EXIT_SUCCESS, flags, i, isdir;
-
+#if ENABLE_SELINUX
+	security_context_t scontext;
+#endif
 	enum {
 		OPT_CMD           =  0x1,
 		OPT_DIRECTORY     =  0x2,
@@ -46,15 +88,42 @@
 		OPT_GROUP         = 0x10,
 		OPT_MODE          = 0x20,
 		OPT_OWNER         = 0x40,
+#if ENABLE_SELINUX
+		OPT_SET_SECURITY_CONTEXT = 0x80,
+		OPT_PRESERVE_SECURITY_CONTEXT = 0x100,
+#endif
 	};
 
 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
 	applet_long_options = install_long_options;
 #endif
-	opt_complementary = "?:s--d:d--s";
+	opt_complementary = "?:s--d:d--s" USE_SELINUX(":Z--\xff:\xff--Z");
 	/* -c exists for backwards compatibility, its needed */
-	flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str);
+	flags = getopt32(argc, argv, "cdpsg:m:o:" USE_SELINUX("Z:\xff"), &gid_str, \
&mode_str, &uid_str USE_SELINUX(, &scontext));  
+#if ENABLE_SELINUX
+	if (flags & OPT_PRESERVE_SECURITY_CONTEXT) {
+		use_default_selinux_context = 0;
+		copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
+		if(!is_selinux_enabled()) {
+			bb_error_msg("warning: ignoring --preserve-context. "
+					 "The kernel is not SELinux-enabled.");
+		}
+	}
+	if (flags & OPT_SET_SECURITY_CONTEXT) {
+		if(!is_selinux_enabled()) {
+			bb_error_msg("warning: ignoring --context (-Z). "
+					 "The kernel is not SELinux-enabled.");
+		} else {
+			if (setfscreatecon(scontext) < 0) {
+				bb_error_msg_and_die("cannot set default security context %s", scontext);
+			}
+		}
+		use_default_selinux_context = 0;
+		copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT;
+	}
+#endif
+
 	/* preserve access and modification time, this is GNU behaviour, BSD only preserves \
modification time */  if (flags & OPT_PRESERVE_TIME) {
 		copy_flags |= FILEUTILS_PRESERVE_STATUS;
@@ -117,7 +186,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
Index: libbb/copy_file.c
===================================================================
--- libbb/copy_file.c	(revision 17961)
+++ libbb/copy_file.c	(working copy)
@@ -3,6 +3,7 @@
  * Mini copy_file implementation for busybox
  *
  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
+ * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
@@ -62,6 +63,26 @@
 		dest_exists = 1;
 	}
 
+#if ENABLE_SELINUX
+	if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
+		security_context_t con;
+		if (lgetfilecon (source, &con) >= 0) {
+			if (setfscreatecon(con) < 0) {
+				bb_perror_msg ("cannot set setfscreatecon %s", con);
+				freecon(con);
+				return -1;
+			}
+		} else {
+			if(errno == ENOTSUP || errno == ENODATA) {
+				setfscreatecon(NULL);
+			} else {
+				bb_perror_msg ("cannot  lgetfilecon %s", source);
+				return -1;
+			}
+		}
+	}
+#endif
+
 	if (S_ISDIR(source_stat.st_mode)) {
 		DIR *dp;
 		struct dirent *d;
@@ -202,8 +223,27 @@
 				close(src_fd);
 				return -1;
 			}
+		}				
+
+#if ENABLE_SELINUX
+		if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
+		 || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
+		 && is_selinux_enabled() > 0) {
+			security_context_t con;  
+			if(getfscreatecon(&con) == -1) {
+				bb_perror_msg ("cannot getfscreatecon");
+				return -1;
+			}				
+			if (con) {
+				if(setfilecon(dest, con) == -1) {
+					bb_perror_msg ("cannot setfilecon:%s,%s",dest,con);
+					freecon(con);
+					return -1;
+				}
+				freecon(con);
+			}
 		}
-
+#endif
 		if (bb_copyfd_eof(src_fd, dst_fd) == -1)
 			status = -1;
 		if (close(dst_fd) < 0) {



_______________________________________________
busybox mailing list
busybox@busybox.net
http://busybox.net/cgi-bin/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic