Skip to content

linux: follow list pointers in walk_internal_list (keyboard_notifiers) - #2102

Open
ricardojrdez wants to merge 2 commits into
volatilityfoundation:developfrom
reverseame:fix/linux-walk-internal-list
Open

ricardojrdez wants to merge 2 commits into
volatilityfoundation:developfrom
reverseame:fix/linux-walk-internal-list

Conversation

@ricardojrdez

Copy link
Copy Markdown
Contributor

linux.malware.keyboard_notifiers reports the address of the first notifier_block as a callback and never reports the real callbacks.

LinuxUtilities.walk_internal_list() receives the list head's pointer member (keyboard_notifier_list.head) and builds each element with vmlinux.object(struct_name, offset=list_start.vol.offset). vol.offset of a pointer object is where the pointer is stored, not where it points. The first "element" is therefore built on top of the head member itself: its notifier_call is the value of head (the address of the first notifier_block), and its next is whatever follows head in memory, which ends the walk. The real notifier_blocks and their notifier_calls are never visited. keyboard_notifiers is the only caller of walk_internal_list in the tree.

Change

Dereference the pointer to find each element (list_start.dereference().vol.offset), and use that address for the repeat-entry check. The readable check now runs before the address is used. LinuxUtilities is bumped to 2.4.1; the method signature is unchanged and the docstring now says that list_start is a pointer to the first element.

Testing

Added test/test_linux_walk_internal_list.py, which walks a fake list whose head pointer is stored at a different address from the first element. It checks the yielded element addresses, the empty list, a cycle, and max_count.

pytest -q --volatility=vol.py test/test_linux_walk_internal_list.py   # 4 passed (develop: 3 failed)
ruff check / ruff format --check                                      # clean

Real image: Linux 6.18.59 x86-64 (VirtualBox core) with an out-of-tree module that calls register_keyboard_notifier() with memmodel_kbd_nb.notifier_call = memmodel_kbd_notify. Addresses from the guest's /proc/kallsyms taken from the same boot:

Address Module
guest memmodel_kbd_notify = 0xffffffffc0601010, memmodel_kbd_nb = 0xffffffffc054b020 memmodel_hook
develop (77ff0b1) 0xffffc054b020 (the notifier_block) N/A
This PR 0xffffc0601010 (the callback) memmodel_hook

On an Ubuntu 24.04 (6.8.0-117) image with no registered keyboard notifiers, both develop and this PR return an empty list.

The Symbol column is still N/A for a callback inside a module; that comes from module_lookup_by_address() and is not changed here.

walk_internal_list built each element at the address of the pointer it was
given (list_start.vol.offset) instead of the address it points to. For
linux.malware.keyboard_notifiers, the list head pointer was therefore read as a
notifier_block: its notifier_call was the head value (the address of the first
notifier_block), and the real callbacks were never visited. Dereference the
pointer to locate each element.
@eve-mem

eve-mem commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Thanks for this.

Here are my comments as a random vol user,

Firstly - something that isn't covered in my testing library! Doh! I built two images with speakup loaded, which registers a real keyboard notifier: one on Ubuntu 18.04 (4.15.0-213) and one on 24.04 (6.8.0-117). On both, develop reports keyboard_notifier_block and this PR reports keyboard_notifier_call. I confirmed both against /proc/kallsyms in the guest - so that is good.

I haven't been able to test the positive case against everything (e.g. where it should find something) but running this against all the other images doesn't accidentally break anything as far as I can see.

For the if not list_start.is_readable() I think it would be wise to do a debug log line to show that something was being skipped.

Again I'm not sure on the the testing bits, my only comment is being absolutely sure with the asserts that we're not off by one etc. It's far to easy to do.

Thanks
🦊

- Emit a debug message with the pointer location and target when walk_internal_list
  stops on an unreadable pointer, so skipped list elements are visible.
- max_count is documented as the maximum number of yielded elements, but the walk
  yielded max_count + 1. Check the bound before yielding.
- Tests: assert the exact elements under max_count, the max_count == length boundary,
  and the unreadable-pointer stop and log.
@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Thanks a lot for testing this, and for building the speakup images. It's good to have a real notifier confirmed against /proc/kallsyms on both 4.15 and 6.8.

I've pushed 486a486 with your suggestions:

  • Debug log on unreadable pointers: when the walk stops at if not list_start.is_readable(), it now logs where the pointer is and what it points to, e.g. walk_internal_list: Pointer at 0x2008 to unreadable notifier_block at 0xdead000. Stopping enumeration.
  • Off-by-one: you were right to be suspicious. max_count is documented as the maximum number of elements yielded, but the loop yielded max_count + 1. This was already in develop, and my test asserted the wrong behaviour (len == 4 for max_count=3). The bound is now checked before yielding.
  • Tests: the max_count test now checks the exact addresses yielded, not just how many. There's a new boundary case (a list of exactly max_count elements yields all of them), and a new test for a non-null but unreadable pointer partway through the list, which checks that the walk stops and the debug message is logged.

@eve-mem

eve-mem commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Great - thanks for making those changes.

I still feel a bit uneasy around the testing - adding things like class FakePointer seems useful for this partiucalr testing - but I'm wondering if there needs to be some bigger planning on how to do this kind of testing in general so that something sensible, maintainable, and reusable exists - but that is all above my pay grade really.

@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Thanks @eve-mem!

I agree with you on the testing side. FakePointer gets the job done here, but it's a local workaround. If more plugins need tests like this, each one will end up writing its own slightly different mocks for pointers, lists and structures. That will be hard to maintain, though.

A first step could be to move this kind of helper into a shared module under test/, so other plugin tests can reuse it instead of copying it. Longer term, it might be worth deciding how unit tests should build symbol-backed objects without a full memory image. One option that comes to my mind is a minimal ISF plus an in-memory layer, which would test the real object code rather than duck-typed fakes.

I'd keep that out of this PR so the fix stays small. I'm happy to open a separate issue so the maintainers can weigh in on the approach, and I can help with the work once there's agreement on a direction.

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