Skip to content

Commit 0a8edfb

Browse files
committed
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 <[email protected]>
1 parent b1d07b5 commit 0a8edfb

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

gcc/analyzer/sm-taint.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,48 @@ taint_state_machine::on_condition (sm_context *sm_ctxt,
848848
case LE_EXPR:
849849
case LT_EXPR:
850850
{
851+
/* Detect where build_range_check has optimized
852+
(c>=low) && (c<=high)
853+
into
854+
(c-low>=0) && (c-low<=high-low)
855+
and thus into:
856+
(unsigned)(c - low) <= (unsigned)(high-low). */
857+
if (const binop_svalue *binop_sval
858+
= lhs->dyn_cast_binop_svalue ())
859+
{
860+
const svalue *inner_lhs = binop_sval->get_arg0 ();
861+
enum tree_code inner_op = binop_sval->get_op ();
862+
const svalue *inner_rhs = binop_sval->get_arg1 ();
863+
if (const svalue *before_cast = inner_lhs->maybe_undo_cast ())
864+
inner_lhs = before_cast;
865+
if (tree outer_rhs_cst = rhs->maybe_get_constant ())
866+
if (tree inner_rhs_cst = inner_rhs->maybe_get_constant ())
867+
if (inner_op == PLUS_EXPR
868+
&& TREE_CODE (inner_rhs_cst) == INTEGER_CST
869+
&& TREE_CODE (outer_rhs_cst) == INTEGER_CST
870+
&& TYPE_UNSIGNED (TREE_TYPE (inner_rhs_cst))
871+
&& TYPE_UNSIGNED (TREE_TYPE (outer_rhs_cst)))
872+
{
873+
/* We have
874+
(unsigned)(INNER_LHS + CST_A) </<= UNSIGNED_CST_B
875+
and thus an optimized test of INNER_LHS (before any
876+
cast to unsigned) against a range.
877+
Transition any of the tainted states to the stop state.
878+
We have to special-case this here rather than in
879+
region_model::on_condition since we can't apply
880+
both conditions simultaneously (we'd have a transition
881+
from the old state to has_lb, then a transition from
882+
the old state *again* to has_ub). */
883+
state_t old_state
884+
= sm_ctxt->get_state (stmt, inner_lhs);
885+
if (old_state == m_tainted
886+
|| old_state == m_has_lb
887+
|| old_state == m_has_ub)
888+
sm_ctxt->set_next_state (stmt, inner_lhs, m_stop);
889+
return;
890+
}
891+
}
892+
851893
sm_ctxt->on_transition (node, stmt, lhs, m_tainted,
852894
m_has_ub);
853895
sm_ctxt->on_transition (node, stmt, lhs, m_has_lb,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// TODO: remove need for the taint option:
2+
/* { dg-additional-options "-fanalyzer-checker=taint" } */
3+
/* { dg-skip-if "" { *-*-* } { "-fno-fat-lto-objects" } { "" } } */
4+
5+
#define LOWER_LIMIT 5
6+
#define UPPER_LIMIT 20
7+
8+
static int arr[UPPER_LIMIT];
9+
10+
static int
11+
called_by_test_1 (int iarg)
12+
{
13+
return arr[iarg]; /* { dg-warning "without bounds checking" } */
14+
}
15+
16+
int __attribute__((tainted_args))
17+
test_1 (unsigned long ularg)
18+
{
19+
return called_by_test_1 (ularg);
20+
}
21+
22+
static int
23+
called_by_test_2 (int iarg)
24+
{
25+
if (iarg < LOWER_LIMIT || iarg > UPPER_LIMIT)
26+
return 0;
27+
return arr[iarg]; /* { dg-bogus "bounds checking" } */
28+
}
29+
30+
int __attribute__((tainted_args))
31+
test_2 (unsigned long ularg)
32+
{
33+
return called_by_test_2 (ularg);
34+
}
35+
36+
int __attribute__((tainted_args))
37+
test_3 (int iarg)
38+
{
39+
if (iarg < LOWER_LIMIT || iarg > UPPER_LIMIT)
40+
return 0;
41+
return arr[iarg]; /* { dg-bogus "bounds checking" } */
42+
}
43+
44+
static int
45+
called_by_test_4 (int iarg)
46+
{
47+
if (iarg < LOWER_LIMIT || iarg > UPPER_LIMIT)
48+
return 0;
49+
return arr[iarg]; /* { dg-bogus "bounds checking" } */
50+
}
51+
52+
int __attribute__((tainted_args))
53+
test_4 (unsigned uarg)
54+
{
55+
return called_by_test_4 (uarg);
56+
}

0 commit comments

Comments
 (0)