Apparmor /dev/net/tun overflow
| Affects | Status | Importance | Assigned to | Milestone | |
|---|---|---|---|---|---|
| libvirt (Ubuntu) |
Fix Released
|
Medium
|
Unassigned | ||
| Jammy |
Fix Released
|
Undecided
|
Unassigned | ||
| Noble |
Fix Released
|
Undecided
|
Hector CAO | ||
| Plucky |
Fix Released
|
Undecided
|
Hector CAO | ||
| Questing |
Fix Released
|
Medium
|
Unassigned | ||
Bug Description
[ Impact ]
* libvirt calls virt-aa-helper (with argument -F)
to append a new rule to the apparmor profile of the VM.
virt-aa-helper does not check for duplicate and blindly
appends the rule to the profile. since there is no rule
removal when a device is detached, this can make the
profile grow in size and even hit the size limit
* Backport the upstream patch that checks and does
not add duplicates over and over again
[ Test Plan ]
Create a guest via libvirt/kvm, for example with uvtool
$ uvt-kvm create --password=ubuntu q2 arch=amd64 label=daily release=questing
Create a network device definition for libvirt in a file
$ cat net-add-test.xml
<interface type='network'>
<source network='default' bridge='virbr0'/>
<target dev='vnet1'/>
<model type='virtio'/>
<alias name='net1'/>
</interface>
Attach and detach that over and over like:
$ for i in $(seq 1 10); do virsh attach-device q2 net-add-test.xml; sleep 1; virsh detach-device q2 net-add-test.xml; done
Get the UUID of the guest
$ virsh dominfo q2 | grep UUID
UUID 33884b16-
In the bad case, this will have many such entries
In the good case no further ones would have been added
$ grep dev.*tun libvirt-
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
"/dev/net/tun" rwk,
[ Where problems could occur ]
* The change is isolated to dynamic apparmor handling, so we talk about rules missed to be added in the worst case. Therefore we'd want to look out for new apparmor denials or generally apparmor related issues to be reported that were not present before.
[ Other Info ]
* n/a
----
I'm using libvirt 10.0.0 libvirt from noble
```
root@vs-
libvirtd (libvirt) 10.0.0
root@vs-
ii libvirt-clients 10.0.0-2ubuntu8.8 amd64 Programs for the libvirt library
ii libvirt-daemon 10.0.0-2ubuntu8.8 amd64 Virtualization daemon
ii libvirt-
ii libvirt-
ii libvirt-
ii libvirt-
ii libvirt-
ii libvirt0:amd64 10.0.0-2ubuntu8.8 amd64 library for interfacing with different virtualization systems
```
Libvirt configuration
```
root@vs-
default_
default_
group = "kvm"
security_driver = "apparmor"
stdio_handler = "logd"
user = "nova"
vnc_listen = "0.0.0.0"
vnc_tls = 1
vnc_tls_x509_verify = 1root@vs-
root@vs-
auth_tcp = "none"
keepalive_count = 9
keepalive_interval = 10
listen_addr = "10.10.0.191"
listen_tcp = 0
listen_tls = 1
log_level = 3
log_outputs = "1:file:
root@vs-
#
# This can be used to setup URI aliases for frequently
# used connection URIs. Aliases may contain only the
# characters a-Z, 0-9, _, -.
#
# Following the '=' may be any valid libvirt connection
# URI, including arbitrary parameters
#uri_aliases = [
# "hail=qemu+
# "sleet=
#]
#
# These can be used in cases when no URI is supplied by the application
# (@uri_default also prevents probing of the hypervisor driver).
#
#uri_default = "qemu:///system"
```
Each time when NIC is added to VM, a new /dev/net/tun appears in /etc/apparmor.
When interface is detach the /dev/net/tun is not removed.
There are two problems related to this
1. Even when all interface removed from domain, it has allowed rules for tun device (which it should not)
2. Overflow is possible when many attach/detach actions are executed file size may grow up to 10Mb limit and apparmor will refuse any other changes.
Related branches
- Lukas Märdian (community): Approve
- git-ubuntu import: Pending requested
-
Diff: 89 lines (+67/-0)3 files modifieddebian/changelog (+7/-0)
debian/patches/series (+1/-0)
debian/patches/ubuntu-aa/lp2120278-virt-aa-helper-Avoid-duplicate-when-append-rule.patch (+59/-0)
- Christian Ehrhardt (community): Approve
- git-ubuntu import: Pending requested
-
Diff: 89 lines (+67/-0)3 files modifieddebian/changelog (+7/-0)
debian/patches/series (+1/-0)
debian/patches/ubuntu-aa/lp2120278-virt-aa-helper-Avoid-duplicate-when-append-rule.patch (+59/-0)
- Christian Ehrhardt (community): Approve
- git-ubuntu import: Pending requested
-
Diff: 90 lines (+68/-0)3 files modifieddebian/changelog (+7/-0)
debian/patches/series (+1/-0)
debian/patches/ubuntu-aa/lp2120278-virt-aa-helper-Avoid-duplicate-when-append-rule.patch (+60/-0)
- Lukas Märdian (community): Approve
- git-ubuntu import: Pending requested
-
Diff: 90 lines (+67/-0)3 files modifieddebian/changelog (+7/-0)
debian/patches/series (+1/-0)
debian/patches/ubuntu-aa/lp2120278-virt-aa-helper-Avoid-duplicate-when-append-rule.patch (+59/-0)
| Changed in libvirt (Ubuntu): | |
| status: | Confirmed → In Progress |
| Changed in libvirt (Ubuntu Questing): | |
| status: | In Progress → Fix Committed |
| Changed in libvirt (Ubuntu Questing): | |
| assignee: | Hector CAO (hectorcao) → nobody |
| Changed in libvirt (Ubuntu Plucky): | |
| assignee: | nobody → Hector CAO (hectorcao) |
| Changed in libvirt (Ubuntu Noble): | |
| assignee: | nobody → Hector CAO (hectorcao) |
| Changed in libvirt (Ubuntu Noble): | |
| status: | New → In Progress |
| Changed in libvirt (Ubuntu Plucky): | |
| status: | New → In Progress |
| description: | updated |
| Changed in libvirt (Ubuntu Jammy): | |
| status: | New → In Progress |

Hi Vasyl,
Let me start with confirming - you are just right - this is a problem.
Although not an intense or common one.
It was discussed back then if leaving the tun interface available for those who had it before is a problem and it was not considered a big one.
But yeah, with enough add/remove it could get too much and break entirely.
The problem is that libvirt apparmor handling has two ways, the initial set which is converting the XML description into rules - that works fine. The later add/removal are just events and the removal usually does not carry much info, so it can be (depends on the case) hard to map it back to what to remove.
I have not checked that case, so it might be easy here (or not).
Sadly these efforts just do not fit well for quite a while, only the more breaking more common issues are tackled. This is somewhere in between, but I agree would be worth at least to check if it is one of the easier or more challenging cases.
I'm tagging this libvirt- apparmor- dev to be together with similar group of "this actually would need a major dev effort around libvirt/apparmor to be better" bugs.