Skip to contentSkip to conversation

setfiles segfault

PATCH Chris PeBenito 2 participants 2 messages Apr 8, 2005 SELinux security module

lore
Share & tools
lore
#0 lore raw

On the current setfiles, if you do altroot, and you point it at an empty directory, it segfaults:

gorn setfiles # ./setfiles /etc/security/selinux/file_contexts -d -r /tmp/empty/ /tmp/empty/ ./setfiles: labeling files, pretending /tmp/empty is /
./setfiles: labeling files under /tmp/empty/
Segmentation fault
gorn setfiles # touch /tmp/empty/foo
gorn setfiles # ./setfiles /etc/security/selinux/file_contexts -d -r /tmp/empty/ /tmp/empty/

./setfiles:  labeling files, pretending /tmp/empty is /
./setfiles:  labeling files under /tmp/empty/
./setfiles:  /tmp/empty/foo matched by system_u:object_r:default_t

matchpathcon_filespec_eval: hash table stats: 1 elements, 1/65536 buckets used, longest chain length 1 ./setfiles: Done.

As evidenced above, the altroot directory does not match the root directory correctly. Any trailing /'s are stripped by the time the path gets used in the match() call, which is correct. But then if there is an altroot, the name pointer is advanced by the length of the root path string to strip off the altroot path, for matching. This works in all cases, except for the root directory, which becomes an empty string since the path is the same as the altroot path. Since the lstat() in the function uses the true path, it succeeds, but the matchpathcon_index() fails, leaving the hashtable empty (fl_head is null). Then when we get back to main and do the matchpathcon_filespec_destroy(), the function tries to free a null pointer, causing the segfault.

The attached patch fixes this condition, by checking if the name is an empty string. I initially considered concatenating on a / in this case, rather then the current implementation, but this won't work since all the strings are const. It also removes buf from the function, as it seems redundant, since the one operation done to buf is also done to name, and buf is just a copy of name. This patch is against sourceforge cvs.

 setfiles.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
Signature · 8 lines
Chris PeBenito
<[email protected]>
Developer,
Hardened Gentoo Linux
Embedded Gentoo Linux
 
Public Key: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xE6AF9243
Key fingerprint = B0E6 877A 883F A57A 8E6A  CB00 BC8E E42D E6AF 9243

Index: setfiles/setfiles.c
===================================================================
RCS file: /cvsroot/selinux/nsa/selinux-usr/policycoreutils/setfiles/setfiles.c,v retrieving revision 1.27

setfiles.c +5 -3
diff -u -r1.27 setfiles.c
@@ -164,7 +164,6 @@
 {
 	int ret;
 	const char *fullname = name;
-	const char *buf = name;
 
 	/* fullname will be the real file that gets labeled
 	 * name will be what is matched in the policy */
@@ -175,7 +174,6 @@
 			return -1;
 		}
 		name += rootpathlen;
-		buf += rootpathlen;
 	}
 
 	if (excludeCtr > 0) {
@@ -190,7 +188,11 @@
 		return -1;
 	}
 
-	return matchpathcon_index(buf, sb->st_mode, con);
+	if(rootpath != NULL && name[0]=='\0')
+		/* this is actually the root dir of the alt root */
+		return matchpathcon_index("/", sb->st_mode, con);
+	else
+		return matchpathcon_index(name, sb->st_mode, con);
 }
 
 void usage(const char * const name)
#1 lore raw
Quoted 36 lines · Chris PeBenito

On the current setfiles, if you do altroot, and you point it at an empty directory, it segfaults:

gorn setfiles # ./setfiles /etc/security/selinux/file_contexts -d -r /tmp/empty/ /tmp/empty/ ./setfiles: labeling files, pretending /tmp/empty is /
./setfiles: labeling files under /tmp/empty/
Segmentation fault
gorn setfiles # touch /tmp/empty/foo
gorn setfiles # ./setfiles /etc/security/selinux/file_contexts -d -r /tmp/empty/ /tmp/empty/

./setfiles:  labeling files, pretending /tmp/empty is /
./setfiles:  labeling files under /tmp/empty/
./setfiles:  /tmp/empty/foo matched by system_u:object_r:default_t

matchpathcon_filespec_eval: hash table stats: 1 elements, 1/65536 buckets used, longest chain length 1 ./setfiles: Done.

As evidenced above, the altroot directory does not match the root directory correctly. Any trailing /'s are stripped by the time the path gets used in the match() call, which is correct. But then if there is an altroot, the name pointer is advanced by the length of the root path string to strip off the altroot path, for matching. This works in all cases, except for the root directory, which becomes an empty string since the path is the same as the altroot path. Since the lstat() in the function uses the true path, it succeeds, but the matchpathcon_index() fails, leaving the hashtable empty (fl_head is null). Then when we get back to main and do the matchpathcon_filespec_destroy(), the function tries to free a null pointer, causing the segfault.

The attached patch fixes this condition, by checking if the name is an empty string. I initially considered concatenating on a / in this case, rather then the current implementation, but this won't work since all the strings are const. It also removes buf from the function, as it seems redundant, since the one operation done to buf is also done to name, and buf is just a copy of name. This patch is against sourceforge cvs.

Quoted 2 lines
 setfiles.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

Thanks, merged into policycoreutils 1.23.4. I also modified matchpathcon_filespec_destroy in libselinux 1.23.6 to check for a null fl_head on entry.

Signature · 8 lines
Stephen Smalley <[email protected]>
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.