Skip to content

Instantly share code, notes, and snippets.

@brant-ruan
Last active January 13, 2023 10:11
Show Gist options
  • Save brant-ruan/12196c36e8a740f200d4c168e83fa466 to your computer and use it in GitHub Desktop.
Save brant-ruan/12196c36e8a740f200d4c168e83fa466 to your computer and use it in GitHub Desktop.
Pawnyable LK04
// gcc fuse.c -o test -D_FILE_OFFSET_BITS=64 -static -pthread -lfuse -ldl
#define FUSE_USE_VERSION 29
#include <errno.h>
#include <fuse.h>
#include <stdio.h>
#include <string.h>
void fatal(const char *msg) {
perror(msg);
exit(1);
}
static const char *content = "Hello, World!\n";
static int getattr_callback(const char *path, struct stat *stbuf) {
puts("[+] getattr_callback");
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/file") == 0) {
stbuf->st_mode = S_IFREG | 0777;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(content);
return 0;
}
return -ENOENT;
}
static int open_callback(const char *path, struct fuse_file_info *fi) {
puts("[+] open_callback");
return 0;
}
static int read_callback(const char *path,
char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
puts("[+] read_callback");
if (strcmp(path, "/file") == 0) {
size_t len = strlen(content);
if (offset >= len)
return 0;
if ((size > len) || (offset + size > len)) {
memcpy(buf, content + offset, len - offset);
return len - offset;
} else {
memcpy(buf, content + offset, size);
return size;
}
}
return -ENOENT;
}
static struct fuse_operations fops = {
.getattr = getattr_callback,
.open = open_callback,
.read = read_callback,
};
/*
int main(int argc, char *argv[]) {
return fuse_main(argc, argv, &fops, NULL);
}
*/
int main() {
struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
struct fuse_chan *chan;
struct fuse *fuse;
if (!(chan = fuse_mount("/tmp/test", &args)))
fatal("fuse_mount");
if (!(fuse = fuse_new(chan, &args, &fops, sizeof(fops), NULL))) {
fuse_unmount("/tmp/test", chan);
fatal("fuse_new");
}
fuse_set_signal_handlers(fuse_get_session(fuse));
fuse_loop_mt(fuse);
fuse_unmount("/tmp/test", chan);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment