Message ID | 20230824143903.3161185-1-dmalcolm@redhat.com |
---|---|
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp1168811vqm; Thu, 24 Aug 2023 07:43:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHcr+X2nzBDVRRYTl4RjAyeoZF9A2jT8C78ZC+rkWXigrvvheTBqN7OlDOpyWobFNTZrGqS X-Received: by 2002:a17:906:9bce:b0:98e:370c:be69 with SMTP id de14-20020a1709069bce00b0098e370cbe69mr17558309ejc.6.1692888205385; Thu, 24 Aug 2023 07:43:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692888205; cv=none; d=google.com; s=arc-20160816; b=sKEsFVaoicFZwLMD3iEyZfLB8dpz0nN5fvdq5QfsAP2OZli72+A10wbdnnwPCRk1HW jP7K6mJgLeZOl/V1Fml04o/OGngeCzV68ZKw7LTGaaDRk33eWtkzOwiLIXEEokpGpDpi eg/Udzlz/r3qUJPOqbyc4O95QiRZ392gs0OP4k85OIekXh2vvJGPJH59aR78gpIwN8jx cfjYvFX25ElTQx5l25f/H392ZPEGI4Wd+Owy0JA19aL6X5awwLeJf+q1rDXAHFl8uVwm aSDexJG2ByGEjlJ9BNlJ/vOoxXY4iYFxzEI81VvCq/J8hlF8ucId0r8UntWwBpWOoUQR 9jXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=iWAFq5o1fY1MBum7yPX7ZzoNnlkcwxHJcEN5eoQmLvU=; fh=fa0YJSH7y4k0snNuxsKgOI6vGP7j2f0jR9F1WHC95UA=; b=O1IlLzcadhiLwhECGqylXgXcbu2Jil44uvyle3lUYzIllk0pLG5+fB1UsoGqiq+CcU 8c6V/4nNPEFUExizv91+J5qJvwrB7NOBnX1I3GWGzAZyu6m2YYJgD8I0h2HLSFjEgNYZ 0ao/fJ3UtxmaeSNwh5x/v5oOgpd3qkpKMYYeWL38aGYMzmsbqBjGStCokVlajKSYxwvt NWKTaqIhof7E06/gigyD994U+07FtppERkT6lce3SfQw3rYV/NAaNRPirwwEYcW8gwO6 Pb5Y/JEGq5ZT4E4k9QXjAiItgA6nC+wlfRRGfeivc9cN29iu89fMGwQq1vrjoPUV3xv5 Le7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ib2H+OMl; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id gw1-20020a170906f14100b0099331b3e6f6si10415171ejb.661.2023.08.24.07.43.25 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Aug 2023 07:43:25 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ib2H+OMl; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D73873882036 for <ouuuleilei@gmail.com>; Thu, 24 Aug 2023 14:40:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D73873882036 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692888054; bh=iWAFq5o1fY1MBum7yPX7ZzoNnlkcwxHJcEN5eoQmLvU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Ib2H+OMlazg+zhU0RVFXfoAd4tQ9z43aEO0vxpWt4ZU4nQbH1G479gLuf+WvUW0+W wSJEDLsZ5UjkrSlYy/kKYsaxmSK7f4f8VGexc7MZyImBxTyFfqccsX0L6KCMfTxI7b 1IdfdQeq6JC8uA/w32dbfjzeLQOCWsho4lXUZ2Hk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 2DAC23858C2C for <gcc-patches@gcc.gnu.org>; Thu, 24 Aug 2023 14:39:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2DAC23858C2C Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-216-oTGxKLAAPWSgIGHE0x_mlA-1; Thu, 24 Aug 2023 10:39:06 -0400 X-MC-Unique: oTGxKLAAPWSgIGHE0x_mlA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C6F083C14AA1 for <gcc-patches@gcc.gnu.org>; Thu, 24 Aug 2023 14:39:05 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.32.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F70B40D2839; Thu, 24 Aug 2023 14:39:05 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm <dmalcolm@redhat.com> Subject: [pushed 0/9] analyzer: strlen, strcpy, and strcat [PR105899] Date: Thu, 24 Aug 2023 10:38:54 -0400 Message-Id: <20230824143903.3161185-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=-4.2 required=5.0 tests=BAYES_00, BODY_8BITS, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: David Malcolm via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: David Malcolm <dmalcolm@redhat.com> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775121943244713760 X-GMAIL-MSGID: 1775121943244713760 |
Series |
analyzer: strlen, strcpy, and strcat [PR105899]
|
|
Message
David Malcolm
Aug. 24, 2023, 2:38 p.m. UTC
This patch kit makes improvements to the analyzer's new strlen implementation, and wires it up to strcpy and strcat. For example, given: #include <string.h> void test (void) { char buf[10]; strcpy (buf, "hello world!"); } we now emit: demo.c: In function ‘test’: demo.c:6:3: warning: stack-based buffer overflow [CWE-121] [-Wanalyzer-out-of-bounds] 6 | strcpy (buf, "hello world!"); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ ‘test’: events 1-2 | | 5 | char buf[10]; | | ^~~ | | | | | (1) capacity: 10 bytes | 6 | strcpy (buf, "hello world!"); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (2) out-of-bounds write from byte 10 till byte 12 but ‘buf’ ends at byte 10 | demo.c:6:3: note: write of 3 bytes to beyond the end of ‘buf’ 6 | strcpy (buf, "hello world!"); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ demo.c:6:3: note: valid subscripts for ‘buf’ are ‘[0]’ to ‘[9]’ ┌─────┬─────┬────┬────┬────┬────┬────┬────┬────┬────┐┌─────┬─────┬─────┐ │ [0] │ [1] │[2] │[3] │[4] │[5] │[6] │[7] │[8] │[9] ││[10] │[11] │[12] │ ├─────┼─────┼────┼────┼────┼────┼────┼────┼────┼────┤├─────┼─────┼─────┤ │ ‘h’ │ ‘e’ │‘l’ │‘l’ │‘o’ │‘ ’ │‘w’ │‘o’ │‘r’ │‘l’ ││ ‘d’ │ ‘!’ │ NUL │ ├─────┴─────┴────┴────┴────┴────┴────┴────┴────┴────┴┴─────┴─────┴─────┤ │ string literal (type: ‘char[13]’) │ └──────────────────────────────────────────────────────────────────────┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ v v v v v v v v v v v v v ┌─────┬────────────────────────────────────────┬────┐┌─────────────────┐ │ [0] │ ... │[9] ││ │ ├─────┴────────────────────────────────────────┴────┤│after valid range│ │ ‘buf’ (type: ‘char[10]’) ││ │ └───────────────────────────────────────────────────┘└─────────────────┘ ├─────────────────────────┬─────────────────────────┤├────────┬────────┤ │ │ ╭─────────┴────────╮ ╭───────────┴──────────╮ │capacity: 10 bytes│ │⚠️ overflow of 3 bytes│ ╰──────────────────╯ ╰──────────────────────╯ in addition to the pre-existing: demo.c:6:3: warning: ‘__builtin_memcpy’ writing 13 bytes into a region of size 10 overflows the destination [-Wstringop-overflow=] demo.c:5:8: note: destination object ‘buf’ of size 10 5 | char buf[10]; | ^~~ Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-3461-g9aaec66917c96a through to r14-3469-gbbdc0e0d0042ae. David Malcolm (9): analyzer: add logging to impl_path_context analyzer: handle symbolic bindings in scan_for_null_terminator [PR105899] analyzer: reimplement kf_strcpy [PR105899] analyzer: eliminate region_model::get_string_size [PR105899] analyzer: reimplement kf_memcpy_memmove analyzer: handle strlen(INIT_VAL(STRING_REG)) [PR105899] analyzer: handle INIT_VAL(ELEMENT_REG(STRING_REG), CONSTANT_SVAL) [PR105899] analyzer: handle strlen(BITS_WITHIN) [PR105899] analyzer: implement kf_strcat [PR105899] gcc/analyzer/call-details.cc | 12 +- gcc/analyzer/call-details.h | 5 +- gcc/analyzer/engine.cc | 13 +- gcc/analyzer/kf.cc | 116 +++++--- gcc/analyzer/region-model-manager.cc | 19 ++ gcc/analyzer/region-model.cc | 261 +++++++++++++----- gcc/analyzer/region-model.h | 22 +- gcc/doc/invoke.texi | 1 + .../analyzer/out-of-bounds-diagram-16.c | 31 +++ gcc/testsuite/gcc.dg/analyzer/sprintf-1.c | 11 + gcc/testsuite/gcc.dg/analyzer/strcat-1.c | 136 +++++++++ gcc/testsuite/gcc.dg/analyzer/strcpy-1.c | 22 ++ gcc/testsuite/gcc.dg/analyzer/strcpy-3.c | 8 + gcc/testsuite/gcc.dg/analyzer/strcpy-4.c | 51 ++++ 14 files changed, 601 insertions(+), 107 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/out-of-bounds-diagram-16.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/strcat-1.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/strcpy-4.c