Better error message when script fails due to script-security setting
PATCH 1/1 Better error message when script fails due to script-security setting
From: Selva Nair <[email protected]...>
- Add a new return value (-2) for openvpn_execve() when external program execution is not allowed due to a low script-security setting.
- Add a corresponding error message
Errors and warnings in such cases will now display as "WARNING: failed running command (<cmd>) :" followed by
"disallowed by script-security setting" on all platforms
instead of the current
"external program did not execute -- returned error code -1" on Windows and
"external program fork failed" on other platforms.
The error is FATAL for some scripts and that behaviour is unchanged.
This helps the Windows GUI to detect when a connection failure results from a safer script-security setting enforced by the GUI, and show a relevant message.
---
This is being presented as a better alternative for patch 684. A separate patch may be needed for 2.4 -- will do if this is found acceptable.
src/openvpn/run_command.c | 25 +++++++++++++++++++++---- src/openvpn/win32.c | 10 +++++++--- 2 files changed, 28 insertions(+), 7 deletions(-)
src/openvpn/run_command.c +21 -4
@@ -65,12 +65,23 @@ system_error_message(int stat, struct gc_arena *gc) { buf_printf(&out, "external program did not execute -- "); } -buf_printf (&out,"returned error code %d" , stat); + if (stat == -2) + { + buf_printf(&out, "disallowed by script-security setting"); + } + else + { + buf_printf(&out, "returned error code %d", stat); + } #else /* ifdef _WIN32 */ if (stat == -1) { buf_printf(&out, "external program fork failed"); } + else if (stat == -2) + { + buf_printf(&out, "disallowed by script-security setting"); + } else if (!WIFEXITED(stat)) { buf_printf(&out, "external program did not exit normally");
@@ -114,6 +125,8 @@ openvpn_execve_allowed(const unsigned int flags)* Run execve() inside a fork(). Designed to replicate the semantics of system() but * in a safer way that doesn't require the invocation of a shell or the risks * associated with formatting and parsing a command line. +* Returns the exit status of child, -2 if openvpn_execve_allowed() returned false, +* or -1 on other errors. */ int openvpn_execve (const struct argv *a,const struct env_set *es,const unsigned int flags)
@@ -150,10 +163,14 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned in } } } -else if (!warn_shown && (script_security () <SSEC_SCRIPTS )) +else { -msg (M_WARN ,SCRIPT_SECURITY_WARNING ); - warn_shown = true; + ret =-2 ; +if (!warn_shown && (script_security () <SSEC_SCRIPTS )) + { +msg (M_WARN ,SCRIPT_SECURITY_WARNING ); + warn_shown = true; + } }#else /* if defined(ENABLE_FEATURE_EXECVE) */ msg (M_WARN ,"openvpn_execve: execve function not available" );
src/openvpn/win32.c +7 -3
@@ -1137,10 +1137,14 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned infree (env);gc_free (&gc); } -else if (!exec_warn && (script_security () <SSEC_SCRIPTS )) +else { -msg (M_WARN ,SCRIPT_SECURITY_WARNING ); - exec_warn = true; + ret =-2 ; +if (!exec_warn && (script_security () <SSEC_SCRIPTS )) + { +msg (M_WARN ,SCRIPT_SECURITY_WARNING ); + exec_warn = true; + } } }else
Signature · 1 line
2.1.4
Hi,
Quoted 7 lines · On Sat, Feb 16, 2019 at 08:55:41PM -0500, [email protected]... wrote:
From: Selva Nair <[email protected]...>
- Add a new return value (-2) for
openvpn_execve()when external program execution is not allowed due to a low script-security setting.- Add a corresponding error message
I find this a useful way forward (because it will not introduce new FATALs where we had none before, just make the errors more clear).
Since David had reservations on the other patch, I want to have explicit agreement from him before going on - and will poke on IRC to make sure I get attention.
Nevertheless, as we agreed to tag 2.4.7 *today* to make the release tomorrow and meet the Debian deadline, I will not include this in 2.4.7.
We can always do a 2.4.8 release "really soon", and include this (plus other potential 2.4 material which is important for Windows, like the OpenSSL 1.1.1 / cryptoapi stuff).
"Release early, release often" used to be the mantra... :-)
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Hi
Quoted 9 lines · Gert Doering
Hi,
Quoted 6 lines · On Sat, Feb 16, 2019 at 08:55:41PM -0500, [email protected]... wrote:
From: Selva Nair <[email protected]...>
- Add a new return value (-2) for
openvpn_execve()when external program execution is not allowed due to a low script-security setting.
Quoted 9 lines
Quoted 1 line
- Add a corresponding error message
I find this a useful way forward (because it will not introduce new FATALs where we had none before, just make the errors more clear).
Since David had reservations on the other patch, I want to have explicit agreement from him before going on - and will poke on IRC to make sure I get attention.
Just to be explicit:
Since David pointed out the "regressive" nature of the earlier patch, I too have
reservations about it, and prefer this approach.
Nevertheless, as we agreed to tag 2.4.7 *today* to make the release tomorrow
Quoted 1 line
and meet the Debian deadline, I will not include this in 2.4.7.
The planned script-security over-ride feature in the GUI is pretty intrusive. Taking
time to give it more thought, and preferably get some user feedback, is not a bad thing.
Selva
Quoted 25 lines · On 17/02/2019 02:55, [email protected]... wrote:
From: Selva Nair <[email protected]...>
- Add a new return value (-2) for
openvpn_execve()when external program execution is not allowed due to a low script-security setting.- Add a corresponding error message
Errors and warnings in such cases will now display as "WARNING: failed running command (<cmd>) :" followed by
"disallowed by script-security setting" on all platforms
instead of the current
"external program did not execute -- returned error code -1" on Windows and
"external program fork failed" on other platforms.The error is FATAL for some scripts and that behaviour is unchanged.
This helps the Windows GUI to detect when a connection failure results from a safer script-security setting enforced by the GUI, and show a relevant message.
Quoted 5 lines
Signed-off-by Selva Nair <[email protected]...>---
This is being presented as a better alternative for patch 684. A separate patch may be needed for 2.4 -- will do if this is found acceptable.
Generally speaking, this looks much better and should avoid changing the behaviour. It just clarifies the error better. Feature-ACK from me.
[...]
Quoted 7 lines
src/openvpn/run_command.c +0
-0@@ -65,12 +65,23 @@ system_error_message(int stat, struct gc_arena *gc) {buf_printf (&out,"external program did not execute -- " );
src/openvpn/run_command.c system_error_message(int stat, struct gc_arena *gc) · line 75
Quoted 10 lines
src/openvpn/run_command.c +8
-1} -buf_printf (&out,"returned error code %d" , stat); +if (stat ==-2 ) + { +buf_printf (&out,"disallowed by script-security setting" ); + } +else + { +buf_printf (&out,"returned error code %d" , stat); + }
Perhaps we could swap out the -1 and -2 values with macro constants, for code clarity? Also, perhaps it would look more structured if using a switch() instead of if()/else if().
Otherwise, the code looks good.
Another interesting are which most likely quite seldom fails, but we actually have yet another "undocumented" exit code ... This is not this patch's fault, but should probably be reviewed a bit more carefully.
In run_command.c:149-153:
--------------------------------------------------------------------
pid = fork();
if (pid == (pid_t)0) /* child side */
{
execve(cmd, argv, envp);
exit(127);
}--------------------------------------------------------------------
If execve() fails, the exit code is 127. That would normally be caught by the waitpid() later on and this exit code would be returned by openvpn_execve().
This should be improved in a separate patch though, but is not of high urgency. I don't expect most setups having a high execve() failure rate. But catching it and reporting this error path would be good.
Signature · 4 lines
kind regards, David Sommerseth OpenVPN Inc
Hi,
Thanks for the review.
Quoted 51 lines · David Sommerseth
Quoted 25 lines · On 17/02/2019 02:55, [email protected]... wrote:
From: Selva Nair <[email protected]...>
- Add a new return value (-2) for
openvpn_execve()when external program execution is not allowed due to a low script-security setting.- Add a corresponding error message
Errors and warnings in such cases will now display as "WARNING: failed running command (<cmd>) :" followed by
"disallowed by script-security setting" on all platforms
instead of the current
"external program did not execute -- returned error code -1" on Windows and
"external program fork failed" on other platforms.The error is FATAL for some scripts and that behaviour is unchanged.
This helps the Windows GUI to detect when a connection failure results from a safer script-security setting enforced by the GUI, and show a relevant message.
Quoted 5 lines
Signed-off-by Selva Nair <[email protected]...>---
This is being presented as a better alternative for patch 684. A separate patch may be needed for 2.4 -- will do if this is found acceptable.Generally speaking, this looks much better and should avoid changing the behaviour. It just clarifies the error better. Feature-ACK from me.
[...]
Quoted 14 lines
src/openvpn/run_command.c +5
-1@@ -65,12 +65,23 @@ system_error_message(int stat, struct gc_arena *gc) {buf_printf (&out,"external program did not execute -- " ); } -buf_printf (&out,"returned error code %d" , stat); +if (stat ==-2 ) + { +buf_printf (&out,"disallowed by script-security setting" ); + } +else
Quoted 8 lines
src/openvpn/run_command.c system_error_message(int stat, struct gc_arena *gc) · line 75
Quoted 3 lines
src/openvpn/run_command.c +3
-0+ { +buf_printf (&out,"returned error code %d" , stat); + }Perhaps we could swap out the -1 and -2 values with macro constants, for code
clarity? Also, perhaps it would look more structured if using aswitch()instead of if()/elseif().
Because of branching based on if (WIFEXITED(stat)) etc., switch() may be only
marginally cleaner and cause some deep indentation. But will give it a go.
Quoted 23 lines
Otherwise, the code looks good.
Another interesting are which most likely quite seldom fails, but we actually
have yet another "undocumented" exit code ... This is not this patch's fault,
but should probably be reviewed a bit more carefully.In run_command.c:149-153:
--------------------------------------------------------------------pid = fork(); if (pid == (pid_t)0) /* child side */ { execve(cmd, argv, envp); exit(127); }--------------------------------------------------------------------
If
execve()fails, the exit code is 127. That would normally be caught by thewaitpid()later on and this exit code would be returned byopenvpn_execve().
Quoted 5 lines
This should be improved in a separate patch though, but is not of high urgency. I don't expect most setups having a high
execve()failure rate. But
catching it and reporting this error path would be good.
In fact its easy to trigger this -- just have a shebang with a non-existent path like
#!/bin/sh1 and child will exit with 127. In system_error_message() we translate that as
"could not execute external program"
This "could not" instead of "did not" is a good choice of words, but would be better
if we somehow marshal the actual errno set by execve (in this case ENOENT) to the parent. Its lost in the current implementation. But that's beyond this patch,
as you say.
I'll just add a macro for this 127 as well and leave it at that.
Selva
PATCH v2 1/1 Better error message when script fails due to script-security setting
In reply to [email protected]...From: Selva Nair <[email protected]...>
- Add a new return value (-2) for openvpn_execve() when external program execution is not allowed due to a low script-security setting.
- Add a corresponding error message
Errors and warnings in such cases will now display as "WARNING: failed running command (<cmd>) :" followed by
"disallowed by script-security setting" on all platforms
instead of the current
"external program did not execute -- returned error code -1" on Windows and
"external program fork failed" on other platforms.
The error is FATAL for some scripts and that behaviour is unchanged.
This helps the Windows GUI to detect when a connection failure results from a safer script-security setting enforced by the GUI, and show a relevant message.
v2 changes as suggested by <[email protected]...>
- define macros for return values of openvpn_execve()
- replace if/else by switch() in system_error_message()
--- src/openvpn/run_command.c | 93 ++++++++++++++++++++++++++++------------------- src/openvpn/run_command.h | 4 ++ src/openvpn/win32.c | 12 ++++-- 3 files changed, 68 insertions(+), 41 deletions(-)
src/openvpn/run_command.c +56 -37
@@ -54,44 +54,57 @@ script_security_set(int level) }/* -* Print an error message based on the status code returned by system(). +* Generate an error message based on the status code returned by openvpn_execve(). */ static const char *system_error_message (int stat,struct gc_arena *gc) {struct buffer out =alloc_buf_gc (256 , gc); -#ifdef _WIN32 -if (stat ==-1 ) + +switch (stat) { -buf_printf (&out,"external program did not execute -- " ); - } -buf_printf (&out,"returned error code %d" , stat); +case OPENVPN_EXECVE_NOT_ALLOWED : +buf_printf (&out,"disallowed by script-security setting" ); +break ; + +#ifdef _WIN32 +case OPENVPN_EXECVE_ERROR : +buf_printf (&out,"external program did not execute -- " ); +/* fall through */ + +default : +buf_printf (&out,"returned error code %d" , stat); +break ;#else /* ifdef _WIN32 */ -if (stat ==-1 ) - { -buf_printf (&out,"external program fork failed" ); - } -else if (!WIFEXITED (stat)) - { -buf_printf (&out,"external program did not exit normally" ); - } -else - { -const int cmd_ret =WEXITSTATUS (stat); -if (!cmd_ret) - { -buf_printf (&out,"external program exited normally" ); - } -else if (cmd_ret ==127 ) - { -buf_printf (&out,"could not execute external program" ); - } -else - { -buf_printf (&out,"external program exited with error status: %d" , cmd_ret); - } - } + +case OPENVPN_EXECVE_ERROR : +buf_printf (&out,"external program fork failed" ); +break ; + +default : +if (!WIFEXITED (stat)) + { +buf_printf (&out,"external program did not exit normally" ); + } +else + { +const int cmd_ret =WEXITSTATUS (stat); +if (!cmd_ret) + { +buf_printf (&out,"external program exited normally" ); + } +else if (cmd_ret ==OPENVPN_EXECVE_FAILURE ) + { +buf_printf (&out,"could not execute external program" ); + } +else + { +buf_printf (&out,"external program exited with error status: %d" , cmd_ret); + } + } +break ;#endif /* ifdef _WIN32 */ + }return (const char *)out.data; }
@@ -114,12 +127,14 @@ openvpn_execve_allowed(const unsigned int flags)* Run execve() inside a fork(). Designed to replicate the semantics of system() but * in a safer way that doesn't require the invocation of a shell or the risks * associated with formatting and parsing a command line. +* Returns the exit status of child, OPENVPN_EXECVE_NOT_ALLOWED if openvpn_execve_allowed() +* returns false, or OPENVPN_EXECVE_ERROR on other errors. */ int openvpn_execve (const struct argv *a,const struct env_set *es,const unsigned int flags) {struct gc_arena gc =gc_new (); -int ret =-1 ; +int ret =OPENVPN_EXECVE_ERROR ;static bool warn_shown = false;if (a && a->argv[0 ])
@@ -136,7 +151,7 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned inif (pid == (pid_t )0 )/* child side */ {execve (cmd, argv, envp); -exit (127 ); +exit (OPENVPN_EXECVE_FAILURE ); }else if (pid < (pid_t )0 )/* fork failed */ {
@@ -146,14 +161,18 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned in {if (waitpid (pid, &ret,0 ) != pid) { - ret =-1 ; + ret =OPENVPN_EXECVE_ERROR ; } } } -else if (!warn_shown && (script_security () <SSEC_SCRIPTS )) +else { -msg (M_WARN ,SCRIPT_SECURITY_WARNING ); - warn_shown = true; + ret =OPENVPN_EXECVE_NOT_ALLOWED ; +if (!warn_shown && (script_security () <SSEC_SCRIPTS )) + { +msg (M_WARN ,SCRIPT_SECURITY_WARNING ); + warn_shown = true; + } }#else /* if defined(ENABLE_FEATURE_EXECVE) */ msg (M_WARN ,"openvpn_execve: execve function not available" );
@@ -227,7 +246,7 @@ openvpn_popen(const struct argv *a, const struct env_set *es)close (pipe_stdout[0 ]);/* Close read end */ dup2 (pipe_stdout[1 ],1 );execve (cmd, argv, envp); -exit (127 ); +exit (OPENVPN_EXECVE_FAILURE ); }else if (pid > (pid_t )0 )/* parent side */ {
src/openvpn/run_command.h +4 -0
@@ -33,6 +33,10 @@#define SSEC_SCRIPTS 2/* allow calling of built-in programs and user-defined scripts */ #define SSEC_PW_ENV 3/* allow calling of built-in programs and user-defined scripts that may receive a password as an environmental variable */ +#define OPENVPN_EXECVE_ERROR -1/* generic error while forking to run an external program */ +#define OPENVPN_EXECVE_NOT_ALLOWED -2/* external program not run due to script security */ +#define OPENVPN_EXECVE_FAILURE 127/* exit code passed back from child when execve fails */ +int script_security (void );void script_security_set (int level);
src/openvpn/win32.c +8 -4
@@ -1088,7 +1088,7 @@ wide_cmd_line(const struct argv *a, struct gc_arena *gc)int openvpn_execve (const struct argv *a,const struct env_set *es,const unsigned int flags) { -int ret =-1 ; +int ret =OPENVPN_EXECVE_ERROR ;static bool exec_warn = false;if (a && a->argv[0 ])
@@ -1137,10 +1137,14 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned infree (env);gc_free (&gc); } -else if (!exec_warn && (script_security () <SSEC_SCRIPTS )) +else { -msg (M_WARN ,SCRIPT_SECURITY_WARNING ); - exec_warn = true; + ret =OPENVPN_EXECVE_NOT_ALLOWED ; +if (!exec_warn && (script_security () <SSEC_SCRIPTS )) + { +msg (M_WARN ,SCRIPT_SECURITY_WARNING ); + exec_warn = true; + } } }else
Signature · 1 line
2.1.4
Thanks. I have not tested the various error conditions, but the code makes sense to me and it passes compile and t_client tests (which use --up here), so it's not breaking stuff in fundamental and non-obvious ways.
(Since David was mostly happy with the v1 approach except for the "127" and "switch()", I assume he's totally fine with v2 now :-) )
Your patch has been applied to the master branch - it won't apply to 2.4 as we have no "run_command.*" there yet. Shall we cherrypick bf97c00f7dba4 and 253f015558 to get there? Or do you want to do a 2.4 version of the patch?
commit 01a3c876d4911ff7ea82b04edeb43defdebc69d7 (master)
Author: Selva Nair Date: Wed Feb 20 19:46:22 2019 -0500
Better error message when script fails due to script-security setting
Signed-off-by: Selva Nair <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg18223.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Hi,
Quoted 7 lines · Gert Doering
Acked-by Gert Doering <[email protected]...>Thanks. I have not tested the various error conditions, but the code makes sense to me and it passes compile and
t_clienttests (which use --up here), so it's not breaking stuff in fundamental and non-obvious ways.
Quoted 8 lines
(Since David was mostly happy with the v1 approach except for the "127" and "
switch()", I assume he's totally fine with v2 now :-) )Your patch has been applied to the master branch - it won't apply to 2.4 as we have no "run_command.*" there yet. Shall we cherrypick
bf97c00f7dba4and253f015558to get there? Or do you want to do a 2.4 version of the patch?
Would require more cherry-picks than that, or some manual edits, isn't it?
I can send a patch for 2.4 -- would you prefer the same as this -- i.e with switch(stat) in misc.c and and preprocessor macros in misc.h or a simpler, return code = -2 and error message as in version 1?
Selva
Hi,
Quoted 8 lines · Selva Nair
Quoted 4 lines · Gert Doering
Your patch has been applied to the master branch - it won't apply to 2.4 as we have no "run_command.*" there yet. Shall we cherrypick
bf97c00f7dba4and253f015558to get there? Or do you want to do a 2.4 version of the patch?Would require more cherry-picks than that, or some manual edits, isn't it?
Yes, as we also have uncrustify patches that affect neighboring code.
Quoted 3 lines
I can send a patch for 2.4 -- would you prefer the same as this -- i.e with switch(stat) in
misc.cand and preprocessor macros inmisc.hor a simpler, return code = -2 and error message as in version 1?
I think we should have the same code (so, with the switch()), just in misc.c - when we come here next time, at least it will be the same code, not "something completely different".
thanks,
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Quoted 3 lines · Gert Doering
(Since David was mostly happy with the v1 approach except for the "127" and "
switch()", I assume he's totally fine with v2 now :-) )
Yeah, sorry I didn't manage to do the second review. I've done it post-apply, and it looks much better. It would get an ACK from me without much fuzz. Thanks!
Signature · 4 lines
kind regards, David Sommerseth OpenVPN Inc