linuxfile-permissionssetuidfile-ownership

Confused: File ownership changed with setuid special permission flag


File Permission with setuid enabled(rws).

File Owner: vaisakh

vaisakh@computer:~/me$ ls -l
total 4
-rwsr-xr-x 1 vaisakh vaisakh   60 May  3 17:05 vaisakh.sh

Switch to an another user var23

vaisakh@computer:~/me$ su var23
Password:

Rechecking the permission

var23@computer:/home/vaisakh/me$ ls -l
total 4
-rwsr-xr-x 1 vaisakh vaisakh   60 May  3 17:05 vaisakh.sh

Executable for var23 too

var23@computer:/home/vaisakh/me$ ./vaisakh.sh
Its vaisakh
total 4
-rwsr-xr-x 1 vaisakh vaisakh   60 May  3 17:05 vaisakh.sh

Checking the write permission.

Note: Only the vaisakh(owner) have write permission.

But since the s(setuid) is enabled, file will execute with owner(vaisakh)'s permission(rws).

( Means it will allow the 'var23' to write to the file )

var23@computer:/home/vaisakh/me$ vim vaisakh.sh

After the var23 edit the file, checking the file permission again.

var23@computer:/home/vaisakh/me$ ls -l
total 4
-rwxr-xr-x 1 var23 var23   67 May  3 17:09 vaisakh.sh
var23@computer:/home/vaisakh/me$

File content vaisakh.sh.

var23@computer:~/var23/Prometheus/me1$ cat vaisakh.sh
#!/bin/sh
echo "Its vaisakh"
ls -l
var23@computer:~/var23/Prometheus/me1$

Question:

  1. Couldn't understand why the ownership changes from vaisakh -> var23(permission too rws -> rwx) ?
  2. What is the minimum permission need by a file, to make use of setuid( let non-owner users to inherit owner permission )? Read-Execute(r_x) !
  3. Does the setuid is only applicable for execute flag ?

    Eg:- If the the actual file permission is 4711, non-owner user can't read it. Why its not elevating the owner permission 4711 and allow others to read the content.


Solution

  • Note: Only the vaisakh(owner) have write permission.

    But since the s(setuid) is enabled, file will execute with owner(vaisakh)'s permission(rws).

    Nope. The setuid bit only make sense for true executables, and not for scripts nor text files.

    My manpage on chmod (1) says (emphasize mine):

    4000 (the setuid bit). Executable files with this bit set will run with effective uid set to the uid of the file owner...

    But when you run a shell script, the executable is indeed /bin/sh (or whatever shell you use).

    So what happened at edit time? vim actually opens the original file (should warn that it is not writable) in read only mode. At save time, the original is renamed as a backup file and a plain new file is created with the current file content. Depending on editor config, the backup file may be immediately removed. So what actually matters is write access to the folder containing the file. BTW, that also explains why the file has lost the setuid bit.