public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: Theo de Raadt <deraadt@openbsd.org>
Cc: otto@cvs.openbsd.org, djm@cvs.openbsd.org,
	libc-alpha@sourceware.org, "Alejandro Colomar" <alx@kernel.org>,
	"Theo de Raadt" <deraadt@theos.com>,
	"Todd C . Miller" <Todd.Miller@sudo.ws>,
	"Jason A. Donenfeld" <Jason@zx2c4.com>,
	"Cristian Rodríguez" <crrodriguez@opensuse.org>,
	"Adhemerval Zanella" <adhemerval.zanella@linaro.org>,
	"Yann Droneaud" <ydroneaud@opteya.com>,
	"Joseph Myers" <joseph@codesourcery.com>,
	"Serge Hallyn" <serge@hallyn.com>,
	"Iker Pedrosa" <ipedrosa@redhat.com>
Subject: Re: [PATCH] Give a useful meaning to arc4random_uniform(0);
Date: Sat, 31 Dec 2022 17:03:07 +0100	[thread overview]
Message-ID: <941bc826-5626-1cbd-c667-03d248fa9137@gmail.com> (raw)
In-Reply-To: <23022d82-6573-42bd-ea19-d050ebe08ff5@gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 3502 bytes --]



On 12/31/22 16:59, Alejandro Colomar wrote:
> Hi Theo,
> 
> On 12/31/22 16:13, Alejandro Colomar wrote:
>> Hi Theo,
>>
>> On 12/31/22 15:56, Alejandro Colomar wrote:
>>>>
>>>> I do not like your proposal at all.  A function like arc4random_range()
>>>> is even more likely to be used wrong by passing backwards points
>>>> and you seem to have a lot of hubris to add a range check to it.
>>
>> I didn't understand the entire sentence, since I'm not a native English 
>> speaker.   Sorry for that.  About adding a range check, I'm not against it.  
>> But what to do in that case?  abort()?  I don't see anything significantly 
>> better?  In the Linux kernel, the used something BUILD_BUG, but I don't know 
>> those macros very much.
>>
>> I'm really open to discussion about what would the the best behavior when max 
>> < min.
> 
> Since there's no obvious thing to do when the bounds are reversed (some may want 
> to abort(); others may prefer to set errno?; others may just want to ignore the 
> possibility...
> 
> I tried to understand what it does with the obvious implementation:
> 
> 
>  > Alejandro Colomar <alx.manpages@gmail.com> wrote:
>  >> uint32_t
>  >> arc4random_range(uint32_t min, uint32_t max)
>  >> {
>  >>     return arc4random_uniform(max - min + 1) + min;
>  >> }
> 
> Well, let's substitute with some actual values:
> 
>      arc4random_range(7, 4);
> 
> This will result in:
> 
>      arc4random_uniform(4 - 7 + 1) + 7;
> 
> which evaluates to:
> 
>      arc4random_uniform(-2) + 7;
> 
> and is equivalent to:
> 
>      arc4random_uniform(UINT32_MAX - 1) + 7;
> 
> Let's first ignore the +7.  The arc4random_uniform(UINT32_MAX - 1) call can 
> generate 2^32 - 2 random numbers.  By offsetting to 7, the 2 value that are 
> excluded are 6 and 5.
> 
> So it seems that a reversed call to arc4random_range(min, max) has an 
> interesting property: it generates random numbers outside of the non-inclusive 
> range (min, max),

should have been (max, min).

> that is the compementary set of numbers that arc4random(max, min) would produce.
> 
> If properly documented, it's not a bad behavior.
> 
> So, I'd rather not check bounds, and instead document the behavior when the 
> ranges are reversed.  Then, anyone is free to add their own wrapper that adds 
> checks and performs their favourite action on reversed input, which might differ 
> from one programmer to another.
> 
> Cheers,
> 
> Alex
> 
>>
>> Cheers,
>>
>> Alex
>>
>>>
>>> Oh, I just checked hubris in the dictionary and it seems you did mention ego. 
>>> I'll try to rebate you with something useful.
>>>
>>> If you run `grep -rn 'arc4random_uniform('` in the OpenBSD tree, there will 
>>> be many cases where you'd really benefit from this.  I'll just pick a few:
>>>
>>>
>>> sys/net/pf_lb.c:224:
>>>              cut = arc4random_uniform(1 + high - low) + low;
>>> better as:
>>>              cut = arc4random_range(low, high);
>>>
>>>
>>> sys/kern/kern_fork.c:648:
>>>          pid = 2 + arc4random_uniform(PID_MAX - 1);
>>> better as:
>>>          pid = arc4random_range(2, PID_MAX);
>>>
>>>
>>> usr.bin/nc/netcat.c:1501:
>>>                  cp = arc4random_uniform(x + 1);
>>> better as:
>>>                  cp = arc4random_range(0, x);
>>>
>>>
> 

-- 
<http://www.alejandro-colomar.es/>

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

  reply	other threads:[~2022-12-31 16:03 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-31  2:36 Alejandro Colomar
2022-12-31  2:48 ` Alejandro Colomar
2022-12-31  2:57 ` Jason A. Donenfeld
2022-12-31 13:39   ` Alejandro Colomar
2022-12-31  8:50 ` Theo de Raadt
2022-12-31  8:51   ` Theo de Raadt
2022-12-31 14:56     ` Alejandro Colomar
2022-12-31 15:13       ` Alejandro Colomar
2022-12-31 15:17         ` Alejandro Colomar
2022-12-31 15:59         ` Alejandro Colomar
2022-12-31 16:03           ` Alejandro Colomar [this message]
2023-01-01  8:41           ` Theo de Raadt
2022-12-31 23:07   ` Damien Miller
2022-12-31 23:58     ` Alejandro Colomar
2023-01-01  7:48       ` Ariadne Conill
2023-01-01  9:21         ` Otto Moerbeek
2023-01-01 14:05         ` Alejandro Colomar
2023-01-01  8:34       ` Theo de Raadt
2023-01-01 21:37 ` Arsen Arsenović
2023-01-01 23:50   ` Alejandro Colomar
2023-01-02  0:02     ` Arsen Arsenović
2023-01-02 11:24       ` Alejandro Colomar

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=941bc826-5626-1cbd-c667-03d248fa9137@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=Jason@zx2c4.com \
    --cc=Todd.Miller@sudo.ws \
    --cc=adhemerval.zanella@linaro.org \
    --cc=alx@kernel.org \
    --cc=crrodriguez@opensuse.org \
    --cc=deraadt@openbsd.org \
    --cc=deraadt@theos.com \
    --cc=djm@cvs.openbsd.org \
    --cc=ipedrosa@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.org \
    --cc=otto@cvs.openbsd.org \
    --cc=serge@hallyn.com \
    --cc=ydroneaud@opteya.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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).