1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2010 Dan Carpenter.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include "smatch.h"
19*1f5207b7SJohn Levon #include "smatch_slist.h"
20*1f5207b7SJohn Levon 
21*1f5207b7SJohn Levon static int my_id;
22*1f5207b7SJohn Levon 
23*1f5207b7SJohn Levon STATE(alloced);
24*1f5207b7SJohn Levon STATE(string);
25*1f5207b7SJohn Levon 
my_get_variable(struct expression * expr,struct symbol ** sym)26*1f5207b7SJohn Levon static char *my_get_variable(struct expression *expr, struct symbol **sym)
27*1f5207b7SJohn Levon {
28*1f5207b7SJohn Levon 	char *name;
29*1f5207b7SJohn Levon 
30*1f5207b7SJohn Levon 	name = expr_to_var_sym(expr, sym);
31*1f5207b7SJohn Levon 	free_string(name);
32*1f5207b7SJohn Levon 	if (!name || !*sym)
33*1f5207b7SJohn Levon 		return NULL;
34*1f5207b7SJohn Levon 
35*1f5207b7SJohn Levon 	return (*sym)->ident->name;
36*1f5207b7SJohn Levon }
37*1f5207b7SJohn Levon 
match_kmalloc(const char * fn,struct expression * expr,void * unused)38*1f5207b7SJohn Levon static void match_kmalloc(const char *fn, struct expression *expr, void *unused)
39*1f5207b7SJohn Levon {
40*1f5207b7SJohn Levon 	char *name;
41*1f5207b7SJohn Levon 	struct symbol *sym;
42*1f5207b7SJohn Levon 
43*1f5207b7SJohn Levon 	name = my_get_variable(expr->left, &sym);
44*1f5207b7SJohn Levon 	if (!name)
45*1f5207b7SJohn Levon 		return;
46*1f5207b7SJohn Levon 	set_state(my_id, name, sym, &alloced);
47*1f5207b7SJohn Levon }
48*1f5207b7SJohn Levon 
match_strcpy(const char * fn,struct expression * expr,void * unused)49*1f5207b7SJohn Levon static void match_strcpy(const char *fn, struct expression *expr, void *unused)
50*1f5207b7SJohn Levon {
51*1f5207b7SJohn Levon 	struct expression *dest;
52*1f5207b7SJohn Levon 	char *name;
53*1f5207b7SJohn Levon 	struct symbol *sym;
54*1f5207b7SJohn Levon 
55*1f5207b7SJohn Levon 	dest = get_argument_from_call_expr(expr->args, 0);
56*1f5207b7SJohn Levon 	name = my_get_variable(dest, &sym);
57*1f5207b7SJohn Levon 	if (!name || !sym)
58*1f5207b7SJohn Levon 		return;
59*1f5207b7SJohn Levon 	if (!get_state(my_id, name, sym))
60*1f5207b7SJohn Levon 		return;
61*1f5207b7SJohn Levon 	set_state(my_id, name, sym, &string);
62*1f5207b7SJohn Levon }
63*1f5207b7SJohn Levon 
match_copy_to_user(const char * fn,struct expression * expr,void * unused)64*1f5207b7SJohn Levon static void match_copy_to_user(const char *fn, struct expression *expr, void *unused)
65*1f5207b7SJohn Levon {
66*1f5207b7SJohn Levon 	struct expression *src;
67*1f5207b7SJohn Levon 	char *name;
68*1f5207b7SJohn Levon 	struct symbol *sym;
69*1f5207b7SJohn Levon 	struct sm_state *sm;
70*1f5207b7SJohn Levon 
71*1f5207b7SJohn Levon 	src = get_argument_from_call_expr(expr->args, 1);
72*1f5207b7SJohn Levon 	name = my_get_variable(src, &sym);
73*1f5207b7SJohn Levon 	if (!name || !sym)
74*1f5207b7SJohn Levon 		return;
75*1f5207b7SJohn Levon 	sm = get_sm_state(my_id, name, sym);
76*1f5207b7SJohn Levon 	if (!sm || !slist_has_state(sm->possible, &string))
77*1f5207b7SJohn Levon 		return;
78*1f5207b7SJohn Levon 	name = expr_to_var(src);
79*1f5207b7SJohn Levon 	sm_warning("possible info leak '%s'", name);
80*1f5207b7SJohn Levon 	free_string(name);
81*1f5207b7SJohn Levon }
82*1f5207b7SJohn Levon 
check_info_leak(int id)83*1f5207b7SJohn Levon void check_info_leak(int id)
84*1f5207b7SJohn Levon {
85*1f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
86*1f5207b7SJohn Levon 		return;
87*1f5207b7SJohn Levon 	my_id = id;
88*1f5207b7SJohn Levon 	add_function_assign_hook("kmalloc", &match_kmalloc, NULL);
89*1f5207b7SJohn Levon 	add_function_hook("strcpy", &match_strcpy, NULL);
90*1f5207b7SJohn Levon 	add_function_hook("strlcpy", &match_strcpy, NULL);
91*1f5207b7SJohn Levon 	add_function_hook("strlcat", &match_strcpy, NULL);
92*1f5207b7SJohn Levon 	add_function_hook("strncpy", &match_strcpy, NULL);
93*1f5207b7SJohn Levon 	add_function_hook("copy_to_user", &match_copy_to_user, NULL);
94*1f5207b7SJohn Levon }