Bug #19724
closedDir.home returns incorrect home directory
Description
This is probably related to 16787.
From the patch on that issue I conclude that ruby uses getlogin and co to obtain the name of the logged-in user, but that is not valid. Ruby must use the uid of the running process since the user that logged in may since have switched to a different user-id (e.g. using sudo -u <user> -i
). This will cause getlogin to still report the name that the user logged in at the current terminal, but that is not the user that ruby should be using.
E.g., If I ssh into a machine as root, and then use sudo -u user -i and then run a ruby process that tries to write to "my" home directory it fails (most commonly in trying to access /root/.gem or similar).
I know why ruby originally wanted to use getlogin (at least according to comments on the above issue), which was to support multiple login names having the same UID but different home directories. To the best of my knowledge, this was always an invalid configuration. Usernames and UIDs in the PW DB should have a 1-to-1 mapping. In any case getlogin is not the right answer, since it does not yield the login name of the running process.
It is particularly bad since ruby uses this method even if ${HOME} is set!
This is on current up-to-date Ubuntu 22.04.