Skip to contentSkip to conversation

policycoreutils: get setfiles to skip mounts without seclabel

PATCH In review Thomas Liu 3 participants 7 messages Jul 14, 2009 – Jul 27, 2009 SELinux security module

lore
Share & tools
lore

Thomas Liu

author
#0 lore raw

PATCH 1/1 policycoreutils: get setfiles to skip mounts without seclabel

Get setfiles to check paths for seclabel and skip them if it is not supported.

Parse /proc/mounts and add paths that do not have seclabel to the exclude list. If another path shows up that does have seclabel, remove it from the exclude list, since setfiles will try and when it fails it will skip it.

Also made one of the error messages in add_exclude more descriptive.

---

 policycoreutils/setfiles/setfiles.c |   80 ++++++++++++++++++++++++++++++++++-
 1 files changed, 77 insertions(+), 3 deletions(-)
policycoreutils/setfiles/setfiles.c +77 -3
index 1356fb9..9f27a1d 100644
@@ -11,6 +11,7 @@
 #include <ctype.h>
 #include <regex.h>
 #include <sys/vfs.h>
+#include <sys/utsname.h>
 #define __USE_XOPEN_EXTENDED 1	/* nftw */
 #define SKIP -2
 #define ERR -1
@@ -39,7 +40,7 @@ static int force = 0;
 static int progress = 0;
 static unsigned long long count = 0;
 
-#define MAX_EXCLUDES 100
+#define MAX_EXCLUDES 1000
 static int excludeCtr = 0;
 struct edir {
 	char *directory;
@@ -243,8 +244,8 @@ static int add_exclude(const char *directory)
 		return 1;
 	}
 	if (lstat(directory, &sb)) {
-		fprintf(stderr, "Directory \"%s\" not found, ignoring.\n",
-			directory);
+		fprintf(stderr, "Can't stat directory \"%s\", %s.\n",
+			directory, strerror(errno));
 		return 0;
 	}
 	if ((sb.st_mode & S_IFDIR) == 0) {
@@ -275,6 +276,20 @@ static int add_exclude(const char *directory)
 	return 0;
 }
 
+static void remove_exclude(const char *directory)
+{
+	int i = 0;
+	for (i = 0; i < excludeCtr; i++) {
+		if (strcmp(directory, excludeArray[i].directory) == 0) {
+			if (i != excludeCtr-1)
+				excludeArray[i] = excludeArray[excludeCtr-1];
+			excludeCtr--;
+			return;
+		}
+	}
+	return;
+}
+
 static int exclude(const char *file)
 {
 	int i = 0;
@@ -699,6 +714,62 @@ static void maybe_audit_mass_relabel(void)
 #endif
 }
 
+/*
+   Search /proc mounts for all file systems that do not support extended
+   attributes and add them to the exclude directory table.  File systems
+   support seclabel are labeled seclabel
+*/
+static void exclude_non_seclabel_mounts()
+{
+	struct utsname uts;
+	FILE *fp = fopen("/proc/mounts", "r");
+	size_t len;
+	ssize_t num;
+	int index = 0, found = 0;
+	char *mount_info[4];
+	char *buf = NULL, *item;
+	/* Check to see if the kernel supports seclabel */
+	if (uname(&uts) == 0 && strverscmp(uts.release, "2.6.30") < 0)
+		return;
+	if (!fp)
+		return;
+
+	while ((num = getline(&buf, &len, fp)) != -1) {
+		found = 0;
+		index = 0;
+		item = strtok(buf, " ");
+		while (item != NULL) {
+			mount_info[index] = item;
+			if (index == 3)
+				break;
+			index++;
+			item = strtok(NULL, " ");
+		}
+		if (index < 3) {
+			fprintf(stderr,
+				"/proc/mounts record \"%s\" has incorrect format.\n",
+				buf);
+			continue;
+		}
+
+		/* remove pre-existing entry */
+		remove_exclude(mount_info[1]);
+
+		item = strtok(mount_info[3], ",");
+		while (item != NULL) {
+			if (strcmp(item, "seclabel") == 0) {
+				found = 1;
+				break;
+			}
+			item = strtok(NULL, ",");
+		}
+
+		/* exclude mount points with out seclabel flag */
+		if (!found)
+			add_exclude(mount_info[1]);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	struct stat sb;
@@ -766,6 +837,9 @@ int main(int argc, char **argv)
 			exit(0);
 	}
 
+	/* This must happen before getops */
+	exclude_non_seclabel_mounts();
+
 	/* Process any options. */
 	while ((opt = getopt(argc, argv, "c:de:f:ilnpqrsvo:FRW0")) > 0) {
 		switch (opt) {

--
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.

#1 lore raw
Quoted 41 lines · Thomas Liu

Get setfiles to check paths for seclabel and skip them if it is not supported.

Parse /proc/mounts and add paths that do not have seclabel to the exclude list. If another path shows up that does have seclabel, remove it from the exclude list, since setfiles will try and when it fails it will skip it.

Also made one of the error messages in add_exclude more descriptive.

---

 policycoreutils/setfiles/setfiles.c |   80 ++++++++++++++++++++++++++++++++++-
 1 files changed, 77 insertions(+), 3 deletions(-)
policycoreutils/setfiles/setfiles.c +2 -1
index 1356fb9..9f27a1d 100644
@@ -11,6 +11,7 @@
 #include <ctype.h>
 #include <regex.h>
 #include <sys/vfs.h>
+#include <sys/utsname.h>
 #define __USE_XOPEN_EXTENDED 1	/* nftw */
 #define SKIP -2
 #define ERR -1
@@ -39,7 +40,7 @@ static int force = 0;
 static int progress = 0;
 static unsigned long long count = 0;
 
-#define MAX_EXCLUDES 100
+#define MAX_EXCLUDES 1000
 static int excludeCtr = 0;
 struct edir {
 	char *directory;

policycoreutils/setfiles/setfiles.c static int add_exclude(const char *directory) · line 248

Quoted 7 lines
policycoreutils/setfiles/setfiles.c +2 -2
@@ -243,8 +244,8 @@ static int add_exclude(const char *directory)
 		return 1;
 	}
 	if (lstat(directory, &sb)) {
-		fprintf(stderr, "Directory \"%s\" not found, ignoring.\n",
-			directory);
+		fprintf(stderr, "Can't stat directory \"%s\", %s.\n",
+			directory, strerror(errno));

Might want to keep the "ignoring" part as otherwise this appears to be a fatal error.

Quoted 7 lines
policycoreutils/setfiles/setfiles.c +0 -0
 		return 0;
 	}
 	if ((sb.st_mode & S_IFDIR) == 0) {
@@ -275,6 +276,20 @@ static int add_exclude(const char *directory)
 	return 0;
 }
 

policycoreutils/setfiles/setfiles.c static int add_exclude(const char *directory) · line 288

Quoted 10 lines
policycoreutils/setfiles/setfiles.c +10 -0
+static void remove_exclude(const char *directory)
+{
+	int i = 0;
+	for (i = 0; i < excludeCtr; i++) {
+		if (strcmp(directory, excludeArray[i].directory) == 0) {
+			if (i != excludeCtr-1)
+				excludeArray[i] = excludeArray[excludeCtr-1];
+			excludeCtr--;
+			return;
+		}

The function name and interface doesn't clearly indicate that this function only works if removing the last entry (and if so, why are we scanning the entire array)? But see below as well.

Quoted 18 lines
policycoreutils/setfiles/setfiles.c +11 -0
+	}
+	return;
+}
+
 static int exclude(const char *file)
 {
 	int i = 0;
@@ -699,6 +714,62 @@ static void maybe_audit_mass_relabel(void)
 #endif
 }
 
+/*
+   Search /proc mounts for all file systems that do not support extended
+   attributes and add them to the exclude directory table.  File systems
+   support seclabel are labeled seclabel
+*/
+static void exclude_non_seclabel_mounts()
+{

policycoreutils/setfiles/setfiles.c static void maybe_audit_mass_relabel(void) · line 733

Quoted 10 lines
policycoreutils/setfiles/setfiles.c +10 -0
+	struct utsname uts;
+	FILE *fp = fopen("/proc/mounts", "r");
+	size_t len;
+	ssize_t num;
+	int index = 0, found = 0;
+	char *mount_info[4];
+	char *buf = NULL, *item;
+	/* Check to see if the kernel supports seclabel */
+	if (uname(&uts) == 0 && strverscmp(uts.release, "2.6.30") < 0)
+		return;

Move the fopen down here, or fclose(fp) before returning above.

Quoted 13 lines
policycoreutils/setfiles/setfiles.c +13 -0
+	if (!fp)
+		return;
+
+	while ((num = getline(&buf, &len, fp)) != -1) {
+		found = 0;
+		index = 0;
+		item = strtok(buf, " ");
+		while (item != NULL) {
+			mount_info[index] = item;
+			if (index == 3)
+				break;
+			index++;
+			item = strtok(NULL, " ");

policycoreutils/setfiles/setfiles.c static void maybe_audit_mass_relabel(void) · line 756

Quoted 10 lines
policycoreutils/setfiles/setfiles.c +10 -0
+		}
+		if (index < 3) {
+			fprintf(stderr,
+				"/proc/mounts record \"%s\" has incorrect format.\n",
+				buf);
+			continue;
+		}
+
+		/* remove pre-existing entry */
+		remove_exclude(mount_info[1]);

Do we really need this? Are there multiple entries on the same mount point directory with different option lists? Is this to handle special cases like rootfs (which should likely just get ignored)?

Quoted 20 lines
policycoreutils/setfiles/setfiles.c +16 -0
+
+		item = strtok(mount_info[3], ",");
+		while (item != NULL) {
+			if (strcmp(item, "seclabel") == 0) {
+				found = 1;
+				break;
+			}
+			item = strtok(NULL, ",");
+		}
+
+		/* exclude mount points with out seclabel flag */
+		if (!found)
+			add_exclude(mount_info[1]);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	struct stat sb;

policycoreutils/setfiles/setfiles.c int main(int argc, char **argv) · line 841

Quoted 5 lines
policycoreutils/setfiles/setfiles.c +2 -0
@@ -766,6 +837,9 @@ int main(int argc, char **argv)
 			exit(0);
 	}
 
+	/* This must happen before getops */
+	exclude_non_seclabel_mounts();

What happens if a directory gets excluded by both this logic and via explicit option? We don't want multiple entries in the array for that.

Quoted 11 lines
policycoreutils/setfiles/setfiles.c +1 -1
+
 	/* Process any options. */
 	while ((opt = getopt(argc, argv, "c:de:f:ilnpqrsvo:FRW0")) > 0) {
 		switch (opt) {
 
 
 
--

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.

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.
author
#2 lore raw
Quoted 60 lines · Stephen Smalley
Quoted 41 lines · Thomas Liu

Get setfiles to check paths for seclabel and skip them if it is not supported.

Parse /proc/mounts and add paths that do not have seclabel to the exclude list. If another path shows up that does have seclabel, remove it from the exclude list, since setfiles will try and when it fails it will skip it.

Also made one of the error messages in add_exclude more descriptive.

---

 policycoreutils/setfiles/setfiles.c |   80 ++++++++++++++++++++++++++++++++++-
 1 files changed, 77 insertions(+), 3 deletions(-)
policycoreutils/setfiles/setfiles.c +2 -1
index 1356fb9..9f27a1d 100644
@@ -11,6 +11,7 @@
 #include <ctype.h>
 #include <regex.h>
 #include <sys/vfs.h>
+#include <sys/utsname.h>
 #define __USE_XOPEN_EXTENDED 1	/* nftw */
 #define SKIP -2
 #define ERR -1
@@ -39,7 +40,7 @@ static int force = 0;
 static int progress = 0;
 static unsigned long long count = 0;
 
-#define MAX_EXCLUDES 100
+#define MAX_EXCLUDES 1000
 static int excludeCtr = 0;
 struct edir {
 	char *directory;

policycoreutils/setfiles/setfiles.c static int add_exclude(const char *directory) · line 248

Quoted 7 lines
policycoreutils/setfiles/setfiles.c +2 -2
@@ -243,8 +244,8 @@ static int add_exclude(const char *directory)
 		return 1;
 	}
 	if (lstat(directory, &sb)) {
-		fprintf(stderr, "Directory \"%s\" not found, ignoring.\n",
-			directory);
+		fprintf(stderr, "Can't stat directory \"%s\", %s.\n",
+			directory, strerror(errno));

Might want to keep the "ignoring" part as otherwise this appears to be a fatal error.

Quoted 7 lines
policycoreutils/setfiles/setfiles.c +0 -0
 		return 0;
 	}
 	if ((sb.st_mode & S_IFDIR) == 0) {
@@ -275,6 +276,20 @@ static int add_exclude(const char *directory)
 	return 0;
 }
 
Quoted 10 lines

policycoreutils/setfiles/setfiles.c static int add_exclude(const char *directory) · line 288

Quoted 10 lines
policycoreutils/setfiles/setfiles.c +10 -0
+static void remove_exclude(const char *directory)
+{
+	int i = 0;
+	for (i = 0; i < excludeCtr; i++) {
+		if (strcmp(directory, excludeArray[i].directory) == 0) {
+			if (i != excludeCtr-1)
+				excludeArray[i] = excludeArray[excludeCtr-1];
+			excludeCtr--;
+			return;
+		}

It doesn't only work if removing the last entry. If the current entry matches the directory we passed in, it swaps it with the last one and then decrements excludeCtr, "removing" the current entry.

Quoted 50 lines

The function name and interface doesn't clearly indicate that this function only works if removing the last entry (and if so, why are we scanning the entire array)? But see below as well.

Quoted 18 lines
policycoreutils/setfiles/setfiles.c +11 -0
+	}
+	return;
+}
+
 static int exclude(const char *file)
 {
 	int i = 0;
@@ -699,6 +714,62 @@ static void maybe_audit_mass_relabel(void)
 #endif
 }
 
+/*
+   Search /proc mounts for all file systems that do not support extended
+   attributes and add them to the exclude directory table.  File systems
+   support seclabel are labeled seclabel
+*/
+static void exclude_non_seclabel_mounts()
+{

policycoreutils/setfiles/setfiles.c static void maybe_audit_mass_relabel(void) · line 733

Quoted 10 lines
policycoreutils/setfiles/setfiles.c +10 -0
+	struct utsname uts;
+	FILE *fp = fopen("/proc/mounts", "r");
+	size_t len;
+	ssize_t num;
+	int index = 0, found = 0;
+	char *mount_info[4];
+	char *buf = NULL, *item;
+	/* Check to see if the kernel supports seclabel */
+	if (uname(&uts) == 0 && strverscmp(uts.release, "2.6.30") < 0)
+		return;

Move the fopen down here, or fclose(fp) before returning above.

Quoted 15 lines
policycoreutils/setfiles/setfiles.c +15 -0
+	if (!fp)
+		return;
+
+	while ((num = getline(&buf, &len, fp)) != -1) {
+		found = 0;
+		index = 0;
+		item = strtok(buf, " ");
+		while (item != NULL) {
+			mount_info[index] = item;
+			if (index == 3)
+				break;
+			index++;
+			item = strtok(NULL, " ");
+		}
+		if (index < 3) {
Quoted 8 lines

policycoreutils/setfiles/setfiles.c static void maybe_audit_mass_relabel(void) · line 756

Quoted 8 lines
policycoreutils/setfiles/setfiles.c +8 -0
+			fprintf(stderr,
+				"/proc/mounts record \"%s\" has incorrect format.\n",
+				buf);
+			continue;
+		}
+
+		/* remove pre-existing entry */
+		remove_exclude(mount_info[1]);

Our impression is that it is needed, because of bind mounts or if two things are just mounted in the same place.

Although it seems that maybe the logic should be always add and remove if found instead of always removing and adding if not found.

Thoughts?

Quoted 27 lines

Do we really need this? Are there multiple entries on the same mount point directory with different option lists? Is this to handle special cases like rootfs (which should likely just get ignored)?

Quoted 23 lines
policycoreutils/setfiles/setfiles.c +16 -0
+
+		item = strtok(mount_info[3], ",");
+		while (item != NULL) {
+			if (strcmp(item, "seclabel") == 0) {
+				found = 1;
+				break;
+			}
+			item = strtok(NULL, ",");
+		}
+
+		/* exclude mount points with out seclabel flag */
+		if (!found)
+			add_exclude(mount_info[1]);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	struct stat sb;
@@ -766,6 +837,9 @@ int main(int argc, char **argv)
 			exit(0);
 	}
 
Quoted 2 lines

policycoreutils/setfiles/setfiles.c int main(int argc, char **argv) · line 841

Quoted 2 lines
policycoreutils/setfiles/setfiles.c +2 -0
+	/* This must happen before getops */
+	exclude_non_seclabel_mounts();

That's true. I guess I could either have add_exclude check to see if it's already in the list or just always remove first.

Quoted 14 lines

What happens if a directory gets excluded by both this logic and via explicit option? We don't want multiple entries in the array for that.

Quoted 11 lines
policycoreutils/setfiles/setfiles.c +1 -1
+
 	/* Process any options. */
 	while ((opt = getopt(argc, argv, "c:de:f:ilnpqrsvo:FRW0")) > 0) {
 		switch (opt) {
 
 
 
--

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.

--
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 133 lines · Thomas Liu
Quoted 59 lines · Stephen Smalley
Quoted 41 lines · Thomas Liu

Get setfiles to check paths for seclabel and skip them if it is not supported.

Parse /proc/mounts and add paths that do not have seclabel to the exclude list. If another path shows up that does have seclabel, remove it from the exclude list, since setfiles will try and when it fails it will skip it.

Also made one of the error messages in add_exclude more descriptive.

---

 policycoreutils/setfiles/setfiles.c |   80 ++++++++++++++++++++++++++++++++++-
 1 files changed, 77 insertions(+), 3 deletions(-)
policycoreutils/setfiles/setfiles.c +2 -1
index 1356fb9..9f27a1d 100644
@@ -11,6 +11,7 @@
 #include <ctype.h>
 #include <regex.h>
 #include <sys/vfs.h>
+#include <sys/utsname.h>
 #define __USE_XOPEN_EXTENDED 1	/* nftw */
 #define SKIP -2
 #define ERR -1
@@ -39,7 +40,7 @@ static int force = 0;
 static int progress = 0;
 static unsigned long long count = 0;
 
-#define MAX_EXCLUDES 100
+#define MAX_EXCLUDES 1000
 static int excludeCtr = 0;
 struct edir {
 	char *directory;

policycoreutils/setfiles/setfiles.c static int add_exclude(const char *directory) · line 248

Quoted 7 lines
policycoreutils/setfiles/setfiles.c +2 -2
@@ -243,8 +244,8 @@ static int add_exclude(const char *directory)
 		return 1;
 	}
 	if (lstat(directory, &sb)) {
-		fprintf(stderr, "Directory \"%s\" not found, ignoring.\n",
-			directory);
+		fprintf(stderr, "Can't stat directory \"%s\", %s.\n",
+			directory, strerror(errno));

Might want to keep the "ignoring" part as otherwise this appears to be a fatal error.

Quoted 7 lines
policycoreutils/setfiles/setfiles.c +0 -0
 		return 0;
 	}
 	if ((sb.st_mode & S_IFDIR) == 0) {
@@ -275,6 +276,20 @@ static int add_exclude(const char *directory)
 	return 0;
 }
 
Quoted 10 lines

policycoreutils/setfiles/setfiles.c static int add_exclude(const char *directory) · line 288

Quoted 10 lines
policycoreutils/setfiles/setfiles.c +10 -0
+static void remove_exclude(const char *directory)
+{
+	int i = 0;
+	for (i = 0; i < excludeCtr; i++) {
+		if (strcmp(directory, excludeArray[i].directory) == 0) {
+			if (i != excludeCtr-1)
+				excludeArray[i] = excludeArray[excludeCtr-1];
+			excludeCtr--;
+			return;
+		}

It doesn't only work if removing the last entry. If the current entry matches the directory we passed in, it swaps it with the last one and then decrements excludeCtr, "removing" the current entry.

Quoted 49 lines

The function name and interface doesn't clearly indicate that this function only works if removing the last entry (and if so, why are we scanning the entire array)? But see below as well.

Quoted 18 lines
policycoreutils/setfiles/setfiles.c +11 -0
+	}
+	return;
+}
+
 static int exclude(const char *file)
 {
 	int i = 0;
@@ -699,6 +714,62 @@ static void maybe_audit_mass_relabel(void)
 #endif
 }
 
+/*
+   Search /proc mounts for all file systems that do not support extended
+   attributes and add them to the exclude directory table.  File systems
+   support seclabel are labeled seclabel
+*/
+static void exclude_non_seclabel_mounts()
+{

policycoreutils/setfiles/setfiles.c static void maybe_audit_mass_relabel(void) · line 733

Quoted 10 lines
policycoreutils/setfiles/setfiles.c +10 -0
+	struct utsname uts;
+	FILE *fp = fopen("/proc/mounts", "r");
+	size_t len;
+	ssize_t num;
+	int index = 0, found = 0;
+	char *mount_info[4];
+	char *buf = NULL, *item;
+	/* Check to see if the kernel supports seclabel */
+	if (uname(&uts) == 0 && strverscmp(uts.release, "2.6.30") < 0)
+		return;

Move the fopen down here, or fclose(fp) before returning above.

Quoted 15 lines
policycoreutils/setfiles/setfiles.c +15 -0
+	if (!fp)
+		return;
+
+	while ((num = getline(&buf, &len, fp)) != -1) {
+		found = 0;
+		index = 0;
+		item = strtok(buf, " ");
+		while (item != NULL) {
+			mount_info[index] = item;
+			if (index == 3)
+				break;
+			index++;
+			item = strtok(NULL, " ");
+		}
+		if (index < 3) {
Quoted 8 lines

policycoreutils/setfiles/setfiles.c static void maybe_audit_mass_relabel(void) · line 756

Quoted 8 lines
policycoreutils/setfiles/setfiles.c +8 -0
+			fprintf(stderr,
+				"/proc/mounts record \"%s\" has incorrect format.\n",
+				buf);
+			continue;
+		}
+
+		/* remove pre-existing entry */
+		remove_exclude(mount_info[1]);

Our impression is that it is needed, because of bind mounts or if two things are just mounted in the same place.

Quoted 5 lines

Although it seems that maybe the logic should be always add and remove if found instead of always removing and adding if not found.

Thoughts?

If you mount /tmp with ext3 and then mount nfs over /tmp then we want to remove the /tmp directory. Currently when this happens there are two entries in the /proc/mounts directory. rootfs mounts as / as no support for seclabel and the another entry which supports seclabel, so we need to remove the first.

We are making an assumption that the last entry is always the final say. Which may or may not be the case.

Quoted 27 lines
Quoted 27 lines

Do we really need this? Are there multiple entries on the same mount point directory with different option lists? Is this to handle special cases like rootfs (which should likely just get ignored)?

Quoted 23 lines
policycoreutils/setfiles/setfiles.c +16 -0
+
+		item = strtok(mount_info[3], ",");
+		while (item != NULL) {
+			if (strcmp(item, "seclabel") == 0) {
+				found = 1;
+				break;
+			}
+			item = strtok(NULL, ",");
+		}
+
+		/* exclude mount points with out seclabel flag */
+		if (!found)
+			add_exclude(mount_info[1]);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	struct stat sb;
@@ -766,6 +837,9 @@ int main(int argc, char **argv)
 			exit(0);
 	}
 
Quoted 8 lines
Quoted 2 lines

policycoreutils/setfiles/setfiles.c int main(int argc, char **argv) · line 841

Quoted 2 lines
policycoreutils/setfiles/setfiles.c +2 -0
+	/* This must happen before getops */
+	exclude_non_seclabel_mounts();

That's true. I guess I could either have add_exclude check to see if it's already in the list or just always remove first.

Quoted 3 lines

What happens if a directory gets excluded by both this logic and via explicit option? We don't want multiple entries in the array for that.

It would end up being in the list multiple times, which would not be a problem unless you wanted to pathologically cause problems.

-e /etc -e /etc -e /etc -e /etc -e /usr

Would cause each directory looked at too search multiple entries as upposed to slowing down startup by checking for duplicates.

remove_exlcude()
add_exclude()

Would fix.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
policycoreutils/setfiles/setfiles.c +1 -0
+
 	/* Process any options. */
 	while ((opt = getopt(argc, argv, "c:de:f:ilnpqrsvo:FRW0")) > 0) {
 		switch (opt) {
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

--
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.

--
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.

author
#4 lore raw

PATCH 1/1 policycoreutils: get setfiles to skip mounts without seclabel

Get setfiles to check paths for seclabel and skip them if it is not supported.

Parse /proc/mounts and add paths that do not have seclabel to the exclude list. If another path shows up that does have seclabel, remove it from the exclude list, since setfiles will try and when it fails it will skip it.

Also made one of the error messages in add_exclude more descriptive.

---
Fixed up a few things based on suggestions.

 policycoreutils/setfiles/setfiles.c |   82 ++++++++++++++++++++++++++++++++++-
 1 files changed, 79 insertions(+), 3 deletions(-)
policycoreutils/setfiles/setfiles.c +79 -3
index 1356fb9..00dfd87 100644
@@ -11,6 +11,7 @@
 #include <ctype.h>
 #include <regex.h>
 #include <sys/vfs.h>
+#include <sys/utsname.h>
 #define __USE_XOPEN_EXTENDED 1	/* nftw */
 #define SKIP -2
 #define ERR -1
@@ -39,7 +40,7 @@ static int force = 0;
 static int progress = 0;
 static unsigned long long count = 0;
 
-#define MAX_EXCLUDES 100
+#define MAX_EXCLUDES 1000
 static int excludeCtr = 0;
 struct edir {
 	char *directory;
@@ -243,8 +244,8 @@ static int add_exclude(const char *directory)
 		return 1;
 	}
 	if (lstat(directory, &sb)) {
-		fprintf(stderr, "Directory \"%s\" not found, ignoring.\n",
-			directory);
+		fprintf(stderr, "Can't stat directory \"%s\", %s.\n",
+			directory, strerror(errno));
 		return 0;
 	}
 	if ((sb.st_mode & S_IFDIR) == 0) {
@@ -275,6 +276,20 @@ static int add_exclude(const char *directory)
 	return 0;
 }
 
+static void remove_exclude(const char *directory)
+{
+	int i = 0;
+	for (i = 0; i < excludeCtr; i++) {
+		if (strcmp(directory, excludeArray[i].directory) == 0) {
+			if (i != excludeCtr-1)
+				excludeArray[i] = excludeArray[excludeCtr-1];
+			excludeCtr--;
+			return;
+		}
+	}
+	return;
+}
+
 static int exclude(const char *file)
 {
 	int i = 0;
@@ -699,6 +714,63 @@ static void maybe_audit_mass_relabel(void)
 #endif
 }
 
+/*
+   Search /proc mounts for all file systems that do not support extended
+   attributes and add them to the exclude directory table.  File systems
+   support seclabel are labeled seclabel
+*/
+static void exclude_non_seclabel_mounts()
+{
+	struct utsname uts;
+	FILE *fp;
+	size_t len;
+	ssize_t num;
+	int index = 0, found = 0;
+	char *mount_info[4];
+	char *buf = NULL, *item;
+	/* Check to see if the kernel supports seclabel */
+	if (uname(&uts) == 0 && strverscmp(uts.release, "2.6.30") < 0)
+		return;
+	fp = fopen("/proc/mounts", "r");
+	if (!fp)
+		return;
+
+	while ((num = getline(&buf, &len, fp)) != -1) {
+		found = 0;
+		index = 0;
+		item = strtok(buf, " ");
+		while (item != NULL) {
+			mount_info[index] = item;
+			if (index == 3)
+				break;
+			index++;
+			item = strtok(NULL, " ");
+		}
+		if (index < 3) {
+			fprintf(stderr,
+				"/proc/mounts record \"%s\" has incorrect format.\n",
+				buf);
+			continue;
+		}
+
+		/* remove pre-existing entry */
+		remove_exclude(mount_info[1]);
+
+		item = strtok(mount_info[3], ",");
+		while (item != NULL) {
+			if (strcmp(item, "seclabel") == 0) {
+				found = 1;
+				break;
+			}
+			item = strtok(NULL, ",");
+		}
+
+		/* exclude mount points with out seclabel flag */
+		if (!found)
+			add_exclude(mount_info[1]);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	struct stat sb;
@@ -766,6 +838,9 @@ int main(int argc, char **argv)
 			exit(0);
 	}
 
+	/* This must happen before getops */
+	exclude_non_seclabel_mounts();
+
 	/* Process any options. */
 	while ((opt = getopt(argc, argv, "c:de:f:ilnpqrsvo:FRW0")) > 0) {
 		switch (opt) {
@@ -802,6 +877,7 @@ int main(int argc, char **argv)
 				break;
 			}
 		case 'e':
+			remove_exclude(optarg);
 			if (add_exclude(optarg))
 				exit(1);
 			break;

--
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 8 lines · Thomas Liu

Get setfiles to check paths for seclabel and skip them if it is not supported.

Parse /proc/mounts and add paths that do not have seclabel to the exclude list. If another path shows up that does have seclabel, remove it from the exclude list, since setfiles will try and when it fails it will skip it.

Quoted 6 lines

Also made one of the error messages in add_exclude more descriptive.

---

Thanks, merged in policycoreutils 2.0.68.

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 9 lines · Stephen Smalley
Quoted 8 lines · Thomas Liu

Get setfiles to check paths for seclabel and skip them if it is not supported.

Parse /proc/mounts and add paths that do not have seclabel to the exclude list. If another path shows up that does have seclabel, remove it from the exclude list, since setfiles will try and when it fails it will skip it.

Quoted 8 lines
Quoted 6 lines

Also made one of the error messages in add_exclude more descriptive.

---

Thanks, merged in policycoreutils 2.0.68.

Applied this patch on top to free the buffer allocated by getline() and to free any removed entries from the excludeArray. valgrind --leak-check=full then shows no leakage.

policycoreutils/setfiles/setfiles.c +5 -0
index 1c780a4..5e5d957 100644
@@ -281,6 +281,7 @@ static void remove_exclude(const char *directory)
 	int i = 0;
 	for (i = 0; i < excludeCtr; i++) {
 		if (strcmp(directory, excludeArray[i].directory) == 0) {
+			free(excludeArray[i].directory);
 			if (i != excludeCtr-1)
 				excludeArray[i] = excludeArray[excludeCtr-1];
 			excludeCtr--;
@@ -728,9 +729,11 @@ static void exclude_non_seclabel_mounts()
 	int index = 0, found = 0;
 	char *mount_info[4];
 	char *buf = NULL, *item;
+
 	/* Check to see if the kernel supports seclabel */
 	if (uname(&uts) == 0 && strverscmp(uts.release, "2.6.30") < 0)
 		return;
+
 	fp = fopen("/proc/mounts", "r");
 	if (!fp)
 		return;
@@ -769,6 +772,8 @@ static void exclude_non_seclabel_mounts()
 		if (!found)
 			add_exclude(mount_info[1]);
 	}
+
+	free(buf);
 }
 
 int main(int argc, char **argv)
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.