summaryrefslogtreecommitdiffstats
path: root/src/audit.c
blob: 2313c7c337da4cede63b5bbc3537083913591f7b (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
  Copyright (C) 2008, 2009, 2010 Jiri Olsa <olsajiri@gmail.com>

  This file is part of the latrace.

  The latrace 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 3 of the License, or
  (at your option) any later version.

  The latrace 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 the latrace (file COPYING).  If not, see 
  <http://www.gnu.org/licenses/>.
*/


#include <link.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <bits/wordsize.h>
#include <gnu/lib-names.h>
#include <stdlib.h>

#include "config.h"


extern struct lt_config_audit cfg;

static __thread int pipe_fd = 0;
static __thread int flow_below_stack = 0;
static __thread int indent_depth = 0;


static int check_names(char *name, char **ptr)
{
	char *n;

	for(n = *ptr; n; n = *(++ptr)) {
		if ((n[0] == '*' && strstr(name, n + 1)) ||
		    strcmp(name, n) == 0) {
			PRINT_VERBOSE(&cfg, 2,
				"return %d for name %s\n", 1, name);
			return 1;
		}
	}

	PRINT_VERBOSE(&cfg, 2, "return %d for name %s\n",
			0, name);
	return 0;
}

static int check_flow_below(const char *symname, int in)
{
	int ret = flow_below_stack;

	if (check_names((char*) symname, cfg.flow_below))
		in ? ret = ++flow_below_stack : flow_below_stack--;

	return ret;
}

static int sym_entry(const char *symname, char *lib_from, char *lib_to, 
			La_regs *regs)
{
	int argret = -1;
	char *argbuf = "", *argdbuf = "";
	struct timeval tv;

	PRINT_VERBOSE(&cfg, 2, "%s@%s\n", symname, lib_to);

	if (cfg.flow_below_cnt && !check_flow_below(symname, 1))
		return 0;

	if (lt_sh(&cfg, timestamp) || lt_sh(&cfg, counts))
		gettimeofday(&tv, NULL);

#ifdef CONFIG_ARCH_HAVE_ARGS
	argret = lt_sh(&cfg, args_enabled) ?
		lt_args_sym_entry(cfg.sh, (char*) symname, regs, &argbuf, &argdbuf) : -1;
#endif

	if (lt_sh(&cfg, pipe)) {
		char buf[FIFO_MSG_MAXLEN];
		int len;

		if (!pipe_fd)
			pipe_fd = lt_fifo_create(&cfg, cfg.dir);

		len = lt_fifo_msym_get(&cfg, buf, FIFO_MSG_TYPE_ENTRY, &tv,
				(char*) symname, lib_to, argbuf, argdbuf);

		return lt_fifo_send(&cfg, pipe_fd, buf, len);
	}

	indent_depth++;

	lt_out_entry(cfg.sh, &tv, syscall(SYS_gettid),
			indent_depth,
			symname, lib_to,
			argbuf, argdbuf);

	if (!argret) {
		free(argbuf);
		if (lt_sh(&cfg, args_detailed) && (*argdbuf))
			free(argdbuf);
	}

	return 0;
}

static int sym_exit(const char *symname, char *lib_from, char *lib_to, 
			const La_regs *inregs, La_retval *outregs)
{
	int argret = -1;
	char *argbuf = "", *argdbuf = "";
	struct timeval tv;

	PRINT_VERBOSE(&cfg, 2, "%s@%s\n", symname, lib_to);

	if (cfg.flow_below_cnt && !check_flow_below(symname, 0))
		return 0;

	if (lt_sh(&cfg, timestamp) || lt_sh(&cfg, counts))
		gettimeofday(&tv, NULL);

#ifdef CONFIG_ARCH_HAVE_ARGS
	argret = lt_sh(&cfg, args_enabled) ?
		lt_args_sym_exit(cfg.sh, (char*) symname,
			(La_regs*) inregs, outregs, &argbuf, &argdbuf) : -1;
#endif

	if (lt_sh(&cfg, pipe)) {
		char buf[FIFO_MSG_MAXLEN];
		int len;

		len = lt_fifo_msym_get(&cfg, buf, FIFO_MSG_TYPE_EXIT, &tv,
				(char*) symname, lib_to, argbuf, argdbuf);

		return lt_fifo_send(&cfg, pipe_fd, buf, len);
	}

	lt_out_exit(cfg.sh, &tv, syscall(SYS_gettid),
			indent_depth,
			symname, lib_from,
			argbuf, argdbuf);

	indent_depth--;

	if (!argret) {
		free(argbuf);

		if (lt_sh(&cfg, args_detailed) && (*argdbuf))
			free(argdbuf);
	}

	return 0;
}

static int check_pid()
{
	pid_t pid = getpid();

	PRINT_VERBOSE(&cfg, 1, "tid = %d, cfg tid = %d\n",
			pid, lt_sh(&cfg, pid));

	if (pid != lt_sh(&cfg, pid))
		return -1;

	return 0;
}

#define CHECK_PID(ret) \
do { \
	if (cfg.sh->not_follow_fork && \
	    check_pid()) \
		return ret; \
} while(0)

unsigned int la_version(unsigned int v)
{
	return v;
}

unsigned int la_objopen(struct link_map *l, Lmid_t a, uintptr_t *cookie)
{
	char *name = l->l_name;

	if (!cfg.init_ok)
		return 0;

	if (!name)
		return 0;

	/* executable itself */
	if (!(*name))
		return LA_FLG_BINDTO | LA_FLG_BINDFROM;

	/* audit all as default */
	if ((!cfg.libs_to_cnt) &&
	    (!cfg.libs_from_cnt) &&
	    (!cfg.libs_both_cnt))
		return LA_FLG_BINDTO | LA_FLG_BINDFROM;

	if (check_names(name, cfg.libs_to))
		return LA_FLG_BINDTO;

	if (check_names(name, cfg.libs_from))
		return LA_FLG_BINDFROM;

	if (check_names(name, cfg.libs_both))
		return LA_FLG_BINDTO | LA_FLG_BINDFROM;

	/* wrong library name specified ? */
	return 0;
}

static unsigned int la_symbind(const char *symname)
{
	unsigned int flags = 0;

	if (cfg.symbols_cnt) {
		flags = LA_SYMB_NOPLTENTER|LA_SYMB_NOPLTEXIT;
		if (check_names((char*) symname, cfg.symbols))
			flags = 0;
	}

	if (cfg.symbols_omit_cnt) {
		if (check_names((char*) symname, cfg.symbols_omit))
			flags = LA_SYMB_NOPLTENTER|LA_SYMB_NOPLTEXIT;
	}

	return flags;
}

void la_activity(uintptr_t *cookie, unsigned int act)
{
	PRINT_VERBOSE(&cfg, 2, "entry\n");
}

char* la_objsearch(const char *name, uintptr_t *cookie, unsigned int flag)
{
	if (flag == LA_SER_ORIG)
		return (char*) name;

	return lt_objsearch(&cfg, name, cookie, flag);
}

void la_preinit(uintptr_t *__cookie)
{
	PRINT_VERBOSE(&cfg, 2, "entry\n");
}

unsigned int la_objclose(uintptr_t *__cookie)
{
	PRINT_VERBOSE(&cfg, 2, "entry\n");
	return 0;
}

uintptr_t la_symbind32(Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
		uintptr_t *defcook, unsigned int *flags, const char *symname)
{
	*flags = la_symbind(symname);
	return sym->st_value;
}

uintptr_t la_symbind64(Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
		uintptr_t *defcook, unsigned int *flags, const char *symname)
{
	*flags = la_symbind(symname);
	return sym->st_value;
}

ElfW(Addr)
pltenter(ElfW(Sym) *sym, unsigned int ndx, uintptr_t *refcook,
          uintptr_t *defcook, La_regs *regs, unsigned int *flags,
          const char *symname, long int *framesizep)
{
	struct link_map *lr = (struct link_map*) *refcook;
	struct link_map *ld = (struct link_map*) *defcook;

	do {
		if (lt_sh(&cfg, disabled))
			break;

		CHECK_PID(sym->st_value);

		sym_entry(symname, lr ? lr->l_name : NULL,
				   ld ? ld->l_name : NULL, regs);

	} while(0);

	*framesizep = lt_stack_framesize(&cfg, regs);

	return sym->st_value;
}

unsigned int pltexit(ElfW(Sym) *sym, unsigned int ndx, uintptr_t *refcook,
         uintptr_t *defcook, const La_regs *inregs, La_retval *outregs,
         const char *symname)
{
	struct link_map *lr = (struct link_map*) *refcook;
	struct link_map *ld = (struct link_map*) *defcook;

	do {
		if (lt_sh(&cfg, disabled))
			break;

		CHECK_PID(0);

		sym_exit(symname, lr ? lr->l_name : NULL,
				  ld ? ld->l_name : NULL, inregs, outregs);

	} while(0);

	return 0;
}