Skip to content

Commit 2025597

Browse files
committed
MDEV-21778 Disable system commands in mysql/mariadb client
mysql --sandbox disables system (\!), tee (\T), pager with an argument(\P foo), source (\.) does *not* disable edit (\e). Use EDITOR=/bin/false to disable or, for example, EDITOR=rnano for something more useful does *not* disable pager (\P) without an argument. Use PAGER=cat or, for example PAGER=less LESSSECURE=1 for something more useful using a disabled command is an error, which can be ignored with --force Also, a "sandbox" command (\-) - enables the sandbox mode until EOF (current file or the session, if interactive)
1 parent 83aedea commit 2025597

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

client/mysql.cc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ typedef struct st_status
136136
ulong query_start_line;
137137
char *file_name;
138138
LINE_BUFFER *line_buff;
139-
bool batch,add_to_history;
139+
bool batch, add_to_history, sandbox;
140140
} STATUS;
141141

142142

@@ -154,7 +154,7 @@ static my_bool ignore_errors=0,wait_flag=0,quick=0,
154154
vertical=0, line_numbers=1, column_names=1,opt_html=0,
155155
opt_xml=0,opt_nopager=1, opt_outfile=0, named_cmds= 0,
156156
tty_password= 0, opt_nobeep=0, opt_reconnect=1,
157-
opt_secure_auth= 0,
157+
opt_secure_auth= 0,
158158
default_pager_set= 0, opt_sigint_ignore= 0,
159159
auto_vertical_output= 0,
160160
show_warnings= 0, executing_query= 0,
@@ -235,7 +235,8 @@ static int com_quit(String *str,char*),
235235
com_rehash(String *str, char*), com_tee(String *str, char*),
236236
com_notee(String *str, char*), com_charset(String *str,char*),
237237
com_prompt(String *str, char*), com_delimiter(String *str, char*),
238-
com_warnings(String *str, char*), com_nowarnings(String *str, char*);
238+
com_warnings(String *str, char*), com_nowarnings(String *str, char*),
239+
com_sandbox(String *str, char*);
239240

240241
#ifdef USE_POPEN
241242
static int com_nopager(String *str, char*), com_pager(String *str, char*),
@@ -311,6 +312,8 @@ static COMMANDS commands[] = {
311312
{ "prompt", 'R', com_prompt, 1, "Change your mysql prompt."},
312313
{ "quit", 'q', com_quit, 0, "Quit mysql." },
313314
{ "rehash", '#', com_rehash, 0, "Rebuild completion hash." },
315+
{ "sandbox", '-', com_sandbox, 0,
316+
"Disallow commands that access the file system (except \\P without an argument and \\e)." },
314317
{ "source", '.', com_source, 1,
315318
"Execute an SQL script file. Takes a file name as an argument."},
316319
{ "status", 's', com_status, 0, "Get status information from the server."},
@@ -1675,6 +1678,8 @@ static struct my_option my_long_options[] =
16751678
&safe_updates, &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
16761679
{"i-am-a-dummy", 'U', "Synonym for option --safe-updates, -U.",
16771680
&safe_updates, &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1681+
{"sandbox", 0, "Disallow commands that access the file system (except \\P without an argument and \\e).",
1682+
&status.sandbox, &status.sandbox, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
16781683
{"secure-auth", 0, "Refuse client connecting to server if it"
16791684
" uses old (pre-4.1.1) protocol.", &opt_secure_auth,
16801685
&opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
@@ -4146,6 +4151,8 @@ static int com_tee(String *, char *line)
41464151
{
41474152
char file_name[FN_REFLEN], *end, *param;
41484153

4154+
if (status.sandbox)
4155+
return put_info("Not allowed in the sandbox mode", INFO_ERROR, 0);
41494156
if (status.batch)
41504157
return 0;
41514158
while (my_isspace(charset_info, *line))
@@ -4226,6 +4233,8 @@ static int com_pager(String *, char *line)
42264233
}
42274234
else
42284235
{
4236+
if (status.sandbox)
4237+
return put_info("Not allowed in the sandbox mode", INFO_ERROR, 0);
42294238
end= strmake_buf(pager_name, param);
42304239
while (end > pager_name && (my_isspace(charset_info,end[-1]) ||
42314240
my_iscntrl(charset_info,end[-1])))
@@ -4321,6 +4330,9 @@ static int com_shell(String *, char *line)
43214330
{
43224331
char *shell_cmd;
43234332

4333+
if (status.sandbox)
4334+
return put_info("Not allowed in the sandbox mode", INFO_ERROR, 0);
4335+
43244336
/* Skip space from line begin */
43254337
while (my_isspace(charset_info, *line))
43264338
line++;
@@ -4416,6 +4428,9 @@ static int com_source(String *, char *line)
44164428
FILE *sql_file;
44174429
my_bool save_ignore_errors;
44184430

4431+
if (status.sandbox)
4432+
return put_info("Not allowed in the sandbox mode", INFO_ERROR, 0);
4433+
44194434
/* Skip space from file name */
44204435
while (my_isspace(charset_info,*line))
44214436
line++;
@@ -4450,6 +4465,7 @@ static int com_source(String *, char *line)
44504465
bfill((char*) &status,sizeof(status),(char) 0);
44514466

44524467
status.batch=old_status.batch; // Run in batch mode
4468+
status.sandbox=old_status.sandbox;
44534469
status.line_buff=line_buff;
44544470
status.file_name=source_name;
44554471
glob_buffer.length(0); // Empty command buffer
@@ -4571,6 +4587,13 @@ static int com_use(String *, char *line)
45714587
return 0;
45724588
}
45734589

4590+
static int com_sandbox(String *, char *)
4591+
{
4592+
status.sandbox= 1;
4593+
put_info("Sandbox mode.", INFO_INFO);
4594+
return 0;
4595+
}
4596+
45744597
static int com_warnings(String *, char *)
45754598
{
45764599
show_warnings = 1;

mysql-test/main/mysql.result

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,27 @@ drop table t1;
633633
WARNING: option '--enable-cleartext-plugin' is obsolete.
634634
1
635635
1
636+
# End of 10.3 tests
637+
#
638+
# MDEV-21778 Disable system commands in mysql/mariadb client
639+
#
640+
ERROR at line 1: Not allowed in the sandbox mode
641+
1
642+
ERROR at line 1: Not allowed in the sandbox mode
643+
2
644+
ERROR at line 1: Not allowed in the sandbox mode
645+
3
646+
1
647+
entering sandbox
648+
system
649+
tee
650+
source
651+
^^^
652+
2
653+
entering sandbox
654+
system
655+
tee
656+
source
657+
^^^
658+
3
659+
# End of 10.5 tests

mysql-test/main/mysql.test

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,43 @@ drop table t1;
716716
#
717717
--echo
718718
--exec $MYSQL test --enable-cleartext-plugin -e "select 1"
719+
720+
--echo # End of 10.3 tests
721+
722+
--echo #
723+
--echo # MDEV-21778 Disable system commands in mysql/mariadb client
724+
--echo #
725+
726+
--error 1
727+
--exec $MYSQL --sandbox -Ne "select 1; \! echo foo; select 0" 2>&1
728+
--error 1
729+
--exec $MYSQL --sandbox -Ne "select 2; \T echo foo; select 0" 2>&1
730+
--error 1
731+
--exec $MYSQL --sandbox -Ne "select 3; \. echo.foo; select 0" 2>&1
732+
733+
--write_file $MYSQL_TMP_DIR/mysql_in
734+
select 'entering sandbox';
735+
\-
736+
select 'system';
737+
\! echo foo
738+
select 'tee';
739+
\T echo foo
740+
select 'source';
741+
\. echo.foo
742+
select '^^^';
743+
EOF
744+
745+
write_line "select 1;
746+
source $MYSQL_TMP_DIR/mysql_in;
747+
select 2;
748+
source $MYSQL_TMP_DIR/mysql_in;
749+
sandbox;
750+
select 3;
751+
source $MYSQL_TMP_DIR/mysql_in;" $MYSQL_TMP_DIR/mysql_in2;
752+
753+
--exec $MYSQL -fN <$MYSQL_TMP_DIR/mysql_in2
754+
755+
--remove_file $MYSQL_TMP_DIR/mysql_in
756+
--remove_file $MYSQL_TMP_DIR/mysql_in2
757+
758+
--echo # End of 10.5 tests

0 commit comments

Comments
 (0)