Skip to content

Crash when throwing an exception with a malicious __hash__ override #73148

Closed

Description

@JelleZijlstra
BPO 28962
Nosy @serhiy-storchaka, @MojoVampire, @JelleZijlstra
Dependencies
  • bpo-25782: CPython hangs on error context set to the error itself
  • Superseder
  • bpo-28603: traceback module can't format/print unhashable exceptions
  • Files
  • baderror.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 2018-06-03.09:59:49.342>
    created_at = <Date 2016-12-13.16:28:07.836>
    labels = ['interpreter-core', '3.7']
    title = 'Crash when throwing an exception with a malicious __hash__ override'
    updated_at = <Date 2018-06-03.09:59:49.341>
    user = 'https://github.com/JelleZijlstra'

    bugs.python.org fields:

    activity = <Date 2018-06-03.09:59:49.341>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-06-03.09:59:49.342>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2016-12-13.16:28:07.836>
    creator = 'JelleZijlstra'
    dependencies = ['25782']
    files = ['45876']
    hgrepos = []
    issue_num = 28962
    keywords = []
    message_count = 5.0
    messages = ['283118', '283139', '283831', '314241', '318531']
    nosy_count = 4.0
    nosy_names = ['serhiy.storchaka', 'xcombelle', 'josh.r', 'JelleZijlstra']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = '28603'
    type = None
    url = 'https://bugs.python.org/issue28962'
    versions = ['Python 3.6', 'Python 3.7']

    Activity

    1. JelleZijlstra commented on Dec 13, 2016

      @JelleZijlstra
      MemberAuthor
      $ cat baderror.py 
      class BadError(Exception):
          def __init__(self):
              self.i = 0
          def __hash__(self):
              self.i += 1
              return self.i
      
      
      e = BadError()
      raise e from e
      $ ./python.exe -V
      Python 3.5.2+
      $ ./python.exe baderror.py 
      Segmentation fault: 11

      I have reproduced this with Python 3.3, 3.4, 3.5, and 3.6; I assume it's been present throughout the 3 series.

      This is because print_exception_recursive in pythonrun.c keeps following the __cause__ chain, and here the exception is its own __cause__. It uses a set to ensure that it breaks cycles, but that doesn't help here because of the exception's incorrect __hash__ method.

    2. added
      3.7 (EOL)
      interpreter-core
      on Dec 13, 2016
    3. MojoVampire commented on Dec 13, 2016

      MojoVampiremannequin
      Mannequin

      Is this something that needs to be defended against? My understanding is that it's pretty easy to segfault CPython in a number of ways if you can execute 100% arbitrary code. The only way to cause this problem is if you can define malicious exceptions and cause one to be raised from itself, neither of which occurs in reasonable code.

    4. xcombelle commented on Dec 22, 2016

      xcombellemannequin
      Mannequin

      To my knowledge it is not the kind of arbitrary code which could segfault python code. It is far different for example that the fact by using bytecode magic you can segfault the interpreter, as this last case is explicitly warned in documentation.

    5. serhiy-storchaka commented on Mar 22, 2018

      @serhiy-storchaka
      Member

      Seems it was fixed somewhere between 3.6.3 and 3.6.5+.

      Traceback (most recent call last):
        File "baderror.py", line 10, in <module>
          raise e from e
      __main__.BadError

      3.5 is now in security-only fixes stage, and this doesn't look like a security issue.

    6. serhiy-storchaka commented on Jun 3, 2018

      @serhiy-storchaka
      Member

      It was fixed in bpo-28603.

    7. 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

      3.7 (EOL)interpreter-core

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions