From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 1CB4C3858CDB; Wed, 29 Mar 2023 14:03:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1CB4C3858CDB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680098598; bh=Dmii+YghP1WH+S9p330xjQfbdOFJeZefvY0wyRq9vJc=; h=From:To:Subject:Date:From; b=A1U9xgOclHkjtdR+PCYZ2hoTbh0JD+LmuMEjJKQddfK1UXbx6cSB6xkv07sLbGvcg zQa0wZVBerBLEIKtuqfAS/EFhDqb+zKvnhibNV4WAxbJGG5PxV+oycNKlj5nG1utFO u9A3UmItZtEWwnH8Yubp64LRwy07830wDocyQoug= From: "colomar.6.4.3 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/109335] New: -Wanalyzer-malloc-leak false positives and false negatives Date: Wed, 29 Mar 2023 14:03:17 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: analyzer X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: colomar.6.4.3 at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: dmalcolm at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109335 Bug ID: 109335 Summary: -Wanalyzer-malloc-leak false positives and false negatives Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: analyzer Assignee: dmalcolm at gcc dot gnu.org Reporter: colomar.6.4.3 at gmail dot com Target Milestone: --- Created attachment 54786 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D54786&action=3Dedit Preprocessed reproducer Link: With both GCC 12.2.0 (Debian), and GCC 13.0.1 20230315 (built from source), I can reproduce these false positives. I'm on Debian Sid with libbsd-dev 0.11.7-4, and libc-dev 2.36-8. 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=20 #include #include #include #include #include #include #include #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 =3D agetpass("Enter your password: "); if (pass =3D=3D NULL) err(EXIT_FAILURE, "agetpass"); if (strcmp(pass, "secret") =3D=3D 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 =3D MALLOCARRAY(PASS_MAX + 2, char); if (pass =3D=3D NULL) return NULL; if (readpassphrase(prompt, pass, PASS_MAX + 2, RPP_REQUIRE_TTY) =3D= =3D NULL) goto fail; len =3D strlen(pass); if (len =3D=3D PASS_MAX + 1) { errno =3D ENOBUFS; goto fail; } return pass; fail: freezero(pass, PASS_MAX + 2); return NULL; } void erase_pass(char *pass) { freezero(pass, PASS_MAX + 2); } This shows the false positives: $ cc -Wall -Wextra pass.c $(pkgconf --cflags --libs libbsd-overlay) -fanaly= zer -O3 pass.c: In function =E2=80=98agetpass=E2=80=99: pass.c:84:12: warning: leak of =E2=80=98pass=E2=80=99 [CWE-401] [-Wanalyzer= -malloc-leak] 84 | if (pass =3D=3D NULL) | ^ =E2=80=98do_work=E2=80=99: events 1-3 | | 22 | do_work(void) | | ^~~~~~~ | | | | | (1) entry to =E2=80=98do_work=E2=80=99 |...... | 26 | pass =3D agetpass("Enter your password: "); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (2) allocated here | | (3) calling =E2=80=98agetpass=E2=80=99 from =E2= =80=98do_work=E2=80=99 | +--> =E2=80=98agetpass=E2=80=99: events 4-5 | | 78 | agetpass(const char *prompt) | | ^~~~~~~~ | | | | | (4) entry to =E2=80=98agetpass=E2=80=99 |...... | 84 | if (pass =3D=3D NULL) | | ~ | | | | | (5) =E2=80=98pass=E2=80=99 leaks here; was a= llocated at (2) | pass.c:91:12: warning: leak of =E2=80=98pass=E2=80=99 [CWE-401] [-Wanalyzer= -malloc-leak] 91 | if (len =3D=3D PASS_MAX + 1) { | ^ =E2=80=98do_work=E2=80=99: events 1-3 | | 22 | do_work(void) | | ^~~~~~~ | | | | | (1) entry to =E2=80=98do_work=E2=80=99 |...... | 26 | pass =3D agetpass("Enter your password: "); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (2) allocated here | | (3) calling =E2=80=98agetpass=E2=80=99 from =E2= =80=98do_work=E2=80=99 | +--> =E2=80=98agetpass=E2=80=99: events 4-9 | | 78 | agetpass(const char *prompt) | | ^~~~~~~~ | | | | | (4) entry to =E2=80=98agetpass=E2=80=99 |...... | 84 | if (pass =3D=3D NULL) | | ~ | | | | | (5) following =E2=80=98false=E2=80=99 branch= ... |...... | 87 | if (readpassphrase(prompt, pass, PASS_MAX + 2, RPP_REQUIRE_TTY) =3D=3D NULL) | |=20=20=20=20=20=20=20=20=20=20=20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | || | | |(6) ...to here | | (7) following =E2=80=98false=E2=80=99 branch= ... |...... | 90 | len =3D strlen(pass); | | ~~~~~~~~~~~~ | | | | | (8) ...to here | 91 | if (len =3D=3D PASS_MAX + 1) { | | ~ | | | | | (9) following =E2=80=98false=E2=80=99 branch= (when =E2=80=98len !=3D 8192=E2=80=99)... | =E2=80=98agetpass=E2=80=99: event 10 | |cc1: | (10): ...to here | =E2=80=98agetpass=E2=80=99: event 11 | | 91 | if (len =3D=3D PASS_MAX + 1) { | | ^ | | | | | (11) =E2=80=98pass=E2=80=99 leaks here; was = allocated at (2) | Maybe I'm missing something, but I don't think falanyzer is correct here. For the false negatives you just need to compile the above with -DBUG_2 or -DBUG_3. I didn't copy here the results, because well, it's a negative, so it simply shows the same as the above (so the false negatives and false positives happen in= the same compilation, which is quite confusing).=