Skip to main content

cp /dev/null

I am always puzzled when people do

  $ mv file.txt /dev/null
...and get away with it. Well, they shouldn't, because it won't work and /dev/null will get overwritten with a plain file. However, I was suprised to see that cp really works that way, when used with the proper options:
$ cp /dev/null /var/tmp/foo 
$ ls -l /var/tmp/foo 
-rw-r--r-- 1 root root 0 May 27 19:02 /var/tmp/foo

$ cp -R /dev/null /var/tmp/foo 
$ ls -l /var/tmp/foo 
crw-r--r-- 1 root root 1, 3 May 27 19:03 /var/tmp/foo

$ cp /tmp/file.txt /var/tmp/foo 
$ ls -l /var/tmp/foo 
crw-r--r-- 1 root root 1, 3 May 27 19:03 /var/tmp/foo

$ cp -R /tmp/file.txt /var/tmp/foo 
$ ls -l /var/tmp/foo 
crw-r--r-- 1 root root 1, 3 May 27 19:03 /var/tmp/foo
On my way to this adventure I stumbled across this, introducing a new "feature" in busybox:
config FEATURE_NON_POSIX_CP
        bool "Non-POSIX, but safer, copying to special nodes"
        default y
        help
          With this option, "cp file symlink" will delete symlink
          and create a regular file. This does not conform to POSIX,
          but prevents a symlink attack.
          Similarly, "cp file device" will not send file's data
          to the device. (To do that, use "cat file >device")
Ouch :-(