Update apparmor profile to match upstream

Bug #1992377 reported by Lena Voytek
16
This bug affects 2 people
Affects Status Importance Assigned to Milestone
swtpm
Unknown
Unknown
swtpm (Ubuntu)
Fix Released
Undecided
Lena Voytek
Jammy
Fix Released
Undecided
Lena Voytek
Kinetic
Fix Released
Undecided
Lena Voytek

Bug Description

[Impact]

In its current state, swtpm's apparmor profile has a few restrictions that block common use cases for the software. This includes:
 - Use of vtpm proxy
 - Using one's home folder to manage TPM states
 - Some qemu and libvirt interactions in the tmp directory

Cleaning up these restrictions allows users to run swtpm in these common configurations without messing with local apparmor profiles.

To fix these cases, the swtpm apparmor profile has been updated to match upstream. During the process of bringing the Ubuntu version of the profile upstream, these issues were found and fixed accordingly. More info on these changes can be found here: https://github.com/stefanberger/swtpm/pull/691

[Test Plan]

The fix can be tested by running swtpm in these situations. The following can be used to test using the home folder to manage TPM states using a Windows 11 ISO:

$ sudo apt install swtpm qemu-kvm
$ qemu-img create -f qcow2 win11.img 64G
$ mkdir ~/tpmstatedir
$ swtpm socket --tpm2 --ctrl type=unixio,path=/tmp/swtpm-sock --tpmstate dir=~/tpmstatedir
$ sudo qemu-system-x86_64 -hda win11.img -boot d -m 4096 -enable-kvm -chardev socket,id=chrtpm,path=/tmp/swtpm-sock -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis,tpmdev=tpm0 -cdrom Win11.iso

[Where problems could occur]

This change only decreases apparmor restrictions, so users will not be blocked by any new rules. However, with less restrictions, swtpm is provided with more attack vectors if it were to be compromised. swtpm will no longer be blocked in accessing tmp files that are not its own, and will have additional abilities to manipulate file permissions. If swtpm acted maliciously, it could access and mess with temporary files belonging to other programs.

[Other Info]

This bug has been fixed in kinetic and beyond in version 0.6.3-0ubuntu4.

[Original Description]

When a user uses a tpm state directory for swtpm located somewhere in their home directory, apparmor will deny the creation of a lock file when a qemu vm boots, showing a message such as:

audit: type=1400 audit(1665412130.135:170): apparmor="DENIED" operation="mknod" profile="swtpm" name="/home/.../tpmstatedir/.lock" pid=5218 comm="swtpm" requested_mask="c" denied_mask="c" fsuid=1000 ouid=1000

This is due to a missing line in the apparmor profile that has been added upstream:

owner @{HOME}/** rwk,

To test (using a Windows 11 iso):

$ sudo apt install swtpm qemu-kvm
$ qemu-img create -f qcow2 win11.img 64G
$ mkdir ~/tpmstatedir
$ swtpm socket --tpm2 --ctrl type=unixio,path=/tmp/swtpm-sock --tpmstate dir=~/tpmstatedir
$ sudo qemu-system-x86_64 -hda win11.img -boot d -m 4096 -enable-kvm -chardev socket,id=chrtpm,path=/tmp/swtpm-sock -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis,tpmdev=tpm0 -cdrom Win11.iso

Related branches

Lena Voytek (lvoytek)
Changed in swtpm (Ubuntu Jammy):
assignee: nobody → Lena Voytek (lvoytek)
Changed in swtpm (Ubuntu Kinetic):
assignee: nobody → Lena Voytek (lvoytek)
status: New → In Progress
tags: added: server-todo
Changed in swtpm (Ubuntu Jammy):
status: New → Triaged
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package swtpm - 0.6.3-0ubuntu4

---------------
swtpm (0.6.3-0ubuntu4) kinetic; urgency=medium

  * d/usr.bin.swtpm: Update apparmor profile to match swtpm upstream
    In between adding the apparmor profile to Ubuntu and merging upstream
    additional rules were used to cover more common use cases. (LP: #1992377)
    - The six capability lines fix the broken upstream unit test cases:
      test_ctrlchannel, test_vtpm_proxy, test_tpm2_file_permissions,
      test_tpm2_save_load_state_2_block, and test_tpm2_ctrlchannel2
    - owner @{HOME}/** rwk was added as using a folder in one's home directory
      is common for managing tpm states
    - Access in the tmp directory is further generalized as this is where swtpm
      interacts with qemu and libvirt
    - The ability to read from /etc/nsswitch.conf was added for vtpm proxy to
      work

 -- Lena Voytek <email address hidden> Tue, 11 Oct 2022 10:54:21 -0700

Changed in swtpm (Ubuntu Kinetic):
status: In Progress → Fix Released
tags: added: cetest
Revision history for this message
Lena Voytek (lvoytek) wrote :

Going over this bug again, there are some different requirements for it to be fixed compared to LP: #1989100. As such I'm removing duplicate status for now

tags: removed: server-todo
Lena Voytek (lvoytek)
summary: - Apparmor denies writing to swtpm lock file in user's home directory
+ Update apparmor profile to match upstream
description: updated
Changed in swtpm (Ubuntu Jammy):
status: Triaged → In Progress
Revision history for this message
Stefan Berger (stefanberger) wrote :

Kinetic was updated but not Jammy. What happened?

Revision history for this message
Lena Voytek (lvoytek) wrote :

Jammy got stuck in the upload process. We reuploaded on Tuesday so the changes should land soon

Revision history for this message
Steve Langasek (vorlon) wrote : Please test proposed package

Hello Lena, or anyone else affected,

Accepted swtpm into jammy-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/swtpm/0.6.3-0ubuntu3.1 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested, what testing has been performed on the package and change the tag from verification-needed-jammy to verification-done-jammy. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-jammy. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Changed in swtpm (Ubuntu Jammy):
status: In Progress → Fix Committed
tags: added: verification-needed verification-needed-jammy
Revision history for this message
Lena Voytek (lvoytek) wrote :

Verified the fix on Jammy with the above Windows 11 test case and by checking the profile manually:

# lxc launch ubuntu:22.04 test-swtpm
# lxc exec test-swtpm bash

# cat <<EOF >/etc/apt/sources.list.d/ubuntu-$(lsb_release -cs)-proposed.list
# Enable Ubuntu proposed archive
deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -cs)-proposed restricted main multiverse universe
EOF

# apt update && apt dist-upgrade -y
# apt install swtpm -y

# cat /etc/apparmor.d/usr.bin.swtpm
# vim:syntax=apparmor
# AppArmor policy for swtpm
# Author: Lena Voytek <email address hidden>
# Last Modified: Tue Oct 11 10:53:05 2022

#include <tunables/global>

profile swtpm /usr/bin/swtpm {
  #include <abstractions/base>
  #include <abstractions/openssl>

  # Site-specific additions and overrides. See local/README for details.
  #include <local/usr.bin.swtpm>

  capability chown,
  capability dac_override,
  capability dac_read_search,
  capability fowner,
  capability fsetid,
  capability setgid,
  capability setuid,

  network inet stream,
  network inet6 stream,
  unix (send) type=dgram addr=none peer=(addr=none),
  unix (send, receive) type=stream addr=none peer=(label=libvirt-*),

  /usr/bin/swtpm rm,

  /tmp/** rwk,
  owner @{HOME}/** rwk,
  owner /var/lib/libvirt/swtpm/** rwk,
  /run/libvirt/qemu/swtpm/*.sock rwk,
  owner /var/log/swtpm/libvirt/qemu/*.log rwk,
  owner /run/libvirt/qemu/swtpm/*.pid rwk,
  owner /dev/vtpmx rw,
  owner /etc/nsswitch.conf r,
  owner /var/lib/swtpm/** rwk,
  owner /run/swtpm/sock rw,
}

tags: added: verification-done verification-done-jammy
removed: verification-needed verification-needed-jammy
Revision history for this message
Stefan Berger (stefanberger) wrote :

I tested the proposed package and the test case I described here (https://bugs.launchpad.net/ubuntu/+source/swtpm/+bug/2016744) still does not work:

$ /usr/share/swtpm/swtpm-create-user-config-files --overwrite
Environment variable XDG_CONFIG_HOME is not set. Using ${HOME}/.config.
Writing /home/stefanb/.config/swtpm_setup.conf.
Writing /home/stefanb/.config/swtpm-localca.conf.
Writing /home/stefanb/.config/swtpm-localca.options.

$ swtpm_setup --tpm2 --tpmstate . --overwrite --create-ek-cert
Starting vTPM manufacturing as stefanb:stefanb @ Sun 23 Apr 2023 12:07:17 PM EDT
TPM is listening on Unix socket.
Successfully created RSA 2048 EK with handle 0x81010001.
Could not find @DATAROOTDIR@/swtpm/swtpm-localca in PATH. <<<<<<<<<<
An error occurred. Authoring the TPM state failed.
Ending vTPM manufacturing @ Sun 23 Apr 2023 12:07:17 PM EDT

$ grep DATAROOT /usr/share/swtpm/swtpm-create-user-config-files
create_certs_tool = @DATAROOTDIR@/swtpm/swtpm-localca

The @DATAROOTDIR@ should have been replaced during 'configure'.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Stefan, thanks for testing, but that's a different bug, being handled in a different upload. Specifically, that upload is in the jammy unapproved queue[1] still, and it's not expected that this upload here will fix *that* bug.

1. https://launchpad.net/ubuntu/jammy/+queue?queue_state=1&queue_text=swtpm

Revision history for this message
Stefan Berger (stefanberger) wrote :

Alright, then please ignore my comment

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I plan to get to that other bug today, and more then welcome your testing there when it reaches proposed :)

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I verified the test results and am satisfied that they show the executed planned test case, and that the results are correct.

The package built correctly in all architectures and Ubuntu releases it was meant for.

There are no DEP8 regressions.

There is no SRU freeze ongoing at the moment.

There is no halted phasing on the previous update.

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package swtpm - 0.6.3-0ubuntu3.1

---------------
swtpm (0.6.3-0ubuntu3.1) jammy; urgency=medium

  * d/usr.bin.swtpm: Update apparmor profile to match swtpm upstream
    In between adding the apparmor profile to Ubuntu and merging upstream
    additional rules were used to cover more common use cases. (LP: #1992377)
    - The six capability lines fix the broken upstream unit test cases:
      test_ctrlchannel, test_vtpm_proxy, test_tpm2_file_permissions,
      test_tpm2_save_load_state_2_block, and test_tpm2_ctrlchannel2
    - owner @{HOME}/** rwk was added as using a folder in one's home directory
      is common for managing tpm states
    - Access in the tmp directory is further generalized as this is where swtpm
      interacts with qemu and libvirt
    - The ability to read from /etc/nsswitch.conf was added for vtpm proxy to
      work

 -- Lena Voytek <email address hidden> Wed, 16 Nov 2022 13:54:54 -0700

Changed in swtpm (Ubuntu Jammy):
status: Fix Committed → Fix Released
Revision history for this message
Andreas Hasenack (ahasenack) wrote : Update Released

The verification of the Stable Release Update for swtpm has completed successfully and the package is now being released to -updates. Subsequently, the Ubuntu Stable Release Updates Team is being unsubscribed and will not receive messages about this bug report. In the event that you encounter a regression using the package from -updates please report a new bug using ubuntu-bug and tag the bug report regression-update so we can easily find any regressions.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.