Skip to contentSkip to conversation

libselinux: ensure strlen() is not called on NULL

PATCH Superseded Nicolas Iooss 2 participants 4 messages Sep 18, 2019 – Sep 20, 2019 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: ensure strlen() is not called on NULL

When compile_regex() calls regex_prepare_data() and this function fails in the following condition:

    *regex = regex_data_create();
    if (!(*regex))
        return -1;

... error_data has been zero-ed and compile_regex() calls:

    regex_format_error(&error_data,
        regex_error_format_buffer,
        sizeof(regex_error_format_buffer));

This leads to a call to strlen(error_data->error_buffer), where error_data->error_buffer is NULL.

Avoid this by checking that error_data->error_buffer is not NULL before calling strlen().

This issue has been found using clang's static analyzer:
https://337-118970575-gh.circle-artifacts.com/0/output-scan-build/2019-09-01-181851-6152-1/report-0b122b.html#EndPath

---
 libselinux/src/regex.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/regex.c +1 -1
index a6fcbbfec1f3..f967efe4a32f 100644
@@ -546,7 +546,7 @@ void regex_format_error(struct regex_error_data const *error_data, char *buffer,
 	if (rc < 0)
 		abort();
 
-	if ((size_t)rc < strlen(error_data->error_buffer))
+	if (error_data->error_buffer && (size_t)rc < strlen(error_data->error_buffer))
 		goto truncated;
 #endif
 
Signature · 1 line
2.22.0
#1 lore raw
Quoted 13 lines · Nicolas Iooss

When compile_regex() calls regex_prepare_data() and this function fails in the following condition:

     *regex = regex_data_create();
     if (!(*regex))
         return -1;

... error_data has been zero-ed and compile_regex() calls:

     regex_format_error(&error_data,
         regex_error_format_buffer,
         sizeof(regex_error_format_buffer));
Quoted 5 lines

This leads to a call to strlen(error_data->error_buffer), where error_data->error_buffer is NULL.

Avoid this by checking that error_data->error_buffer is not NULL before calling strlen().

It seems like regex_format_error() should just return immediately if !error_data->error_code (#ifdef USE_PCRE2) or !error_data->error_buffer (#ifndef USE_PCRE2), since there is no back-end error message to get and report in that situation.

Quoted 23 lines

This issue has been found using clang's static analyzer:
https://337-118970575-gh.circle-artifacts.com/0/output-scan-build/2019-09-01-181851-6152-1/report-0b122b.html#EndPath

---
  libselinux/src/regex.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/regex.c +1 -1
index a6fcbbfec1f3..f967efe4a32f 100644
@@ -546,7 +546,7 @@ void regex_format_error(struct regex_error_data const *error_data, char *buffer,
  	if (rc < 0)
  		abort();
 
-	if ((size_t)rc < strlen(error_data->error_buffer))
+	if (error_data->error_buffer && (size_t)rc < strlen(error_data->error_buffer))
  		goto truncated;
  #endif
 
#2 lore raw
Quoted 18 lines · Stephen Smalley
Quoted 16 lines · Nicolas Iooss

When compile_regex() calls regex_prepare_data() and this function fails in the following condition:

     *regex = regex_data_create();
     if (!(*regex))
         return -1;

... error_data has been zero-ed and compile_regex() calls:

     regex_format_error(&error_data,
         regex_error_format_buffer,
         sizeof(regex_error_format_buffer));

This leads to a call to strlen(error_data->error_buffer), where error_data->error_buffer is NULL.

Quoted 7 lines
Quoted 2 lines

Avoid this by checking that error_data->error_buffer is not NULL before calling strlen().

It seems like regex_format_error() should just return immediately if !error_data->error_code (#ifdef USE_PCRE2) or !error_data->error_buffer (#ifndef USE_PCRE2), since there is no back-end error message to get and report in that situation.

I agree. I will modify the patch.

By the way, while reading function regex_format_error() more precisely, something seems strange in:

pos += rc;
if (pos >= buf_size)

    goto truncated;
if (error_data->error_offset > 0) {
    /* ... */
}

pos += rc;

As rc is not reset to zero, its value is added twice to pos. Is this a bug, or am I misunderstanding something?

Thanks,
Nicolas

#3 lore raw
Quoted 34 lines · Nicolas Iooss
Quoted 18 lines · Stephen Smalley
Quoted 16 lines · Nicolas Iooss

When compile_regex() calls regex_prepare_data() and this function fails in the following condition:

      *regex = regex_data_create();
      if (!(*regex))
          return -1;

... error_data has been zero-ed and compile_regex() calls:

      regex_format_error(&error_data,
          regex_error_format_buffer,
          sizeof(regex_error_format_buffer));

This leads to a call to strlen(error_data->error_buffer), where error_data->error_buffer is NULL.

Quoted 7 lines
Quoted 2 lines

Avoid this by checking that error_data->error_buffer is not NULL before calling strlen().

It seems like regex_format_error() should just return immediately if !error_data->error_code (#ifdef USE_PCRE2) or !error_data->error_buffer (#ifndef USE_PCRE2), since there is no back-end error message to get and report in that situation.

I agree. I will modify the patch.

By the way, while reading function regex_format_error() more precisely, something seems strange in:

pos += rc;
if (pos >= buf_size)

Quoted 8 lines
     goto truncated;
if (error_data->error_offset > 0) {
     /* ... */
}

pos += rc;

As rc is not reset to zero, its value is added twice to pos. Is this a bug, or am I misunderstanding something?

I think you are correct - it is a bug.

Quoted 4 lines

Thanks,
Nicolas