Why mknod Requires Root Privileges – File Security Explained

devicesfilesSecurity

I noticed that the mknod command required root privileges when a creating a node other than a regular file, FIFO or a Unix socket.

Why is that? How can a regular user harm a system or compromise other users' privacy with the mknod command that he can't when creating regular files?

Best Answer

If you could call mknod arbitrarily, then you could create device files owned and accessible by you for any device. The device files give you unlimited access to the corresponding devices; therefore, any user could access devices arbitrarily.

For instance, suppose /dev/sda1 holds a file system to which you have no access. (Say, it is mounted to /secret). Over here, /dev/sda1 is block special 8,1, so if you could call mknod, e.g. mknod ~/my_sda1 b 8 1, then you could access anything on /dev/sda1 through your own device file for /dev/sda1 regardless of any filesystem restrictions on /dev/sda1. (You get the device as a flat file without any structure, so you would need to know what to do with it, but there are libraries for accessing block device files.)

Likewise, if you could create your own copy of /dev/mem or /dev/kmem, then you could examine anything in main memory; if you could create your own copy of /dev/tty* or /dev/pts/*, then you could record any keyboard input - and so on.

Therefore, mknod in the hand of ordinary users is harmful and thus its use must be restricted.

N.B. This is why the nodev mount option is crucial for mobile devices, for otherwise you could bring in your own device files on prepared mobile media.

Related Question