From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 88C81385843E for ; Tue, 22 Nov 2022 00:14:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 88C81385843E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669076056; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hoR3Mb4a1fB/e2UGMeEZvPL77TkZSR/oZP2j3e2TkPw=; b=LfOf1veMMXqw6PNB0C40uIQlYOK6SwJLhwDPBm7oDP2TYjfXoTzWXNVxS6NqZs6U0Z5Uvu GX1i9Mq6UqfGcwekRQIk33M0t88T6XJIHNofbJ4VPKLu6JQq3aXKbzogb6TDP9uW5BUaw1 cHWJ9vBL17wZxQoDWme0Rsf0KtYEeiw= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-517-U75gZyhBO8yDfn0hrECd4Q-1; Mon, 21 Nov 2022 19:14:14 -0500 X-MC-Unique: U75gZyhBO8yDfn0hrECd4Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 381CA3C0ED44 for ; Tue, 22 Nov 2022 00:14:14 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id E3F984A9255; Tue, 22 Nov 2022 00:14:13 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed] analyzer, testsuite: add more examples taken from CWE Date: Mon, 21 Nov 2022 19:14:10 -0500 Message-Id: <20221122001410.3254534-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPAM_BODY,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Successfully tested on x86_64-pc-linux-gnu. Pushed to trunk as r13-4218-g9ada45967b4cf5. gcc/testsuite/ChangeLog: * gcc.dg/analyzer/CWE-131-examples.c: New test. * gcc.dg/analyzer/file-CWE-1341-example.c: New test. * gcc.dg/analyzer/malloc-CWE-401-example.c: New test. * gcc.dg/analyzer/malloc-CWE-415-examples.c: New test. * gcc.dg/analyzer/malloc-CWE-416-examples.c: New test. * gcc.dg/analyzer/malloc-CWE-590-examples.c: New test. Signed-off-by: David Malcolm --- .../gcc.dg/analyzer/CWE-131-examples.c | 146 ++++++++++++++++++ .../gcc.dg/analyzer/file-CWE-1341-example.c | 41 +++++ .../gcc.dg/analyzer/malloc-CWE-401-example.c | 37 +++++ .../gcc.dg/analyzer/malloc-CWE-415-examples.c | 53 +++++++ .../gcc.dg/analyzer/malloc-CWE-416-examples.c | 60 +++++++ .../gcc.dg/analyzer/malloc-CWE-590-examples.c | 44 ++++++ 6 files changed, 381 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/CWE-131-examples.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/file-CWE-1341-example.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/malloc-CWE-401-example.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/malloc-CWE-415-examples.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/malloc-CWE-416-examples.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/malloc-CWE-590-examples.c diff --git a/gcc/testsuite/gcc.dg/analyzer/CWE-131-examples.c b/gcc/testsuite/gcc.dg/analyzer/CWE-131-examples.c new file mode 100644 index 00000000000..3bc898cd0cc --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/CWE-131-examples.c @@ -0,0 +1,146 @@ +/* Examples adapted from https://cwe.mitre.org/data/definitions/131.html + which states "Copyright © 2006–2022, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation." + and which has this on: + https://cwe.mitre.org/about/termsofuse.html + + Terms of Use + + CWE™ is free to use by any organization or individual for any research, development, and/or commercial purposes, per these CWE Terms of Use. The MITRE Corporation ("MITRE") has copyrighted the CWE List, Top 25, CWSS, and CWRAF for the benefit of the community in order to ensure each remains a free and open standard, as well as to legally protect the ongoing use of it and any resulting content by government, vendors, and/or users. CWE is a trademark of MITRE. Please contact cwe@mitre.org if you require further clarification on this issue. + + LICENSE + + CWE Submissions: By submitting materials to The MITRE Corporation’s ("MITRE") Common Weakness Enumeration Program (CWE™), you hereby grant to MITRE a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute your submitted materials and derivative works. Unless otherwise required by applicable law or agreed to in writing, it is understood that you are providing such materials on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + + CWE Usage: MITRE hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. + + DISCLAIMERS + + ALL DOCUMENTS AND THE INFORMATION CONTAINED IN THE CWE ARE PROVIDED ON AN "AS IS" BASIS AND THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES, DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION THEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + + IN NO EVENT SHALL THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE INFORMATION OR THE USE OR OTHER DEALINGS IN THE CWE. */ + +#include +#include +#include + +/* Support decls for example 1. */ + +extern unsigned int GetUntrustedSizeValue(); +extern void ExitError(const char *) __attribute__((noreturn)); + +typedef struct Widget +{ +} Widget; + +#define MAX_NUM_WIDGETS 100 + +extern Widget *InitializeWidget(); +extern void showWidgets(Widget **); + +void example_1 (void) +{ + int i; + unsigned int numWidgets; + Widget **WidgetList; + + numWidgets = GetUntrustedSizeValue(); + if ((numWidgets == 0) || (numWidgets > MAX_NUM_WIDGETS)) { + ExitError("Incorrect number of widgets requested!"); + } + WidgetList = (Widget **)malloc(numWidgets * sizeof(Widget *)); + printf("WidgetList ptr=%p\n", WidgetList); + for(i=0; iheaders; + + if (numHeaders > 100) { + ExitError("too many headers!"); + } + headers = malloc(numHeaders * sizeof(PacketHeader)); /* TODO: ideally we'd warn about possible overflow here with negative numHeaders. */ + ParsePacketHeaders(packet, headers); +} + +void example_5 (void) +{ + int *id_sequence; + + /* Allocate space for an array of three ids. */ + id_sequence = (int*) malloc(3); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */ + if (id_sequence == NULL) exit(1); + + /* Populate the id array. */ + id_sequence[0] = 13579; /* { dg-warning "heap-based buffer overflow" } */ + id_sequence[1] = 24680; /* { dg-warning "heap-based buffer overflow" } */ + id_sequence[2] = 97531; /* { dg-warning "heap-based buffer overflow" } */ +} /* { dg-warning "leak of 'id_sequence'" } */ diff --git a/gcc/testsuite/gcc.dg/analyzer/file-CWE-1341-example.c b/gcc/testsuite/gcc.dg/analyzer/file-CWE-1341-example.c new file mode 100644 index 00000000000..2add3cb109b --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/file-CWE-1341-example.c @@ -0,0 +1,41 @@ +/* Example adapted from https://cwe.mitre.org/data/definitions/1341.html + which states "Copyright © 2006–2022, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation." + and which has this on: + https://cwe.mitre.org/about/termsofuse.html + + Terms of Use + + CWE™ is free to use by any organization or individual for any research, development, and/or commercial purposes, per these CWE Terms of Use. The MITRE Corporation ("MITRE") has copyrighted the CWE List, Top 25, CWSS, and CWRAF for the benefit of the community in order to ensure each remains a free and open standard, as well as to legally protect the ongoing use of it and any resulting content by government, vendors, and/or users. CWE is a trademark of MITRE. Please contact cwe@mitre.org if you require further clarification on this issue. + + LICENSE + + CWE Submissions: By submitting materials to The MITRE Corporation’s ("MITRE") Common Weakness Enumeration Program (CWE™), you hereby grant to MITRE a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute your submitted materials and derivative works. Unless otherwise required by applicable law or agreed to in writing, it is understood that you are providing such materials on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + + CWE Usage: MITRE hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. + + DISCLAIMERS + + ALL DOCUMENTS AND THE INFORMATION CONTAINED IN THE CWE ARE PROVIDED ON AN "AS IS" BASIS AND THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES, DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION THEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + + IN NO EVENT SHALL THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE INFORMATION OR THE USE OR OTHER DEALINGS IN THE CWE. */ + +#include +#include +#include + +void example_1 (void) +{ + char b[2000]; + FILE *f = fopen("dbl_cls.c", "r"); /* { dg-message "opened here" } */ + if (f) + { + b[0] = 0; + fread(b, 1, sizeof(b) - 1, f); + printf("%s\n'", b); + int r1 = fclose(f); /* { dg-message "first 'fclose' here" } */ + printf("\n-----------------\n1 close done '%d'\n", r1); + + int r2 = fclose(f); /* { dg-warning "double 'fclose' of FILE 'f'" } */ + printf("2 close done '%d'\n", r2); + } +} diff --git a/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-401-example.c b/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-401-example.c new file mode 100644 index 00000000000..cfb5e86260c --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-401-example.c @@ -0,0 +1,37 @@ +/* Example adapted from https://cwe.mitre.org/data/definitions/401.html + which states "Copyright © 2006–2022, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation." + and which has this on: + https://cwe.mitre.org/about/termsofuse.html + + Terms of Use + + CWE™ is free to use by any organization or individual for any research, development, and/or commercial purposes, per these CWE Terms of Use. The MITRE Corporation ("MITRE") has copyrighted the CWE List, Top 25, CWSS, and CWRAF for the benefit of the community in order to ensure each remains a free and open standard, as well as to legally protect the ongoing use of it and any resulting content by government, vendors, and/or users. CWE is a trademark of MITRE. Please contact cwe@mitre.org if you require further clarification on this issue. + + LICENSE + + CWE Submissions: By submitting materials to The MITRE Corporation’s ("MITRE") Common Weakness Enumeration Program (CWE™), you hereby grant to MITRE a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute your submitted materials and derivative works. Unless otherwise required by applicable law or agreed to in writing, it is understood that you are providing such materials on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + + CWE Usage: MITRE hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. + + DISCLAIMERS + + ALL DOCUMENTS AND THE INFORMATION CONTAINED IN THE CWE ARE PROVIDED ON AN "AS IS" BASIS AND THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES, DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION THEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + + IN NO EVENT SHALL THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE INFORMATION OR THE USE OR OTHER DEALINGS IN THE CWE. */ + +#define BLOCK_SIZE 4096 + +#include +#include + +char* getBlock(int fd) { + char* buf = (char*) malloc(BLOCK_SIZE); + if (!buf) { + return NULL; + } + if (read(fd, buf, BLOCK_SIZE) != BLOCK_SIZE) { + + return NULL; /* TODO: should complain that "buf" is leaked on this path. */ + } + return buf; +} diff --git a/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-415-examples.c b/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-415-examples.c new file mode 100644 index 00000000000..51d878a17c3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-415-examples.c @@ -0,0 +1,53 @@ +/* Example adapted from https://cwe.mitre.org/data/definitions/415.html + which states "Copyright © 2006–2022, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation." + and which has this on: + https://cwe.mitre.org/about/termsofuse.html + + Terms of Use + + CWE™ is free to use by any organization or individual for any research, development, and/or commercial purposes, per these CWE Terms of Use. The MITRE Corporation ("MITRE") has copyrighted the CWE List, Top 25, CWSS, and CWRAF for the benefit of the community in order to ensure each remains a free and open standard, as well as to legally protect the ongoing use of it and any resulting content by government, vendors, and/or users. CWE is a trademark of MITRE. Please contact cwe@mitre.org if you require further clarification on this issue. + + LICENSE + + CWE Submissions: By submitting materials to The MITRE Corporation’s ("MITRE") Common Weakness Enumeration Program (CWE™), you hereby grant to MITRE a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute your submitted materials and derivative works. Unless otherwise required by applicable law or agreed to in writing, it is understood that you are providing such materials on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + + CWE Usage: MITRE hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. + + DISCLAIMERS + + ALL DOCUMENTS AND THE INFORMATION CONTAINED IN THE CWE ARE PROVIDED ON AN "AS IS" BASIS AND THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES, DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION THEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + + IN NO EVENT SHALL THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE INFORMATION OR THE USE OR OTHER DEALINGS IN THE CWE. */ + +#include +#include + +#define SIZE 1024 + +void example_1 (int abrt) +{ + char* ptr = (char*)malloc (SIZE); + /* ... */ + if (abrt) { + free(ptr); + } + /* ... */ + free(ptr); /* { dg-warning "double-'free' of 'ptr'" } */ +} + +#define BUFSIZE1 512 +#define BUFSIZE2 ((BUFSIZE1/2) - 8) + +int main(int argc, char **argv) { + char *buf1R1; + char *buf2R1; + char *buf1R2; + buf1R1 = (char *) malloc(BUFSIZE2); + buf2R1 = (char *) malloc(BUFSIZE2); + free(buf1R1); + free(buf2R1); + buf1R2 = (char *) malloc(BUFSIZE1); + strncpy(buf1R2, argv[1], BUFSIZE1-1); /* { dg-warning "use of possibly-NULL 'buf1R2'" } */ + free(buf2R1); /* { dg-warning "double-'free' of 'buf2R1'" } */ + free(buf1R2); +} diff --git a/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-416-examples.c b/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-416-examples.c new file mode 100644 index 00000000000..3f5e5e26033 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-416-examples.c @@ -0,0 +1,60 @@ +/* Examples adapted from https://cwe.mitre.org/data/definitions/416.html + which states "Copyright © 2006–2022, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation." + and which has this on: + https://cwe.mitre.org/about/termsofuse.html + + Terms of Use + + CWE™ is free to use by any organization or individual for any research, development, and/or commercial purposes, per these CWE Terms of Use. The MITRE Corporation ("MITRE") has copyrighted the CWE List, Top 25, CWSS, and CWRAF for the benefit of the community in order to ensure each remains a free and open standard, as well as to legally protect the ongoing use of it and any resulting content by government, vendors, and/or users. CWE is a trademark of MITRE. Please contact cwe@mitre.org if you require further clarification on this issue. + + LICENSE + + CWE Submissions: By submitting materials to The MITRE Corporation’s ("MITRE") Common Weakness Enumeration Program (CWE™), you hereby grant to MITRE a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute your submitted materials and derivative works. Unless otherwise required by applicable law or agreed to in writing, it is understood that you are providing such materials on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + + CWE Usage: MITRE hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. + + DISCLAIMERS + + ALL DOCUMENTS AND THE INFORMATION CONTAINED IN THE CWE ARE PROVIDED ON AN "AS IS" BASIS AND THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES, DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION THEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + + IN NO EVENT SHALL THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE INFORMATION OR THE USE OR OTHER DEALINGS IN THE CWE. */ + +#include +#include + +#define BUFSIZER1 512 +#define BUFSIZER2 ((BUFSIZER1/2) - 8) + +int main(int argc, char **argv) { + char *buf1R1; + char *buf2R1; + char *buf2R2; + char *buf3R2; + buf1R1 = (char *) malloc(BUFSIZER1); + buf2R1 = (char *) malloc(BUFSIZER1); + free(buf2R1); + buf2R2 = (char *) malloc(BUFSIZER2); + buf3R2 = (char *) malloc(BUFSIZER2); + strncpy(buf2R1, argv[1], BUFSIZER1-1); /* TODO: should complain about use-after-free here. */ + free(buf1R1); + free(buf2R2); + free(buf3R2); +} + +#define SIZE 1024 +extern void logError(const char *, const char *); + +void example_2 (int err) +{ + int abrt = 0; + + char* ptr = (char*)malloc (SIZE); + if (err) { + abrt = 1; + free(ptr); + } + /* ... */ + if (abrt) { + logError("operation aborted before commit", ptr); /* TODO: arguably should complain about use-after-free of ptr here. */ + } +} /* { dg-warning "leak of 'ptr'" } */ diff --git a/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-590-examples.c b/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-590-examples.c new file mode 100644 index 00000000000..036f8889a92 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/malloc-CWE-590-examples.c @@ -0,0 +1,44 @@ +/* { dg-additional-options "-Wno-free-nonheap-object" } */ + +/* Examples adapted from https://cwe.mitre.org/data/definitions/590.html + which states "Copyright © 2006–2022, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation." + and which has this on: + https://cwe.mitre.org/about/termsofuse.html + + Terms of Use + + CWE™ is free to use by any organization or individual for any research, development, and/or commercial purposes, per these CWE Terms of Use. The MITRE Corporation ("MITRE") has copyrighted the CWE List, Top 25, CWSS, and CWRAF for the benefit of the community in order to ensure each remains a free and open standard, as well as to legally protect the ongoing use of it and any resulting content by government, vendors, and/or users. CWE is a trademark of MITRE. Please contact cwe@mitre.org if you require further clarification on this issue. + + LICENSE + + CWE Submissions: By submitting materials to The MITRE Corporation’s ("MITRE") Common Weakness Enumeration Program (CWE™), you hereby grant to MITRE a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute your submitted materials and derivative works. Unless otherwise required by applicable law or agreed to in writing, it is understood that you are providing such materials on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + + CWE Usage: MITRE hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. + + DISCLAIMERS + + ALL DOCUMENTS AND THE INFORMATION CONTAINED IN THE CWE ARE PROVIDED ON AN "AS IS" BASIS AND THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES, DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION THEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + + IN NO EVENT SHALL THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE MITRE CORPORATION, ITS BOARD OF TRUSTEES, OFFICERS, AGENTS, AND EMPLOYEES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE INFORMATION OR THE USE OR OTHER DEALINGS IN THE CWE. */ + +#include + +typedef struct record_t { char placeholder[1024]; } record_t; +#define MAX_SIZE 100 + +void foo_1(){ + record_t bar[MAX_SIZE]; + + /* do something interesting with bar */ + + /* ... */ + free(bar); /* { dg-warning "'free' of '&bar' which points to memory on the stack" } */ +} + +record_t bar[MAX_SIZE]; //Global var +void foo_2(){ + + /* do something interesting with bar */ + /* ... */ + free(bar); /* { dg-warning "'free' of '&bar' which points to memory not on the heap" } */ +} -- 2.26.3