Compare commits
4 commits
master
...
bugfix/ope
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f73701fc4e | ||
|
|
509b0c58b5 | ||
|
|
12a342d1f9 | ||
|
|
0869b206b9 |
1 changed files with 33 additions and 23 deletions
|
|
@ -34,17 +34,30 @@
|
|||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "tmperamental.h"
|
||||
|
||||
static int (*orig_open)(const char *, int, ...);
|
||||
static int (*orig_mkdir)(const char *, mode_t);
|
||||
static int (*orig_creat)(const char *, mode_t);
|
||||
static FILE * (*orig_fopen)(const char *, const char *);
|
||||
static FILE * (*orig_freopen)(const char *, const char *, FILE *);
|
||||
|
||||
static void tmperamental_init ( void ) __attribute__((constructor));
|
||||
|
||||
static void tmperamental_init ( void ) {
|
||||
orig_open = dlsym(RTLD_NEXT, "open");
|
||||
orig_mkdir = dlsym(RTLD_NEXT, "mkdir");
|
||||
orig_creat = dlsym(RTLD_NEXT, "creat");
|
||||
orig_fopen = dlsym(RTLD_NEXT, "fopen");
|
||||
orig_freopen = dlsym(RTLD_NEXT, "freopen");
|
||||
}
|
||||
|
||||
void enforcer ( const char * pathname ) {
|
||||
const char * to_check = "/tmp/";
|
||||
int len = strlen( to_check );
|
||||
char * leading = malloc(sizeof(char) * len);
|
||||
strncpy ( leading, pathname, len );
|
||||
if ( strcmp(to_check, leading) == 0 ) {
|
||||
if ( strncmp("/tmp/", pathname, 5) == 0 ) {
|
||||
printf("tmperamental: caught a write to /tmp.\n");
|
||||
exit(255);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -53,42 +66,39 @@ int open ( const char * pathname, int flags, ... ) {
|
|||
|
||||
va_list v;
|
||||
va_start(v, flags);
|
||||
mode_t mode = va_arg(v, mode_t);
|
||||
va_end(v);
|
||||
if ( mode ) {
|
||||
int (*orig_addr)(const char *, int, mode_t) = dlsym(RTLD_NEXT, "open");
|
||||
return orig_addr(pathname, flags, mode);
|
||||
} else {
|
||||
int (*orig_addr)(const char *, int) = dlsym(RTLD_NEXT, "open");
|
||||
return orig_addr(pathname, flags);
|
||||
if ( flags & O_CREAT )
|
||||
{
|
||||
mode_t mode = va_arg(v, mode_t);
|
||||
va_end(v);
|
||||
return orig_open(pathname, flags, mode);
|
||||
}
|
||||
else
|
||||
{
|
||||
va_end(v);
|
||||
return orig_open(pathname, flags);
|
||||
}
|
||||
}
|
||||
|
||||
int mkdir ( const char *pathname, mode_t mode ) {
|
||||
enforcer( pathname );
|
||||
|
||||
int (*orig_addr)(const char *, mode_t) = dlsym(RTLD_NEXT, "mkdir");
|
||||
return orig_addr(pathname, mode);
|
||||
return orig_mkdir(pathname, mode);
|
||||
}
|
||||
|
||||
int creat ( const char *pathname, mode_t mode ) {
|
||||
enforcer( pathname );
|
||||
|
||||
int (*orig_addr)(const char *, mode_t) = dlsym(RTLD_NEXT, "creat");
|
||||
return orig_addr(pathname, mode);
|
||||
return orig_creat(pathname, mode);
|
||||
}
|
||||
|
||||
FILE * fopen ( const char * path, const char *mode ) {
|
||||
enforcer(path);
|
||||
|
||||
FILE * (*orig_addr)(const char *, const char *) = dlsym(RTLD_NEXT, "fopen");
|
||||
return orig_addr( path, mode );
|
||||
return orig_fopen( path, mode );
|
||||
}
|
||||
|
||||
FILE * freopen ( const char *path, const char *mode, FILE * stream ) {
|
||||
enforcer(path);
|
||||
|
||||
FILE * (*orig_addr)(const char *, const char *, FILE *)
|
||||
= dlsym(RTLD_NEXT, "freopen");
|
||||
return orig_addr(path, mode, stream);
|
||||
return orig_freopen(path, mode, stream);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue