Skip to contentSkip to conversation

Bug in restorecon

PATCH In review Yuichi Nakamura 2 participants 4 messages Jun 4, 2007 – Jun 5, 2007 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bug in restorecon

Hi.

I found a bug in restorecon in trunk. restorecon to relative path does not work. (such as restorecon -R .).
Path is expanded by realpath in restorecon, isn't it ?

Following is a fix.

setfiles/setfiles.c (working copy) renamed from setfiles/setfiles.c (revision 2457) +5 -5
Index: policycoreutils/setfiles/setfiles.c
===================================================================
@@ -223,19 +223,19 @@
        /* fullname will be the real file that gets labeled
         * name will be what is matched in the policy */
        if (NULL != rootpath) {
-               if (0 != strncmp(rootpath, name, rootpathlen)) {
+               if (0 != strncmp(rootpath, fullname, rootpathlen)) {
                        fprintf(stderr, "%s:  %s is not located in %s\n",
-                               progname, name, rootpath);
+                               progname, fullname, rootpath);
                        return -1;
                }
-               name += rootpathlen;
+               fullname += rootpathlen;
        }
 
-       if (rootpath != NULL && name[0] == '\0')
+       if (rootpath != NULL && fullname[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);
+               return matchpathcon_index(fullname, sb->st_mode, con);
 }
 
 void usage(const char *const name)

Regards,
Yuichi Nakamura

--
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 28 lines · Yuichi Nakamura

Hi.

I found a bug in restorecon in trunk. restorecon to relative path does not work. (such as restorecon -R .).
Path is expanded by realpath in restorecon, isn't it ?

Following is a fix.

Index: policycoreutils/setfiles/setfiles.c
===================================================================

setfiles/setfiles.c (working copy) renamed from setfiles/setfiles.c (revision 2457) +3 -3
@@ -223,19 +223,19 @@
        /* fullname will be the real file that gets labeled
         * name will be what is matched in the policy */
        if (NULL != rootpath) {
-               if (0 != strncmp(rootpath, name, rootpathlen)) {
+               if (0 != strncmp(rootpath, fullname, rootpathlen)) {
                        fprintf(stderr, "%s:  %s is not located in %s\n",
-                               progname, name, rootpath);
+                               progname, fullname, rootpath);
                        return -1;
                }
-               name += rootpathlen;
+               fullname += rootpathlen;
        }
 

setfiles/setfiles.c (working copy) line 241

Quoted 10 lines
setfiles/setfiles.c (working copy) renamed from setfiles/setfiles.c (revision 2457) +2 -2
-       if (rootpath != NULL && name[0] == '\0')
+       if (rootpath != NULL && fullname[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);
+               return matchpathcon_index(fullname, sb->st_mode, con);
 }
 
 void usage(const char *const name)

In that case, why have 'fullname' vs. 'name' at all? Just directly manipulate name and use it throughout.

Make sure that setfiles -r /path/to/altroot file_contexts /path/to/altroot still works as expected, e.g. labels /path/to/altroot/etc/shadow with the label of /etc/shadow in policy.

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.
#2 lore raw
Quoted 3 lines · On Mon, 04 Jun 2007 09:28:44 -0400 Stephen Smalley wrote:

On Mon, 2007-06-04 at 03:18 +0900, Yuichi Nakamura wrote:
In that case, why have 'fullname' vs. 'name' at all? Just directly manipulate name and use it throughout.

fullname is unnecessary, so removed.

Quoted 4 lines

Make sure that setfiles -r /path/to/altroot file_contexts /path/to/altroot still works as expected, e.g. labels /path/to/altroot/etc/shadow with the label of /etc/shadow in policy.

I tested,
"touch /altroot/etc/shadow, setfiles -r /altroot"
then /altroot/etc/shadow was labeled as same label for /etc/shadow.

Please see below patch.

setfiles/setfiles.c +15 -17
Index: policycoreutils/setfiles/setfiles.c
===================================================================
@@ -146,20 +146,19 @@
 int match(const char *name, struct stat *sb, char **con)
 {
 	int ret;
-	const char *fullname = name;
 	char path[PATH_MAX + 1];
 
 	if (excludeCtr > 0) {
-		if (exclude(fullname)) {
+		if (exclude(name)) {
 			return -1;
 		}
 	}
-	ret = lstat(fullname, sb);
+	ret = lstat(name, sb);
 	if (ret) {
 		if (ignore_enoent && errno == ENOENT)
 			return 0;
 		fprintf(stderr, "%s:  unable to stat file %s: %s\n", progname,
-			fullname, strerror(errno));
+			name, strerror(errno));
 		return -1;
 	}
 
@@ -168,12 +167,12 @@
 			if (verbose > 1)
 				fprintf(stderr,
 					"Warning! %s refers to a symbolic link, not following last component.\n",
-					fullname);
+					name);
 			char *p = NULL, *file_sep;
-			char *tmp_path = strdupa(fullname);
+			char *tmp_path = strdupa(name);
 			size_t len = 0;
 			if (!tmp_path) {
-				fprintf(stderr, "strdupa on %s failed:  %s\n", fullname,
+				fprintf(stderr, "strdupa on %s failed:  %s\n", name,
 					strerror(errno));
 				return -1;
 			}
@@ -192,7 +191,7 @@
 			if (p)
 				len = strlen(p);
 			if (!p || len + strlen(file_sep) + 2 > PATH_MAX) {
-				fprintf(stderr, "realpath(%s) failed %s\n", fullname,
+				fprintf(stderr, "realpath(%s) failed %s\n", name,
 					strerror(errno));
 				return -1;
 			}
@@ -203,25 +202,24 @@
 				p++;
 			}
 			strcpy(p, file_sep);
-			fullname = path;
-			if (excludeCtr > 0 && exclude(fullname))
+			name = path;
+			if (excludeCtr > 0 && exclude(name))
 				return -1;
 		} else {
 			char *p;
-			p = realpath(fullname, path);
+			p = realpath(name, path);
 			if (!p) {
-				fprintf(stderr, "realpath(%s) failed %s\n", fullname,
+				fprintf(stderr, "realpath(%s) failed %s\n", name,
 					strerror(errno));
 				return -1;
 			}
-			fullname = p;
-			if (excludeCtr > 0 && exclude(fullname))
+			name = p;
+			if (excludeCtr > 0 && exclude(name))
 				return -1;
 		}
 	}
-
-	/* fullname will be the real file that gets labeled
-	 * name will be what is matched in the policy */
+	
+	/* name will be what is matched in the policy */
 
 

Regards,
Yuichi Nakamura

--
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 11 lines · Yuichi Nakamura
Quoted 3 lines · On Mon, 04 Jun 2007 09:28:44 -0400 Stephen Smalley wrote:

On Mon, 2007-06-04 at 03:18 +0900, Yuichi Nakamura wrote:
In that case, why have 'fullname' vs. 'name' at all? Just directly manipulate name and use it throughout.

fullname is unnecessary, so removed.

Quoted 4 lines

Make sure that setfiles -r /path/to/altroot file_contexts /path/to/altroot still works as expected, e.g. labels /path/to/altroot/etc/shadow with the label of /etc/shadow in policy.

Quoted 5 lines

I tested,
"touch /altroot/etc/shadow, setfiles -r /altroot"
then /altroot/etc/shadow was labeled as same label for /etc/shadow.

Please see below patch.

Thanks, merged onto trunk.

Quoted 89 lines

Index: policycoreutils/setfiles/setfiles.c
===================================================================

setfiles/setfiles.c +15 -17
@@ -146,20 +146,19 @@
 int match(const char *name, struct stat *sb, char **con)
 {
 	int ret;
-	const char *fullname = name;
 	char path[PATH_MAX + 1];
 
 	if (excludeCtr > 0) {
-		if (exclude(fullname)) {
+		if (exclude(name)) {
 			return -1;
 		}
 	}
-	ret = lstat(fullname, sb);
+	ret = lstat(name, sb);
 	if (ret) {
 		if (ignore_enoent && errno == ENOENT)
 			return 0;
 		fprintf(stderr, "%s:  unable to stat file %s: %s\n", progname,
-			fullname, strerror(errno));
+			name, strerror(errno));
 		return -1;
 	}
 
@@ -168,12 +167,12 @@
 			if (verbose > 1)
 				fprintf(stderr,
 					"Warning! %s refers to a symbolic link, not following last component.\n",
-					fullname);
+					name);
 			char *p = NULL, *file_sep;
-			char *tmp_path = strdupa(fullname);
+			char *tmp_path = strdupa(name);
 			size_t len = 0;
 			if (!tmp_path) {
-				fprintf(stderr, "strdupa on %s failed:  %s\n", fullname,
+				fprintf(stderr, "strdupa on %s failed:  %s\n", name,
 					strerror(errno));
 				return -1;
 			}
@@ -192,7 +191,7 @@
 			if (p)
 				len = strlen(p);
 			if (!p || len + strlen(file_sep) + 2 > PATH_MAX) {
-				fprintf(stderr, "realpath(%s) failed %s\n", fullname,
+				fprintf(stderr, "realpath(%s) failed %s\n", name,
 					strerror(errno));
 				return -1;
 			}
@@ -203,25 +202,24 @@
 				p++;
 			}
 			strcpy(p, file_sep);
-			fullname = path;
-			if (excludeCtr > 0 && exclude(fullname))
+			name = path;
+			if (excludeCtr > 0 && exclude(name))
 				return -1;
 		} else {
 			char *p;
-			p = realpath(fullname, path);
+			p = realpath(name, path);
 			if (!p) {
-				fprintf(stderr, "realpath(%s) failed %s\n", fullname,
+				fprintf(stderr, "realpath(%s) failed %s\n", name,
 					strerror(errno));
 				return -1;
 			}
-			fullname = p;
-			if (excludeCtr > 0 && exclude(fullname))
+			name = p;
+			if (excludeCtr > 0 && exclude(name))
 				return -1;
 		}
 	}
-
-	/* fullname will be the real file that gets labeled
-	 * name will be what is matched in the policy */
+	
+	/* name will be what is matched in the policy */
 
 
 Regards,

Yuichi Nakamura

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.