-
Notifications
You must be signed in to change notification settings - Fork 5
/
fixnano.c
236 lines (211 loc) · 7.22 KB
/
fixnano.c
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
#define _GNU_SOURCE
#include <dlfcn.h>
#include <inttypes.h>
#include <limits.h>
#include <regex.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wordexp.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
#ifdef __APPLE__
#define OVERRIDE_NAME(function) overridden_##function
#define ORIGINAL_NAME(function) function
#else
#define OVERRIDE_NAME(function) function
#define ORIGINAL_NAME(function) original_##function
#endif
#define VERSION_REGEX "[0-9]+(\\.[0-9]+)?(\\.[0-9]+)?"
FILE *(*original_fopen)(const char *restrict, const char *restrict);
int (*original_fclose)(FILE *);
int (*original_fileno)(FILE *stream);
__attribute__((weak)) const char *getprogname();
__attribute__((weak)) extern char *program_invocation_short_name;
atomic_bool initialized;
bool should_inject;
wordexp_t expansion;
typedef uintmax_t nano_version[3];
nano_version current_version;
struct {
char *buffer;
size_t size;
} nanorc;
FILE *nanorc_file;
static void *find_libc_symbol(const char *name) {
void *symbol = dlsym(RTLD_NEXT, name);
if (symbol) {
return symbol;
} else {
// If dlsym failed with "RTLD_NEXT used in code not dynamically loaded"
Dl_info info;
dladdr(fmemopen /* Something inside of libc */, &info);
return dlsym(dlopen(info.dli_fname, RTLD_LAZY | RTLD_NOLOAD), name);
}
}
static void parse_version(char *version_string, nano_version version) {
char *version_end;
for (size_t i = 0; i < sizeof(nano_version) / sizeof(*version); ++i) {
version[i] = strtoumax(version_string, &version_end, 10);
version[i] *= version_end != version_string;
if (version_end == version_string) {
break;
} else if (*(version_string = version_end) != '.') {
break;
}
++version_string;
}
}
static void get_current_nano_version() {
char executable[PATH_MAX];
#ifdef __APPLE__
uint32_t executable_size = sizeof(executable);
_NSGetExecutablePath(executable, &executable_size);
#else
executable[readlink("/proc/self/exe", executable, sizeof(executable))] = '\0';
#endif
char command[1024];
FILE *output = popen(snprintf(command, sizeof(command),
"DYLD_INSERT_LIBRARIES= LD_PRELOAD= %s --version |"
" grep -E -o '(version |v)" VERSION_REGEX "' |"
" grep -E -o '" VERSION_REGEX "'",
executable) > 0
? command
: "echo version 0",
"r");
char *line = NULL;
size_t size;
getline(&line, &size, output);
pclose(output);
parse_version(strlen(line) ? line : "0", current_version);
free(line);
}
static void fixnano_init() {
should_inject = (getprogname && !strcmp(getprogname(), "nano")) ||
(&program_invocation_short_name && // Does the symbol exist at all?
program_invocation_short_name && // If the symbol exists, is it bound (to libc)?
!strcmp(program_invocation_short_name, "nano"));
original_fopen = find_libc_symbol("fopen");
original_fclose = find_libc_symbol("fclose");
original_fileno = find_libc_symbol("fileno");
if (should_inject) {
wordexp("~/.nanorc", &expansion, 0);
get_current_nano_version();
}
atomic_store_explicit(&initialized, true, memory_order_relaxed);
}
__attribute__((destructor)) static void fixnano_fini() {
wordfree(&expansion);
free(nanorc.buffer);
}
// I can't figure out how to make fixnano_init be called before any other
// library's constructor, so just force initialization on first call to the
// functions we interpose. (If we don't do this, certain applications will call
// these functions in their constructors and the "original" functions will never
// be set.)
#define ENSURE_INITIALIZATION() \
do { \
if (!atomic_load_explicit(&initialized, memory_order_relaxed)) { \
fixnano_init(); \
} \
} while (0)
static void add_nanorc_line(char *line) {
size_t length = strlen(line);
nanorc.buffer = realloc(nanorc.buffer, nanorc.size + length);
memcpy(nanorc.buffer + nanorc.size, line, length);
nanorc.size += length;
}
static char *extract_regex_match(char *line, regmatch_t *match) {
size_t length = match->rm_eo - match->rm_so;
char *result = malloc(length + 1);
memcpy(result, line + match->rm_so, length);
result[length] = '\0';
return result;
}
static int verscmp(nano_version v1, nano_version v2) {
for (size_t i = 0; i < sizeof(nano_version) / sizeof(*v1); ++i) {
if (v2[i] < v1[i]) {
return 1;
} else if (v1[i] < v2[i]) {
return -1;
}
}
return 0;
}
static inline bool supported_version(char *line, regmatch_t *matches) {
nano_version minimum_version = {0, 0, 0};
nano_version maximum_version = {-1, -1, -1};
if (0 <= matches[1].rm_so) {
parse_version(line + matches[1].rm_so, minimum_version);
}
if (0 <= matches[4].rm_so) {
parse_version(line + matches[4].rm_so, maximum_version);
}
return verscmp(current_version, minimum_version) >= 0 && verscmp(current_version, maximum_version) <= 0;
}
FILE *OVERRIDE_NAME(fopen)(const char *restrict path, const char *restrict mode) {
ENSURE_INITIALIZATION();
if (should_inject && !strcmp(path, *expansion.we_wordv)) {
FILE *file = ORIGINAL_NAME(fopen)(path, mode);
regex_t regex;
regcomp(®ex, "^# (" VERSION_REGEX ")?-(" VERSION_REGEX ")? (.*)", REG_EXTENDED);
regmatch_t matches[8];
char *line = NULL;
size_t size = 0;
while (0 < getline(&line, &size, file)) {
if (!regexec(®ex, line, sizeof(matches) / sizeof(*matches), matches, 0) &&
supported_version(line, matches)) {
char *setting = extract_regex_match(line, matches + 7);
add_nanorc_line(setting);
free(setting);
} else {
add_nanorc_line(line);
}
}
regfree(®ex);
free(line);
return nanorc_file = fmemopen(nanorc.buffer, nanorc.size, mode);
} else {
return ORIGINAL_NAME(fopen)(path, mode);
}
}
int OVERRIDE_NAME(fclose)(FILE *stream) {
ENSURE_INITIALIZATION();
if (stream == nanorc_file) {
nanorc_file = NULL;
}
return ORIGINAL_NAME(fclose)(stream);
}
int OVERRIDE_NAME(fileno)(FILE *stream) {
ENSURE_INITIALIZATION();
// Nano's getline reimplementation used to check that this returns something
// other than -1. Return -2 to bypass the check for our memory buffer.
return stream == nanorc_file ? -2 : ORIGINAL_NAME(fileno)(stream);
}
#ifdef __APPLE__
FILE *fopen_DARWIN_EXTSN(const char *restrict, const char *restrict) __DARWIN_EXTSN(fopen);
__attribute__((used, section("__DATA,__interpose"))) static struct {
FILE *(*overridden_fopen)(const char *restrict, const char *restrict);
FILE *(*fopen)(const char *restrict, const char *restrict);
} fopen_overrides[] = {
{overridden_fopen, fopen},
{overridden_fopen, fopen_DARWIN_EXTSN},
};
__attribute__((used, section("__DATA,__interpose"))) static struct {
int (*overridden_fclose)(FILE *);
int (*fclose)(FILE *);
} fclose_overrides[] = {
{overridden_fclose, fclose},
};
__attribute__((used, section("__DATA,__interpose"))) static struct {
int (*overridden_fileno)(FILE *);
int (*fileno)(FILE *);
} fileno_overrides[] = {
{overridden_fileno, fileno},
};
#endif