Skip to content

Commit 1c90fb4

Browse files
aittalamclaude
andcommitted
sandbox: minor review follow-ups on --confine-reads
- standalone llama-server: consume --confine-reads too (sibling of --unsecure), so it doesn't reach common_params_parse() and error out. - warn when --confine-reads is passed to a non-server mode, where it's a no-op, instead of silently ignoring it. - show --confine-reads (and --unsecure) in `--server --help`, which previously listed none of llamafile's own server flags. - capture errno before common_log_resume() on the pledge-failed path so the error message can't print a stale errno. - fix a stale comment on llamafile_sandbox_apply(). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 4ef1888 commit 1c90fb4

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

llama.cpp.patches/patches/tools_server_server.cpp.patch

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ diff --git a/tools/server/server.cpp b/tools/server/server.cpp
5454
common_init();
5555

5656
if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_SERVER)) {
57-
@@ -297,6 +316,85 @@ int llama_server(int argc, char ** argv) {
57+
@@ -297,6 +316,86 @@ int llama_server(int argc, char ** argv) {
5858
llama_backend_free();
5959
};
6060

@@ -122,10 +122,11 @@ diff --git a/tools/server/server.cpp b/tools/server/server.cpp
122122
+ char promises[64];
123123
+ common_log_pause(common_log_main());
124124
+ int sandbox = llamafile_sandbox_server(&spec, promises, sizeof(promises));
125+
+ int sandbox_errno = errno; // capture before resume() spawns a thread
125126
+ common_log_resume(common_log_main());
126127
+ if (sandbox == LLAMAFILE_SANDBOX_FAILED) {
127128
+ SRV_ERR("sandbox: pledge failed: %s; retry with --unsecure to disable sandboxing\n",
128-
+ strerror(errno));
129+
+ strerror(sandbox_errno));
129130
+ clean_up();
130131
+ return 1;
131132
+ } else if (llamafile_sandbox_is_active(sandbox)) {
@@ -140,7 +141,7 @@ diff --git a/tools/server/server.cpp b/tools/server/server.cpp
140141
// start the HTTP server before loading the model to be able to serve /health requests
141142
if (!ctx_http.start()) {
142143
clean_up();
143-
@@ -332,6 +430,13 @@ int llama_server(int argc, char ** argv) {
144+
@@ -332,6 +431,13 @@ int llama_server(int argc, char ** argv) {
144145
// this will unblock start_loop()
145146
ctx_server.terminate();
146147
};
@@ -154,7 +155,7 @@ diff --git a/tools/server/server.cpp b/tools/server/server.cpp
154155
}
155156

156157
// TODO: refactor in common/console
157-
@@ -368,6 +473,11 @@ int llama_server(int argc, char ** argv) {
158+
@@ -368,6 +474,11 @@ int llama_server(int argc, char ** argv) {
158159
} else {
159160
SRV_INF("server is listening on %s\n", ctx_http.listening_address.c_str());
160161

@@ -166,7 +167,7 @@ diff --git a/tools/server/server.cpp b/tools/server/server.cpp
166167
// optionally, notify router server that this instance is ready
167168
std::thread monitor_thread;
168169
if (child.is_child()) {
169-
@@ -392,5 +502,54 @@ int llama_server(int argc, char ** argv) {
170+
@@ -392,5 +503,56 @@ int llama_server(int argc, char ** argv) {
170171
}
171172
}
172173

@@ -204,6 +205,8 @@ diff --git a/tools/server/server.cpp b/tools/server/server.cpp
204205
+ // via parse_llamafile_args instead)
205206
+ if (llamafile_consume_flag(&argc, argv, "--unsecure"))
206207
+ FLAG_unsecure = true;
208+
+ if (llamafile_consume_flag(&argc, argv, "--confine-reads"))
209+
+ FLAG_confine_reads = true;
207210
+
208211
+ // Initialize GPU support early (must happen BEFORE llama_backend_init())
209212
+ // This triggers dynamic loading of GPU backends (CUDA, ROCm, Metal)

llamafile/main.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ static void print_server_help(bool show_full_list_hint) {
207207
" --host ADDR ip address to listen on (default 127.0.0.1)\n"
208208
" --port N tcp port to listen on (default 8080)\n"
209209
" --api-key KEY require this API key for authentication\n"
210+
" --confine-reads restrict file reads to the weights directories\n"
211+
" --unsecure disable pledge() sandboxing (Linux/OpenBSD)\n"
210212
"\n");
211213
if (show_full_list_hint) {
212214
printf("all other llama.cpp options are also accepted.\n"
@@ -397,7 +399,11 @@ int main(int argc, char **argv) {
397399
if (llamafile_has(argv, "--help") || llamafile_has(argv, "-h")) {
398400
switch (args.mode) {
399401
case lf::ProgramMode::SERVER:
400-
break; // fall through to llama.cpp's server help
402+
// Show llamafile's own server flags (which are stripped before
403+
// llama.cpp sees them, so its parser can't list them) before
404+
// falling through to llama.cpp's server help.
405+
print_server_help(false);
406+
break;
401407
case lf::ProgramMode::AUTO:
402408
// Combined mode: args go to the HTTP server, so show the
403409
// llamafile intro followed by llama.cpp's server option list.
@@ -469,6 +475,12 @@ int main(int argc, char **argv) {
469475
args.llama_argv = quiet_argv.data();
470476
}
471477

478+
// --confine-reads only affects the --server sandbox. In the other modes
479+
// (including the default combined mode, whose sandbox is skipped) it does
480+
// nothing, so say so rather than silently ignoring it.
481+
if (FLAG_confine_reads && args.mode != lf::ProgramMode::SERVER)
482+
fprintf(stderr, "warning: --confine-reads only applies to --server mode; ignoring\n");
483+
472484
// Initialize GPU support (triggers dynamic loading of GPU backends)
473485
llamafile_has_gpu();
474486

llamafile/sandbox.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ static int sandbox_install_pledge(const char *promises) {
126126
#endif
127127
}
128128

129-
// Installs the pledge() sandbox unconditionally (no policy gate). Threads
130-
// created after this call inherit it. Used by the unit test and by the gated
131-
// entry points below.
129+
// Installs the pledge() sandbox unconditionally, skipping the --unsecure/GPU
130+
// policy gate (the gated entry points share sandbox_install_pledge() directly
131+
// instead). Exposed for the unit test, which drives the promise sets in a
132+
// forked child without the gate.
132133
int llamafile_sandbox_apply(const char *promises) {
133134
if (!llamafile_sandbox_supported())
134135
return LLAMAFILE_SANDBOX_UNSUPPORTED;

0 commit comments

Comments
 (0)