Skip to contentSkip to conversation

libselinux: compare absolute pathname in matchpathcon -V

PATCH Applied Petr Lautrbach 2 participants 2 messages Jun 20, 2016 – Jun 23, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: compare absolute pathname in matchpathcon -V

filepath needs to be resolved first in order to be correctly found by selabel_lookup_raw()

Fixes:

$ matchpathcon -V passwd
passwd has context system_u:object_r:passwd_file_t:s0, should be
system_u:object_r:passwd_file_t:s0
$ echo $?
1
---
 libselinux/src/matchpathcon.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
libselinux/src/matchpathcon.c +11 -0
index 3868711..a2f2c3e 100644
@@ -471,6 +471,17 @@ int selinux_file_context_verify(const char *path, mode_t mode)
 	char * con = NULL;
 	char * fcontext = NULL;
 	int rc = 0;
+	char stackpath[PATH_MAX + 1];
+	char *p = NULL;
+
+	if (S_ISLNK(mode)) {
+		if (!realpath_not_final(path, stackpath))
+			path = stackpath;
+	} else {
+		p = realpath(path, stackpath);
+		if (p)
+			path = p;
+	}
 
 	rc = lgetfilecon_raw(path, &con);
 	if (rc == -1) {
Signature · 1 line
2.7.4
#1 lore raw
Quoted 4 lines · Petr Lautrbach

filepath needs to be resolved first in order to be correctly found by selabel_lookup_raw()

Fixes:

Quoted 8 lines
$ matchpathcon -V passwd
passwd has context system_u:object_r:passwd_file_t:s0, should be
system_u:object_r:passwd_file_t:s0
$ echo $?
1

Thanks, applied.

Quoted 27 lines
---
 libselinux/src/matchpathcon.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
libselinux/src/matchpathcon.c +11 -0
index 3868711..a2f2c3e 100644
@@ -471,6 +471,17 @@ int selinux_file_context_verify(const char *path, mode_t mode)
 	char * con = NULL;
 	char * fcontext = NULL;
 	int rc = 0;
+	char stackpath[PATH_MAX + 1];
+	char *p = NULL;
+
+	if (S_ISLNK(mode)) {
+		if (!realpath_not_final(path, stackpath))
+			path = stackpath;
+	} else {
+		p = realpath(path, stackpath);
+		if (p)
+			path = p;
+	}
 
 	rc = lgetfilecon_raw(path, &con);
 	if (rc == -1) {