Linux

無法通過不在 /dev 中的節點寫入 /dev/console

  • June 16, 2017

誰能解釋一下這個:

嵌入式 Arm 系統,Linux 3.18.44。沒有 SELinux 或任何東西:

# ls -l /dev/console
crw-------    1 root     root        5,   1 Jan  6 02:40 /dev/console
# ls -l /tmp/console
crw-------    1 root     root        5,   1 Jan  6 02:39 /tmp/console
# echo foo > /dev/console
foo
# echo foo > /tmp/console
-sh: can't create /tmp/console: Permission denied
# ls -ld /tmp
drwxr-xr-x    2 root     root            80 Jan  6 02:39 /tmp
# ls -ld /dev
drwxr-xr-x   11 root     root          5480 Jan  6 02:32 /dev

一些細節來自strace

# strace sh -c 'echo foo > /tmp/console' 2>&1 | grep console
execve("/bin/sh", ["sh", "-c", "echo foo > /tmp/console"], [/* 12 vars */]) = 0
open("/tmp/console", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = -1 EACCES (Pe)

相對:

# strace sh -c 'echo foo > /dev/console' 2>&1 | grep console
execve("/bin/sh", ["sh", "-c", "echo foo > /dev/console"], [/* 12 vars */]) = 0
open("/dev/console", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
foo

它是同一個設備:major 5,minor 1。為什麼設備會關心引用它的文件系統節點的路徑名?如果這就是問題所在,那就是它的樣子:

# mknod -m 600 /tmp/cons c 5 1
# echo foo > /dev/cons
foo
# mknod -m 600 /tmp/cons c 5 1
# echo foo > /tmp/cons
-sh: can't create /tmp/cons: Permission denied

某種“安全劇院”?它可以在 Linux 3.14 下在非常相似的硬體上執行。

/tmp已安裝nodev和 /dev 不是…?

引用自:https://unix.stackexchange.com/questions/371425