Skip to contentSkip to conversation

Bug or Feature? Username in environment in auth-user-pass-verify

Openvpn-devel Steven Haigh 5 participants 7 messages Jun 15, 2017 – Jun 16, 2017

lore
Share & tools
lore
#0 lore raw

Hi all,

I'm just trying to figure out if its expected behaviour to have the 'username' set in the environment when using the auth-user-pass-verify script.

My observations with 2.4.2 seems that even when auth-user-pass-verify is called with via-file, the username is set in the environment. The client-connect script does not have the username present in the environment.

The documentation for script-security states:
2 -- Allow calling of built-in executables and user-defined scripts.
3 -- Allow passwords to be passed to scripts via environmental variables (potentially unsafe).

Ok, so level 3 specifically mentions the username / password via env variables. No mention of level 2.

The auth-user-pass-verify documentation states:
If method is set to "via-env", OpenVPN will call script with the environmental variables username and password set to the username/password strings provided by the client. Be aware that this method is insecure on some platforms which make the environment of a process publicly visible to other unprivileged processes.

If method is set to "via-file", OpenVPN will write the username and password to the first two lines of a temporary file. The filename will be passed as an argument to script, and the file will be automatically deleted by OpenVPN after the script returns. The location of the temporary file is controlled by the --tmp-dir option, and will default to the current directory if unspecified. For security, consider setting --tmp-dir to a volatile storage medium such as /dev/shm (if available) to prevent the username/password file from touching the hard drive.

No mention of the username env variable when using via-file - but this gives me the impression that the username should *not* be set in the environment - but it should be in the file.

So - bug or feature?

Signature · 4 lines
Steven Haigh

? [email protected]...     ? http://www.crc.id.au
? +61 (3) 9001 6090    ? 0412 935 897
#1 lore raw

Hi,

Quoted 3 lines · Steven Haigh

I'm just trying to figure out if its expected behaviour to have the 'username' set in the environment when using the auth-user-pass-verify script.

The code in question (ssl_verify.c) is older than the involvement of any of the currently-active developers... but JJK or Ecrist might know.

Anyway, what the code *says* is:

ssl_verify.c, about line 1095:

verify_user_pass_script(...)
{
...

        /* Set environmental variables prior to calling script */
        if (session->opt->auth_user_pass_verify_script_via_file)
        {
... (no setenv here)
        }
        else
        {
            setenv_str(session->opt->es, "username", up->username);
            setenv_str(session->opt->es, "password", up->password);
        }

so, yes, that is what it *does* - "username" is only ever set together with "password", and that's only setenv'ed if you do not use "via-file".

Now, that is about calling the --verify-auth-user-pass, but I think the "es" (environment set) being affected here is the global per-connection es (not something local to this function), so that would affect --client-connect as well.

[..]

Quoted 6 lines

The auth-user-pass-verify documentation states:
If method is set to "via-env", OpenVPN will call script with the environmental variables username and password set to the username/password strings provided by the client. Be aware that this method is insecure on some platforms which make the environment of a process publicly visible to other unprivileged processes.

This "some platforms" actually something we should eventually verify and clearly spell-out - because Linux and all recent BSDs do *not* show the environment to other unprivileged users.

[..]

Quoted 5 lines

No mention of the username env variable when using via-file - but this gives me the impression that the username should *not* be set in the environment - but it should be in the file.

So - bug or feature?

Username and Password are always handled in tandem when talking about --auth-user-pass-verify, so "both in environment" or "none of them".

Now, if you use a *plugin* (or the management interface), the code will always set both in the es, and delete the password(!) afterwards, leaving the username intact...

Looking from a given distance, I'd say that this is a bug, and "username" should propably be always visible in the es (if present), while password should not.

If you want to experiment: go to ssl_verify.c, and move the line 1123 (master) outside the else {} block:

old:

        else
        {
            setenv_str(session->opt->es, "username", up->username);
            setenv_str(session->opt->es, "password", up->password);
        }

new:

        else
        {
            setenv_str(session->opt->es, "password", up->password);
        }
        setenv_str(session->opt->es, "username", up->username);

... then it should show up in client-connect as well.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#2 lore raw
Quoted 84 lines · AEST Gert Doering

Hi,

Quoted 3 lines · Steven Haigh

I'm just trying to figure out if its expected behaviour to have the 'username' set in the environment when using the auth-user-pass-verify script.

The code in question (ssl_verify.c) is older than the involvement of any of the currently-active developers... but JJK or Ecrist might know.

Anyway, what the code *says* is:

ssl_verify.c, about line 1095:

verify_user_pass_script(...)
{
...

        /* Set environmental variables prior to calling script */
        if (session->opt->auth_user_pass_verify_script_via_file)
        {
... (no setenv here)
        }
        else
        {
            setenv_str(session->opt->es, "username", up->username);
            setenv_str(session->opt->es, "password", up->password);
        }

so, yes, that is what it *does* - "username" is only ever set together with "password", and that's only setenv'ed if you do not use "via-file".

Now, that is about calling the --verify-auth-user-pass, but I think the "es" (environment set) being affected here is the global per-connection es (not something local to this function), so that would affect --client-connect as well.

[..]

Quoted 6 lines

The auth-user-pass-verify documentation states:
If method is set to "via-env", OpenVPN will call script with the environmental variables username and password set to the username/password strings provided by the client. Be aware that this method is insecure on some platforms which make the environment of a process publicly visible to other unprivileged processes.

This "some platforms" actually something we should eventually verify and clearly spell-out - because Linux and all recent BSDs do *not* show the environment to other unprivileged users.

[..]

Quoted 5 lines

No mention of the username env variable when using via-file - but this gives me the impression that the username should *not* be set in the environment - but it should be in the file.

So - bug or feature?

Username and Password are always handled in tandem when talking about --auth-user-pass-verify, so "both in environment" or "none of them".

Now, if you use a *plugin* (or the management interface), the code will always set both in the es, and delete the password(!) afterwards, leaving the username intact...

Looking from a given distance, I'd say that this is a bug, and "username" should propably be always visible in the es (if present), while password should not.

If you want to experiment: go to ssl_verify.c, and move the line 1123 (master) outside the else {} block:

old:

        else
        {
            setenv_str(session->opt->es, "username", up->username);
            setenv_str(session->opt->es, "password", up->password);
        }

new:

Quoted 7 lines
        else
        {
            setenv_str(session->opt->es, "password", up->password);
        }
        setenv_str(session->opt->es, "username", up->username);

... then it should show up in client-connect as well.

I managed to track this down today.

It seems the pam plugin sets the environment variable. As such, as soon as I add the following line to the server config, the username variable appears back in the environment for auth-user-pass-verify:
plugin /usr/lib64/openvpn/plugins/openvpn-plugin-auth-pam.so openvpn

After commenting this line out and restarting the daemon, the username env variable is gone.

That still poses the question, is a plugin supposed to be able to alter the environment presented to the auth-user-pass-verify script?

If so, should these changes also be in the client-connect script?

If not, the environment might need a reset before the auth-user-pass-verify script is called?

Signature · 4 lines
Steven Haigh

📧 [email protected]...	💻 http://www.crc.id.au
📞 +61 (3) 9001 6090	📱 0412 935 897
#3 lore raw
Quoted 4 lines · Steven Haigh
Quoted 3 lines · AEST Gert Doering

Hi,

On Thu, Jun 15, 2017 at 12:50:40PM +1000, Steven Haigh wrote:

[...snip...]

Quoted 17 lines
Quoted 17 lines

old:

        else
        {
            setenv_str(session->opt->es, "username", up->username);
            setenv_str(session->opt->es, "password", up->password);
        }

new:

        else
        {
            setenv_str(session->opt->es, "password", up->password);
        }
        setenv_str(session->opt->es, "username", up->username);
Quoted 8 lines
Quoted 1 line

... then it should show up in client-connect as well.

I managed to track this down today.

It seems the pam plugin sets the environment variable. As such, as soon as I add the following line to the server config, the username variable appears back in the environment for auth-user-pass-verify:
plugin /usr/lib64/openvpn/plugins/openvpn-plugin-auth-pam.so openvpn

Ouch.

Quoted 5 lines

After commenting this line out and restarting the daemon, the username env variable is gone.

That still poses the question, is a plugin supposed to be able to alter the environment presented to the auth-user-pass-verify script?

You have discovered something I've not been thinking of. But yes, plug-ins can actually manage to do that if they manipulate the environment pointer provided to it. All plug-ins runs within the same memory scope as the main OpenVPN process - in fact, plug-ins is a way to extend the features of the core OpenVPN process.

That said ... this needs to be changed, I think it makes sense to take some action to reduce this possibility. By manipulating pointers provided to you via the the plug-in interface, you can make the OpenVPN process crash and burn ... and it is fairly easily. If you manage to modify memory regions too much so other OpenVPN data structures gets corrupted, it will explode in interesting ways.

Quoted 1 line

If so, should these changes also be in the client-connect script?

That should not be possible. There exist no direct API to modify the environment variables from plug-ins, that is purely a side-effect if how the memory buffers and pointers sent to plug-ins are handled.

Quoted 2 lines

If not, the environment might need a reset before the auth-user-pass-verify script is called?

Either reset (which I think is harder) or to copy the information into new fresh buffers which are discarded once the plug-ins returns.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Technologies, Inc
#4 lore raw
Quoted 8 lines · David Sommerseth

That said ... this needs to be changed, I think it makes sense to take some action to reduce this possibility. By manipulating pointers provided to you via the the plug-in interface, you can make the OpenVPN process crash and burn ... and it is fairly easily. If you manage to modify memory regions too much so other OpenVPN data structures gets corrupted, it will explode in interesting ways.

What happens if a plugin crashes on its own because of a segfault (for example)? I think the entire openvpn process will crash, right? If I am right, this means that protecting openvpn private objects is not really going to save the process from a possible horrible death.

Quoted 12 lines
Quoted 2 lines

If so, should these changes also be in the client-connect script?

That should not be possible. There exist no direct API to modify the environment variables from plug-ins, that is purely a side-effect if how the memory buffers and pointers sent to plug-ins are handled.

Quoted 2 lines

If not, the environment might need a reset before the auth-user-pass-verify script is called?

Either reset (which I think is harder) or to copy the information into new fresh buffers which are discarded once the plug-ins returns.

I second this idea - sounds easy and protects the openvpn env from changes. (assuming this is not done on a fast path, because a full blown copy of the env might have perf issues).

Cheers,

--
Antonio Quartulli

#5 lore raw
In reply to David Sommerseth

Hi,

Quoted 8 lines · David Sommerseth
Quoted 2 lines

That still poses the question, is a plugin supposed to be able to alter the environment presented to the auth-user-pass-verify script?

You have discovered something I've not been thinking of. But yes, plug-ins can actually manage to do that if they manipulate the environment pointer provided to it. All plug-ins runs within the same memory scope as the main OpenVPN process - in fact, plug-ins is a way to extend the features of the core OpenVPN process.

While plugins *can* do this, it's not actually what is happening - OpenVPN proper (ssl_verify.c) is doing this.

"username" ends in the environment, *if* one of these is set:

- you use a plugin
- you use management-verify-password (or whatever it's called)
- you use auth-user-pass-verify with "via-env"

Quoted 6 lines

That said ... this needs to be changed, I think it makes sense to take some action to reduce this possibility. By manipulating pointers provided to you via the the plug-in interface, you can make the OpenVPN process crash and burn ... and it is fairly easily. If you manage to modify memory regions too much so other OpenVPN data structures gets corrupted, it will explode in interesting ways.

Well, this is what you get if you link in other code into the same process... power and risk. Not sure how to do it differently - fork() off a helper process that talks over a socket/pipe with openvpn main and then links in the plugins? (Like openssh privilege separation).

Can be done, but would be quite an intrusive change - sounds we've found something to break for 2.5 :)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#6 lore raw
In reply to Gert Doering

Hi Gert et al,

Quoted 28 lines · Gert Doering

Hi,

Quoted 3 lines · Steven Haigh

I'm just trying to figure out if its expected behaviour to have the 'username' set in the environment when using the auth-user-pass-verify script.

The code in question (ssl_verify.c) is older than the involvement of any of the currently-active developers... but JJK or Ecrist might know.

Anyway, what the code *says* is:

ssl_verify.c, about line 1095:

verify_user_pass_script(...)
{
...

         /* Set environmental variables prior to calling script */
         if (session->opt->auth_user_pass_verify_script_via_file)
         {
... (no setenv here)
         }
         else
         {
             setenv_str(session->opt->es, "username", up->username);
             setenv_str(session->opt->es, "password", up->password);
         }
Quoted 2 lines

so, yes, that is what it *does* - "username" is only ever set together with "password", and that's only setenv'ed if you do not use "via-file".

that's how I remember it as well: username is setenv'ed only if you use 'via-env' ; no special reason for it, just how the flow of the code goes.

Quoted 25 lines

Now, that is about calling the --verify-auth-user-pass, but I think the "es" (environment set) being affected here is the global per-connection es (not something local to this function), so that would affect --client-connect as well.

[..]

Quoted 6 lines

The auth-user-pass-verify documentation states:
If method is set to "via-env", OpenVPN will call script with the environmental variables username and password set to the username/password strings provided by the client. Be aware that this method is insecure on some platforms which make the environment of a process publicly visible to other unprivileged processes.

This "some platforms" actually something we should eventually verify and clearly spell-out - because Linux and all recent BSDs do *not* show the environment to other unprivileged users.

[..]

Quoted 5 lines

No mention of the username env variable when using via-file - but this gives me the impression that the username should *not* be set in the environment - but it should be in the file.

So - bug or feature?

Username and Password are always handled in tandem when talking about --auth-user-pass-verify, so "both in environment" or "none of them".

Quoted 9 lines

Now, if you use a *plugin* (or the management interface), the code will always set both in the es, and delete the password(!) afterwards, leaving the username intact...

Looking from a given distance, I'd say that this is a bug, and "username" should propably be always visible in the es (if present), while password should not.

or one could argue that the behaviour should not change when using a plugin - i.e. the plugin should also unset 'username' ;)

then again, there is no harm in setting the username as an env var, so I'd go for the (trivial) patch.

cheers,

JJK