禁用 sysrq f (OOM-killer) 但保留其他 sysrq 键可操作

use*_*001 2 magic-sysrq out-of-memory

我正在遵循一个在启动时自动解密硬盘驱动器的指南,使用自生成的密钥和 tpm2 变量,接近尾声时,它使这一点似乎有意义: https: //blastrock.github.io/fde- tpm-sb.html#disable-the-magic-sysrq-key

神奇的 SysRq 键允许运行一些特殊的内核操作。默认情况下,最危险的功能是禁用的,您应该保持这种状态以获得最大的安全性。

例如,其中之一 (f) 将调用 OOM-killer。此功能可能会杀死您的锁屏,从而使恶意用户能够完全访问您的桌面。

问题是我只找到了如何禁用所有sysrq 键,例如https://askubuntu.com/questions/911522/how-can-i-enable-the-magic-sysrq-key-on-ubuntu-desktophttps ://askubuntu.com/questions/11002/alt-sysrq-reisub-doesnt-reboot-my-laptop,使用添加/etc/sysctl.d/90-sysrq.conf以下行的文件:

kernel.sysrq=1
Run Code Online (Sandbox Code Playgroud)

我希望如果可能的话能够使用所有其他键,例如 REISUB,以防系统崩溃,并且只禁用该F键。

我还发现这篇文章https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html,其中提到添加一个位掩码,例如:

  2 =   0x2 - enable control of console logging level
  4 =   0x4 - enable control of keyboard (SAK, unraw)
  8 =   0x8 - enable debugging dumps of processes etc.
 16 =  0x10 - enable sync command
 32 =  0x20 - enable remount read-only
 64 =  0x40 - enable signalling of processes (term, kill, oom-kill)
128 =  0x80 - allow reboot/poweroff
256 = 0x100 - allow nicing of all RT tasks
Run Code Online (Sandbox Code Playgroud)

但我不明白如何仅禁用 sysrq-f,而将所有其他键设置为其默认值。

我的笔记本电脑(debian 12)上的当前设置如下:

kernel.sysrq=1
Run Code Online (Sandbox Code Playgroud)

Sté*_*las 6

如果自启动以来(包括在 initramfs 中)\xc2\xb9 中没有任何进程向/proc/sys/kernel/sysrq(可能通过sysctl命令)写入内容,则默认值将采用内核编译时配置的值。

\n

您可以通过以下方式找到答案:

\n
$ grep -i sysrq "/boot/config-$(uname -r)"\nCONFIG_MAGIC_SYSRQ=y\nCONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x01b6\nCONFIG_MAGIC_SYSRQ_SERIAL=y\nCONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE=""\n
Run Code Online (Sandbox Code Playgroud)\n

对我来说(在 Debian 上也是如此),它默认启用,但使用 0x01b6,即 438 或 0b110110110 作为掩码

\n

要检查当前值:

\n
$ cat /proc/sys/kernel/sysrq\n438\n$ sysctl kernel.sysrq\nkernel.sysrq = 438\n
Run Code Online (Sandbox Code Playgroud)\n

那是 2|4|16|32|128|256 所以:

\n
  2 =   0x2 - enable control of console logging level\n  4 =   0x4 - enable control of keyboard (SAK, unraw)\n 16 =  0x10 - enable sync command\n 32 =  0x20 - enable remount read-only\n128 =  0x80 - allow reboot/poweroff\n256 = 0x100 - allow nicing of all RT tasks\n
Run Code Online (Sandbox Code Playgroud)\n

所以除了:

\n
  8 =   0x8 - enable debugging dumps of processes etc.\n 64 =  0x40 - enable signalling of processes (term, kill, oom-kill)\n
Run Code Online (Sandbox Code Playgroud)\n

drivers/tty/sysrq.c您可以在内核源代码中检查位掩码的哪一位允许哪个键输入。

\n

f允许SYSRQ_ENABLE_SIGNAL值为0x0040,即上面的 64 ,毫不奇怪。

\n

该位还控制e(结束所有任务)、j(解冻所有冻结的 FS)、i(杀死所有任务)。

\n

因此不可能启用除 之外的所有功能f。您能做的e最好的事情f就是通过将446写入.​ijcltpwzm/proc/sys/kernel/sysrq

\n

但是,只有在调试某些与内核相关的问题时,您会失去对计算机的 shell 访问权限,或者没有非管理员能够物理访问连接到计算机的键盘或串行线,我才会偏离更安全的 438 默认值。

\n
\n

\xc2\xb9 还请注意绕过sysrq_always_enabled所有限制的内核命令行参数。

\n