I am running C++ program from user with sudo access rights. Binary was started with ./binName command.
From it i can execute sudo ls with popen function - this is working fine. So i though that i can open some privileged files with open function, but it's return -1 with errno= 13:Permission denied.
So then i though that i need to forcly set my euid with seteuid but it's return -1 with errno= 13:Permission denied
When i start program with sudo ./binName all working fine and i can open any file with open function. What i am doing wrong?
#include <stdio.h>
#include <vector>
#include <cstdint>
#include <cstring>
#include <string>
#include <fcntl.h>
#include <unistd.h>
#define Log(fmt, ...) printf("%s | " fmt, __FUNCTION__, ##__VA_ARGS__)
bool OpenPath(const std::string& path) {
    if (path.empty()) return false;
    const int f = open(path.c_str(), O_RDWR);
    if (f == -1){
        Log("[!] Unable to open %s, errno= %d:%s\n", path.c_str(), errno, strerror(errno));
        return false;
    }
    close(f);
    return true;
}
bool ReadFromStream(const char *command, std::vector<uint8_t> &output)
{
    FILE *fp = popen(command, "r");
    if (!fp)
    {
        Log("[!] Error: Unable to popen\n");
        return false;
    }
    char str[512];
    while (!feof(fp))
    {
        if (fgets(str, sizeof(str), fp))
        {
            const int size = strlen(str);
            output.insert(output.end(), str, str + size);
        }
    }
    pclose(fp);
    return !output.empty();
}
bool HasRights() {
    const __uid_t uid = getuid();
    const __uid_t gid = geteuid();
    bool elevated = uid == 0 || uid != gid;
    Log("uid= %d, gid= %d, elevated= %d\n", uid, gid, elevated);
    if (elevated && OpenPath("/usr/bin/ls")) // it's just a test path
        return true;
    std::vector<uint8_t> buffer;
    if (ReadFromStream("sudo -n ls", buffer)) {
        const std::string str(buffer.begin(), buffer.end());
        if (str != "sudo: a password is required")
        {
            if (seteuid(0) == -1) {
                Log("[!] Unable seteuid, errno= %d:%s\n", errno, strerror(errno));
            }
            return OpenPath("/usr/bin/ls"); // it's just a test path
        }
    }
    return false;
}
int main(int argc, char **argv)
    const auto hasRights = HasRights();
    Log("hasRights= %d\n", hasRights);
    return 0;
}
output without sudo rights = ./binName
HasRights | uid= 1000, gid= 1000, elevated= 0
HasRights | [!] Unable seteuid, errno= 1:Operation not permitted
OpenPath | [!] Unable to open /usr/bin/ls, errno= 13:Permission denied
test | hasRights= 0
output with sudo rights = sudo ./binName
HasRights | uid= 0, gid= 0, elevated= 1
test | hasRights= 1
output when sudo timeout expired = ./binName
HasRights | uid= 1000, gid= 1000, elevated= 0
sudo: a password is required
test | hasRights= 0
