Friday, May 3, 2013

Permissions broken on /bin/chmod

Imagine you have a Linux system and some administrator ran the following command:

# chmod a-x /bin/chmod

If you try reversing that after it's been done you'll get an error.

# chmod a+x /bin/chmod

-bash: /bin/chmod: Permission denied

I was asked this exact scenario in a Job Interview recently. The interesting thing to me is that one of the interviewers was leading me to believe that if you touched a new file (say /bin/changemethen the default permissions for that new file would be rwxr-xr-x. This is incorrect. The default permissions for a new file on Linux are 666. After you apply the default root umask of 022, the permissions on that new file would be 644, or rw-r--r--

So how do you go about fixing this problem? The real resolution is to copy a file that already exists in /bin. For example the file /bin/touch. You can then use dd or cat to get the contents of the /bin/chmod command into the copy file. You will then be able to use the copy file to "chmod" the /bin/chmod file to the correct permissions. See the example below for the actual commands to do this.

# cp /bin/touch /bin/changeme
# cat /bin/chmod > /bin/changeme
Or:
# dd if=/bin/chmod of=/bin/changeme
# /bin/changeme a+x /bin/chmod

Your /bin/chmod command will now function as normal.