Skip to contentSkip to conversation

Changes to policycoreutils.

PATCH Daniel J Walsh 4 participants 16 messages Mar 17, 2006 – Mar 21, 2006

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

Change fixfiles to use find and -prune if available.

Ivans patch is included

The biggest change to this package is the debut of restorecond (probably need a better name).

This is a daemon that will read /etc/selinux/restorecond.conf file for a list of files to watch. It uses inotify to signal it when any file gets created. It will then do the equivalent of restorecon on the file.

So if you are watching /etc/resolv.conf and the admin uses a script to recreate the file, this daemon will notice and restore it to the correct context.

Homedir support:

The files need to be fully pathed or contain a "~". "~" will indicate the homedir. restorecond also watches /var/run/utmp file for all users logging in and out, and then watches for the files in the homedir, to be created.

This will give us the ability of instantly labeling the ~/public_html directory. So if a user logs in and does a mkdir ~/public_html to tool will label the file correctly.

Policycoreutils installs the daemon but does not turn it on by default, yet. I need people to play with it and see what they think.

chkconfig --add restorecond
service restorecond restart

Will turn it on.

Makefile +1 -1
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/Makefile policycoreutils-1.30/Makefile
@@ -1,4 +1,4 @@
-SUBDIRS=setfiles semanage load_policy newrole run_init restorecon audit2allow audit2why scripts sestatus semodule_package semodule semodule_link semodule_expand setsebool po
+SUBDIRS=setfiles semanage load_policy newrole run_init restorecon restorecond audit2allow audit2why scripts sestatus semodule_package semodule semodule_link semodule_expand setsebool po
 
 all install relabel clean: 
 	@for subdir in $(SUBDIRS); do \
restorecond/Makefile +29 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/Makefile policycoreutils-1.30/restorecond/Makefile
@@ -0,0 +1,29 @@
+# Installation directories.
+PREFIX ?= ${DESTDIR}/usr
+SBINDIR ?= $(PREFIX)/sbin
+MANDIR = $(PREFIX)/share/man
+INITDIR = $(DESTDIR)/etc/rc.d/init.d
+SELINUXDIR = $(DESTDIR)/etc/selinux
+
+CFLAGS ?= -g -Werror -Wall -W
+override CFLAGS += -I$(PREFIX)/include -D_FILE_OFFSET_BITS=64
+LDLIBS += -lselinux -L$(PREFIX)/lib
+
+all: restorecond
+
+restorecond:  restorecond.o utmpwatcher.o stringslist.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+install: all
+	[ -d $(MANDIR)/man8 ] || mkdir -p $(MANDIR)/man8
+	-mkdir -p $(SBINDIR)
+	install -m 755 restorecond $(SBINDIR)
+	install -m 644 restorecond.8 $(MANDIR)/man8
+	-mkdir -p $(INITDIR)
+	install -m 644 restorecond.init $(INITDIR)/restorecond
+	-mkdir -p $(SELINUXDIR)
+	install -m 600 restorecond.conf $(SELINUXDIR)/restorecond.conf
+
+clean:
+	-rm -f restorecond *.o *~
+
restorecond/restorecond.8 +31 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.8 policycoreutils-1.30/restorecond/restorecond.8
@@ -0,0 +1,31 @@
+.TH "restorecond" "8" "2002031409" "" ""
+.SH "NAME"
+restorecond \- daemon that watches for file creation and then corrects file context
+
+.SH "SYNOPSIS"
+.B restorecond  [\-d]
+.P
+
+.SH "DESCRIPTION"
+This manual page describes the
+.BR restorecond
+program.
+.P
+This daemon uses inotify to watch files listed in the /etc/selinux/POLICYTYPE/restorconfiles.conf, when they are created, this daemon will make sure they have 
+the correct file context associated with the policy.
+
+.SH "OPTIONS"
+.TP 
+.B \-d
+Turns on debugging mode.   Application will stay in the foreground and lots of
+debugs messages start printing.
+
+.SH "AUTHOR"
+This man page was written by Dan Walsh <[email protected]>.
+The program was written by Dan Walsh <[email protected]>.
+
+.SH "FILES"
+/etc/selinux/POLICYTYPE/restorconfiles.conf
+
+.SH "SEE ALSO"
+.BR restorecon (8),
restorecond/restorecond.c +462 -0

462 lines not on this page · raw message

restorecond/restorecond.conf +3 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.conf policycoreutils-1.30/restorecond/restorecond.conf
@@ -0,0 +1,3 @@
+/etc/resolv.conf
+/etc/mtab
+~/public_html
restorecond/restorecond.h +31 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.h policycoreutils-1.30/restorecond/restorecond.h
@@ -0,0 +1,31 @@
+/* restorecond.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+
+#ifndef RESTORED_CONFIG_H
+#define RESTORED_CONFIG_H
+
+void exitApp(const char *msg);
+void watch_list_add(int inotify_fd, const char *path);
+
+#endif
+
restorecond/restorecond.init +57 -0

57 lines not on this page · raw message

restorecond/stringslist.c +118 -0

118 lines not on this page · raw message

restorecond/stringslist.h +37 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/stringslist.h policycoreutils-1.30/restorecond/stringslist.h
@@ -0,0 +1,37 @@
+/* stringslist.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+#ifndef STRINGSLIST_H
+#define STRINGSLIST_H
+
+struct stringsList {
+	struct stringsList *next;
+	char *string;
+};
+
+void strings_list_free(struct stringsList *list);
+void strings_list_add(struct stringsList **list, const char *string);
+void strings_list_print(struct stringsList *list);
+int strings_list_find(struct stringsList *list, const char *string);
+int strings_list_diff(struct stringsList *from, struct stringsList *to);
+
+#endif
restorecond/utmpwatcher.c +105 -0

105 lines not on this page · raw message

restorecond/utmpwatcher.h +29 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/utmpwatcher.h policycoreutils-1.30/restorecond/utmpwatcher.h
@@ -0,0 +1,29 @@
+/* utmpwatcher.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+#ifndef UTMPWATCHER_H
+#define UTMPWATCHER_H
+
+unsigned int utmpwatcher_handle(int inotify_fd, int wd);
+void utmpwatcher_add(int inotify_fd, const char *path);
+
+#endif
scripts/fixfiles +9 -1
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/scripts/fixfiles policycoreutils-1.30/scripts/fixfiles
@@ -124,7 +124,15 @@
     exit $?
 fi
 if [ ! -z "$DIRS" ]; then
-    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} -R $1 -v $DIRS 2>&1 >> $LOGFILE
+    if [ -x /usr/bin/find ]; then
+	for d in ${DIRS} ; do find $d \
+	    ! \( -fstype ext2 -o -fstype ext3 -o -fstype jfs -o -fstype xfs \) -prune  -o -print; \
+	    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} $1 -v -f - 2>&1 >> $LOGFILE
+	done
+    else
+	${RESTORECON} ${OUTFILES} ${FORCEFLAG} -R $1 -v $DIRS 2>&1 >> $LOGFILE
+    fi
+
     exit $?
 fi
 LogReadOnly
scripts/Makefile +2 -2
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/scripts/Makefile policycoreutils-1.30/scripts/Makefile
@@ -1,7 +1,7 @@
 # Installation directories.
 PREFIX ?= ${DESTDIR}/usr
 BINDIR ?= $(PREFIX)/bin
-SBINDIR ?= $(PREFIX)/sbin
+SBINDIR ?= $(DESTDIR)/sbin
 MANDIR ?= $(PREFIX)/share/man
 LOCALEDIR ?= /usr/share/locale
 
@@ -13,7 +13,7 @@
 	-mkdir -p $(BINDIR)
 	install -m 755 $(TARGETS) $(SBINDIR)
 	install -m 755 chcat $(BINDIR)
-	install -m 755 fixfiles $(DESTDIR)/sbin
+	install -m 755 fixfiles $(SBINDIR)
 	-mkdir -p $(MANDIR)/man8
 	install -m 644 fixfiles.8 $(MANDIR)/man8/
 	install -m 644 genhomedircon.8 $(MANDIR)/man8/
semanage/seobject.py +7 -8
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/semanage/seobject.py policycoreutils-1.30/semanage/seobject.py
@@ -229,10 +229,9 @@
 			if rc < 0:
 				raise ValueError("Could not set name for %s" % name)
 
-			if serange != "":
-				rc = semanage_seuser_set_mlsrange(self.sh, u, serange)
-				if rc < 0:
-					raise ValueError("Could not set MLS range for %s" % name)
+			rc = semanage_seuser_set_mlsrange(self.sh, u, serange)
+			if rc < 0:
+				raise ValueError("Could not set MLS range for %s" % name)
 
 			rc = semanage_seuser_set_sename(self.sh, u, sename)
 			if rc < 0:
@@ -549,7 +548,7 @@
 				raise ValueError("Could not list roles for user %s" % name)
 
 			roles = string.join(rlist, ' ');
-			ddict[semanage_user_get_name(u)] = (semanage_user_get_mlslevel(u), semanage_user_get_mlsrange(u), roles)
+			ddict[semanage_user_get_name(u)] = (semanage_user_get_prefix(u), semanage_user_get_mlslevel(u), semanage_user_get_mlsrange(u), roles)
 
 		return ddict
 
@@ -559,10 +558,10 @@
 		keys.sort()
 		if is_mls_enabled == 1:
 			if heading:
-				print "\n%-15s %-10s %-30s" % ("", "MLS/", "MLS/")
-				print "%-15s %-10s %-30s %s\n" % ("SELinux User", "MCS Level", "MCS Range", "SELinux Roles")
+				print "\n%-15s %-10s %-10s %-30s" % ("", "Labeling", "MLS/", "MLS/")
+				print "%-15s %-10s %-10s %-30s %s\n" % ("SELinux User", "Prefix", "MCS Level", "MCS Range", "SELinux Roles")
 			for k in keys:
-				print "%-15s %-10s %-30s %s" % (k, translate(ddict[k][0]), translate(ddict[k][1]), ddict[k][2])
+				print "%-15s %-10s %-10s %-30s %s" % (k, ddict[k][0], translate(ddict[k][1]), translate(ddict[k][2]), ddict[k][3])
 		else:
 			if heading:
 				print "%-15s %s\n" % ("SELinux User", "SELinux Roles")
#1 lore raw
Quoted 3 lines · Daniel J Walsh

This will give us the ability of instantly labeling the ~/public_html directory. So if a user logs in and does a
mkdir ~/public_html to tool will label the file correctly.

Do you have an estimate on how fast "instantly" is? A few second's delay in relabelling ~/public_html is probably not a big thing, but a delay in relabelling a just-created file to a more restrictive context may be opening a timing hole (for instance, allowing the grabbing of a just-created GPG or SSH key before the door is closed).

I'm suspecting the answer is "there's a small hard-to-hit hole" that people find acceptable risk (which I'm OK on, as long as we're honest about the size of the risk and how it works....)

#2 lore raw
Quoted 13 lines · [email protected] wrote:
Quoted 3 lines · Daniel J Walsh

This will give us the ability of instantly labeling the ~/public_html directory. So if a user logs in and does a mkdir ~/public_html to tool will label the file correctly.

Do you have an estimate on how fast "instantly" is? A few second's delay in relabelling ~/public_html is probably not a big thing, but a delay in relabelling a just-created file to a more restrictive context may be opening a timing hole (for instance, allowing the grabbing of a just-created GPG or SSH key before the door is closed).

Quoted 5 lines

I'm suspecting the answer is "there's a small hard-to-hit hole" that people find acceptable risk (which I'm OK on, as long as we're honest about the size of the risk and how it works....)

The answer is that is, if the file is created by a confined domain it will be instantly. SELinux
aware application also create it instantly. This is more for the non SELinux aware applicaitons. So the example of the user creating the public_html directory.

It happens very fast, as a matter of fact you can try this command to see it

rmdir public_html; mkdir public_html; ls -ldZ public_html
drwxrwxr-x dwalsh dwalsh user_u:object_r:httpd_sys_content_t public_html

This should not be considered a failsafe security measure, but more of a usability issue. If you have an file that is of critical secuirty you might not want to use this tool on it.

Dan

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#3 lore raw
Quoted 8 lines · Daniel J Walsh

The answer is that is, if the file is created by a confined domain it will be instantly. SELinux
aware application also create it instantly. This is more for the non SELinux aware applicaitons.
So the example of the user creating the public_html directory.

It happens very fast, as a matter of fact you can try this command to see it

Quoted 8 lines

rmdir public_html; mkdir public_html; ls -ldZ public_html
drwxrwxr-x dwalsh dwalsh user_u:object_r:httpd_sys_content_t
public_html

This should not be considered a failsafe security measure, but more of a usability issue.
If you have an file that is of critical secuirty you might not want to use this tool on it.

It shouldn't be applied to any directory writable by an untrusted entity (e.g. ~/public_html) unless you are taking some kind of safeguards to prevent it from being used as a way to relabel files outside the user's control via links.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#4 lore raw
Quoted 10 lines · Stephen Smalley
Quoted 8 lines · Daniel J Walsh

The answer is that is, if the file is created by a confined domain it will be instantly. SELinux
aware application also create it instantly. This is more for the non SELinux aware applicaitons. So the example of the user creating the public_html directory.

It happens very fast, as a matter of fact you can try this command to see it

rmdir public_html; mkdir public_html; ls -ldZ public_html
drwxrwxr-x dwalsh dwalsh user_u:object_r:httpd_sys_content_t public_html

Quoted 10 lines
Quoted 3 lines

This should not be considered a failsafe security measure, but more of a usability issue. If you have an file that is of critical secuirty you might not want to use this tool on it.

It shouldn't be applied to any directory writable by an untrusted entity (e.g. ~/public_html) unless you are taking some kind of safeguards to prevent it from being used as a way to relabel files outside the user's control via links.

Not sure what you mean. It is taking into account the users homedir. And the file. If public_html was not a directory it would be labeled user_home_t. I don't know how someone could cause the relabel to be a problem. I guess if the administrator was to start to add files in /tmp or ~/subdir/subdir/SecretFile. This could be a problem.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#5 lore raw
Quoted 7 lines · Daniel J Walsh
Quoted 6 lines · Stephen Smalley

It shouldn't be applied to any directory writable by an untrusted entity (e.g. ~/public_html) unless you are taking some kind of safeguards to prevent it from being used as a way to relabel files outside the user's control via links.

Quoted 5 lines

Not sure what you mean. It is taking into account the users homedir. And the file. If public_html was not a directory it would be labeled user_home_t. I don't know how someone could cause the relabel to be a
problem. I guess if the administrator was to start to add files in
/tmp or ~/subdir/subdir/SecretFile. This could be a problem.

restorecond, restorecon, and setfiles could benefit from a rewrite to follow the more paranoid conventions of other programs that walk the file tree (e.g. look at coreutils programs like rm -r logic, which has been modified a number of times in response to security-related issues). To date, restorecon and setfiles have simply relied on policy to prevent:
- untrustworthy domains from creating hard links to files that they shouldn't be able to access in the first place, and
- restorecon/setfiles from following untrustworthy symlinks.

And we originally only expected setfiles to be applied upon installation, not for normal runtime operation.

But the code itself could provide stronger safeguards against the threat, particularly now that you are automating the invocation of restorecon-like functionality in response to user events. Again, look to what has been done already in coreutils and elsewhere. There are also recently added new syscalls to help reduce races in walking the file tree (i.e. openat and friends) - possibly there should be some for lsetxattr as well so that lsetfileconat() could be implemented?

Under strict policy, the policy restrictions over creating hard links and over following sym links help counter the risk. Under targeted policy, users are unconfined by TE, so there is no direct benefit to a malicious user in tricking restorecond into relabeling a file to a different type. But now that users are supposed to be limited by MCS restrictions in -targeted, you have to consider the risk that a malicious user might try to use this avenue to get MCS categories dropped from some target file so that he can access it.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#6 lore raw
Quoted 21 lines · Stephen Smalley

restorecond, restorecon, and setfiles could benefit from a rewrite to follow the more paranoid conventions of other programs that walk the file tree (e.g. look at coreutils programs like rm -r logic, which has been modified a number of times in response to security-related issues). To date, restorecon and setfiles have simply relied on policy to prevent:
- untrustworthy domains from creating hard links to files that they shouldn't be able to access in the first place, and
- restorecon/setfiles from following untrustworthy symlinks.

And we originally only expected setfiles to be applied upon installation, not for normal runtime operation.

But the code itself could provide stronger safeguards against the threat, particularly now that you are automating the invocation of restorecon-like functionality in response to user events. Again, look to what has been done already in coreutils and elsewhere. There are also recently added new syscalls to help reduce races in walking the file tree (i.e. openat and friends) - possibly there should be some for lsetxattr as well so that lsetfileconat() could be implemented?

Quoted 8 lines

Under strict policy, the policy restrictions over creating hard links and over following sym links help counter the risk. Under targeted policy, users are unconfined by TE, so there is no direct benefit to a malicious user in tricking restorecond into relabeling a file to a different type. But now that users are supposed to be limited by MCS restrictions in -targeted, you have to consider the risk that a malicious user might try to use this avenue to get MCS categories dropped from some target file so that he can access it.

BTW, on the FreeBSD side, they appear to have ported/rewritten the setfiles logic in their setfmac utility,
http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.sbin/setfmac/setfmac.c

They chose to use fts(3) rather than nftw(3) to walk the file tree, and use fts_accpath to access the file from the current directory.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#10 lore raw
In reply to Daniel J Walsh

restorecond/restorecond.c line 160

Quoted 4 lines · Daniel J Walsh

diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.c policycoreutils-1.30/restorecond/restorecond.c

restorecond/restorecond.c +1 -0
+	if (S_ISLNK(st.st_mode)) {

For the daemon, I don't think you want this symlink realpath logic inherited from restorecon. The admin should just specify the real path in the config file unless he wants the symlink itself to be restored.

Quoted 3 lines
restorecond/restorecond.c +3 -0
+	retcontext=lgetfilecon(filename,&prev_context);
+	
+	if (retcontext >= 0 || errno == ENODATA) {

<snip>

restorecond/restorecond.c line 217

Quoted 1 line
restorecond/restorecond.c +1 -0
+			if (lsetfilecon(filename,scontext) < 0) {

Need to counter the risks I noted earlier for files in user-writable directories. There is both a DAC concern (user should only be able to relabel files he owns) and a MAC concern here (user should only be able to relabel files to which he has suitable permission), and the inode referenced by filename can change between these two calls.

scripts/fixfiles line 131

Quoted 13 lines

diff --exclude-from=exclude -N -u -r nsapolicycoreutils/scripts/fixfiles policycoreutils-1.30/scripts/fixfiles

scripts/fixfiles +5 -1
@@ -124,7 +124,15 @@
     exit $?
 fi
 if [ ! -z "$DIRS" ]; then
-    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} -R $1 -v $DIRS 2>&1 >> $LOGFILE
+    if [ -x /usr/bin/find ]; then
+	for d in ${DIRS} ; do find $d \
+	    ! \( -fstype ext2 -o -fstype ext3 -o -fstype jfs -o -fstype xfs \) -prune  -o -print; \
+	    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} $1 -v -f - 2>&1 >> $LOGFILE
+	done

Am I reading this incorrectly, or does the find command just generate output to the stdout stream above, not piped into restorecon? I think I saw this diff earlier and dropped it because it doesn't work.

Quoted 4 lines

diff --exclude-from=exclude -N -u -r nsapolicycoreutils/semanage/seobject.py policycoreutils-1.30/semanage/seobject.py

semanage/seobject.py line 234

Quoted 10 lines
semanage/seobject.py +3 -4
@@ -229,10 +229,9 @@
 			if rc < 0:
 				raise ValueError("Could not set name for %s" % name)
 
-			if serange != "":
-				rc = semanage_seuser_set_mlsrange(self.sh, u, serange)
-				if rc < 0:
-					raise ValueError("Could not set MLS range for %s" % name)
+			rc = semanage_seuser_set_mlsrange(self.sh, u, serange)
+			if rc < 0:
+				raise ValueError("Could not set MLS range for %s" % name)

This looks like a reversion of a change I made to get semanage working on non-MLS/MCS systems - calling semanage_seuser_set_mlsrange with an empty string yields trouble later.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#8 lore raw
In reply to Stephen Smalley
Quoted 4 lines · Stephen Smalley

There are
also recently added new syscalls to help reduce races in walking the file tree (i.e. openat and friends) - possibly there should be some for lsetxattr as well so that lsetfileconat() could be implemented?

Yes, I think a lsetxattrat() syscall is necessary, and I agree that we need better code to deal with race conditions in the library.

Incidentally it's the first time I've heard of coreutils being cited as a good example in this regard.

Signature · 9 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

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#9 lore raw
Quoted 11 lines · Russell Coker
Quoted 4 lines · Stephen Smalley

There are
also recently added new syscalls to help reduce races in walking the file tree (i.e. openat and friends) - possibly there should be some for lsetxattr as well so that lsetfileconat() could be implemented?

Yes, I think a lsetxattrat() syscall is necessary, and I agree that we need better code to deal with race conditions in the library.

Incidentally it's the first time I've heard of coreutils being cited as a good example in this regard.

Not saying that they necessarily have it right, but they have dealt with a number of security bug reports over time in this space (particularly for rm -rf behavior), so it wouldn't hurt to look at their approach. Likewise for findutils. Unfortunately, they each seem to have their own specialized logic. Converting setfiles and restorecon over to using fts(3) ala the parallel setfmac utility in FreeBSD might help.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#11 lore raw
In reply to Stephen Smalley
Quoted 19 lines · Stephen Smalley

restorecond/restorecond.c

Quoted 5 lines · Daniel J Walsh

diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.c policycoreutils-1.30/restorecond/restorecond.c

restorecond/restorecond.c +1 -0
+	if (S_ISLNK(st.st_mode)) {

For the daemon, I don't think you want this symlink realpath logic inherited from restorecon. The admin should just specify the real path in the config file unless he wants the symlink itself to be restored.

Quoted 4 lines
restorecond/restorecond.c +3 -0
+	retcontext=lgetfilecon(filename,&prev_context);
+	
+	if (retcontext >= 0 || errno == ENODATA) {

<snip>

Quoted 10 lines

restorecond/restorecond.c

Quoted 2 lines
restorecond/restorecond.c +1 -0
+			if (lsetfilecon(filename,scontext) < 0) {

Need to counter the risks I noted earlier for files in user-writable directories. There is both a DAC concern (user should only be able to relabel files he owns) and a MAC concern here (user should only be able to relabel files to which he has suitable permission), and the inode referenced by filename can change between these two calls.

Removed realpath stuff as well as LINK Handling.

Also added a check to make sure the file was not multiply linked. If multiply linked it will syslog the failure.

Quoted 10 lines
Quoted 10 lines

diff --exclude-from=exclude -N -u -r nsapolicycoreutils/scripts/fixfiles policycoreutils-1.30/scripts/fixfiles

scripts/fixfiles +2 -1
@@ -124,7 +124,15 @@
     exit $?
 fi
 if [ ! -z "$DIRS" ]; then
-    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} -R $1 -v $DIRS 2>&1 >> $LOGFILE
+    if [ -x /usr/bin/find ]; then
+	for d in ${DIRS} ; do find $d \
Quoted 9 lines

scripts/fixfiles line 132

Quoted 4 lines
scripts/fixfiles +3 -0
+	    ! \( -fstype ext2 -o -fstype ext3 -o -fstype jfs -o -fstype xfs \) -prune  -o -print; \
+	    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} $1 -v -f - 2>&1 >> $LOGFILE
+	done
 

Am I reading this incorrectly, or does the find command just generate output to the stdout stream above, not piped into restorecon? I think I saw this diff earlier and dropped it because it doesn't work.

Fixed ";" should be "|"

+ ! \( -fstype ext2 -o -fstype ext3 -o -fstype jfs -o -fstype xfs \) -prune -o -print | \

Quoted 7 lines
Quoted 7 lines

diff --exclude-from=exclude -N -u -r nsapolicycoreutils/semanage/seobject.py policycoreutils-1.30/semanage/seobject.py

semanage/seobject.py +0 -0
@@ -229,10 +229,9 @@
 			if rc < 0:
 				raise ValueError("Could not set name for %s" % name)
 
Quoted 14 lines

semanage/seobject.py line 235

Quoted 8 lines
semanage/seobject.py +3 -4
-			if serange != "":
-				rc = semanage_seuser_set_mlsrange(self.sh, u, serange)
-				if rc < 0:
-					raise ValueError("Could not set MLS range for %s" % name)
+			rc = semanage_seuser_set_mlsrange(self.sh, u, serange)
+			if rc < 0:
+				raise ValueError("Could not set MLS range for %s" % name)
 

This looks like a reversion of a change I made to get semanage working on non-MLS/MCS systems - calling semanage_seuser_set_mlsrange with an empty string yields trouble later.

Removed

Makefile +1 -1
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/Makefile policycoreutils-1.30/Makefile
@@ -1,4 +1,4 @@
-SUBDIRS=setfiles semanage load_policy newrole run_init restorecon audit2allow audit2why scripts sestatus semodule_package semodule semodule_link semodule_expand setsebool po
+SUBDIRS=setfiles semanage load_policy newrole run_init restorecon restorecond audit2allow audit2why scripts sestatus semodule_package semodule semodule_link semodule_expand setsebool po
 
 all install relabel clean: 
 	@for subdir in $(SUBDIRS); do \
restorecond/Makefile +29 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/Makefile policycoreutils-1.30/restorecond/Makefile
@@ -0,0 +1,29 @@
+# Installation directories.
+PREFIX ?= ${DESTDIR}/usr
+SBINDIR ?= $(PREFIX)/sbin
+MANDIR = $(PREFIX)/share/man
+INITDIR = $(DESTDIR)/etc/rc.d/init.d
+SELINUXDIR = $(DESTDIR)/etc/selinux
+
+CFLAGS ?= -g -Werror -Wall -W
+override CFLAGS += -I$(PREFIX)/include -D_FILE_OFFSET_BITS=64
+LDLIBS += -lselinux -L$(PREFIX)/lib
+
+all: restorecond
+
+restorecond:  restorecond.o utmpwatcher.o stringslist.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+install: all
+	[ -d $(MANDIR)/man8 ] || mkdir -p $(MANDIR)/man8
+	-mkdir -p $(SBINDIR)
+	install -m 755 restorecond $(SBINDIR)
+	install -m 644 restorecond.8 $(MANDIR)/man8
+	-mkdir -p $(INITDIR)
+	install -m 644 restorecond.init $(INITDIR)/restorecond
+	-mkdir -p $(SELINUXDIR)
+	install -m 600 restorecond.conf $(SELINUXDIR)/restorecond.conf
+
+clean:
+	-rm -f restorecond *.o *~
+
restorecond/restorecond.8 +31 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.8 policycoreutils-1.30/restorecond/restorecond.8
@@ -0,0 +1,31 @@
+.TH "restorecond" "8" "2002031409" "" ""
+.SH "NAME"
+restorecond \- daemon that watches for file creation and then corrects file context
+
+.SH "SYNOPSIS"
+.B restorecond  [\-d]
+.P
+
+.SH "DESCRIPTION"
+This manual page describes the
+.BR restorecond
+program.
+.P
+This daemon uses inotify to watch files listed in the /etc/selinux/POLICYTYPE/restorconfiles.conf, when they are created, this daemon will make sure they have 
+the correct file context associated with the policy.
+
+.SH "OPTIONS"
+.TP 
+.B \-d
+Turns on debugging mode.   Application will stay in the foreground and lots of
+debugs messages start printing.
+
+.SH "AUTHOR"
+This man page was written by Dan Walsh <[email protected]>.
+The program was written by Dan Walsh <[email protected]>.
+
+.SH "FILES"
+/etc/selinux/POLICYTYPE/restorconfiles.conf
+
+.SH "SEE ALSO"
+.BR restorecon (8),
restorecond/restorecond.c +436 -0

436 lines not on this page · raw message

restorecond/restorecond.conf +3 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.conf policycoreutils-1.30/restorecond/restorecond.conf
@@ -0,0 +1,3 @@
+/etc/resolv.conf
+/etc/mtab
+~/public_html
restorecond/restorecond.h +31 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.h policycoreutils-1.30/restorecond/restorecond.h
@@ -0,0 +1,31 @@
+/* restorecond.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+
+#ifndef RESTORED_CONFIG_H
+#define RESTORED_CONFIG_H
+
+void exitApp(const char *msg);
+void watch_list_add(int inotify_fd, const char *path);
+
+#endif
+
restorecond/restorecond.init +57 -0

57 lines not on this page · raw message

restorecond/stringslist.c +118 -0

118 lines not on this page · raw message

restorecond/stringslist.h +37 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/stringslist.h policycoreutils-1.30/restorecond/stringslist.h
@@ -0,0 +1,37 @@
+/* stringslist.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+#ifndef STRINGSLIST_H
+#define STRINGSLIST_H
+
+struct stringsList {
+	struct stringsList *next;
+	char *string;
+};
+
+void strings_list_free(struct stringsList *list);
+void strings_list_add(struct stringsList **list, const char *string);
+void strings_list_print(struct stringsList *list);
+int strings_list_find(struct stringsList *list, const char *string);
+int strings_list_diff(struct stringsList *from, struct stringsList *to);
+
+#endif
restorecond/utmpwatcher.c +105 -0

105 lines not on this page · raw message

restorecond/utmpwatcher.h +29 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/utmpwatcher.h policycoreutils-1.30/restorecond/utmpwatcher.h
@@ -0,0 +1,29 @@
+/* utmpwatcher.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+#ifndef UTMPWATCHER_H
+#define UTMPWATCHER_H
+
+unsigned int utmpwatcher_handle(int inotify_fd, int wd);
+void utmpwatcher_add(int inotify_fd, const char *path);
+
+#endif
scripts/fixfiles +9 -1
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/scripts/fixfiles policycoreutils-1.30/scripts/fixfiles
@@ -124,7 +124,15 @@
     exit $?
 fi
 if [ ! -z "$DIRS" ]; then
-    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} -R $1 -v $DIRS 2>&1 >> $LOGFILE
+    if [ -x /usr/bin/find ]; then
+	for d in ${DIRS} ; do find $d \
+	    ! \( -fstype ext2 -o -fstype ext3 -o -fstype jfs -o -fstype xfs \) -prune  -o -print | \
+	    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} $1 -v -f - 2>&1 >> $LOGFILE
+	done
+    else
+	${RESTORECON} ${OUTFILES} ${FORCEFLAG} -R $1 -v $DIRS 2>&1 >> $LOGFILE
+    fi
+
     exit $?
 fi
 LogReadOnly
semanage/seobject.py +4 -4
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/semanage/seobject.py policycoreutils-1.30/semanage/seobject.py
@@ -549,7 +548,7 @@
 				raise ValueError("Could not list roles for user %s" % name)
 
 			roles = string.join(rlist, ' ');
-			ddict[semanage_user_get_name(u)] = (semanage_user_get_mlslevel(u), semanage_user_get_mlsrange(u), roles)
+			ddict[semanage_user_get_name(u)] = (semanage_user_get_prefix(u), semanage_user_get_mlslevel(u), semanage_user_get_mlsrange(u), roles)
 
 		return ddict
 
@@ -559,10 +558,10 @@
 		keys.sort()
 		if is_mls_enabled == 1:
 			if heading:
-				print "\n%-15s %-10s %-30s" % ("", "MLS/", "MLS/")
-				print "%-15s %-10s %-30s %s\n" % ("SELinux User", "MCS Level", "MCS Range", "SELinux Roles")
+				print "\n%-15s %-10s %-10s %-30s" % ("", "Labeling", "MLS/", "MLS/")
+				print "%-15s %-10s %-10s %-30s %s\n" % ("SELinux User", "Prefix", "MCS Level", "MCS Range", "SELinux Roles")
 			for k in keys:
-				print "%-15s %-10s %-30s %s" % (k, translate(ddict[k][0]), translate(ddict[k][1]), ddict[k][2])
+				print "%-15s %-10s %-10s %-30s %s" % (k, ddict[k][0], translate(ddict[k][1]), translate(ddict[k][2]), ddict[k][3])
 		else:
 			if heading:
 				print "%-15s %s\n" % ("SELinux User", "SELinux Roles")
#15 lore raw
In reply to Daniel J Walsh
Quoted 16 lines · Daniel J Walsh

diff --exclude-from=exclude -N -u -r nsapolicycoreutils/semanage/seobject.py policycoreutils-1.30/semanage/seobject.py

semanage/seobject.py +1 -1
@@ -549,7 +548,7 @@
 				raise ValueError("Could not list roles for user %s" % name)
 
 			roles = string.join(rlist, ' ');
-			ddict[semanage_user_get_name(u)] = (semanage_user_get_mlslevel(u), semanage_user_get_mlsrange(u), roles)
+			ddict[semanage_user_get_name(u)] = (semanage_user_get_prefix(u), semanage_user_get_mlslevel(u), semanage_user_get_mlsrange(u), roles)
 
 		return ddict
 
@@ -559,10 +558,10 @@
 		keys.sort()
 		if is_mls_enabled == 1:
 			if heading:

semanage/seobject.py line 567

Quoted 10 lines
semanage/seobject.py +3 -3
-				print "\n%-15s %-10s %-30s" % ("", "MLS/", "MLS/")
-				print "%-15s %-10s %-30s %s\n" % ("SELinux User", "MCS Level", "MCS Range", "SELinux Roles")
+				print "\n%-15s %-10s %-10s %-30s" % ("", "Labeling", "MLS/", "MLS/")
+				print "%-15s %-10s %-10s %-30s %s\n" % ("SELinux User", "Prefix", "MCS Level", "MCS Range", "SELinux Roles")
 			for k in keys:
-				print "%-15s %-10s %-30s %s" % (k, translate(ddict[k][0]), translate(ddict[k][1]), ddict[k][2])
+				print "%-15s %-10s %-10s %-30s %s" % (k, ddict[k][0], translate(ddict[k][1]), translate(ddict[k][2]), ddict[k][3])
 		else:
 			if heading:
 				print "%-15s %s\n" % ("SELinux User", "SELinux Roles")

Merged this part of the diff (Ivan's earlier patch for semanage user -l to display labeling prefixes). However, he noted two concerns with it:
1) very long lines in the display, and 2) use of get_all by chcat could be broken by this kind of change within seobject.py.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#12 lore raw
In reply to Daniel J Walsh
Quoted 4 lines · Daniel J Walsh

Removed realpath stuff as well as LINK Handling.

Also added a check to make sure the file was not multiply linked. If multiply linked it will syslog the failure.

restorecond/restorecond.c +0 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.c policycoreutils-1.30/restorecond/restorecond.c

<snip>
+ if (lstat(filename, &st)!=0) return;
+
+ if (st.st_nlink > 1) {
+ syslog(LOG_ERR,"Will not restore a file with more than one hard link (%s) %s\n", filename,strerror(errno));
+ return;
+ }
+
+ if (matchpathcon(filename, st.st_mode, &scontext) < 0) {
+ if (errno == ENOENT)
+ return;
+ syslog(LOG_ERR,"matchpathcon(%s) failed %s\n", filename,strerror(errno));
+ return;
+ }
+ retcontext=lgetfilecon(filename,&prev_context);
+
+ if (retcontext >= 0 || errno == ENODATA) {
<snip>
+ if (lsetfilecon(filename,scontext) < 0) {
+

One obvious problem here is that the above sequence isn't necessarily acting on the same inode, so the file that is ultimately relabeled by lsetfilecon could still be a link to a different user's file. An option for that problem is to use a open(..O_NOFOLLOW)+fstat+fgetfilecon +fsetfilecon+close sequence so that you are guaranteed to be operating on a single file throughout the sequence. But this requires at least read permission to the file to be allowed to restorecond for the open() call, which is why we didn't take that approach in setfiles (as it would then require read access to all files). restorecond is possibly operating on a much smaller set of files whose types can be enumerated/identified by an interface/attribute.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#13 lore raw
Quoted 30 lines · Stephen Smalley
Quoted 4 lines · Daniel J Walsh

Removed realpath stuff as well as LINK Handling.

Also added a check to make sure the file was not multiply linked. If multiply linked it will syslog the failure.

diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.c policycoreutils-1.30/restorecond/restorecond.c

restorecond/restorecond.c +18 -0
<snip>
+	if (lstat(filename, &st)!=0) return;
+
+	if (st.st_nlink > 1) {
+		syslog(LOG_ERR,"Will not restore a file with more than one hard link (%s) %s\n", filename,strerror(errno));
+		return;
+	}
+
+	if (matchpathcon(filename, st.st_mode, &scontext) < 0) {
+		if (errno == ENOENT)
+			return;
+		syslog(LOG_ERR,"matchpathcon(%s) failed %s\n", filename,strerror(errno));
+		return;
+	} 
+	retcontext=lgetfilecon(filename,&prev_context);
+	
+	if (retcontext >= 0 || errno == ENODATA) {
<snip>
+			if (lsetfilecon(filename,scontext) < 0) {
+
Quoted 11 lines

One obvious problem here is that the above sequence isn't necessarily acting on the same inode, so the file that is ultimately relabeled by lsetfilecon could still be a link to a different user's file. An option for that problem is to use a open(..O_NOFOLLOW)+fstat+fgetfilecon +fsetfilecon+close sequence so that you are guaranteed to be operating on a single file throughout the sequence. But this requires at least read permission to the file to be allowed to restorecond for the open() call, which is why we didn't take that approach in setfiles (as it would then require read access to all files). restorecond is possibly operating on a much smaller set of files whose types can be enumerated/identified by an interface/attribute.

Also, your hard link check above will prevent relabeling of directories altogether, right? So you would need to at least distinguish the S_IFDIR case.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#14 lore raw

Opens file and uses file descriptor for checking and restoring,

Checks if lnk>1 and not a directory.

Update patch to latest version of policycoreutils.

Also added audit2allow section to allow it to look for dontaudit rules as well as allow rules when translating reference policy.

audit2allow/audit2allow +7 -9
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/audit2allow/audit2allow policycoreutils-1.30/audit2allow/audit2allow
@@ -27,15 +27,14 @@
 import commands, sys, os, pwd, string, getopt, re, selinux
 
 obj="(\{[^\}]*\}|[^ \t:]*)"
-allow_regexp="allow[ \t]+%s[ \t]*%s[ \t]*:[ \t]*%s[ \t]*%s" % (obj, obj, obj, obj)
-
+allow_regexp="(allow|dontaudit)[ \t]+%s[ \t]*%s[ \t]*:[ \t]*%s[ \t]*%s" % (obj, obj, obj, obj)
 awk_script='/^[[:blank:]]*interface[[:blank:]]*\(/ {\n\
         IFACEFILE=FILENAME\n\
 	IFACENAME = gensub("^[[:blank:]]*interface[[:blank:]]*\\\\(\`?","","g",$0);\n\
 	IFACENAME = gensub("\'?,.*$","","g",IFACENAME);\n\
 }\n\
 \n\
-/^[[:blank:]]*allow[[:blank:]]+.*;[[:blank:]]*$/ {\n\
+/^[[:blank:]]*(allow|dontaudit)[[:blank:]]+.*;[[:blank:]]*$/ {\n\
 \n\
   if ((length(IFACENAME) > 0) && (IFACEFILE == FILENAME)){\n\
 		ALLOW = gensub("^[[:blank:]]*","","g",$0)\n\
@@ -84,14 +83,13 @@
                 m=re.match(regexp,r)
                 if m==None:
                     continue
-                else:
-                    val=m.groups()
+                val=m.groups()
                 file=os.path.basename(val[0]).split(".")[0]
                 iface=val[1]
-                Scon=val[2].split()
-                Tcon=val[3].split()
-                Class=val[4].split()
-                Access=trans.get(val[5].split())
+                Scon=val[3].split()
+                Tcon=val[4].split()
+                Class=val[5].split()
+                Access=trans.get(val[6].split())
                 for s in Scon:
                     for t in Tcon:
                         for c in Class:
Makefile +1 -1
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/Makefile policycoreutils-1.30/Makefile
@@ -1,4 +1,4 @@
-SUBDIRS=setfiles semanage load_policy newrole run_init restorecon audit2allow audit2why scripts sestatus semodule_package semodule semodule_link semodule_expand setsebool po
+SUBDIRS=setfiles semanage load_policy newrole run_init restorecon restorecond audit2allow audit2why scripts sestatus semodule_package semodule semodule_link semodule_expand setsebool po
 
 all install relabel clean: 
 	@for subdir in $(SUBDIRS); do \
restorecond/Makefile +29 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/Makefile policycoreutils-1.30/restorecond/Makefile
@@ -0,0 +1,29 @@
+# Installation directories.
+PREFIX ?= ${DESTDIR}/usr
+SBINDIR ?= $(PREFIX)/sbin
+MANDIR = $(PREFIX)/share/man
+INITDIR = $(DESTDIR)/etc/rc.d/init.d
+SELINUXDIR = $(DESTDIR)/etc/selinux
+
+CFLAGS ?= -g -Werror -Wall -W
+override CFLAGS += -I$(PREFIX)/include -D_FILE_OFFSET_BITS=64
+LDLIBS += -lselinux -L$(PREFIX)/lib
+
+all: restorecond
+
+restorecond:  restorecond.o utmpwatcher.o stringslist.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+install: all
+	[ -d $(MANDIR)/man8 ] || mkdir -p $(MANDIR)/man8
+	-mkdir -p $(SBINDIR)
+	install -m 755 restorecond $(SBINDIR)
+	install -m 644 restorecond.8 $(MANDIR)/man8
+	-mkdir -p $(INITDIR)
+	install -m 644 restorecond.init $(INITDIR)/restorecond
+	-mkdir -p $(SELINUXDIR)
+	install -m 600 restorecond.conf $(SELINUXDIR)/restorecond.conf
+
+clean:
+	-rm -f restorecond *.o *~
+
restorecond/restorecond.8 +31 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.8 policycoreutils-1.30/restorecond/restorecond.8
@@ -0,0 +1,31 @@
+.TH "restorecond" "8" "2002031409" "" ""
+.SH "NAME"
+restorecond \- daemon that watches for file creation and then corrects file context
+
+.SH "SYNOPSIS"
+.B restorecond  [\-d]
+.P
+
+.SH "DESCRIPTION"
+This manual page describes the
+.BR restorecond
+program.
+.P
+This daemon uses inotify to watch files listed in the /etc/selinux/POLICYTYPE/restorconfiles.conf, when they are created, this daemon will make sure they have 
+the correct file context associated with the policy.
+
+.SH "OPTIONS"
+.TP 
+.B \-d
+Turns on debugging mode.   Application will stay in the foreground and lots of
+debugs messages start printing.
+
+.SH "AUTHOR"
+This man page was written by Dan Walsh <[email protected]>.
+The program was written by Dan Walsh <[email protected]>.
+
+.SH "FILES"
+/etc/selinux/POLICYTYPE/restorconfiles.conf
+
+.SH "SEE ALSO"
+.BR restorecon (8),
restorecond/restorecond.c +451 -0

451 lines not on this page · raw message

restorecond/restorecond.conf +3 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.conf policycoreutils-1.30/restorecond/restorecond.conf
@@ -0,0 +1,3 @@
+/etc/resolv.conf
+/etc/mtab
+~/public_html
restorecond/restorecond.h +31 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/restorecond.h policycoreutils-1.30/restorecond/restorecond.h
@@ -0,0 +1,31 @@
+/* restorecond.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+
+#ifndef RESTORED_CONFIG_H
+#define RESTORED_CONFIG_H
+
+void exitApp(const char *msg);
+void watch_list_add(int inotify_fd, const char *path);
+
+#endif
+
restorecond/restorecond.init +61 -0

61 lines not on this page · raw message

restorecond/stringslist.c +118 -0

118 lines not on this page · raw message

restorecond/stringslist.h +37 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/stringslist.h policycoreutils-1.30/restorecond/stringslist.h
@@ -0,0 +1,37 @@
+/* stringslist.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+#ifndef STRINGSLIST_H
+#define STRINGSLIST_H
+
+struct stringsList {
+	struct stringsList *next;
+	char *string;
+};
+
+void strings_list_free(struct stringsList *list);
+void strings_list_add(struct stringsList **list, const char *string);
+void strings_list_print(struct stringsList *list);
+int strings_list_find(struct stringsList *list, const char *string);
+int strings_list_diff(struct stringsList *from, struct stringsList *to);
+
+#endif
restorecond/utmpwatcher.c +105 -0

105 lines not on this page · raw message

restorecond/utmpwatcher.h +29 -0
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/restorecond/utmpwatcher.h policycoreutils-1.30/restorecond/utmpwatcher.h
@@ -0,0 +1,29 @@
+/* utmpwatcher.h -- 
+ * Copyright 2006 Red Hat Inc., Durham, North Carolina.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Authors:
+ *   Dan Walsh <[email protected]>
+ * 
+ */
+#ifndef UTMPWATCHER_H
+#define UTMPWATCHER_H
+
+unsigned int utmpwatcher_handle(int inotify_fd, int wd);
+void utmpwatcher_add(int inotify_fd, const char *path);
+
+#endif
scripts/fixfiles +9 -1
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/scripts/fixfiles policycoreutils-1.30/scripts/fixfiles
@@ -124,7 +124,15 @@
     exit $?
 fi
 if [ ! -z "$DIRS" ]; then
-    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} -R $1 -v $DIRS 2>&1 >> $LOGFILE
+    if [ -x /usr/bin/find ]; then
+	for d in ${DIRS} ; do find $d \
+	    ! \( -fstype ext2 -o -fstype ext3 -o -fstype jfs -o -fstype xfs \) -prune  -o -print | \
+	    ${RESTORECON} ${OUTFILES} ${FORCEFLAG} $1 -v -f - 2>&1 >> $LOGFILE
+	done
+    else
+	${RESTORECON} ${OUTFILES} ${FORCEFLAG} -R $1 -v $DIRS 2>&1 >> $LOGFILE
+    fi
+
     exit $?
 fi
 LogReadOnly
#7 lore raw
In reply to Stephen Smalley
Quoted 33 lines · Stephen Smalley
Quoted 21 lines · Stephen Smalley

restorecond, restorecon, and setfiles could benefit from a rewrite to follow the more paranoid conventions of other programs that walk the file tree (e.g. look at coreutils programs like rm -r logic, which has been modified a number of times in response to security-related issues). To date, restorecon and setfiles have simply relied on policy to prevent:
- untrustworthy domains from creating hard links to files that they shouldn't be able to access in the first place, and
- restorecon/setfiles from following untrustworthy symlinks.

And we originally only expected setfiles to be applied upon installation, not for normal runtime operation.

But the code itself could provide stronger safeguards against the threat, particularly now that you are automating the invocation of restorecon-like functionality in response to user events. Again, look to what has been done already in coreutils and elsewhere. There are also recently added new syscalls to help reduce races in walking the file tree (i.e. openat and friends) - possibly there should be some for lsetxattr as well so that lsetfileconat() could be implemented?

Quoted 9 lines

Under strict policy, the policy restrictions over creating hard links and over following sym links help counter the risk. Under targeted policy, users are unconfined by TE, so there is no direct benefit to a malicious user in tricking restorecond into relabeling a file to a different type. But now that users are supposed to be limited by MCS restrictions in -targeted, you have to consider the risk that a malicious user might try to use this avenue to get MCS categories dropped from some target file so that he can access it.

Quoted 8 lines

BTW, on the FreeBSD side, they appear to have ported/rewritten the setfiles logic in their setfmac utility,
http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.sbin/setfmac/setfmac.c

They chose to use fts(3) rather than nftw(3) to walk the file tree, and use fts_accpath to access the file from the current directory.

Yes this would be a good idea, since we could do the equivalent of find -prune, when we encounter a file system that does not support extended attributes.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.