Skip to content

Commit f95ad90

Browse files
committed
backport r71299 from trunk:
Fixes issue5705: os.setuid() and friends did not accept the same range of values that pwd.getpwnam() returns.
1 parent fc57717 commit f95ad90

2 files changed

Lines changed: 99 additions & 18 deletions

File tree

Lib/test/test_os.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,48 @@ def test_write(self):
656656
class Win32ErrorTests(unittest.TestCase):
657657
pass
658658

659+
class PosixUidGidTests(unittest.TestCase):
660+
if hasattr(os, 'setuid'):
661+
def test_setuid(self):
662+
if os.getuid() != 0:
663+
self.assertRaises(os.error, os.setuid, 0)
664+
self.assertRaises(OverflowError, os.setuid, 1<<32)
665+
666+
if hasattr(os, 'setgid'):
667+
def test_setgid(self):
668+
if os.getuid() != 0:
669+
self.assertRaises(os.error, os.setgid, 0)
670+
self.assertRaises(OverflowError, os.setgid, 1<<32)
671+
672+
if hasattr(os, 'seteuid'):
673+
def test_seteuid(self):
674+
if os.getuid() != 0:
675+
self.assertRaises(os.error, os.seteuid, 0)
676+
self.assertRaises(OverflowError, os.seteuid, 1<<32)
677+
678+
if hasattr(os, 'setegid'):
679+
def test_setegid(self):
680+
if os.getuid() != 0:
681+
self.assertRaises(os.error, os.setegid, 0)
682+
self.assertRaises(OverflowError, os.setegid, 1<<32)
683+
684+
if hasattr(os, 'setreuid'):
685+
def test_setreuid(self):
686+
if os.getuid() != 0:
687+
self.assertRaises(os.error, os.setreuid, 0, 0)
688+
self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
689+
self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
690+
691+
if hasattr(os, 'setregid'):
692+
def test_setregid(self):
693+
if os.getuid() != 0:
694+
self.assertRaises(os.error, os.setregid, 0, 0)
695+
self.assertRaises(OverflowError, os.setregid, 1<<32, 0)
696+
self.assertRaises(OverflowError, os.setregid, 0, 1<<32)
697+
else:
698+
class PosixUidGidTests(unittest.TestCase):
699+
pass
700+
659701
def test_main():
660702
support.run_unittest(
661703
FileTests,
@@ -666,7 +708,8 @@ def test_main():
666708
DevNullTests,
667709
URandomTests,
668710
ExecTests,
669-
Win32ErrorTests
711+
Win32ErrorTests,
712+
PosixUidGidTests
670713
)
671714

672715
if __name__ == "__main__":

Modules/posixmodule.c

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4057,9 +4057,15 @@ Set the current process's user id.");
40574057
static PyObject *
40584058
posix_setuid(PyObject *self, PyObject *args)
40594059
{
4060-
int uid;
4061-
if (!PyArg_ParseTuple(args, "i:setuid", &uid))
4060+
long uid_arg;
4061+
uid_t uid;
4062+
if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
40624063
return NULL;
4064+
uid = uid_arg;
4065+
if (uid != uid_arg) {
4066+
PyErr_SetString(PyExc_OverflowError, "user id too big");
4067+
return NULL;
4068+
}
40634069
if (setuid(uid) < 0)
40644070
return posix_error();
40654071
Py_INCREF(Py_None);
@@ -4076,10 +4082,16 @@ Set the current process's effective user id.");
40764082
static PyObject *
40774083
posix_seteuid (PyObject *self, PyObject *args)
40784084
{
4079-
int euid;
4080-
if (!PyArg_ParseTuple(args, "i", &euid)) {
4085+
long euid_arg;
4086+
uid_t euid;
4087+
if (!PyArg_ParseTuple(args, "l", &euid_arg))
4088+
return NULL;
4089+
euid = euid_arg;
4090+
if (euid != euid_arg) {
4091+
PyErr_SetString(PyExc_OverflowError, "user id too big");
40814092
return NULL;
4082-
} else if (seteuid(euid) < 0) {
4093+
}
4094+
if (seteuid(euid) < 0) {
40834095
return posix_error();
40844096
} else {
40854097
Py_INCREF(Py_None);
@@ -4096,10 +4108,16 @@ Set the current process's effective group id.");
40964108
static PyObject *
40974109
posix_setegid (PyObject *self, PyObject *args)
40984110
{
4099-
int egid;
4100-
if (!PyArg_ParseTuple(args, "i", &egid)) {
4111+
long egid_arg;
4112+
gid_t egid;
4113+
if (!PyArg_ParseTuple(args, "l", &egid_arg))
4114+
return NULL;
4115+
egid = egid_arg;
4116+
if (egid != egid_arg) {
4117+
PyErr_SetString(PyExc_OverflowError, "group id too big");
41014118
return NULL;
4102-
} else if (setegid(egid) < 0) {
4119+
}
4120+
if (setegid(egid) < 0) {
41034121
return posix_error();
41044122
} else {
41054123
Py_INCREF(Py_None);
@@ -4116,10 +4134,17 @@ Set the current process's real and effective user ids.");
41164134
static PyObject *
41174135
posix_setreuid (PyObject *self, PyObject *args)
41184136
{
4119-
int ruid, euid;
4120-
if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4137+
long ruid_arg, euid_arg;
4138+
uid_t ruid, euid;
4139+
if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
41214140
return NULL;
4122-
} else if (setreuid(ruid, euid) < 0) {
4141+
ruid = ruid_arg;
4142+
euid = euid_arg;
4143+
if (euid != euid_arg || ruid != ruid_arg) {
4144+
PyErr_SetString(PyExc_OverflowError, "user id too big");
4145+
return NULL;
4146+
}
4147+
if (setreuid(ruid, euid) < 0) {
41234148
return posix_error();
41244149
} else {
41254150
Py_INCREF(Py_None);
@@ -4136,10 +4161,17 @@ Set the current process's real and effective group ids.");
41364161
static PyObject *
41374162
posix_setregid (PyObject *self, PyObject *args)
41384163
{
4139-
int rgid, egid;
4140-
if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4164+
long rgid_arg, egid_arg;
4165+
gid_t rgid, egid;
4166+
if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4167+
return NULL;
4168+
rgid = rgid_arg;
4169+
egid = egid_arg;
4170+
if (egid != egid_arg || rgid != rgid_arg) {
4171+
PyErr_SetString(PyExc_OverflowError, "group id too big");
41414172
return NULL;
4142-
} else if (setregid(rgid, egid) < 0) {
4173+
}
4174+
if (setregid(rgid, egid) < 0) {
41434175
return posix_error();
41444176
} else {
41454177
Py_INCREF(Py_None);
@@ -4156,9 +4188,15 @@ Set the current process's group id.");
41564188
static PyObject *
41574189
posix_setgid(PyObject *self, PyObject *args)
41584190
{
4159-
int gid;
4160-
if (!PyArg_ParseTuple(args, "i:setgid", &gid))
4191+
long gid_arg;
4192+
gid_t gid;
4193+
if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4194+
return NULL;
4195+
gid = gid_arg;
4196+
if (gid != gid_arg) {
4197+
PyErr_SetString(PyExc_OverflowError, "group id too big");
41614198
return NULL;
4199+
}
41624200
if (setgid(gid) < 0)
41634201
return posix_error();
41644202
Py_INCREF(Py_None);
@@ -4205,7 +4243,7 @@ posix_setgroups(PyObject *self, PyObject *groups)
42054243
return NULL;
42064244
}
42074245
grouplist[i] = x;
4208-
/* read back the value to see if it fitted in gid_t */
4246+
/* read back to see if it fits in gid_t */
42094247
if (grouplist[i] != x) {
42104248
PyErr_SetString(PyExc_TypeError,
42114249
"group id too big");

0 commit comments

Comments
 (0)