Bug 106373 - False positives from -Wanalyzer-tainted-array-index on comparison with non-const
Summary: False positives from -Wanalyzer-tainted-array-index on comparison with non-const
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: analyzer (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords:
Depends on:
Blocks: analyzer-linux
  Show dependency treegraph
 
Reported: 2022-07-20 18:09 UTC by David Malcolm
Modified: 2022-07-20 21:32 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-07-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Malcolm 2022-07-20 18:09:20 UTC
See: https://godbolt.org/z/P5nGMohxd

Am seeing false positive with -O1 -fanalyzer -fanalyzer-checker=taint on this code:

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) {
    ret = -22;
    goto out_unlock;
  }

out_unlock:
  return ret;
}


../../src/raw_gadget.c: In function ‘simplified_raw_ioctl_ep_disable’:
../../src/raw_gadget.c:23:18: warning: use of attacker-controlled value ‘value’ in array lookup without upper-bounds checking [CWE-129] [-Wanalyzer-tainted-array-index]
   23 |   if (dev->eps[i].state == 0) {
      |       ~~~~~~~~~~~^~~~~~
  ‘simplified_raw_ioctl_ep_disable’: event 1
    |
    |   15 | simplified_raw_ioctl_ep_disable(struct raw_dev *dev, unsigned long value)
    |      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      | |
    |      | (1) function ‘simplified_raw_ioctl_ep_disable’ marked with ‘__attribute__((tainted_args))’
    |
    +--> ‘simplified_raw_ioctl_ep_disable’: events 2-5
           |
           |   15 | simplified_raw_ioctl_ep_disable(struct raw_dev *dev, unsigned long value)
           |      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      | |
           |      | (2) entry to ‘simplified_raw_ioctl_ep_disable’
           |......
           |   19 |   if (i < 0 || i >= dev->eps_num) {
           |      |      ~
           |      |      |
           |      |      (3) following ‘false’ branch...
           |......
           |   23 |   if (dev->eps[i].state == 0) {
           |      |       ~~~~~~~~~~~~~~~~~
           |      |                  |
           |      |                  (4) ...to here
           |      |                  (5) use of attacker-controlled value ‘value’ in array lookup without upper-bounds checking
           |

Seems to need at least -O1.

Reduced from false positives seen in various ioctl handlers in Linux kernel in drivers/usb/gadget/legacy/raw_gadget.c (with "allyesconfig").
Comment 1 David Malcolm 2022-07-20 19:06:51 UTC
The issue isn't the casts; it's the comparison against non-const, which looks like:

  _1 = dev_6(D)->eps_num;
  if (_1 <= i_4(D))

at the gimple level, and where i_4(D) is on the RHS, and sm-taint.cc is only looking at comparisons of the LHS.

I'm testing a fix.
Comment 2 GCC Commits 2022-07-20 21:28:14 UTC
The master branch has been updated by David Malcolm <dmalcolm@gcc.gnu.org>:

https://gcc.gnu.org/g:5e830693dd335621940368b6d39b23afc2c98545

commit r13-1768-g5e830693dd335621940368b6d39b23afc2c98545
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Wed Jul 20 17:25:35 2022 -0400

    analyzer: update "tainted" state of RHS in comparisons [PR106373]
    
    Doing so fixes various false positives from
    -Wanalyzer-tainted-array-index at -O1 and above (e.g. seen on the
    Linux kernel)
    
    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 <dmalcolm@redhat.com>
Comment 3 David Malcolm 2022-07-20 21:32:02 UTC
Should be fixed on trunk by the above patch.