11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2017 Oracle.
31f5207b7SJohn Levon  *
41f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon  * of the License, or (at your option) any later version.
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121f5207b7SJohn Levon  * GNU General Public License for more details.
131f5207b7SJohn Levon  *
141f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon  */
171f5207b7SJohn Levon 
181f5207b7SJohn Levon /*
191f5207b7SJohn Levon  * This is to help create Trinity fuzzer templates.
201f5207b7SJohn Levon  *
211f5207b7SJohn Levon  */
221f5207b7SJohn Levon 
231f5207b7SJohn Levon #include "smatch.h"
241f5207b7SJohn Levon #include "smatch_slist.h"
251f5207b7SJohn Levon 
261f5207b7SJohn Levon static int my_id;
271f5207b7SJohn Levon 
281f5207b7SJohn Levon STATE(ARG_FD);
291f5207b7SJohn Levon #if 0
301f5207b7SJohn Levon STATE(arg_range);
311f5207b7SJohn Levon STATE(arg_op);
321f5207b7SJohn Levon STATE(arg_list);
331f5207b7SJohn Levon STATE(arg_cpu);
341f5207b7SJohn Levon STATE(arg_pathname);
351f5207b7SJohn Levon #endif
361f5207b7SJohn Levon // nr_segs * sizeof(struct iovec)
371f5207b7SJohn Levon // if (nr_segs > UIO_MAXIOV)
381f5207b7SJohn Levon #if 0
391f5207b7SJohn Levon STATE(arg_ioveclen);
401f5207b7SJohn Levon STATE(arg_sockaddrlen);
411f5207b7SJohn Levon STATE(arg_socketinfo);
421f5207b7SJohn Levon #endif
431f5207b7SJohn Levon 
merge_states(struct smatch_state * s1,struct smatch_state * s2)441f5207b7SJohn Levon struct smatch_state *merge_states(struct smatch_state *s1, struct smatch_state *s2)
451f5207b7SJohn Levon {
461f5207b7SJohn Levon 	if (s1 == &undefined)
471f5207b7SJohn Levon 		return s2;
481f5207b7SJohn Levon 	return s1;
491f5207b7SJohn Levon }
501f5207b7SJohn Levon 
511f5207b7SJohn Levon struct typedef_lookup {
521f5207b7SJohn Levon 	const char *name;
531f5207b7SJohn Levon 	struct symbol *sym;
541f5207b7SJohn Levon 	int failed;
551f5207b7SJohn Levon };
561f5207b7SJohn Levon 
_typedef_lookup(const char * name)571f5207b7SJohn Levon static struct symbol *_typedef_lookup(const char *name)
581f5207b7SJohn Levon {
591f5207b7SJohn Levon 	struct ident *id;
601f5207b7SJohn Levon 	struct symbol *node;
611f5207b7SJohn Levon 
621f5207b7SJohn Levon 	id = built_in_ident(name);
631f5207b7SJohn Levon 	if (!id)
641f5207b7SJohn Levon 		return NULL;
651f5207b7SJohn Levon 	node = lookup_symbol(id, NS_TYPEDEF);
661f5207b7SJohn Levon 	if (!node || node->type != SYM_NODE)
671f5207b7SJohn Levon 		return NULL;
681f5207b7SJohn Levon 	return get_real_base_type(node);
691f5207b7SJohn Levon }
701f5207b7SJohn Levon 
typedef_lookup(struct typedef_lookup * tl)711f5207b7SJohn Levon static void typedef_lookup(struct typedef_lookup *tl)
721f5207b7SJohn Levon {
731f5207b7SJohn Levon 	if (tl->sym || tl->failed)
741f5207b7SJohn Levon 		return;
751f5207b7SJohn Levon 	tl->sym = _typedef_lookup(tl->name);
761f5207b7SJohn Levon 	if (!tl->sym)
771f5207b7SJohn Levon 		tl->failed = 1;
781f5207b7SJohn Levon }
791f5207b7SJohn Levon 
is_mode_t(struct symbol * sym)801f5207b7SJohn Levon static int is_mode_t(struct symbol *sym)
811f5207b7SJohn Levon {
821f5207b7SJohn Levon 	static struct typedef_lookup umode_t = { .name = "umode_t" };
831f5207b7SJohn Levon 	struct symbol *type;
841f5207b7SJohn Levon 
851f5207b7SJohn Levon 	typedef_lookup(&umode_t);
861f5207b7SJohn Levon 	if (!umode_t.sym)
871f5207b7SJohn Levon 		return 0;
881f5207b7SJohn Levon 	type = get_base_type(sym);
891f5207b7SJohn Levon 	if (type == umode_t.sym)
901f5207b7SJohn Levon 		return 1;
911f5207b7SJohn Levon 	return 0;
921f5207b7SJohn Levon }
931f5207b7SJohn Levon 
is_pid_t(struct symbol * sym)941f5207b7SJohn Levon static int is_pid_t(struct symbol *sym)
951f5207b7SJohn Levon {
961f5207b7SJohn Levon 	static struct typedef_lookup pid_t = { .name = "pid_t" };
971f5207b7SJohn Levon 	struct symbol *type;
981f5207b7SJohn Levon 
991f5207b7SJohn Levon 	typedef_lookup(&pid_t);
1001f5207b7SJohn Levon 	if (!pid_t.sym)
1011f5207b7SJohn Levon 		return 0;
1021f5207b7SJohn Levon 	type = get_base_type(sym);
1031f5207b7SJohn Levon 	if (type == pid_t.sym)
1041f5207b7SJohn Levon 		return 1;
1051f5207b7SJohn Levon 	return 0;
1061f5207b7SJohn Levon }
1071f5207b7SJohn Levon 
get_arg_type_from_type(struct symbol * sym)1081f5207b7SJohn Levon static const char *get_arg_type_from_type(struct symbol *sym)
1091f5207b7SJohn Levon {
1101f5207b7SJohn Levon 	struct symbol *type;
1111f5207b7SJohn Levon 
1121f5207b7SJohn Levon 	if (is_mode_t(sym))
1131f5207b7SJohn Levon 		return "ARG_MODE_T";
1141f5207b7SJohn Levon 	if (is_pid_t(sym))
1151f5207b7SJohn Levon 		return "ARG_PID";
1161f5207b7SJohn Levon 
1171f5207b7SJohn Levon 	type = get_real_base_type(sym);
1181f5207b7SJohn Levon 	if (!type || type->type != SYM_PTR)
1191f5207b7SJohn Levon 		return NULL;
1201f5207b7SJohn Levon 	type = get_real_base_type(type);
1211f5207b7SJohn Levon 	if (!type)
1221f5207b7SJohn Levon 		return NULL;
1231f5207b7SJohn Levon 	if (type == &char_ctype)
1241f5207b7SJohn Levon 		return "ARG_MMAP";
1251f5207b7SJohn Levon 	if (!type->ident)
1261f5207b7SJohn Levon 		return NULL;
1271f5207b7SJohn Levon 	if (strcmp(type->ident->name, "iovec") == 0)
1281f5207b7SJohn Levon 		return "ARG_IOVEC";
1291f5207b7SJohn Levon 	if (strcmp(type->ident->name, "sockaddr") == 0)
1301f5207b7SJohn Levon 		return "ARG_SOCKADDR";
1311f5207b7SJohn Levon 	return "ARG_ADDRESS";
1321f5207b7SJohn Levon }
1331f5207b7SJohn Levon 
match_fdget(const char * fn,struct expression * expr,void * unused)1341f5207b7SJohn Levon static void match_fdget(const char *fn, struct expression *expr, void *unused)
1351f5207b7SJohn Levon {
1361f5207b7SJohn Levon 	struct expression *arg;
1371f5207b7SJohn Levon 
1381f5207b7SJohn Levon 	arg = get_argument_from_call_expr(expr->args, 0);
1391f5207b7SJohn Levon 	set_state_expr(my_id, arg, &ARG_FD);
1401f5207b7SJohn Levon }
1411f5207b7SJohn Levon 
get_syscall_arg_type(struct symbol * sym)1421f5207b7SJohn Levon const char *get_syscall_arg_type(struct symbol *sym)
1431f5207b7SJohn Levon {
1441f5207b7SJohn Levon 	struct smatch_state *state;
1451f5207b7SJohn Levon 	const char *type;
1461f5207b7SJohn Levon 
1471f5207b7SJohn Levon 	if (!sym || !sym->ident)
1481f5207b7SJohn Levon 		return "ARG_UNDEFINED";
1491f5207b7SJohn Levon 	type = get_arg_type_from_type(sym);
1501f5207b7SJohn Levon 	if (type)
1511f5207b7SJohn Levon 		return type;
1521f5207b7SJohn Levon 	state = get_state(my_id, sym->ident->name, sym);
1531f5207b7SJohn Levon 	if (!state)
1541f5207b7SJohn Levon 		return "ARG_UNDEFINED";
1551f5207b7SJohn Levon 	return state->name;
1561f5207b7SJohn Levon }
1571f5207b7SJohn Levon 
check_syscall_arg_type(int id)1581f5207b7SJohn Levon void check_syscall_arg_type(int id)
1591f5207b7SJohn Levon {
1601f5207b7SJohn Levon 	my_id = id;
1611f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
1621f5207b7SJohn Levon 		return;
1631f5207b7SJohn Levon 
164*efe51d0cSJohn Levon 	set_dynamic_states(my_id);
1651f5207b7SJohn Levon 	add_merge_hook(my_id, &merge_states);
1661f5207b7SJohn Levon 	add_function_hook("fdget", &match_fdget, NULL);
1671f5207b7SJohn Levon }
1681f5207b7SJohn Levon 
1691f5207b7SJohn Levon 
170