Skip to content

Login shell does not always preserve target user environment #1335

@BAMF0

Description

@BAMF0

Describe the bug
PAM environment variables included in env_keep do not get overridden by the target user's environment when passing the -i option to sudo-rs.

To Reproduce
Steps to reproduce the behavior:

  1. Edit /etc/sudoers using visudo to include:
Defaults env_keep += "HOME"
  1. Enter a login shell as a different user in the sudo group:
root@host$ sudo -i -u ubuntu
  1. Print the HOME directory:
ubuntu@host$ echo $HOME
  1. HOME directory of the user who invoked the login shell is shown:
/root

Expected behavior
When entering a login shell using sudo -i -u <target_user> the environment is expected to reflect the target user's PAM environment, not the invoking user's, even though PAM environment variables have been preserved using env_keep.

Environment (please complete the following information):

  • Linux distribution: Ubuntu 26.04 (development branch)
  • sudo-rs version: 0.2.10

Additional context
This is a special case present in ogsudo (see Command Environment in the Sudoers manual):

As a special case, if sudo's -i option (initial login) is specified, sudoers will initialize the environment regardless of the value of env_reset. The DISPLAY, PATH and TERM variables remain unchanged; HOME, MAIL, SHELL, USER, and LOGNAME are set based on the target user. On AIX (and Linux systems without PAM), the contents of /etc/environment are also included. On BSD systems, if the use_loginclass flag is enabled, the path and setenv variables in /etc/login.conf are also applied. All other environment variables are removed.

Metadata

Metadata

Assignees

Labels

C-checkerPermission checking logicnon-compliantused to track failing compliance tests

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions