Linux阻止非特权用户查看dmesg
如何限制LXD容器管理程序内的普通用户和root用户查看和使用dmesg命令输出。
如何防止非root(非特权)Linux用户查看Linux内核日志缓冲区?
Linux可以阻止特权用户查看dmesg命令输出吗?
内核环形缓冲区不过是一种数据结构,它显示与Linux内核操作有关的消息。
像任何其他缓冲区一样,它总是始终保持恒定大小,在收到新消息时删除最旧的消息。
让我们看看如何防止无特权的用户使用dmesg从内核日志缓冲区查看消息。
如何在Linux上查看dmesg命令输出
可以使用dmesg命令查看或控制内核环形缓冲区。
要显示来自内核环形缓冲区的所有消息,只需执行dmesg命令:
$ dmesg
或者
$ sudo dmesg
使用以下命令可以看到更多可读的输出:
$ sudo dmesg -H --color
如何限制对内核syslog的非特权访问?
以root用户身份运行以下sysctl命令:
$ sudo sysctl -w kernel.dmesg_restrict=1
kernel.dmesg_restrict = 1
要查看它的值,请运行:
$ sysctl kernel.dmesg_restrict
dmesg_restrict指示是否禁止未特权用户使用dmesg(8)查看来自内核日志缓冲区的消息。
当dmesg_restrict设置为(0)时,没有限制。
将dmesg_restrict设置为(1)时,用户必须具有CAP_SYSLOG才能使用dmesg(8)。
内核配置选项CONFIG_SECURITY_DMESG_RESTRICT设置dmesg_restrict的默认值。
要使更改永久生效,请在Linux上使用sudo和tee命令将条目添加到/etc/sysctl.conf文件中:
echo 'kernel.dmesg_restrict=1' | sudo tee -a /etc/sysctl.conf
现在,非特权用户在运行时将收到以下错误消息
$ dmesg
输出示例:
dmesg: read kernel buffer failed: Operation not permitted
为什么限制对内核syslog的访问?
内核syslog包含调试信息,这些信息在利用其他漏洞(例如内核堆地址)期间通常非常有用。
与其徒劳地尝试清理成百上千的printk语句并同时削弱有用的调试功能,不如创建一个防止无特权用户读取系统日志的选项,要简单得多。
此补丁大致基于grsecuritys GRKERNSEC_DMESG创建了dmesg_restrict sysctl。
设置为默认值0时,不强制执行任何限制。
设置为1时,只有具有CAP_SYS_ADMIN的用户才能通过dmesg(8)或其他机制读取内核syslog。
如何清除环形缓冲区?
执行以下命令:
$ sudo dmesg --clear
您也可以先在屏幕上打印环形缓冲区后清除环形缓冲区:
$ sudo dmesg --read-clear
如何禁用将消息打印到控制台?
出于安全原因或避免在控制台上显示过多消息,请运行:
$ sudo dmesg --console-off
要再次打开它,请通过--console-on选项:
$ sudo dmesg --console-on
如何仅显示内核消息?
$ sudo dmesg --kernel
要仅显示用户空间消息,请运行:
$ sudo dmesg --userspace
查看dmesg手册页
$ 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).

