2 * Copyright (C) 2017 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
24 static const char *untrusted_fn_ptrs
[] = {
25 "(struct target_core_fabric_ops)->fabric_make_np",
26 "(struct target_core_fabric_ops)->fabric_make_tpg",
27 "(struct configfs_group_operations)->make_item",
28 "(cgroup_subsys_state)->css_alloc",
31 static bool from_untrusted_fn_ptr(struct expression
*expr
)
33 struct expression
*prev
;
38 prev
= get_assigned_expr(expr
);
39 if (!prev
|| prev
->type
!= EXPR_CALL
)
42 member_name
= get_member_name(prev
->fn
);
46 for (i
= 0; i
< ARRAY_SIZE(untrusted_fn_ptrs
); i
++) {
47 if (strcmp(member_name
, untrusted_fn_ptrs
[i
]) == 0) {
52 free_string(member_name
);
56 static void match_condition(struct expression
*expr
)
60 while (expr
->type
== EXPR_ASSIGNMENT
)
61 expr
= strip_expr(expr
->left
);
63 if (!is_pointer(expr
))
66 if (implied_not_equal(expr
, 0) &&
67 possible_err_ptr(expr
) &&
68 !from_untrusted_fn_ptr(expr
)) {
69 name
= expr_to_str(expr
);
70 sm_msg("warn: '%s' is an error pointer or valid", name
);
75 static void match_condition2(struct expression
*expr
)
77 struct range_list
*rl
;
78 struct data_range
*drange
;
81 if (!is_pointer(expr
))
83 if (!get_implied_rl(expr
, &rl
))
86 FOR_EACH_PTR(rl
, drange
) {
87 if (sval_cmp(drange
->min
, drange
->max
) != 0)
89 if (drange
->min
.value
>= -4095 && drange
->min
.value
< 0)
91 } END_FOR_EACH_PTR(drange
);
96 if (from_untrusted_fn_ptr(expr
))
99 name
= expr_to_str(expr
);
100 sm_warning("'%s' could be an error pointer", name
);
104 void check_checking_for_null_instead_of_err_ptr(int id
)
106 if (option_project
!= PROJ_KERNEL
)
110 add_hook(&match_condition
, CONDITION_HOOK
);
112 add_hook(&match_condition2
, CONDITION_HOOK
);