Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:18:17+00:00 2026-06-03T09:18:17+00:00

I am a bit confused with setreuid. Scenario: A process runs as normal user

  • 0

I am a bit confused with setreuid.

Scenario: A process runs as normal user (id: cateof) but for a very short fraction of time needs to be run as root. I have to elevate permissions to root from cateof and then back to normal user. My first thought was that nesting my “root call” between a setreuid(0, 0); and a setreuid(ruid, euid); would be enough, but I was wrong. The only way to go back to normal user is to call setreuid(ruid, euid) twice in a raw after the “root call”.

Here is the code:

int main(...) {
    //check the permission, that the program is setuid
    //become normal user 
    ruid = getuid ();
    euid = geteuid ();
    setreuid(ruid, euid); 
    ...
    setreuid(0, 0);
    root_action();
    setreuid(ruid, euid); //undo root #1
    setreuid(geteuid(), getuid()); //undo root#2

If I don’t call the setreuid(geteuid(), getuid()) at the last line the process keeps running as root. Why do I need to call it twice???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:18:19+00:00Added an answer on June 3, 2026 at 9:18 am

    You’re first call to setreuid is actually a no-op – it does not drop privileges, you are essentially saying set my real uid to my getuid and my effective uid to my geteuid, which in the case of a setuid app is the same as you obtained.

    At the start of a setuid root program run as user bob, then getuid() == bob, geteuid() == root

    If you want to drop privileges, then you should probably have called:

    setreuid(euid, ruid);
    

    Thad would have set the effective uid to bob and the real uid to rood. Everything done after that point will be as if by the unprivileged user bob, understanding that you have not completely dropped the ability to switch back to root privileges at this point, because you have not wiped out the saved-user-id information.

    Obtaining root privileges would be done by:

    setreuid(ruid, euid);
    

    Similarly, at the end, when you are re-dropping privileges, you need to do the same:

    setreuid(euid, ruid);
    

    i.e. set the effective uid to the id bob. the [answer here][1] is a similar situation, which explains the details a little more concisely.

    Generally, when checking this information, a little printerhelper like:

    void printids(char *header) {
        uid_t ruid, euid, saveduid;
        getresuid(&ruid, &euid, &saveduid);
        printf("%s ruid=%d euid=%d saveduid=%d\n", header, ruid, euid, saveduid);
    }
    

    assists in determining the privileges/uid information at all the steps.

    it would be a little simpler to just use seteuid() for the temporary changing of the privileges, rather than the little more heavy handed setreuid(). You can also use the setresuid() call to be more explicit on the setting of the real, effective and saved userid values.

    Saving and dropping privileges:

    setresuid(ruid, ruid, euid);
    

    Re-obtaining root privileges:

    setresuid(euid, euid, -1);
    

    Dropping back to non-root privileges:

    setresuid(ruid, ruid, -1);
    

    i.e. we let the saved user-id keep the root information, and manipulate the realuid and euid values toggling between root/non-root

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bit confused here, I have an on-demand instance but do I get charged even
I am a bit confused in user space and kernel space . Is kernel
A bit confused about how to go about updating rubygems. Process keeps changing (or
I'm a bit confused by the tic function, but I'm not sure if there's
I'm a bit confused, I set up my app as a simple converter but
I'm a bit confused from what I've heard Java doesn't do events. But I
Little bit confused... I am trying to track mailto links being clicked, but constantly
I am little bit confused regarding worst Case time and Avg case Time complexity.
I'm a bit confused about how many controllers I need, and when I can
I'm a bit confused over the .net redistributables... Our C# code uses some API

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.