test_cryptsetup.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import os
  2. import subprocess
  3. import infra.basetest
  4. class TestCryptSetup(infra.basetest.BRTest):
  5. # A specific configuration is needed for using cryptsetup:
  6. # - A kernel config fragment enables all the parts needed for
  7. # mounting a LUKS2 volume,
  8. # - Enable OpenSSL for cryptsetup crypto backend library,
  9. # - Enable e2fsprog for formatting a ext4 filesystem.
  10. kern_frag = \
  11. infra.filepath("tests/package/test_cryptsetup/linux-cryptsetup.fragment")
  12. config = \
  13. f"""
  14. BR2_aarch64=y
  15. BR2_TOOLCHAIN_EXTERNAL=y
  16. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  17. BR2_LINUX_KERNEL=y
  18. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  19. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.75"
  20. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  21. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  22. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{kern_frag}"
  23. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  24. BR2_PACKAGE_CRYPTSETUP=y
  25. BR2_PACKAGE_E2FSPROGS=y
  26. BR2_PACKAGE_OPENSSL=y
  27. BR2_TARGET_ROOTFS_CPIO=y
  28. BR2_TARGET_ROOTFS_CPIO_GZIP=y
  29. # BR2_TARGET_ROOTFS_TAR is not set
  30. """
  31. def test_run(self):
  32. disk_file = os.path.join(self.builddir, "images", "disk.img")
  33. self.emulator.logfile.write(f"Creating disk image: {disk_file}")
  34. subprocess.check_call(
  35. ["dd", "if=/dev/urandom", f"of={disk_file}", "bs=1M", "count=20"],
  36. stdout=self.emulator.logfile,
  37. stderr=self.emulator.logfile)
  38. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  39. kern = os.path.join(self.builddir, "images", "Image")
  40. bootargs = ["console=ttyAMA0"]
  41. qemu_opts = ["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
  42. "-initrd", img,
  43. "-drive", f"file={disk_file},if=virtio,format=raw"]
  44. self.emulator.boot(arch="aarch64",
  45. kernel=kern,
  46. kernel_cmdline=bootargs,
  47. options=qemu_opts)
  48. self.emulator.login()
  49. # Check the program can execute.
  50. self.assertRunOk("cryptsetup --version")
  51. passkey = "ThisIsAPassKey."
  52. dev = "/dev/vda"
  53. dm_name = "luks-vol"
  54. dm_dev = f"/dev/mapper/{dm_name}"
  55. mnt_pt = "/mnt/secure-volume"
  56. # Check the device is NOT detected as a LUKS volume, because
  57. # it is not formatted yet.
  58. is_luks_cmd = f"cryptsetup isLuks {dev}"
  59. _, ret = self.emulator.run(is_luks_cmd)
  60. self.assertNotEqual(ret, 0)
  61. # Format the LUKS volume.
  62. cmd = f"echo {passkey} | cryptsetup luksFormat {dev}"
  63. self.assertRunOk(cmd, timeout=30)
  64. # Check the device is now detected as a LUKS device.
  65. self.assertRunOk(is_luks_cmd)
  66. # Dump LUKS device header information.
  67. self.assertRunOk(f"cryptsetup luksDump {dev}")
  68. # Open the LUKS device
  69. luks_open_cmd = f"echo {passkey} | "
  70. luks_open_cmd += f"cryptsetup open --type luks {dev} {dm_name}"
  71. self.assertRunOk(luks_open_cmd, timeout=10)
  72. # Create an ext4 filesystem.
  73. self.assertRunOk(f"mke2fs -T ext4 {dm_dev}", timeout=10)
  74. # Create the mount point directory.
  75. self.assertRunOk(f"mkdir {mnt_pt}")
  76. # Mount the LUKS device.
  77. mount_cmd = f"mount {dm_dev} {mnt_pt}"
  78. self.assertRunOk(mount_cmd)
  79. # Create a plain text file in the mounted filesystem.
  80. msg = "This is a plain text message"
  81. plain_file = f"{mnt_pt}/file.txt"
  82. self.assertRunOk(f"echo '{msg}' > {plain_file}")
  83. # Unmount.
  84. self.assertRunOk(f"umount {mnt_pt}")
  85. # We are supposed to see our plain text message on the
  86. # dm-crypt device.
  87. self.assertRunOk(f"grep -Fq '{msg}' {dm_dev}", timeout=10)
  88. # Close the LUKS device
  89. self.assertRunOk(f"cryptsetup close {dm_name}")
  90. # We are NOT supposed to find our plain text message on the
  91. # encrypted storage device.
  92. _, ret = self.emulator.run(f"grep -Fq '{msg}' {dev}", timeout=10)
  93. self.assertNotEqual(ret, 0)
  94. # Try to open LUKS volume with a wrong password. This is
  95. # expected to fail.
  96. cmd = f"echo 'Wrong{passkey}' | "
  97. cmd += f"cryptsetup open --type luks {dev} {dm_name}"
  98. _, ret = self.emulator.run(cmd, timeout=10)
  99. self.assertNotEqual(ret, 0)
  100. # Check the device-mapper device was NOT created (since we
  101. # tried to open it with a wrong password).
  102. self.assertRunOk(f"test ! -e {dm_dev}")
  103. # Reopen the LUKS device, with the good passkey this time...
  104. self.assertRunOk(luks_open_cmd, timeout=10)
  105. # ...remount...
  106. self.assertRunOk(mount_cmd)
  107. # ...and read back our plain text file. We check we get back
  108. # our original message.
  109. out, ret = self.emulator.run(f"cat {plain_file}")
  110. self.assertEqual(ret, 0)
  111. self.assertEqual(out[0], msg)