osx - macos 10 setuid failing for no reason -
i'm running code change real uid if process:
#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 %d\n",m_targetuid); seteuid(m_targetuid); printstat(); if (m_targetuid != realuid) { printf("setting real uid %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); } } }
according man page, setuid functino shouldn't fail if effective userid equal specified uid, reason fails. ideas?
man page:
setuid() function sets real , effective user ids , saved set-user-id of current process specified value. setuid() function permitted if effective user id of super user, or if specified user id same effective user id. if not, specified user id same real user id, setuid() set effective user id real user id.
and output when run root:
nnlnb-mm-041: root# /tmp/setuidbug 70 uid: 0, euid: 0 setting effective uid 70 uid: 0, euid: 70 setting real uid 70 setuid(70) returned: -1 setuid(70) failed: 1, getuid() returned 0, geteuid returned 70
i managed solve it, apparently in macos have set effective uid root work. code below.
#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 %d\n",m_targetuid); seteuid(m_targetuid); printstat(); printf("setting effective uid 0\n"); seteuid(0); printstat(); if (m_targetuid != realuid) { printf("setting real uid %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(); }
and output is:
uid: 0, euid: 0 setting effective uid 70 uid: 0, euid: 70 setting effective uid 0 uid: 0, euid: 0 setting real uid 70 setuid(70) returned: 0 uid: 70, euid: 70
Comments
Post a Comment