extract: post-extract realpath check of every symlink; classifier refuses link chains (N2-1 review)
The lexical relative-target check missed chains: `a -> b/../..` with
`b -> ../..`, or `l1 -> l2/../../..`, each resolve inside the archive
lexically but above the extract through the other link.
- scripts/lib/strip-escaping-links.py (new, the authority): after tar,
lstat-walk the WHOLE extract (never following links), realpath(strict)
every symlink, and remove + record (stripped_escaping_symlink) any that
resolves outside the extract or to it, dangles or loops. Repeats to a
fixed point; a walk error fails the extract. Own-account absolute
/home[N]/<user>/ targets are kept (they cannot resolve in the sandbox and
resolve only inside the customer's site container on WHP).
- scan-symlinks.php: two passes; a relative link whose resolution passes
through another symlink entry of the archive is DANGEROUS.
- fixtures/tests: cpmove-chain (both shapes) and a hand-built tree for the
post-extract pass (chains, /etc, dangling .bak sidecar, escaping .bak,
loop, link to the top). 3 chain checks fail on a226b49.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a226b496a2
commit
72b8fa9d08
5 files changed
+196
-7
No files matched your search
@@ -92,10 +92,13 @@ $dangerousPrefixes = [
|
||||
|
||||
/**
|
||||
* Lexical resolution of a relative link from its own directory in the
|
||||
* archive: a reason when it reaches/climbs above the archive root, or when a
|
||||
* link under <top>/homedir/ resolves outside that homedir; NULL otherwise.
|
||||
* archive: a reason when it reaches/climbs above the archive root, when a
|
||||
* link under <top>/homedir/ resolves outside that homedir, or when any
|
||||
* component on the way is itself a symlink entry ($linkSet: normalized
|
||||
* archive path => true); NULL otherwise. extract.sh's post-extract
|
||||
* strip-escaping-links.py (realpath of every link) is the authority.
|
||||
*/
|
||||
function relative_symlink_escape(string $archivePath, string $target): ?string {
|
||||
function relative_symlink_escape(string $archivePath, string $target, array $linkSet = []): ?string {
|
||||
$ap = preg_replace('#^(\./)+#', '', $archivePath);
|
||||
$dir = array_values(array_filter(explode('/', (string) $ap), fn($x) => $x !== '' && $x !== '.'));
|
||||
array_pop($dir);
|
||||
@@ -109,6 +112,10 @@ function relative_symlink_escape(string $archivePath, string $target): ?string {
|
||||
continue;
|
||||
}
|
||||
$stack[] = $seg;
|
||||
// No chaining: a component that is itself a symlink entry in this
|
||||
// archive would be expanded by the kernel, which this lexical walk
|
||||
// cannot model (`a -> b/..` with `b -> x/y/z`).
|
||||
if (isset($linkSet[implode('/', $stack)])) return 'passes through another symlink in the archive';
|
||||
}
|
||||
if ($home !== null && (count($stack) < 2 || $stack[0] !== $home[0] || $stack[1] !== 'homedir')) {
|
||||
return 'escapes the account homedir';
|
||||
@@ -126,6 +133,9 @@ if (!$fh) {
|
||||
exit(2);
|
||||
}
|
||||
|
||||
// Pass 1: every symlink entry (the chain rule needs the whole set).
|
||||
$links = [];
|
||||
$linkSet = [];
|
||||
while (($line = fgets($fh)) !== false) {
|
||||
if ($line === '' || $line[0] !== 'l') continue;
|
||||
$arrow = strpos($line, ' -> ');
|
||||
@@ -134,15 +144,20 @@ while (($line = fgets($fh)) !== false) {
|
||||
$right = rtrim(substr($line, $arrow + 4), "\r\n");
|
||||
$parts = preg_split('/\s+/', $left, 6);
|
||||
if (count($parts) < 6) continue;
|
||||
$archivePath = $parts[5];
|
||||
$target = $right;
|
||||
$links[] = [$parts[5], $right];
|
||||
$linkSet[trim((string) preg_replace('#^(\./)+#', '', $parts[5]), '/')] = true;
|
||||
}
|
||||
pclose($fh);
|
||||
|
||||
// Pass 2: classify.
|
||||
foreach ($links as [$archivePath, $target]) {
|
||||
|
||||
// N2-1: a RELATIVE target that climbs out of the archive (or out of
|
||||
// the account's homedir/) is DANGEROUS (stripped), not safe: GNU tar
|
||||
// extracts `cpmove-u/sds -> ../../../../root/.my.cnf` as-is. Same rule
|
||||
// as WHP's CpanelBackupImporter::relativeSymlinkEscape().
|
||||
if ($target !== '' && $target[0] !== '/') {
|
||||
$escape = relative_symlink_escape($archivePath, $target);
|
||||
$escape = relative_symlink_escape($archivePath, $target, $linkSet);
|
||||
if ($escape !== null) {
|
||||
$findings[] = [
|
||||
'type' => 'DANGEROUS',
|
||||
@@ -212,7 +227,6 @@ while (($line = fgets($fh)) !== false) {
|
||||
'reason' => $reason,
|
||||
];
|
||||
}
|
||||
pclose($fh);
|
||||
|
||||
$dangerousCount = count(array_filter($findings, fn($f) => $f['type'] === 'DANGEROUS'));
|
||||
$uncertainCount = count(array_filter($findings, fn($f) => $f['type'] === 'UNCERTAIN'));
|
||||
|
||||
Reference in new issue
Block a user