2

I'd like to verify that the current system is using NX bit protection somehow. I know that this can be done by checking the initial lines in the dmesg output. But that's a rolling buffer, so I'm not always going to be able to do it on a long-running system.

Is there another way to verify the NX usage? I can't see any relevant /proc file. I was thinking of checking the memory maps for the loaded modules (is the data section executable), but I can't find any place where I could get them from.

I'm trying to check that with root privileges, but passively (I know you could write a module to do the check actively, but don't want to go that far).

Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309
viraptor
  • 286
  • 1
  • 9
  • Could you accept the solution below if it's ok, I tried hard to elaborate then... If there is anything else to help with this question, please comment. Cheers. – Vlastimil Burián Feb 22 '23 at 23:59

1 Answers1

1

TL;DR

You can use the following one-liner that will show you if your CPU is NX-capable (Link for more info to NX bit on Wikipedia).

grep -m1 nx /proc/cpuinfo

Example output:

flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d

and also check if your kernel was not instructed by the administrator to disable the NX feature with:

grep noexec=off /proc/cmdline

There shall be no output if the protection is on.


dmesg seems to be the only reliable way

But it is just a check if your CPU has the NX ability, thus it will always be more reliable to check with dmesg:

dmesg | grep 'Execute Disable'

It should output:

[    0.000000] NX (Execute Disable) protection: active

Disabling / Enabling

It is however possible to disable the NX feature by the kernel by adding noexec=off to your GRUB.

With my kernel of 5.4 version it is still possible to use it:

  • /proc/cpuinfo still contains the nx string

  • dmesg changes to:

    [    0.000000] NX (Execute Disable) protection: disabled by kernel command line option
    
  • you can use journalctl alternatively:

    journalctl -b | grep 'Execute Disable'
    

Script check

With colors for Linux

#!/bin/sh

# Wiki: https://en.wikipedia.org/wiki/NX_bit

print_ok__continue () { tput setaf 2 2>/dev/null; tput bold 2>/dev/null; printf '%s: OK\n' "$@"; tput sgr0; }

print_error___exit () { tput setaf 1 2>/dev/null; tput bold 2>/dev/null; printf >&2 '%s\n' "$@"; tput sgr0; exit 1; }

if grep -m1 -q nx /proc/cpuinfo; then
    print_ok__continue '[1]: CPU NX bit is present in your CPU flags'
else
    print_error___exit '[1]: CPU NX bit is missing in your CPU flags'
fi

if ! grep -q noexec=off /proc/cmdline; then
    print_ok__continue '[2]: CPU NX bit is not disabled in GRUB line'
else
    print_error___exit '[2]: CPU NX bit is manually disabled by system administrator via GRUB'
fi

if dmesg | grep -q 'NX (Execute Disable) protection: active'; then
    print_ok__continue '[3]: CPU NX bit is used by your Linux kernel'
else
    print_error___exit '[3]: CPU NX bit is not used by your Linux kernel according to dmesg'
fi

printf '%s\n' 'All tests PASSED, congratulations, you have NX bit capable and enabled CPU + OS!'
Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309