All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel J Walsh <dwalsh@redhat.com>
To: Stephen Smalley <sds@epoch.ncsc.mil>
Cc: Russell Coker <russell@coker.com.au>, SE Linux <selinux@tycho.nsa.gov>
Subject: Re: init patch for loading policy
Date: Tue, 21 Oct 2003 14:07:26 -0400	[thread overview]
Message-ID: <3F9575DE.3010802@redhat.com> (raw)
In-Reply-To: <1066748352.27065.100.camel@moss-spartans.epoch.ncsc.mil>


[-- Attachment #1.1: Type: text/plain, Size: 408 bytes --]

Stephen Smalley wrote:

>On Tue, 2003-10-21 at 10:43, Russell Coker wrote: 
>  
>
>>The results I have so far indicate that this approach has significant 
>>problems.
>>
>>Diverting /sbin/init with a shell script works better than this.
>>    
>>
>
>Ok, thanks for looking into it.  So what exactly is the problem with
>diverting /sbin/init again?  
>  
>

Here is my patch to init to load initial policy.



[-- Attachment #1.2: Type: text/html, Size: 859 bytes --]

[-- Attachment #2: sysvinit-selinux.patch --]
[-- Type: text/plain, Size: 3586 bytes --]

--- sysvinit-2.85/src/init.c.selinux	2003-10-21 11:01:52.000000000 -0400
+++ sysvinit-2.85/src/init.c	2003-10-21 11:18:20.000000000 -0400
@@ -78,6 +78,87 @@
 			sigemptyset(&sa.sa_mask); \
 			sigaction(sig, &sa, NULL); \
 		} while(0)
+#ifdef WITH_SELINUX
+#include <sys/mman.h>
+#include <selinux/selinux.h>
+#include <sys/mount.h>
+
+#define POLICY_FILE "/etc/security/selinux/policy"
+#define DEFAULT_POLICY_VERSION 15
+#define SELINUXMNT "/selinux"
+#define POLICY_VERSION_FILE "/selinux/policyvers"
+#define SELINUX_ENFORCE_FILE "/selinux/enforce"
+static int load_policy(int *enforce) 
+{
+  int fd,ret=-1;
+  struct stat sb;
+  void *map;
+  char policy_file[PATH_MAX];
+  int policy_version=DEFAULT_POLICY_VERSION;
+  FILE *fp;
+
+  log(L_VB, "Loading security policy\n");
+  if (mount("none", SELINUXMNT, "selinuxfs", 0, 0) < 0) {
+    if (errno == ENODEV) {
+      log(L_VB, "SELinux not supported by kernel: %s\n",SELINUXMNT,strerror(errno));
+    } 
+    else {
+      log(L_VB, "Failed to mount %s: %s\n",SELINUXMNT,strerror(errno));
+      exit(1);
+    }
+    return ret; /* Never gets here */
+  }
+
+  /* Check policy version file.  For now this will default to 15.  When all 
+     kernel supports POLICY_VERSION_FILE, this should become an error */
+  fp = fopen(POLICY_VERSION_FILE, "r");
+  if(fp)
+    {
+      fscanf(fp,"%d",&policy_version);
+      fclose(fp); 
+    }
+  
+  fp = fopen(SELINUX_ENFORCE_FILE, "r");
+  if(fp)
+    {
+      fscanf(fp,"%d",enforce);
+      fclose(fp); 
+    }
+  else {
+    log(L_VB,  "Can't open '%s':  %s\n",
+	    SELINUX_ENFORCE_FILE, strerror(errno));
+    goto UMOUNT;
+  }
+  snprintf(policy_file,sizeof(policy_file),"%s.%d",POLICY_FILE,policy_version);
+  fd = open(policy_file, O_RDONLY);
+  if (fd < 0) {
+    log(L_VB,  "Can't open '%s':  %s\n",
+	    policy_file, strerror(errno));
+    goto UMOUNT;
+  }
+  
+  if (fstat(fd, &sb) < 0) {
+    log(L_VB, "Can't stat '%s':  %s\n",
+	    policy_file, strerror(errno));
+    goto UMOUNT;
+  }
+  
+  map = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
+  if (map == MAP_FAILED) {
+    log(L_VB,  "Can't map '%s':  %s\n",
+	    policy_file, strerror(errno));
+    goto UMOUNT;
+  }
+  ret=security_load_policy(map, sb.st_size);
+  if (ret < 0) {
+    log(L_VB, "security_load_policy failed\n");
+  }
+
+ UMOUNT:
+  umount(SELINUXMNT); 
+  return(ret);
+}
+#endif
 
 /* Version information */
 char *Version = "@(#) init " VERSION "  " DATE "  miquels@cistron.nl";
@@ -2576,6 +2657,20 @@
 		maxproclen += strlen(argv[f]) + 1;
 	}
 
+#ifdef WITH_SELINUX
+  	if (getenv("SELINUX_INIT") == NULL) {
+	  putenv("SELINUX_INIT=YES");
+	  int enforce=0;
+	  if (load_policy(&enforce) == 0 ) {
+	    execv(myname, argv);
+	  } else {
+	    if (enforce) 
+	      /* SELinux in enforcing mode but load_policy failed */
+	      exit(1);
+	  }
+	}
+#endif
+  
 	/* Start booting. */
 	argv0 = argv[0];
 	argv[1] = NULL;
--- sysvinit-2.85/src/Makefile.selinux	2003-10-21 11:01:52.000000000 -0400
+++ sysvinit-2.85/src/Makefile	2003-10-21 11:01:52.000000000 -0400
@@ -32,7 +32,7 @@
 all:		$(PROGS)
 
 init:		init.o init_utmp.o
-		$(CC) $(LDFLAGS) $(STATIC) -o $@ init.o init_utmp.o
+		$(CC) $(LDFLAGS) $(STATIC) -o $@ init.o init_utmp.o -lselinux
 
 halt:		halt.o ifdown.o hddown.o utmp.o reboot.h
 		$(CC) $(LDFLAGS) -o $@ halt.o ifdown.o hddown.o utmp.o
@@ -62,7 +62,7 @@
 		$(CC) $(LDFLAGS) -o $@ bootlogd.o
 
 init.o:		init.c init.h set.h reboot.h
-		$(CC) -c $(CFLAGS) init.c
+		$(CC) -c $(CFLAGS) -DWITH_SELINUX init.c
 
 utmp.o:		utmp.c init.h
 		$(CC) -c $(CFLAGS) utmp.c

  parent reply	other threads:[~2003-10-21 18:07 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-19 15:48 init patch for loading policy Russell Coker
2003-10-20  8:21 ` Carsten Grohmann
2003-10-20 18:02 ` Stephen Smalley
2003-10-20 20:10   ` Daniel J Walsh
2003-10-20 20:46     ` Stephen Smalley
2003-10-20 20:56       ` Daniel J Walsh
2003-10-21 12:19         ` Stephen Smalley
2003-10-21  0:52   ` Russell Coker
2003-10-21 12:29     ` Stephen Smalley
2003-10-21 14:43       ` Russell Coker
2003-10-21 14:59         ` Stephen Smalley
2003-10-21 16:00           ` Russell Coker
2003-10-21 18:38             ` Daniel J Walsh
2003-10-21 20:14             ` Bastian Blank
2003-10-21 17:50           ` Daniel J Walsh
2003-10-22 22:31             ` Joubert Berger
2003-10-23  1:42               ` Russell Coker
2003-10-21 18:07           ` Daniel J Walsh [this message]
2003-10-21 18:54             ` Stephen Smalley
2003-10-21 19:56               ` Stephen Smalley
2003-10-21 12:32     ` Stephen Smalley
2003-10-21 13:56       ` Russell Coker
2003-10-20 20:47 ` Bastian Blank
2003-10-21  0:57   ` Russell Coker
2003-10-21  6:26     ` Bastian Blank

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3F9575DE.3010802@redhat.com \
    --to=dwalsh@redhat.com \
    --cc=russell@coker.com.au \
    --cc=sds@epoch.ncsc.mil \
    --cc=selinux@tycho.nsa.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.