Skip to contentSkip to conversation

libselinux: ensure that digest_len is not zero

PATCH In review Nicolas Iooss 1 participant 2 messages Aug 5, 2019 – Sep 1, 2019 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: ensure that digest_len is not zero

In add_xattr_entry(), if selabel_get_digests_all_partial_matches() returns with digest_len = 0, the code gets executed as:

    sha1_buf = malloc(digest_len * 2 + 1);  /* Allocate 1 byte */

    /* ... */

    for (i = 0; i < digest_len; i++)  /* Do not do anything */
        sprintf((&sha1_buf[i * 2]), "%02x", xattr_digest[i]);

    /* ... */

    new_entry->digest = strdup(sha1_buf);  /* use of uninitiliazed content */

This is reported by some static code analyzers, even though in practise digest_len should never be zero, and the call to sprintf() ensures that the content of sha1_buf is initialized and terminated by '\0'.

Make sure to never call strdup() on an uninitialized string by verifying that digest_len != 0.

---
 libselinux/src/selinux_restorecon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/selinux_restorecon.c +1 -1
index 1be453f3b494..028d8924235f 100644
@@ -309,7 +309,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
 						&calculated_digest,
 						&xattr_digest, &digest_len);
 
-	if (!xattr_digest) {
+	if (!xattr_digest || !digest_len) {
 		free(calculated_digest);
 		return 1;
 	}
Signature · 1 line
2.22.0
#1 lore raw
Quoted 15 lines · Nicolas Iooss

In add_xattr_entry(), if selabel_get_digests_all_partial_matches() returns with digest_len = 0, the code gets executed as:

    sha1_buf = malloc(digest_len * 2 + 1);  /* Allocate 1 byte */

    /* ... */

    for (i = 0; i < digest_len; i++)  /* Do not do anything */
        sprintf((&sha1_buf[i * 2]), "%02x", xattr_digest[i]);

    /* ... */

    new_entry->digest = strdup(sha1_buf);  /* use of uninitiliazed content */
Quoted 8 lines

This is reported by some static code analyzers, even though in practise digest_len should never be zero, and the call to sprintf() ensures that the content of sha1_buf is initialized and terminated by '\0'.

Make sure to never call strdup() on an uninitialized string by verifying that digest_len != 0.

Merged.

Quoted 20 lines
---
 libselinux/src/selinux_restorecon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/selinux_restorecon.c +1 -2
index 1be453f3b494..028d8924235f 100644
@@ -309,7 +309,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
                                                &calculated_digest,
                                                &xattr_digest, &digest_len);
 
-       if (!xattr_digest) {
+       if (!xattr_digest || !digest_len) {
                free(calculated_digest);
                return 1;
        }
--

2.22.0