-
Notifications
You must be signed in to change notification settings - Fork 2
/
auditlib.cpp
427 lines (353 loc) · 14.4 KB
/
auditlib.cpp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// auditlib.so
#include <bits/stdc++.h>
#include <sys/stat.h>
#include <string>
#include <link.h>
// Snippets included from:
// https://man7.org/linux/man-pages/man7/rtld-audit.7.html
// Check if an output file exists
inline bool exists (const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
// Init file clears the file to prepare for new output
static void initFile(char * filename) {
std::fstream file;
file.open(filename, std::ios::out);
if(!file) {
printf("Error in creating output file.\n");
exit (EXIT_FAILURE);
}
file.close();
}
// Shared function for printing output to file or terminal
static void doPrint(std::string output) {
char * out = getenv("LDAUDIT_OUTFILE");
// Open for append to write, and then close
// This is obviously not ideal for many writes, but I don't
// see an easy way to do this without having an exit function.
if (out) {
std::ofstream file;
file.open(out, std::ios_base::app);
if (!file) {
printf("Error writing to output file.\n");
exit (EXIT_FAILURE);
}
file << output;
file.close();
} else {
std::cout << output;
}
}
// Prepare output file for writing
__attribute__((constructor))
static void init(void) {
// Do we want to write output to a file?
char * out = getenv("LDAUDIT_OUTFILE");
// Prepare file for writing
if (out) {
initFile(out);
}
}
/*
unsigned int la_version(unsigned int version);
This is the only function that must be defined by an auditing
library: it performs the initial handshake between the dynamic
linker and the auditing library. When invoking this function,
the dynamic linker passes, in version, the highest version of the
auditing interface that the linker supports.
A typical implementation of this function simply returns the
constant LAV_CURRENT, which indicates the version of <link.h>
that was used to build the audit module. If the dynamic linker
does not support this version of the audit interface, it will
refuse to activate this audit module. If the function returns
zero, the dynamic linker also does not activate this audit
module.
In order to enable backwards compatibility with older dynamic
linkers, an audit module can examine the version argument and
return an earlier version than LAV_CURRENT, assuming the module
can adjust its implementation to match the requirements of the
previous version of the audit interface. The la_version function
should not return the value of version without further checks
because it could correspond to an interface that does not match
the <link.h> definitions used to build the audit module.
*/
unsigned int la_version(unsigned int version) {
// If version == 0 the library will be ignored by the linker.
if (version == 0) {
return version;
}
// Prepare output - top of the yaml, and first event (version)
std::string output = "auditlib:\n la_version: " + std::to_string(version) + "\n audits:\n";
output += " - event: handshake\n function: la_version\n value: " + std::to_string(version) + "\n";
doPrint(output);
return LAV_CURRENT;
}
/*
The dynamic linker invokes this function after all shared objects
have been loaded, before control is passed to the application
(i.e., before calling main()). Note that main() may still later
dynamically load objects using dlopen(3).
*/
void la_preinit(uintptr_t *cookie) {
std::stringstream ss;
ss << cookie;
std::string output = " - event: pre_init\n function: la_preinit\n initiated_by: " + ss.str() +"\n";
doPrint(output);
}
/*
The dynamic linker invokes this function to inform the auditing
library that it is about to search for a shared object. The name
argument is the filename or pathname that is to be searched for.
cookie identifies the shared object that initiated the search.
flag is set to one of the following values:
LA_SER_ORIG
This is the original name that is being searched for.
Typically, this name comes from an ELF DT_NEEDED entry, or
is the filename argument given to dlopen(3).
LA_SER_LIBPATH
name was created using a directory specified in
LD_LIBRARY_PATH.
LA_SER_RUNPATH
name was created using a directory specified in an ELF
DT_RPATH or DT_RUNPATH list.
LA_SER_CONFIG
name was found via the ldconfig(8) cache
(/etc/ld.so.cache).
LA_SER_DEFAULT
name was found via a search of one of the default
directories.
LA_SER_SECURE
name is specific to a secure object (unused on Linux).
As its function result, la_objsearch() returns the pathname that
the dynamic linker should use for further processing. If NULL is
returned, then this pathname is ignored for further processing.
If this audit library simply intends to monitor search paths,
then name should be returned.
*/
char * la_objsearch(const char *name, uintptr_t *cookie, unsigned int flag) {
// Derive a human friendly flag name
std::string flagName;
std::string desc;
switch(flag) {
case LA_SER_ORIG:
desc = "This is the original name that is being searched for (ELF NEEDED or filename)";
flagName = "LA_SER_ORIG";
break;
case LA_SER_LIBPATH:
flagName = "LA_SER_LIBPATH";
desc = "name was created using a directory specified in LD_LIBRARY_PATH";
break;
case LA_SER_RUNPATH:
flagName = "LA_SER_RUNPATH";
desc = "name was created using a directory specified in an ELF DT_RPATH or DT_RUNPATH list.";
break;
case LA_SER_DEFAULT:
flagName = "LA_SER_DEFAULT";
desc = "name was found via a search of one of the default directories.";
break;
case LA_SER_CONFIG:
flagName = "LA_SER_CONFIG";
desc = "name was found via the ldconfig(8) cache (/etc/ld.so.cache).";
break;
case LA_SER_SECURE:
desc = "name is specific to a secure object (unused on Linux).";
flagName = "LA_SER_SECURE";
}
std::stringstream ss;
ss << cookie;
std::string output = " - event: searching_for\n function: la_objsearch\n name: \"" + std::string(name) + "\"\n";
output += " initiated_by: " + ss.str() + "\n flag: \"" + flagName + "\"\n description: \"" + desc + "\"\n";
doPrint(output);
return (char*)name;
}
/*
The dynamic linker calls this function to inform the auditing
library that link-map activity is occurring. cookie identifies
the object at the head of the link map. When the dynamic linker
invokes this function, flag is set to one of the following
values:
LA_ACT_ADD
New objects are being added to the link map.
LA_ACT_DELETE
Objects are being removed from the link map.
LA_ACT_CONSISTENT
Link-map activity has been completed: the map is once
again consistent.
*/
void la_activity (uintptr_t *cookie, unsigned int flag) {
std::string flagName;
std::string desc;
switch(flag) {
case LA_ACT_CONSISTENT:
flagName = "LA_ACT_CONSISTENT";
desc = "Link-map activity has been completed (map is consistent)";
break;
case LA_ACT_ADD:
flagName = "LA_ACT_ADD";
desc = "New objects are being added to the link map.";
break;
case LA_ACT_DELETE:
flagName = "LA_ACT_DELETE";
desc = "Objects are being removed from the link map.";
break;
default:
flagName = "???";
}
std::stringstream ss;
ss << cookie;
std::string output = " - event: activity_occurring\n function: la_activity\n initiated_by: " + ss.str() + "\n flag: " + flagName + "\n description: "+ desc +"\n";
doPrint(output);
}
/*
The dynamic linker calls this function when a new shared object
is loaded. The map argument is a pointer to a link-map structure
that describes the object. The lmid field has one of the
following values
LM_ID_BASE
Link map is part of the initial namespace.
LM_ID_NEWLM
Link map is part of a new namespace requested via
dlmopen(3).
cookie is a pointer to an identifier for this object. The
identifier is provided to later calls to functions in the
auditing library in order to identify this object. This
identifier is initialized to point to object's link map, but the
audit library can change the identifier to some other value that
it may prefer to use to identify the object.
As its return value, la_objopen() returns a bit mask created by
ORing zero or more of the following constants, which allow the
auditing library to select the objects to be monitored by
la_symbind*():
LA_FLG_BINDTO
Audit symbol bindings to this object.
LA_FLG_BINDFROM
Audit symbol bindings from this object.
A return value of 0 from la_objopen() indicates that no symbol
bindings should be audited for this object.
*/
unsigned int la_objopen(struct link_map *map, Lmid_t lmid, uintptr_t *cookie) {
std::string flagName;
std::string linkType;
switch(lmid) {
case LM_ID_BASE:
flagName = "LM_ID_BASE";
linkType = "Link map is part of the initial namespace";
break;
case LM_ID_NEWLM:
flagName = "LM_ID_NEWLM";
linkType = "Link map is part of a new namespace requested via dlmopen(3).";
break;
default:
flagName = "???";
linkType = "Unknown";
}
std::stringstream ss;
ss << cookie;
std::string output = " - event: object_loaded\n name: \"" + std::string(map->l_name) + "\"\n function: la_objopen\n identifier: " + ss.str() + "\n flag: " + flagName + "\n description: " + linkType +"\n";
doPrint(output);
return LA_FLG_BINDTO | LA_FLG_BINDFROM;
}
/*
The dynamic linker invokes this function after any finalization code
for the object has been executed, before the object is unloaded.
The cookie argument is the identifier obtained from a previous
invocation of la_objopen().
In the current implementation, the value returned by la_objclose() is ignored.
*/
unsigned int la_objclose (uintptr_t *cookie) {
std::stringstream ss;
ss << cookie;
std::string output = " - event: object_closed\n function: la_objclose\n identifier: " + ss.str() + "\n";
doPrint(output);
return 0;
}
/*
The dynamic linker invokes one of these functions when a symbol
binding occurs between two shared objects that have been marked
for auditing notification by la_objopen(). The la_symbind32()
function is employed on 32-bit platforms; the la_symbind64()
function is employed on 64-bit platforms.
The sym argument is a pointer to a structure that provides
information about the symbol being bound. The structure
definition is shown in <elf.h>. Among the fields of this
structure, st_value indicates the address to which the symbol is
bound.
The ndx argument gives the index of the symbol in the symbol
table of the bound shared object.
The refcook argument identifies the shared object that is making
the symbol reference; this is the same identifier that is
provided to the la_objopen() function that returned
LA_FLG_BINDFROM. The defcook argument identifies the shared
object that defines the referenced symbol; this is the same
identifier that is provided to the la_objopen() function that
returned LA_FLG_BINDTO.
The symname argument points a string containing the name of the
symbol.
The flags argument is a bit mask that both provides information
about the symbol and can be used to modify further auditing of
this PLT (Procedure Linkage Table) entry. The dynamic linker may
supply the following bit values in this argument:
LA_SYMB_DLSYM
The binding resulted from a call to dlsym(3).
LA_SYMB_ALTVALUE
A previous la_symbind*() call returned an alternate value
for this symbol.
By default, if the auditing library implements la_pltenter() and
la_pltexit() functions (see below), then these functions are
invoked, after la_symbind(), for PLT entries, each time the
symbol is referenced. The following flags can be ORed into
*flags to change this default behavior:
LA_SYMB_NOPLTENTER
Don't call la_pltenter() for this symbol.
LA_SYMB_NOPLTEXIT
Don't call la_pltexit() for this symbol.
The return value of la_symbind32() and la_symbind64() is the
address to which control should be passed after the function
returns. If the auditing library is simply monitoring symbol
bindings, then it should return sym->st_value. A different value
may be returned if the library wishes to direct control to an
alternate location.
*/
uintptr_t la_symbind32(Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
uintptr_t *defcook, unsigned int *flags, const char *symname) {
std::stringstream refcookie;
refcookie << refcook;
std::stringstream defcookie;
defcookie << defcook;
std::string desc;
switch (*flags) {
case LA_SYMB_DLSYM:
desc = "The binding resulted from a call to dlsym(3).";
break;
case LA_SYMB_ALTVALUE:
desc = "A previous la_symbind*() call returned an alternate value for this symbol.";
break;
default:
desc = "Unknown";
}
std::string output = " - event: symbol_bind\n name: \"" + std::string(symname) + "\"\n function: la_symbind32\n where_needed: " + refcookie.str() + "\n where_defined: " + defcookie.str() +"\n index_symbol: " + std::to_string(ndx) + "\n description: " + desc + "\n";
doPrint(output);
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) {
std::stringstream refcookie;
refcookie << refcook;
std::stringstream defcookie;
defcookie << defcook;
std::string desc;
switch (*flags) {
case LA_SYMB_DLSYM:
desc = "The binding resulted from a call to dlsym(3).";
break;
case LA_SYMB_ALTVALUE:
desc = "A previous la_symbind*() call returned an alternate value for this symbol.";
break;
default:
desc = "Unknown";
}
std::string output = " - event: symbol_bind\n name: \"" + std::string(symname) + "\"\n function: la_symbind64\n where_needed: " + refcookie.str() + "\n where_defined: " + defcookie.str() +"\n index_symbol: " + std::to_string(ndx) + "\n description: " + desc + "\n";
doPrint(output);
return sym->st_value;
}