LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-15-2015, 03:46 AM   #1
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Rep: Reputation: Disabled
Why the EUID of a setuid program is not 0?


1. I created a script named 'testfile' under the root account and set the permission to be "4755". The code contained in the script is shown below:
Code:
echo $UID
echo $EUID
Then, I switched to my normal user account, say 'glen', and ran that script 'testfile'. What baffled me was that the output was
Code:
1000
1000
Shouldn't it be
Code:
1000
0
as I was running this script under root privilege?

2. Since we are on this topic, hope you guys can also answer me another question related to this.

When I wrote the code above, I first tried to output the uid and euid by calling the function getuid() and geteuid(), which, according the to manual page, return the real user id and effective user id respectively, but the problem was that it didn't work, and nor did it work when I keyed in the command 'getuid' on the terminal directly. It output an error saying that the command 'getuid' was not found. WHY?

In case it matters, my distribution is Ubuntu 14.04

Hope you guys can help me out here, thanks in advance!
 
Old 10-15-2015, 04:48 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hi!

First of all, the getuid() and geteuid() functions are both in the C language. Not the shell. An example:
Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    int UID = 0;
    int EUID = 0;

    UID = getuid();
    EUID = geteuid();

    printf("UID: %d, EUID: %d\n", UID, EUID);

    return 0;
}
Running this C program yields:
Code:
$ ./uid_c 
UID: 501, EUID: 501
...as a regular user, and
Code:
# ./uid_c 
UID: 0, EUID: 0
...as root.
^Note that I run this under OS X, hence 501 as regular user.

But to answer your other question. If you invoke your script as non-root, you will always get a different value than 0. It works as expected.
And here is a thread on the difference between UID and EUID:
http://www.linuxquestions.org/questi...nd-euid-75124/

Best regards,
HMW

Last edited by HMW; 10-15-2015 at 04:50 AM.
 
Old 10-15-2015, 04:52 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
setuid only works on compiled programs, not shell scripts. See http://www.faqs.org/faqs/unix-faq/fa...section-7.html.

getuid() and geteuid() are system calls that can be made from a compiled program, not from a script. In a shell script, UID and EUID serve the same purpose.
 
Old 10-15-2015, 04:54 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Also, the suid bit is not honoured for scripts.
 
Old 10-15-2015, 05:19 AM   #5
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Original Poster
Rep: Reputation: Disabled
Thank you all for replying.

Just to clarify, UID and EUID would be exactly the same if I am running a shell script whose setuid bit is on, but if it is for a compiled program (Does this term exclusively refer to binary file?) and this compiled program has its setuid bit turned on, then I would have the UID reading my UID and EUID reading the root's(i.e. 0). Am I right?

Last edited by glenjoker; 10-15-2015 at 05:23 AM.
 
Old 10-15-2015, 05:44 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by glenjoker View Post
Thank you all for replying.

Just to clarify, UID and EUID would be exactly the same if I am running a shell script whose setuid bit is on, but if it is for a compiled program (Does this term exclusively refer to binary file?) and this compiled program has its setuid bit turned on, then I would have the UID reading my UID and EUID reading the root's(i.e. 0). Am I right?
Only if the ownership of the binary is root.

The setuid bit only flags that the EUID should set to the owner of the binary, and that owner can be set to any UID by root - otherwise it is owned by the creator.

That is why it is considered a security issue. Some places do not want users to be able to give away access to their login - thus having user writable filesystems mounted as "nosuid", prevents users from creating a setuid binary and allowing other users access it (the setuid bit is not honored when the filesystem the binary is on is mounted "nosuid").

Note: it is rather difficult to get a setuid binary secure. There are a LOT of things you must not do - buffer overruns, some system calls (system, popen ...) can be coerced to give away inappropriate access. Also note: the real UID also give the binary access to the users files as well...

Last edited by jpollard; 10-15-2015 at 05:50 AM.
 
Old 10-15-2015, 05:51 AM   #7
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
Only if the ownership of the binary is root.

The setuid bit only flags that the EUID should set to the owner of the binary, and that owner can be set to any UID by root - otherwise it is owned by the creator.

That is why it is considered a security issue. Some places do not want users to be able to give away access to their login - thus having user writable filesystems mounted as "nosuid", prevents users from creating a setuid binary and allowing other users access it (the setuid bit is not honored when the filesystem the binary is on is mounted "nosuid")
Get it. Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
setuid program owned by non-root user hmadhi Linux - Security 4 11-22-2011 01:33 AM
Why won't redhat 4/5 cron run my setuid root program? wingram77090 Programming 4 02-24-2010 07:38 PM
setuid question, or how to run a program with different ID linuxfond Linux - Newbie 3 07-09-2004 04:27 AM
Difference between UID and EUID Ash Linux - Newbie 2 07-23-2003 07:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration