util  Diff

Differences From Artifact [ca4bf4ac94]:

To Artifact [302b4f4c89]:


   166    166   static const char** wiretap_argv = NULL;
   167    167   static const char* redir_prefix;
   168    168   static const char* redir_to;
   169    169   static size_t redir_len;
   170    170   static const char* hq;
   171    171   static size_t hq_len;
   172    172   
   173         -void configdirs() {
          173  +static void
          174  +configdirs() {
   174    175   	const char* nkvd = getenv("nkvd_gulag");
   175    176   	if (nkvd != NULL) redir_to = nkvd, redir_len = strlen(nkvd);
   176    177   
   177    178   	const char* xdg = getenv("XDG_CONFIG_HOME");
   178    179   	if (xdg != NULL) redir_to = xdg, redir_len = strlen(xdg);
   179    180   
   180    181   	const char* home = getenv("HOME");
................................................................................
   208    209   	strcpy(buf + homelen, CONFDIR);
   209    210   	redir_len = homelen + sizeof CONFDIR;
   210    211   #	undef CONFDIR
   211    212   
   212    213   	redir_to = buf;
   213    214   }
   214    215   
   215         -bool checkpath(const char* path, size_t len, char* gulag) {
          216  +static bool
          217  +checkpath(const char* path, size_t len, char* gulag) {
   216    218   	char c[hq_len + len + 4];
   217    219   	strncpy(c, hq, hq_len);
   218    220   	c[hq_len] = '/';
   219    221   	c[hq_len+1] = '.';
   220    222   	c[hq_len+2] = 0;
   221    223   	
   222    224   	const char* all = getenv("nkvd_interdict_all");
................................................................................
   239    241   		return true;
   240    242   	} else {
   241    243   		/* no further questions, citizen */
   242    244   		return false;
   243    245   	}
   244    246   }
   245    247   
   246         -bool interrogate(const char* path, size_t plen, char* gulag) {
          248  +static bool
          249  +interrogate(const char* path, size_t plen, char* gulag) {
   247    250   	/* papers, please */
   248    251   	if (path[0] != '/') for (size_t i = 64; i<PATH_MAX; i << 1) {
   249    252   		char cwd[i + plen + 1];
   250    253   		if (getcwd(cwd, i) == NULL) continue;
   251    254   		size_t cwdlen = strlen(cwd);
   252    255   		cwd[cwdlen] = '/';
   253    256   		strncpy(cwd + cwdlen + 1, path, plen + 1);
   254    257   		return checkpath(cwd,cwdlen + plen, gulag);
   255    258   	}
   256    259   	return checkpath(path, plen, gulag);
   257    260   }
   258    261   
   259         -bool shitlist(const char* name) {
          262  +static bool
          263  +shitlist(const char* name) {
   260    264   #	ifdef _NO_GNU /* avoid POSIX weirdness */
   261    265   		{ char* name = strdup(name); /* yay shadowing! */
   262    266   #	endif
   263    267   
   264    268   	const char* base = basename(name);
   265    269   
   266    270   	const char* list = getenv("nkvd_subversives");
................................................................................
   302    306   #pragma GCC optimize ("O0")
   303    307   
   304    308   #define STAT_PARAMS  const char* volatile path, struct stat* volatile statbuf
   305    309   #define XSTAT_PARAMS int volatile ver, const char* volatile path, struct stat* volatile statbuf
   306    310   #define decl_stat_ptr(nm)  static int (*libc_##nm) (STAT_PARAMS)
   307    311   #define decl_xstat_ptr(nm) static int (*libc_##nm) (XSTAT_PARAMS)
   308    312   
   309         -static int (*libc_open)  (const char*,int,...);
   310         -static int (*libc_unlink)(const char*);
          313  +static int (*libc_open)   (const char* volatile,int,...);
          314  +static int (*libc_unlink) (const char* volatile);
          315  +static int (*libc_access) (const char* volatile,int);
          316  +static int (*libc_eaccess)(const char* volatile,int);
          317  +static int (*libc_rename) (const char* volatile,const char* volatile);
   311    318   
   312    319   decl_stat_ptr(stat);
   313    320   decl_stat_ptr(stat64);
   314    321   decl_stat_ptr(lstat);
   315    322   decl_stat_ptr(lstat64);
   316    323   
   317    324   #ifndef _NO_GNU
   318    325   	decl_xstat_ptr(xstat);
   319    326   	decl_xstat_ptr(xstat64);
   320    327   	decl_xstat_ptr(lxstat);
   321    328   	decl_xstat_ptr(lxstat64);
   322    329   #endif
   323    330   
   324         -int nkvd_stat(int (*volatile fn)(STAT_PARAMS), STAT_PARAMS) {
          331  +static int
          332  +nkvd_stat(int (*volatile fn)(STAT_PARAMS), STAT_PARAMS) {
   325    333   	if (intercept) {
   326    334   		size_t plen = strlen(path);
   327    335   		char gulag[redir_len + plen];
   328    336   		if (interrogate(path, plen, gulag)) {
   329    337   			return (*fn)(gulag,statbuf);
   330    338   		}
   331    339   	}
   332    340   	return (*fn)(path,statbuf);
   333    341   }
   334    342   
   335         -int nkvd_xstat(int (*volatile fn)(XSTAT_PARAMS), XSTAT_PARAMS) {
          343  +static int
          344  +nkvd_xstat(int (*volatile fn)(XSTAT_PARAMS), XSTAT_PARAMS) {
   336    345   	if (intercept) {
   337    346   		size_t plen = strlen(path);
   338    347   		char gulag[redir_len + plen];
   339    348   		if (interrogate(path, plen, gulag)) {
   340    349   			return (*fn)(ver,gulag,statbuf);
   341    350   		}
   342    351   	}
................................................................................
   366    375   		char gulag[redir_len + plen];
   367    376   		if (interrogate(path, plen, gulag)) {
   368    377   			return (*libc_unlink)(gulag);
   369    378   		}
   370    379   	}
   371    380   	return (*libc_unlink)(path);
   372    381   };
          382  +
          383  +typedef int(*intfn)(const char*,int);
          384  +static int
          385  +nkvd_intcall(intfn fn, const char* volatile path, int mode) {
          386  +	if (intercept) {
          387  +		size_t plen = strlen(path);
          388  +		char gulag[redir_len + plen];
          389  +		if (interrogate(path, plen, gulag)) {
          390  +			return (*fn)(gulag,mode);
          391  +		}
          392  +	}
          393  +	return (*fn)(path,mode);
          394  +};
          395  +
          396  +int access (const char* volatile path, int mode)
          397  +	{ nkvd_intcall(libc_access,path,mode); }
          398  +
          399  +int eaccess (const char* volatile path, int mode)
          400  +	{ nkvd_intcall(libc_eaccess,path,mode); }
          401  +
          402  +#define max(a,b) ((a) > (b) ? (a) : (b))
          403  +int rename(const char* volatile from,
          404  +           const char* volatile to) {
          405  +	if (intercept) {
          406  +		const size_t fromlen = strlen(from),
          407  +		               tolen = strlen(from);
          408  +		char fromgulag[redir_len + tolen  ],
          409  +		       togulag[redir_len + fromlen];
          410  +
          411  +		const char* fromfinal,
          412  +		          * tofinal;
          413  +
          414  +		if (interrogate(from, fromlen, fromgulag))
          415  +			fromfinal = fromgulag;
          416  +		else fromfinal = from;
          417  +
          418  +		if (interrogate(to, tolen, togulag))
          419  +			tofinal = togulag;
          420  +		else tofinal = to;
          421  +
          422  +		return (*libc_rename)(fromfinal,tofinal);
          423  +	}
          424  +
          425  +	return (*libc_rename)(from,to);
          426  +};
   373    427   
   374    428   int open(const char* volatile path, int flags, ...) {
   375    429   	/* fuck you for using varargs, asshole */
   376    430   	va_list v; va_start(v,flags);
   377    431   	mode_t m;
   378    432   	if(flags & O_CREAT) m = va_arg(v, mode_t);
   379    433   	va_end(v);
................................................................................
   384    438   		if (interrogate(path, plen, gulag)) {
   385    439   			return (*libc_open)(gulag, flags, m);
   386    440   		}
   387    441   	}
   388    442   	return (*libc_open)(path, flags, m);
   389    443   }
   390    444   
          445  +#include <stdio.h>
   391    446   int nkvd_init(int argc, const char** argv) {
   392    447   	wiretap_argc = argc;
   393    448   	wiretap_argv = argv;
   394    449   
   395    450   #	ifndef _USE_RTLD_NEXT
   396    451   		/* RTLD_NEXT is buggy as hell so we avoid it by default */
   397    452   		void* SYMSRC = dlopen(_LIBC,RTLD_LAZY);
................................................................................
   400    455   				"libc from " bold(_LIBC) "; please recompile "
   401    456   				" and specify your libc with -D_LIBC= on the "
   402    457   				"compile command line.");
   403    458   #	endif
   404    459   
   405    460   #	define load_fn(fn,pfx) libc_##fn = dlsym(SYMSRC, #pfx #fn)
   406    461   		load_fn(open,);      load_fn(unlink,);
          462  +		load_fn(access,);    load_fn(eaccess,);
          463  +		load_fn(rename,);
   407    464   		load_fn(stat,);      load_fn(lstat,);
   408    465   		load_fn(stat64,);    load_fn(lstat64,);
   409    466   		
   410    467   #		ifndef _NO_GNU
   411    468   			load_fn(xstat,__);   load_fn(lxstat,__);
   412    469   			load_fn(xstat64,__); load_fn(lxstat64,__);
   413    470   #		endif
   414    471   #	undef load_fn
          472  +#	define dump(v) printf("-- " #v ": %p", libc_##v)
          473  +	dump(lstat);
          474  +	dump(lstat64);
          475  +	dump(lxstat);
          476  +	dump(lxstat64);
   415    477   	
   416    478   	/* have enough stat fns been found for us to proceed? */
   417    479   	bool statfns = (  (libc_stat    && libc_lstat)
   418    480   	               || (libc_stat64  && libc_lstat64)
   419    481   #ifndef _NO_GNU
   420    482   	               || (libc_xstat   && libc_lxstat)
   421    483   	               || (libc_xstat64 && libc_lxstat64)
   422    484   #endif
   423    485   	               );
   424    486   
   425         -	if (! (libc_open && statfns && libc_unlink))
          487  +	if (! (libc_open && statfns && libc_unlink &&
          488  +	       libc_access && libc_eaccess && libc_rename))
   426    489   		fail(-3, "your libc is defective. bailing.");
   427    490   
   428    491   	intercept = shitlist(argv[0]);
   429    492   	if (intercept) {
   430    493   		configdirs();
   431    494   	}
   432    495   
   433    496   	return 0; /* ?? why is this int, not void */
   434    497   }
   435    498   
   436    499   __attribute__((section(".init_array"))) static void* nkvd_constructor = &nkvd_init;
   437    500   
   438    501   int main() { return 0; } /* stub for -pie */