Github messages for voidlinux
 help / color / mirror / Atom feed
* [ISSUE] qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu
@ 2021-08-18 14:38 sernkut
  2021-08-18 22:20 ` paper42
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: sernkut @ 2021-08-18 14:38 UTC (permalink / raw)
  To: ml

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

New 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>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu
  2021-08-18 14:38 [ISSUE] qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu sernkut
@ 2021-08-18 22:20 ` paper42
  2021-08-18 22:39 ` sernkut
  2021-08-18 22:39 ` [ISSUE] [CLOSED] " sernkut
  2 siblings, 0 replies; 4+ messages in thread
From: paper42 @ 2021-08-18 22:20 UTC (permalink / raw)
  To: ml

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

New comment by paper42 on void-packages repository

https://github.com/void-linux/void-packages/issues/32562#issuecomment-901467601

Comment:
This sounds exactly like the issue in #31904, is there a reason why you created a new issue instead of commenting there?

Your second solution sounds better to me, but we will need to make sure that moving the files will not conflict with #29074. There are more qualified people to decide this.

cc @Hoshpak - maintainer of libvirt and qemu

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu
  2021-08-18 14:38 [ISSUE] qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu sernkut
  2021-08-18 22:20 ` paper42
@ 2021-08-18 22:39 ` sernkut
  2021-08-18 22:39 ` [ISSUE] [CLOSED] " sernkut
  2 siblings, 0 replies; 4+ messages in thread
From: sernkut @ 2021-08-18 22:39 UTC (permalink / raw)
  To: ml

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

New comment by sernkut on void-packages repository

https://github.com/void-linux/void-packages/issues/32562#issuecomment-901474974

Comment:
Right. I'm sorry i don't know what i was thinking when i created a second issue. I was just in a bit of a hurry. I'll close this and create a condensed version as a comment on the other issue. Thank you!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [ISSUE] [CLOSED] qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu
  2021-08-18 14:38 [ISSUE] qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu sernkut
  2021-08-18 22:20 ` paper42
  2021-08-18 22:39 ` sernkut
@ 2021-08-18 22:39 ` sernkut
  2 siblings, 0 replies; 4+ messages in thread
From: sernkut @ 2021-08-18 22:39 UTC (permalink / raw)
  To: ml

[-- 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>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-18 22:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-18 14:38 [ISSUE] qemu/libvirt: virt-aa-helper refuses to create AppArmor VM profiles when using QEMU provided OVMF images from /usr/share/qemu sernkut
2021-08-18 22:20 ` paper42
2021-08-18 22:39 ` sernkut
2021-08-18 22:39 ` [ISSUE] [CLOSED] " sernkut

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).