Skip to content

Commit 482cde6

Browse files
authored
Fix #45 - Enable sandbox by default -g to setup the granularity
1 parent f05e404 commit 482cde6

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/main.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void r2mcp_help(void) {
3838
" -d [pdc] select a different decompiler (pdc by default)\n"
3939
" -D [tool] disable the specified tool (repeatable)\n"
4040
" -e [tool] enable only the specified tool (repeatable)\n"
41+
" -g [grain] set cfg.sandbox.grain (default: exec,socket; use all to disable sandbox)\n"
4142
" -h show this help\n"
4243
" -i ignore analysis level specified in analyze calls\n"
4344
" -l [file] append debug logs to this file\n"
@@ -82,6 +83,7 @@ int r2mcp_main(int argc, const char **argv) {
8283
char *baseurl = NULL;
8384
char *svc_baseurl = NULL;
8485
char *sandbox = NULL;
86+
char *sandbox_grain = strdup ("exec,socket");
8587
char *logfile = NULL;
8688
char *prompts_dir = NULL;
8789
bool load_prompts = true;
@@ -90,7 +92,7 @@ int r2mcp_main(int argc, const char **argv) {
9092
const char *dsl_tests = NULL;
9193
RList *disabled_tools = NULL;
9294
RGetopt opt;
93-
r_getopt_init (&opt, argc, argv, "hmvpd:nc:u:l:s:rite:D:RT:S:P:NL");
95+
r_getopt_init (&opt, argc, argv, "hmvpd:nc:u:g:l:s:rite:D:RT:S:P:NL");
9496
int c;
9597
while ((c = r_getopt_next (&opt)) != -1) {
9698
switch (c) {
@@ -111,6 +113,10 @@ int r2mcp_main(int argc, const char **argv) {
111113
baseurl = strdup (opt.arg);
112114
R_LOG_INFO ("[R2MCP] HTTP r2pipe client mode enabled, baseurl=%s", baseurl);
113115
break;
116+
case 'g':
117+
free (sandbox_grain);
118+
sandbox_grain = strdup (opt.arg);
119+
break;
114120
case 'l':
115121
logfile = strdup (opt.arg);
116122
break;
@@ -205,6 +211,7 @@ int r2mcp_main(int argc, const char **argv) {
205211
.baseurl = baseurl,
206212
.svc_baseurl = svc_baseurl,
207213
.sandbox = sandbox,
214+
.sandbox_grain = sandbox_grain,
208215
.logfile = logfile,
209216
.prompts_dir = prompts_dir,
210217
.load_prompts = load_prompts,
@@ -269,6 +276,7 @@ int r2mcp_main(int argc, const char **argv) {
269276
free (ss.baseurl);
270277
free (ss.svc_baseurl);
271278
free (ss.sandbox);
279+
free (ss.sandbox_grain);
272280
free (ss.logfile);
273281
free (ss.prompts_dir);
274282
if (ss.enabled_tools) {
@@ -294,6 +302,7 @@ int r2mcp_main(int argc, const char **argv) {
294302
/* Cleanup */
295303
free (ss.baseurl);
296304
free (ss.sandbox);
305+
free (ss.sandbox_grain);
297306
free (ss.logfile);
298307
free (ss.prompts_dir);
299308
if (ss.enabled_tools) {

src/r2api.inc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ static void r2state_settings(RCore *core) {
2121
r_config_set_i (core->config, "scr.limit", 16768);
2222
}
2323

24+
static void r2state_sandbox_settings(ServerState *ss, RCore *core) {
25+
const char *sandbox_grain = (ss && ss->sandbox_grain)? ss->sandbox_grain: "exec,socket";
26+
if (!strcmp (sandbox_grain, "all")) {
27+
r_config_set_b (core->config, "cfg.sandbox", false);
28+
} else {
29+
r_config_set_b (core->config, "cfg.sandbox", true);
30+
r_config_set (core->config, "cfg.sandbox.grain", sandbox_grain);
31+
}
32+
}
33+
2434
static bool logcb(void *user, int type, const char *origin, const char *msg) {
2535
if (type > R_LOG_LEVEL_WARN) {
2636
return false;
@@ -191,6 +201,7 @@ R_IPI bool r2_open_file(ServerState *ss, const char *filepath) {
191201
R_LOG_ERROR ("Failed to initialize r2 core\n");
192202
return false;
193203
}
204+
r_config_set_b (core->config, "cfg.sandbox", false);
194205

195206
if (ss->rstate.file_opened) {
196207
R_LOG_INFO ("Closing previously opened file: %s", ss->rstate.current_file);
@@ -230,6 +241,7 @@ R_IPI bool r2_open_file(ServerState *ss, const char *filepath) {
230241
free (ss->rstate.current_file);
231242
ss->rstate.current_file = strdup (filepath);
232243
ss->rstate.file_opened = true;
244+
r2state_sandbox_settings (ss, core);
233245
R_LOG_INFO ("File opened successfully: %s", filepath);
234246

235247
return true;

src/r2mcp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ typedef struct {
5252
char *svc_baseurl;
5353
/* Optional sandbox path. When set, only allow opening files under this dir */
5454
char *sandbox;
55+
/* Optional radare2 sandbox grain mask; "all" disables cfg.sandbox */
56+
char *sandbox_grain;
5557
/* Optional path to append debug logs when set via -l */
5658
char *logfile;
5759
/* Optional custom prompts directory path */

0 commit comments

Comments
 (0)