Github messages for voidlinux
 help / color / mirror / Atom feed
From: sernkut <sernkut@users.noreply.github.com>
To: ml@inbox.vuxu.org
Subject: Re: [ISSUE] [CLOSED] qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu
Date: Thu, 19 Aug 2021 00:39:13 +0200	[thread overview]
Message-ID: <20210818223913.a34pzCoGblG8jB46U7jpQAfDYTebv8F1JMUXVZDlzTs@z> (raw)
In-Reply-To: <gh-mailinglist-notifications-41a7ca26-5023-4802-975b-f1789d68868e-void-packages-32562@inbox.vuxu.org>

[-- Attachment #1: Type: text/plain, Size: 5099 bytes --]

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.  

<font size="2">(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)</font>

      parent reply	other threads:[~2021-08-18 22:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-18 14:38 [ISSUE] " sernkut
2021-08-18 22:20 ` paper42
2021-08-18 22:39 ` sernkut
2021-08-18 22:39 ` sernkut [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210818223913.a34pzCoGblG8jB46U7jpQAfDYTebv8F1JMUXVZDlzTs@z \
    --to=sernkut@users.noreply.github.com \
    --cc=ml@inbox.vuxu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).