Skip to content

Distribute and run LLMs with a single file.

Introduce pledge() / seccomp security to llama.cpp

Justine Tunney8f214602977f
diff --git a/README.md b/README.md
index f6e28801..e62d5b24 100644
--- a/README.md
+++ b/README.md
@@ -232,6 +232,42 @@ share with your friends.
recommendations of specific models, licenses, or data sets on the part
of Mozilla.)
+### Security
+
+llamafile adds pledge() and SECCOMP sandboxing to llama.cpp. This is
+enabled by default. It can be turned off by passing the `--unsecure`
+flag. Sandboxing is currently only supported on Linux and OpenBSD; on
+other platforms it'll simply log a warning.
+
+Our approach to security has these benefits:
+
+1. After it starts up, your HTTP server isn't able to access the
+ filesystem at all. This is good, since it means if someone discovers
+ a bug in the llama.cpp server, then it's much less likely they'll be
+ able to access sensitive information on your machine or make changes
+ to its configuration. On Linux, we're able to sandbox things even
+ further; the only networking related system call the HTTP server will
+ allowed to use after starting up, is accept(). That further limits an
+ attacker's ability to exfiltrate information, in the event that your
+ HTTP server is compromised.
+
+2. The main CLI command won't be able to access the network at all. This
+ is enforced by the operating system kernel. It also won't be able to
+ write to the file system. This keeps your computer safe in the event
+ that a bug is ever discovered in the the GGUF file format that lets
+ an attacker craft malicious weights files and post them online. The
+ only exception to this rule is if you pass the `--prompt-cache` flag
+ without also specifying `--prompt-cache-ro`. In that case, security
+ currently needs to be weakened to allow `cpath` and `wpath` access,
+ but network access will remain forbidden.
+
+Therefore your llamafile is able to protect itself against the outside
+world, but that doesn't mean you're protected from llamafile. Sandboxing
+is self-imposed. If you obtained your llamafile from an untrusted source
+then its author could have simply modified it to not do that. In that
+case, you can run the untrusted llamafile inside another sandbox, such
+as a virtual machine, to make sure it behaves how you expect.
+
## zipalign documentation
```
diff --git a/llama.cpp/README.llamafile b/llama.cpp/README.llamafile
index d0acce77..29ae873f 100644
--- a/llama.cpp/README.llamafile
+++ b/llama.cpp/README.llamafile
@@ -30,6 +30,7 @@ LOCAL MODIFICATIONS
- Refactor ggml.c, llama.cpp, and llava to use llamafile_open() APIs
- Add support to main() programs for Cosmo /zip/.args files
- Use condition variables for server rather than busy loop
+ - Introduce pledge() SECCOMP sandboxing to improve security
- Remove log callback pointer API from Metal GPU module
- Use runtime dispatching for matmul quants
- Remove operating system #ifdef statements
diff --git a/llama.cpp/common.cpp b/llama.cpp/common.cpp
index aeb6be10..74592344 100644
--- a/llama.cpp/common.cpp
+++ b/llama.cpp/common.cpp
@@ -504,6 +504,8 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
params.instruct = true;
} else if (arg == "--infill") {
params.infill = true;
+ } else if (arg == "--unsecure") {
+ params.unsecure = true;
} else if (arg == "--multiline-input") {
params.multiline_input = true;
} else if (arg == "--simple-io") {
@@ -863,6 +865,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
printf(" draft model for speculative decoding (default: %s)\n", params.model.c_str());
printf(" -ld LOGDIR, --logdir LOGDIR\n");
printf(" path under which to save YAML logs (no logging if unset)\n");
+ printf(" --unsecure disables pledge() sandboxing on Linux and OpenBSD\n");
printf("\n");
#ifndef LOG_DISABLE_LOGS
log_print_usage();
diff --git a/llama.cpp/common.h b/llama.cpp/common.h
index dd6b002e..bebe5b82 100644
--- a/llama.cpp/common.h
+++ b/llama.cpp/common.h
@@ -122,6 +122,8 @@ struct gpt_params {
bool verbose_prompt = false; // print prompt tokens before generation
bool infill = false; // use infill mode
+ bool unsecure = false; // disable pledge() sandboxing
+
// multimodal models (see examples/llava)
std::string mmproj = ""; // path to multimodal projector
std::string image = ""; // path to an image file
diff --git a/llama.cpp/llama.cpp b/llama.cpp/llama.cpp
index ba16e561..7f222da9 100644
--- a/llama.cpp/llama.cpp
+++ b/llama.cpp/llama.cpp
@@ -754,7 +754,7 @@ struct llama_mmap {
int fd = fileno(llamafile_fp(file->file));
// prefetch/readahead impairs performance on NUMA systems
if (numa) { prefetch = 0; }
- addr = mmap(NULL, size, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0);
+ addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
ThrowRuntimeError(format("mmap failed: %s", strerror(errno)));
}
diff --git a/llama.cpp/main/main.cc b/llama.cpp/main/main.cc
index a1407f63..53f88335 100644
--- a/llama.cpp/main/main.cc
+++ b/llama.cpp/main/main.cc
@@ -130,6 +130,29 @@ int main(int argc, char ** argv) {
atexit([]() { console::cleanup(); });
is_terminal = isatty(2);
+ if (!params.unsecure) {
+ // Enable pledge() security on Linux and OpenBSD.
+ // - We do this *after* opening the log file for writing.
+ // - We do this *before* loading any weights or graphdefs.
+ // In effect, what this does is:
+ // - We'll no longer be able to talk to the network going forward.
+ // - We'll no longer be able to change the filesystem going forward.
+ // Cosmopolitan Libc implements pledge() on Linux using SECCOMP.
+ const char *promises;
+ if (!params.path_prompt_cache.empty() && !params.prompt_cache_ro) {
+ // TODO(jart): Open prompt cache in RW mode before pledge()
+ promises = "stdio rpath tty cpath wpath";
+ } else {
+ promises = "stdio rpath tty";
+ }
+ if (pledge(0, 0)) {
+ LOG_TEE("warning: this OS doesn't support pledge() security\n");
+ } else if (pledge(promises, 0)) {
+ perror("pledge");
+ exit(1);
+ }
+ }
+
if (params.logits_all) {
printf("\n************\n");
printf("%s: please use the 'perplexity' tool for perplexity calculations\n", __func__);
diff --git a/llama.cpp/server/server.cpp b/llama.cpp/server/server.cpp
index ff3abd06..ba2b118c 100644
--- a/llama.cpp/server/server.cpp
+++ b/llama.cpp/server/server.cpp
@@ -38,6 +38,7 @@ struct server_params
int32_t read_timeout = 600;
int32_t write_timeout = 600;
bool nobrowser = false;
+ bool unsecure = false;
};
static bool server_verbose = false;
@@ -1842,6 +1843,7 @@ static void server_print_usage(const char *argv0, const gpt_params &params,
printf(" Set a file to load a system prompt (initial prompt of all slots), this is useful for chat applications.\n");
printf(" --mmproj MMPROJ_FILE path to a multimodal projector file for LLaVA.\n");
printf(" --nobrowser Do not attempt to open a web browser tab at startup.\n");
+ printf(" --unsecure disables pledge() sandboxing on Linux and OpenBSD\n");
printf("\n");
}
@@ -2182,6 +2184,10 @@ static void server_params_parse(int argc, char **argv, server_params &sparams,
{
sparams.nobrowser = true;
}
+ else if (arg == "--unsecure")
+ {
+ sparams.unsecure = true;
+ }
else
{
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
@@ -2500,10 +2506,7 @@ static void append_to_generated_text_from_generated_token_probs(llama_server_con
int main(int argc, char **argv)
{
llamafile_check_cpu();
-
-#ifdef __COSMOPOLITAN__
LoadZipArgs(&argc, &argv);
-#endif
// own arguments required by this example
gpt_params params;
@@ -2880,6 +2883,31 @@ int main(int argc, char **argv)
llamafile_launch_browser(url);
}
+ if (!sparams.unsecure) {
+ // Enables pledge() security on Linux and OpenBSD.
+ // - We do this *after* binding the server socket.
+ // - We do this *after* opening the log file for writing.
+ // - We do this *after* opening a tab in the user browser.
+ // - We do this *before* loading any weights or graphdefs.
+ // In effect, what this does is:
+ // - Filesystem access is disabled entirely (except ZipOS).
+ // - On Linux, network access is restricted to accept() only.
+ // Cosmopolitan Libc implements pledge() on Linux using SECCOMP.
+ const char *promises;
+ if (IsOpenbsd()) {
+ promises = "stdio inet";
+ } else {
+ promises = "stdio anet";
+ }
+ __pledge_mode = PLEDGE_PENALTY_RETURN_EPERM;
+ if (pledge(0, 0)) {
+ LOG_TEE("warning: this OS doesn't support pledge() security\n");
+ } else if (pledge(promises, 0)) {
+ perror("pledge");
+ exit(1);
+ }
+ }
+
tinyprint(2, "loading weights...\n", NULL);
LOG_INFO("HTTP server listening", {
@@ -2888,8 +2916,10 @@ int main(int argc, char **argv)
});
// run the HTTP server in a thread - see comment below
+ fprintf(stderr, "run listen thread...\n");
std::thread t([&]()
{
+ fprintf(stderr, "listen_after_bind...\n");
if (!svr.listen_after_bind())
{
return 1;
diff --git a/llamafile/cuda.c b/llamafile/cuda.c
index b7a14374..80ef4efc 100644
--- a/llamafile/cuda.c
+++ b/llamafile/cuda.c
@@ -358,6 +358,11 @@ static bool CompileNativeCuda(char dso[static PATH_MAX]) {
static bool ImportCudaImpl(void) {
+ // No dynamic linking support on OpenBSD yet.
+ if (IsOpenbsd()) {
+ return false;
+ }
+
// get native cuda dll if possible
char dso[PATH_MAX];
if (!CompileNativeCuda(dso)) {