Skip to contentSkip to conversation

sestatus: resolve symlinks in path when looking for a process

PATCH In review Nicolas Iooss 2 participants 4 messages Apr 22, 2018 – Apr 25, 2018 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 sestatus: resolve symlinks in path when looking for a process

"sestatus -v" uses /proc/$PID/exe symbolic link in order to find the context of processes present in /etc/sestatus.conf. For example, this file includes "/usr/sbin/sshd".

On Arch Linux, /bin, /sbin and /usr/sbin are symbolic links to /usr/bin, so sshd process is seen as "/usr/bin/sshd" instead of "/usr/sbin/sshd". This causes "sestatus -v" to show nothing in "Process contexts:" for sshd, agetty, etc.

Use realpath() to resolve any symlink components in program paths defined in /etc/sestatus.conf. This makes "sestatus -v" show the expected result:

Process contexts:
Current context: sysadm_u:sysadm_r:sysadm_t
Init context: system_u:system_r:init_t
/sbin/agetty system_u:system_r:getty_t
/usr/sbin/sshd system_u:system_r:sshd_t

---
 policycoreutils/sestatus/sestatus.c | 6 ++++++
 1 file changed, 6 insertions(+)
policycoreutils/sestatus/sestatus.c +6 -0
index b05e794cabe7..d7f198c2fa0d 100644
@@ -61,6 +61,7 @@ int cmp_cmdline(const char *command, int pid)
 int pidof(const char *command)
 {
 /* inspired by killall5.c from psmisc */
+	char stackpath[PATH_MAX + 1], *p;
 	DIR *dir;
 	struct dirent *de;
 	int pid, ret = -1, self = getpid();
@@ -70,6 +71,11 @@ int pidof(const char *command)
 		return -1;
 	}
 
+	/* Resolve the path if it contains symbolic links */
+	p = realpath(command, stackpath);
+	if (p)
+		command = p;
+
 	while ((de = readdir(dir)) != NULL) {
 		errno = 0;
 		pid = (int)strtol(de->d_name, (char **)NULL, 10);
Signature · 1 line
2.17.0
#1 lore raw

PATCH 2/2 sestatus: free process and file contexts which are checked

clang's static analyzer reports a potential memory leak because the buffers allocated in pc and fc are not freed in main(), in sestatus.c. Free these buffers properly.

---
 policycoreutils/sestatus/sestatus.c | 2 ++
 1 file changed, 2 insertions(+)
policycoreutils/sestatus/sestatus.c +2 -0
index d7f198c2fa0d..9a92e72ff4fd 100644
@@ -436,6 +436,7 @@ int main(int argc, char **argv)
 			printf("%s\n", context);
 			freecon(context);
 		}
+		free(pc[i]);
 	}
 
 	printf("\nFile contexts:\n");
@@ -478,6 +479,7 @@ int main(int argc, char **argv)
 				freecon(context);
 			}
 		}
+		free(fc[i]);
 	}
 
 	return 0;
Signature · 1 line
2.17.0
#2 lore raw

Re: [PATCH 2/2] sestatus: free process and file contexts which are checked

Quoted 30 lines · Nicolas Iooss

clang's static analyzer reports a potential memory leak because the buffers allocated in pc and fc are not freed in main(), in sestatus.c. Free these buffers properly.

---
 policycoreutils/sestatus/sestatus.c | 2 ++
 1 file changed, 2 insertions(+)
policycoreutils/sestatus/sestatus.c +2 -1
index d7f198c2fa0d..9a92e72ff4fd 100644
@@ -436,6 +436,7 @@ int main(int argc, char **argv)
                        printf("%s\n", context);
                        freecon(context);
                }
+               free(pc[i]);
        }
 
        printf("\nFile contexts:\n");
@@ -478,6 +479,7 @@ int main(int argc, char **argv)
                                freecon(context);
                        }
                }
+               free(fc[i]);
        }
 
        return 0;
--
Quoted 3 lines

2.17.0

ack on both patches in this series.

#3 lore raw

Re: [PATCH 2/2] sestatus: free process and file contexts which are checked

Merged: https://github.com/SELinuxProject/selinux/pull/94

Quoted 35 lines · William Roberts
Quoted 30 lines · Nicolas Iooss

clang's static analyzer reports a potential memory leak because the buffers allocated in pc and fc are not freed in main(), in sestatus.c. Free these buffers properly.

---
 policycoreutils/sestatus/sestatus.c | 2 ++
 1 file changed, 2 insertions(+)
policycoreutils/sestatus/sestatus.c +2 -1
index d7f198c2fa0d..9a92e72ff4fd 100644
@@ -436,6 +436,7 @@ int main(int argc, char **argv)
                        printf("%s\n", context);
                        freecon(context);
                }
+               free(pc[i]);
        }
 
        printf("\nFile contexts:\n");
@@ -478,6 +479,7 @@ int main(int argc, char **argv)
                                freecon(context);
                        }
                }
+               free(fc[i]);
        }
 
        return 0;
--
Quoted 3 lines

2.17.0

ack on both patches in this series.