Bug 1478761 - Double sudo no longer sets USER environment variable
Double sudo no longer sets USER environment variable
Product: Red Hat Enterprise Linux 7
Classification: Red Hat
Component: sudo (Show other bugs)
x86_64 Linux
unspecified Severity unspecified
: rc
: ---
Assigned To: Marek Tamaskovic
BaseOS QE Security Team
Depends On:
  Show dependency treegraph
Reported: 2017-08-06 20:45 EDT by tiernan.messmer
Modified: 2017-10-02 07:20 EDT (History)
2 users (show)

See Also:
Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2017-10-02 07:19:48 EDT
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description tiernan.messmer 2017-08-06 20:45:28 EDT
Description of problem:
When sudo is used to switch user, the environment variable USER is set with the new username. After the latest security patch, this appears to have broken when sudo is used twice in a row, eg userA sudos to userB, which then sudos to userC - after the final sudo the USER environment variable is still equal to userB not userC as expected.

This behaviour worked correctly on earlier patch versions of sudo on RHEL7

Version-Release number of selected component (if applicable):
sudo.x86_64 1.8.19p2-10.el7

How reproducible:
Completely reproducible

Steps to Reproduce:
1. On a system create 2 users, userA and userB. root will be used as the third user, but the specific users do not matter
2. Add the following to the sudoers file


2. Login to the system as userA
3. Run the following commands as per actual results. Notice the final echo $USER outputs the wrong username. It doesn't seem to matter which users are used, as long as sudo is invoked twice.

Actual results:

[userA@system ~] echo $USER
[userA@system ~] sudo -s
[root@system ~] echo $USER
[root@system ~] sudo -s -u userB
[userB@system ~] echo $USER

Expected results:
On a system with an older version of sudo the following is observed:

[userA@system ~] echo $USER
[userA@system ~] sudo -s
[root@system ~] echo $USER
[root@system ~] sudo -s -u userB
[userB@system ~] echo $USER

Additional info:
env_reset is set and env_keep does not contain USER as per the defaults in /etc/sudoers.
Comment 2 Marek Tamaskovic 2017-09-27 02:37:20 EDT
Here's what is happening:

1) sudo is invoked without USERNAME set

2) sudo executes the other sudo command, setting LOGNAME, USER and
USERNAME to the name of target user

3) the second sudo preserves USERNAME as per sudoers, which is set to
the name of the user the first sudo ran as (root in your example)

4) sudo also preserves LOGNAME and USER to avoid having LOGNAME, USER
and USERNAME be inconsistent.

Sudo is behaving as intended, though the behavior with respect to
keeping LOGNAME, USER and USERNAME be consistent is not well

It would probably be less surprising if sudo were to preserve USER and
LOGNAME too if USERNAME is listed in env_keep.
Comment 3 tiernan.messmer 2017-09-27 03:41:10 EDT
This explains the behaviour. Removing USERNAME from env_keep reverts the the previous behaviour as seen on systems with sudo version before 1.8.19p2-10.el7. The sudoers file did not not appear to have been changed between versions, so env_keep including USERNAME on older versions did not exhibit this behaviour.

I guess the question at this stage though is why the behaviour changed between versions. Was this a part of the security bug that was fixed in 1.8.19p2-10.el7? Or was this an additional bugfix that was accidentally backported with the security fix?
Comment 4 Marek Tamaskovic 2017-10-02 07:19:48 EDT
I think it was fixed in new version.

Note You need to log in before you can comment on or make changes to this bug.