Skip to content
RCE Disclosed report Intermediate

pash CR injection leading to potential authenticated RCE on hg.mozilla.org

The pash SSH wrapper for hg.mozilla.org allowed users to edit repository descriptions, but the input was read with Python's input() which stops at newline, while hg's config parser splits on additional line separators like carriage return. An attacker could inject arbitrary configuration items, including hooks, into the repository's hgrc file, potentially leading to remote code execution.

Mozilla

Originally disclosed on Mozilla

Critical View original
  • Fixed in 16 days
  • Paid in 16 days
  • Disclosed after 16 days
Text
Happy Holidays!


`hg.mozilla.org` seems to use [`pash`](https://hg.mozilla.org/hgcustom/version-control-tools/file/tip/hgserver/pash) to handle incoming SSH requests for `hg` operations.

Among the commands supported there exists the possibility to change some repository configuration
via the `edit` command. Among other operations the `edit` command allows users to edit repository
descriptions this is implemented in the `edit_repo_description` function. It is defined [as follows](https://hg.mozilla.org/hgcustom/version-control-tools/file/tip/hgserver/pash/hg_helper.py#l430):

'''python
def edit_repo_description(repo_name):
    user = os.getenv('USER')
    user_repo_dir = user.replace('@', '_')
    print(EDIT_DESCRIPTION.format(user_dir=user_repo_dir, repo=repo_name))
    selection = prompt_user('Proceed?', ['yes', 'no'])
    if selection != 'yes':
        return

    repo_path = get_and_validate_user_repo(repo_name)
    repo_description = input('Enter a one line descripton for the '
                                 'repository: ')
    if repo_description == '':
        return

    repo_description = escape(repo_description)

    config_path, config = get_user_repo_config(repo_path)

    if not config.has_section('web'):
        config.add_section('web')

    config.set('web', 'description', repo_description)

    with open(config_path, 'w+') as fh:
        config.write(fh)

    run_command('%s -R %s replicatehgrc' % (HG, repo_path))

'''

Note the use of the Python built-in [`input`](https://docs.python.org/3/library/functions.html#input) function to read the new description of the repository.
The `input` function read up until the newline character `\n` to set the `repo_description` in the repository configuration `.hg/hgrc`.

Unfortunately [`hg` splits](https://www.mercurial-scm.org/repo/hg/file/tip/mercurial/config.py#l171) the configuration file with the Python function [`splitlines`](https://docs.python.org/3/library/stdtypes.html#str.splitlinesdata.splitlines) which makes use of additional line separators like `\d` among others.

This allows us to use the `edit` operation to inject arbitrary configuration items into the target repo's `.hg/hgrc` file.

I've not tried this against `hg.mozilla.org` but I've verified it locally using the following command line mimicking a SSH connection:

'''
echo -en '2\n3\n1\nlol\x0d[hooks]\x0dpre-log=touch /tmp/joernchen_was_here\n'|SSH_ORIGINAL_COMMAND='edit a' python pash.py
'''

Afterwards we can observe the injected hook in within the repository:

'''
hg config |grep hooks
hooks.pre-log=touch /tmp/joernchen_was_here
'''
Via injected hooks in the `hg` configuration it should be easy to achieve RCE.

The original report as published on Mozilla's Bugzilla. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Impact

An authenticated user could inject arbitrary configuration into a repository, potentially leading to remote code execution on the server, though the author has not tested this against the live site.

What the attacker needed

SSH access to hg.mozilla.org; ability to use the edit command

How it was found

  1. Connect via SSH with the command 'edit a' to edit a repository description.
  2. When prompted, enter a description containing a carriage return followed by malicious configuration lines.
  3. The injected configuration is written to the repository's hgrc file.
  4. The injected hook can then be executed when the repository is accessed.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A tool that lets users edit repository descriptions didn't properly handle special line-break characters. By sneaking in a carriage return, an attacker could add extra settings to the repository's configuration file. Those settings could include commands that run on the server, which is dangerous.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.