From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202408 header.b=n3bsY+7Q; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id E89E05A004E for ; Wed, 18 Sep 2024 12:44:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202408; t=1726656248; bh=jW2s+Uhbc4pmiJ84AB0EgzwxYS9A5SgxHH0i9Vxr/Ho=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n3bsY+7QMaUioBNPA5ZKSdzOe3TASWYTA7pc5oTjdYJb/67V0XFR2vOPY8zBYJZ7J UerfKolrvBFtmIVT17lhV6xdhiVPyKO3izYzchewg0BPMDWo75uNPFA/um+kcuT0fA SNf/tyDMMalixWPi0p0aYMyTQEQKa9IbgN0yWF1+zRMB35ofq8q79frtdrIdhWMUUC LXy7B2Oa5oQjjRbpLT2D8CV0R3u8wai02QwO/kPUPoVv6WyGr0ZzGXTOdEwKdwIBVd eCVZrSEdfolv8nVib5n82dz79C8WpPD+rlMV2azNN1wqER8sSToZT94/smDCpPqpsY lZ47XEOLtQOnQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4X7wHr5Q27z4x6n; Wed, 18 Sep 2024 20:44:08 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v3 2/2] util: Remove possible quadratic behaviour from write_remainder() Date: Wed, 18 Sep 2024 20:44:06 +1000 Message-ID: <20240918104406.3050878-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240918104406.3050878-1-david@gibson.dropbear.id.au> References: <20240918104406.3050878-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: BCZJH5ASUZHOZSUSJMMAR6P3JZTAY3SS X-Message-ID-Hash: BCZJH5ASUZHOZSUSJMMAR6P3JZTAY3SS X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Markus Armbruster , David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: write_remainder() steps through the buffers in an IO vector writing out everything past a certain byte offset. However, on each iteration it rescans the buffer from the beginning to find out where we're up to. With an unfortunate set of write sizes this could lead to quadratic behaviour. In an even less likely set of circumstances (total vector length > maximum size_t) the 'skip' variable could overflow. This is one factor in a longstanding Coverity error we've seen (although I still can't figure out the remainder of its complaint). Rework write_remainder() to always work out our new position in the vector relative to our old/current position, rather than starting from the beginning each time. As a bonus this seems to fix the Coverity error. Signed-off-by: David Gibson --- util.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/util.c b/util.c index 7db7c2e7..87309c51 100644 --- a/util.c +++ b/util.c @@ -597,10 +597,15 @@ int write_all_buf(int fd, const void *buf, size_t len) size_t left = len; while (left) { - ssize_t rc = write(fd, p, left); + ssize_t rc; + + do + rc = write(fd, p, left); + while ((rc < 0) && errno == EINTR); if (rc < 0) return -1; + p += rc; left -= rc; } @@ -615,28 +620,30 @@ int write_all_buf(int fd, const void *buf, size_t len) * * Return: 0 on success, -1 on error (with errno set) * - * #syscalls write writev + * #syscalls writev */ int write_remainder(int fd, const struct iovec *iov, size_t iovcnt, size_t skip) { - size_t offset, i; + size_t i = 0, offset; - while ((i = iov_skip_bytes(iov, iovcnt, skip, &offset)) < iovcnt) { + while ((i += iov_skip_bytes(iov + i, iovcnt - i, skip, &offset)) < iovcnt) { ssize_t rc; if (offset) { - rc = write(fd, (char *)iov[i].iov_base + offset, - iov[i].iov_len - offset); - } else { - rc = writev(fd, &iov[i], iovcnt - i); + /* Write the remainder of the partially written buffer */ + if (write_all_buf(fd, (char *)iov[i].iov_base + offset, + iov[i].iov_len - offset) < 0) + return -1; + i++; } + /* Write as much of the remaining whole buffers as we can */ + rc = writev(fd, &iov[i], iovcnt - i); if (rc < 0) return -1; - skip += rc; + skip = rc; } - return 0; } -- 2.46.0