summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2009-04-05 23:43:58 (GMT)
committerGregory P. Smith <greg@mad-scientist.com>2009-04-05 23:43:58 (GMT)
commit6d30793cf2dfa23d5144026305631ade3e9f6e47 (patch)
treec44067ba799c3ca055c4057b092a7ca9596edf74
parentfdda68e0308ca35d68f47ffd3380c768ac59ca72 (diff)
downloadcpython-6d30793cf2dfa23d5144026305631ade3e9f6e47.zip
cpython-6d30793cf2dfa23d5144026305631ade3e9f6e47.tar.gz
cpython-6d30793cf2dfa23d5144026305631ade3e9f6e47.tar.bz2
Fixes issue5705: os.setuid() and friends did not accept the same range of
values that pwd.getpwnam() returns.
-rw-r--r--Lib/test/test_os.py45
-rw-r--r--Modules/posixmodule.c72
2 files changed, 99 insertions, 18 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 64b29ea..9b8ba02 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -608,6 +608,48 @@ if sys.platform != 'win32':
class Win32ErrorTests(unittest.TestCase):
pass
+ class PosixUidGidTests(unittest.TestCase):
+ if hasattr(os, 'setuid'):
+ def test_setuid(self):
+ if os.getuid() != 0:
+ self.assertRaises(os.error, os.setuid, 0)
+ self.assertRaises(OverflowError, os.setuid, 1<<32)
+
+ if hasattr(os, 'setgid'):
+ def test_setgid(self):
+ if os.getuid() != 0:
+ self.assertRaises(os.error, os.setgid, 0)
+ self.assertRaises(OverflowError, os.setgid, 1<<32)
+
+ if hasattr(os, 'seteuid'):
+ def test_seteuid(self):
+ if os.getuid() != 0:
+ self.assertRaises(os.error, os.seteuid, 0)
+ self.assertRaises(OverflowError, os.seteuid, 1<<32)
+
+ if hasattr(os, 'setegid'):
+ def test_setegid(self):
+ if os.getuid() != 0:
+ self.assertRaises(os.error, os.setegid, 0)
+ self.assertRaises(OverflowError, os.setegid, 1<<32)
+
+ if hasattr(os, 'setreuid'):
+ def test_setreuid(self):
+ if os.getuid() != 0:
+ self.assertRaises(os.error, os.setreuid, 0, 0)
+ self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
+ self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
+
+ if hasattr(os, 'setregid'):
+ def test_setregid(self):
+ if os.getuid() != 0:
+ self.assertRaises(os.error, os.setregid, 0, 0)
+ self.assertRaises(OverflowError, os.setregid, 1<<32, 0)
+ self.assertRaises(OverflowError, os.setregid, 0, 1<<32)
+else:
+ class PosixUidGidTests(unittest.TestCase):
+ pass
+
def test_main():
test_support.run_unittest(
FileTests,
@@ -619,7 +661,8 @@ def test_main():
DevNullTests,
URandomTests,
Win32ErrorTests,
- TestInvalidFD
+ TestInvalidFD,
+ PosixUidGidTests
)
if __name__ == "__main__":
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 6261d2f..7a96300 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -5597,9 +5597,15 @@ Set the current process's user id.");
static PyObject *
posix_setuid(PyObject *self, PyObject *args)
{
- int uid;
- if (!PyArg_ParseTuple(args, "i:setuid", &uid))
+ long uid_arg;
+ uid_t uid;
+ if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
return NULL;
+ uid = uid_arg;
+ if (uid != uid_arg) {
+ PyErr_SetString(PyExc_OverflowError, "user id too big");
+ return NULL;
+ }
if (setuid(uid) < 0)
return posix_error();
Py_INCREF(Py_None);
@@ -5616,10 +5622,16 @@ Set the current process's effective user id.");
static PyObject *
posix_seteuid (PyObject *self, PyObject *args)
{
- int euid;
- if (!PyArg_ParseTuple(args, "i", &euid)) {
+ long euid_arg;
+ uid_t euid;
+ if (!PyArg_ParseTuple(args, "l", &euid_arg))
+ return NULL;
+ euid = euid_arg;
+ if (euid != euid_arg) {
+ PyErr_SetString(PyExc_OverflowError, "user id too big");
return NULL;
- } else if (seteuid(euid) < 0) {
+ }
+ if (seteuid(euid) < 0) {
return posix_error();
} else {
Py_INCREF(Py_None);
@@ -5636,10 +5648,16 @@ Set the current process's effective group id.");
static PyObject *
posix_setegid (PyObject *self, PyObject *args)
{
- int egid;
- if (!PyArg_ParseTuple(args, "i", &egid)) {
+ long egid_arg;
+ gid_t egid;
+ if (!PyArg_ParseTuple(args, "l", &egid_arg))
+ return NULL;
+ egid = egid_arg;
+ if (egid != egid_arg) {
+ PyErr_SetString(PyExc_OverflowError, "group id too big");
return NULL;
- } else if (setegid(egid) < 0) {
+ }
+ if (setegid(egid) < 0) {
return posix_error();
} else {
Py_INCREF(Py_None);
@@ -5656,10 +5674,17 @@ Set the current process's real and effective user ids.");
static PyObject *
posix_setreuid (PyObject *self, PyObject *args)
{
- int ruid, euid;
- if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
+ long ruid_arg, euid_arg;
+ uid_t ruid, euid;
+ if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
return NULL;
- } else if (setreuid(ruid, euid) < 0) {
+ ruid = ruid_arg;
+ euid = euid_arg;
+ if (euid != euid_arg || ruid != ruid_arg) {
+ PyErr_SetString(PyExc_OverflowError, "user id too big");
+ return NULL;
+ }
+ if (setreuid(ruid, euid) < 0) {
return posix_error();
} else {
Py_INCREF(Py_None);
@@ -5676,10 +5701,17 @@ Set the current process's real and effective group ids.");
static PyObject *
posix_setregid (PyObject *self, PyObject *args)
{
- int rgid, egid;
- if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
+ long rgid_arg, egid_arg;
+ gid_t rgid, egid;
+ if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
+ return NULL;
+ rgid = rgid_arg;
+ egid = egid_arg;
+ if (egid != egid_arg || rgid != rgid_arg) {
+ PyErr_SetString(PyExc_OverflowError, "group id too big");
return NULL;
- } else if (setregid(rgid, egid) < 0) {
+ }
+ if (setregid(rgid, egid) < 0) {
return posix_error();
} else {
Py_INCREF(Py_None);
@@ -5696,9 +5728,15 @@ Set the current process's group id.");
static PyObject *
posix_setgid(PyObject *self, PyObject *args)
{
- int gid;
- if (!PyArg_ParseTuple(args, "i:setgid", &gid))
+ long gid_arg;
+ gid_t gid;
+ if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
+ return NULL;
+ gid = gid_arg;
+ if (gid != gid_arg) {
+ PyErr_SetString(PyExc_OverflowError, "group id too big");
return NULL;
+ }
if (setgid(gid) < 0)
return posix_error();
Py_INCREF(Py_None);
@@ -5746,7 +5784,7 @@ posix_setgroups(PyObject *self, PyObject *groups)
return NULL;
}
grouplist[i] = x;
- /* read back the value to see if it fitted in gid_t */
+ /* read back to see if it fits in gid_t */
if (grouplist[i] != x) {
PyErr_SetString(PyExc_TypeError,
"group id too big");