Search code examples
linuxunixsolarissetuid

Purpose of issetugid?


According to the man pages for issetugid, the call is supposed to either (1) alert to uid/gid changes; or (2) alert to a possible tainted environment. The function name suggests a third purpose.

First question: what is it purpose?

When I look at the implementations available (for example, on Linux system as a library since Linux kernel does not provide the API), I find the following:

if (getuid() != geteuid()) return 1; 
if (getgid() != getegid()) return 1; 
return 0; 

On Solaris, it looks as follows:

return ((curproc->p_flag & SUGID) != 0);

I'm a bit suspicious, but that's partially because its difficult understand what functions like geteuid and getegid return across all platforms - for example, BSD, Linux, Unix and Solaris.

Second question: is the Linux code semantically equivalent to Solaris code?

Third question: are geteuid and getegid implemented the same across platforms? How about for systems that have I three id's play - real, effective, and saved?

Fourth question: is the effective id the only id's that matter here?

If a process starts as UID = 0 and temporarily drops privileges, then the saved id's come into play. A process that temporarily drops root does not need to exec and should not be tainted.

Fifth question: is a process that temporarily drops root tainted?

Sixth question: should a process whose effective id is the saved id be considered tainted?


Solution

  • Six questions is a bit much to answer in a system designed for one question to answer, especially if no one person knows the answers to all six, but I'll try...

    1) The purpose of issetugid() is to let libraries know if they're being used in a program that was run with raised privileges so they can avoid risky behavior such as trusting LD_LIBRARY_PATH, NLSPATH, etc. environment variables that would let the caller load modules that can abuse the raised privileges. You can see some historical discussions on it like this ncurses 4.1 security bug thread.

    2) That code appears to be less secure than the BSD & Solaris versions, since it doesn't take into account the saved setid bits.

    3) They probably have different implementations on different kernels - look at the platform source code to find out.

    4, 5 & 6) No, yes, yes - a process that can change its euid or egid back to higher levels should still not trust environment variables that cause it to load user-provided code to exploit them.