Skip to content

Commit

Permalink
Refactor lfi-run implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed May 11, 2024
1 parent 5c69212 commit 9e361d9
Show file tree
Hide file tree
Showing 26 changed files with 1,511 additions and 3,194 deletions.
7 changes: 3 additions & 4 deletions lfi-run/Knitfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ local dsrc = knit.join(
knit.glob(f"arch/$arch/*.d"),
knit.glob("core/*.d"),
knit.glob("alloc/*.d"),
knit.glob("lfi/*.d"),
knit.glob("buddy/*.d"),
knit.glob("cwalk/*.d")
)
Expand Down Expand Up @@ -60,15 +61,15 @@ local dflags = {

local flags = {
dc = dflags[dc],
ld := -Wl,--gc-sections -fno-pic
ld := -Wl,--gc-sections -fno-pic -llfi -llfiverify
}

if pagesize == "16384" then
flags.dc = flags.dc .. " " .. version(dc, "page16k")
end

return b{
$ lfi-run: $obj ../lfi-verify/target/release/liblfiverify.a
$ lfi-run: $obj
gcc $input -o $output -static $(flags.ld)

$ .unified.o: $dsrc
Expand All @@ -80,6 +81,4 @@ return b{
gcc -c $input -o $output -D$darch
$ .%.o: %.c
gcc -O2 -Wall -c $input -o $output -I.

include("../lfi-verify/Knitfile")
}
2 changes: 1 addition & 1 deletion lfi-run/core/alloc.d
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void kfree(T)(T[] arr) {
free(arr.ptr);
}

void kfree_all(T)(T[] arr) {
void kfreearray(T)(T[] arr) {
if (!arr)
return;
static if (HasDtor!(T)) {
Expand Down
83 changes: 0 additions & 83 deletions lfi-run/core/interval.d

This file was deleted.

11 changes: 6 additions & 5 deletions lfi-run/core/lib.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ void* malloc(usize size);
void* aligned_alloc(usize alignment, usize size);
void free(void* ptr);

ulong strtoull(const(char)* nptr, char** endptr, int base);

usize strlen(const(char)* s);
usize strnlen(const(char)* s, usize len);
int strncmp(const(char)* s1, const(char)* s2, usize size);
Expand All @@ -40,6 +38,7 @@ ssize read(int fd, void* buf, usize count);
ssize write(int fd, void* buf, usize count);
ssize lseek(int fd, ssize offset, int whence);
int close(int fd);
int mkdirat(int dirfd, const(char)* pathname, int mode);

void* fopen(const(char)* path, const(char)* mode);
void* fdopen(int fd, const(char)* mode);
Expand Down Expand Up @@ -68,8 +67,6 @@ void* mremap(void* old, usize old_size, usize new_size, int flags);

noreturn exit(int status);

int getpagesize();

__gshared {
extern void* stdout;
extern void* stderr;
Expand All @@ -95,7 +92,6 @@ enum {
PROT_READ = 1,
PROT_WRITE = 2,
PROT_EXEC = 4,
PROT_BTI = 16,
}

enum {
Expand All @@ -104,6 +100,11 @@ enum {
SEEK_END = 2,
}

enum {
AT_FDCWD = -100,
AT_EMPTY_PATH = 0x1000,
}

enum {
O_RDONLY = 0,
O_WRONLY = 1,
Expand Down
77 changes: 0 additions & 77 deletions lfi-run/elf.d
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
module elf;

enum {
ELF_MAGIC = 0x464C457FU, // "\x7ELF" in little endian

PT_NULL = 0,
PT_LOAD = 1,
PT_DYNAMIC = 2,
PT_INTERP = 3,
PT_NOTE = 4,
PT_SHLIB = 5,
PT_PHDR = 6,
PT_TLS = 7,
PT_NUM = 8,

ET_NONE = 0,
ET_REL = 1,
ET_EXEC = 2,
ET_DYN = 3,
ET_CORE = 4,
ET_NUM = 5,
ET_LOOS = 0xfe00,
ET_HIOS = 0xfeff,
ET_LOPROC = 0xff00,
ET_HIPROC = 0xffff,

AT_NULL = 0,
AT_IGNORE = 1,
AT_EXECFD = 2,
Expand All @@ -45,63 +22,9 @@ enum {
AT_RANDOM = 25,
AT_HWCAP2 = 17,
AT_EXECFN = 31,

PF_X = 1 << 0,
PF_W = 1 << 1,
PF_R = 1 << 2,

ELFCLASS64 = 2,

EV_CURRENT = 1,
}

struct FileHeader {
alias uword = ulong;

uint magic;
ubyte width;
ubyte[11] _elf;
ushort type;
ushort machine;
uint version_;
uword entry;
uword phoff;
uword shoff;
uint flags;
ushort ehsize;
ushort phentsize;
ushort phnum;
ushort shentsize;
ushort shnum;
ushort shstrndx;
}

struct ProgHeader {
alias uword = ulong;

uint type;
uint flags;
uword offset;
uword vaddr;
uword paddr;
uword filesz;
uword memsz;
uword align_;
}

struct Auxv {
ulong a_type;
ulong a_val;
}

char* elf_interp(ubyte* buf) {
FileHeader* hdr = cast(FileHeader*) buf;
ProgHeader* phdr = cast(ProgHeader*) (buf + hdr.phoff);

for (int x = 0; x < hdr.phnum; x++) {
if (phdr[x].type == PT_INTERP) {
return cast(char*) buf + phdr[x].offset;
}
}
return null;
}
92 changes: 92 additions & 0 deletions lfi-run/fd.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module fd;

import core.lib;
import core.alloc;

import file;
import proc;

struct FDFile {
void* dev;
usize refs;
ssize function(void*, Proc*, ubyte[]) read;
ssize function(void*, Proc*, ubyte[]) write;
ssize function(void*, Proc*, ssize, uint) lseek;
int function(void*, Proc*) close;
int function(void*, Proc*, Stat*) stat;
ssize function(void*, Proc*, void*, usize) getdents;
int function(void*) mapfd;
}

enum {
NOFILE = 128,
}

struct FDTable {
FDFile*[NOFILE] files;
}

void fdassign(FDTable* t, int fd, FDFile* ff) {
ff.refs++;
t.files[fd] = ff;
}

int fdalloc(FDTable* t) {
int i;
for (i = 0; i < t.files.length; i++) {
if (t.files[i] == null)
break;
}
if (i >= t.files.length)
return -1;
return i;
}

FDFile* fdget(FDTable* t, int fd) {
if (fdhas(t, fd)) {
return t.files[fd];
}
return null;
}

void fdrelease(FDFile* f) {
f.refs--;
if (f.refs == 0)
kfree(f);
}

bool fdremove(FDTable* t, int fd) {
if (fdhas(t, fd)) {
fdrelease(t.files[fd]);
t.files[fd] = null;
return true;
}
return false;
}

bool fdhas(FDTable* t, int fd) {
return fd >= 0 && fd < t.files.length && t.files[fd] != null;
}

void fdcopy(FDTable* t, ref FDTable to) {
assert(t.files.length == to.files.length);

for (int i = 0; i < t.files.length; i++) {
if (t.files[i] != null) {
t.files[i].refs++;
to.files[i] = t.files[i];
}
}
}

void fdclear(FDTable* t) {
for (int fd = 0; fd < t.files.length; fd++) {
fdremove(t, fd);
}
}

void fdinit(FDTable* t) {
fdassign(t, 0, filefdnew(fileno(stdin)));
fdassign(t, 1, filefdnew(fileno(stdout)));
fdassign(t, 2, filefdnew(fileno(stderr)));
}
Loading

0 comments on commit 9e361d9

Please sign in to comment.