Skip to contentSkip to conversation

libselinux: prevent path traversal via crafted username in getseuser()

PATCH In review Stephen Smalley 2 participants 3 messages Nov 19 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libselinux: prevent path traversal via crafted username in getseuser()

getseuser() builds "<policy_root>/logins/<username>" and reads the per-user seuser mapping from that file. The username is whatever NSS resolved for the login and is passed straight through by pam_selinux; Linux imposes no character-set restriction on NSS names, and directory backends (LDAP/AD) can serve names containing "/".

A crafted username can make a root-running PAM stack read a file the user controls and take that user's SELinux user and MLS range from it, i.e. the confinement decision is delegated to whoever named the account. Fall through to the ordinary seusers lookup for such names instead.

---
 libselinux/src/seusers.c | 8 ++++++++
 1 file changed, 8 insertions(+)
libselinux/src/seusers.c +8 -0
index ea542392..df9385eb 100644
@@ -293,6 +293,14 @@ int getseuser(const char *username, const char *service, char **r_seuser,
 	char *rec = NULL;
 	char *path = NULL;
 	FILE *fp = NULL;
+
+	/*
+	 * Prevent path traversal via crafted username; no valid username
+	 * should be NULL, start with ".", or contain a '/'.
+	 */
+	if (!username || username[0] == '.' || strchr(username, '/'))
+		goto err;
+
 	if (asprintf(&path, "%s/logins/%s", selinux_policy_root(), username) <
 	    0)
 		goto err;
Signature · 1 line
2.47.3
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Stephen Smalley

getseuser() builds "<policy_root>/logins/<username>" and reads the per-user seuser mapping from that file. The username is whatever NSS resolved for the login and is passed straight through by pam_selinux; Linux imposes no character-set restriction on NSS names, and directory backends (LDAP/AD) can serve names containing "/".

Quoted 6 lines

A crafted username can make a root-running PAM stack read a file the user controls and take that user's SELinux user and MLS range from it, i.e. the confinement decision is delegated to whoever named the account. Fall through to the ordinary seusers lookup for such names instead.

Quoted 25 lines
---
 libselinux/src/seusers.c | 8 ++++++++
 1 file changed, 8 insertions(+)
libselinux/src/seusers.c +8 -0
index ea542392..df9385eb 100644
@@ -293,6 +293,14 @@ int getseuser(const char *username, const char *service, char **r_seuser,
 	char *rec = NULL;
 	char *path = NULL;
 	FILE *fp = NULL;
+
+	/*
+	 * Prevent path traversal via crafted username; no valid username
+	 * should be NULL, start with ".", or contain a '/'.
+	 */
+	if (!username || username[0] == '.' || strchr(username, '/'))
+		goto err;
+
 	if (asprintf(&path, "%s/logins/%s", selinux_policy_root(), username) <
 	    0)
 		goto err;
Signature · 1 line
2.47.3
#2 lore raw
Quoted 8 lines · Petr Lautrbach
Quoted 6 lines · Stephen Smalley

getseuser() builds "<policy_root>/logins/<username>" and reads the per-user seuser mapping from that file. The username is whatever NSS resolved for the login and is passed straight through by pam_selinux; Linux imposes no character-set restriction on NSS names, and directory backends (LDAP/AD) can serve names containing "/".

Quoted 8 lines
Quoted 6 lines

A crafted username can make a root-running PAM stack read a file the user controls and take that user's SELinux user and MLS range from it, i.e. the confinement decision is delegated to whoever named the account. Fall through to the ordinary seusers lookup for such names instead.

Merged.

Quoted 25 lines
Quoted 25 lines
---
 libselinux/src/seusers.c | 8 ++++++++
 1 file changed, 8 insertions(+)
libselinux/src/seusers.c +8 -0
index ea542392..df9385eb 100644
@@ -293,6 +293,14 @@ int getseuser(const char *username, const char *service, char **r_seuser,
 	char *rec = NULL;
 	char *path = NULL;
 	FILE *fp = NULL;
+
+	/*
+	 * Prevent path traversal via crafted username; no valid username
+	 * should be NULL, start with ".", or contain a '/'.
+	 */
+	if (!username || username[0] == '.' || strchr(username, '/'))
+		goto err;
+
 	if (asprintf(&path, "%s/logins/%s", selinux_policy_root(), username) <
 	    0)
 		goto err;
Signature · 1 line
2.47.3