public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: GCC <gcc@gcc.gnu.org>
Subject: -Wanalyzer-malloc-leak false positives
Date: Wed, 29 Mar 2023 15:20:29 +0200	[thread overview]
Message-ID: <45c0584d-b326-a975-7ebc-cef76e154530@gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 6287 bytes --]

Hi!

With both GCC 12.2.0 (Debian), and GCC 13.0.1 20230315 (built from source),
I can reproduce these false positives.

The reproducer program is a small program that checks a password against a
hardcoded string, and conditionally prints "validated".  I wrote it
precisely to demonstrate how [[gnu::malloc(deallocator)]] can be used to
ensure that passwords are not leaked in memory, but I found out that it
fails to detect some conditions.

Here's the program (it uses agetpass(), as defined in the shadow project):

$ cat pass.c 
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <readpassphrase.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define PASS_MAX  BUFSIZ - 1


#define MALLOCARRAY(n, type)   ((type *) mallocarray(n, sizeof(type)))


[[gnu::malloc, gnu::malloc(free)]] void *mallocarray(size_t nmemb, size_t size);
void erase_pass(char *pass);
[[gnu::malloc(erase_pass)]] char *agetpass(const char *prompt);


void
do_work(void)
{
	char  *pass;

	pass = agetpass("Enter your password: ");
	if (pass == NULL)
		err(EXIT_FAILURE, "agetpass");

	if (strcmp(pass, "secret") == 0)
		puts("validated");

	/* erase_pass() zeroes the memory (think of memset(3), or bzero(3))
	   and then releases the memory to the system (think of free(3)).
	   If you only call free(pass), then you release the memory to the
	   system without zeroing it.  Remember it contains a password!
	   We would be leaking a password into the system memory, which can
	   later be assigned to a different process.

	   So, we should call erase_pass() as soon as possible, but let's
	   say we forgot, and just call free():
	*/
#if defined(BUG_1)
	// We forgot to zero the memory.
	free(pass);
	// GCC correctly catches this as -Wmismatched-dealloc
#elif defined(BUG_2)
	// We zeroed, but forgot to free(3).
	bzero(pass, PASS_MAX + 2);
	// GCC misses this.
#elif defined(BUG_3)
	// We forgot both of them.
	// GCC also misses this.
#else
	erase_pass(pass);  // OK, but 2 false positives.
#endif
}


int
main(void)
{
	do_work();

	for (;;)
		sleep(1);
}


void *
mallocarray(size_t nmemb, size_t size)
{
	return reallocarray(NULL, nmemb, size);
}


char *
agetpass(const char *prompt)
{
	char    *pass;
	size_t  len;

	pass = MALLOCARRAY(PASS_MAX + 2, char);
	if (pass == NULL)
		return NULL;

	if (readpassphrase(prompt, pass, PASS_MAX + 2, RPP_REQUIRE_TTY) == NULL)
		goto fail;

	len = strlen(pass);
	if (len == PASS_MAX + 1) {
		errno = ENOBUFS;
		goto fail;
	}

	return pass;

fail:
	freezero(pass, PASS_MAX + 2);
	return NULL;
}


void
erase_pass(char *pass)
{
	freezero(pass, PASS_MAX + 2);
}



$ cc -Wall -Wextra pass.c $(pkgconf --cflags --libs libbsd-overlay) -fanalyzer -O3
pass.c: In function ‘agetpass’:
pass.c:84:12: warning: leak of ‘pass’ [CWE-401] [-Wanalyzer-malloc-leak]
   84 |         if (pass == NULL)
      |            ^
  ‘do_work’: events 1-3
    |
    |   22 | do_work(void)
    |      | ^~~~~~~
    |      | |
    |      | (1) entry to ‘do_work’
    |......
    |   26 |         pass = agetpass("Enter your password: ");
    |      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                |
    |      |                (2) allocated here
    |      |                (3) calling ‘agetpass’ from ‘do_work’
    |
    +--> ‘agetpass’: events 4-5
           |
           |   78 | agetpass(const char *prompt)
           |      | ^~~~~~~~
           |      | |
           |      | (4) entry to ‘agetpass’
           |......
           |   84 |         if (pass == NULL)
           |      |            ~
           |      |            |
           |      |            (5) ‘pass’ leaks here; was allocated at (2)
           |
pass.c:91:12: warning: leak of ‘pass’ [CWE-401] [-Wanalyzer-malloc-leak]
   91 |         if (len == PASS_MAX + 1) {
      |            ^
  ‘do_work’: events 1-3
    |
    |   22 | do_work(void)
    |      | ^~~~~~~
    |      | |
    |      | (1) entry to ‘do_work’
    |......
    |   26 |         pass = agetpass("Enter your password: ");
    |      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                |
    |      |                (2) allocated here
    |      |                (3) calling ‘agetpass’ from ‘do_work’
    |
    +--> ‘agetpass’: events 4-9
           |
           |   78 | agetpass(const char *prompt)
           |      | ^~~~~~~~
           |      | |
           |      | (4) entry to ‘agetpass’
           |......
           |   84 |         if (pass == NULL)
           |      |            ~
           |      |            |
           |      |            (5) following ‘false’ branch...
           |......
           |   87 |         if (readpassphrase(prompt, pass, PASS_MAX + 2, RPP_REQUIRE_TTY) == NULL)
           |      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |            ||
           |      |            |(6) ...to here
           |      |            (7) following ‘false’ branch...
           |......
           |   90 |         len = strlen(pass);
           |      |               ~~~~~~~~~~~~
           |      |               |
           |      |               (8) ...to here
           |   91 |         if (len == PASS_MAX + 1) {
           |      |            ~
           |      |            |
           |      |            (9) following ‘false’ branch (when ‘len != 8192’)...
           |
         ‘agetpass’: event 10
           |
           |cc1:
           | (10): ...to here
           |
         ‘agetpass’: event 11
           |
           |   91 |         if (len == PASS_MAX + 1) {
           |      |            ^
           |      |            |
           |      |            (11) ‘pass’ leaks here; was allocated at (2)
           |


Maybe I'm missing something, but I don't think falanyzer is correct here.
Should I report this in bugzilla?

Cheers,
Alex

-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

             reply	other threads:[~2023-03-29 13:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-29 13:20 Alejandro Colomar [this message]
2023-03-29 13:32 ` David Malcolm
2023-03-29 13:41   ` Alejandro Colomar
2023-03-29 14:04     ` Alejandro Colomar

Reply instructions:

You may reply publicly 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=45c0584d-b326-a975-7ebc-cef76e154530@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=gcc@gcc.gnu.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).