diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2bbc3fe --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# read by bash, which a CR at the end of a line breaks. A checkout on Windows turns the line ends into CRLF otherwise +*.sh text eol=lf diff --git a/.github/actions/save-ccache/action.yml b/.github/actions/save-ccache/action.yml index c2f1c43..74c2de9 100644 --- a/.github/actions/save-ccache/action.yml +++ b/.github/actions/save-ccache/action.yml @@ -23,7 +23,7 @@ runs: run: gh cache delete "$CACHE_KEY" --repo "$GITHUB_REPOSITORY" || true - name: Save the cache - uses: actions/cache/save@v4 + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ inputs.path }} key: ${{ inputs.key }} diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..3195bbd --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,28 @@ +version: 2 +updates: + # The actions of the workflows are pinned to a commit, with the version in a comment. Dependabot updates both. + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly + # let a new release settle for a week before proposing it + cooldown: + default-days: 7 + commit-message: + prefix: ci + + # The versions of the tools that build the site, in docs/mkdocs/requirements.in. One pull request for all of them, + # the Docs workflow builds the site with --strict and fails on an update that does not work. + - package-ecosystem: pip + directory: /docs/mkdocs + schedule: + interval: weekly + # let a new release settle for a week before proposing it + cooldown: + default-days: 7 + commit-message: + prefix: docs + groups: + docs: + patterns: + - "*" diff --git a/.github/workflows/build-deb.yml b/.github/workflows/build-deb.yml index 314fc7c..0531506 100644 --- a/.github/workflows/build-deb.yml +++ b/.github/workflows/build-deb.yml @@ -14,6 +14,9 @@ on: - '.github/workflows/build-deb.yml' - 'debian/**' - 'bin/build_monero_cpp.sh' + - 'bin/hardening_flags.sh' + - 'bin/check_elf_hardening.sh' + - 'CMakeLists.txt' workflow_dispatch: inputs: full: @@ -54,8 +57,9 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + persist-credentials: false submodules: recursive - name: Determine package version @@ -70,7 +74,7 @@ jobs: docker system prune -af || true - name: Restore ccache - uses: actions/cache/restore@v4 + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: .ccache key: ccache-deb-${{ matrix.name }}-${{ matrix.arch }} @@ -101,7 +105,15 @@ jobs: key: ccache-deb-${{ matrix.name }}-${{ matrix.arch }} - name: Upload deb package - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: python3-monero_${{ steps.version.outputs.version }}-1${{ matrix.name }}1_${{ matrix.arch }} path: debian/artifacts/python3-monero_*.deb + + # the segments and the exports of the module in the package, and the stack protector and the branch protection in its code. The + # module of the armhf package is read but not disassembled, the objdump of the arm64 runner does not read 32 bit arm. + # After the upload, so that a package that fails can be downloaded and looked at. The release needs this job to succeed + - name: Check hardening and exports + run: | + dpkg-deb -x debian/artifacts/python3-monero_*.deb deb-check + bin/check_elf_hardening.sh deb-check/usr/lib/python3/dist-packages/monero*.so diff --git a/.github/workflows/build-macos.yml b/.github/workflows/build-macos.yml index ad6e920..31f6d8e 100644 --- a/.github/workflows/build-macos.yml +++ b/.github/workflows/build-macos.yml @@ -7,6 +7,9 @@ on: branches: [ main ] paths: - '.github/workflows/build-macos.yml' + - 'CMakeLists.txt' + - 'bin/check_macho_hardening.sh' + - 'bin/install_unbound_macos.sh' workflow_dispatch: workflow_call: @@ -24,6 +27,7 @@ jobs: build: name: ${{ matrix.os }} runs-on: ${{ matrix.os }} + timeout-minutes: 120 env: CCACHE_DIR: ${{ github.workspace }}/.ccache CCACHE_MAXSIZE: 500M @@ -36,13 +40,14 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + persist-credentials: false submodules: recursive - name: Install dependencies run: | - HOMEBREW_NO_AUTO_UPDATE=1 brew install python boost@1.85 hidapi openssl@3 zmq libpgm miniupnpc expat libunwind-headers protobuf unbound ccache + HOMEBREW_NO_AUTO_UPDATE=1 brew install python boost@1.85 hidapi openssl@3 zmq libpgm miniupnpc expat libunwind-headers protobuf ccache brew unlink boost || true brew link boost@1.85 --force # brew links openssl@4 into /opt/homebrew, and the -I/opt/homebrew/include of boost and zeromq comes @@ -54,6 +59,10 @@ jobs: echo "OPENSSL_ROOT_DIR=$(brew --prefix openssl@3)" >> "$GITHUB_ENV" pip3 install pytest pytest-cov setuptools wheel scikit-build-core --break-system-packages + # static, against openssl@3: the unbound of brew is built with OpenSSL 4 + - name: Install unbound + run: bin/install_unbound_macos.sh + - name: Install pybind11 v2.13.6 run: | pip3 install pybind11==2.13.6 pybind11-stubgen --break-system-packages @@ -63,7 +72,7 @@ jobs: pip3 install delocate --break-system-packages - name: Restore ccache - uses: actions/cache/restore@v4 + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: .ccache key: ccache-${{ matrix.os }} @@ -116,8 +125,14 @@ jobs: run: | delocate-wheel -w wheelhouse -v dist/*.whl + # the canary and the exports of the module in the wheel that is published + - name: Check hardening and exports + run: | + python3 -m zipfile -e wheelhouse/*.whl wheel-check + bin/check_macho_hardening.sh wheel-check/monero*.so + - name: Upload artifacts - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: monero-${{ matrix.os }} path: wheelhouse/*.whl diff --git a/.github/workflows/build-windows.yml b/.github/workflows/build-windows.yml index 3f6446e..cb9f45f 100644 --- a/.github/workflows/build-windows.yml +++ b/.github/workflows/build-windows.yml @@ -7,6 +7,8 @@ on: branches: [ main ] paths: - '.github/workflows/build-windows.yml' + - 'CMakeLists.txt' + - 'bin/check_pe_hardening.sh' workflow_dispatch: workflow_call: @@ -23,6 +25,7 @@ permissions: jobs: msys2-mingw64: name: MSYS2 MinGW64 + timeout-minutes: 120 runs-on: windows-latest env: CCACHE_DIR: ${{ github.workspace }}/.ccache @@ -34,12 +37,13 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + persist-credentials: false submodules: recursive - name: Setup Python 3.13 - uses: actions/setup-python@v5 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: '3.13' architecture: 'x64' @@ -50,7 +54,7 @@ jobs: python -m pip install scikit-build-core delvewheel - name: Setup MSYS2 MINGW64 - uses: msys2/setup-msys2@66cd2cce69caa17b53920067426061ca1de3a884 # v2.32.0 + uses: msys2/setup-msys2@ec48f7c5447b3140e2b088413ae3a55687bccb6e # v2.33.0 with: msystem: MINGW64 update: true @@ -131,14 +135,14 @@ jobs: echo "124edc26b009678a4c5e759481a364abf63ed7f2f49a53092e8bd0a47c0c0740 mingw-w64-x86_64-libusb-1.0.30-1-any.pkg.tar.zst" | sha256sum -c pacman -U --noconfirm mingw-w64-x86_64-hidapi-0.15.0-2-any.pkg.tar.zst mingw-w64-x86_64-libusb-1.0.30-1-any.pkg.tar.zst - - name: Install boost v1.92.0 + - name: Install boost v1.87.0 run: | - wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst - echo "8d5a7621778e955a52755940fe16ead78c0e57a2ff443418b43c25388df1ba2a mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst" | sha256sum -c - wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-libs-1.92.0-3-any.pkg.tar.zst - echo "832ee9a748c387fc70e0fb90f3bd6d513db627d3c283512123aeb904f4b2dc5a mingw-w64-x86_64-boost-libs-1.92.0-3-any.pkg.tar.zst" | sha256sum -c - # boost-libs in the repo is a newer rebuild that needs the split gcc runtime - pacman -U --noconfirm mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst mingw-w64-x86_64-boost-libs-1.92.0-3-any.pkg.tar.zst + wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst + echo "241919a5885a9270d0e0937e71cf5e146c2a8cabecc2714f09248350181cb1b6 mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst" | sha256sum -c + wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst + echo "5fd77df3e5983ba0d9c689f46151119e0e59b3da6850c5a0ba54068eeae7749c mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst" | sha256sum -c + # the version that monero-cpp builds with. The boost-libs in the repo is a newer rebuild that needs the split gcc runtime + pacman -U --noconfirm mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst - name: Install pybind11 v2.13.6 run: | @@ -147,7 +151,7 @@ jobs: pacman -U --noconfirm mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst - name: Restore ccache - uses: actions/cache/restore@v4 + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: .ccache key: ccache-windows @@ -207,8 +211,15 @@ jobs: MINGW_BIN_WIN=$(cygpath -w "$MINGW_PREFIX/bin") "$WIN_PYTHON_EXE" -m delvewheel repair dist/*.whl -w wheelhouse --add-path "$MINGW_BIN_WIN" + # the flags of the image (ASLR, DEP) and the exports of the module in the wheel that is published + - name: Check hardening and exports + run: | + export WIN_PYTHON_EXE=$(cygpath -u "$pythonLocation/python.exe") + "$WIN_PYTHON_EXE" -m zipfile -e wheelhouse/*.whl wheel-check + bash bin/check_pe_hardening.sh wheel-check/monero*.pyd + - name: Upload artifacts - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: monero-python-win-amd64 path: wheelhouse/*.whl diff --git a/.github/workflows/codacy.yml b/.github/workflows/codacy.yml index a011031..6204ad4 100644 --- a/.github/workflows/codacy.yml +++ b/.github/workflows/codacy.yml @@ -1,6 +1,8 @@ name: Codacy Coverage Reporter -on: +# workflow_run is what gives this job the secret after a run of Tests on a pull request. It does not check out or run anything +# of the pull request: it downloads the coverage files, and reads them with a reporter pinned to a checksum. +on: # zizmor: ignore[dangerous-triggers] workflow_run: workflows: - Tests @@ -19,41 +21,40 @@ jobs: # skipped every run once the repository moved to libmonero. if: github.event.repository.fork == false && (github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion != 'cancelled') runs-on: ubuntu-latest + timeout-minutes: 15 steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - name: Download coverage report id: download - uses: actions/download-artifact@v4 + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: name: coverage-reports-linux # without a token the action ignores run-id and looks in the current run github-token: ${{ github.token }} run-id: ${{ github.event.workflow_run.id }} - - name: Report python coverage + # The reporter is the version named here, checked against the SHA-512 written here, which is the one that Codacy publishes + # on GitHub (codacy/codacy-coverage-reporter, release 14.1.3, file codacy-coverage-reporter-linux.SHA512SUM). The action of + # Codacy and its get.sh were not pinned: they change with no commit here, and they ran with the token of the project. + # To update, set the version, download the binary and the .SHA512SUM of the release, check one against the other and write the hash + - name: Download the Codacy reporter if: ${{ !cancelled() && steps.download.outcome == 'success' }} - uses: codacy/codacy-coverage-reporter-action@v1 - with: - project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} - language: python - coverage-reports: coverage.xml + env: + REPORTER_VERSION: 14.1.3 + REPORTER_SHA512: 73a6c93fc5db509fd0423d6e545759c3f61d8360ee12054e32a9277daab6add0da6c6639248f6ed6c0042366440293ebc0b6144325aef52c5d3d4b230d65eb17 + run: | + curl --retry 5 --retry-delay 5 --retry-max-time 150 -fsSLo codacy-coverage-reporter "https://artifacts.codacy.com/bin/codacy-coverage-reporter/$REPORTER_VERSION/codacy-coverage-reporter-linux" + echo "$REPORTER_SHA512 codacy-coverage-reporter" | sha512sum -c - + chmod +x codacy-coverage-reporter - - name: Report c++ coverage + # only this step has the token + - name: Report python, c++ and c coverage if: ${{ !cancelled() && steps.download.outcome == 'success' }} - uses: codacy/codacy-coverage-reporter-action@v1 - with: - project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} - language: cpp - coverage-reports: coverage.info - - - name: Report c coverage - if: ${{ !cancelled() && steps.download.outcome == 'success' }} - uses: codacy/codacy-coverage-reporter-action@v1 - with: - project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} - language: c - coverage-reports: coverage.info + env: + CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }} + # the commit that Tests ran on, which is the head of the pull request. A manual run has no workflow_run + COMMIT_UUID: ${{ github.event.workflow_run.head_sha || github.sha }} + run: | + ./codacy-coverage-reporter report --language python --coverage-reports coverage.xml --partial --commit-uuid "$COMMIT_UUID" + ./codacy-coverage-reporter report --language cpp --coverage-reports coverage.info --partial --commit-uuid "$COMMIT_UUID" + ./codacy-coverage-reporter report --language c --coverage-reports coverage.info --partial --commit-uuid "$COMMIT_UUID" + ./codacy-coverage-reporter final --commit-uuid "$COMMIT_UUID" diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 77af887..66ce48e 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -31,12 +31,15 @@ permissions: jobs: build: runs-on: ubuntu-24.04 + timeout-minutes: 20 steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Setup Python - uses: actions/setup-python@v5 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: '3.12' cache: pip @@ -60,7 +63,7 @@ jobs: - name: Upload the site if: github.event_name != 'pull_request' - uses: actions/upload-pages-artifact@v3 + uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0 with: path: docs/mkdocs/public @@ -68,6 +71,7 @@ jobs: needs: build if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' runs-on: ubuntu-24.04 + timeout-minutes: 10 permissions: pages: write id-token: write @@ -80,4 +84,4 @@ jobs: steps: - name: Deploy to GitHub Pages id: deployment - uses: actions/deploy-pages@v4 + uses: actions/deploy-pages@368f82528645a54fb793d4d04e342629a3f51346 # v5.0.1 diff --git a/.github/workflows/lint-workflows.yml b/.github/workflows/lint-workflows.yml new file mode 100644 index 0000000..c73032f --- /dev/null +++ b/.github/workflows/lint-workflows.yml @@ -0,0 +1,60 @@ +name: Lint workflows + +# actionlint reads the workflows (the syntax, the expressions, the shell scripts with shellcheck, the inputs of the actions) and zizmor looks for +# what is unsafe in them: an expression in a script, a permission that is not needed, an action that is not pinned. Both fail the run. +# The configuration of zizmor is .github/zizmor.yml. +on: + push: + branches: [ main ] + paths: + - '.github/**' + pull_request: + branches: [ main ] + paths: + - '.github/**' + workflow_dispatch: + +# A new push to a pull request cancels the previous run. Other events get their own +# group so they are never cancelled or replaced. +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +permissions: + contents: read + +jobs: + lint: + name: actionlint and zizmor + runs-on: ubuntu-24.04 + timeout-minutes: 10 + + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # the release named here, checked against the sha256 that is written here, which is the one in actionlint_1.7.12_checksums.txt of the release + - name: Install actionlint + env: + ACTIONLINT_VERSION: 1.7.12 + ACTIONLINT_SHA256: 8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8 + run: | + curl --retry 5 --retry-delay 5 -fsSLo actionlint.tar.gz "https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz" + echo "$ACTIONLINT_SHA256 actionlint.tar.gz" | sha256sum -c - + tar -xzf actionlint.tar.gz -C "$RUNNER_TEMP" actionlint + + - name: Run actionlint + run: '"$RUNNER_TEMP/actionlint" -color' + + # pinned, a new version can report more + - name: Install zizmor + run: | + python3 -m venv "$RUNNER_TEMP/zizmor" + "$RUNNER_TEMP/zizmor/bin/pip" install --quiet zizmor==1.32.0 + + - name: Run zizmor + env: + GH_TOKEN: ${{ github.token }} + run: '"$RUNNER_TEMP/zizmor/bin/zizmor" --format github .github' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1fb3002..6224570 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,6 +14,7 @@ jobs: check: name: Check the tag and the builds of main runs-on: ubuntu-latest + timeout-minutes: 10 outputs: build_macos_run_id: ${{ steps.find_runs.outputs.build_macos_run_id }} build_windows_run_id: ${{ steps.find_runs.outputs.build_windows_run_id }} @@ -69,20 +70,23 @@ jobs: name: Publish draft release needs: [check, deb] runs-on: ubuntu-latest + timeout-minutes: 30 permissions: contents: write actions: read + attestations: write + id-token: write steps: - name: Download deb packages - uses: actions/download-artifact@v4 + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: pattern: python3-monero_* path: release-assets merge-multiple: true - name: Download macos wheels - uses: actions/download-artifact@v4 + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: github-token: ${{ github.token }} run-id: ${{ needs.check.outputs.build_macos_run_id }} @@ -91,7 +95,7 @@ jobs: merge-multiple: true - name: Download windows wheel - uses: actions/download-artifact@v4 + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: github-token: ${{ github.token }} run-id: ${{ needs.check.outputs.build_windows_run_id }} @@ -100,7 +104,7 @@ jobs: merge-multiple: true - name: Download coverage report - uses: actions/download-artifact@v4 + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: github-token: ${{ github.token }} run-id: ${{ needs.check.outputs.test_run_id }} @@ -118,13 +122,20 @@ jobs: working-directory: release-assets run: sha256sum -- *.deb *.whl *.zip > SHA256SUMS + # lets anyone check an asset against this workflow: gh attestation verify --repo libmonero/monero-python + - name: Attest the assets + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-path: release-assets/* + - name: Create draft release env: GH_TOKEN: ${{ github.token }} + TAG: ${{ github.ref_name }} run: | - gh release create "${{ github.ref_name }}" \ - --repo "${{ github.repository }}" \ + gh release create "$TAG" \ + --repo "$GITHUB_REPOSITORY" \ --draft \ - --title "${{ github.ref_name }}" \ + --title "$TAG" \ --generate-notes \ release-assets/*.deb release-assets/*.whl release-assets/*.zip release-assets/SHA256SUMS diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 962943a..dc7b839 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,6 +19,9 @@ on: - '.github/workflows/build-*.yml' - '.github/workflows/codacy.yml' - '.github/workflows/release.yml' + - '.github/workflows/lint-workflows.yml' + - '.github/zizmor.yml' + - '.github/dependabot.yml' workflow_dispatch: @@ -30,11 +33,13 @@ concurrency: permissions: contents: read - actions: write # replace the ccache entries jobs: linux: runs-on: ubuntu-24.04 + permissions: + contents: read + actions: write # replace the ccache entries name: linux (pytest) timeout-minutes: 120 env: @@ -43,8 +48,9 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + persist-credentials: false submodules: recursive - name: Install dependencies @@ -77,7 +83,7 @@ jobs: cd ../ - name: Restore ccache - uses: actions/cache/restore@v4 + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: .ccache key: ccache-linux-test @@ -158,7 +164,7 @@ jobs: - name: Upload container logs if: always() - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: container-logs-linux path: container-logs/ @@ -169,7 +175,7 @@ jobs: - name: Upload tests logs if: always() - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: test-logs path: monero_tests_*.log @@ -199,7 +205,7 @@ jobs: - name: Upload coverage report if: always() - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: coverage-reports-linux path: | @@ -213,6 +219,10 @@ jobs: windows: name: windows (pytest) runs-on: windows-latest + permissions: + contents: read + actions: write # replace the ccache entries + timeout-minutes: 120 env: CCACHE_DIR: ${{ github.workspace }}/.ccache CCACHE_MAXSIZE: 500M @@ -223,12 +233,13 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + persist-credentials: false submodules: recursive - name: Setup Python 3.13 - uses: actions/setup-python@v5 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: '3.13' architecture: 'x64' @@ -239,7 +250,7 @@ jobs: python -m pip install pytest pytest-rerunfailures pytest-timeout pytest-cov typing_extensions scikit-build-core - name: Setup MSYS2 MINGW64 - uses: msys2/setup-msys2@66cd2cce69caa17b53920067426061ca1de3a884 # v2.32.0 + uses: msys2/setup-msys2@ec48f7c5447b3140e2b088413ae3a55687bccb6e # v2.33.0 with: msystem: MINGW64 update: true @@ -320,14 +331,14 @@ jobs: echo "124edc26b009678a4c5e759481a364abf63ed7f2f49a53092e8bd0a47c0c0740 mingw-w64-x86_64-libusb-1.0.30-1-any.pkg.tar.zst" | sha256sum -c pacman -U --noconfirm mingw-w64-x86_64-hidapi-0.15.0-2-any.pkg.tar.zst mingw-w64-x86_64-libusb-1.0.30-1-any.pkg.tar.zst - - name: Install boost v1.92.0 + - name: Install boost v1.87.0 run: | - wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst - echo "8d5a7621778e955a52755940fe16ead78c0e57a2ff443418b43c25388df1ba2a mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst" | sha256sum -c - wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-libs-1.92.0-3-any.pkg.tar.zst - echo "832ee9a748c387fc70e0fb90f3bd6d513db627d3c283512123aeb904f4b2dc5a mingw-w64-x86_64-boost-libs-1.92.0-3-any.pkg.tar.zst" | sha256sum -c - # boost-libs in the repo is a newer rebuild that needs the split gcc runtime - pacman -U --noconfirm mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst mingw-w64-x86_64-boost-libs-1.92.0-3-any.pkg.tar.zst + wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst + echo "241919a5885a9270d0e0937e71cf5e146c2a8cabecc2714f09248350181cb1b6 mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst" | sha256sum -c + wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst + echo "5fd77df3e5983ba0d9c689f46151119e0e59b3da6850c5a0ba54068eeae7749c mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst" | sha256sum -c + # the version that monero-cpp builds with. The boost-libs in the repo is a newer rebuild that needs the split gcc runtime + pacman -U --noconfirm mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst - name: Install pybind11 v2.13.6 run: | @@ -336,7 +347,7 @@ jobs: pacman -U --noconfirm mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst - name: Restore ccache - uses: actions/cache/restore@v4 + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: .ccache key: ccache-windows @@ -410,6 +421,10 @@ jobs: macos: name: ${{ matrix.os }} (pytest) runs-on: ${{ matrix.os }} + permissions: + contents: read + actions: write # replace the ccache entries + timeout-minutes: 120 env: CCACHE_DIR: ${{ github.workspace }}/.ccache CCACHE_MAXSIZE: 500M @@ -422,13 +437,14 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + persist-credentials: false submodules: recursive - name: Install dependencies run: | - HOMEBREW_NO_AUTO_UPDATE=1 brew install python boost@1.85 hidapi openssl@3 zmq libpgm miniupnpc expat libunwind-headers protobuf unbound ccache + HOMEBREW_NO_AUTO_UPDATE=1 brew install python boost@1.85 hidapi openssl@3 zmq libpgm miniupnpc expat libunwind-headers protobuf ccache brew unlink boost || true brew link boost@1.85 --force # brew links openssl@4 into /opt/homebrew, and the -I/opt/homebrew/include of boost and zeromq comes @@ -440,12 +456,16 @@ jobs: echo "OPENSSL_ROOT_DIR=$(brew --prefix openssl@3)" >> "$GITHUB_ENV" pip3 install pytest pytest-rerunfailures pytest-timeout pytest-cov setuptools wheel scikit-build-core --break-system-packages + # static, against openssl@3: the unbound of brew is built with OpenSSL 4 + - name: Install unbound + run: bin/install_unbound_macos.sh + - name: Install pybind11 v2.13.6 run: | pip3 install pybind11==2.13.6 pybind11-stubgen --break-system-packages - name: Restore ccache - uses: actions/cache/restore@v4 + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: .ccache key: ccache-${{ matrix.os }} diff --git a/.github/zizmor.yml b/.github/zizmor.yml new file mode 100644 index 0000000..4227d98 --- /dev/null +++ b/.github/zizmor.yml @@ -0,0 +1,4 @@ +rules: + # the local actions and workflows are referenced by path, the '$/' form of GitHub is not used in this repository + self-repository: + disable: true diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a97f3e..6227581 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,37 @@ if(ENABLE_COVERAGE) add_link_options(--coverage) endif() +# The flags of the hardening for the sources of the module. The libraries it links get theirs from +# bin/hardening_flags.sh, and bin/check_elf_hardening.sh, bin/check_pe_hardening.sh and +# bin/check_macho_hardening.sh look for the result in the binaries. A flag that the compiler does not take is left out. +# The clang of Apple has the stack protector on, and a python module has no use of the other flags there. +include(CheckCXXCompilerFlag) +if (CMAKE_SYSTEM_NAME STREQUAL "Linux") + foreach(flag IN ITEMS -fstack-protector-strong -fstack-clash-protection) + string(MAKE_C_IDENTIFIER "${flag}" flag_id) + check_cxx_compiler_flag("${flag}" "MONERO_PYTHON_CXX_${flag_id}") + if (MONERO_PYTHON_CXX_${flag_id}) + add_compile_options("${flag}") + endif() + endforeach() + + add_compile_options("-D_GLIBCXX_ASSERTIONS") + add_compile_definitions("$<$,$,$>:_FORTIFY_SOURCE=2>") + + if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64)$") + check_cxx_compiler_flag(-fcf-protection=full MONERO_PYTHON_CXX_FCF_PROTECTION) + if (MONERO_PYTHON_CXX_FCF_PROTECTION) + add_compile_options(-fcf-protection=full) + endif() + endif() +elseif (MINGW) + # the gcc of MSYS2 does not turn the stack protector on, and the runtime of mingw-w64 has __stack_chk_fail and the guard + check_cxx_compiler_flag(-fstack-protector-strong MONERO_PYTHON_CXX_FSTACK_PROTECTOR_STRONG) + if (MONERO_PYTHON_CXX_FSTACK_PROTECTOR_STRONG) + add_compile_options(-fstack-protector-strong) + endif() +endif() + ################### # Monero C++ ################### @@ -361,4 +392,10 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Cl target_link_options(monero PRIVATE -Wl,--exclude-libs,ALL -Wl,--gc-sections) endif() +# full RELRO, and a stack that is not executable: a library of the static ones that has no .note.GNU-stack makes the linker ask for an executable +# stack for the whole module, which the deb of Ubuntu 24.04 had +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + target_link_options(monero PRIVATE -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack) +endif() + install(TARGETS monero DESTINATION .) diff --git a/README.md b/README.md index e8e6738..358a2ef 100644 --- a/README.md +++ b/README.md @@ -189,9 +189,11 @@ The package is built from [debian/](debian) with `dpkg-buildpackage`, the way CI echo "89fb61549a437d65076a092a367fcb264fc7045ee9ebef48cd81f5a76a5588f8 mingw-w64-x86_64-hidapi-0.15.0-2-any.pkg.tar.zst" | sha256sum -c echo "124edc26b009678a4c5e759481a364abf63ed7f2f49a53092e8bd0a47c0c0740 mingw-w64-x86_64-libusb-1.0.30-1-any.pkg.tar.zst" | sha256sum -c pacman -U mingw-w64-x86_64-hidapi-0.15.0-2-any.pkg.tar.zst mingw-w64-x86_64-libusb-1.0.30-1-any.pkg.tar.zst - wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst - echo "8d5a7621778e955a52755940fe16ead78c0e57a2ff443418b43c25388df1ba2a mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst" | sha256sum -c - pacman -U mingw-w64-x86_64-boost-1.92.0-3-any.pkg.tar.zst + wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst + echo "241919a5885a9270d0e0937e71cf5e146c2a8cabecc2714f09248350181cb1b6 mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst" | sha256sum -c + wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst + echo "5fd77df3e5983ba0d9c689f46151119e0e59b3da6850c5a0ba54068eeae7749c mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst" | sha256sum -c + pacman -U mingw-w64-x86_64-boost-1.87.0-3-any.pkg.tar.zst mingw-w64-x86_64-boost-libs-1.87.0-3-any.pkg.tar.zst wget https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst echo "5b56ad87bc544336b2d51edb8bb0614f46542571362daf4d7e53bf23120175a1 mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst" | sha256sum -c pacman -U mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..247344e --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,25 @@ +# Security Policy + +## Supported versions + +Security fixes are made on `main` and included in a subsequent release. Older releases are not guaranteed to receive security fixes; use the latest release where possible. + +## Reporting a vulnerability + +Please report vulnerabilities privately through [GitHub's private vulnerability reporting](https://github.com/libmonero/monero-python/security/advisories/new). Do not disclose vulnerability details in public issues. + +If private reporting is unavailable, contact a repository maintainer privately before sharing technical details. Include the affected version, impact, and steps to reproduce, and remove secrets or personal data from any attached logs. + +## Verifying release provenance + +Release assets (the `.deb` packages, the wheels and the coverage report) carry GitHub build provenance attestations, and the release lists their SHA256 sums in `SHA256SUMS`. Verify an asset with GitHub CLI: + +```sh +gh attestation verify --repo libmonero/monero-python +``` + +or against the sums: + +```sh +sha256sum -c SHA256SUMS +``` diff --git a/bin/build_monero_cpp.sh b/bin/build_monero_cpp.sh index 0c07a32..288d0dd 100755 --- a/bin/build_monero_cpp.sh +++ b/bin/build_monero_cpp.sh @@ -21,6 +21,10 @@ case $(uname -m) in *) arch=native;; esac +# the flags of the hardening, for monero-project and monero-cpp +# shellcheck source=bin/hardening_flags.sh +. "$root/bin/hardening_flags.sh" + openssl_args=() if [ -n "${OPENSSL_ROOT_DIR:-}" ]; then openssl_args+=(-DOPENSSL_ROOT_DIR="$OPENSSL_ROOT_DIR") diff --git a/bin/check_elf_hardening.sh b/bin/check_elf_hardening.sh new file mode 100755 index 0000000..b494673 --- /dev/null +++ b/bin/check_elf_hardening.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +# Checks the hardening of the module on Linux: the segments and the dynamic section, the exports, and how much of the code the compiler protected. +# +# check_elf_hardening.sh path/to/monero.cpython-312-x86_64-linux-gnu.so +# +# The module is stripped (by pybind11 and by dh_strip), so there are no function names to count by kind: what is counted are the calls to +# __stack_chk_fail, one for each function that has a canary, and, on x86_64, the endbr64 instructions of the branch protection. The disassembly +# is read for x86_64 and aarch64 only, the objdump of an arm64 runner does not read the 32 bit arm module that is built there. +# +# What the debs had, to put the minimums where a build with none of the flags falls under them: +# before the flags, with every symbol exported and no code removed (0.9.4): +# trixie amd64 : 13306 calls to __stack_chk_fail, 47087 endbr64, 5 _chk functions imported, no full RELRO +# noble amd64 : 27284 calls to __stack_chk_fail, 82737 endbr64, 15 _chk functions imported, no full RELRO, an executable stack (GNU_STACK RWE) +# with the flags of bin/hardening_flags.sh and CMakeLists.txt (run 43531387312, bookworm amd64): 6472 calls to __stack_chk_fail, 66916 endbr64, +# 12 _chk functions imported, 890 exports. There are fewer calls than before because --gc-sections now drops the code that nothing reaches. +# The minimums are about half of the build with the flags. The one of aarch64 was not measured. +# +# The exports are PyInit_monero, the names that the linker adds, the C++ symbols of Boost and the standard library, and the typeinfo: the +# headers of Boost mark many of their classes visible (BOOST_SYMBOL_VISIBLE), namespace std is visible, and pybind11 makes the typeinfo of the +# types it binds (the enums of monero, the function types of the vector binders), so the module's own objects export them whatever +# -fvisibility=hidden says, as the Windows module exports the ones of Boost.Serialization. The symbols of the static libraries are hidden. +# The 32 that were not allowed at first, in the builds of bookworm and trixie, were all typeinfo. +set -euo pipefail +export LC_ALL=C + +library="${1:?usage: check_elf_hardening.sh monero.cpython-312-x86_64-linux-gnu.so}" + +MIN_FORTIFY_IMPORTS=8 +MIN_CANARY_CALLS_X86_64=3000 +MIN_CANARY_CALLS_AARCH64=1000 +MIN_LANDING_PADS_X86_64=20000 + +failed=0 +fail() { + echo "FAIL: $*" + failed=1 +} + +for tool in readelf awk; do + command -v "$tool" > /dev/null || { echo "$tool is needed"; exit 2; } +done + +# the output of a tool is kept in a variable and read with here-strings: grep -q that stops at the first match makes the tool before it in a pipe +# fail with SIGPIPE, which pipefail turns into a failure +header=$(readelf -h "$library") +machine=$(awk -F: '/Machine:/ { gsub(/^[ \t]+/, "", $2); print $2 }' <<< "$header") +echo "$library: $machine" +grep -Eq 'Type:[[:space:]]+DYN' <<< "$header" || fail "the module is not a shared object" + +program_headers=$(readelf -lW "$library") +grep -q 'GNU_RELRO' <<< "$program_headers" || fail "RELRO segment is missing" +awk '$1 == "GNU_STACK" { found = 1; if ($7 ~ /E/) executable = 1 } END { exit !(found && !executable) }' <<< "$program_headers" || fail "GNU_STACK is executable or missing" +dynamic_section=$(readelf -dW "$library") +grep -Eq '\(BIND_NOW\)|\(FLAGS_1\).*NOW' <<< "$dynamic_section" || fail "full RELRO (BIND_NOW) is missing" +# a path of the build machine in the module, or a path to look for libraries in +if grep -Eq '\((RPATH|RUNPATH)\)' <<< "$dynamic_section"; then + fail "the module has an RPATH or RUNPATH: $(grep -E '\((RPATH|RUNPATH)\)' <<< "$dynamic_section")" +fi + +dynamic_symbols=$(readelf --dyn-syms --wide "$library") +grep -Eq 'UND[[:space:]]+__stack_chk_fail(@|$)' <<< "$dynamic_symbols" || fail "__stack_chk_fail is not imported, so there are no stack canaries" +fortify_imports=$(awk '$7 == "UND" && $8 ~ /^__[A-Za-z0-9_]+_chk(@|$)/ { n++ } END { print n + 0 }' <<< "$dynamic_symbols") +echo "FORTIFY functions imported: $fortify_imports (minimum $MIN_FORTIFY_IMPORTS)" +[ "$fortify_imports" -ge "$MIN_FORTIFY_IMPORTS" ] || fail "only $fortify_imports _chk functions are imported, FORTIFY_SOURCE is not on in most of the code" + +# a python module exports PyInit_monero and nothing else. The linker adds a few names of its own to some shared objects +exports=$(awk 'NF >= 8 && $1 ~ /^[0-9]+:$/ && $7 != "UND" { sub(/@.*/, "", $8); print $8 }' <<< "$dynamic_symbols" | sort -u) +# the mangled names of Boost and of the standard library: the symbol itself, its guard variable, vtable, typeinfo and thunks, and the local statics of a function +visible_cpp='^_ZT[IS]|^_Z(GV|TV|TI|TS|TT|GR)?Z?(Th[0-9n]+_|Tv[0-9n]+_[0-9n]+_)?(N[KVr]*)?(5boost|St|9__gnu_cxx)' +unexpected=$(grep -Evx 'PyInit_monero|_init|_fini|_edata|_end|__bss_start' <<< "$exports" | grep -Ev "$visible_cpp" || true) +echo "exports: $(wc -l <<< "$exports")" +grep -qx 'PyInit_monero' <<< "$exports" || fail "PyInit_monero is not exported" +if [ -n "$unexpected" ]; then + fail "the module exports $(wc -l <<< "$unexpected") symbols besides PyInit_monero, the typeinfo and the C++ of Boost and std, such as:" + head -n 30 <<< "$unexpected" +fi + +case "$machine" in + "Advanced Micro Devices X86-64") canary_pattern='call.*__stack_chk_fail' min_canary=$MIN_CANARY_CALLS_X86_64 ;; + AArch64) canary_pattern='bl[[:space:]].*__stack_chk_fail' min_canary=$MIN_CANARY_CALLS_AARCH64 ;; + *) canary_pattern='' ;; +esac +if [ -n "$canary_pattern" ] && command -v objdump > /dev/null; then + disassembly=$(objdump -d --no-show-raw-insn "$library") + calls=$(grep -Ec "$canary_pattern" <<< "$disassembly" || true) + echo "calls to __stack_chk_fail: $calls (minimum $min_canary)" + [ "$calls" -ge "$min_canary" ] || fail "only $calls calls to __stack_chk_fail, so most of the code has no stack protector" + if [ "$machine" = "Advanced Micro Devices X86-64" ]; then + pads=$(grep -c endbr64 <<< "$disassembly" || true) + echo "endbr64: $pads (minimum $MIN_LANDING_PADS_X86_64)" + [ "$pads" -ge "$MIN_LANDING_PADS_X86_64" ] || fail "only $pads endbr64 instructions, so most of the code has no branch protection" + fi +else + echo "the code of $machine is not disassembled here, the counts are skipped" +fi + +if [ "$failed" -ne 0 ]; then + exit 1 +fi +echo "hardening of $library: ok" diff --git a/bin/check_macho_hardening.sh b/bin/check_macho_hardening.sh new file mode 100755 index 0000000..94fa08b --- /dev/null +++ b/bin/check_macho_hardening.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# Checks the hardening and the exports of the module on macOS. Run it on macOS, which has nm and otool. +# +# check_macho_hardening.sh path/to/monero.cpython-314-darwin.so +# +# Apple clang turns the stack protector on (-fstack-protector-strong), so what is checked is that the module imports the guard and the handler of +# the canary, and that many functions call the handler. The count is the number of calls to ___stack_chk_fail in the code, a call for each function +# that has a canary. The minimum was not measured on this module: monero-c, which links the same libraries of monero-project, has about 3400, and +# a module built with no stack protector, or whose libraries were, has a handful. It is a guess, the first run of the check prints the real number. +# +# The exports are _PyInit_monero and nothing else, which the linker is asked for with -exported_symbol in CMakeLists.txt. +set -euo pipefail +export LC_ALL=C + +library="${1:?usage: check_macho_hardening.sh monero.cpython-314-darwin.so}" +MIN_CANARY_CALLS=500 + +failed=0 +fail() { + echo "FAIL: $*" + failed=1 +} + +for tool in nm otool awk; do + command -v "$tool" > /dev/null || { echo "$tool is needed"; exit 2; } +done + +echo "$library" +undefined=$(nm -u "$library") +for symbol in ___stack_chk_fail ___stack_chk_guard; do + # nm of Apple prints the name alone for -u, and some print the type before it + grep -Eq "(^|[[:space:]])${symbol}\$" <<< "$undefined" || fail "$symbol is not imported, so there are no stack canaries" +done + +# a call is a line of the code that names the handler. The stub of the dynamic linker is named in each of them +calls=$(otool -tV "$library" | awk '/___stack_chk_fail/ { n++ } END { print n + 0 }') +echo "calls to ___stack_chk_fail: $calls (minimum $MIN_CANARY_CALLS)" +[ "$calls" -ge "$MIN_CANARY_CALLS" ] || fail "only $calls calls to ___stack_chk_fail, so most of the code has no stack protector" + +# the names of the external symbols that the module defines, with the type before them when nm prints it +exports=$(nm -gU "$library" | awk 'NF { print $NF }' | sort -u) +echo "exports: $(wc -l <<< "$exports" | tr -d ' ')" +grep -qx '_PyInit_monero' <<< "$exports" || fail "_PyInit_monero is not exported" +unexpected=$(grep -Evx '_PyInit_monero|__mh_[a-z_]+_header' <<< "$exports" || true) +if [ -n "$unexpected" ]; then + fail "the module exports $(wc -l <<< "$unexpected" | tr -d ' ') symbols besides _PyInit_monero, such as:" + head -n 20 <<< "$unexpected" +fi + +if [ "$failed" -ne 0 ]; then + exit 1 +fi +echo "hardening of $library: ok" diff --git a/bin/check_pe_hardening.sh b/bin/check_pe_hardening.sh new file mode 100755 index 0000000..b78e69d --- /dev/null +++ b/bin/check_pe_hardening.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# Checks the hardening and the exports of the module on Windows. Run it in MSYS2 MINGW64 (it needs objdump and c++filt of binutils, and awk). +# +# check_pe_hardening.sh path/to/monero.cp313-win_amd64.pyd +# +# 1. The flags of the image: ASLR with a high entropy address space (DYNAMIC_BASE, HIGH_ENTROPY_VA) and DEP (NX_COMPAT). mingw-w64 sets them. +# 2. The exports: PyInit_monero, and nothing that isn't Boost.Serialization or Boost.Archive. Those are the 1099 symbols that the headers of +# Boost mark with BOOST_SYMBOL_EXPORT and that --exclude-libs doesn't remove. The wheel of 0.9.10 has 1100 exports, and no other. +# +# There is no count of the stack canaries as on Linux: the module is stripped by pybind11, and the calls to __stack_chk_fail have no name left. +# CMakeLists.txt turns the flag on, and the build of the module fails to link if the runtime does not have it. +set -euo pipefail +export LC_ALL=C + +library="${1:?usage: check_pe_hardening.sh monero.cp313-win_amd64.pyd}" + +failed=0 +fail() { + echo "FAIL: $*" + failed=1 +} + +for tool in objdump c++filt awk sort; do + command -v "$tool" > /dev/null || { echo "$tool is needed"; exit 2; } +done + +# the output of a tool is kept in a variable and read with here-strings: grep -q that stops at the first match makes the tool before it in a pipe +# fail with SIGPIPE, which pipefail turns into a failure +headers=$(objdump -p "$library") +echo "$library" +for flag in DYNAMIC_BASE HIGH_ENTROPY_VA NX_COMPAT; do + grep -q "$flag" <<< "$headers" || fail "the image has no $flag in its DllCharacteristics" +done + +exports=$(sed -n '/Ordinal\/Name Pointer/,$p' <<< "$headers" | awk '/^[ \t]*\[ *[0-9]+\]/ { print $NF }' | sort -u) +echo "exports: $(wc -l <<< "$exports")" +grep -qx 'PyInit_monero' <<< "$exports" || fail "PyInit_monero is not exported" +foreign=$(grep -vx 'PyInit_monero' <<< "$exports" | c++filt | grep -Ev '^((vtable|typeinfo|typeinfo name|guard variable|VTT|construction vtable) for )?((non-virtual |virtual )?thunk to )?boost::(serialization|archive)::' || true) +if [ -n "$foreign" ]; then + fail "the module exports $(wc -l <<< "$foreign") symbols that are not PyInit_monero or of Boost.Serialization and Boost.Archive, such as:" + head -n 20 <<< "$foreign" +fi + +if [ "$failed" -ne 0 ]; then + exit 1 +fi +echo "hardening of $library: ok" diff --git a/bin/hardening_flags.sh b/bin/hardening_flags.sh new file mode 100644 index 0000000..e90965c --- /dev/null +++ b/bin/hardening_flags.sh @@ -0,0 +1,35 @@ +# shellcheck shell=sh +# Sourced by the scripts that build the static libraries of the module (bin/build_monero_cpp.sh, +# debian/install-static-deps.sh). On Linux it adds the flags of the hardening to CFLAGS and CXXFLAGS, which +# cmake and configure take from the environment: +# +# . bin/hardening_flags.sh +# +# Debian's gcc turns none of them on, Ubuntu's turns on some, so the same sources gave a different library on +# each release. A flag that the compiler does not know (-fstack-clash-protection on 32 bit arm, -fcf-protection +# outside of x86) is left out instead of failing the build. The module has the same flags in CMakeLists.txt. +# macOS is not touched, the clang of Apple has the stack protector on, and the Windows build has its own toolchain. +# +# -O2 is there because configure drops its default -O2 once CFLAGS is set, and _FORTIFY_SOURCE needs it. The +# CMAKE_BUILD_TYPE of monero-project adds its own -O3 after it. +if [ "$(uname -s)" = Linux ]; then + hardening_flags="-O2 -D_FORTIFY_SOURCE=2" + hardening_flag_candidates="-fstack-protector-strong -fstack-clash-protection" + if [ "$(uname -m)" = x86_64 ]; then + hardening_flag_candidates="$hardening_flag_candidates -fcf-protection=full" + fi + for hardening_flag in $hardening_flag_candidates; do + # CC can be "ccache gcc" + # shellcheck disable=SC2086 + if echo 'int main(void) { return 0; }' | ${CC:-cc} -Werror "$hardening_flag" -x c -c -o /dev/null - 2> /dev/null; then + hardening_flags="$hardening_flags $hardening_flag" + else + echo "hardening: the compiler does not take $hardening_flag, building without it" >&2 + fi + done + CFLAGS="${CFLAGS:+$CFLAGS }$hardening_flags" + CXXFLAGS="${CXXFLAGS:+$CXXFLAGS }$hardening_flags -D_GLIBCXX_ASSERTIONS" + export CFLAGS CXXFLAGS + echo "hardening: CFLAGS=$CFLAGS" >&2 + unset hardening_flags hardening_flag_candidates hardening_flag +fi diff --git a/bin/install_unbound_macos.sh b/bin/install_unbound_macos.sh new file mode 100755 index 0000000..cb2d1b9 --- /dev/null +++ b/bin/install_unbound_macos.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# Builds libunbound as a static library in /usr/local against openssl@3, for the macOS builds (build-macos.yml and test.yml). It needs the +# formulae expat and openssl@3 of brew, and sudo for the install. +# +# The unbound of brew is built with OpenSSL 4, and the module links OpenSSL 3 as boost 1.85 does not build with 4: the same as the +# build of monero-cpp does. It is the same version and the same checksum as debian/install-static-deps.sh has for Linux. The library +# is made with no libevent and no DoH, so the module needs neither libevent nor nghttp2. +set -euo pipefail + +version=1.22.0 +sha256=c5dd1bdef5d5685b2cedb749158dd152c52d44f65529a34ac15cd88d4b1b3d43 + +# the headers and the library of brew's unbound would be found before the ones of /usr/local +if brew list --versions unbound > /dev/null 2>&1; then + brew unlink unbound +fi + +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +cd "$work" + +curl --retry 5 --retry-delay 5 -fsSLO "https://www.nlnetlabs.nl/downloads/unbound/unbound-${version}.tar.gz" +echo "${sha256} unbound-${version}.tar.gz" | shasum -a 256 -c - +tar xzf "unbound-${version}.tar.gz" +cd "unbound-${version}" +./configure --with-libexpat="$(brew --prefix expat)" --with-ssl="$(brew --prefix openssl@3)" --with-libunbound-only --with-libevent=no --enable-static --disable-shared --with-pic +make -j3 +sudo make install diff --git a/debian/install-static-deps.sh b/debian/install-static-deps.sh index 619e49a..0dadef7 100755 --- a/debian/install-static-deps.sh +++ b/debian/install-static-deps.sh @@ -11,6 +11,9 @@ if command -v ccache > /dev/null 2>&1; then export CC="ccache gcc" fi +# the flags of the hardening, which expat and unbound get from the environment +. "$(dirname "$0")/../bin/hardening_flags.sh" + work=$(mktemp -d) trap 'rm -rf "$work"' EXIT cd "$work" diff --git a/external/monero-cpp b/external/monero-cpp index 1674b58..6178753 160000 --- a/external/monero-cpp +++ b/external/monero-cpp @@ -1 +1 @@ -Subproject commit 1674b581ebb6e88a8b2f1d0206d420d51402c978 +Subproject commit 6178753bc9b1956b4d2e834507b6ff4cb55229c7 diff --git a/src/python/monero_tx_query.pyi b/src/python/monero_tx_query.pyi index 8e00691..f192e0e 100644 --- a/src/python/monero_tx_query.pyi +++ b/src/python/monero_tx_query.pyi @@ -17,7 +17,7 @@ class MoneroTxQuery(MoneroTxWallet): """Get transactions by hashes.""" height: int | None """Get transactions by height.""" - include_outputs: int | None + include_outputs: bool | None """Include outputs in transaction data.""" input_query: MoneroOutputQuery | None """Query to apply on transaction inputs.""" diff --git a/tests/test_monero_wallet_common.py b/tests/test_monero_wallet_common.py index fd1c6a5..f6b2417 100644 --- a/tests/test_monero_wallet_common.py +++ b/tests/test_monero_wallet_common.py @@ -260,6 +260,7 @@ def test_sync_with_pool_same_accounts(self, daemon: MoneroDaemonRpc, wallet: Mon # Can sync with txs submitted and flushed from the pool @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_RELAYS disabled") @pytest.mark.skipif(TestUtils.LITE_MODE, reason="LITE_MODE enabled") + @pytest.mark.skipif(TestUtils.CI, reason="CI is too slow to catchup failed txs (see wallet2::process_unconfirmed_transfer)") def test_sync_with_pool_submit_and_flush(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: logger.warning("This test takes at least 500 seconds (~8 minutes) to catchup failed txs (see wallet2::process_unconfirmed_transfer).") config: MoneroTxConfig = MoneroTxConfig() diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index 48e2df8..be7b02e 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -28,6 +28,8 @@ class TestUtils(ABC): _LOADED: bool = False """Indicates if test configuration is loaded.""" + CI: bool = getenv("GITHUB_ACTIONS", "").lower() == "true" + """indicates if tests are running in the CI of GitHub. It is set when the class is defined, since the tests use it in skipif.""" IN_CONTAINER: bool = True """indicates if tests are running in docker container.""" MIN_BLOCK_HEIGHT: int = 0