Skip to content

librepgp: report errors on short writes to file outputs - #2591

Merged
ronaldtse merged 1 commit into
mainfrom
fix/short-file-write
Nov 26, 2026
Merged

ronaldtse merged 1 commit into
mainfrom
fix/short-file-write

Conversation

@ronaldtse

@ronaldtse ronaldtse commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2590.

Summary

The function file_dst_write() in src/librepgp/stream-common.cpp treated any non-negative return of write() as success. When write() completes partially, for example on a full filesystem or when a file size limit is hit, the remaining data was silently dropped and RNP_SUCCESS was returned. As a result, rnp_save_keys() and rnp_output_finish() succeeded while the file was truncated and the saved keys were lost. Many thanks go to @misja for the precise report and the minimal reproducer.

Fix

  1. The function file_dst_write() now writes the remainder in a loop. Short writes are retried until the buffer is exhausted, EINTR is retried, a zero return is treated as an error, and any failure returns RNP_ERROR_WRITE together with the recorded errno.
  2. The function dst_finish() now propagates the write error of the destination even when no finish callback is set, which is the case for the file destination, so the flush of the buffered output reports the failure through rnp_output_finish(). The finish callback, for example the renaming of a temporary output file over the target one, is skipped when the data was not written in full, so a truncated output cannot replace a good file. This also covers the RNP_OUTPUT_FILE_RANDOM case mentioned in the issue.

Verification

  • The reproducer from the issue, built against the unfixed library, reproduces the reported behavior: rnp_save_keys returns 0, rnp_output_finish returns 0, and the 512-byte truncated pubring.gpg fails to reload. Built against this branch, both calls return RNP_ERROR_WRITE and no truncated file is left behind.
  • The new regression test test_output_short_write uses RLIMIT_FSIZE to force the short-write condition. The test lowers only the soft limit, so it also works for a non-root user, and it ignores and restores SIGXFSZ. The test is POSIX-only and is gated with #ifndef _WIN32, which matches the existing platform-specific tests in the suite. The fix itself is platform-neutral, since it uses the same write() call as before in a retry loop, and ssize_t, EINTR and EIO are available on Windows through uniwin.h and the UCRT.

Test plan

  • The new regression test test_output_short_write passes.
  • The full suite passes with 334 of 334 tests locally on the OpenSSL backend, excluding the large and cli harness tests.
  • The issue reproducer was verified against the unfixed and the fixed libraries.
  • clang-format version 11.1.0, which matches CI, was applied to the touched files.

file_dst_write() treated any non-negative write() return as success, so
a short write (full filesystem, file size limit) silently dropped the
remaining data and reported success, leaving a truncated file: a saved
keyring could be cut short with rnp_save_keys() and rnp_output_finish()
succeeding (found by Misja Hoebe, #2590).

Write the remainder in a loop instead, and propagate a write error from
dst_finish() so rnp_output_finish() reports it; do not run the finish
callback (such as renaming a temporary output file over the target one)
when the data was not written in full.
@ronaldtse

Copy link
Copy Markdown
Contributor Author

@kwkwan two quick ones when you have a moment:

Thank you!

@ronaldtse
ronaldtse merged commit 68349a9 into main Nov 26, 2026
120 of 151 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A short write to a file is reported as success, leaving a truncated keyring

2 participants