1

I am attempting to follow through with a lesson on setuid bits and have done the following on a CentOS 7 VM (via Terminal.app on my Mac):

  1. create user adam & program.sh file in his home folder
  2. program.sh is a basic script that contains:

    echo "This is a test" >> /etc/stickytestfile.txt
    
  3. set chmod 777 as well as chmod u+s to program.sh and /usr/bin/sh

but I still get 'Permission denied'. How should I proceed?

-bash-4.2$ whoami
adam
-bash-4.2$ ls -al
total 32
drwsrwxrwx. 2 adam adam 4096 Oct  7 17:44 .
drwxr-xr-x. 6 root root   58 Oct  7 15:30 ..
-rw-------. 1 adam adam  853 Oct  7 19:26 .bash_history
-rw-r--r--. 1 adam adam   18 Nov 20  2015 .bash_logout
-rw-r--r--. 1 adam adam  193 Nov 20  2015 .bash_profile
-rw-r--r--. 1 adam adam  231 Nov 20  2015 .bashrc
-rw-rw-r--. 1 adam adam   29 Oct  7 16:07 adampersonalfile
-rw-rw-r--. 1 adam adam   27 Oct  7 16:07 adamsharedfile
-rwsrwxrwx. 1 root root   50 Oct  7 17:44 program.sh
-bash-4.2$ whereis sh
sh: /usr/bin/sh /usr/share/man/man1/sh.1.gz
-bash-4.2$ ls -al /usr/bin/sh
lrwxrwxrwx. 1 root root 4 Oct  2 16:05 /usr/bin/sh -> bash
-bash-4.2$ su
Password:
bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[root@localhost adam]# chmod u+s /usr/bin/sh
[root@localhost adam]# ls -al /usr/bin/sh
lrwxrwxrwx. 1 root root 4 Oct  2 16:05 /usr/bin/sh -> bash
[root@localhost adam]# exit
exit
-bash-4.2$ sh program.sh
program.sh: line 1: /etc/stickytest.txt: Permission denied
1

Look at the INVOCATION section of the bash man page, bash(1):

    If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, …, and the effective user id is set to the real user id.  If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.
(Emphasis added.)

If you run bash without the -p option, if it is running setuid (effective user id not equal to the real user id), it immediately “drops privileges”; i.e., reverts to your real identity.

By the way, you’re using the wrong terminology.  The bit you’re talking about is the setuid bit, not the sticky bit.

P.S. I hope the instructions you are following tell you to be sure to set determine bash’s initial mode (permissions bits) and to restore it when you’re done.

  • Hi yes I'm talking about the setuid bit. Hmm the video tutorials never showed the use of -p option. perhaps I'll try that. I have no idea what 'bash's initial mode' is . I'll check on that. Thanks! – calebsuresh Oct 9 '16 at 4:02
  • I mean, if you had done ls -l /usr/bin/bash before doing the chmod, you would have seen that it was -rwxr-xr-x (i.e., 755), and you should change it back to that. chmod u-s should work, but it would have been better to see what it was originally, so you can explicitly change it back to that. It would have been even better to copy bash to bash.tmp, then chmod u+s bash.tmp, and delete bash.tmp when you're done with it. – G-Man Oct 9 '16 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.