Skip to content

lslogins: fix lastlog2 tty/host buffer overflow in get_lastlog2#4418

Merged
karelzak merged 1 commit into
util-linux:masterfrom
aizu-m:lslogins-lastlog2-overflow
Jun 15, 2026
Merged

lslogins: fix lastlog2 tty/host buffer overflow in get_lastlog2#4418
karelzak merged 1 commit into
util-linux:masterfrom
aizu-m:lslogins-lastlog2-overflow

Conversation

@aizu-m

@aizu-m aizu-m commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Comparing the lastlog2 reader against the wtmp branch in the same function, the last mem2strcpy() argument looked wrong, so I traced it.

  1. lslogins allocates user->last_tty and user->last_hostname at sizeof(ut_line)+1 and sizeof(ut_host)+1.
  2. get_lastlog2() reads the tty/host for the user from the lastlog2 database; both are unbounded TEXT columns.
  3. it then calls mem2strcpy(dst, value, strlen(value), strlen(value) + 1), taking the limit from the source length.
  4. mem2strcpy() memsets and copies that many bytes into dst, so the source length defeats the cap and a value wider than the field writes past the heap buffer.

Under ASAN a host longer than 256 bytes gives a heap-buffer-overflow one byte after the 257-byte buffer (the memset). The values reach the database from pam_lastlog2 (PAM_TTY/PAM_RHOST, stored untruncated) or from any database handed to lslogins with --lastlog2-file. Capped the limit to the destination field size, which is what the wtmp and plain-lastlog branches just above already do.

get_lastlog2() copies the TTY and host strings read from the lastlog2
database into heap buffers of sizeof(ut_line)+1 and sizeof(ut_host)+1
bytes, but passed strlen(value)+1 as the mem2strcpy() limit. mem2strcpy()
zero-fills and copies that many bytes regardless of the destination, so a
database value longer than the field overflows the buffer. Cap the limit
to the destination field size, matching the wtmp and plain-lastlog paths
in the same function.

Signed-off-by: aizu-m <aizumusheer2@gmail.com>
@karelzak karelzak merged commit 06d2d84 into util-linux:master Jun 15, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants