macos 10 setuid не работает без причины

Я запускаю этот код, чтобы изменить реальный uid, если процесс:

#include <cstdlib>
#include <cstdio>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

void printstat()
{
    printf("uid: %d, euid: %d\n",getuid(),geteuid());
}

int main(int argc, char** argv)
{
    if (argc < 2)
    {
            return -1;
    }
    int m_targetUid = atoi(argv[1]);
    printstat();
    uid_t realUID = getuid();
    printf("Setting effective uid to %d\n",m_targetUid);
    seteuid(m_targetUid);
    printstat();
    if (m_targetUid != realUID)
    {
            printf("Setting real uid to %d\n",m_targetUid);
            int res = setuid(m_targetUid);
            printf("setuid(%d) returned: %d\n",m_targetUid,res);
            if (0 > setuid(m_targetUid))
            {
                    printf("setuid(%d) failed: %d, getuid() returned %d, geteuid returned %d\n",m_targetUid,errno,realUID,geteuid());
                    exit(-1);
            }
    }
}

согласно справочной странице, функция setuid не должна давать сбой, если эффективный идентификатор пользователя равен указанному uid, но по какой-то причине это не удается. есть идеи?

справочная страница:

    The setuid() function sets the real and effective user IDs and the saved set-user-ID of the current process to the specified value.  The setuid() function is permitted if the effective user ID is that of the
    super user, or if the specified user ID is the same as the effective user ID.  If not, but the specified user ID is the same as the real user ID, setuid() will set the effective user ID to the real user ID.

и это вывод, когда я запускаю его как root:

nnlnb-mm-041: root# /tmp/setuidBug 70
uid: 0, euid: 0
Setting effective uid to 70
uid: 0, euid: 70
Setting real uid to 70
setuid(70) returned: -1
setuid(70) failed: 1, getuid() returned 0, geteuid returned 70

person ItamarBe    schedule 16.12.2014    source источник
comment
Мне удалось решить эту проблему самостоятельно после. оказывается, вы должны изменить euid обратно на 0, чтобы setuid работал. отредактирует сообщение с кодом, который работает.   -  person ItamarBe    schedule 05.01.2015
comment
Не превращайте ответы в вопросы. Вы можете опубликовать свой собственный ответ, если хотите.   -  person nobody    schedule 05.01.2015


Ответы (1)


Наконец-то мне удалось это решить, видимо, в macos вам нужно установить эффективный uid обратно на root, чтобы он работал. код ниже.

#include <cstdlib>
#include <cstdio>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

void printstat()
{
    printf("uid: %d, euid: %d\n",getuid(),geteuid());
}

int main(int argc, char** argv)
{
    if (argc < 2)
    {
            return -1;
    }
    int m_targetUid = atoi(argv[1]);
    printstat();
    uid_t realUID = getuid();
    printf("Setting effective uid to %d\n",m_targetUid);
    seteuid(m_targetUid);
    printstat();
    printf("Setting effective uid to 0\n");
    seteuid(0);
    printstat();
    if (m_targetUid != realUID)
    {
            printf("Setting real uid to %d\n",m_targetUid);
            int res = setuid(m_targetUid);
            printf("setuid(%d) returned: %d\n",m_targetUid,res);
            if (0 > setuid(m_targetUid))
            {
                    printf("setuid(%d) failed: %d, getuid() returned %d, geteuid returned     %d\n",m_targetUid,errno,realUID,geteuid());
                    exit(-1);
            }
    }
    printstat();
}

и теперь вывод:

    uid: 0, euid: 0
    Setting effective uid to 70
    uid: 0, euid: 70
    Setting effective uid to 0
    uid: 0, euid: 0
    Setting real uid to 70
    setuid(70) returned: 0
    uid: 70, euid: 70
person ItamarBe    schedule 05.01.2015