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.133.124]) by sourceware.org (Postfix) with ESMTPS id 89F353858407 for ; Wed, 20 Jul 2022 21:29:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 89F353858407 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-414-zCIqj_c-NvuCx1SiDAUc7A-1; Wed, 20 Jul 2022 17:29:31 -0400 X-MC-Unique: zCIqj_c-NvuCx1SiDAUc7A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 12E72101A586 for ; Wed, 20 Jul 2022 21:29:31 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.16.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id E233690A00; Wed, 20 Jul 2022 21:29:30 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Subject: [committed] analyzer: update "tainted" state of RHS in comparisons [PR106373] Date: Wed, 20 Jul 2022 17:29:29 -0400 Message-Id: <20220720212929.1490197-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.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, 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-List-Received-Date: Wed, 20 Jul 2022 21:29:34 -0000 Doing so fixes various false positives from -Wanalyzer-tainted-array-index at -O1 and above (e.g. seen on the Linux kernel) Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-1768-g5e830693dd3356. gcc/analyzer/ChangeLog: PR analyzer/106373 * sm-taint.cc (taint_state_machine::on_condition): Potentially update the state of the RHS as well as the LHS. gcc/testsuite/ChangeLog: PR analyzer/106373 * gcc.dg/analyzer/torture/taint-read-index-3.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/sm-taint.cc | 18 +++++-- .../analyzer/torture/taint-read-index-3.c | 52 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/torture/taint-read-index-3.c diff --git a/gcc/analyzer/sm-taint.cc b/gcc/analyzer/sm-taint.cc index 9cb78886c9f..0486c01aaca 100644 --- a/gcc/analyzer/sm-taint.cc +++ b/gcc/analyzer/sm-taint.cc @@ -830,13 +830,11 @@ taint_state_machine::on_condition (sm_context *sm_ctxt, const gimple *stmt, const svalue *lhs, enum tree_code op, - const svalue *rhs ATTRIBUTE_UNUSED) const + const svalue *rhs) const { if (stmt == NULL) return; - // TODO: this doesn't use the RHS; should we make it symmetric? - // TODO switch (op) { @@ -845,10 +843,17 @@ taint_state_machine::on_condition (sm_context *sm_ctxt, case GE_EXPR: case GT_EXPR: { + /* (LHS >= RHS) or (LHS > RHS) + LHS gains a lower bound + RHS gains an upper bound. */ sm_ctxt->on_transition (node, stmt, lhs, m_tainted, m_has_lb); sm_ctxt->on_transition (node, stmt, lhs, m_has_ub, m_stop); + sm_ctxt->on_transition (node, stmt, rhs, m_tainted, + m_has_ub); + sm_ctxt->on_transition (node, stmt, rhs, m_has_lb, + m_stop); } break; case LE_EXPR: @@ -896,10 +901,17 @@ taint_state_machine::on_condition (sm_context *sm_ctxt, } } + /* (LHS <= RHS) or (LHS < RHS) + LHS gains an upper bound + RHS gains a lower bound. */ sm_ctxt->on_transition (node, stmt, lhs, m_tainted, m_has_ub); sm_ctxt->on_transition (node, stmt, lhs, m_has_lb, m_stop); + sm_ctxt->on_transition (node, stmt, rhs, m_tainted, + m_has_lb); + sm_ctxt->on_transition (node, stmt, rhs, m_has_ub, + m_stop); } break; default: diff --git a/gcc/testsuite/gcc.dg/analyzer/torture/taint-read-index-3.c b/gcc/testsuite/gcc.dg/analyzer/torture/taint-read-index-3.c new file mode 100644 index 00000000000..8eb6061a08b --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/torture/taint-read-index-3.c @@ -0,0 +1,52 @@ +// TODO: remove need for the taint option: +/* { dg-additional-options "-fanalyzer-checker=taint" } */ +/* { dg-skip-if "" { *-*-* } { "-fno-fat-lto-objects" } { "" } } */ + +struct raw_ep { + /* ...snip... */ + int state; + /* ...snip... */ +}; + +struct raw_dev { + /* ...snip... */ + struct raw_ep eps[30]; + int eps_num; + /* ...snip... */ +}; + +int __attribute__((tainted_args)) +simplified_raw_ioctl_ep_disable(struct raw_dev *dev, unsigned long value) +{ + int ret = 0, i = value; + + if (i < 0 || i >= dev->eps_num) { + ret = -16; + goto out_unlock; + } + if (dev->eps[i].state == 0) { /* { dg-bogus "attacker-controlled" } */ + ret = -22; + goto out_unlock; + } + +out_unlock: + return ret; +} + +int __attribute__((tainted_args)) +test_2(struct raw_dev *dev, int i) +{ + int ret = 0; + + if (i < 0 || i >= dev->eps_num) { + ret = -16; + goto out_unlock; + } + if (dev->eps[i].state == 0) { /* { dg-bogus "attacker-controlled" } */ + ret = -22; + goto out_unlock; + } + +out_unlock: + return ret; +} -- 2.26.3