-
Notifications
You must be signed in to change notification settings - Fork 254
login: fixed PID matching when PAM (and forking) used #1282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
login: fixed PID matching when PAM (and forking) used #1282
Conversation
|
Updated as requested. |
Would you mind showing an example of a shell session where you show the problem, and another with the fixed program where it's solved? That would help review this. Also, if you could link to some documentation that confirms the first sentence, it would also help review. Thanks! |
|
Here is an example of work of unpatched $ logname
LOGIN
$ utmpdump /run/utmp
Utmp dump of /run/utmp
[2] [00000] [~~ ] [reboot ] [~ ] [6.15.5-gentoo-k2k-special10] [0.0.0.0 ] [2025-07-10T14:46:16,621237+00:00]
[7] [00796] [ ] [k2k ] [tty2 ] [:0 ] [0.0.0.0 ] [2025-07-10T14:46:18,945439+00:00]
[7] [00900] [ts/0] [k2k ] [pts/0 ] [:0 ] [0.0.0.0 ] [2025-07-10T14:46:19,557053+00:00]
[6] [118299] [tty3] [LOGIN ] [tty3 ] [ ] [0.0.0.0 ] [2025-07-11T18:20:23,728441+00:00]
[7] [118306] [3 ] [k2k ] [tty3 ] [ ] [0.0.0.0 ] [2025-07-11T18:20:34,419835+00:00]
$ pstree -aps $$
systemd,1
└─login,118299 --
└─bash,118306
└─pstree,118400 -aps 118306
$ systemctl status getty@tty3.service
■ getty@tty3.service - Getty on tty3
Loaded: loaded (/usr/lib/systemd/system/getty@.service; disabled; preset: enabled)
Active: active (running) since Fri 2025-07-11 20:20:23 CEST; 1min 0s ago
Invocation: 29c9b16245d2470182603a413b2f213e
Docs: man:agetty(8)
man:systemd-getty-generator(8)
https://0pointer.de/blog/projects/serial-console.html
Main PID: 118299 (login)
Tasks: 0 (limit: 112791)
Memory: 688K (peak: 1.8M)
CPU: 18ms
CGroup: /system.slice/system-getty.slice/getty@tty3.service
■ 118299 /bin/login --The next example is on the same system, but with purposed patch: $ logname
k2k
$ utmpdump /run/utmp
Utmp dump of /run/utmp
[2] [00000] [~~ ] [reboot ] [~ ] [6.15.5-gentoo-k2k-special10] [0.0.0.0 ] [2025-07-10T14:46:16,621237+00:00]
[7] [00796] [ ] [k2k ] [tty2 ] [:0 ] [0.0.0.0 ] [2025-07-10T14:46:18,945439+00:00]
[7] [00900] [ts/0] [k2k ] [pts/0 ] [:0 ] [0.0.0.0 ] [2025-07-10T14:46:19,557053+00:00]
[7] [99316] [tty3] [k2k ] [tty3 ] [ ] [0.0.0.0 ] [2025-07-11T18:07:06,899838+00:00]
$ pstree -aps $$
systemd,1
└─login,3777 --
└─bash,99316
└─pstree,100149 -aps 99316
$ systemctl status getty@tty3.service
■ getty@tty3.service - Getty on tty3
Loaded: loaded (/usr/lib/systemd/system/getty@.service; disabled; preset: enabled)
Active: active (running) since Thu 2025-07-10 16:53:45 CEST; 1 day 3h ago
Invocation: 9167c89332fc4a4b9eaafc2b50784d3e
Docs: man:agetty(8)
man:systemd-getty-generator(8)
https://0pointer.de/blog/projects/serial-console.html
Main PID: 3777 (login)
Tasks: 0 (limit: 112791)
Memory: 776K (peak: 2.2M)
CPU: 19ms
CGroup: /system.slice/system-getty.slice/getty@tty3.service
■ 3777 /bin/login --When Lines 1086 to 1128 in 36debf3
Formally, match with parent PID could be guarded with Strictly speaking, the we can try to match own PID only when PAM is not used, and parent PID only when PAM is used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Cc: @thesamesam |
|
Updated to have a stricter matching:
|
|
Added a second commit for one more similar bug |
TBH, I preferred the previous approach. I hate preprocessor conditionals. Is it incorrect to do this for non-PAM? |
'login' binary may fork itself before this check, so the entry may contain parent PID instead of current process PID
Changed back, added more explanations to the commit message. |
|
Moved second commit to separate PR (#1286) to unblock this one. |
Hmm, sounds reasonable. What if the process was run from another user's account with something like su(1) from the same tty? Is it still impossible? (Just trying to rule out weird cases; I didn't think too much of it.) |
|
|
Sounds good. Is there any Gentoo bug associated with this? If so, please include it in the commit message. Else, I'll merge. |
No, I haven't open a Gentoo bug as it is a clear upstream bug. |
|
Thanks! I'll merge, then. |
| while ((ut = getutxent()) != NULL) { | ||
| if ( (ut->ut_pid == getpid ()) | ||
| if ( ( (ut->ut_pid == getpid ()) | ||
| || (ut->ut_pid == getppid ())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(sorry I'm late, but)
Is there a chance that in fact both pid and ppid have (different) valid entries already? Would it be better to cache the ut if ut->ut->pid == getppid(), keep running through the list, and then only return the cached ut if no ut->ut_pid == getpid() was found?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My reply is here: #1282 (comment)
|
Probably all PID checks are redundant. Or a safer (for any reason) approach:
|
|
Actually utmp handling is described here: https://man7.org/linux/man-pages/man5/utmp.5.html I'll add another PR with correction to utmp handling (to bring it back to the originally expected behaviour). |
|
More fixes are here |
An entry with parent process PID is also valid for the current entry, specially when PAM is used as login fork itself before updating utmp.
Without this patch a duplicated utmp entry is produced each time (one entry is LOGIN_PROCESS and additional entry is USER_PROCESS).