Skip to content

Commit

Permalink
Fixed mkdir -p and find in subdir (by adding chdir and using absolute…
Browse files Browse the repository at this point in the history
… paths)
  • Loading branch information
glatard committed May 15, 2020
1 parent 2592fa7 commit 13b8a3e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
8 changes: 8 additions & 0 deletions functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ extern "C" {
return ((funcptr_mkdir)libc_mkdir)(passpath, mode);
}

int chdir(const char *pathname){
initialize_passthrough_if_necessary();
char passpath[PATH_MAX];
pass_getpath(pathname, passpath);
log_msg(INFO, "chdir path %s", passpath);
return ((funcptr_chdir)libc_chdir)(passpath);
}

int rename(const char *oldpath, const char *newpath){
initialize_passthrough_if_necessary();
char oldpasspath[PATH_MAX];
Expand Down
7 changes: 4 additions & 3 deletions logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <string.h>
#include <stdlib.h>

// maybe use tmpnam?
static const char* log_fn = "pass.log";

const char* get_lvlname(int lvl){
switch(lvl){
Expand All @@ -22,7 +20,10 @@ const char* get_lvlname(int lvl){
}

int log_msg(int lvl, const char* msg, ...){

char log_fn[PATH_MAX];
*log_fn = '\0';
strcat(log_fn, getenv("HOME"));
strcat(log_fn, "/pass.log");
if (lvl > DEBUG_LVL)
return 0;

Expand Down
25 changes: 23 additions & 2 deletions passthrough.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void* libc_unlink;
void* libc_unlinkat;
void* libc_readdir;
void* libc_mkdir;
void* libc_chdir;
void* libc_access;
void* libc_faccessat;
void* libc_stat;
Expand Down Expand Up @@ -77,12 +78,29 @@ void* libattr_fsetxattr;
void* libmagic;
void* libmagic_magic_file;

static const char* relmount = "./mount";
static char relmount[PATH_MAX];
static char mount_dir[PATH_MAX];
static const char* source_file = "./sources.txt";
static char source_file[PATH_MAX];
static char source_mounts[1][PATH_MAX];


static char * get_sea_home()
{
char* sea_home;
sea_home = getenv("PWD");
return sea_home;
}

static void init_paths()
{
char* sea_home = get_sea_home();
strcat(relmount, sea_home);
strcat(relmount, "/mount");

strcat(source_file, sea_home);
strcat(source_file, "/sources.txt");
}

// Our "copy" of stdout, because the application might close stdout
// or reuse the first file descriptors for other purposes.
static FILE* fdout = 0;
Expand Down Expand Up @@ -236,6 +254,8 @@ int pass_getpath(const char* oldpath, char passpath[PATH_MAX]){

static void initialize_passthrough() {
//xprintf("initialize_passthrough(): Setting up pass-through\n");
init_paths();

libc = dlopen("libc.so.6", RTLD_LAZY); // TODO: link with correct libc, version vs. 32 bit vs. 64 bit
libc_open = dlsym(libc, "open");
libc___open = dlsym(libc, "__open");
Expand All @@ -255,6 +275,7 @@ static void initialize_passthrough() {

libc_readdir = dlsym(libc, "readdir");
libc_mkdir = dlsym(libc, "mkdir");
libc_chdir = dlsym(libc, "chdir");
libc_rename = dlsym(libc, "rename");
libc_renameat = dlsym(libc, "renameat");
libc_renameat2 = dlsym(libc, "renameat2");
Expand Down
5 changes: 5 additions & 0 deletions passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define PRELOAD_PASSTHROUGH_H_

#include <stdio.h>

#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
Expand All @@ -17,6 +18,8 @@
#include <stdarg.h>
#include <magic.h>

extern char* log_fn;

typedef int (*funcptr_open)(const char*, int, mode_t);
typedef int (*funcptr___open)(const char*, int, mode_t);
typedef int (*funcptr_open64)(const char*, int, mode_t);
Expand All @@ -36,6 +39,7 @@ typedef off_t (*funcptr_lseek)(int, off_t, int);

typedef struct dirent* (*funcptr_readdir)(DIR*);
typedef int (*funcptr_mkdir)(const char*, mode_t mode);
typedef int (*funcptr_chdir)(const char*);
typedef int (*funcptr_rename)(const char*, const char*);
typedef int (*funcptr_renameat)(int, const char*, int, const char*);
typedef int (*funcptr_renameat2)(int, const char*, int, const char*, unsigned int);
Expand Down Expand Up @@ -95,6 +99,7 @@ extern void* libc_remove;
extern void* libc_unlink;
extern void* libc_unlinkat;
extern void* libc_mkdir;
extern void* libc_chdir;
extern void* libc_readdir;

extern void* libc_access;
Expand Down
6 changes: 3 additions & 3 deletions tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# Source and mount are
# cleaned up in setup
SOURCE="source"
MOUNT="mount"
SOURCE="$PWD/source"
MOUNT="$PWD/mount"

@test "test" {
load setup
Expand Down Expand Up @@ -89,7 +89,7 @@ MOUNT="mount"
f=$(find ${MOUNT} -name file_in_subdir.txt)
[[ "$f" == *"mount/subdir/file_in_subdir.txt" ]]
f=$(find ${MOUNT} -name file_in_source.txt)
[[ "$f" == *"mount/subdir/file_in_source.txt" ]]
[[ "$f" == *"mount/file_in_source.txt" ]]
}

@test "grep" {
Expand Down

0 comments on commit 13b8a3e

Please sign in to comment.