-
Notifications
You must be signed in to change notification settings - Fork 20
/
osutils.H
150 lines (133 loc) · 4.09 KB
/
osutils.H
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
///
/// @file
/// \brief Utility functions with OS-specific implementations.
///
#ifndef DTRACKER_OSUTILS_H
#define DTRACKER_OSUTILS_H
#include <iostream>
#include <string>
#include <assert.h>
#include <libgen.h>
#include <limits.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "pin.H"
#include "libdft_api.h"
/* Platform specific white-lists.
* Data coming from files matching these patterns are not tainted.
*/
#if defined(TARGET_LINUX)
#define DTRACKER_FILE_WHITELIST_RE "\\.so$|\\.so\\."
#define DTRACKER_PATH_WHITELIST_RE "^/proc/|^/lib/|^/usr/lib/|^/etc/|^/usr/share/"
#elif defined(TARGET_MAC)
#define DTRACKER_FILE_WHITELIST_RE "\\.dylib$"
#define DTRACKER_PATH_WHITELIST_RE NULL
#elif defined(TARGET_WINDOWS)
#define DTRACKER_FILE_WHITELIST_RE "\\.dll$"
#define DTRACKER_PATH_WHITELIST_RE NULL
#endif
///
/// @brief Determines if a filename is whitelisted.
///
/// Whitelisted files are not tainted by dtracker.
/// Without whitelisting, the slowdown factor because of taint
/// tracking is HUGE.
///
/// @param fname -- the filename to be checked.
/// @return 1 if the filename is whitelisted. 0 otherwise.
///
inline int in_dtracker_whitelist(const std::string & fname) {
// Note: basename() and dirname() may modify their arguments.
// For this, we create a duplicate of fname to give them.
// Also their return value should not be freed because it
// is either a pointer into fname or statically allocated.
char *fdup;
// Check file patterns.
if (DTRACKER_FILE_WHITELIST_RE != NULL && (fdup = strdup(fname.c_str()))) {
int status = -1;
regex_t re;
char *bname = basename(fdup);
if (regcomp(&re, DTRACKER_FILE_WHITELIST_RE, REG_EXTENDED|REG_NOSUB) == 0) {
status = regexec(&re, bname, (size_t) 0, NULL, 0);
regfree(&re);
}
free(fdup);
if (status == 0) return 1;
}
// Check dir patterns.
if (DTRACKER_PATH_WHITELIST_RE != NULL && (fdup = strdup(fname.c_str()))) {
int status = -1;
regex_t re;
// We have to do this crap because dirname() does not append a /.
char *dname_noslash = dirname(fdup);
size_t dname_sz = (strlen(dname_noslash)+2)*sizeof(char);
char *dname = (char *)malloc(dname_sz);
if (dname != NULL && regcomp(&re, DTRACKER_PATH_WHITELIST_RE, REG_EXTENDED|REG_NOSUB) == 0) {
snprintf(dname, dname_sz, "%s/", dname_noslash);
status = regexec(&re, dname, (size_t) 0, NULL, 0);
regfree(&re);
free(dname);
}
free(fdup);
if (status == 0) return 1;
}
return 0;
}
///
/// @brief Retrieves the absolute path to a file, resolving any symlinks.
///
/// Currently only implemented for Linux/MacOS, for which the finction is
/// a simple wrapper over realpath(3).
///
/// @param path -- a file path to be resolved.
/// @return A string representing the absolute path to the file or NULL.
inline std::string path_resolve(const std::string & path) {
#if defined(TARGET_LINUX) || defined(TARGET_MAC)
char *crval = realpath(path.c_str(), NULL);
if (crval != NULL) {
std::string rval(crval);
free(crval);
return rval;
}
else {
return NULL;
}
#elif defined(TARGET_WINDOWS)
assert(0);
return NULL;
#endif
}
inline int path_isdir(const std::string & path) {
#if defined(TARGET_LINUX) || defined(TARGET_MAC)
struct stat stats;
return (stat(path.c_str(), &stats) == 0 && S_ISDIR(stats.st_mode));
#elif defined(TARGET_WINDOWS)
assert(0);
return -1;
#endif
}
inline int path_exists(const std::string & path) {
#if defined(TARGET_LINUX) || defined(TARGET_MAC)
return (access(path.c_str(), F_OK) == 0);
#elif defined(TARGET_WINDOWS)
assert(0);
return -1;
#endif
}
///
/// @brief Resolves an open file descriptor to a filename.
///
/// Any symbolic links in the path are resolved. If an error occurs,
/// the respective error message is returned instead of the file path.
/// Because the function uses a static buffer, the file path may be
/// returned truncated ending with "...".
///
/// @param fd -- the file descriptor to be resolved.
/// @return A string representing the full path to the file.
std::string fdname(int fd);
#endif
/* vim: set noet ts=4 sts=4 sw=4 ai : */