Problem
You've run something like the following and accidentally removed the execute permission from /bin/chmod:
[ec2-user@ip-172-31-30-6 ~]$ sudo chmod -x /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rw-r--r-- 1 root root 54384 Jan 23 2020 /bin/chmod
...
[root@ip-172-31-30-6 ~]# /bin/chmod +x /usr/bin/netstat
-bash: /bin/chmod: Permission denied
Now you can't execute chmod, to change the permissions on any files on the system including chmod itself. Below are a couple of ways to fix it.
Solution
Use the ld.so and ld-linux.so* dynamic loader to execute chmod:
According to its man page [1], "The programs ld.so and ld-linux.so* find and load the shared libraries needed by a program, prepare the program to run, and then run it.".
We can use this to execute chmod despite the fact it doesn't have execute permissions, and undo our mistake. Before doing so, we first need to find the ld linux binary. In Amazon Linux 2, I found ld.so under /usr/lib64/ld-2.26.so.
[ec2-user@ip-172-31-30-6 ~]$ sudo find /usr/lib64 -name "ld*.so*"
/usr/lib64/ld-2.26.so
/usr/lib64/ld-linux-x86-64.so.2
...
Now that we've found them we can use either one of them to execute chmod:
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rw-r--r-- 1 root root 54384 Jan 23 2020 /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ sudo /usr/lib64/ld-2.26.so /bin/chmod +x /bin/chmod
Finally we verify that the issue is resolved and we can execute chmod to our hearts content:
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rwxr-xr-x 1 root root 54384 Jan 23 2020 /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ sudo /bin/chmod +x /usr/bin/netstat
[ec2-user@ip-172-31-30-6 ~]$
Using Perl
An example is shown below:
[ec2-user@ip-172-31-30-6 ~]$ sudo chmod -x /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rw-r--r-- 1 root root 54384 Jan 23 2020 /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ sudo perl -e 'chmod(0755, "/bin/chmod")'
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rwxr-xr-x 1 root root 54384 Jan 23 2020 /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$
As you can see chmod has execute permissions once again.Rsync from another server
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rw-r--r-- 1 root root 54384 Jan 23 2020 /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ rsync -av SOURCE_SERVER:/bin/chmod /tmp/chmod
...
[ec2-user@ip-172-31-30-6 ~]$ sudo mv /tmp/chmod /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rwxr-xr-x 1 root root 54384 Jan 23 2020 /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$
Making a copy and replacing its contents
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rw-r--r-- 1 root root 54384 Jan 23 2020 /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ sudo cp /bin/chown /bin/chmod2
[ec2-user@ip-172-31-30-6 ~]$ sudo rsync /bin/chmod /bin/chmod2
[ec2-user@ip-172-31-30-6 ~]$ sudo /bin/chmod2 +x /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$ sudo rm -f /bin/chmod2
[ec2-user@ip-172-31-30-6 ~]$ ls -l /bin/chmod
-rwxr-xr-x 1 root root 54384 Jan 23 2020 /bin/chmod
[ec2-user@ip-172-31-30-6 ~]$
Using a Live CD
[1] ld-linux(8) - Linux man page
https://linux.die.net/man/8/ld-linux
[2] chmod - Perldoc Browser
https://perldoc.perl.org/functions/chmod
No comments:
Post a Comment