Skip to content

CVE-2020-10735: Prevent DoS by large int<->str conversions #95778

Description

@gpshead

Problem

A Denial Of Service (DoS) issue was identified in CPython because we use binary bignum’s for our int implementation. A huge integer will always consume a near-quadratic amount of CPU time in conversion to or from a base 10 (decimal) string with a large number of digits. No efficient algorithm exists to do otherwise.

It is quite common for Python code implementing network protocols and data serialization to do int(untrusted_string_or_bytes_value) on input to get a numeric value, without having limited the input length or to do log("processing thing id %s", unknowingly_huge_integer) or any similar concept to convert an int to a string without first checking its magnitude. (http, json, xmlrpc, logging, loading large values into integer via linear-time conversions such as hexadecimal stored in yaml, or anything computing larger values based on user controlled inputs… which then wind up attempting to output as decimal later on). All of these can suffer a CPU consuming DoS in the face of untrusted data.

Everyone auditing all existing code for this, adding length guards, and maintaining that practice everywhere is not feasible nor is it what we deem the vast majority of our users want to do.

This issue has been reported to the Python Security Response Team multiple times by a few different people since early 2020, most recently a few weeks ago while I was in the middle of polishing up the PR so it’d be ready before 3.11.0rc2.

Mitigation

After discussion on the Python Security Response Team mailing list the conclusion was that we needed to limit the size of integer to string conversions for non-linear time conversions (anything not a power-of-2 base) by default. And offer the ability to configure or disable this limit.

The Python Steering Council is aware of this change and accepts it as necessary.

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    3.11only security fixes
    3.12only security fixes
    on Aug 8, 2022
  2. self-assigned this
    on Aug 8, 2022
  3. changed the title [-]Placeholder issue for a specific security fix[/-] [+]CVE-2020-10735: Prevent DoS by large int<->str conversions[/+] on Sep 2, 2022
  4. added 3 commits that reference this issue on Sep 2, 2022
  5. 28 remaining items

  6. gpshead commented on Sep 5, 2022

    @gpshead
    MemberAuthor

    Yes, using a better algorithm is covered over in #90716.

  7. gpshead commented on Sep 5, 2022

    @gpshead
    MemberAuthor

    But if some version of Tim's suggestions in #90716 went in, billion digit conversions might become feasible. :-) But at that point, maybe we'd be able to drop these limits entirely?

    Removing the limit might not be feasible as after this feature some code could come to depend on its existence indirectly to protect other things in whole systems from large values, knowingly or not. That doesn't mean we couldn't, just that it'd need consideration.

    We also need to consider the worst case performance of any fancier algorithm before removing the ability to limit it. Untrusted data seeking a DoS would target that. Ex: If a faster conversion algorithm is typically $O(n * log_2(n))$ but devolves into $O(n^2)$ on some values, we need to handle the worst case unexpected computation.

    A notebook could even be designed to avoid half the problem by having their REPL repr and storage code auto-switch to hex for large values.

  8. added a commit that references this issue on Sep 5, 2022
  9. purplesyringa commented on Sep 5, 2022

    @purplesyringa

    This is appalling.

    Situation: people pass untrusted data to slow built-in functions. Reasonable solution: a) patch vulnerable programs, b) optimize said functions. CPython's solution: just limit stuff.

    Your rationale is founded on the fact that developers constantly add bugs and fixing them is not an option. Fixing bugs is like, more than half of what developers normally do.

    When ReDoS was discovered, did we limit the number of iterations of the matching algorithm? No. Then why should we do the same here? There are dangerous tools, but they are often much more useful due to lack of artificial limits. When did we drop "Special cases aren't special enough to break the rules."

    To add insult to injury, people seem to be talking about limiting repr too. How many bugs that will cause, I can't tell: I saw people use repr(...) to generate non-Python code as a universal solution that works with both integers and strings, and I foresee problems when it starts generating non-digit strings.

    You're limiting integer to string conversions. How soon are you going to limit repr(list) because of the billion laughs attack?

    Why is _PY_LONG_MAX_STR_DIGITS_THRESHOLD even a constant? No one's going to recompile Python, and if people actually start using this security "feature", chances are it'll be enabled everywhere, and then programs that use big arithmetic and parse numbers from the user input will silently stop working. It's not even big arithmetic, really: the discussed limit to repr seems to be only 512 bit, which is much less than thousands-of-bits numbers used in cryptography right now, in Python. In fact, the problem doesn't go away if it's variable: the problem is that it's global, affecting the whole application rather than the user-facing part. When did we drop "Explicit is better than implicit?"

    And then, has anyone actually researched how much trusted code parses integers from untrusted sources directly? To me it seems that even ReDoS should be more popular than this. I think people seldom call int on user input directly: they either use parsing libraries like json and yaml, or (hear me out) use (web) frameworks that parse the data themselves. Either way, there's a finite number of places where checks should be added, and even if it's tens of places, it's so much better than limiting every single program, including those that don't even face the outer world, because it'll be actually configurable.

    I don't think I even understand why a limit is added in the first place. If Python used a quadratic algorithm for multiplication instead of FFT, that would not be considered a vulnerability but an inefficiency. There are much faster algorithms than the one used by CPython, I literally found one by googling "subquadratic base conversion". There: Richard P. Brent and Paul Zimmermann, Modern Computer Arithmetic. #90716, the issue that was tracking this, was dropped because what, developers tried to invent stuff themselves instead of comparing with state of art, and just chose the simplest option after an inevitable failure?

  10. gpshead commented on Sep 5, 2022

    @gpshead
    MemberAuthor

    As a reminder to everybody the Python Community Code Of Conduct applies here.

    Closing. This is fixed. We'll open new issues for any follow up work necessary.

  11. stevendaprano commented on Sep 6, 2022

    @stevendaprano
    Member

    Everyone auditing all existing code for this, adding length guards, and maintaining that practice everywhere is not feasible nor is it what we deem the vast majority of our users want to do.

    If applications don't validate their data before running str <-> int conversions, I'm not entirely sure that having an exploit that allows attackers to crash the application with an unexpected ValueError instead of merely slowing it down for a few seconds is a great improvement.

    I'm only a naive developer but this kinda feels like the fix is worse than the problem its solving. What I am missing?

  12. added a commit that references this issue on Sep 6, 2022
  13. gpshead commented on Sep 6, 2022

    @gpshead
    MemberAuthor

    Please redirect further discussion to discuss.python.org.

  14. locked as resolved and limited conversation to collaborators on Sep 6, 2022
  15. gpshead commented on Sep 13, 2022

    @gpshead
    MemberAuthor

    Further Discussion

    ... is taking place in discuss.python.org threads

  16. vstinner commented on Sep 15, 2022

    @vstinner
    Member

    There is a minor bug when parsing the -X option: #96848

  17. vstinner commented on Sep 16, 2022

    @vstinner
    Member

    I propose PR #96874 to mention sys.set_int_max_str_digits() in the error message.

  18. merwok commented on Dec 30, 2022

    @merwok
    Member

    Follow-up PR for a couple spots missed for documentation: #100627

  19. linked a pull request that will close this issuegh-95778: add doc missing in some places #100627on Dec 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.10 (EOL)end of life3.11only security fixes3.12only security fixes3.7 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifetype-bugAn unexpected behavior, bug, or errortype-featureA feature request or enhancementtype-securityA security issue

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions