Network support for socket CAN

Shannon Barber

Active Member
Jun 27, 2019
15
6
43
48
I know this is a bit of a long-shot but maybe there are some others with similar use-cases.
Right now Proxmox has integrated support for virtual-eth networking to containers and it works well.
I really want to be able to add interfaces to a container for network devices other than Ethernet, namely CAN for our use-case.
(Kernel support is provided via vxcan not vcan, there's a naming discrepancy in the kernel between the eth and can interface drivers.)

Alternatively, are there existing hooks we can use to trigger a script to run when a container is started?
We have scripts that will set-up the bridge between host devices and containers. If we could get it called with some environment variables set telling us all the information about the container and it's network, e.g. container ID and nsid, then we could just hook up our script.

For reference
Bash:
# vxcan pair
candev=${1-can0}
vmid=${2-100}
nsid_cont=$(ip link show veth${vmid}i0 | grep link | awk -F'link-netnsid' '{ print $2 }')
ip link add $candev-vx${vmid} link-netnsid ${nsid_cont} type vxcan peer name vx$candev link-netnsid 0
ip link set dev $candev-vx$vmid mtu 16
ip link set dev $candev-vx${vmid} up
# CAN bridge
cangw -A -s $candev -d $candev-vx${vmid}
cangw -A -d $candev -s $candev-vx${vmid}
 
  • Like
Reactions: g.bnz
Thanks Shannon.

Your script saved the day for me too - i.e. it works for me too.
One observation - "mtu 16" might not be suitable for CAN FD case.

And I agree - it would be better if the problem will be resolved at "Proxmox GUI level".
 
We have non-FD CAN which is why I went out of my way to set the MTU to 16.
it will default to 72 so you can just remove that part if you have FD.

There is a hook system in Proxmox so on container start-up you can call a script.

Put the script (or a symlink) in the /var/lib/vz/snippets/ directory then in the lxc config file add:
hookscript: local:snippets/vxcan_hook.sh

This only works when PVE starts the container.
If you go into the container and tell it to reboot then you'll lose the devices.
Maybe there's a way to hook that as well but we haven't found it yet.

Bash:
#!/bin/bash

set -xume

modprobe can
modprobe can-raw
modprobe can-gw

vmid="$1"
phase="$2"

LOG=~/vxcan.log
printf "%s %s\n" "$0" "$@" >>${LOG}


wait() {
    #vmid=${1-101}
    while ! pct status "${vmid}" | grep running 1>/dev/null; do
        sleep 1;
    done
}

add() {
    candev=${1-can0}
    vmid=${2-101}
    candev_cont=${3-can0}
    candev_index=${4-0}
    wait "${vmid}"
    nsid_cont=$(ip link show "veth${vmid}i0" | grep link | awk -F'link-netnsid' '{ print $2 }')
    ip link add "vxcan${vmid}i${candev_index}_host" link-netnsid "${nsid_cont}" type vxcan peer name ${candev_cont} link-netnsid 0 >>${LOG}
    ip link set up "vxcan${vmid}i${candev_index}_host" >>${LOG}
    cangw -A -s "$candev" -d "vxcan${vmid}i${candev_index}_host" >>${LOG}
    cangw -A -d "$candev" -s "vxcan${vmid}i${candev_index}_host" >>${LOG}
}

del() {
    candev=${1-can0}
    vmid=${2-101}
    candev_index=${3-0}
    nsid_cont=$(ip link show "veth${vmid}i0" | grep link | awk -F'link-netnsid' '{ print $2 }') >>${LOG}
    cangw -D -d can1 -s "vxcan${vmid}i${candev_index}_host" >>${LOG} || true
    cangw -D -s can1 -d "vxcan${vmid}i${candev_index}_host" >>${LOG} || true
    ip link del "vxcan${vmid}i${candev_index}_host" >>${LOG} || true
}

if [[ "$phase" == 'post-start' ]]
then
      add can0 "${vmid}" can0 0
      add can1 "${vmid}" can1 1
fi
 
Last edited:
  • Like
Reactions: g.bnz
Hello Proxmox community!

I would like to share the results of an experiment with application-specific AppArmor confinement inside an unprivileged LXC container.

My goal was to add an additional AppArmor security layer for nginx while preserving the original Proxmox-generated LXC profile, without granting the container additional privileges to manage AppArmor.

After several experiments, I found a working solution that also survives a reboot initiated from inside the container.

1. Environment

  • Proxmox VE 9.2.26
  • pve-container 6.1.18
  • lxc-pve 7.0.0-2
  • AppArmor 4.1.1-pmx1
  • Debian 13 unprivileged LXC
  • Nesting enabled
  • nginx 1.26.3 running entirely as www-data, including the master process
The original LXC AppArmor confinement remains enabled. No securityfs mount or additional AppArmor management permissions are provided to the guest.

2. Loading an application-specific profile

The application profile is stored on the Proxmox host and loaded directly into the existing LXC AppArmor namespace.

For CT 109:

apparmor_parser -r -K -n 'lxc-109_<-var-lib-lxc>' /etc/lxc/ct109-nginx.apparmor
Inside the container, a systemd override for nginx specifies:

[Service]
AppArmorProfile=ct109-nginx
The profile is mandatory, without the optional "-" prefix.

All nginx processes subsequently report:

ct109-nginx (enforce)
3. AppArmor compatibility issues

During testing, I encountered problems involving disconnected paths, inherited file descriptors and UNIX socket permissions.

The following profile worked in my environment:

abi <abi/4.0>,
#include <tunables/global>

profile ct109-nginx flags=(enforce,attach_disconnected.path=/disconnected) {
#include <abstractions/base>
/usr/sbin/nginx rix,
/usr/bin/kill rix,
/etc/nginx/** r,
/srv/sites/** r,
/run/nginx/ rw,
/run/nginx/** rwk,
/var/log/nginx/*.log rw,
network inet stream,
network inet6 stream,
network unix stream,
signal (send,receive) peer=ct109-nginx,
signal (receive) peer=unconfined,
/disconnected/dev/null r,
/disconnected/run/systemd/journal/stdout rw,
}
This is a workload-specific profile for a static HTTP backend, not a universal nginx profile.

Using ABI 4.0 resolved the UNIX socket issues observed during my experiments with the newer ABI. I would appreciate feedback from anyone familiar with these compatibility issues.

4. The problem with Proxmox hookscripts

Initially, I used a standard Proxmox hookscript to load the profile during the post-start phase.

This worked perfectly when starting the container using pct start.

However, after executing systemctl reboot inside the container, the AppArmor namespace was recreated and the custom profile was missing.

The nginx service remained inactive.

After inspecting the installed Proxmox scripts, I found the following behavior in /usr/share/lxc/pve-container-stop-wrapper:

if (unlink("/var/lib/lxc/$vmid/reboot")) {
exec {'systemctl'} 'systemctl', '--no-block', 'restart', "pve-container\@${vmid}.service";
}
The guest-initiated reboot follows a different lifecycle path and, in my tests, does not execute the configured Proxmox post-start hook.

5. Event-driven solution using a systemd drop-in

Instead of modifying Proxmox package files or implementing a periodic watchdog, I moved the initialization logic into an instance-specific systemd drop-in on the host:

/etc/systemd/system/pve-container@109.service.d/90-apparmor.conf
Contents:

[Service]
TimeoutStartSec=150s
ExecStartPre=/var/lib/vz/snippets/ct109-apparmor.sh %i pre-start
ExecStartPost=/usr/bin/lxc-wait -n %i -s RUNNING -t 30
ExecStartPost=/var/lib/vz/snippets/ct109-apparmor.sh %i post-start
The host-side initialization script performs the following operations:

  • Validates the profile file before starting the container.
  • Loads the profile into the container's AppArmor namespace.
  • Waits for the guest systemd to become available.
  • Checks the mandatory AppArmorProfile setting.
  • Starts nginx.
  • Verifies that the nginx master process is running in enforce mode.
The script is owned by root and is not writable from inside the container.

The nginx service is disabled for ordinary guest boot and is started by the initialization script only after the AppArmor profile has been loaded.

The original Proxmox hookscript registration is removed to avoid duplicate execution.

This solution works for both normal Proxmox startup and guest-initiated reboot.

6. Verified results

The following tests passed:

  • Cold start using pct start.
  • Reboot initiated from inside the container.
  • Automatic restoration of the application-specific AppArmor profile.
  • All nginx processes running as www-data.
  • All nginx processes confined by the custom profile in enforce mode.
  • HTTP responses from both static websites.
  • nginx stub_status.
  • Explicit denial of access to a file outside the permitted paths.
  • Container startup refusal when the profile file is missing.
  • Successful recovery after restoring the profile file.
  • nginx log rotation and reopening of the error log.
  • No unexpected AppArmor denials observed during the log rotation test.
No Proxmox package files were modified, no periodic watchdog was required, and the original LXC confinement was preserved.

7. Limitations

The systemd drop-in, initialization script and AppArmor profile are host-local files. They must be provisioned separately on potential migration targets and included in the host recovery procedure.

A full hypervisor reboot and container migration have not yet been tested.

This approach is intended as an additional application security layer, not as a replacement for the existing LXC security model.

8. Question for the Proxmox developers

Is there a more appropriate, officially supported mechanism for loading host-managed application-specific AppArmor profiles into an unprivileged container's namespace?

Would it be possible to introduce a lifecycle hook that executes on every actual container startup, including a reboot initiated from inside the guest?

Native support for additional host-managed AppArmor profiles would be particularly useful for administrators who want application-level mandatory access control without granting AppArmor management privileges to their containers.

I would be interested in hearing your opinions on this approach, its limitations and possible improvements.

Thank you!

Disclosure: ChatGPT assisted with technical research, troubleshooting, and preparation of this post. All experiments were performed on my own infrastructure, and the reported results were verified through actual testing.