Skip to content

gh-97612: Fix shell injection in get-remote-certificate.py #97613

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

Merged
merged 1 commit into from
Sep 28, 2022
Merged

gh-97612: Fix shell injection in get-remote-certificate.py #97613

merged 1 commit into from
Sep 28, 2022

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Sep 28, 2022

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a shell to run "openssl" commands. Issue reported and initial fix by Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

@vstinner
Copy link
Member Author

@gpshead: Would you mind to review this fix?

The fix should also be backported to the 3.7 branch, but there is a conflict, so I will do the backport manually once this PR is merged.

cc @calebshortt

@zware
Copy link
Member

zware commented Sep 28, 2022

What's the point of even keeping this script? Has anyone used it in the past 12 years?

@vstinner
Copy link
Member Author

What's the point of even keeping this script? Has anyone used it in the past 12 years?

In Python 3.12, IMO it's fine to consider removing the script. But for stable Python versions (3.7-3.11), IMO we should fix the script.

I'm working on proposal to remove most Tools/scripts/ scripts, but first I have to look into these scripts to decide what to do with them, categorize them, etc. There are more than 70 scripts.

'openssl s_client -connect "%s:%s" -showcerts < /dev/null' %
(host, port))
cmd = ['openssl', 's_client', '-connect', '%s:%s' % (host, port), '-showcerts']
status, output = subproc(cmd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on windows stdin was hooked up to a file containing "quit\n". was that necessary? If so, use your added subproc stdin= support from above to do that. otherwise, get rid of the added subprob stdin= support to make this PR smaller.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I removed the now unused stdin parameter.

This script is used to connect to a HTTPS server. Saying "quit\n" to a HTTP server makes no sense to me. Maybe on Windows, previously the command was blocked until the user wrotes something and then press ENTER? But with this change, the stdin is replaced with DEVNULL (NUL device on Windows) and so it should no longer block.

First, I basically rewrote the whole script. I don't get half of the code. For example, it runs the "x509" to get the same output than the input... it doesn't make any sense to me. But I tried to keep this change as small as possible...

Then I think that we should just remove the script in the main branch :-)

My main motivation for this change is to prevent users to report the same issue later, even if the impact of the vulnerability is basically non existent since I don't imagine that anyone runs such script on a production server with inputs taken from the Internet...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I'd take the current report as reason to remove the script from main, and the next report as reason to remove it from other branches. Or just go ahead and remove it everywhere. It's not worth fixing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the fix is straightforward, I prefer first to fix the script in all branches.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created https://discuss.python.org/t/remove-outdated-tools-scripts-scripts/19571 discussion to propose removing outdated example scripts.

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.
@vstinner
Copy link
Member Author

@gpshead: Would you mind to review the updated PR?

@vstinner vstinner merged commit 83a0f44 into python:main Sep 28, 2022
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9, 3.10, 3.11.
🐍🍒⛏🤖

@vstinner vstinner deleted the get_certs branch September 28, 2022 23:17
@bedevere-bot
Copy link

GH-97630 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Sep 28, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 28, 2022
…honGH-97613)

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)

Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-bot
Copy link

GH-97631 is a backport of this pull request to the 3.10 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Sep 28, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 28, 2022
…honGH-97613)

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)

Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Sep 28, 2022
@bedevere-bot
Copy link

GH-97632 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 28, 2022
…honGH-97613)

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)

Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-bot
Copy link

GH-97633 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 28, 2022
…honGH-97613)

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)

Co-authored-by: Victor Stinner <vstinner@python.org>
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington
Copy link
Contributor

Sorry, @vstinner, I could not cleanly backport this to 3.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 83a0f44ffd8b398673ae56c310cf5768d359c341 3.7

@bedevere-bot
Copy link

GH-97634 is a backport of this pull request to the 3.7 branch.

@vstinner
Copy link
Member Author

@zware: I prefer the conservative approach, first fix the vulnerability, then discuss removing outdated scripts.

miss-islington added a commit that referenced this pull request Sep 28, 2022
Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)

Co-authored-by: Victor Stinner <vstinner@python.org>
miss-islington added a commit that referenced this pull request Sep 28, 2022
Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)

Co-authored-by: Victor Stinner <vstinner@python.org>
ambv pushed a commit that referenced this pull request Oct 4, 2022
…97613) (GH-97632)

gh-97612: Fix shell injection in get-remote-certificate.py (GH-97613)

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)

Co-authored-by: Victor Stinner <vstinner@python.org>
ambv pushed a commit that referenced this pull request Oct 4, 2022
…97613) (GH-97633)

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)

Co-authored-by: Victor Stinner <vstinner@python.org>
ambv pushed a commit that referenced this pull request Oct 5, 2022
) (#97634)

Fix a shell code injection vulnerability in the
get-remote-certificate.py example script. The script no longer uses a
shell to run "openssl" commands. Issue reported and initial fix by
Caleb Shortt.

Remove the Windows code path to send "quit" on stdin to the "openssl
s_client" command: use DEVNULL on all platforms instead.

Co-authored-by: Caleb Shortt <caleb@rgauge.com>
(cherry picked from commit 83a0f44)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-security A security issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants