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

Slow IDNA decoding with large strings [CVE-2022-45061] #98433

Closed
guidovranken opened this issue Oct 19, 2022 · 4 comments
Closed

Slow IDNA decoding with large strings [CVE-2022-45061] #98433

guidovranken opened this issue Oct 19, 2022 · 4 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error type-security A security issue

Comments

@guidovranken
Copy link

guidovranken commented Oct 19, 2022

Bug report

Originally reported to the security address on September 9.

('xn--016c'+'a'*5000).encode('utf-8').decode('idna')

The execution time is not linear in relation to the input string size, which can cause slowness with large inputs:

10 chars = 0.016 seconds
100 chars = 0.047 seconds
1000 chars = 2.883 seconds
2500 chars = 17.724 seconds
5000 chars = 1 min 10 seconds

Comment by @tiran:

According to spec https://unicode.org/reports/tr46/ an IDNA label must not be longer than 63 characters. Python's idna module enforces the restriction, but too late.

This may be abused in some cases, for example by passing a crafted host name to asyncio create_connection:

import asyncio

async def main():
    loop = asyncio.get_running_loop()

    await loop.create_connection(
        lambda: [], ('xn--016c'+'a'*5000).encode('utf-8'), 443
    )

asyncio.run(main())

Your environment

  • CPython versions tested on: CPython repository 'main' branch checkout, version 3.8.12, version 2.7.18
  • Operating system and architecture: Ubuntu Linux x64
@guidovranken guidovranken added the type-bug An unexpected behavior, bug, or error label Oct 19, 2022
@gpshead gpshead added type-security A security issue 3.11 only security fixes 3.10 only security fixes 3.9 only security fixes 3.8 only security fixes 3.12 bugs and security fixes labels Nov 4, 2022
@gpshead
Copy link
Member

gpshead commented Nov 4, 2022

This is probably in ToUnicode and ToASCII of https://github.com/python/cpython/blob/main/Lib/encodings/idna.py and/or in https://github.com/python/cpython/blob/main/Lib/encodings/punycode.py itself, where we could presumably just do an up front length check and reject inputs that are obviously too long to possibly decode into a label length that DNS standards will accept.

If there are libraries that allow an attacker controlled hostname without a reasonable length check on it to get into a connect or similar call that tries idna decoding, that'd make this remotely exploitable. Based solely on code inspection, the urllib.request.HTTPRedirectHandler class is probably vulnerable to this - https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L652 - the location or uri headers it consumes on a HTTP 302 redirect reponse to construct the new URL are not obviously limited, nor is the host that ultimately winds it way down into the socket module. (I didn't test this, I was just reading code) A test case would be to point urllib at a malicious server that sends a 2000 byte idna hostname in a 302 redirect header...

@vstinner
Copy link
Member

vstinner commented Nov 4, 2022

The issue #99083 was marked as a duplicate of this issue.

gpshead added a commit to gpshead/cpython that referenced this issue Nov 4, 2022
There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

An early length check would still be a good idea given that DNS IDNA
label names cannot be more than 63 ASCII characters.
gpshead added a commit that referenced this issue Nov 8, 2022
There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

This also adds an early length check in IDNA decoding to outright reject
huge inputs early on given the ultimate result is defined to be 63 or fewer
characters.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 8, 2022
There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

This also adds an early length check in IDNA decoding to outright reject
huge inputs early on given the ultimate result is defined to be 63 or fewer
characters.
(cherry picked from commit d315722)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
gpshead added a commit that referenced this issue Nov 8, 2022
There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit d315722)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
miss-islington added a commit to miss-islington/cpython that referenced this issue Nov 8, 2022
) (pythonGH-99222)

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit d315722)

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
miss-islington added a commit to miss-islington/cpython that referenced this issue Nov 8, 2022
) (pythonGH-99222)

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit d315722)

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
miss-islington added a commit to miss-islington/cpython that referenced this issue Nov 8, 2022
) (pythonGH-99222)

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit d315722)

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
@gpshead gpshead added the 3.7 (EOL) end of life label Nov 8, 2022
ned-deily pushed a commit that referenced this issue Nov 8, 2022
There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
miss-islington added a commit that referenced this issue Nov 8, 2022
There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit d315722)

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
@gpshead
Copy link
Member

gpshead commented Nov 9, 2022

PRs are either merged or will be merged before the next release (marked as release-blockers) so I'm closing this.

A CVE id has been assigned CVE-2022-45061 for tracking purposes.

@gpshead gpshead closed this as completed Nov 9, 2022
@gpshead gpshead changed the title Slow IDNA decoding with large strings Slow IDNA decoding with large strings [CVE-2022-45061] Nov 9, 2022
@vstinner
Copy link
Member

vstinner commented Nov 9, 2022

I created https://python-security.readthedocs.io/vuln/slow-idna-large-strings.html to track this vulnerability. The fix is not merged into 3.8 and 3.9 branches yet.

ambv pushed a commit that referenced this issue Nov 10, 2022
… (GH-99231)

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit d315722)
(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
ambv pushed a commit that referenced this issue Nov 10, 2022
… (#99230)

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit d315722)
(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
halstead pushed a commit to openembedded/openembedded-core that referenced this issue Nov 23, 2022
Fix CVE-2022-45061, referenced as
python/cpython#98433
patch taken from
python/cpython@064ec20

Signed-off-by: Omkar <omkarpatil10.93@gmail.com>
Signed-off-by: Steve Sakoman <steve@sakoman.com>
rpurdie pushed a commit to yoctoproject/poky that referenced this issue Dec 7, 2022
Fix CVE-2022-45061, referenced as
python/cpython#98433
patch taken from
python/cpython@064ec20

(From OE-Core rev: 4498ca9a299bd5d9a7173ec67daf17cb66b6d286)

Signed-off-by: Omkar <omkarpatil10.93@gmail.com>
Signed-off-by: Steve Sakoman <steve@sakoman.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
jpuhlman pushed a commit to MontaVista-OpenSourceTechnology/poky that referenced this issue Dec 8, 2022
Source: poky
MR: 123948, 123690
Type: Security Fix
Disposition: Merged from poky
ChangeID: 124e5c83914ea141f93c28f054d9b53babf1c6ea
Description:

Fix CVE-2022-45061, referenced as
python/cpython#98433
patch taken from
python/cpython@064ec20

(From OE-Core rev: 4498ca9a299bd5d9a7173ec67daf17cb66b6d286)

Signed-off-by: Omkar <omkarpatil10.93@gmail.com>
Signed-off-by: Steve Sakoman <steve@sakoman.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Signed-off-by: Jeremy A. Puhlman <jpuhlman@mvista.com>
stratakis pushed a commit to stratakis/cpython that referenced this issue Dec 19, 2022
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Dec 19, 2022
…er.patch

00394 #
pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

Backported from python3.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Dec 21, 2022
…er.patch

00394 #
pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

Backported from python3.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
hroncok pushed a commit to fedora-python/cpython that referenced this issue Oct 6, 2023
…er.patch

00394 #
pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

Backported from python3.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Mar 11, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Mar 11, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Mar 20, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Mar 20, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Mar 20, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Mar 20, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
stratakis pushed a commit to stratakis/cpython that referenced this issue Mar 25, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
hroncok pushed a commit to fedora-python/cpython that referenced this issue Mar 26, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
mcepl pushed a commit to openSUSE-Python/cpython that referenced this issue Apr 2, 2024
…oder

pythongh-98433: Fix quadratic time idna decoding.

There was an unnecessary quadratic loop in idna decoding. This restores
the behavior to linear.

(cherry picked from commit a6f6c3a)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error type-security A security issue
Projects
None yet
Development

No branches or pull requests

3 participants