The kernel ring buffer is nothing but a data structure that shows messages related to the operation of the Linux kernel. Like any other buffer, it always s always a constant size, removing the oldest messages when new messages come in. Let us see how to prevent unprivileged users from using dmesg to view messages from the kernel’s log buffer.
How to view dmesg command output on Linux
One can use the dmesg command to see or control the kernel ring buffer. To display all messages from the kernel ring buffer just type the dmesg command:
$ dmesg
OR
$ sudo dmesg
Sample outputs:
It is possible to see more human readable output with the following command:
$ sudo dmesg -H --color
How do I restrict unprivileged access to kernel syslog?
Run the following sysctl command as root user:
$ sudo sysctl -w kernel.dmesg_restrict=1
kernel.dmesg_restrict = 1
To view value, run:
$ sysctl kernel.dmesg_restrict
From the Linux kernel docs:
This toggle indicates whether unprivileged users are prevented from using dmesg(8) to view messages from the kernel’s log buffer. When dmesg_restrict is set to (0) there are no restrictions. When dmesg_restrict is set set to (1), users must have CAP_SYSLOG to use dmesg(8). The kernel config option CONFIG_SECURITY_DMESG_RESTRICT sets the default value of dmesg_restrict.
To make changes permanent add entry to /etc/sysctl.conf file using the sudo and tee command on Linux:
echo 'kernel.dmesg_restrict=1' | sudo tee -a /etc/sysctl.conf
Now unprivileged users will be greeted with the following error message when they run
$ dmesg
Sample outputs:
dmesg: read kernel buffer failed: Operation not permitted
Another session from LXC/KVM container:
Why restrict access to to kernel syslog?
From the lwn mailing list:
The kernel syslog contains debugging information that is often useful during exploitation of other vulnerabilities, such as kernel heap addresses. Rather than futilely attempt to sanitize hundreds (or thousands) of printk statements and simultaneously cripple useful debugging functionality, it is far simpler to create an option that prevents unprivileged users from reading the syslog.
This patch, loosely based on grsecurity’s GRKERNSEC_DMESG, creates the dmesg_restrict sysctl. When set to “0”, the default, no restrictions are enforced. When set to “1”, only users with CAP_SYS_ADMIN can read the kernel syslog via dmesg(8) or other mechanisms.
How do I clear the ring buffer?
Type the following command:
$ sudo dmesg --clear
You can also clear the ring buffer after first printing its contents on screen:
$ sudo dmesg --read-clear
How can I disable the printing of messages to the console?
For security reason or to avoid too much message on the console, run:
$ sudo dmesg --console-off
To turn it on again pass the --console-on option:
$ sudo dmesg --console-on
How do I show kernel messages only?
$ sudo dmesg --kernel
To just display userspace messages, run:
$ sudo dmesg --userspace
I suggest you read dmesg man page or type the following command:
$ dmesg -h
Usage: dmesg [options] Display or control the kernel ring buffer. Options: -C, --clear clear the kernel ring buffer -c, --read-clear read and clear all messages -D, --console-off disable printing messages to console -E, --console-on enable printing messages to console -F, --file <file> use the file instead of the kernel log buffer -f, --facility <list> restrict output to defined facilities -H, --human human readable output -k, --kernel display kernel messages -L, --color[=<when>] colorize messages (auto, always or never) colors are enabled by default -l, --level <list> restrict output to defined levels -n, --console-level <level> set level of messages printed to console -P, --nopager do not pipe output into a pager -r, --raw print the raw message buffer -S, --syslog force to use syslog(2) rather than /dev/kmsg -s, --buffer-size <size> buffer size to query the kernel ring buffer -u, --userspace display userspace messages -w, --follow wait for new messages -x, --decode decode facility and level to readable string -d, --show-delta show time delta between printed messages -e, --reltime show local time and time delta in readable format -T, --ctime show human readable timestamp (may be inaccurate!) -t, --notime don't print messages timestamp --time-format <format> show time stamp using format: [delta|reltime|ctime|notime|iso] Suspending/resume will make ctime and iso timestamps inaccurate. -h, --help display this help and exit -V, --version output version information and exit Supported log facilities: kern - kernel messages user - random user-level messages mail - mail system daemon - system daemons auth - security/authorization messages syslog - messages generated internally by syslogd lpr - line printer subsystem news - network news subsystem Supported log levels (priorities): emerg - system is unusable alert - action must be taken immediately crit - critical conditions err - error conditions warn - warning conditions notice - normal but significant condition info - informational debug - debug-level messages For more details see dmesg(1).
Conclusion
You learned how to use dmesg command restrict unprivileged users from running or viewing outputs from the dmesg command.
🐧 4 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
I had no idea about such option. Finally no more spam from kernel ;)
I think you have a typo in “sudo tee -a /etc/sysctlc.onf” code
thanks for the heads up! the tutorial has been updated.
“echo ‘kernel.dmesg_restrict=1’ | sudo tee -a /etc/sysctlc.onf”
At the end it should read “”sysctl.conf.