snapd-apparmor 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/sh
  2. # This script is provided for integration with systemd on distributions where
  3. # apparmor profiles generated and managed by snapd are not loaded by the
  4. # system-wide apparmor systemd integration on early boot-up.
  5. #
  6. # Only the start operation is provided as all other activity is managed by
  7. # snapd as a part of the life-cycle of particular snaps.
  8. #
  9. # In addition the script assumes that the system-wide apparmor service has
  10. # already executed, initializing apparmor file-systems as necessary.
  11. # NOTE: This script doesn't set -e as it contains code copied from apparmor
  12. # init script that also does not set it. In addition the intent is to simply
  13. # load application profiles, as many as we can, even if for whatever reason
  14. # some of those fail.
  15. # The following portion is copied from /lib/apparmor/functions as shipped by Ubuntu
  16. # <copied-code>
  17. SECURITYFS="/sys/kernel/security"
  18. export AA_SFS="$SECURITYFS/apparmor"
  19. # Checks to see if the current container is capable of having internal AppArmor
  20. # profiles that should be loaded. Callers of this function should have already
  21. # verified that they're running inside of a container environment with
  22. # something like `systemd-detect-virt --container`.
  23. #
  24. # The only known container environments capable of supporting internal policy
  25. # are LXD and LXC environment.
  26. #
  27. # Returns 0 if the container environment is capable of having its own internal
  28. # policy and non-zero otherwise.
  29. #
  30. # IMPORTANT: This function will return 0 in the case of a non-LXD/non-LXC
  31. # system container technology being nested inside of a LXD/LXC container that
  32. # utilized an AppArmor namespace and profile stacking. The reason 0 will be
  33. # returned is because .ns_stacked will be "yes" and .ns_name will still match
  34. # "lx[dc]-*" since the nested system container technology will not have set up
  35. # a new AppArmor profile namespace. This will result in the nested system
  36. # container's boot process to experience failed policy loads but the boot
  37. # process should continue without any loss of functionality. This is an
  38. # unsupported configuration that cannot be properly handled by this function.
  39. is_container_with_internal_policy() {
  40. ns_stacked_path="${AA_SFS}/.ns_stacked"
  41. ns_name_path="${AA_SFS}/.ns_name"
  42. ns_stacked
  43. ns_name
  44. if ! [ -f "$ns_stacked_path" ] || ! [ -f "$ns_name_path" ]; then
  45. return 1
  46. fi
  47. read -r ns_stacked < "$ns_stacked_path"
  48. if [ "$ns_stacked" != "yes" ]; then
  49. return 1
  50. fi
  51. # LXD and LXC set up AppArmor namespaces starting with "lxd-" and
  52. # "lxc-", respectively. Return non-zero for all other namespace
  53. # identifiers.
  54. read -r ns_name < "$ns_name_path"
  55. if [ "${ns_name#lxd-*}" = "$ns_name" ] && \
  56. [ "${ns_name#lxc-*}" = "$ns_name" ]; then
  57. return 1
  58. fi
  59. return 0
  60. }
  61. # This terminates code copied from /lib/apparmor/functions on Ubuntu
  62. # </copied-code>
  63. case "$1" in
  64. start)
  65. # <copied-code>
  66. if [ -x /usr/bin/systemd-detect-virt ] && \
  67. systemd-detect-virt --quiet --container && \
  68. ! is_container_with_internal_policy; then
  69. exit 0
  70. fi
  71. # </copied-code>
  72. if [ "$(find /var/lib/snapd/apparmor/profiles/ -type f | wc -l)" -eq 0 ]; then
  73. exit 0
  74. fi
  75. for profile in /var/lib/snapd/apparmor/profiles/*; do
  76. # Filter out profiles with names ending with ~, those are temporary files created by snapd.
  77. test "${profile%\~}" != "${profile}" && continue
  78. echo "$profile"
  79. done | xargs \
  80. -P"$(getconf _NPROCESSORS_ONLN)" \
  81. apparmor_parser \
  82. --replace \
  83. --write-cache \
  84. --cache-loc=/var/cache/apparmor \
  85. -O no-expr-simplify \
  86. --quiet
  87. ;;
  88. esac