-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_access_ok_math.c
104 lines (85 loc) · 2.41 KB
/
check_access_ok_math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (C) 2010 Dan Carpenter.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
static int my_id;
static int can_overflow(struct expression *expr)
{
sval_t max;
int uncapped = 0;
expr = strip_expr(expr);
if (expr->type == EXPR_BINOP) {
uncapped += can_overflow(expr->left);
uncapped += can_overflow(expr->right);
if (uncapped &&
(expr->op == '+' || expr->op == '*' || expr->op == SPECIAL_LEFTSHIFT))
return 1;
return 0;
}
if (get_implied_max(expr, &max))
return 0;
if (get_absolute_max(expr, &max) && sval_cmp_val(max, 4096) <= 0)
return 0;
return 1;
}
static void match_size(struct expression *size_expr)
{
char *name;
size_expr = strip_expr(size_expr);
if (!size_expr)
return;
if (size_expr->type != EXPR_BINOP) {
size_expr = get_assigned_expr(size_expr);
if (!size_expr || size_expr->type != EXPR_BINOP)
return;
}
if (!can_overflow(size_expr))
return;
name = expr_to_str(size_expr);
sm_warning("math in access_ok() is dangerous '%s'", name);
free_string(name);
}
static void match_access_ok(const char *fn, struct expression *expr, void *data)
{
struct expression *size_expr;
size_expr = get_argument_from_call_expr(expr->args, 1);
match_size(size_expr);
}
static void split_asm_constraints(struct statement *stmt)
{
struct asm_operand *op;
FOR_EACH_PTR(stmt->asm_inputs, op) {
match_size(op->constraint);
} END_FOR_EACH_PTR(op);
}
static void match_asm_stmt(struct statement *stmt)
{
char *name;
name = get_macro_name(stmt->pos);
if (!name || strcmp(name, "access_ok") != 0)
return;
split_asm_constraints(stmt);
}
void check_access_ok_math(int id)
{
my_id = id;
if (option_project != PROJ_KERNEL)
return;
if (!option_spammy)
return;
add_function_hook("__access_ok", &match_access_ok, NULL);
add_hook(&match_asm_stmt, ASM_HOOK);
}