Skip to content

{az} use runpy instead of os.execl#21261

Open
bluca wants to merge 1 commit into
Azure:devfrom
bluca:runpy
Open

{az} use runpy instead of os.execl#21261
bluca wants to merge 1 commit into
Azure:devfrom
bluca:runpy

Conversation

@bluca

@bluca bluca commented Feb 11, 2022

Copy link
Copy Markdown
Member

Description

python -m adds the current working directory to the start of sys.path,
which means running from say /tmp with a fake azure.py causes az to load
it and run it:

$ cd /tmp
$ echo 'raise RuntimeError(pwned)' > azure.py
$ az --help
Traceback (most recent call last):
File "/usr/lib/python3.9/runpy.py", line 188, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/lib/python3.9/runpy.py", line 111, in _get_module_details
import(pkg_name)
File "/tmp/azure.py", line 1, in
raise RuntimeError("pwned")
RuntimeError: pwned

Use runpy instead.

Original report: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1005251

Reported-by: Jakub Wilk jwilk@jwilk.net

🤖 PR Validation — ️✔️ All clear

Breaking Changes Tests
️✔️ None ️✔️ 130/130

python -m adds the current working directory to the start of sys.path,
which means running from say /tmp with a fake azure.py causes az to load
it and run it:

  $ cd /tmp
  $ echo 'raise RuntimeError(pwned)' > azure.py
  $ az --help
    Traceback (most recent call last):
      File "/usr/lib/python3.9/runpy.py", line 188, in _run_module_as_main
        mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
      File "/usr/lib/python3.9/runpy.py", line 111, in _get_module_details
        __import__(pkg_name)
      File "/tmp/azure.py", line 1, in <module>
        raise RuntimeError("pwned")
    RuntimeError: pwned

Use runpy instead.

Original report: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1005251

Reported-by: Jakub Wilk <jwilk@jwilk.net>
@yonzhan
yonzhan requested a review from jiasli February 12, 2022 01:07
@yonzhan yonzhan added this to the Feb 2022 (2022-03-01) milestone Feb 12, 2022
@yonzhan

yonzhan commented Feb 12, 2022

Copy link
Copy Markdown
Collaborator

Core may cause breaking changes

@jeremycline

Copy link
Copy Markdown
Member

For what it's worth, we carry this patch in the Fedora project (reported in Fedora 35 at https://bugzilla.redhat.com/show_bug.cgi?id=2053193).

It would be great to see this merged since it's a security issue and multiple Linux distributions have had to pick up a patch and carry it for years.

@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

🔔 Routing this PR to @Azure/act-platform-engineering-squad.

@naga-nandyala

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the az launcher script to avoid invoking python -m azure.cli (which prepends the current working directory to sys.path) by switching to runpy, preventing accidental/hostile shadowing such as /tmp/azure.py being imported.

Changes:

  • Add runpy import to the az shim script.
  • Replace os.execl(..., '-m', 'azure.cli', ...) with runpy.run_module('azure.cli').

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/azure-cli/az
os.environ['AZ_INSTALLER'] = 'PIP'

os.execl(sys.executable, sys.executable, '-m', 'azure.cli', *sys.argv[1:])
runpy.run_module('azure.cli')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants