Commit 21c134d0 authored by Gregory P. Smith's avatar Gregory P. Smith

Merged revisions 77007 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r77007 | gregory.p.smith | 2009-12-23 01:31:11 -0800 (Wed, 23 Dec 2009) | 3 lines

  Fix possible integer overflow in lchown and fchown functions.  For issue1747858.
........
parent 25339af9
...@@ -143,32 +143,61 @@ class PosixTester(unittest.TestCase): ...@@ -143,32 +143,61 @@ class PosixTester(unittest.TestCase):
if hasattr(posix, 'stat'): if hasattr(posix, 'stat'):
self.assert_(posix.stat(test_support.TESTFN)) self.assert_(posix.stat(test_support.TESTFN))
def _test_all_chown_common(self, chown_func, first_param):
"""Common code for chown, fchown and lchown tests."""
if os.getuid() == 0:
try:
# Many linux distros have a nfsnobody user as MAX_UID-2
# that makes a good test case for signedness issues.
# http://bugs.python.org/issue1747858
# This part of the test only runs when run as root.
# Only scary people run their tests as root.
ent = pwd.getpwnam('nfsnobody')
chown_func(first_param, ent.pw_uid, ent.pw_gid)
except KeyError:
pass
else:
# non-root cannot chown to root, raises OSError
self.assertRaises(OSError, chown_func,
first_param, 0, 0)
# test a successful chown call
chown_func(first_param, os.getuid(), os.getgid())
def _test_chown(self):
# raise an OSError if the file does not exist
os.unlink(test_support.TESTFN)
self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
# re-create the file
open(test_support.TESTFN, 'w').close()
self._test_all_chown_common(posix.chown, test_support.TESTFN)
if hasattr(posix, 'chown'): if hasattr(posix, 'chown'):
def test_chown(self): test_chown = _test_chown
# raise an OSError if the file does not exist
os.unlink(test_support.TESTFN) def _test_fchown(self):
self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1) os.unlink(test_support.TESTFN)
# re-create the file # re-create the file
open(test_support.TESTFN, 'w').close() test_file = open(test_support.TESTFN, 'w')
if os.getuid() == 0: try:
try: fd = test_file.fileno()
# Many linux distros have a nfsnobody user as MAX_UID-2 self._test_all_chown_common(posix.fchown, fd)
# that makes a good test case for signedness issues. finally:
# http://bugs.python.org/issue1747858 test_file.close()
# This part of the test only runs when run as root.
# Only scary people run their tests as root. if hasattr(posix, 'fchown'):
ent = pwd.getpwnam('nfsnobody') test_fchown = _test_fchown
posix.chown(test_support.TESTFN, ent.pw_uid, ent.pw_gid)
except KeyError: def _test_lchown(self):
pass os.unlink(test_support.TESTFN)
else: # create a symlink
# non-root cannot chown to root, raises OSError os.symlink('/tmp/dummy-symlink-target', test_support.TESTFN)
self.assertRaises(OSError, posix.chown, self._test_all_chown_common(posix.lchown, test_support.TESTFN)
test_support.TESTFN, 0, 0)
if hasattr(posix, 'lchown'):
# test a successful chown call test_lchown = _test_lchown
posix.chown(test_support.TESTFN, os.getuid(), os.getgid())
def test_chdir(self): def test_chdir(self):
if hasattr(posix, 'chdir'): if hasattr(posix, 'chdir'):
......
...@@ -32,6 +32,10 @@ Core and Builtins ...@@ -32,6 +32,10 @@ Core and Builtins
- Issue #7084: Fix a (very unlikely) crash when printing a list from one - Issue #7084: Fix a (very unlikely) crash when printing a list from one
thread, and mutating it from another one. Patch by Scott Dial. thread, and mutating it from another one. Patch by Scott Dial.
- Issue #1747858: Fix lchown & fchown to work with large uid's and gid's on
64-bit platforms.
Library Library
------- -------
......
...@@ -1923,9 +1923,10 @@ fd to the numeric uid and gid."); ...@@ -1923,9 +1923,10 @@ fd to the numeric uid and gid.");
static PyObject * static PyObject *
posix_fchown(PyObject *self, PyObject *args) posix_fchown(PyObject *self, PyObject *args)
{ {
int fd, uid, gid; int fd;
long uid, gid;
int res; int res;
if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid)) if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
return NULL; return NULL;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
res = fchown(fd, (uid_t) uid, (gid_t) gid); res = fchown(fd, (uid_t) uid, (gid_t) gid);
...@@ -1946,9 +1947,9 @@ static PyObject * ...@@ -1946,9 +1947,9 @@ static PyObject *
posix_lchown(PyObject *self, PyObject *args) posix_lchown(PyObject *self, PyObject *args)
{ {
char *path = NULL; char *path = NULL;
int uid, gid; long uid, gid;
int res; int res;
if (!PyArg_ParseTuple(args, "etii:lchown", if (!PyArg_ParseTuple(args, "etll:lchown",
Py_FileSystemDefaultEncoding, &path, Py_FileSystemDefaultEncoding, &path,
&uid, &gid)) &uid, &gid))
return NULL; return NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment