Responding to the following in the answer from user Benjamin Peter:
I also heard secure boot must be disabled.
With AlmaLinux 8.7 and a 4.18.0-425.3.1.el8.x86_64
kernel was able to read a MSR when secure boot was enabled.
read_smi_count.c is the code for program tested. Was able to run it to successfully read the MSR_SMI_COUNT (0x34) register. The following is the output after had built the program, which prompts what needs to be done to give the user program access to read the MSR register:
[mr_halfword@skylake-alma release]$ read_smi_count/read_smi_count
Error: No permission to open /dev/cpu/0/msr. Try:
sudo chmod o+r /dev/cpu/0/msr
[mr_halfword@skylake-alma release]$ sudo chmod o+r /dev/cpu/0/msr
[sudo] password for mr_halfword:
[mr_halfword@skylake-alma release]$ read_smi_count/read_smi_count
Error: No permission to open /dev/cpu/0/msr. Try:
sudo setcap cap_sys_rawio=ep read_smi_count/read_smi_count
[mr_halfword@skylake-alma release]$ sudo setcap cap_sys_rawio=ep read_smi_count/read_smi_count
[mr_halfword@skylake-alma release]$ read_smi_count/read_smi_count
SMI COUNT = 15240
The output of dmesg
confirms the Kernel is locked down as a result of EFI secure boot being enabled:
mr_halfword@skylake-alma release]$ dmesg|grep lockdown
[ 0.000000] Kernel is locked down from EFI secure boot; see man kernel_lockdown.7
[ 1.578247] Lockdown: swapper/0: Hibernation is restricted; see man kernel_lockdown.7
[ 37.750117] Lockdown: x86_energy_perf: Direct MSR access is restricted; see man kernel_lockdown.7
The lockdown mode is integrity:
[mr_halfword@skylake-alma release]$ cat /sys/kernel/security/lockdown
none [integrity]
The above output doesn't show the option of a lockdown mode of confidentiality. Haven't investigated if confidentiality mode would prevent reading of MSR registers.
Linux kernel lockdown, integrity, and confidentiality notes that confidentiality mode applies additional restrictions to prevent reading of secrets from the Kernel.