Verify User Ownership of the GRUB2 Configuration Files

Description

The GRUB2 configuration files in /boot/grub2 and in /boot/efi/EFI, with their subdirectories, which are the files whose names start with grub, such as grub.cfg and grubenv, and the files named user.cfg, should be owned by the root user. To set the owner of the files in /boot/grub2, run the command:

$ sudo find /boot/grub2 -type f \( -name 'grub*' -o -name user.cfg \) -exec chown root {} +

The EFI system partition is a FAT file system, which takes the owner of its files from the uid mount option: set uid=0 on the /boot/efi entry in /etc/fstab.

Rationale

Only root should be able to modify the boot parameters.

Remediation

Shell script

The following script can be run on the host to remediate the issue.

#!/bin/bash

# Remediation is applicable only in certain platforms
if ( rpm --quiet -q grub2-common && rpm --quiet -q kernel-core ) && { ( ! ( [ -f /.dockerenv ] || [ -f /run/.containerenv ] ) ); }; then

newown=""
if id "0" >/dev/null 2>&1; then
  newown="0"
fi

if [[ -z "$newown" ]]; then
  >&2 echo "0 is not a defined user on the system"
else

find -P /boot/grub2/  -type f  ! -user 0 -regextype posix-extended -regex '^(.*/)?(grub[^/]*|user\.cfg)$' -exec chown --no-dereference "$newown" {} \;

find -P /boot/efi/EFI/  -type f  ! -user 0 -regextype posix-extended -regex '^(.*/)?(grub[^/]*|user\.cfg)$' -exec chown --no-dereference "$newown" {} \;

fi

else
    >&2 echo 'Remediation is not applicable, nothing was done'
fi

Ansible playbook

The following playbook can be run with Ansible to remediate the issue.

- name: Gather the package facts
  package_facts:
    manager: auto
  tags:
  - CCE-88597-0
  - configure_strategy
  - file_owner_grub2_config_files
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Set the file_owner_grub2_config_files_newown variable if represented by uid
  ansible.builtin.set_fact:
    file_owner_grub2_config_files_newown: '0'
  when:
  - ( "grub2-common" in ansible_facts.packages and "kernel-core" in ansible_facts.packages
    )
  - not ( ansible_virtualization_type in ["docker", "lxc", "openvz", "podman", "container"]
    )
  tags:
  - CCE-88597-0
  - configure_strategy
  - file_owner_grub2_config_files
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Find /boot/grub2/ file(s) matching ^(.*/)?(grub[^/]*|user\.cfg)$ recursively
  ansible.builtin.command: find -P /boot/grub2/  -type f  ! -user 0 -regextype posix-extended
    -regex "^(.*/)?(grub[^/]*|user\.cfg)$"
  register: files_found
  changed_when: false
  failed_when: false
  check_mode: false
  when:
  - ( "grub2-common" in ansible_facts.packages and "kernel-core" in ansible_facts.packages
    )
  - not ( ansible_virtualization_type in ["docker", "lxc", "openvz", "podman", "container"]
    )
  tags:
  - CCE-88597-0
  - configure_strategy
  - file_owner_grub2_config_files
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Ensure owner on /boot/grub2/ file(s) matching ^(.*/)?(grub[^/]*|user\.cfg)$
  ansible.builtin.file:
    path: '{{ item }}'
    follow: false
    owner: '{{ file_owner_grub2_config_files_newown }}'
    state: file
  with_items:
  - '{{ files_found.stdout_lines }}'
  when:
  - ( "grub2-common" in ansible_facts.packages and "kernel-core" in ansible_facts.packages
    )
  - not ( ansible_virtualization_type in ["docker", "lxc", "openvz", "podman", "container"]
    )
  tags:
  - CCE-88597-0
  - configure_strategy
  - file_owner_grub2_config_files
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Find /boot/efi/EFI/ file(s) matching ^(.*/)?(grub[^/]*|user\.cfg)$ recursively
  ansible.builtin.command: find -P /boot/efi/EFI/  -type f  ! -user 0 -regextype posix-extended
    -regex "^(.*/)?(grub[^/]*|user\.cfg)$"
  register: files_found
  changed_when: false
  failed_when: false
  check_mode: false
  when:
  - ( "grub2-common" in ansible_facts.packages and "kernel-core" in ansible_facts.packages
    )
  - not ( ansible_virtualization_type in ["docker", "lxc", "openvz", "podman", "container"]
    )
  tags:
  - CCE-88597-0
  - configure_strategy
  - file_owner_grub2_config_files
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Ensure owner on /boot/efi/EFI/ file(s) matching ^(.*/)?(grub[^/]*|user\.cfg)$
  ansible.builtin.file:
    path: '{{ item }}'
    follow: false
    owner: '{{ file_owner_grub2_config_files_newown }}'
    state: file
  with_items:
  - '{{ files_found.stdout_lines }}'
  when:
  - ( "grub2-common" in ansible_facts.packages and "kernel-core" in ansible_facts.packages
    )
  - not ( ansible_virtualization_type in ["docker", "lxc", "openvz", "podman", "container"]
    )
  tags:
  - CCE-88597-0
  - configure_strategy
  - file_owner_grub2_config_files
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed