public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/106284] False positives from -Wanalyzer-tainted-array-index with optimized conditionals
Date: Fri, 15 Jul 2022 15:30:17 +0000	[thread overview]
Message-ID: <bug-106284-4-gqttLCITQH@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-106284-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106284

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by David Malcolm <dmalcolm@gcc.gnu.org>:

https://gcc.gnu.org/g:0a8edfbd37d399d1103d86e134ba0a92f8c873c3

commit r13-1713-g0a8edfbd37d399d1103d86e134ba0a92f8c873c3
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Fri Jul 15 11:28:34 2022 -0400

    analyzer: fix taint false positive on optimized range checks [PR106284]

    PR analyzer/106284 reports a false positive from
    -Wanalyzer-tainted-array-index seen on the Linux kernel
    with a version of my patches from:
      https://gcc.gnu.org/pipermail/gcc-patches/2021-November/584372.html
    in drivers/usb/class/usblp.c in function âusblp_set_protocolâ handling
    usblp_ioctl on IOCNR_SET_PROTOCOL, which has:

      | 1337 |         if (protocol < USBLP_FIRST_PROTOCOL || protocol >
USBLP_LAST_PROTOCOL)
      |      |            ~
      |      |            |
      |      |            (15) following âfalseâ branch...
      |......
      | 1341 |         if (usblp->intf->num_altsetting > 1) {
      |      |            ~~~~~~~~~~~~
      |      |            |     |
      |      |            |     (16) ...to here
      |      |            (17) following âtrueâ branch...
      | 1342 |                 alts = usblp->protocol[protocol].alt_setting;
      |      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |      |                      |
      |      |                      (18) ...to here
      |      |                      (19) use of attacker-controlled value
âargâ in array lookup without bounds checking

    where "arg" is "protocol" (albeit from the caller frame, the ioctl
    callback), and is clearly checked at (15).

    The root cause is that at -O1 and above fold-const's build_range-check
    can optimize range checks
      (c>=low) && (c<=high)
    into
      (c-low>=0) && (c-low<=high-low)
    and thus into a single check:
      (unsigned)(c - low) <= (unsigned)(high-low).

    I initially attempted to fix this by detecting such conditions in
    region_model::on_condition, and calling on_condition for both of the
    implied conditions.  This turned out not to work since the current
    sm_context framework doesn't support applying two conditions
    simultaneously: it led to a transition from the old state to has_lb,
    then a transition from the old state *again* to has_ub, thus leaving
    the new state as has_ub, rather than the stop state.

    Instead, this patch fixes things by special-casing it within
    taint_state_machine::on_condition.

    gcc/analyzer/ChangeLog:
            PR analyzer/106284
            * sm-taint.cc (taint_state_machine::on_condition): Handle range
            checks optimized by build_range_check.

    gcc/testsuite/ChangeLog:
            PR analyzer/106284
            * gcc.dg/analyzer/torture/taint-read-index-2.c: New test.

    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

  parent reply	other threads:[~2022-07-15 15:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-13 20:08 [Bug analyzer/106284] New: " dmalcolm at gcc dot gnu.org
2022-07-14 21:09 ` [Bug analyzer/106284] " dmalcolm at gcc dot gnu.org
2022-07-15 15:30 ` cvs-commit at gcc dot gnu.org [this message]
2022-07-15 15:41 ` dmalcolm at gcc dot gnu.org
2022-07-19 13:56 ` cvs-commit at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-106284-4-gqttLCITQH@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).