Iwant to restrict both normal users and root user inside LXD container “hypervisor” from viewing and using dmesg command output. How do I prevent non-root (unprivileged) Linux users from viewing the Linux kernel’s log buffer?
One can use dmesg command 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:
Fig.01: dmesg command displaying the Linux kernel ring buffer
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.
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