util  Diff

Differences From Artifact [ca4bf4ac94]:

To Artifact [302b4f4c89]:


166
167
168
169
170
171
172

173
174
175
176
177
178
179
180
...
208
209
210
211
212
213
214

215
216
217
218
219
220
221
222
...
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
...
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
...
366
367
368
369
370
371
372













































373
374
375
376
377
378
379
...
384
385
386
387
388
389
390

391
392
393
394
395
396
397
...
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
427
428
429
430
431
432
433
434
435
436
437
438
static const char** wiretap_argv = NULL;
static const char* redir_prefix;
static const char* redir_to;
static size_t redir_len;
static const char* hq;
static size_t hq_len;


void configdirs() {
	const char* nkvd = getenv("nkvd_gulag");
	if (nkvd != NULL) redir_to = nkvd, redir_len = strlen(nkvd);

	const char* xdg = getenv("XDG_CONFIG_HOME");
	if (xdg != NULL) redir_to = xdg, redir_len = strlen(xdg);

	const char* home = getenv("HOME");
................................................................................
	strcpy(buf + homelen, CONFDIR);
	redir_len = homelen + sizeof CONFDIR;
#	undef CONFDIR

	redir_to = buf;
}


bool checkpath(const char* path, size_t len, char* gulag) {
	char c[hq_len + len + 4];
	strncpy(c, hq, hq_len);
	c[hq_len] = '/';
	c[hq_len+1] = '.';
	c[hq_len+2] = 0;
	
	const char* all = getenv("nkvd_interdict_all");
................................................................................
		return true;
	} else {
		/* no further questions, citizen */
		return false;
	}
}


bool interrogate(const char* path, size_t plen, char* gulag) {
	/* papers, please */
	if (path[0] != '/') for (size_t i = 64; i<PATH_MAX; i << 1) {
		char cwd[i + plen + 1];
		if (getcwd(cwd, i) == NULL) continue;
		size_t cwdlen = strlen(cwd);
		cwd[cwdlen] = '/';
		strncpy(cwd + cwdlen + 1, path, plen + 1);
		return checkpath(cwd,cwdlen + plen, gulag);
	}
	return checkpath(path, plen, gulag);
}


bool shitlist(const char* name) {
#	ifdef _NO_GNU /* avoid POSIX weirdness */
		{ char* name = strdup(name); /* yay shadowing! */
#	endif

	const char* base = basename(name);

	const char* list = getenv("nkvd_subversives");
................................................................................
#pragma GCC optimize ("O0")

#define STAT_PARAMS  const char* volatile path, struct stat* volatile statbuf
#define XSTAT_PARAMS int volatile ver, const char* volatile path, struct stat* volatile statbuf
#define decl_stat_ptr(nm)  static int (*libc_##nm) (STAT_PARAMS)
#define decl_xstat_ptr(nm) static int (*libc_##nm) (XSTAT_PARAMS)

static int (*libc_open)  (const char*,int,...);
static int (*libc_unlink)(const char*);




decl_stat_ptr(stat);
decl_stat_ptr(stat64);
decl_stat_ptr(lstat);
decl_stat_ptr(lstat64);

#ifndef _NO_GNU
	decl_xstat_ptr(xstat);
	decl_xstat_ptr(xstat64);
	decl_xstat_ptr(lxstat);
	decl_xstat_ptr(lxstat64);
#endif


int nkvd_stat(int (*volatile fn)(STAT_PARAMS), STAT_PARAMS) {
	if (intercept) {
		size_t plen = strlen(path);
		char gulag[redir_len + plen];
		if (interrogate(path, plen, gulag)) {
			return (*fn)(gulag,statbuf);
		}
	}
	return (*fn)(path,statbuf);
}


int nkvd_xstat(int (*volatile fn)(XSTAT_PARAMS), XSTAT_PARAMS) {
	if (intercept) {
		size_t plen = strlen(path);
		char gulag[redir_len + plen];
		if (interrogate(path, plen, gulag)) {
			return (*fn)(ver,gulag,statbuf);
		}
	}
................................................................................
		char gulag[redir_len + plen];
		if (interrogate(path, plen, gulag)) {
			return (*libc_unlink)(gulag);
		}
	}
	return (*libc_unlink)(path);
};














































int open(const char* volatile path, int flags, ...) {
	/* fuck you for using varargs, asshole */
	va_list v; va_start(v,flags);
	mode_t m;
	if(flags & O_CREAT) m = va_arg(v, mode_t);
	va_end(v);
................................................................................
		if (interrogate(path, plen, gulag)) {
			return (*libc_open)(gulag, flags, m);
		}
	}
	return (*libc_open)(path, flags, m);
}


int nkvd_init(int argc, const char** argv) {
	wiretap_argc = argc;
	wiretap_argv = argv;

#	ifndef _USE_RTLD_NEXT
		/* RTLD_NEXT is buggy as hell so we avoid it by default */
		void* SYMSRC = dlopen(_LIBC,RTLD_LAZY);
................................................................................
				"libc from " bold(_LIBC) "; please recompile "
				" and specify your libc with -D_LIBC= on the "
				"compile command line.");
#	endif

#	define load_fn(fn,pfx) libc_##fn = dlsym(SYMSRC, #pfx #fn)
		load_fn(open,);      load_fn(unlink,);


		load_fn(stat,);      load_fn(lstat,);
		load_fn(stat64,);    load_fn(lstat64,);
		
#		ifndef _NO_GNU
			load_fn(xstat,__);   load_fn(lxstat,__);
			load_fn(xstat64,__); load_fn(lxstat64,__);
#		endif
#	undef load_fn





	
	/* have enough stat fns been found for us to proceed? */
	bool statfns = (  (libc_stat    && libc_lstat)
	               || (libc_stat64  && libc_lstat64)
#ifndef _NO_GNU
	               || (libc_xstat   && libc_lxstat)
	               || (libc_xstat64 && libc_lxstat64)
#endif
	               );

	if (! (libc_open && statfns && libc_unlink))

		fail(-3, "your libc is defective. bailing.");

	intercept = shitlist(argv[0]);
	if (intercept) {
		configdirs();
	}

	return 0; /* ?? why is this int, not void */
}

__attribute__((section(".init_array"))) static void* nkvd_constructor = &nkvd_init;

int main() { return 0; } /* stub for -pie */







>
|







 







>
|







 







>
|












>
|







 







|
|
>
>
>













>
|










>
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>







 







>
>








>
>
>
>
>










|
>













166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
...
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
...
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
...
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
427
428
429
430
431
432
433
...
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
static const char** wiretap_argv = NULL;
static const char* redir_prefix;
static const char* redir_to;
static size_t redir_len;
static const char* hq;
static size_t hq_len;

static void
configdirs() {
	const char* nkvd = getenv("nkvd_gulag");
	if (nkvd != NULL) redir_to = nkvd, redir_len = strlen(nkvd);

	const char* xdg = getenv("XDG_CONFIG_HOME");
	if (xdg != NULL) redir_to = xdg, redir_len = strlen(xdg);

	const char* home = getenv("HOME");
................................................................................
	strcpy(buf + homelen, CONFDIR);
	redir_len = homelen + sizeof CONFDIR;
#	undef CONFDIR

	redir_to = buf;
}

static bool
checkpath(const char* path, size_t len, char* gulag) {
	char c[hq_len + len + 4];
	strncpy(c, hq, hq_len);
	c[hq_len] = '/';
	c[hq_len+1] = '.';
	c[hq_len+2] = 0;
	
	const char* all = getenv("nkvd_interdict_all");
................................................................................
		return true;
	} else {
		/* no further questions, citizen */
		return false;
	}
}

static bool
interrogate(const char* path, size_t plen, char* gulag) {
	/* papers, please */
	if (path[0] != '/') for (size_t i = 64; i<PATH_MAX; i << 1) {
		char cwd[i + plen + 1];
		if (getcwd(cwd, i) == NULL) continue;
		size_t cwdlen = strlen(cwd);
		cwd[cwdlen] = '/';
		strncpy(cwd + cwdlen + 1, path, plen + 1);
		return checkpath(cwd,cwdlen + plen, gulag);
	}
	return checkpath(path, plen, gulag);
}

static bool
shitlist(const char* name) {
#	ifdef _NO_GNU /* avoid POSIX weirdness */
		{ char* name = strdup(name); /* yay shadowing! */
#	endif

	const char* base = basename(name);

	const char* list = getenv("nkvd_subversives");
................................................................................
#pragma GCC optimize ("O0")

#define STAT_PARAMS  const char* volatile path, struct stat* volatile statbuf
#define XSTAT_PARAMS int volatile ver, const char* volatile path, struct stat* volatile statbuf
#define decl_stat_ptr(nm)  static int (*libc_##nm) (STAT_PARAMS)
#define decl_xstat_ptr(nm) static int (*libc_##nm) (XSTAT_PARAMS)

static int (*libc_open)   (const char* volatile,int,...);
static int (*libc_unlink) (const char* volatile);
static int (*libc_access) (const char* volatile,int);
static int (*libc_eaccess)(const char* volatile,int);
static int (*libc_rename) (const char* volatile,const char* volatile);

decl_stat_ptr(stat);
decl_stat_ptr(stat64);
decl_stat_ptr(lstat);
decl_stat_ptr(lstat64);

#ifndef _NO_GNU
	decl_xstat_ptr(xstat);
	decl_xstat_ptr(xstat64);
	decl_xstat_ptr(lxstat);
	decl_xstat_ptr(lxstat64);
#endif

static int
nkvd_stat(int (*volatile fn)(STAT_PARAMS), STAT_PARAMS) {
	if (intercept) {
		size_t plen = strlen(path);
		char gulag[redir_len + plen];
		if (interrogate(path, plen, gulag)) {
			return (*fn)(gulag,statbuf);
		}
	}
	return (*fn)(path,statbuf);
}

static int
nkvd_xstat(int (*volatile fn)(XSTAT_PARAMS), XSTAT_PARAMS) {
	if (intercept) {
		size_t plen = strlen(path);
		char gulag[redir_len + plen];
		if (interrogate(path, plen, gulag)) {
			return (*fn)(ver,gulag,statbuf);
		}
	}
................................................................................
		char gulag[redir_len + plen];
		if (interrogate(path, plen, gulag)) {
			return (*libc_unlink)(gulag);
		}
	}
	return (*libc_unlink)(path);
};

typedef int(*intfn)(const char*,int);
static int
nkvd_intcall(intfn fn, const char* volatile path, int mode) {
	if (intercept) {
		size_t plen = strlen(path);
		char gulag[redir_len + plen];
		if (interrogate(path, plen, gulag)) {
			return (*fn)(gulag,mode);
		}
	}
	return (*fn)(path,mode);
};

int access (const char* volatile path, int mode)
	{ nkvd_intcall(libc_access,path,mode); }

int eaccess (const char* volatile path, int mode)
	{ nkvd_intcall(libc_eaccess,path,mode); }

#define max(a,b) ((a) > (b) ? (a) : (b))
int rename(const char* volatile from,
           const char* volatile to) {
	if (intercept) {
		const size_t fromlen = strlen(from),
		               tolen = strlen(from);
		char fromgulag[redir_len + tolen  ],
		       togulag[redir_len + fromlen];

		const char* fromfinal,
		          * tofinal;

		if (interrogate(from, fromlen, fromgulag))
			fromfinal = fromgulag;
		else fromfinal = from;

		if (interrogate(to, tolen, togulag))
			tofinal = togulag;
		else tofinal = to;

		return (*libc_rename)(fromfinal,tofinal);
	}

	return (*libc_rename)(from,to);
};

int open(const char* volatile path, int flags, ...) {
	/* fuck you for using varargs, asshole */
	va_list v; va_start(v,flags);
	mode_t m;
	if(flags & O_CREAT) m = va_arg(v, mode_t);
	va_end(v);
................................................................................
		if (interrogate(path, plen, gulag)) {
			return (*libc_open)(gulag, flags, m);
		}
	}
	return (*libc_open)(path, flags, m);
}

#include <stdio.h>
int nkvd_init(int argc, const char** argv) {
	wiretap_argc = argc;
	wiretap_argv = argv;

#	ifndef _USE_RTLD_NEXT
		/* RTLD_NEXT is buggy as hell so we avoid it by default */
		void* SYMSRC = dlopen(_LIBC,RTLD_LAZY);
................................................................................
				"libc from " bold(_LIBC) "; please recompile "
				" and specify your libc with -D_LIBC= on the "
				"compile command line.");
#	endif

#	define load_fn(fn,pfx) libc_##fn = dlsym(SYMSRC, #pfx #fn)
		load_fn(open,);      load_fn(unlink,);
		load_fn(access,);    load_fn(eaccess,);
		load_fn(rename,);
		load_fn(stat,);      load_fn(lstat,);
		load_fn(stat64,);    load_fn(lstat64,);
		
#		ifndef _NO_GNU
			load_fn(xstat,__);   load_fn(lxstat,__);
			load_fn(xstat64,__); load_fn(lxstat64,__);
#		endif
#	undef load_fn
#	define dump(v) printf("-- " #v ": %p", libc_##v)
	dump(lstat);
	dump(lstat64);
	dump(lxstat);
	dump(lxstat64);
	
	/* have enough stat fns been found for us to proceed? */
	bool statfns = (  (libc_stat    && libc_lstat)
	               || (libc_stat64  && libc_lstat64)
#ifndef _NO_GNU
	               || (libc_xstat   && libc_lxstat)
	               || (libc_xstat64 && libc_lxstat64)
#endif
	               );

	if (! (libc_open && statfns && libc_unlink &&
	       libc_access && libc_eaccess && libc_rename))
		fail(-3, "your libc is defective. bailing.");

	intercept = shitlist(argv[0]);
	if (intercept) {
		configdirs();
	}

	return 0; /* ?? why is this int, not void */
}

__attribute__((section(".init_array"))) static void* nkvd_constructor = &nkvd_init;

int main() { return 0; } /* stub for -pie */