libselinux: Remove trailing slash on selabel_file lookups.
PATCH 1/1 libselinux: Remove trailing slash on selabel_file lookups.
Having a trailing slash on an selabel file lookup can yield a different result.
For example:
/path/foo(/.*)? user:role:type1
/path/foo/bar -d user:role:type2
A lookup of "/path/foo/bar/" would yield user:role:type1 instead of user:role:type2, which is unlike typical Linux behaviors where trailing slashes are ignored.
Many callers already strip the trailing slash before the lookup or users revise the file contexts to work around this. Fix it comprehensively.
--- libselinux/src/label_file.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
libselinux/src/label_file.c +17 -1
@@ -853,7 +853,7 @@ static const struct spec **lookup_all(struct selabel_handle *rec, {struct saved_data *data = (struct saved_data *)rec->data;struct spec *spec_arr = data->spec_arr; -int i, rc, file_stem; +int i, rc, file_stem, len;mode_t mode = (mode_t )type;char *clean_key =NULL ;const char *prev_slash, *next_slash;
@@ -894,6 +894,22 @@ static const struct spec **lookup_all(struct selabel_handle *rec, key = clean_key; } +/* remove trailing slash */ + len =strlen (key); +if (key[len -1 ] =='/' ) { +/* reuse clean_key from above if available */ +if (!clean_key) { + clean_key = (char *)malloc (len); +if (!clean_key) + goto finish; + +strncpy (clean_key, key, len -1 ); + } + + clean_key[len -1 ] ='\0' ; + key = clean_key; + } + sub =selabel_sub_key (data, key);if (sub) key = sub;
Signature · 1 line
2.26.2
Stephen Smalley
maintainerQuoted 25 lines · Chris PeBenito
Having a trailing slash on an selabel file lookup can yield a different result.
For example:
/path/foo(/.*)? user:role:type1
/path/foo/bar -d user:role:type2A lookup of "/path/foo/bar/" would yield user:role:type1 instead of user:role:type2, which is unlike typical Linux behaviors where trailing slashes are ignored.
Many callers already strip the trailing slash before the lookup or users revise the file contexts to work around this. Fix it comprehensively.
Signed-off-by Chris PeBenito <[email protected]>--- libselinux/src/label_file.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)libselinux/src/label_file.c +0
-0
libselinux/src/label_file.c static const struct spec **lookup_all(struct selabel_handle *rec, · line 856
Quoted 5 lines
libselinux/src/label_file.c +1
-1@@ -853,7 +853,7 @@ static const struct spec **lookup_all(struct selabel_handle *rec, {struct saved_data *data = (struct saved_data *)rec->data;struct spec *spec_arr = data->spec_arr; -int i, rc, file_stem; +int i, rc, file_stem, len;
size_t len?
libselinux/src/label_file.c static const struct spec **lookup_all(struct selabel_handle *rec, · line 899
Quoted 10 lines
libselinux/src/label_file.c +3
-0mode_t mode = (mode_t )type;char *clean_key =NULL ;const char *prev_slash, *next_slash;@@ -894,6 +894,22 @@ static const struct spec **lookup_all(struct selabel_handle *rec, key = clean_key; } +/* remove trailing slash */ + len =strlen (key); +if (key[len -1 ] =='/' ) {
What if len == 0?
Quoted 19 lines
libselinux/src/label_file.c +13
-1+/* reuse clean_key from above if available */ +if (!clean_key) { + clean_key = (char *)malloc (len); +if (!clean_key) + goto finish; + +strncpy (clean_key, key, len -1 ); + } + + clean_key[len -1 ] ='\0' ; + key = clean_key; + } + sub =selabel_sub_key (data, key);if (sub) key = sub; --2.26.2