test_audit.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import infra.basetest
  3. class TestAudit(infra.basetest.BRTest):
  4. # This test needs a Kernel with the audit support (the builtin
  5. # test Kernel does not have this support). Since the audit support
  6. # enabled by default, a kernel fragment is not required.
  7. config = \
  8. """
  9. BR2_aarch64=y
  10. BR2_TOOLCHAIN_EXTERNAL=y
  11. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  12. BR2_LINUX_KERNEL=y
  13. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  14. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.6.58"
  15. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  16. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  17. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  18. BR2_PACKAGE_AUDIT=y
  19. BR2_TARGET_ROOTFS_CPIO=y
  20. BR2_TARGET_ROOTFS_CPIO_GZIP=y
  21. # BR2_TARGET_ROOTFS_TAR is not set
  22. """
  23. def test_run(self):
  24. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  25. kern = os.path.join(self.builddir, "images", "Image")
  26. self.emulator.boot(arch="aarch64",
  27. kernel=kern,
  28. kernel_cmdline=["console=ttyAMA0"],
  29. options=["-M", "virt",
  30. "-cpu", "cortex-a57",
  31. "-m", "256M",
  32. "-initrd", img])
  33. self.emulator.login()
  34. # We check the program can run by showing its version. This
  35. # invocation also checks the Kernel has the audit support
  36. # enabled.
  37. self.assertRunOk("auditctl -v")
  38. # We define a normal user name for this test.
  39. user = "audit-test"
  40. # Audit rule inspired from auditctl manual page examples.
  41. # We add an audit rule logging write access on the
  42. # system password file.
  43. cmd = "auditctl -a always,exit -F path=/etc/shadow -F perm=wa"
  44. self.assertRunOk(cmd)
  45. # We do a read-only access on this file, as the root user.
  46. self.assertRunOk("cat /etc/shadow")
  47. # We check our previous read-only access did NOT generated an
  48. # event record.
  49. ausearch_cmd = "ausearch --format text"
  50. out, ret = self.emulator.run(ausearch_cmd)
  51. self.assertEqual(ret, 0)
  52. open_shadow_str = "acting as root, successfully opened-file /etc/shadow"
  53. self.assertNotIn(open_shadow_str, "\n".join(out))
  54. # We create a normal user. This will modify the shadow password file.
  55. cmd = f"adduser -D -h /tmp -H -s /bin/sh {user}"
  56. self.assertRunOk(cmd)
  57. # We are now expecting an event record of this modification.
  58. out, ret = self.emulator.run(ausearch_cmd)
  59. self.assertEqual(ret, 0)
  60. self.assertIn(open_shadow_str, "\n".join(out))
  61. # We add a new audit rule, recording failed open of the system
  62. # password file.
  63. cmd = "auditctl -a always,exit -S openat -F success=0 -F path=/etc/shadow"
  64. self.assertRunOk(cmd)
  65. # We attempt to read the system password file as our new
  66. # normal user. This command is expected to fail (as only root
  67. # can root is supposed to read this file).
  68. cmd = f"su - {user} -c 'cat /etc/shadow'"
  69. _, ret = self.emulator.run(cmd)
  70. self.assertNotEqual(ret, 0)
  71. # Our last failed read attempt is supposed to have generated
  72. # an event. We check we can see it in the log.
  73. out, ret = self.emulator.run(ausearch_cmd)
  74. self.assertEqual(ret, 0)
  75. evt_str = f"acting as {user}, unsuccessfully opened-file /etc/shadow"
  76. self.assertIn(evt_str, "\n".join(out))