Skip to content

linux.kmsg: read the newest printk ringbuffer record - #2101

Open
ricardojrdez wants to merge 2 commits into
volatilityfoundation:developfrom
reverseame:fix/linux-kmsg-newest-record
Open

ricardojrdez wants to merge 2 commits into
volatilityfoundation:developfrom
reverseame:fix/linux-kmsg-newest-record

Conversation

@ricardojrdez

Copy link
Copy Markdown
Contributor

linux.kmsg on kernels >= 5.10 never reports the newest kernel-log record.

Kmsg_5_10_to_.run() walks the printk descriptor ring with while cur_id != end_id, where end_id is desc_ring.head_id. In kernel/printk/printk_ringbuffer.h, head_id is the ID of the newest descriptor (and tail_id the oldest), so the loop exits when the cursor reaches the newest descriptor, before reading it. The most recent record is therefore always missing.

The same loop also has no bound: if tail_id == head_id (a ring with a single record) or the IDs are inconsistent in a smeared image, the cursor walks the whole 62-bit ID space.

Change

Read from tail_id through head_id inclusive, and bound the walk by the ring size (desc_count), which is the maximum number of descriptors the ring can hold. The descriptor-state check is unchanged, so a head descriptor that is still reserved by a writer is skipped as before. The plugin version is bumped to 2.0.1.

Testing

Added test/test_linux_kmsg.py, which drives Kmsg_5_10_to_.run() over a fake descriptor ring and checks that the newest record is reported, that a reserved head is skipped, that a single-record ring works, and that inconsistent IDs are bounded by the ring size.

pytest -q --volatility=vol.py test/test_linux_kmsg.py   # 5 passed (develop: newest-record test fails, single-record test does not terminate)
ruff check / ruff format --check                         # clean

Real images:

Image develop (77ff0b1) This PR
Linux 6.18.59 x86-64 (VirtualBox core); last dmesg line written with echo ... > /dev/kmsg right before acquisition 670 rows, last is the preceding record 671 rows, last is the written line
Ubuntu 24.04, 6.8.0-117 x86-64 (LiME) with a rootkit LKM loaded 696 rows 697 rows: the extra record is the newest one, diamorphine: module verification failed: signature and/or required key missing - tainting kernel

On both images the first N rows are identical to develop; only the newest record is added.

The 5.10+ reader stopped when the cursor reached desc_ring.head_id, before
reading that descriptor. head_id is the ID of the newest descriptor, so the
most recent log record was never reported. Read from tail_id through head_id
inclusive, bounded by the ring size.
@eve-mem

eve-mem commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Hello!

Here are my thoughts as a random vol user.

I tested this against all the images I have and it has meant that I get the extra lines on a lot of images. That's everything major/minor from 4 till now. No regressions on any images I have.

I think if the walk hits the ring-size limit before reaching the newest record, it stops without saying so. On a corrupted or inconsistent image that silently loses records? Maybe add a vollog.debug(...) so it's noticed?

I'm not sure about writing tests like that - not something I have really done before. I'm sure someone else can give you a view on it.

Thanks!
🦊

Suggested in review: if the descriptor IDs are inconsistent and the walk
reaches desc_count descriptors without meeting head_id, records after that
point are not reported. Emit a debug message so this is noticed.
@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Thanks a lot, Eve, for running it across your whole image set, and good catch on the silent stop. Pushed 578c289: when the walk reaches the ring-size bound without meeting head_id, it now emits a vollog.debug with tail_id, head_id and the ring size, and the bounded-walk test asserts that message (and that it is absent on a consistent ring).

@ikelos ikelos added the looks-like-ai AI is ok, but it affects how much effort it takes to review it. label Nov 15, 2026
@ikelos

ikelos commented Nov 15, 2026

Copy link
Copy Markdown
Member

Thanks for this (and for giving it a look over @eve-mem ). This seems reasonable, and the change is small and specific. Thank you for putting the vollog debugging message in place, generally where something unexpected may have happened we like to leave a debug message, so that someone somewhere has a chance of figuring out something's gone wrong. We'll run the tests over this, but otherwise it should be good to go in, thanks.

@cpuu

cpuu commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Thanks for this, @ricardojrdez. I've been running into the missing-final-message issue during my recent Linux memory-forensics work. Before implementing a fix myself, I checked for existing PRs and found this one. It addresses exactly the same issue I was seeing.

I applied your kmsg changes from 578c289 to my existing analysis setup and compared the results on eight memory dumps, keeping the rest of the code, the memory image, and the matching symbols unchanged for each comparison.

The kernel series I tested were 6.8, 6.12, 6.17, 6.18, 7.0, and 7.2:

Capture Kernel series Rows before Rows after
1 6.8 756 757
2 6.8 757 758
3 6.17 775 776
4 7.0 790 791
5 7.0 860 861
6 6.18 709 710
7 6.12 723 724
8 7.2 863 864

Across these dumps, the output increased from 6,233 to 6,241 rows, recovering the missing final record in every case.

All existing rows remained identical, including their fields and ordering. Each added row matched the message and timestamp in the capture-time dmesg records, and its text also matched a direct read of the head descriptor in the same dump. None of these runs hit the ring-size-bound diagnostic.

The five tests included in this PR also passed. The newest-record test failed with the previous implementation, as expected.

This resolves the issue in all eight captures I tested. I plan to build on this fix in my follow-up work. Thanks again!

@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Thanks a lot, @cpuu, for taking the time to validate this.

Glad it unblocks your follow-up work. Also, if you're interested in collaborating on research in Linux memory forensics, I'd be happy to hear from you. Feel free to ping me anytime!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

looks-like-ai AI is ok, but it affects how much effort it takes to review it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants