From patchwork Wed Jan 24 15:16:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 191614 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp1056517dyi; Wed, 24 Jan 2024 07:17:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IG+ttuADKh8NeMv9Sw5fKYv+V7LhbrQhSKso9e4fcqJNDeINKtJrrgtnjg8hfP1qv1xqctQ X-Received: by 2002:ac8:5a88:0:b0:42a:384a:78a6 with SMTP id c8-20020ac85a88000000b0042a384a78a6mr3196809qtc.30.1706109425676; Wed, 24 Jan 2024 07:17:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706109425; cv=pass; d=google.com; s=arc-20160816; b=EIkSJeQLMXQd2Hftxe5IgnPXcgQarny4uckTfG98MNUi+C99RYvk3qY9kpWN2jDRck EF/cLHN+XRSHcH8jf7flcHAP0MSaPswERUZD+KtcFNDVDwy3Blc8DetjT+sVgxjt6yXU 2YsSfJRbSMNyTxGmp6pUpCxq2/ES3nHgnMZd2LsFO9KnIMuqVsHXnW3zEKuiDpthDbFs 6BvXhhA/6RVMOdkn0z52Lp6ULlQAYAO9GG+Yowtc+xPld7qDZgSJfP4/VQX2iPkRGaWP dsPV8WNpGHQMn7GMtv1rxupnh0ikkyA0+F+bETpkObOx5qDjyL3bPOuzRu6tGC1ocoiG MMlQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=btEoVHPifAWuYrpORjLpqyAqg1wb/SXU3N2x6v6RXm8=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=ip4UqUGegD9sSIToLqIjxKQhkE6okXOnAPSep/qiQqVjVGCIe8U3+Xteoz1skIv7G2 ligWXyfNoRFSo92+YwEGBJBD5MD92oQ9sVNIWwe1GLTUSanQ11VIGVGd9zl8i8e198Hx 9vBAbMh4w0AK01E9uvu59cc9BHcvTTIh5EsCDCnl/xQjFnTlla/LLUAO/6sR/JRljihf FcP7sH0TEEePtOCd55efoC5x78ythd9biII0bkG2U/PbgsCrIkhaEy85RlM+QGISJ5Hr bGoZx5244r56/t9b4CNhROxhqjK/mAiNYDK6je7wtbDOg9NC6m4y7IMtbQ6AQUoGNHAl xG8g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eJMfzNJG; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id h7-20020a05622a170700b0042a5f965469si1208846qtk.143.2024.01.24.07.17.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 07:17:05 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=eJMfzNJG; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 792CE38582A7 for ; Wed, 24 Jan 2024 15:17:04 +0000 (GMT) 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 B4843385800C for ; Wed, 24 Jan 2024 15:16:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B4843385800C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B4843385800C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706109383; cv=none; b=Hw2f8xHAbn7jrN0kwauEKU6FOPpi9+zs4KoL5cV0ELaOe5SZ9og4iFzA4jz18Ar0IKOU2w7fSq9kAGWchaVUZsaJxA2tIN55NyX/jzgASqByND6zlFLJ2KiLy/KJ3JQ7jXyG73179VR+6SuBsPrVPMdNGX000v802ffuC5pGBg8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706109383; c=relaxed/simple; bh=UPgDYDidNXzFo72qO4lVHIanEFqfLltzYMD7ErKjBh4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=BXYYdYpNv5Jl8NRDMhvZdg3aJWGLjYOao4xk08hmPvrp/4srp4o9LAbY3Eg8ZfZFCPRgDIZO91dQtDMM1xdb6dwF9SECdSQrpp1QAB1inIM+SNSDSQPsdjZg80RJCL6RfsMdRYQAmKOp9X820/OWfNii60AMo4wnPqFiZccZyoY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706109378; 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=btEoVHPifAWuYrpORjLpqyAqg1wb/SXU3N2x6v6RXm8=; b=eJMfzNJGGMVxle6sGe52UymCiR/4uroEMU3lo1HaIhp1O48oN16RwLSFqdqN2eAzg95Uav Q1JI9ArrCraIawhYRk46ngXeYDFrhQho9P3t4mWavIqZ0KaysMM7a8ut5Y24u2BE9GYRPM QZ7IM8MyAUMc1NFvkwMwGJib09koy2o= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-48-eiyrCR6VPSewhA6qV1Wp_A-1; Wed, 24 Jan 2024 10:16:16 -0500 X-MC-Unique: eiyrCR6VPSewhA6qV1Wp_A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A00BC1013661 for ; Wed, 24 Jan 2024 15:16:16 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.32.139]) by smtp.corp.redhat.com (Postfix) with ESMTP id 68E55C2E1E6; Wed, 24 Jan 2024 15:16:16 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: fix taint false +ve due to overzealous state purging [PR112977] Date: Wed, 24 Jan 2024 10:16:15 -0500 Message-Id: <20240124151615.1538286-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788985397077446273 X-GMAIL-MSGID: 1788985397077446273 Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r14-8391-ge503f9aca91926. gcc/analyzer/ChangeLog: PR analyzer/112977 * engine.cc (impl_region_model_context::on_liveness_change): Pass m_ext_state to sm_state_map::on_liveness_change. * program-state.cc (sm_state_map::on_svalue_leak): Guard removal of map entry based on can_purge_p. (sm_state_map::on_liveness_change): Add ext_state param. Add workaround for bad interaction between state purging and alt-inherited sm-state. * program-state.h (sm_state_map::on_liveness_change): Add ext_state param. * sm-taint.cc (taint_state_machine::has_alt_get_inherited_state_p): New. (taint_state_machine::can_purge_p): Return false for "has_lb" and "has_ub". * sm.h (state_machine::has_alt_get_inherited_state_p): New vfunc. gcc/testsuite/ChangeLog: PR analyzer/112977 * gcc.dg/plugin/plugin.exp: Add taint-pr112977.c. * gcc.dg/plugin/taint-pr112977.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/engine.cc | 2 +- gcc/analyzer/program-state.cc | 65 +++++++++++++++++++- gcc/analyzer/program-state.h | 1 + gcc/analyzer/sm-taint.cc | 9 +++ gcc/analyzer/sm.h | 6 ++ gcc/testsuite/gcc.dg/plugin/plugin.exp | 3 +- gcc/testsuite/gcc.dg/plugin/taint-pr112977.c | 44 +++++++++++++ 7 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/plugin/taint-pr112977.c diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc index fde8412bc15..44ff20cf9af 100644 --- a/gcc/analyzer/engine.cc +++ b/gcc/analyzer/engine.cc @@ -179,7 +179,7 @@ on_liveness_change (const svalue_set &live_svalues, const region_model *model) { for (sm_state_map *smap : m_new_state->m_checker_states) - smap->on_liveness_change (live_svalues, model, this); + smap->on_liveness_change (live_svalues, model, m_ext_state, this); } void diff --git a/gcc/analyzer/program-state.cc b/gcc/analyzer/program-state.cc index 888f2a9c40b..55dd6ca7166 100644 --- a/gcc/analyzer/program-state.cc +++ b/gcc/analyzer/program-state.cc @@ -560,9 +560,10 @@ sm_state_map::on_svalue_leak (const svalue *sval, { if (state_machine::state_t state = get_state (sval, ctxt->m_ext_state)) { - if (!m_sm.can_purge_p (state)) + if (m_sm.can_purge_p (state)) + m_map.remove (sval); + else ctxt->on_state_leak (m_sm, sval, state); - m_map.remove (sval); } } @@ -572,6 +573,7 @@ sm_state_map::on_svalue_leak (const svalue *sval, void sm_state_map::on_liveness_change (const svalue_set &live_svalues, const region_model *model, + const extrinsic_state &ext_state, impl_region_model_context *ctxt) { svalue_set svals_to_unset; @@ -605,9 +607,68 @@ sm_state_map::on_liveness_change (const svalue_set &live_svalues, ctxt->on_state_leak (m_sm, sval, e.m_state); } + sm_state_map old_sm_map = *this; + for (svalue_set::iterator iter = svals_to_unset.begin (); iter != svals_to_unset.end (); ++iter) m_map.remove (*iter); + + /* For state machines like "taint" where states can be + alt-inherited from other svalues, ensure that state purging doesn't + make us lose sm-state. + + Consider e.g.: + + make_tainted(foo); + if (foo.field > 128) + return; + arr[foo.field].f1 = v1; + + where the last line is: + + (A): _t1 = foo.field; + (B): _t2 = _t1 * sizeof(arr[0]); + (C): [arr + _t2].f1 = val; + + At (A), foo is 'tainted' and foo.field is 'has_ub'. + After (B), foo.field's value (in _t1) is no longer directly + within LIVE_SVALUES, so with state purging enabled, we would + erroneously purge the "has_ub" state from the svalue. + + Given that _t2's value's state comes from _t1's value's state, + we need to preserve that information. + + Hence for all svalues that have had their explicit sm-state unset, + having their sm-state being unset, determine if doing so has changed + their effective state, and if so, explicitly set their state. + + For example, in the above, unsetting the "has_ub" for _t1's value means + that _t2's effective value changes from "has_ub" (from alt-inherited + from _t1's value) to "tainted" (inherited from "foo"'s value). + + For such cases, preserve the effective state by explicitly setting the + new state. In the above example, this means explicitly setting _t2's + value to the value ("has_ub") it was previously alt-inheriting from _t1's + value. */ + if (m_sm.has_alt_get_inherited_state_p ()) + { + auto_vec svalues_needing_state; + for (auto unset_sval : svals_to_unset) + { + const state_machine::state_t old_state + = old_sm_map.get_state (unset_sval, ext_state); + const state_machine::state_t new_state + = get_state (unset_sval, ext_state); + if (new_state != old_state) + svalues_needing_state.safe_push (unset_sval); + } + for (auto sval : svalues_needing_state) + { + const state_machine::state_t old_state + = old_sm_map.get_state (sval, ext_state); + impl_set_state (sval, old_state, nullptr, ext_state); + } + } } /* Purge state from SVAL (in response to a call to an unknown function). */ diff --git a/gcc/analyzer/program-state.h b/gcc/analyzer/program-state.h index 50bf2ab277a..69bf931f070 100644 --- a/gcc/analyzer/program-state.h +++ b/gcc/analyzer/program-state.h @@ -155,6 +155,7 @@ public: impl_region_model_context *ctxt); void on_liveness_change (const svalue_set &live_svalues, const region_model *model, + const extrinsic_state &ext_state, impl_region_model_context *ctxt); void on_unknown_change (const svalue *sval, diff --git a/gcc/analyzer/sm-taint.cc b/gcc/analyzer/sm-taint.cc index dc4b078c411..bbf683f82ef 100644 --- a/gcc/analyzer/sm-taint.cc +++ b/gcc/analyzer/sm-taint.cc @@ -86,6 +86,12 @@ public: const extrinsic_state &ext_state) const final override; + bool + has_alt_get_inherited_state_p () const final override + { + return true; + } + bool on_stmt (sm_context *sm_ctxt, const supernode *node, const gimple *stmt) const final override; @@ -1199,6 +1205,9 @@ taint_state_machine::on_bounded_ranges (sm_context *sm_ctxt, bool taint_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const { + if (s == m_has_lb || s == m_has_ub) + return false; + return true; } diff --git a/gcc/analyzer/sm.h b/gcc/analyzer/sm.h index e7f634f4245..a58f5dbc395 100644 --- a/gcc/analyzer/sm.h +++ b/gcc/analyzer/sm.h @@ -78,6 +78,12 @@ public: return NULL; } + virtual bool + has_alt_get_inherited_state_p () const + { + return false; + } + virtual state_machine::state_t get_default_state (const svalue *) const { return m_start; diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp index a5a72daac1a..8141cc2aa46 100644 --- a/gcc/testsuite/gcc.dg/plugin/plugin.exp +++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp @@ -170,7 +170,8 @@ set plugin_test_list [list \ taint-pr112850-precise.c \ taint-pr112850-too-complex.c \ taint-pr112850-unsanitized.c \ - taint-pr112927.c } \ + taint-pr112927.c \ + taint-pr112977.c } \ { analyzer_cpython_plugin.c \ cpython-plugin-test-no-Python-h.c \ cpython-plugin-test-PyList_Append.c \ diff --git a/gcc/testsuite/gcc.dg/plugin/taint-pr112977.c b/gcc/testsuite/gcc.dg/plugin/taint-pr112977.c new file mode 100644 index 00000000000..a9beb00b88d --- /dev/null +++ b/gcc/testsuite/gcc.dg/plugin/taint-pr112977.c @@ -0,0 +1,44 @@ +/* Reduced from false positive in Linux kernel in + drivers/scsi/aacraid/aachba.c. */ + +/* { dg-do compile } */ +/* { dg-options "-fanalyzer" } */ +/* { dg-require-effective-target analyzer } */ + +typedef unsigned char u8; +typedef unsigned int u32; + +extern unsigned long +copy_from_user(void* to, const void* from, unsigned long n); + +struct fsa_dev_info +{ + u8 valid; + u8 deleted; +}; +struct aac_dev +{ + int maximum_num_containers; + struct fsa_dev_info* fsa_dev; +}; +struct aac_delete_disk +{ + u32 disknum; + u32 cnum; +}; +int +force_delete_disk(struct aac_dev* dev, void* arg) +{ + struct aac_delete_disk dd; + struct fsa_dev_info* fsa_dev_ptr; + fsa_dev_ptr = dev->fsa_dev; + if (!fsa_dev_ptr) + return -16; + if (copy_from_user(&dd, arg, sizeof(struct aac_delete_disk))) + return -14; + if (dd.cnum >= dev->maximum_num_containers) + return -22; + fsa_dev_ptr[dd.cnum].deleted = 1; + fsa_dev_ptr[dd.cnum].valid = 0; /* { dg-bogus "use of attacker-controlled value as offset without upper-bounds checking" } */ + return 0; +}