Skip to content

Crash in imputil.imp.find_module when a *directory* exists with the given name due to double-close of FILE* #58274

Closed

Description

@davidmalcolm
BPO 14066
Nosy @davidmalcolm
Superseder
  • bpo-7732: imp.find_module crashes Python if there exists a directory named "init.py"
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2012-02-20.20:36:46.322>
    created_at = <Date 2012-02-20.19:01:40.848>
    labels = ['library', 'type-crash']
    title = 'Crash in imputil.imp.find_module when a *directory* exists with the given name due to double-close of FILE*'
    updated_at = <Date 2012-02-20.20:36:46.321>
    user = 'https://github.com/davidmalcolm'

    bugs.python.org fields:

    activity = <Date 2012-02-20.20:36:46.321>
    actor = 'dmalcolm'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-02-20.20:36:46.322>
    closer = 'dmalcolm'
    components = ['Library (Lib)']
    creation = <Date 2012-02-20.19:01:40.848>
    creator = 'dmalcolm'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 14066
    keywords = []
    message_count = 2.0
    messages = ['153799', '153801']
    nosy_count = 1.0
    nosy_names = ['dmalcolm']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '7732'
    type = 'crash'
    url = 'https://bugs.python.org/issue14066'
    versions = ['Python 2.7']

    Activity

    1. davidmalcolm commented on Feb 20, 2012

      @davidmalcolm
      MemberAuthor
      $ mkdir some_directory_ending_with_a.py
      $ python -c "import imputil; imputil.imp.find_module('some_directory_ending_with_a')"
      *** glibc detected *** python: double free or corruption (!prev): 0x0000000001589bf0 ***
      Aborted

      What's happening is that call_find_module opens fp, and tries to create a PyFileObject from it:

      2844 if (fp != NULL) {

      >2845 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
      2846 if (fob == NULL) {
      2847 fclose(fp);
      2848 return NULL;
      2849 }
      2850 }

      The call fails at the very end of fill_file_fields() inside the call to dircheck() here:
      180 f->f_fp = fp;
      181 f = dircheck(f);
      182 return (PyObject *) f;
      183 }
      184

      but f->fp == fp

      Hence when fill_file_fields returns NULL, and f is cleaned up here:
      484 if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {

      > 485 Py_DECREF(f);
      486 Py_DECREF(o_name);
      487 return NULL;
      488 }

      then f->fp is closed within file_dealloc()

      Then, when PyFile_FromFile returns NULL, call_find_module calls fclose(fp):
      2844 if (fp != NULL) {
      2845 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
      2846 if (fob == NULL) {

      >2847 fclose(fp);
      2848 return NULL;
      2849 }
      2850 }

      and it attempts to close fp for the second time.

      The documentation for PyFile_FromFile:
      http://docs.python.org/c-api/file.html#PyFile_FromFile
      says:
      "Return NULL and close the file using close on failure".

      It therefore looks like call_find_module's fclose(fp) is incorrect here, and is what leads to the double-free.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Feb 20, 2012
    3. davidmalcolm commented on Feb 20, 2012

      @davidmalcolm
      MemberAuthor

      Duplicate of bpo-7732

    4. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-crashA hard crash of the interpreter, possibly with a core dump

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions