Is the SUID bit broken or am I losing it?

Asked by Chris Wagner

Create a little script/file with the following content:
-------------------------------------------------
#! /bin/sh
echo `whoami`
-------------------------------------------------

Supposing you named that file suid.sh, make it executable:
chris@bethany-laptop:~/Desktop$ chmod +x suid.sh

Now run it:
chris@bethany-laptop:~/Desktop$ ./suid.sh
chris

Okay, that's expected. Now let's change the owner and add the "suid bit":
chris@bethany-laptop:~/Desktop$ sudo chown bethany\: suid.sh
chris@bethany-laptop:~/Desktop$ sudo chmod +s suid.sh

And run it again:
chris@bethany-laptop:~/Desktop$ ./suid.sh
chris

Bah! That's not what I expected. I expected the script to run as user bethany.

Any advice/insight would be appreciated! :)

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Will Nowak
Solved:
Last query:
Last reply:
Revision history for this message
Chris Wagner (chris-wagner) said :
#1

Well, a little more fiddling around reveals that the SUID bit *does* work properly for true executables (what are called "ELF" binaries, I believe); the bit simply does not work for shebang scripts.

I'd still appreciate any additional insight: Why? Is there any simple work-around (aside from the obvious, of writing an executable wrapper)?

Thanks.

Revision history for this message
Best Will Nowak (compbrain) said :
#2

Hi there:

Most modern systems ignore the Setuid bit for shell scripts. [http://en.wikipedia.org/wiki/Setuid] For some info why, checkout http://www.samag.com/documents/s=1149/sam0106a/0106a.htm or google for "setuid shell script dangers".

Best!
-Will

Revision history for this message
Chris Wagner (chris-wagner) said :
#3

Thanks Will!