Bug in restorecon for symlinks in root dir
Hi,
restorecon has a bug for symlinks in the root directory, such as /vmlinuz
or the /lib64 -> /lib symlink found on all debian amd64 systems.
The bug arises in the way the containing directories "realname" is found for symlinks. Basically, the directory name is split by overwriting the last / character with a \0 char; then calling realname(3) on the first component, then joining them again.
For obvious reasons, this doesn't work for filenames such as /lib64; but it only affects symlinks (thats why "restorecon /etc" works fine). The easiest workaround is to use "restorecon //lib64"
Attached is a patch to fix this bug. It's untested yet, but I guess you'll review it anyway. And it's a very easy thing, it's got to work. ;-)
best regards,
Erich Schubert
Signature · 5 lines
erich@(vitavonni.de|debian.org) -- GPG Key ID: 4B3A135C (o_
This one's tricky. You have to use imaginary numbers, like //\
eleventeen... --- Hobbes V_/_
Die Stärke eines Menschen kann man daran messen,
wie er mit seinen Schwächen fertig wird!restorecon/restorecon.c +4 -1
@@ -178,7 +178,10 @@return 1 ; } file_sep =strrchr (tmp_path,'/' ); -if (file_sep) { +if (file_sep == tmp_path) { + file_sep++; + p =strcpy (path,"/" ); + }else if (file_sep) { *file_sep =0 ; file_sep++; p =realpath (tmp_path, path);
Quoted 13 lines · Erich Schubert
Hi,
restorecon has a bug for symlinks in the root directory, such as /vmlinuz
or the /lib64 -> /lib symlink found on all debian amd64 systems.The bug arises in the way the containing directories "realname" is found for symlinks. Basically, the directory name is split by overwriting the last / character with a \0 char; then calling realname(3) on the first component, then joining them again.
For obvious reasons, this doesn't work for filenames such as /lib64; but it only affects symlinks (thats why "restorecon /etc" works fine). The easiest workaround is to use "restorecon //lib64"
Quoted 3 lines
Attached is a patch to fix this bug. It's untested yet, but I guess you'll review it anyway. And it's a very easy thing, it's got to work. ;-)
I don't think that this is quite what we want either. As patched, we do get the same behavior as we would with an unpatched workaround of using //lib64, which also isn't what we want:
# restorecon -nv /lib64 restorecon reset //lib64 context user_u:object_r:root_t:s0->system_u:object_r:default_t:s0 versus the expected result of: restorecon reset /lib64 context user_u:object_r:root_t:s0->system_u:object_r:lib_t:s0
The following seems to work, although it is a bit strange. Possibly the entire approach there needs to be reworked.
Index: policycoreutils/restorecon/restorecon.c
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/policycoreutils/restorecon/restorecon.c,v retrieving revision 1.40
restorecon/restorecon.c +4 -1
@@ -178,7 +178,10 @@ int restore(const char *filename)return 1 ; } file_sep =strrchr (tmp_path,'/' ); -if (file_sep) { +if (file_sep == tmp_path) { + file_sep++; + p =strcpy (path,"" ); + }else if (file_sep) { *file_sep =0 ; file_sep++; p =realpath (tmp_path, path);
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.
Hi Stephen,
Quoted 3 lines
restorecon reset //lib64 context user_u:object_r:root_t:s0->system_u:object_r:default_t:s0 versus the expected result of:
restorecon reset /lib64 context user_u:object_r:root_t:s0->system_u:object_r:lib_t:s0
Ah, I was already wondering why this was happening already...
restorecon/restorecon.c int restore(const char *filename) · line 183
Quoted 3 lines
The following seems to work, although it is a bit strange. Possibly the entire approach there needs to be reworked.
restorecon/restorecon.c +1
-0+ p =strcpy (path,"" );
Yep, that should work better. The problem is that realpath("") doesn't work for obvious reasons. I wonder what realpath does to a trailing slash.
Do we need some handling for double slashes? I.e. will restorecon /etc//shadow work as expected? Does it work correctly for links like //lib64?
I assume it will, but I didn't verify it.
best regards,
Erich Schubert
Signature · 10 lines
erich@(vitavonni.de|debian.org) -- GPG Key ID: 4B3A135C (o_
It's not denial. I'm just selective about the reality I accept. //\
Signatur befindet sich auf der Rückseite dieser E-Mail. V_/_
--
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.Quoted 15 lines · Erich Schubert
Hi Stephen,
Quoted 3 lines
restorecon reset //lib64 context user_u:object_r:root_t:s0->system_u:object_r:default_t:s0 versus the expected result of:
restorecon reset /lib64 context user_u:object_r:root_t:s0->system_u:object_r:lib_t:s0Ah, I was already wondering why this was happening already...
restorecon/restorecon.c int restore(const char *filename) · line 183
Quoted 3 lines
The following seems to work, although it is a bit strange. Possibly the entire approach there needs to be reworked.
restorecon/restorecon.c +1
-0+ p =strcpy (path,"" );Yep, that should work better. The problem is that
realpath("")doesn't work for obvious reasons. I wonder what realpath does to a trailing slash.
Quoted 4 lines
Do we need some handling for double slashes? I.e. will restorecon
/etc//shadowwork as expected? Does it work correctly for links like //lib64?
I assume it will, but I didn't verify it.
restorecon /etc//shadow or //lib64/libc.so.6 appear to work correctly, but restorecon //lib64 would end up with default_t even with this patch.
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.
Hi Stephen,
Quoted 2 lines
restorecon
/etc//shadowor //lib64/libc.so.6 appear to work correctly, but restorecon //lib64 would end up withdefault_teven with this patch.
Ah, yes. That is another corner case. Has anyone tried what realname() does if the filename always contains that trailing slash? For the root directory it won't strip it, but what for e.g. /dev/?
I.e. if we wouldn't transform
/some/path/filename to /some/path\0filename
then call realname on the first part, but actually copy the path including the last / to a temporary variable, then calling realpath() on that?
Or maybe just use this fragment:
restorecon.c 2006-08-24 21:01:44.000000000 +0200 renamed from restorecon.c 2006-08-24 20:57:56.000000000 +0200 +5 -2
@@ -197,8 +197,11 @@ return 1; } p += len; - *p = '/'; - p++; + /* ensure trailing slash of directory name */ + if (len == 0 || *(p-1) != '/') { + *p = '/'; + p++; + } strcpy(p, file_sep); filename = path; } else {
The len==0 part should never be needed.
best regards,
Erich Schubert
Signature · 10 lines
erich@(vitavonni.de|debian.org) -- GPG Key ID: 4B3A135C (o_ The early bird gets the worm, but the second mouse gets the cheese. //\ Wie kann ich wissen, was ich denke, bevor ich höre, was ich sage. V_/_ -- 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.
Hello Stephen,
I've seen you've commited the fix for restorecon on the /lib64 symlink.
How about this fix for correct handling of //lib64 etc.?
In contrast to my previous mail, the "len == 0" part in the if statement should be necessary (for the /lib64 case; since path="" in that situation)
The "len == 0" test is only unneeded if we used my initial patch (with strcpy("/", path)); since the len == 0 case should then never occur.
The patch is straightforward - it only adds / to the path if it doesn't end in a slash yet. Since this will result in path names at most 1 char shorter than before, the buffer size check doesn't need to be modified (though it wastes one character then)
Quoted 6 lines
restorecon.c 2006-08-24 21:01:44.000000000 +0200 renamed from restorecon.c 2006-08-24 20:57:56.000000000 +0200 +0
-0@@ -197,8 +197,11 @@ return 1; } p += len;
restorecon.c 2006-08-24 21:01:44.000000000 +0200 line 207
Quoted 10 lines
restorecon.c 2006-08-24 21:01:44.000000000 +0200 renamed from restorecon.c 2006-08-24 20:57:56.000000000 +0200 +5
-2- *p = '/'; - p++; + /* ensure trailing slash of directory name */ + if (len == 0 || *(p-1) != '/') { + *p = '/'; + p++; + } strcpy(p, file_sep); filename = path; } else {
best regards,
Erich Schubert
Signature · 10 lines
erich@(vitavonni.de|debian.org) -- GPG Key ID: 4B3A135C (o_
The problem with the future is that it keeps turning into the present. //\
Signatur befindet sich auf der Rückseite dieser E-Mail. V_/_
--
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.Stephen Smalley
Acked-byQuoted 15 lines · Erich Schubert
Hello Stephen,
I've seen you've commited the fix for restorecon on the /lib64 symlink.How about this fix for correct handling of //lib64 etc.?
In contrast to my previous mail, the "len == 0" part in the if statement should be necessary (for the /lib64 case; since path="" in that situation)
The "len == 0" test is only unneeded if we used my initial patch (withstrcpy("/", path)); since the len == 0 case should then never occur.The patch is straightforward - it only adds / to the path if it doesn't end in a slash yet. Since this will result in path names at most 1 char shorter than before, the buffer size check doesn't need to be modified (though it wastes one character then)
Quoted 16 lines
Quoted 6 lines
restorecon.c 2006-08-24 21:01:44.000000000 +0200 renamed from restorecon.c 2006-08-24 20:57:56.000000000 +0200 +0
-0@@ -197,8 +197,11 @@ return 1; } p += len;restorecon.c 2006-08-24 21:01:44.000000000 +0200 line 207
Quoted 10 lines
restorecon.c 2006-08-24 21:01:44.000000000 +0200 renamed from restorecon.c 2006-08-24 20:57:56.000000000 +0200 +5
-2- *p = '/'; - p++; + /* ensure trailing slash of directory name */ + if (len == 0 || *(p-1) != '/') { + *p = '/'; + p++; + } strcpy(p, file_sep); filename = path; } else {
Looks fine to me.
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.
Joshua Brindle
Acked-byQuoted 35 lines · Stephen Smalley
Quoted 15 lines · Erich Schubert
Hello Stephen,
I've seen you've commited the fix for restorecon on the /lib64 symlink.How about this fix for correct handling of //lib64 etc.?
In contrast to my previous mail, the "len == 0" part in the if statement should be necessary (for the /lib64 case; since path="" in that situation)
The "len == 0" test is only unneeded if we used my initial patch (withstrcpy("/", path)); since the len == 0 case should then never occur.The patch is straightforward - it only adds / to the path if it doesn't end in a slash yet. Since this will result in path names at most 1 char shorter than before, the buffer size check doesn't need to be modified (though it wastes one character then)
Quoted 16 lines
Quoted 6 lines
restorecon.c 2006-08-24 21:01:44.000000000 +0200 renamed from restorecon.c 2006-08-24 20:57:56.000000000 +0200 +0
-0@@ -197,8 +197,11 @@ return 1; } p += len;restorecon.c 2006-08-24 21:01:44.000000000 +0200 line 207
Quoted 10 lines
restorecon.c 2006-08-24 21:01:44.000000000 +0200 renamed from restorecon.c 2006-08-24 20:57:56.000000000 +0200 +5
-2- *p = '/'; - p++; + /* ensure trailing slash of directory name */ + if (len == 0 || *(p-1) != '/') { + *p = '/'; + p++; + } strcpy(p, file_sep); filename = path; } else {Looks fine to me.
Quoted 1 line
Acked-by Stephen Smalley <[email protected]>
--
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.