Closed issue by sernkut on void-packages repository https://github.com/void-linux/void-packages/issues/32562 Description: # qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu This issue is related to #31904 ### System xuname: - Void 5.13.10_1 x86_64-musl GenuineIntel uptodate rrnFFF Packages: - qemu-6.0.0_3, libvirt-7.6.0_1 ### Expected behavior When using AppArmor, a VM that is using the QEMU provided UEFI OVMF images should start normally. ### Actual behavior When trying to start a VM using the default QEMU provided OVMF UEFI firmware images while AppArmor is setup and running. VM startup fails with `internal error: cannot load AppArmor profile libvirt-{VM-UUID}`. ### Cause The QEMU provided OVMF UEFI firmware image files are under `/usr/share/qemu/*.fd` and this works fine if you are not using AppArmor. But when AppArmor is enabled, libvirtd automatically tries to generate a seperate AppArmor profile for each started VM using `virt-aa-helper`. But when we look into the source code of `virt-aa-helper` ([`src/security/virt-aa-helper.c:454-490:valid_path()`](https://github.com/libvirt/libvirt/blob/master/src/security/virt-aa-helper.c#L454-L490)) we see that there are multiple restricted file paths that will not be added to the AppArmor profile in any circumstance. Here's the aforementioned snippet from the `virt-aa-helper` sources: ``` const char * const restricted[] = { "/bin/", "/etc/", "/lib", "/lost+found/", "/proc/", "/sbin/", "/selinux/", "/sys/", "/usr/bin/", "/usr/lib", "/usr/sbin/", "/usr/share/", "/usr/local/bin/", "/usr/local/etc/", "/usr/local/lib", "/usr/local/sbin/" }; /* these paths are ok for readonly, but not read/write */ const char * const restricted_rw[] = { "/boot/", "/vmlinuz", "/initrd", "/initrd.img", "/usr/share/edk2/", "/usr/share/OVMF/", /* for OVMF images */ "/usr/share/ovmf/", /* for OVMF images */ "/usr/share/AAVMF/", /* for AAVMF images */ "/usr/share/qemu-efi/", /* for AAVMF images */ "/usr/share/qemu-efi-aarch64/" /* for AAVMF images */ }; /* override the above with these */ const char * const override[] = { "/sys/devices/pci", /* for hostdev pci devices */ "/sys/kernel/config/target/vhost", /* for hostdev vhost_scsi devices */ "/etc/libvirt-sandbox/services/" /* for virt-sandbox service config */ }; ``` From the snippet above you can see that `/usr/share/` is restricted, so its not allowed to be referenced in autogenerated AppArmor profiles by `virt-aa-helper`. There are allowed paths under `/usr/share` which are listed in `restricted_rw`. But because the files are under `/usr/share/qemu` when `virt-aa-helper` tries to generate the VM AppArmor profile and if it stumbles across the path `/usr/share/qemu/edk2-x86_64-code.fd` it checks and realizes that the path is in `restricted`, but not in `restricted_rw` and so it skips this file. But then `virt-aa-helper` also [checks and realizes](https://github.com/libvirt/libvirt/blob/master/src/security/virt-aa-helper.c#L1441-L1442) that the generated VM definition is invalid without the UEFI firmware image path, so it errors out completly and doesn't write any AppArmor profile. ### Possible Fixes #### 1. Patch virt-aa-helper's `restricted_rw` to include `/usr/share/qemu` This would work, but might introduce security issues. #### 2. Move files from `/usr/share/qemu` to `/usr/share/ovmf` or `/usr/share/edk2` NOTE: These are the instructions for the x86_64 non secure boot OVMF image, but it's basically the same for the other images. First check the files we need to copy from `/usr/share/qemu/firmware/60-edk2-x86_64.json` and look for the executable and nvram-template filepaths. Then copy the required OVMF firmware files to the `/usr/share/ovmf` directory: ``` mkdir /usr/share/ovmf cp /usr/share/qemu/{edk2-i386-vars.fd,edk2-x86_64-code.fd} /usr/share/ovmf ``` Lastly you need the copy the file from the first step and rename it to eg. `60-edk2-x86_64-custom.json` and then update the paths in that copy to the files in `/usr/share/ovmf`. (The files in `/usr/share/qemu/firmware/` are used by virt-manager when searching for UEFI firmware images) ### Conclusion The safest fix probably would be fix no. 2. On Arch, you can use the OVMF UEFI images by just installing the `ovmf` package which places the files in the correct place (firmware in `/usr/share/ovmf` and definitions used by virt-manager in `/usr/share/qemu/firmware/`). Also Arch doesn't provide any UEFI firmware in the `qemu` package. So a seperate UEFI firmware package is also an option. (Sorry for any grammar/spelling mistakes as english isn't my native language. This is also my first ever issue to anything anywhere so sorry if i did something wrong)