libselinux: ignore invalid class name lookup
PATCH 1/1 libselinux: ignore invalid class name lookup
selinux_check_access relies on string_to_security_class to resolve the class index from its char* argument. There is no input validation done on the string provided. It is possible to supply an argument containing trailing backslashes (i.e., "sock_file//////") so that the paths built in discover_class get truncated. The processing will then reference the same permission file multiple time (e.g., perms/watch_reads will be truncated to perms/watch). This will leak the memory allocated when strdup'ing the permission name. The discover_class_cache will end up in an invalid state (but not corrupted).
Ensure that the class provided does not contain any path separator.
--- libselinux/src/stringrep.c | 3 +++ 1 file changed, 3 insertions(+)
libselinux/src/stringrep.c +3 -0
@@ -63,6 +63,9 @@ static struct discover_class_node * discover_class(const char *s)return NULL ; } +if (strchr (s,'/' ) !=NULL ) +return NULL ; +/* allocate a node */ node =malloc (sizeof (struct discover_class_node ));if (node ==NULL )
Signature · 1 line
2.38.0.135.g90850a2211-goog
An alternative to this patch is to implement stricter input validation on the class name. I could not find any explicit restriction on the characters of a class. Empirically, it seems that [A-Za-z0-9_] would be sufficient to cover the refpolicy and Android classes. A regex matching would have a performance impact here, this is why the strchr solution was sent. Let me know if you’d prefer to explore the regex alternative.
Thanks
Quoted 5 lines · Thiébaud Weksteen
selinux_check_accessrelies onstring_to_security_classto resolve the class index from its char* argument. There is no input validation done on the string provided. It is possible to supply an argument containing trailing backslashes (i.e., "sock_file//////") so that the paths built
I am having trouble reproducing this. Using backslashes causes an error when looking up the "%s/class/%s/index" path.
Using forward slashes just works. Valgrind does not report any memory leaks in either case and I don't see the same permission file being referenced multiple times.
I don't think that we need the regex solution.
Thanks,
Jim
Quoted 30 lines
in
discover_classget truncated. The processing will then reference the same permission file multiple time (e.g., perms/watch_reads will be truncated to perms/watch). This will leak the memory allocated when strdup'ing the permission name. Thediscover_class_cachewill end up in an invalid state (but not corrupted).Ensure that the class provided does not contain any path separator.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/stringrep.c | 3 +++ 1 file changed, 3 insertions(+)libselinux/src/stringrep.c +3
-1@@ -63,6 +63,9 @@ static struct discover_class_node * discover_class(const char *s)return NULL ; } +if (strchr (s,'/' ) !=NULL ) +return NULL ; +/* allocate a node */ node =malloc (sizeof (struct discover_class_node ));if (node ==NULL ) --2.38.0.135.g90850a2211-goog
Quoted 7 lines · James Carter
Quoted 5 lines · Thiébaud Weksteen
selinux_check_accessrelies onstring_to_security_classto resolve the class index from its char* argument. There is no input validation done on the string provided. It is possible to supply an argument containing trailing backslashes (i.e., "sock_file//////") so that the paths built
Two other interfaces might also be affected by path traversing inputs:
- getseuser(3) via the username parameter
- bool_open(), called by security_get_boolean_pending(3), security_get_boolean_active(3) and security_set_boolean(3) , via the name parameter
Quoted 43 lines
I am having trouble reproducing this. Using backslashes causes an error when looking up the "%s/class/%s/index" path.
Using forward slashes just works. Valgrind does not report any memory leaks in either case and I don't see the same permission file being referenced multiple times.I don't think that we need the regex solution.
Thanks,
JimQuoted 30 lines
in
discover_classget truncated. The processing will then reference the same permission file multiple time (e.g., perms/watch_reads will be truncated to perms/watch). This will leak the memory allocated when strdup'ing the permission name. Thediscover_class_cachewill end up in an invalid state (but not corrupted).Ensure that the class provided does not contain any path separator.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/stringrep.c | 3 +++ 1 file changed, 3 insertions(+)libselinux/src/stringrep.c +3
-1@@ -63,6 +63,9 @@ static struct discover_class_node * discover_class(const char *s)return NULL ; } +if (strchr (s,'/' ) !=NULL ) +return NULL ; +/* allocate a node */ node =malloc (sizeof (struct discover_class_node ));if (node ==NULL ) --2.38.0.135.g90850a2211-goog
Quoted 10 lines · Christian Göttsche
Quoted 7 lines · James Carter
Quoted 5 lines · Thiébaud Weksteen
selinux_check_accessrelies onstring_to_security_classto resolve the class index from its char* argument. There is no input validation done on the string provided. It is possible to supply an argument containing trailing backslashes (i.e., "sock_file//////") so that the paths built
Quoted 13 lines
Two other interfaces might also be affected by path traversing inputs:
- getseuser(3) via the username parameter
-bool_open(), called bysecurity_get_boolean_pending(3),security_get_boolean_active(3)andsecurity_set_boolean(3), via the name parameterQuoted 6 lines
I am having trouble reproducing this. Using backslashes causes an error when looking up the "%s/class/%s/index" path.
Using forward slashes just works. Valgrind does not report any memory leaks in either case and I don't see the same permission file being referenced multiple times.
Apologies, it should read "forward slashes" and not "backward slashes". The exact argument is "sock_file" followed by 4051 forward slashes. With that length, the index file and perms directory will open successfully (both paths are 4089 bytes long, assuming /sys/fs/selinux for selinuxfs). When reading the watch_with_perm entry, the "_with_perm" will be dropped and the "watch" permission will be read instead (the "_" character is the 4096th byte). On Android, PATH_MAX is 4096.
This was found by our fuzzer for the selinux_check_access function [1].
Thanks
Quoted 37 lines
Quoted 37 lines
I don't think that we need the regex solution.
Thanks,
JimQuoted 30 lines
in
discover_classget truncated. The processing will then reference the same permission file multiple time (e.g., perms/watch_reads will be truncated to perms/watch). This will leak the memory allocated when strdup'ing the permission name. Thediscover_class_cachewill end up in an invalid state (but not corrupted).Ensure that the class provided does not contain any path separator.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/stringrep.c | 3 +++ 1 file changed, 3 insertions(+)libselinux/src/stringrep.c +3
-1@@ -63,6 +63,9 @@ static struct discover_class_node * discover_class(const char *s)return NULL ; } +if (strchr (s,'/' ) !=NULL ) +return NULL ; +/* allocate a node */ node =malloc (sizeof (struct discover_class_node ));if (node ==NULL ) --2.38.0.135.g90850a2211-goog
Quoted 36 lines · Thiébaud Weksteen
Quoted 10 lines · Christian Göttsche
Quoted 7 lines · James Carter
Quoted 5 lines · Thiébaud Weksteen
selinux_check_accessrelies onstring_to_security_classto resolve the class index from its char* argument. There is no input validation done on the string provided. It is possible to supply an argument containing trailing backslashes (i.e., "sock_file//////") so that the paths builtQuoted 13 lines
Two other interfaces might also be affected by path traversing inputs:
- getseuser(3) via the username parameter
-bool_open(), called bysecurity_get_boolean_pending(3),security_get_boolean_active(3)andsecurity_set_boolean(3), via the name parameterQuoted 6 lines
I am having trouble reproducing this. Using backslashes causes an error when looking up the "%s/class/%s/index" path.
Using forward slashes just works. Valgrind does not report any memory leaks in either case and I don't see the same permission file being referenced multiple times.Apologies, it should read "forward slashes" and not "backward slashes". The exact argument is "
sock_file" followed by 4051 forward slashes. With that length, the index file and perms directory will open successfully (both paths are 4089 bytes long, assuming/sys/fs/selinuxfor selinuxfs). When reading thewatch_with_permentry, the "_with_perm" will be dropped and the "watch" permission will be read instead (the "_" character is the 4096th byte). On Android,PATH_MAXis 4096.
Quoted 6 lines
This was found by our fuzzer for the
selinux_check_accessfunction [1].Thanks
Thanks for the clarification.
Jim
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
I don't think that we need the regex solution.
Thanks,
JimQuoted 30 lines
in
discover_classget truncated. The processing will then reference the same permission file multiple time (e.g., perms/watch_reads will be truncated to perms/watch). This will leak the memory allocated when strdup'ing the permission name. Thediscover_class_cachewill end up in an invalid state (but not corrupted).Ensure that the class provided does not contain any path separator.
Signed-off-by Thiébaud Weksteen <[email protected]>--- libselinux/src/stringrep.c | 3 +++ 1 file changed, 3 insertions(+)libselinux/src/stringrep.c +3
-1@@ -63,6 +63,9 @@ static struct discover_class_node * discover_class(const char *s)return NULL ; } +if (strchr (s,'/' ) !=NULL ) +return NULL ; +/* allocate a node */ node =malloc (sizeof (struct discover_class_node ));if (node ==NULL ) --2.38.0.135.g90850a2211-goog
Quoted 11 lines · Thiébaud Weksteen
selinux_check_accessrelies onstring_to_security_classto resolve the class index from its char* argument. There is no input validation done on the string provided. It is possible to supply an argument containing trailing backslashes (i.e., "sock_file//////") so that the paths built indiscover_classget truncated. The processing will then reference the same permission file multiple time (e.g., perms/watch_reads will be truncated to perms/watch). This will leak the memory allocated when strdup'ing the permission name. Thediscover_class_cachewill end up in an invalid state (but not corrupted).
Quoted 3 lines
Ensure that the class provided does not contain any path separator.
Signed-off-by Thiébaud Weksteen <[email protected]>
Quoted 21 lines
--- libselinux/src/stringrep.c | 3 +++ 1 file changed, 3 insertions(+)libselinux/src/stringrep.c +3
-1@@ -63,6 +63,9 @@ static struct discover_class_node * discover_class(const char *s)return NULL ; } +if (strchr (s,'/' ) !=NULL ) +return NULL ; +/* allocate a node */ node =malloc (sizeof (struct discover_class_node ));if (node ==NULL ) --2.38.0.135.g90850a2211-goog
Quoted 13 lines · James Carter
Quoted 11 lines · Thiébaud Weksteen
selinux_check_accessrelies onstring_to_security_classto resolve the class index from its char* argument. There is no input validation done on the string provided. It is possible to supply an argument containing trailing backslashes (i.e., "sock_file//////") so that the paths built indiscover_classget truncated. The processing will then reference the same permission file multiple time (e.g., perms/watch_reads will be truncated to perms/watch). This will leak the memory allocated when strdup'ing the permission name. Thediscover_class_cachewill end up in an invalid state (but not corrupted).
Quoted 6 lines
Quoted 3 lines
Ensure that the class provided does not contain any path separator.
Signed-off-by Thiébaud Weksteen <[email protected]>Acked-by James Carter <[email protected]>
Merged.
Thanks,
Jim
Quoted 21 lines
Quoted 21 lines
--- libselinux/src/stringrep.c | 3 +++ 1 file changed, 3 insertions(+)libselinux/src/stringrep.c +3
-1@@ -63,6 +63,9 @@ static struct discover_class_node * discover_class(const char *s)return NULL ; } +if (strchr (s,'/' ) !=NULL ) +return NULL ; +/* allocate a node */ node =malloc (sizeof (struct discover_class_node ));if (node ==NULL ) --2.38.0.135.g90850a2211-goog