您可以使用LD_PRELOAD技巧进行此操作。这是一个将以特定前缀开头的路径映射到另一个位置的实现。该代码也在github上。
例如,您可以在/etc/
没有root用户的情况下伪造文件的存在。这对于owncloud客户端是必要的,该客户端在文件/etc/ownCloud/sync-exclude.list
不存在时拒绝工作。
它通过覆盖open()
和open64()
功能来将一个目录映射到另一个目录,例如,对的所有open()
调用/etc/ownCloud/...
都可以重定向到/home/user1/.etc/ownCloud/...
。
只需调整path_map
,然后在预加载lib的情况下编译并运行程序:
gcc -std=c99 -Wall -shared -fPIC path-mapping.c -o path-mapping.so -ldl
LD_PRELOAD=/path/to/my/path-mapping.so someprogram
源代码path-mapping.c
:
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dlfcn.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdarg.h>
#include <malloc.h>
// List of path pairs. Paths beginning with the first item will be
// translated by replacing the matching part with the second item.
static const char *path_map[][2] = {
{ "/etc/ownCloud/", "/home/user1/.etc/ownCloud/" },
};
__thread char *buffer = NULL;
__thread int buffer_size = -1;
typedef FILE* (*orig_fopen_func_type)(const char *path, const char *mode);
typedef int (*orig_open_func_type)(const char *pathname, int flags, ...);
static int starts_with(const char *str, const char *prefix) {
return (strncmp(prefix, str, strlen(prefix)) == 0);
}
static char *get_buffer(int min_size) {
int step = 63;
if (min_size < 1) {
min_size = 1;
}
if (min_size > buffer_size) {
if (buffer != NULL) {
free(buffer);
buffer = NULL;
buffer_size = -1;
}
buffer = malloc(min_size + step);
if (buffer != NULL) {
buffer_size = min_size + step;
}
}
return buffer;
}
static const char *fix_path(const char *path)
{
int count = (sizeof path_map) / (sizeof *path_map); // Array length
for (int i = 0; i < count; i++) {
const char *prefix = path_map[i][0];
const char *replace = path_map[i][1];
if (starts_with(path, prefix)) {
const char *rest = path + strlen(prefix);
char *new_path = get_buffer(strlen(path) + strlen(replace) - strlen(prefix));
strcpy(new_path, replace);
strcat(new_path, rest);
printf("Mapped Path: %s ==> %s\n", path, new_path);
return new_path;
}
}
return path;
}
int open(const char *pathname, int flags, ...)
{
const char *new_path = fix_path(pathname);
orig_open_func_type orig_func;
orig_func = (orig_open_func_type)dlsym(RTLD_NEXT, "open");
// If O_CREAT is used to create a file, the file access mode must be given.
if (flags & O_CREAT) {
va_list args;
va_start(args, flags);
int mode = va_arg(args, int);
va_end(args);
return orig_func(new_path, flags, mode);
} else {
return orig_func(new_path, flags);
}
}
int open64(const char *pathname, int flags, ...)
{
const char *new_path = fix_path(pathname);
orig_open_func_type orig_func;
orig_func = (orig_open_func_type)dlsym(RTLD_NEXT, "open64");
// If O_CREAT is used to create a file, the file access mode must be given.
if (flags & O_CREAT) {
va_list args;
va_start(args, flags);
int mode = va_arg(args, int);
va_end(args);
return orig_func(new_path, flags, mode);
} else {
return orig_func(new_path, flags);
}
}
LD_PRELOAD
或FUSE 来完成,就像有类似的问题一样,但是我不知道任何现有的实现。