blob: e6a07b613b8d02b38e8903cbee5c47eaf9e69556 [file]
#pragma once
#include "arch_callback.h"
#include "boxmap.h"
#include "lfi_arch.h"
#include "lfi_core.h"
#include "lfiv.h"
#include "log.h"
#include "mmap.h"
#include <signal.h>
#include <threads.h>
#define EXPORT __attribute__((visibility("default")))
struct LFIEngine {
struct BoxMap *bm;
struct LFIOptions opts;
struct LFIVerifier verifier;
void (*sys_handler)(struct LFIContext *ctx);
struct LFIContext *(*clone_cb)(struct LFIBox *box);
stack_t altstack;
};
struct LFIBox {
// Non-zero protection key if pku is enabled (zero if disabled).
int pkey;
// Address space information.
uintptr_t base;
size_t size;
lfiptr min; // Smallest valid address (up to guard region)
lfiptr max; // Largest valid address (up to guard region)
lfiptr max_exec; // Largest valid executable address
// Memory mapper object from libmmap.
struct MMAddrSpace mm;
// Pointer to the page at the start of the sandbox holding runtime call
// entrypoints.
void *sys_page;
struct Sys *sys;
// Address of return function in this sandbox.
lfiptr retaddr;
struct CallbackInfo cbinfo;
void *callbacks[MAXCALLBACKS];
struct LFIEngine *engine;
void *userdata;
};
struct Sys {
uintptr_t rtcalls[32];
};
struct LFIContext {
// Registers of sandbox and associated host thread (stack, thread pointer)
// are stored here.
struct LFIRegs regs;
// Set to 1 if a lfi_ret_end should be automatically called when returning
// from a callback to abort further sandbox execution of the callback.
uint64_t abort_callback;
// Set to 1 if the most recent callback was aborted.
uint64_t abort_status;
#ifdef CTXREG
// Context register storage. The first slot holds a pointer to this
// LFIContext, and remaining slots are available for thread-local data
// (e.g., thread pointer).
uint64_t ctxreg[8];
#endif
// User-provided data pointer -- tracks per-sandbox context for Linux
// runtime.
void *userdata;
// Sandbox that this context is associated with.
struct LFIBox *box;
};
extern thread_local int lfi_error;
extern thread_local char *lfi_error_desc;
void lfi_box_cb_free(struct LFIBox *box);
static inline size_t
kb(size_t x)
{
return x * 1024;
}
static inline size_t
mb(size_t x)
{
return x * 1024 * 1024;
}
static inline size_t
gb(size_t x)
{
return x * 1024 * 1024 * 1024;
}
// Guard sizes for sandbox isolation. These are configurable per-architecture.
//
// BOX_INTERNAL_GUARD: space at the end of each sandbox (within the box's
// allocated size) that is not usable by sandbox code. This reduces the
// sandbox's usable address range.
//
// BOX_EXTERNAL_GUARD: additional space allocated beyond each sandbox's size,
// increasing the per-sandbox footprint. This provides isolation between
// adjacent sandboxes when hardware masking alone is insufficient.
//
// REGION_GUARD: space reserved on each side of the entire sandbox pool region,
// protecting against accesses escaping the pool.
#if defined(LFI_ARCH_ARM64)
#define BOX_INTERNAL_GUARD kb(192)
#define BOX_EXTERNAL_GUARD 0
#define REGION_GUARD mb(512)
#elif defined(LFI_ARCH_X64)
#define BOX_INTERNAL_GUARD 0
#ifdef HAVE_PKU
#define BOX_EXTERNAL_GUARD 0
#define REGION_GUARD 0
#else
// TODO: verify that we can safely reduce BOX_EXTERNAL_GUARD if Segue is
// enabled.
#define BOX_EXTERNAL_GUARD gb(40)
#define REGION_GUARD gb(40)
#endif
#elif defined(LFI_ARCH_RISCV64)
#define BOX_INTERNAL_GUARD 0
#define BOX_EXTERNAL_GUARD 0
#define REGION_GUARD 0
#else
#error "invalid architecture"
#endif
// Return the total amount of virtual address space needed for a sandbox of a
// certain size. This is the sandbox size plus any external guard.
static inline size_t
box_footprint(size_t boxsize)
{
return boxsize + BOX_EXTERNAL_GUARD;
}