Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segfault in mmap object when using __index__ method that closes the mmap #103987

Closed
cfbolz opened this issue Apr 29, 2023 · 9 comments
Closed

segfault in mmap object when using __index__ method that closes the mmap #103987

cfbolz opened this issue Apr 29, 2023 · 9 comments
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@cfbolz
Copy link
Contributor

cfbolz commented Apr 29, 2023

The following (artificial) code segfaults CPython (I tried a bunch of versions, including git main) on my x86 Ubuntu Linux 22.10:

import mmap

with open("abcds", "w+") as f:
    f.write("foobar")
    f.flush()

    class X(object):
        def __index__(self):
            m.close()
            return 1

    m = mmap.mmap(f.fileno(), 6, access=mmap.ACCESS_READ)

    print(m[1])
    print(m[X()])

The problem is this code in mmapmodule.c

static PyObject *
mmap_subscript(mmap_object *self, PyObject *item)
{
    CHECK_VALID(NULL);
    if (PyIndex_Check(item)) {
        Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
        if (i == -1 && PyErr_Occurred())
            return NULL;
        if (i < 0)
            i += self->size;
        if (i < 0 || i >= self->size) {
            PyErr_SetString(PyExc_IndexError,
                "mmap index out of range");
            return NULL;
        }
        return PyLong_FromLong(Py_CHARMASK(self->data[i]));
...

the CHECK_VALID(NULL) call which checks whether the mmap object is closed happens before the PyNumber_AsSsize_t call which closes the object (and similarly for the slice handling which happens further down).

Linked PRs

@cfbolz cfbolz added the type-crash A hard crash of the interpreter, possibly with a core dump label Apr 29, 2023
@arhadthedev arhadthedev added the extension-modules C modules in the Modules dir label Apr 29, 2023
@sobolevn
Copy link
Member

Yes, adding CHECK_VALID(NULL); right after Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); seems like a reasonale check.

@cfbolz would you like to send a PR? :)

@cfbolz
Copy link
Contributor Author

cfbolz commented Apr 29, 2023

heh, in principle yes, but I am currently busy doing an equivalent fix in pypy ;-)

@Agent-Hellboy
Copy link
Contributor

Agent-Hellboy commented Apr 29, 2023

Hi @sobolevn I can create a PR, I guess it's a one-line change , I don't know whether we need to modify some test or not

@sobolevn
Copy link
Member

@Agent-Hellboy yes, adding regression tests for crashes is a must :)

@Agent-Hellboy
Copy link
Contributor

okay, Thanks, let me explore
I guess this is the expected output

Traceback (most recent call last):
  File "/home/proshan/play_python/my_cpython/cpython/b.py", line 15, in <module>
    print(m[X()])
          ~^^^^^
ValueError: mmap closed or invalid

@sobolevn
Copy link
Member

Yes, looks correct to me

@cfbolz
Copy link
Contributor Author

cfbolz commented Apr 29, 2023

that's what I went with too. there's an interesting corner case with this kind of code:

...
m = mmap.mmap(...)
m.close()
m["abc"]

should this give a TypeError because of the invalid index type? or an error that the mmap is closed (as it does now)? if the latter, you would have to check twice whether the mmap is closed.

@sobolevn
Copy link
Member

or an error that the mmap is closed (as it does now)?

I think that's the case, because other exceptions might break users' code (unlikely, but possible).

Agent-Hellboy added a commit to Agent-Hellboy/cpython that referenced this issue Apr 29, 2023
@sunmy2019 sunmy2019 added 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels May 12, 2023
Agent-Hellboy added a commit to Agent-Hellboy/cpython that referenced this issue May 12, 2023
JelleZijlstra added a commit that referenced this issue May 20, 2023
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 20, 2023
(cherry picked from commit ceaa4c3)

Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit that referenced this issue May 20, 2023
…4677)

gh-103987: fix several crashes in mmap module (GH-103990)
(cherry picked from commit ceaa4c3)

Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
carljm added a commit to gsallam/cpython_with_perfmap_apii that referenced this issue May 20, 2023
* main: (30 commits)
  pythongh-103987: fix several crashes in mmap module (python#103990)
  docs: fix wrong indentation causing rendering error in dis page (python#104661)
  pythongh-94906: Support multiple steps in math.nextafter (python#103881)
  pythongh-104472: Skip `test_subprocess.ProcessTestCase.test_empty_env` if ASAN is enabled (python#104667)
  pythongh-103839: Allow building Tkinter against Tcl 8.7 without external libtommath (pythonGH-103842)
  pythongh-85984: New additions and improvements to the tty library. (python#101832)
  pythongh-104659: Consolidate python examples in enum documentation (python#104665)
  pythongh-92248: Deprecate `type`, `choices`, `metavar` parameters of `argparse.BooleanOptionalAction` (python#103678)
  pythongh-104645: fix error handling in marshal tests (python#104646)
  pythongh-104600: Make type.__type_params__ writable (python#104634)
  pythongh-104602: Add additional test for listcomp with lambda (python#104639)
  pythongh-104640: Disallow walrus in comprehension within type scopes (python#104641)
  pythongh-103921: Rename "type" header in argparse docs (python#104654)
  Improve readability of `typing._ProtocolMeta.__instancecheck__` (python#104649)
  pythongh-96522: Fix deadlock in pty.spawn (python#96639)
  pythonGH-102818: Do not call `PyTraceBack_Here` in sys.settrace trampoline.  (pythonGH-104579)
  pythonGH-103545: Add macOS specific constants for ``os.setpriority`` to ``os`` (python#104606)
  pythongh-104623: Update macOS installer to SQLite 3.42.0 (pythonGH-104624)
  pythongh-104619: never leak comprehension locals to outer locals() (python#104637)
  pythongh-104602: ensure all cellvars are known up front (python#104603)
  ...
@sobolevn
Copy link
Member

sobolevn commented Jun 8, 2023

Thanks everyone!

@sobolevn sobolevn closed this as completed Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

5 participants