From patchwork Tue Aug 22 01:21:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 136436 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3347880vqi; Mon, 21 Aug 2023 18:23:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHLX16fLCigo0F9BquFTmQbicSaEaDvzJrV8Yxedl/msI8LJq4XB2pYnpiHLj3LgbmISWzX X-Received: by 2002:a17:906:255a:b0:99c:47a:8bcd with SMTP id j26-20020a170906255a00b0099c047a8bcdmr5347028ejb.67.1692667389270; Mon, 21 Aug 2023 18:23:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692667389; cv=none; d=google.com; s=arc-20160816; b=vtR1cc5EpxlJ9csARWHo3yqGuk17XmHi/8TbxUt8Dk0gymA8NEATYIYv3umkSYtq9P xO9OqJQyBgSBXTP6tjZBXV8wbkdYMrZCS+wmvNJvoFVl20Mqu/qrpjvglhFHTO3lqMyy oha2EXXMW7zKA88JIcsuhYnOx9u8QXLMSC9kNdyYKdqEvsG8+ND6MZxpoc0oaDRAl6Gf tMdcfzkxAfUuwezi6VqmURHjM73UgmW1NySRZ9aoCEQXrkNnyDjWIQB1YY5NnWxmpfA4 SHVqXymWYpKnvGQl2bQrgqEt0Xo2mwNvBiW5pYKL9YbmMgMJm0Ty1+361uE52Ky6CdCQ qopA== 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:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=9sqOUAGiVOnFjS5BZaD6z7yrkdm5Xh2Uz6flYzo7cmg=; fh=fa0YJSH7y4k0snNuxsKgOI6vGP7j2f0jR9F1WHC95UA=; b=l8b3aNLoBlNtpuj+q+OdfzyxFA+6eKNhyRqrHGq1Wu7+jqUV3D6aRAvohKXqV9l0LJ UqFTE/+JsmV47AJSWmjkYbTeRS+6L0TIpQas2RXkYDIbBv1bdILoyRTe0rjKyrt9cJZf DXJR453lKQ0Jq75cpUXFoPsJOLT6pw5O48zOpNKfxWcz8AUqJz0nml+otLA1RK4ihzEv OdYbCICerEV4lPfyny6duch+qlfFnQoLXpZm1LfQjAKk9YNZCw5iRRaRksNUgb0SfJFY RzLU/MQnkFpxupK/4HsxoKuCuTl0FY8xMmhti2O9dHOoLcP9hrDt7xx8iXx3JNT5V2Yi uLbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=T3bB5HpF; 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 lf17-20020a170906ae5100b009a18a87b00fsi3207778ejb.46.2023.08.21.18.23.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 18:23:09 -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=T3bB5HpF; 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 CB29C3882105 for ; Tue, 22 Aug 2023 01:22:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CB29C3882105 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692667352; bh=9sqOUAGiVOnFjS5BZaD6z7yrkdm5Xh2Uz6flYzo7cmg=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=T3bB5HpFqcW1uSCANi/zWMQ8JvN9qJd7l5e/9W4DNy8EeQZ6+Feesj+oEZVwFdsse Il6jFPz0qIR+3PH4sVwfVWK4iagCQv3tuSMdG9gaXoirkmXnpAvaIi5pTFEEqSckfB fjjF4d+TlAlsrZmHepVbpf+xlWwP3ndsenGjUyDM= 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 0FD473858C2F for ; Tue, 22 Aug 2023 01:21:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0FD473858C2F Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-36-SsB60AjgMwKU8VfwHAi5mg-1; Mon, 21 Aug 2023 21:21:31 -0400 X-MC-Unique: SsB60AjgMwKU8VfwHAi5mg-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 09DFE101A528 for ; Tue, 22 Aug 2023 01:21:31 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.16.93]) by smtp.corp.redhat.com (Postfix) with ESMTP id B053740D2843; Tue, 22 Aug 2023 01:21:30 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed 5/6] analyzer: add kf_fopen Date: Mon, 21 Aug 2023 21:21:26 -0400 Message-Id: <20230822012127.2817996-5-dmalcolm@redhat.com> In-Reply-To: <20230822012127.2817996-1-dmalcolm@redhat.com> References: <20230822012127.2817996-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 X-Spam-Status: No, score=-11.4 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_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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774890400395178395 X-GMAIL-MSGID: 1774890400395178395 Add checking to -fanalyzer that both params of calls to "fopen" are valid null-terminated strings. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-3375-g4325c82736d9e8. gcc/analyzer/ChangeLog: * kf.cc (class kf_fopen): New. (register_known_functions): Register it. gcc/testsuite/ChangeLog: * gcc.dg/analyzer/fopen-1.c: New test. --- gcc/analyzer/kf.cc | 28 +++++++++++ gcc/testsuite/gcc.dg/analyzer/fopen-1.c | 66 +++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/fopen-1.c diff --git a/gcc/analyzer/kf.cc b/gcc/analyzer/kf.cc index 6b2db8613768..1601cf15c685 100644 --- a/gcc/analyzer/kf.cc +++ b/gcc/analyzer/kf.cc @@ -420,6 +420,33 @@ kf_error::impl_call_pre (const call_details &cd) const model->check_for_null_terminated_string_arg (cd, fmt_arg_idx); } +/* Handler for fopen. + FILE *fopen (const char *filename, const char *mode); + See e.g. https://en.cppreference.com/w/c/io/fopen + https://www.man7.org/linux/man-pages/man3/fopen.3.html + https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170 */ + +class kf_fopen : public known_function +{ +public: + bool matches_call_types_p (const call_details &cd) const final override + { + return (cd.num_args () == 2 + && cd.arg_is_pointer_p (0) + && cd.arg_is_pointer_p (1)); + } + + void impl_call_pre (const call_details &cd) const final override + { + cd.check_for_null_terminated_string_arg (0); + cd.check_for_null_terminated_string_arg (1); + cd.set_any_lhs_with_defaults (); + + /* fopen's mode param is effectively a mini-DSL, but there are various + non-standard extensions, so we don't bother to check it. */ + } +}; + /* Handler for "free", after sm-handling. If the ptr points to an underlying heap region, delete the region, @@ -1422,6 +1449,7 @@ register_known_functions (known_function_manager &kfm) /* Known POSIX functions, and some non-standard extensions. */ { + kfm.add ("fopen", make_unique ()); kfm.add ("putenv", make_unique ()); register_known_fd_functions (kfm); diff --git a/gcc/testsuite/gcc.dg/analyzer/fopen-1.c b/gcc/testsuite/gcc.dg/analyzer/fopen-1.c new file mode 100644 index 000000000000..e5b00e93b6da --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/fopen-1.c @@ -0,0 +1,66 @@ +typedef struct FILE FILE; +FILE *fopen (const char *pathname, const char *mode); +#define NULL ((void *)0) + +FILE * +test_passthrough (const char *pathname, const char *mode) +{ + return fopen (pathname, mode); +} + +FILE * +test_null_pathname (const char *pathname, const char *mode) +{ + return fopen (NULL, mode); +} + +FILE * +test_null_mode (const char *pathname) +{ + return fopen (pathname, NULL); +} + +FILE * +test_simple_r (void) +{ + return fopen ("foo.txt", "r"); +} + +FILE * +test_swapped_args (void) +{ + return fopen ("r", "foo.txt"); /* TODO: would be nice to detect this. */ +} + +FILE * +test_unterminated_pathname (const char *mode) +{ + char buf[3] = "abc"; + return fopen (buf, mode); /* { dg-warning "stack-based buffer over-read" } */ + /* { dg-message "while looking for null terminator for argument 1 \\('&buf'\\) of 'fopen'..." "event" { target *-*-* } .-1 } */ +} + +FILE * +test_unterminated_mode (const char *filename) +{ + char buf[3] = "abc"; + return fopen (filename, buf); /* { dg-warning "stack-based buffer over-read" } */ + /* { dg-message "while looking for null terminator for argument 2 \\('&buf'\\) of 'fopen'..." "event" { target *-*-* } .-1 } */ +} + +FILE * +test_uninitialized_pathname (const char *mode) +{ + char buf[10]; + return fopen (buf, mode); /* { dg-warning "use of uninitialized value 'buf\\\[0\\\]'" } */ + /* { dg-message "while looking for null terminator for argument 1 \\('&buf'\\) of 'fopen'..." "event" { target *-*-* } .-1 } */ +} + +FILE * +test_uninitialized_mode (const char *filename) +{ + char buf[10]; + return fopen (filename, buf); /* { dg-warning "use of uninitialized value 'buf\\\[0\\\]'" } */ + /* { dg-message "while looking for null terminator for argument 2 \\('&buf'\\) of 'fopen'..." "event" { target *-*-* } .-1 } */ +} +