Git development
 help / color / mirror / Atom feed
From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Phillip Wood <phillip.wood123@gmail.com>,
	Patrick Steinhardt <ps@pks.im>,
	Harald Nordgren <haraldnordgren@gmail.com>,
	Simon Richter <Simon.Richter@hogyros.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH v3 0/5] Address overflows pointed out by Coverity
Date: Wed, 25 Nov 2026 20:55:44 +0000	[thread overview]
Message-ID: <pull.2253.v3.git.1795640149.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2253.git.1791958171.gitgitgadget@gmail.com>

This patch series is part of my effort to bring Git for Windows' Coverity
reports down to zero; Previous patch series have been merged in 347fe8a91b5
(Merge branch 'js/coverity-fixes', 2026-07-16), 0b75ef5168c (Merge branch
'js/coverity-fixes-null-safety', 2026-07-19), fcc8c931b53 (Merge branch
'js/coverity-unchecked-returns-fix', 2026-08-24) and 8bc9c7fcfd8 (Merge
branch 'js/coverity-fixes', 2026-10-03).

Changes since v2:

 * Fixed a grammar error and an overstatement in the "Changes since v1"
   section :-)
 * Accepted Junio's rewritten explanation in 2/5's commit message.
 * Clarified the design decision to abort on programing errors in 4/5's
   commit message.
 * Adjusted a few straggler ssize_t variables that can now safely be int or
   even be dropped.

Changes since v1:

 * Five callers of read_in_full() and all callers of write_in_full() are now
   no longer restricted by the range of ssize_t.
 * The signed_mult_overflows() and mult_overflows() macros have been
   dropped.
 * Since the approxidate() function only deals with non-negative values, the
   complicated overflow check could have been simplified.
 * Rebased to master, to avoid merge conflicts.

Johannes Schindelin (5):
  archive-tar: guard get_path_prefix against zero-length input
  http: use `size_t` for byte position in fwrite_sha1_file
  wrapper: avoid byte-count overflow in write_in_full()
  wrapper: add read_in_full_or_error()
  date: avoid overflow in approxidate relative-time multiplication

 Makefile                        |  1 +
 archive-tar.c                   |  2 +
 builtin/blame.c                 |  2 +-
 combine-diff.c                  |  6 +-
 config.c                        | 14 ++---
 date.c                          | 26 +++++++--
 dir.c                           |  2 +-
 entry.c                         |  2 +-
 grep.c                          |  2 +-
 http.c                          |  2 +-
 imap-send.c                     |  2 +-
 notes-merge.c                   | 13 +----
 odb/streaming.c                 |  6 +-
 parallel-checkout.c             |  2 +-
 refs/packed-backend.c           |  4 +-
 strbuf.c                        | 10 +++-
 t/meson.build                   |  1 +
 t/t0006-date.sh                 | 13 +++++
 t/unit-tests/u-reftable-stack.c |  2 +-
 t/unit-tests/u-wrapper.c        | 99 +++++++++++++++++++++++++++++++++
 wrapper.c                       | 56 ++++++++++++++-----
 wrapper.h                       | 14 ++++-
 22 files changed, 218 insertions(+), 63 deletions(-)
 create mode 100644 t/unit-tests/u-wrapper.c


base-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2253%2Fdscho%2Fcoverity-fixes-overflow-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2253/dscho/coverity-fixes-overflow-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/2253

Range-diff vs v2:

 1:  bb8225547f = 1:  bb8225547f archive-tar: guard get_path_prefix against zero-length input
 2:  38cc1dec53 ! 2:  aa2d3961b2 http: use `size_t` for byte position in fwrite_sha1_file
     @@ Metadata
       ## Commit message ##
          http: use `size_t` for byte position in fwrite_sha1_file
      
     -    The `fwrite_sha1_file()` function tracks the number of bytes written via
     -    an `int` variable, but the total size to write is of type `size_t`. When
     -    the HTTP response body exceeds INT_MAX (~2GB), this causes an overflow
     -    and a heap under-read, then writes that data to the local object file.
     -
     -    The hash check afterward would reject the corrupt object (so no bad data
     -    enters the object store), but the under-read itself could leak adjacent
     -    heap contents to the temporary file. In practice this is unlikely
     -    because curl delivers data in small chunks (typically 16KB per callback
     -    invocation), so in practice, the total size is far below INT_MAX in a
     -    single call. However, the function interface accepts arbitrary sizes,
     -    and the accumulation across the loop means that the `int` variable grows
     -    across calls within a single invocation, not across callbacks.
     -
     -    Change the type from `int` to `size_t` to match the type of the total
     -    size. This is a hardening fix: the attack surface (a malicious HTTP
     -    server triggering a >2GB single-callback delivery) is theoretical under
     -    normal curl configurations, but the type mismatch is a genuine defect.
     +    The `fwrite_sha1_file()` function tracks the number of bytes written
     +    during a single invocation in an `int` variable (`posn`), even though
     +    the buffer length `size = eltsize * nmemb` has type `size_t`. If a
     +    caller or callback ever passed more than `INT_MAX` bytes in a single
     +    call, `posn` could overflow and wrap negative, causing `(char *)ptr +
     +    posn` to under-read the buffer. While curl delivers data in small
     +    chunks in practice, matching the variable type to `size` avoids the
     +    defect.
      
          Pointed out by Coverity.
      
          Helped-by: Patrick Steinhardt <ps@pks.im>
          Helped-by: Junio C Hamano <gitster@pobox.com>
     +    Assisted-by: GPT-6 Luna
          Assisted-by: Claude Opus 4.6 and GPT-6.1 Sol
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
 3:  b24520c50d ! 3:  f38c7e7133 wrapper: avoid byte-count overflow in write_in_full()
     @@ Makefile: CLAR_TEST_SUITES += u-strvec
       CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
       CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
      
     + ## config.c ##
     +@@ config.c: static struct strbuf store_create_section(const char *key,
     + 	return sb;
     + }
     + 
     +-static ssize_t write_section(int fd, const char *key,
     +-			     const struct config_store_data *store)
     ++static int write_section(int fd, const char *key,
     ++			 const struct config_store_data *store)
     + {
     + 	struct strbuf sb = store_create_section(key, store);
     +-	ssize_t ret;
     ++	int ret;
     + 
     + 	ret = write_in_full(fd, sb.buf, sb.len);
     + 	strbuf_release(&sb);
     +@@ config.c: static ssize_t write_section(int fd, const char *key,
     + 	return ret;
     + }
     + 
     +-static ssize_t write_pair(int fd, const char *key, const char *value,
     +-			  const char *comment,
     +-			  const struct config_store_data *store)
     ++static int write_pair(int fd, const char *key, const char *value,
     ++		      const char *comment,
     ++		      const struct config_store_data *store)
     + {
     + 	int i;
     +-	ssize_t ret;
     ++	int ret;
     + 	const char *quote = "";
     + 	struct strbuf sb = STRBUF_INIT;
     + 
     +
     + ## entry.c ##
     +@@ entry.c: static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
     + 	char *new_blob;
     + 	struct strbuf buf = STRBUF_INIT;
     + 	size_t size;
     +-	ssize_t wrote;
     ++	int wrote;
     + 	size_t newsize = 0;
     + 	struct stat st;
     + 	const struct submodule *sub;
     +
       ## imap-send.c ##
      @@ imap-send.c: static int socket_write(struct imap_socket *sock, const char *buf, int len)
       		n = SSL_write(sock->ssl, buf, len);
     @@ notes-merge.c: static void write_buf_to_worktree(const struct object_id *obj,
       	close(fd);
       	free(path);
      
     + ## odb/streaming.c ##
     +@@ odb/streaming.c: int odb_stream_blob_to_fd(struct object_database *odb,
     + 		goto close_and_exit;
     + 	for (;;) {
     + 		char buf[1024 * 16];
     +-		ssize_t wrote, holeto;
     ++		ssize_t holeto;
     + 		ssize_t readlen = odb_stream_read(st, buf, sizeof(buf));
     + 
     + 		if (readlen < 0)
     +@@ odb/streaming.c: int odb_stream_blob_to_fd(struct object_database *odb,
     + 			goto close_and_exit;
     + 		else
     + 			kept = 0;
     +-		wrote = write_in_full(fd, buf, readlen);
     +-
     +-		if (wrote < 0)
     ++		if (write_in_full(fd, buf, readlen) < 0)
     + 			goto close_and_exit;
     + 	}
     + 	if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
     +
     + ## parallel-checkout.c ##
     +@@ parallel-checkout.c: static int write_pc_item_to_fd(struct parallel_checkout_item *pc_item, int fd,
     + 	struct strbuf buf = STRBUF_INIT;
     + 	char *blob;
     + 	size_t size;
     +-	ssize_t wrote;
     ++	int wrote;
     + 
     + 	/* Sanity check */
     + 	ASSERT(is_eligible_for_parallel_checkout(pc_item->ce, &pc_item->ca));
     +
       ## t/meson.build ##
      @@ t/meson.build: clar_test_suites = [
         'unit-tests/u-trailer.c',
 4:  ca30ed613d ! 4:  d45982f1cc wrapper: add read_in_full_or_error()
     @@ Commit message
          that returns a Boolean to indicate success or faiure.
      
          Callers that do need to know that byte count can still call
     -    `read_in_full()`, but will now error out early if the requested byte
     -    count exceeds what `ssize_t` can represent.
     +    `read_in_full()`, but will now abort to signal a programming error when
     +    the requested byte count exceeds what `ssize_t` can represent.
      
          Pointed out by Coverity.
      
          Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
     -    Assisted-by: GPT-6.1
     +    Assisted-by: GPT-6.1 and GPT-6 Luna
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
       ## builtin/blame.c ##
 5:  6057734709 = 5:  23ec6cf8f0 date: avoid overflow in approxidate relative-time multiplication

-- 
gitgitgadget

  parent reply	other threads:[~2026-11-25 20:55 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  6:09 [PATCH 0/5] Address overflows pointed out by Coverity Johannes Schindelin via GitGitGadget
2026-10-14  6:09 ` [PATCH 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-10-14  6:09 ` [PATCH 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-10-16  8:38   ` Patrick Steinhardt
2026-10-16 14:30     ` Junio C Hamano
2026-10-17  0:18       ` Simon Richter
2026-10-17  4:10         ` Junio C Hamano
2026-10-17  4:11         ` Junio C Hamano
2026-10-14  6:09 ` [PATCH 3/5] wrapper: reject insanely large byte counts when reading/writing Johannes Schindelin via GitGitGadget
2026-10-14 11:37   ` Phillip Wood
2026-11-15 19:24     ` Johannes Schindelin
2026-10-14  6:09 ` [PATCH 4/5] git-compat-util: add `signed_mult_overflows` and `mult_overflows` macros Johannes Schindelin via GitGitGadget
2026-10-15  9:27   ` Phillip Wood
2026-11-15 19:29     ` Johannes Schindelin
2026-10-14  6:09 ` [PATCH 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-10-14 21:44   ` Junio C Hamano
2026-10-15  9:28   ` Phillip Wood
2026-11-15 19:35     ` Johannes Schindelin
2026-10-16  8:43 ` [PATCH 0/5] Address overflows pointed out by Coverity Harald Nordgren
2026-11-07 22:36 ` Junio C Hamano
2026-11-23 22:13 ` [PATCH v2 " Johannes Schindelin via GitGitGadget
2026-11-23 22:13   ` [PATCH v2 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-11-24 17:34     ` Junio C Hamano
2026-11-23 22:13   ` [PATCH v2 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-11-24 17:54     ` Junio C Hamano
2026-11-25 14:04       ` Johannes Schindelin
2026-11-23 22:13   ` [PATCH v2 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-11-24 14:37     ` Phillip Wood
2026-11-25 14:20       ` Johannes Schindelin
2026-11-23 22:13   ` [PATCH v2 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-11-24 14:24     ` Junio C Hamano
2026-11-23 22:13   ` [PATCH v2 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-11-24 14:44     ` Phillip Wood
2026-11-24 18:04   ` [PATCH v2 0/5] Address overflows pointed out by Coverity Junio C Hamano
2026-11-25 20:55 ` Johannes Schindelin via GitGitGadget [this message]
2026-11-25 20:55   ` [PATCH v3 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-11-25 23:47     ` Junio C Hamano
2026-11-26 15:14       ` Phillip Wood
2026-11-25 20:55   ` [PATCH v3 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-11-25 23:57     ` Junio C Hamano
2026-11-25 20:55   ` [PATCH v3 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-11-25 22:08     ` Junio C Hamano
2026-11-26 14:08     ` Aniket Gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.2253.v3.git.1795640149.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=Simon.Richter@hogyros.de \
    --cc=git@vger.kernel.org \
    --cc=haraldnordgren@gmail.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=phillip.wood123@gmail.com \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox