SELinux Archive on lore.kernel.org
 help / color / Atom feed
From: jwcart2 <jwcart2@tycho.nsa.gov>
To: Nicolas Iooss <nicolas.iooss@m4x.org>, selinux@vger.kernel.org
Subject: Re: [Non-DoD Source] [PATCH 3/9] libsepol: do not dereference a failed allocated pointer
Date: Tue, 10 Sep 2019 16:11:39 -0400
Message-ID: <f922c2fb-b8bb-6b73-6c51-0daff051b256@tycho.nsa.gov> (raw)
In-Reply-To: <18ca1f8d-6934-f3e3-31aa-a2a5d1493ffc@tycho.nsa.gov>

On 9/10/19 2:52 PM, jwcart2 wrote:
> On 9/1/19 2:06 PM, Nicolas Iooss wrote:
>> When strs_stack_init(&stack) fails to allocate memory and stack is still
>> NULL, it should not be dereferenced with strs_stack_pop(stack).
>>
>> This issue has been found using Infer static analyzer.
>>
>> Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
>> ---
>>   libsepol/src/kernel_to_cil.c  | 8 +++++---
>>   libsepol/src/kernel_to_conf.c | 8 +++++---
>>   2 files changed, 10 insertions(+), 6 deletions(-)
>>
>> diff --git a/libsepol/src/kernel_to_cil.c b/libsepol/src/kernel_to_cil.c
>> index 320af37b2bc8..9fcc254707ba 100644
>> --- a/libsepol/src/kernel_to_cil.c
>> +++ b/libsepol/src/kernel_to_cil.c
>> @@ -108,10 +108,12 @@ static char *cond_expr_to_str(struct policydb *pdb, 
>> struct cond_expr *expr)
>>       return str;
>>   exit:
>> -    while ((new_val = strs_stack_pop(stack)) != NULL) {
>> -        free(new_val);
>> +    if (stack) {
>> +        while ((new_val = strs_stack_pop(stack)) != NULL) {
>> +            free(new_val);
>> +        }
>> +        strs_stack_destroy(&stack);
>>       }
>> -    strs_stack_destroy(&stack);
>>       return NULL;
>>   }
> 
> constraint_expr_to_str() has the same problem.
> 
>> diff --git a/libsepol/src/kernel_to_conf.c b/libsepol/src/kernel_to_conf.c
>> index 930bafabdd4b..2c8da49a11ab 100644
>> --- a/libsepol/src/kernel_to_conf.c
>> +++ b/libsepol/src/kernel_to_conf.c
>> @@ -106,10 +106,12 @@ static char *cond_expr_to_str(struct policydb *pdb, 
>> struct cond_expr *expr)
>>       return str;
>>   exit:
>> -    while ((new_val = strs_stack_pop(stack)) != NULL) {
>> -        free(new_val);
>> +    if (stack != NULL) {

I forgot to mention that you should just use "if (stack) {" like above so the 
syntax is consistent.

Thanks,
Jim

>> +        while ((new_val = strs_stack_pop(stack)) != NULL) {
>> +            free(new_val);
>> +        }
>> +        strs_stack_destroy(&stack);
>>       }
>> -    strs_stack_destroy(&stack);
>>       return NULL;
>>   }
>>
> 
> Same comment here. constraint_expr_to_str() also needs to be fixed.
> 


-- 
James Carter <jwcart2@tycho.nsa.gov>
National Security Agency

  reply index

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-01 18:06 [PATCH 0/9] Fix issues found by static analyzers Nicolas Iooss
2019-09-01 18:06 ` [PATCH 1/9] semodule-utils: fix comparison with argc Nicolas Iooss
2019-09-01 18:06 ` [PATCH 2/9] libsepol/cil: help static analyzers by aborting when an allocation fails Nicolas Iooss
2019-09-10 18:48   ` [Non-DoD Source] " jwcart2
2019-09-01 18:06 ` [PATCH 3/9] libsepol: do not dereference a failed allocated pointer Nicolas Iooss
2019-09-10 18:52   ` [Non-DoD Source] " jwcart2
2019-09-10 20:11     ` jwcart2 [this message]
2019-09-01 18:06 ` [PATCH 4/9] libsepol: do not dereference scope if it can be NULL Nicolas Iooss
2019-09-01 18:06 ` [PATCH 5/9] libsepol: reset *p to NULL if sepol_module_package_create fails Nicolas Iooss
2019-09-01 18:06 ` [PATCH 6/9] libsepol/cil: do not dereference perm_value_to_cil when it has not been allocated Nicolas Iooss
2019-09-01 18:06 ` [PATCH 7/9] python/chcat: remove unnecessary assignment Nicolas Iooss
2019-09-01 18:06 ` [PATCH 8/9] python/sepolicy: remove unnecessary pass statement Nicolas Iooss
2019-09-01 18:06 ` [PATCH 9/9] libsepol/tests: do not dereference a NULL pointer Nicolas Iooss

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f922c2fb-b8bb-6b73-6c51-0daff051b256@tycho.nsa.gov \
    --to=jwcart2@tycho.nsa.gov \
    --cc=nicolas.iooss@m4x.org \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

SELinux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/selinux/0 selinux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 selinux selinux/ https://lore.kernel.org/selinux \
		selinux@vger.kernel.org selinux@archiver.kernel.org
	public-inbox-index selinux


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.selinux


AGPL code for this site: git clone https://public-inbox.org/ public-inbox