Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-25 23:44:48 +03:00
1 parent 639362fbf5
commit bdd69f0aad
101 files changed
+22691 -531

No files matched your search

+111 -16
View File
@@ -36,6 +36,15 @@ INTERPRETER_PATTERN = re.compile(r"Requesting program interpreter: ([^]]+)]")
GLIBC_NAME_PATTERN = re.compile(r"\bName: (GLIBC_[A-Za-z0-9_.]+)\b")
GLIBC_VERSION_PATTERN = re.compile(r"^GLIBC_(\d+)\.(\d+)(?:\.(\d+))?$")
GLIBC_ABI_MINIMUMS = {"GLIBC_ABI_DT_RELR": (2, 36)}
PLUGIN_IDENTIFIER_PATTERN = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
PLUGIN_VERSION_PATTERN = re.compile(
r"^(?:0|[1-9][0-9]*)\."
r"(?:0|[1-9][0-9]*)\."
r"(?:0|[1-9][0-9]*)"
r"(?:-(?:0|[1-9][0-9]*|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*)"
r"(?:\.(?:0|[1-9][0-9]*|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*))*)?$"
)
PLUGIN_RUNPATH = "$ORIGIN/../../../../../lib"
def parse_version(value: str) -> tuple[int, ...]:
@@ -118,26 +127,91 @@ def validate_symlinks(library_dir: Path) -> None:
ensure_inside(path, library_dir, "library symlink")
def validate(root: Path, maximum_glibc: tuple[int, ...] | None = None) -> list[str]:
def bootstrap_plugin_libraries(root: Path) -> list[Path]:
plugin_root = root / "share/echoion/bootstrap-plugins"
if not plugin_root.exists() and not plugin_root.is_symlink():
return []
if plugin_root.is_symlink() or not plugin_root.is_dir():
raise ValueError(
f"bootstrap plugin root is not a real directory: {plugin_root}"
)
libraries: list[Path] = []
for plugin_directory in sorted(plugin_root.iterdir()):
plugin_id = plugin_directory.name
if (
plugin_directory.is_symlink()
or not plugin_directory.is_dir()
or PLUGIN_IDENTIFIER_PATTERN.fullmatch(plugin_id) is None
):
raise ValueError(f"invalid bootstrap plugin directory: {plugin_directory}")
versions = sorted(plugin_directory.iterdir())
if len(versions) != 1:
raise ValueError(
f"bootstrap plugin {plugin_id!r} must contain exactly one version"
)
version_directory = versions[0]
if (
version_directory.is_symlink()
or not version_directory.is_dir()
or len(version_directory.name) > 64
or PLUGIN_VERSION_PATTERN.fullmatch(version_directory.name) is None
):
raise ValueError(
f"invalid bootstrap plugin version: {version_directory}"
)
expected = version_directory / f"{plugin_id}.so"
entries = sorted(version_directory.iterdir())
if entries != [expected]:
raise ValueError(
f"bootstrap plugin {plugin_id!r} version "
f"{version_directory.name!r} has unexpected files"
)
if expected.is_symlink() or not expected.is_file() or not is_elf(expected):
raise ValueError(f"missing regular bootstrap plugin ELF: {expected}")
libraries.append(expected)
return libraries
def validate(
root: Path,
maximum_glibc: tuple[int, ...] | None = None,
forbidden_content_prefixes: tuple[bytes, ...] = (),
) -> list[str]:
executable = root / "bin/echoiond"
updater = root / "bin/echoion-updater"
core = root / "lib/Echoion.ts"
executable_dir = (root / "bin").resolve(strict=True)
library_dir = (root / "lib").resolve(strict=True)
for required in (executable, core):
for required in (executable, updater, core):
if not required.is_file() or required.is_symlink() or not is_elf(required):
raise ValueError(f"missing regular ELF payload: {required}")
validate_symlinks(library_dir)
elf_files: list[Path] = [executable]
elf_files: list[Path] = []
for path in sorted(executable_dir.iterdir()):
if path.is_symlink() or not path.is_file() or not is_elf(path):
raise ValueError(f"unexpected non-ELF entry in bin/: {path}")
elf_files.append(path)
for path in sorted(library_dir.iterdir()):
if path.is_symlink():
continue
if not path.is_file() or not is_elf(path):
raise ValueError(f"unexpected non-ELF entry in lib/: {path}")
elf_files.append(path)
plugin_files = set(bootstrap_plugin_libraries(root))
elf_files.extend(sorted(plugin_files))
report: list[str] = []
for path in elf_files:
relative = path.relative_to(root).as_posix()
contents = path.read_bytes()
for prefix in forbidden_content_prefixes:
if prefix in contents:
rendered = os.fsdecode(prefix)
raise ValueError(
f"{relative} embeds forbidden build/source path: {rendered}"
)
header = command_output(["readelf", "-hW", os.fspath(path)])
if not re.search(r"^\s*Class:\s+ELF64\s*$", header, re.MULTILINE):
raise ValueError(f"{relative} is not a 64-bit ELF")
@@ -149,7 +223,12 @@ def validate(root: Path, maximum_glibc: tuple[int, ...] | None = None) -> list[s
raise ValueError(f"{relative} is not an x86-64 ELF")
dynamic = command_output(["readelf", "-dW", os.fspath(path)])
runpaths = RUNPATH_PATTERN.findall(dynamic)
expected_runpath = "$ORIGIN/../lib" if path == executable else "$ORIGIN"
if path in plugin_files:
expected_runpath = PLUGIN_RUNPATH
elif path.parent == executable_dir:
expected_runpath = "$ORIGIN/../lib"
else:
expected_runpath = "$ORIGIN"
if runpaths != [expected_runpath]:
raise ValueError(
f"{relative} RUNPATH must be {expected_runpath!r}, got {runpaths!r}"
@@ -189,17 +268,18 @@ def validate(root: Path, maximum_glibc: tuple[int, ...] | None = None) -> list[s
newest = ".".join(map(str, max(glibc_versions)))
report.append(f"ABI\t{relative}\tGLIBC_MAX={newest}")
program_headers = command_output(["readelf", "-lW", os.fspath(executable)])
interpreter_match = INTERPRETER_PATTERN.search(program_headers)
if interpreter_match is None:
raise ValueError("echoiond has no ELF interpreter")
interpreter = interpreter_match.group(1)
if not re.fullmatch(
r"/(?:[^/]+/)*(?:ld-linux[^/]*\.so[^/]*|ld64\.so[^/]*)",
interpreter,
):
raise ValueError(f"unexpected ELF interpreter: {interpreter}")
report.append(f"INTERPRETER\tbin/echoiond\t{interpreter}\tsystem")
if path.parent == executable_dir:
program_headers = command_output(["readelf", "-lW", os.fspath(path)])
interpreter_match = INTERPRETER_PATTERN.search(program_headers)
if interpreter_match is None:
raise ValueError(f"{relative} has no ELF interpreter")
interpreter = interpreter_match.group(1)
if not re.fullmatch(
r"/(?:[^/]+/)*(?:ld-linux[^/]*\.so[^/]*|ld64\.so[^/]*)",
interpreter,
):
raise ValueError(f"unexpected ELF interpreter in {relative}: {interpreter}")
report.append(f"INTERPRETER\t{relative}\t{interpreter}\tsystem")
return sorted(report)
@@ -229,13 +309,28 @@ def main() -> int:
parser.add_argument("--root", required=True, type=Path)
parser.add_argument("--report", type=Path)
parser.add_argument("--max-glibc", type=parse_version)
parser.add_argument(
"--forbid-content-prefix",
action="append",
default=[],
help="reject ELF files containing this build/source path (repeatable)",
)
arguments = parser.parse_args()
if shutil.which("readelf") is None:
parser.error("readelf is required")
try:
root = arguments.root.resolve(strict=True)
report = validate(root, arguments.max_glibc)
forbidden_content_prefixes = tuple(
os.fsencode(prefix)
for prefix in arguments.forbid_content_prefix
if prefix
)
report = validate(
root,
arguments.max_glibc,
forbidden_content_prefixes,
)
except (OSError, ValueError) as error:
parser.error(str(error))
if arguments.report: