2014-12-16 4 views
1

Я бег этого кода, чтобы изменить реальный идентификатор пользователя, если процесс:MacOS 10 УИП недостатка без причины

#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); 
      } 
    } 
} 

по странице человека, то УИП functino не должен потерпеть неудачу, если эффективная идент равный указанному 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. 

и это выход, когда я запустить его в качестве корня:

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 
+0

мне удалось решить эту проблему на моем собственном после этого. оказывается, вы должны изменить euid на 0 для работы setuid. отредактирует сообщение с кодом, который работает. – ItamarBe

+0

Не редактируйте ответы на вопросы. Вы можете оставить свой собственный ответ, если хотите. –

ответ

0

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

#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 

 Смежные вопросы

  • Нет связанных вопросов^_^