public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top, Matej Hrica <mhrica@redhat.com>
Subject: Re: [PATCH 3/4] util, lineread, tap: Overflow checks on long signed sums and subtractions
Date: Fri, 28 Jun 2024 17:15:09 +1000	[thread overview]
Message-ID: <Zn5i_WYXlIY_F8Mr@zatzit> (raw)
In-Reply-To: <20240627224645.6cad668d@elisabeth>

[-- Attachment #1: Type: text/plain, Size: 3549 bytes --]

On Thu, Jun 27, 2024 at 10:46:45PM +0200, Stefano Brivio wrote:
> On Thu, 27 Jun 2024 09:55:46 +0200
> Stefano Brivio <sbrivio@redhat.com> wrote:
> 
> > On Thu, 27 Jun 2024 11:13:14 +1000
> > David Gibson <david@gibson.dropbear.id.au> wrote:
> > 
> > > On Thu, Jun 27, 2024 at 01:45:35AM +0200, Stefano Brivio wrote:  
> > > > Potential sum and subtraction overflows were reported by Coverity in a
> > > > few places where we use size_t and ssize_t types.
> > > > 
> > > > Strictly speaking, those are not false positives even if I don't see a
> > > > way to trigger those: for instance, if we receive up to n bytes from a
> > > > socket up, the return value from recv() is already constrained and
> > > > can't overflow for the usage we make in tap_handler_passt().    
> > > 
> > > Actually, I think they are false positives.  In a bunch of cases the
> > > reasoning for that does rely on assuming the kernel will never return
> > > a value greater than the buffer size for read(), write() or similar.  
> > 
> > No, that was exactly my point: return values are constrained by the
> > kernel, but a static checker doesn't necessarily have to assume a kernel
> > that's properly functioning.
> > 
> > In general, static checkers do, especially if POSIX has a clear
> > definition of a system call, and for what matters to us, they should.
> > 
> > But here Coverity is ignoring that, and I'm not sure we should call it
> > a false positive. It's kind of arbitrary really. I think Coverity in
> > these cases just prefers to "blindly" apply CERT C INT32-C locally,
> > which is not necessarily a bad choice, because "false positives" are
> > not so much of a nuisance.
> > 
> > > So possibly just ASSERT()ing that would suffice.  
> > 
> > In some cases yes, but as we have built-ins in gcc and Clang that aim
> > at keeping the cost of the checks down by, quoting gcc documentation,
> > using "hardware instructions to implement these built-in functions where
> > possible", and they already implement the operation, open-coding our own
> > checks for assertions looks redundant and might result in slower code.
> 
> I tried, but in most cases, not even open-coding the whole thing, as an
> ASSERT() or an early return, say:
> 
> 			if ((rc > 0 && lr->count < LONG_MIN + rc) ||
> 			    (rc < 0 && lr->count > LONG_MAX + rc))
> 				return -1;

I'm not talking about open-coding a general overflow checking add/sub,
I'm talking about checking the much tighter range we actually have.
That should let Coverity reason that an overflow isn't possible, and
also check for other possible weirdness.

> or its ASSERT() equivalent is enough. That's because, without using
> built-in functions, we can't have (of course) "infinite precision"
> types. From:
> 
>   https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
> 
>   These built-in functions promote the first two operands into infinite
>   precision signed type and perform addition on those promoted operands
> 
> And it looks like Coverity wants to see operations executed in those
> types: size_t or unsigned long long is not enough.
> 
> In two cases, I could make Coverity happy with some rather bulky
> asserts, if I turn operations into size_t plus ssize_t, in size_t
> domain. But the result looks really bulky.

-- 
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2024-06-28  7:19 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-26 23:45 [PATCH 0/4] Small, assorted "hardening" fixes Stefano Brivio
2024-06-26 23:45 ` [PATCH 1/4] conf: Copy up to MAXDNSRCH - 1 bytes, not MAXDNSRCH Stefano Brivio
2024-06-27  0:45   ` David Gibson
2024-06-27  7:27     ` Stefano Brivio
2024-06-27 10:11       ` David Gibson
2024-06-26 23:45 ` [PATCH 2/4] tcp_splice: Check return value of setsockopt() for SO_RCVLOWAT Stefano Brivio
2024-06-27  0:46   ` David Gibson
2024-06-26 23:45 ` [PATCH 3/4] util, lineread, tap: Overflow checks on long signed sums and subtractions Stefano Brivio
2024-06-27  1:13   ` David Gibson
2024-06-27  7:55     ` Stefano Brivio
2024-06-27 20:46       ` Stefano Brivio
2024-06-28  7:15         ` David Gibson [this message]
2024-06-28  7:11       ` David Gibson
2024-06-28  7:55         ` Stefano Brivio
2024-06-28 18:30           ` Stefano Brivio
2024-07-08 13:01             ` Stefano Brivio
2024-06-26 23:45 ` [PATCH 4/4] tap: Drop frames from guest whose length is more than remaining buffer Stefano Brivio
2024-06-27  1:30   ` David Gibson
2024-06-27  8:21     ` Stefano Brivio
2024-06-28  7:19       ` David Gibson
2024-06-28  7:56         ` Stefano Brivio

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=Zn5i_WYXlIY_F8Mr@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=mhrica@redhat.com \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).