Skip to contentSkip to conversation

restorecon

PATCH Russell Coker 2 participants 3 messages Aug 20, 2004 SELinux security module

lore
Share & tools
lore
#0 lore raw

In 1.16 there is a new check in restorecon:
+ if (S_ISLNK(st.st_mode)) {
+ fprintf(stderr,"Warning! %s refers to a symbolic link, not expanding
path.\
n", filename);
+ } else {

+    char *p;
+    p = realpath(filename, path);
+    if (!p) {

+ fprintf(stderr,"realpath(%s) failed %s\n", filename, strerror(errno));
+ return 1;
+ }
+ filename = p;
+ }

Is this really what we want?

In the case of the target not being a sym-link realpath() will expand sym-links in the middle of the path. But if the target is a sym-link then restorecon will not expand the sym-links in the path and may do things that are not expected or desired (/usr/lib/X11 is one example).

I've attached a patch for policycoreutils to fix this. In the case of the target being a sym-link it calls realpath() on the parent directory.

To test this create a sym-link /etc/foo pointing to /lib and then try relabelling /etc/foo/X where X is a sym-link.

Of course this shouldn't be an issue if the machine is in enforcing mode as restorecon_t can only get read access to lnk_file class through the following rules:

(163702) allow  restorecon_t  lib_t : lnk_file  { read getattr lock ioctl }; 
(163708) allow  restorecon_t  ld_so_t : lnk_file  { read getattr lock ioctl }; 
(163712) allow  restorecon_t  shlib_t : lnk_file  { read getattr lock ioctl }; 
(163734) allow  restorecon_t  proc_t : lnk_file  read ; 
(163756) allow  restorecon_t  policy_config_t : lnk_file  { getattr read }; 
(163786) allow  restorecon_t  selinux_config_t : lnk_file  { getattr read }; 
(163795) allow  restorecon_t  file_context_t : lnk_file  { getattr read }; 
Signature · 4 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
restorecon/restorecon.c +17 -0
diff -ru policycoreutils-1.16.orig/restorecon/restorecon.c policycoreutils-1.16/restorecon/restorecon.c
@@ -63,6 +63,23 @@
   }
   if (S_ISLNK(st.st_mode)) {
     fprintf(stderr,"Warning! %s refers to a symbolic link, not expanding path.\n", filename);
+    char *p = NULL, *file_sep;
+    char *tmp_path = strdup(filename);
+    file_sep = strrchr(tmp_path, '/');
+    if(file_sep)
+    {
+      *file_sep = 0;
+      file_sep++;
+      p = realpath(tmp_path, path);
+    }
+    if (!p || strlen(path) + strlen(file_sep) + 1 > PATH_MAX) {
+      fprintf(stderr,"realpath(%s) failed %s\n", filename, strerror(errno));
+      free(tmp_path);
+      return 1;
+    }
+    sprintf(p + strlen(p), "/%s", file_sep);
+    filename = p;
+    free(tmp_path);
   } else {
     char *p;
     p = realpath(filename, path);
#1 lore raw
Quoted 1 line · Russell Coker

In 1.16 there is a new check in restorecon:

<snip>

Quoted 1 line

Is this really what we want?

We definitely want to apply realpath() in the case where the final component is not a symlink, so that commands like:
cd /etc; restorecon passwd shadow group (i.e. relative paths) -or-
restorecon /usr/lib/X11/config (i.e. intermediate symlinks)
will work and apply the same security context that would be applied by setfiles.

In the case where the final component is a symlink, restorecon can't tell whether the user meant to restore the context on the symlink itself or on the referenced file. At present, it just falls back to the old behavior (apply to the symlink) with a warning.

Quoted 2 lines

I've attached a patch for policycoreutils to fix this. In the case of the target being a sym-link it calls realpath() on the parent directory.

Reasonable. But still doesn't address the issue of whether the user meant to relabel the symlink itself or the referenced file.

Quoted 6 lines

To test this create a sym-link /etc/foo pointing to /lib and then try relabelling /etc/foo/X where X is a sym-link.

Of course this shouldn't be an issue if the machine is in enforcing mode as restorecon_t can only get read access to lnk_file class through the following rules:

Yes, restorecon and setfiles are very limited in the set of symlink types that they can read.

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.
#2 lore raw
Quoted 2 lines · Stephen Smalley

Reasonable. But still doesn't address the issue of whether the user meant to relabel the symlink itself or the referenced file.

I think we have to assume that the user wants to relabel the sym-link not follow it.

In enforcing mode with the default policy there will be no option to follow the link. It's desirable to get the equivalent behaviour in enforcing and permissive modes, so that means not following the link.

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.