8

Some time ago while in a conversation in IRC, one user in a channel I was in suggested someone setuid a directory in order for it to inherit the userid on files to solve a problem someone else was having. At the time I spoke up and said "linux doesn't support setuid directories". After that, the person giving the advice showed me a pastebin (http://codepad.org/4In62f13) of his system honouring the setuid permission set on a directory.

Just to explain, when i say "linux doesnt support setuid directories" what I mean is that you can go "chmod u+s directory" and it will set the bit on the directory. However, linux (as i understood it) ignores this bit (on directories).

Try as I might, I just cant quite replicate that pastebin. Someone suggested to me once that it might be possible to emulate the behaviour with selinux - and playing around with rules, its possible to force a uid on a file, but not from a setuid directory permission (that I can see). Reading around on the internet has been fairly uninformative - most places claim "no, setuid on directories does not work with linux" with the occasional "it can be done under specific circumstances" (such as this: http://arstechnica.com/etc/linux/2003/linux.ars-12032003.html)

I dont remember who the original person was, but the original system was a debian 6 system, and the filesystem it was running was xfs mounted with "default,acl". I've tried replicating that, but no luck so far (tried so far with various versions of debian, ubuntu, fedora and centos)

Can anyone clue me in on what or how you get a system to honor setuid on a directory?

1
  • XFS certainly has a mount option (grpid|bsdgroups or nogrpid|sysvgroups) that means that files in directories get created with the group of the directory rather than the group of the owner; perhaps play with that?
    – Zanchey
    Mar 20, 2012 at 15:58

3 Answers 3

7

Setuid for directories does not behave like setgid. Unless, the shell output was from FreeBSD, some one was bored and having a little fun at your expense.

The setuid permission set on a directory is ignored on UNIX and Linux systems.[4] FreeBSD can be configured to interpret it analogously to setgid, namely, to force all files and sub-directories to be owned by the top directory owner.[5]

In FreeBSD, directories behave as if their setgid bit was always set, regardless of its actual value. As is stated in open(2), "When a new file is created it is given the group of the directory which contains it."

http://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories

4
  • I believe that to be the case... however, the guy was pretty certain he did achieve his goal... i then turned to the debian forums because he was using debian, and got this for a response: forums.debian.net/… I possibly posted in a less then idea state and got the cold shoulder. After about a month and bit researching it, im not certain that it definitely cant be done, but i cant understand why debian guys believe its possible without modifying something.
    – Takigama
    Mar 20, 2012 at 15:41
  • If he achieved it, he would be the best source on how it was done. I've researched the topic myself and turned up nothing. Mar 20, 2012 at 16:25
  • @AaronCopley: Do you have another source than wikipedia for the fact that setuid permission set on a directory is ingored on Linux systems? Mar 24, 2013 at 15:00
  • No. But you are welcome to try it and see for yourself. :) Mar 25, 2013 at 20:16
2

Partial answer / work around:

I was trying to do the same thing, I decided not to fight it and try another method. What I tried was access control lists, to set default permissions. (Note: you may need to enable them first).

setfacl -R --set-file=- . <<EOF
# file: testdir/
# owner: testuser
# group: testgroup
user::rwx
group::rwx
group:normaluser:rwx
mask::rwx
other::r-x
default:group::rwx
default:group:normaluser:rwx
default:mask::rwx
default:other::r-x
EOF

Here testuser is used to run tests, and normal user can delete the results, without resulting to using root (each time).

1
  • This adds a new group, but set group id works fine. Sep 20, 2013 at 12:14
-2

From RHEL man chmod

chmod preserves a directory’s set-user-ID and set-group-ID bits unless you explicitly specify otherwise. You can set or clear the bits with symbolic modes like u+s and g-s, and you can set (but not clear) the bits with a numeric mode.

Numerically if I recall correctly, chmod 4711 ./dir adds the set UID bit, chmod 2711 ./dir adds the set gid bit therefor 6711 setting uid + gid inheritance as per the demonstration in pastebin.

Per the man page chmod u+s == chmod 4XXX and chmod g+s == chmod 2XXX

3
  • 1
    no, i understand how to apply them, thats not the problem.
    – Takigama
    Mar 20, 2012 at 14:54
  • (hit enter by mistake) The problem is what they do to a directory, my belief was that setuid on directories was not something linux did, for example (ubuntu 10.10 system): testuser@boson:~$ ls -ald ~ drwsrwsrwx 3 testuser testuser 4096 2012-03-21 01:55 /home/testuser That dir has both setuid and setgid. Now, as the user t t@boson:~$ touch ~testuser/file t@boson:~$ ls -la !$ ls -la ~testuser/file -rw-r--r-- 1 t testuser 0 2012-03-21 01:57 /home/testuser/file Note how the setuid bit on the directory has no effect (what i expect). how do you make a linux system use setuid on a directory?
    – Takigama
    Mar 20, 2012 at 15:04
  • You can clear them with something like =755 or 00755 utilizing the numerics Mar 7, 2014 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .