Skip to content

Conversation

tomponline
Copy link
Member

When a process inside a container accesses /dev/lxd LXD will now check the NSpid status variable of each candidate monitor process to confirm the process has no PIDs in other namespaces. If it does then it can't be the LXC monitor process, even if it has "[lxc monitor]" in its process name.

Inspired by ideas from lxc/incus#2285

Copilot

This comment was marked as outdated.

@tomponline tomponline force-pushed the tp-monitor branch 3 times, most recently from f212c52 to b0c0669 Compare July 17, 2025 10:17
… in devlxdFindContainerForPID

This function checks the candidate monitor PID process's NSpid status variable to ensure it is
not in any other PID namespaces (and thus can't be the monitor process).

Inspired by lxc/incus@25cc63e

Inspired-by: Stéphane Graber <stgraber@stgraber.org>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
License: Apache-2.0
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances process identification for /dev/lxd access by verifying that candidate monitor processes have matching PID namespaces (via NSpid) before associating them with containers.

  • Replaced loadContainerFromLXCMonitorPID with getLXCMonitorContainer, which checks both PPid and NSpid in /proc/<pid>/status, and only loads the container if the process is in the correct namespace.
  • Refactored devlxdFindContainerForPID to use getLXCMonitorContainer during process tree traversal and cleaned up legacy namespace-checking code.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lxd/daemon.go Updated seccomp handler to use getLXCMonitorContainer and treat a nil container as non-error.
lxd/api_devlxd.go Added getLXCMonitorContainer with namespace checks, removed old loader, and refactored PID lookup.
Comments suppressed due to low confidence (1)

lxd/api_devlxd.go:164

  • Consider adding unit tests for getLXCMonitorContainer to cover scenarios where the NSpid matches or does not match the PID, and when the command-line prefix is missing, ensuring the new logic behaves as expected.
// getLXCMonitorContainer returns the container instance for the candidateMonitorPID, but only if:

@tomponline tomponline marked this pull request as ready for review July 17, 2025 11:37
Copy link
Contributor

@roosterfish roosterfish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, only a small comment for clarification.

}

// Fallback to loading all containers and checking their PID namespaces.
// This is the approach used when the process isn't actually a descendant of the container's init process,
// such as when using `lxc exec`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are the following lines related to the container's init process, isn't instance.LoadNodeAll just running a SQL query? Probably I cannot follow the comment here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, should the comment rather be on the loop below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the comment is in the right place.

Its say that we use this fallback approach when the origin process of the devlxd request isn't actually a descendant of the container's init process (which is what happens when accessing /dev/lxd from a shell launched with lxc exec).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see it's just a header for the entire block. Thanks.

@tomponline tomponline merged commit 3ceda67 into canonical:main Jul 17, 2025
30 of 31 checks passed
@tomponline tomponline deleted the tp-monitor branch July 17, 2025 12:52
@tomponline tomponline requested a review from mihalicyn July 17, 2025 12:52
@tomponline tomponline self-assigned this Jul 22, 2025
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