Skip to content

Commit 4015177

Browse files
committed
SDL: Add option to drop privileges with pledge()
1 parent 8ab757e commit 4015177

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ if(NOT LIBMGBA_ONLY)
3131
set(USE_EDITLINE ON CACHE BOOL "Whether or not to enable the CLI-mode debugger")
3232
endif()
3333
set(USE_GDB_STUB ON CACHE BOOL "Whether or not to enable the GDB stub ARM debugger")
34+
set(USE_PLEDGE OFF CACHE BOOL "Whether or not to drop privileges with pledge")
3435
set(USE_FFMPEG ON CACHE BOOL "Whether or not to enable FFmpeg support")
3536
set(USE_ZLIB ON CACHE BOOL "Whether or not to enable zlib support")
3637
set(USE_MINIZIP ON CACHE BOOL "Whether or not to enable external minizip support")
@@ -473,6 +474,10 @@ find_feature(USE_SQLITE3 "sqlite3")
473474
find_feature(USE_ELF "libelf")
474475
find_feature(ENABLE_PYTHON "PythonLibs")
475476

477+
if(USE_PLEDGE)
478+
set(USE_EPOXY OFF)
479+
endif()
480+
476481
if(USE_FFMPEG)
477482
set(USE_LIBAVRESAMPLE ON)
478483
set(USE_LIBSWRESAMPLE ON)
@@ -504,6 +509,10 @@ if(USE_GDB_STUB)
504509
endif()
505510
source_group("Debugger" FILES ${DEBUGGER_SRC})
506511

512+
if(USE_PLEDGE)
513+
list(APPEND FEATURES PLEDGE)
514+
endif()
515+
507516
if(USE_FFMPEG)
508517
list(APPEND FEATURES FFMPEG)
509518
if(USE_LIBSWRESAMPLE)
@@ -1221,6 +1230,7 @@ if(NOT QUIET AND NOT LIBMGBA_ONLY)
12211230
message(STATUS " CLI debugger: ${USE_EDITLINE}")
12221231
endif()
12231232
message(STATUS " GDB stub: ${USE_GDB_STUB}")
1233+
message(STATUS " pledge: ${USE_PLEDGE}")
12241234
message(STATUS " GIF/Video recording: ${USE_FFMPEG}")
12251235
message(STATUS " Screenshot/advanced savestate support: ${USE_PNG}")
12261236
message(STATUS " ZIP support: ${SUMMARY_ZIP}")

src/platform/sdl/main.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ static void mSDLDeinit(struct mSDLRenderer* renderer);
4444

4545
static int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args);
4646

47+
#ifdef USE_PLEDGE
48+
static bool mPledgeBroad(struct mArguments* args);
49+
static bool mPledgeNarrow(struct mArguments* args);
50+
#endif
51+
4752
static struct VFile* _state = NULL;
4853

4954
static void _loadState(struct mCoreThread* thread) {
@@ -149,6 +154,15 @@ int main(int argc, char** argv) {
149154
renderer.player.bindings = &renderer.core->inputMap;
150155
mSDLInitBindingsGBA(&renderer.core->inputMap);
151156
mSDLInitEvents(&renderer.events);
157+
158+
#ifdef USE_PLEDGE
159+
if (!mPledgeBroad(&args)) {
160+
freeArguments(&args);
161+
fprintf(stderr, "pledge\n");
162+
return 1;
163+
}
164+
#endif
165+
152166
mSDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&renderer.core->config));
153167
mSDLAttachPlayer(&renderer.events, &renderer.player);
154168
mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&renderer.core->config));
@@ -264,6 +278,12 @@ int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
264278
state->close(state);
265279
}
266280
}
281+
#ifdef USE_PLEDGE
282+
if (!mPledgeNarrow(args)) {
283+
didFail = true;
284+
fprintf(stderr, "pledge\n");
285+
}
286+
#endif
267287
renderer->runloop(renderer, &thread);
268288
mSDLPauseAudio(&renderer->audio);
269289
if (mCoreThreadHasCrashed(&thread)) {
@@ -312,3 +332,43 @@ static void mSDLDeinit(struct mSDLRenderer* renderer) {
312332

313333
SDL_Quit();
314334
}
335+
336+
#ifdef USE_PLEDGE
337+
static bool mPledgeBroad(struct mArguments *args) {
338+
if (args->debuggerType == DEBUGGER_CLI) {
339+
if (pledge("stdio rpath wpath cpath inet fattr unix dns sendfd prot_exec tty drm audio", NULL) == -1) {
340+
return false;
341+
}
342+
#ifdef USE_GDB_STUB
343+
} else if (args->debuggerType == DEBUGGER_GDB) {
344+
if (pledge("stdio rpath wpath cpath inet fattr unix dns sendfd prot_exec drm audio", NULL) == -1) {
345+
return false;
346+
}
347+
#endif
348+
} else {
349+
if (pledge("stdio rpath wpath cpath inet fattr unix dns sendfd prot_exec drm audio", NULL) == -1) {
350+
return false;
351+
}
352+
}
353+
return true;
354+
}
355+
356+
static bool mPledgeNarrow(struct mArguments *args) {
357+
if (args->debuggerType == DEBUGGER_CLI) {
358+
if (pledge("stdio rpath wpath cpath fattr sendfd tty prot_exec drm audio", NULL) == -1) {
359+
return false;
360+
}
361+
#ifdef USE_GDB_STUB
362+
} else if (args->debuggerType == DEBUGGER_GDB) {
363+
if (pledge("stdio rpath wpath cpath inet fattr sendfd prot_exec drm audio", NULL) == -1) {
364+
return false;
365+
}
366+
#endif
367+
} else {
368+
if (pledge("stdio rpath wpath cpath fattr sendfd prot_exec drm audio", NULL) == -1) {
369+
return false;
370+
}
371+
}
372+
return true;
373+
}
374+
#endif

0 commit comments

Comments
 (0)