|
| 1 | +# Copyright 2025 The Sigstore Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import importlib |
| 16 | +import os |
| 17 | +from pathlib import Path |
| 18 | +import sys |
| 19 | +import tempfile |
| 20 | + |
| 21 | +# type: ignore |
| 22 | +import atheris |
| 23 | +from sigstore.models import TrustedRoot |
| 24 | +from utils import any_files |
| 25 | +from utils import create_fuzz_files |
| 26 | + |
| 27 | +from model_signing import signing |
| 28 | +from model_signing import verifying |
| 29 | + |
| 30 | + |
| 31 | +def _patch_sigstore_get_dirs(metadata_dir: Path, artifacts_dir: Path) -> None: |
| 32 | + """Overwrite sigstore._internal.tuf._get_dirs(url: str). |
| 33 | +
|
| 34 | + This allows us to return directories that the fuzzer controls. |
| 35 | + """ |
| 36 | + tuf_mod = importlib.import_module("sigstore._internal.tuf") |
| 37 | + |
| 38 | + def _stub_get_dirs(url: str): |
| 39 | + return metadata_dir, artifacts_dir |
| 40 | + |
| 41 | + tuf_mod._get_dirs = _stub_get_dirs |
| 42 | + |
| 43 | + |
| 44 | +def _patch_trust_updater_offline_default_true() -> None: |
| 45 | + """Make TrustUpdater.__init__ offline by default. |
| 46 | +
|
| 47 | + This avoids network calls at runtime which is important |
| 48 | + for when the fuzzer runs on OSS-Fuzz. |
| 49 | + """ |
| 50 | + tuf_mod = importlib.import_module("sigstore._internal.tuf") |
| 51 | + trust_updater = tuf_mod.TrustUpdater |
| 52 | + _orig_init = trust_updater.__init__ |
| 53 | + |
| 54 | + def _patched_init(self, url: str, offline: bool = True) -> None: |
| 55 | + _orig_init(self, url, offline=True) |
| 56 | + |
| 57 | + trust_updater.__init__ = _patched_init |
| 58 | + |
| 59 | + |
| 60 | +def TestOneInput(data: bytes) -> None: |
| 61 | + fdp = atheris.FuzzedDataProvider(data) |
| 62 | + |
| 63 | + # When the fuzzer creates a signer further down, |
| 64 | + # Sigstore will use a trusted root that the fuzzer |
| 65 | + # has created. It is possible for the fuzzer to create |
| 66 | + # an invalid trusted root, so it creates and tests it |
| 67 | + # here - very early in the whole iteration - to return |
| 68 | + # if it is invalid. If it is valid, it will use it laeter. |
| 69 | + root_sz = fdp.ConsumeIntInRange(0, 16 * 1024) # up to 16KB |
| 70 | + trusted_root_bytes = fdp.ConsumeBytes(root_sz) |
| 71 | + |
| 72 | + tmp_tr_path: Path |
| 73 | + with tempfile.NamedTemporaryFile( |
| 74 | + prefix="trusted_root_", suffix=".json", delete=False |
| 75 | + ) as tmp_tr: |
| 76 | + tmp_tr_path = Path(tmp_tr.name) |
| 77 | + tmp_tr.write(trusted_root_bytes) |
| 78 | + |
| 79 | + try: |
| 80 | + # Early validation to catch bad JSON |
| 81 | + TrustedRoot.from_file(str(tmp_tr_path)) |
| 82 | + except Exception: |
| 83 | + # Bad or unsupported JSON: return and retry |
| 84 | + os.unlink(tmp_tr_path) |
| 85 | + return |
| 86 | + |
| 87 | + # Temp dirs for sigstore TUF (metadata/artifacts) + model + signature |
| 88 | + with ( |
| 89 | + tempfile.TemporaryDirectory(prefix="tuf-metadata-") as md_tmp, |
| 90 | + tempfile.TemporaryDirectory(prefix="tuf-artifacts-") as art_tmp, |
| 91 | + tempfile.TemporaryDirectory(prefix="mt_file_fuzz_") as tmpdir, |
| 92 | + tempfile.TemporaryDirectory(prefix="mt_sig_fuzz_") as sigdir, |
| 93 | + ): |
| 94 | + # Create the model root |
| 95 | + root = Path(tmpdir) |
| 96 | + |
| 97 | + # 1) Populate model dir with randomizd files and exit early if empty |
| 98 | + create_fuzz_files(root, fdp) |
| 99 | + if not any_files(root): |
| 100 | + return |
| 101 | + |
| 102 | + metadata_dir = Path(md_tmp) |
| 103 | + artifacts_dir = Path(art_tmp) |
| 104 | + |
| 105 | + # 2) Create the hooks into sigstore python |
| 106 | + _patch_sigstore_get_dirs(metadata_dir, artifacts_dir) |
| 107 | + _patch_trust_updater_offline_default_true() |
| 108 | + |
| 109 | + # 3) Write the (already validated) trusted_root.json into artifacts dir |
| 110 | + trusted_root_path = artifacts_dir / "trusted_root.json" |
| 111 | + trusted_root_path.write_bytes(trusted_root_bytes) |
| 112 | + |
| 113 | + # 4) Fuzz/write signing_config.v0.2.json |
| 114 | + signing_config_path = artifacts_dir / "signing_config.v0.2.json" |
| 115 | + cfg_sz = fdp.ConsumeIntInRange(0, 16 * 1024) # up to 16KB |
| 116 | + signing_config_path.write_bytes(fdp.ConsumeBytes(cfg_sz)) |
| 117 | + |
| 118 | + # 5) Prepare signature path |
| 119 | + signature_path = Path(sigdir) / "model.signature" |
| 120 | + |
| 121 | + # 6) Sign |
| 122 | + expected_identity = ( |
| 123 | + fdp.ConsumeBytes(32).decode("utf-8", errors="ignore") |
| 124 | + or "default-identity" |
| 125 | + ) |
| 126 | + expected_oidc_issuer = ( |
| 127 | + fdp.ConsumeBytes(32).decode("utf-8", errors="ignore") |
| 128 | + or "https://example.com/" |
| 129 | + ) |
| 130 | + sigstore_oidc_beacon_token = ( |
| 131 | + fdp.ConsumeBytes(64).decode("utf-8", errors="ignore") or "token" |
| 132 | + ) |
| 133 | + |
| 134 | + sc = signing.Config() |
| 135 | + sc.use_sigstore_signer( |
| 136 | + use_staging=True, identity_token=sigstore_oidc_beacon_token |
| 137 | + ) |
| 138 | + sc.sign(root, signature_path) |
| 139 | + |
| 140 | + if not signature_path.exists(): |
| 141 | + return |
| 142 | + |
| 143 | + # 7) Verify |
| 144 | + vc = verifying.Config() |
| 145 | + vc.use_sigstore_verifier( |
| 146 | + identity=expected_identity, |
| 147 | + oidc_issuer=expected_oidc_issuer, |
| 148 | + use_staging=True, |
| 149 | + ) |
| 150 | + vc.verify(root, signature_path) |
| 151 | + |
| 152 | + |
| 153 | +def main(): |
| 154 | + atheris.instrument_all() |
| 155 | + atheris.Setup(sys.argv, TestOneInput) |
| 156 | + atheris.Fuzz() |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + main() |
0 commit comments