Fix command/config injection in logrotate -u and startup --service-name (CWE-78/CWE-22, follow-up to #6089)#6127
Open
alanturing881 wants to merge 1 commit into
Conversation
…-name (CWE-78/CWE-22) lib/API/LogManagement.js: `pm2 logrotate -u <user>` concatenated the unsanitized user value both into the logrotate template's %USER% substitution (newline injection -> arbitrary postrotate script executed as root on the next log rotation) and into the destination file path `/etc/logrotate.d/pm2-<user>` (path traversal once the value contains its own `/`, escaping the intended directory). lib/API/Startup.js: `pm2 startup`/`unstartup --service-name <name>` concatenated the unsanitized service name into shell command strings joined with `&& ` and passed to sexec() (child_process.exec, real shell), letting shell metacharacters (`;`, `#`, backticks, `$()`) execute arbitrary commands as root instead of being treated as a literal service name. Both are fixed with the same minimal allowlist validation (`/^[a-zA-Z0-9._-]+$/`) already used elsewhere in this codebase (e.g. lib/tools/open.js's SUDO_USER check), rejecting path separators, newlines and shell metacharacters before the value reaches the filesystem/shell sink. These were originally reported in Unitech#6089 (items Unitech#5, Unitech#6, Unitech#7) but were not addressed by the fixes shipped in v7.0.0 for the other items in that report. Co-Authored-By: iaohkut <thb2601@gmail.com>
|
iaohkut seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two of the vulnerabilities reported in #6089 (items #5, #6, #7) were not addressed by the security
fixes shipped in v7.0.0 for the other items in that report. Since #6089 is already public (closed
issue, full technical detail visible), this PR fixes them directly rather than filing a new private
advisory.
lib/API/LogManagement.js—pm2 logrotate -u <user>(root only): theuservalue wasconcatenated unsanitized into both the logrotate template's
%USER%substitution (embeddednewlines let an attacker inject an arbitrary
postrotate/endscriptblock, executed as root thenext time logs rotate) and the destination path
/etc/logrotate.d/pm2-<user>(path traversal oncethe value contains its own
/, escaping the intended directory).lib/API/Startup.js—pm2 startup/unstartup --service-name <name>(root only): the servicename was concatenated unsanitized into shell command strings joined with
&&and passed tosexec()(child_process.exec, real shell), letting shell metacharacters (;,#, backticks,$()) execute arbitrary commands as root instead of being treated as a literal service/unit name.Both are common patterns where the value in question is templated by provisioning/CI tooling from a
less-trusted source (hostname, tenant name, etc.) before being passed to a command that must run as
root.
Fix
Both are fixed with the same minimal allowlist validation (
/^[a-zA-Z0-9._-]+$/) already usedelsewhere in this codebase (e.g.
lib/tools/open.js'sSUDO_USERcheck), rejecting path separators,newlines and shell metacharacters before the value reaches the filesystem/shell sink. Legitimate
values (alphanumeric service/user names) are unaffected.
Test plan
node -csyntax check on both modified fileswhile legitimate values (
www-data,my-app-1) still pass validation and reach the originalcode path — see harness output below (ran against the patched files, no root/real systemd
touched):
test/e2e/misc/startup.sh(requiresroot + real init system, couldn't safely run destructively in my sandbox) to confirm no
regression for the normal
pm2 startup/unstartupflows.Related: #6089