News for hackers. Stuff that matters.
Re:Should random() be banned? by ThePadawan on Thursday February 11, 2021 @12:30PM (#26104456)
I'm not big on bans.

What I am big on is forcing developers to make deliberate choices. That's why I like React's policy of naming functionality "dangerouslySetInnerHTML" or "__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED".

If you add usages for these in a PR I'm reviewing without justification, it's not getting merged.

So why not make cryptographically unsafe random unsafeRandom() or shittyRandom() or iCopyPastedThisFromStackOverflowRandom()?

Re:Should random() be banned? by Macha on Thursday February 11, 2021 @03:34PM (#26106749)
This assumes writing crypto code is the most common use case for random numbers.

How often do you write crypto code?

vs

How often do people use random numbers + threshold for A/B tests? How often do game developers use random numbers for gameplay variety? How often is random used for animation variety? Do these use cases need the overhead of a cryptography RNG?

A former employer had the same issue as in the article - the security team implemented an automated vulnerability scanner in our github enterprise instance, and it spammed comments and marked a review as requiring changes if it edited any merge request which touched a file which used java.util.Random. It lasted a day before the security team was made turn it off as on our team (and many others), literally 0 uses of random numbers were those requiring a secure random.

Re:Should random() be banned? by FalconSensei on Thursday February 11, 2021 @04:27PM (#26107511)
Also: if someone is working on crypto and doesn't know that random() isn't true random, should they be working on that?
Re:Should random() be banned? by aaomidi on Thursday February 11, 2021 @07:29PM (#26109443)
There isn't a threshold where you become capable of working on crypto problems and code.

Making this safer makes mistakes far less likely and makes all of us safer.

Re:Should random() be banned? by bigiain on Thursday February 11, 2021 @10:18PM (#26110404)
I disagree.

Making one specific aspect of incompetent crypto coding “safer” doesn’t solve any of the problems.

We can argue about what threshold you need to reach to be considered “capable” of writing crypto code (that’s not just a learning exercise), but not knowing the deficiencies of random() is clearly well below that bar. Even knowing that is barely past the “don’t eat your crayons” level of skill.

If anything, a hook in your CI pipeline that automatically fires anybody who checks in code using random() in a cryptographic context is a better way to “make all of us safer”.

Re:Should random() be banned? by aaomidi on Thursday February 11, 2021 @11:55PM (#26110867)
You could be a great mathematician and not realize what kind of random you're picking. It's not as simple as you put it.

This discussion goes for other stuff too. Rust is getting popular because even amazing C and C++ devs make terrible mistakes that cause severe security and privacy problems down the line.

Re:Should random() be banned? by thekyle on Friday February 12, 2021 @10:16AM (#26114512)
Just because someone is a great mathematician does not mean they would be a great cryptographer. Mathematics and cryptography are about as related as computer science and cryptography (not that much).
Re:Should random() be banned? by lucideer on Friday February 12, 2021 @10:22AM (#26114592)
> should they be

This is the point of people suggesting things like bans or explicit warnings. They "shouldn't", but what's stopping them.

Re:Should random() be banned? by JauntyHatAngle on Thursday February 11, 2021 @06:50PM (#26109140)
The level of expertise far outstrips the demand in my experience, so the "should" often becomes moot.
Re:Should random() be banned? by adrr on Thursday February 11, 2021 @09:12PM (#26110060)
I’ve seen guids used as secrets on APIs/cookies. Those are easily guessable and also not random.
Re:Should random() be banned? by hombre_fatal on Thursday February 11, 2021 @09:27PM (#26110137)
Not sure what you have in mind when you say guids, but the word guid doesn't confer any info here. Most people are referring to uuids when they say guid, and a v4 uuid has 122 bits of randomness. A random 122 bit number can certainly be sufficient for most applications like api keys over the network.
Re:Should random() be banned? by matthewdgreen on Thursday February 11, 2021 @09:42PM (#26110198)
Some standard uuid libraries have weird not-very-random fallbacks when the RNG fails to initialize. It’s rare, but you can get strange stuff without realizing it. Better to use a real RNG and check error codes.
Re:Should random() be banned? by hombre_fatal on Friday February 12, 2021 @05:07AM (#26112085)
Yep, there's no rule of thumb that can spare you from reading the docs and knowing basic things about the code you're about to pull in.
Re:Should random() be banned? by adrr on Thursday February 11, 2021 @09:51PM (#26110252)
Guids is uuid and they can be interchanged but they all have same concept of creating unique identifiers that won’t ever collide.

It isn’t random is the problem and they are are used everywhere as a secret. People think they are random 122bit value.

Re:Should random() be banned? by Dylan16807 on Thursday February 11, 2021 @09:57PM (#26110286)
No, that's why you need to be specific. v4 is random, other versions aren't.
Re:Should random() be banned? by adrr on Thursday February 11, 2021 @09:59PM (#26110304)
You’re right I’d just looked at the rail implementation. I was basing it on my past knowledge of windows GUID implementation.
Re:Should random() be banned? by tgsovlerkhgsel on Thursday February 11, 2021 @09:28PM (#26110141)
I genuinely don't see the reason why non-cryptographic random number generators exist outside of niche applications.

The main arguments I've seen are speed and determinism.

However, a cryptographically secure, deterministic PRNG can be built from hash or block cipher primitives that have hardware acceleration, making them quite fast. Seed (and potentially periodically re-seed) it from a strong source of randomness, and you've got a fast and cryptographically secure non-deterministic PRNG.

I thought that "classic" PRNGs like the widespread Mersenne Twister even had issues that can cause practical problems when used in certain kinds of simulations (Monte Carlo, possibly) that rely on large amounts of random numbers, but I haven't been able to find a clear source for this.

I'm certainly defaulting to secure ones, and I'm surprised modern languages and libraries don't do this by default for their standard randomness functions.

Re:Should random() be banned? by bigiain on Thursday February 11, 2021 @10:09PM (#26110358)
> I genuinely don't see the reason why non-cryptographic random number generators exist outside of niche applications.

Because for well over 99.99% of developers, cryptography is a “niche application”.

I’ve never written crypto code I’ve deployed anywhere. If I need crypto, I use the highest level crypto library I can find that people I trust who _do_ know about crypto recommend.

The only time I ever recall non cryptographic random() functions to have surprised or affected me was way back when I discovered if you forgot to seed the random on an AppleII the games I wrote in Basic all started out with the same “random” choices.

Re:Should random() be banned? by tgsovlerkhgsel on Saturday February 13, 2021 @06:59PM (#26128285)
Cryptography is rare, but generating other data that needs to be unpredictable (e.g. session IDs, password reset tokens, random passwords getting generated, gift card codes, real-money gambling numbers) is quite common, I think.

And the default implementation of random() doesn't seem to be any faster than AES-CTR (which is the core of one form of secure PRNG).

Re:Should random() be banned? by hxtk on Thursday February 11, 2021 @11:13PM (#26110662)
It's true that most people who aren't doing sensitive work don't need cryptographically secure random number generators, but if you use something secure by default, it probably won't cause problems, and to the extent that it does you can catch them with some profiling. If you use insecure RNG by default, it probably won't cause problems but if it does you'll find them when a black hat hacker compromises your system in production.

Very few people set out to roll their own crypto. The issue in my experience is less about someone writing their own hand-optimized password hash function and more about people having overly-narrow views of what counts as security critical code.

Re:Should random() be banned? by Spooky23 on Thursday February 11, 2021 @10:02PM (#26110322)
The problem is the “banning” things as suggested in the article leads to bullshit like FIPS 140-2 mode.

So MD5 hashes are disabled in the system library for applications where it is not presenting any meaningful risk, for example. The other issue is that bans usually come with lists of things are not aren’t banned. So now you are stuck with waiting for some disinterested committee to support something that delivers a benefit.

Re:Should random() be banned? by devlopr on Thursday February 11, 2021 @10:26PM (#26110451)
Use the minimal amount of resources and scale up. If you need mostly random don't force cryptographic level random. It adds unnecessary processor cycles and reduces speed.
Re:Should random() be banned? by tgsovlerkhgsel on Saturday February 13, 2021 @07:01PM (#26128303)
I benchmarked it, and AES-CTR is faster than `random()` on a machine with AES-NI.

That's my main point: It does _not_ seem to be meaningfully more expensive to use cryptographic randomness. Yes, you could build a faster non-cryptographic PRNG, but that's not what is done by the default library.

Re:Should random() be banned? by fanf2 on Friday February 12, 2021 @08:23AM (#26113334)
The most common case for me is when fuzzing, when I want reproducible random numbers, so it must be possible to initialize the RNG from a known fixed seed so that I can re-run and debug a failure.
Re:Should random() be banned? by tgsovlerkhgsel on Saturday February 13, 2021 @07:03PM (#26128314)
Hence my suggestion to use a deterministic cryptographic generator: This way, you can either get "proper" randomness by seeding it with a random value, or predictable (and yet strong against anyone who doesn't know the seed) randomness by seeding with a fixed value.
Re:Should random() be banned? by jcelerier on Thursday February 11, 2021 @04:09PM (#26107222)
> It lasted a day before the security team was made turn it off as on our team (and many others), literally 0 uses of random numbers were those requiring a secure random.

can concur, currently approaching 400k SLOC of C++ in the repo. A few dozens different places crop up where random is needed (with a quick and dirty grepping). Literally 0% is for secure stuff. Most of it has to be as fast as possible (and very low quality, as it just needs to be random / noisy enough to look random for human perception)

Re:Should random() be banned? by kortex on Friday February 12, 2021 @08:54AM (#26113625)
This just kind of proves GP's point. Random APIs usually tell you what the RNG is, but not the why/how. Most people don't care if it's /dev/(u)random, Mersenne twister, PCG, LFSR, LCG, RDRAND, etc. They care about roughly 4 attributes:

- Is it good for crypto

- Is it fast

- Is it reproducible

- Is it portable

But fundamentally, it's about the use case and interface:

- I need secure random (strong, slow, secure)

- I need Monte Carlo (good enough, fast, reproducible)

- I need chaotic behavior for my game/stress test/back off protocol (usually can be barely random, fast, reproducible)

I think calling the last case InsecureRandom or RandomEnough is reasonable to convey "don't use me for secure purposes".

Re:Should random() be banned? by city41 on Thursday February 11, 2021 @06:29PM (#26108942)
Interestingly a major aspect of video game speed running is figuring out how the game generates random numbers then exploiting the knowledge. For example speedrunners avoid all random battles in an rpg with this tactic. I'm not arguing games need true random for the record.
Re:Should random() be banned? by mmazing on Thursday February 11, 2021 @06:56PM (#26109194)
That's all great and makes sense, but ...

What does that have to do with being verbose and letting developers know they are using an insecure method when it would apply to them?

If I'm writing code and using rng for gameplay variety, and then I notice that I have to use a function called "insecureRandom", at the very least I'm going to read up on an interesting aspect of computing and be a little more informed at the end of the day.

Re:Should random() be banned? by Macha on Thursday February 11, 2021 @07:41PM (#26109511)
Because suitability for use in secure algorithms is just one property of the random number generator. At what point do we then decide it needs to be uniformInsecureBoundedRandom?

Why don't we apply the same logic to string comparisons. Should we replace String.equals with String.shortcuttableEquals()? Since there's plenty of circumstances where that is inappropriate for crypto uses also.

What about other functions with important caveats? Should we have mailGmailMightReject()? fsyncCantFixHardware()? file.existsAtCurrentInstant()?

Re:Should random() be banned? by striking on Thursday February 11, 2021 @07:46PM (#26109549)
I actually don't hate `file.existsAtCurrentInstant`. `file.exists` is so misused.

Maybe functions suitable for crypto use should be in their own "crypto primitives" package.

Re:Should random() be banned? by TheCoelacanth on Thursday February 11, 2021 @08:02PM (#26109674)
I would prefer something like file.existedAtSomePointInTheRecentPast
Re:Should random() be banned? by Khaine on Thursday February 11, 2021 @09:56PM (#26110278)
The other thing is, true randomness doesn't seem random to humans. Which is why spotify and others had to modify shuffle. So true random might not be appropriate for the use case.

What is appropriate to use comes down to context.

Re:Should random() be banned? by Dylan16807 on Thursday February 11, 2021 @09:59PM (#26110303)
You don't modify shuffle to make it seem more random, you modify shuffle because a non-clumping algorithm is more pleasant.
Re:Should random() be banned? by Khaine on Friday February 12, 2021 @01:35AM (#26111252)
It's more pleasant because it is less random. I naive implementation might use true randomness which to a human doesn't appear random.

https://www.businessinsider.com.au/spotify-made-shuffle-feat...

Re:Should random() be banned? by Dylan16807 on Friday February 12, 2021 @03:14AM (#26111643)
Right, I'm not disagreeing with that.

But I bet that the main goal is not "Let's make this change so it sounds more random, and then people will like it because they think it's randomer." Rather, it's "Let's make this change so it sounds better, and also as a side effect people might think it's more random."

Re:Should random() be banned? by 1MoreThing on Thursday February 11, 2021 @04:16PM (#26107333)
The creation of a trueRandom function certainly seems to solve this problem more than taking away a useful tool for cases where pseudo-random is good enough.
Re:Should random() be banned? by extropy on Thursday February 11, 2021 @06:11PM (#26108766)
The problem is that true random is quite expensive without dedicated hardware and you can still easily bias true random if you are not careful.

IMO there is no use of true random unless you really know what you are doing.

Also there is true random - pure entropy bits and there is cryptographically secure pseudorandom, seeded with true random bits.

Re:Should random() be banned? by mmazing on Thursday February 11, 2021 @07:00PM (#26109220)
It's really not clear cut in either way on the surface.

On one side, you can argue that leaning people towards true random will cause unnecessary performance impact because the majority of cases don't need true random.

On another side, the impact of not using true random could cause a catastrophic result for a large number of people.

So which has more weight? I dunno.

In either case, it would be nice if developers knew the consequences of using either method, so this discussion is really more about education than anything else.

Re:Should random() be banned? by ChrisLomont on Thursday February 11, 2021 @08:33PM (#26109843)
>the impact of not using true random could cause a catastrophic result for a large number of people.

And the impact of using 1000x slower trueRandom could cause catastrophic results for an even larger number of people, since by far PRNGs are used where speed is more important than security.

And once you pick a "true random", how true is it? Will it be secure in 10 years? Will we then need a "truerTrueRandom" to mitigate that true random has failed to pass future mathematical or hardware tests? Will it return random numbers fast enough for future uses?

It's a rabbit hole. Let developers use the one they need, and since the vast majority does not need secure random, don't force it on them at significant cost.

If your crypto developer cannot know which to use you're going to have a lot more holes in your crypto than the RNG.

Re:Should random() be banned? by chromanoid on Thursday February 11, 2021 @07:27PM (#26109428)
Pseudo random is sometimes even necessary. It's really cool when you generate reproducible test data via seeds.
Re:Should random() be banned? by wbl on Friday February 12, 2021 @12:52AM (#26111097)
How many of those applications are ones where a single AES encryption is too slow?
Re:Should random() be banned? by ThePadawan on Friday February 12, 2021 @02:03AM (#26111339)
Did you mean to reply to my comment?

I don't disagree with anything you say, but it also doesn't refer to my comment.

Re:Should random() be banned? by Macha on Friday February 12, 2021 @07:04AM (#26112761)
Why not? I feel the same about naming a method to shame/discourage use as you suggested for outright banning. It shouldn't require a justification to use non-CSPRNG, because most use cases I run into for random are not crypto, because I don't write my own crypto.
Re:Should random() be banned? by zrm on Thursday February 11, 2021 @12:41PM (#26104622)
It's also a good idea to give safer things shorter names.

So make random() a CSPRNG (and an alias for SecureRandom() for people who want to be explicit) while InsecureFastRandom() is just what it says and has no other name. Then if you really need performance over unpredictability, it's there, but nobody is confused about what they're getting. And lazy people who don't like to type or pay close attention get the safe one.

Re:Should random() be banned? by cogman10 on Thursday February 11, 2021 @12:55PM (#26104830)
That's be my preference.

random() should be the most universally applicable random which includes making it as secure as possible. Non-universally applicable randoms should be named accordingly.

Re:Should random() be banned? by njharman on Thursday February 11, 2021 @01:07PM (#26104999)
Most simulations, games, everything that isn't generating cryptography does not need security in it's random.

For most domains secure random is a niche, not universally applicable.

Re:Should random() be banned? by SomeCallMeTim on Thursday February 11, 2021 @04:02PM (#26107116)
Actually, in my experience using the default random implementation in games:

* It's not fast enough. * It has patterns that can be seen if you are using it to, for instance, generate 2d noise.

So for games you'd typically use, say, the Mersenne Twister [1], which is faster (amortized) and is distributed evenly across 623 dimensions. [2]

It's not cryptographic, but it's far better for games. If you're not going to have a crypto default random, better to at least have a really good and really fast one.

[1] https://en.wikipedia.org/wiki/Mersenne_twister

[2] http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/ARTICLES/mt.p...

Re:Should random() be banned? by patrec on Thursday February 11, 2021 @06:10PM (#26108746)
Mersenne Twister is the MD5 of random number generators: it's neither secure nor fast (and unlikely md5 not space-efficient, or simple to implement either). You can have something that has better randomness, runs several times faster, uses less space and has a much more compact and simple implementation.

So given that Mersenne Twister sucks in pretty much every possible way other than having a catchy name, likely the sole source of its continued popularity, its probably not a good choice for replacing some "default" random number generator with something better.

Re:Should random() be banned? by MauranKilom on Thursday February 11, 2021 @07:45PM (#26109537)
> You can have something that has better randomness, runs several times faster, uses less space and has a much more compact and simple implementation.

Could you share at least one example of such a generator?

Most of these aspects are trivial to improve on, but having "better randomness" on top is quite the bar.

Re:Should random() be banned? by ChrisLomont on Thursday February 11, 2021 @08:35PM (#26109852)
The PCG class of PRNGs are very good, and have a zillion tunable versions to suit every need.

https://www.pcg-random.org/index.html

Re:Should random() be banned? by sobriquet9 on Thursday February 11, 2021 @10:09PM (#26110359)
Re:Should random() be banned? by patrec on Friday February 12, 2021 @01:37AM (#26111258)
https://en.wikipedia.org/wiki/Mersenne_Twister#Alternatives

https://nullprogram.com/blog/2017/09/21/

I don't think better randomness than MT is as high a bar as you make it out to be, both links above also briefly mention statistical shortcomings of MT.

Re:Should random() be banned? by Khaine on Thursday February 11, 2021 @09:50PM (#26110245)
Doesn't FreeBSD use Mersenne Twister as its CSRNG?
Re:Should random() be banned? by fanf2 on Friday February 12, 2021 @08:30AM (#26113392)
No, FreeBSD uses Fortuna https://en.wikipedia.org/wiki/Fortuna_(PRNG)

Mersenne Twister is not secure, and it isn’t a very good RNG even for insecure uses.

Re:Should random() be banned? by Khaine on Friday February 12, 2021 @04:34PM (#26118808)
Thanks! I don't know why I thought they used it.
Re:Should random() be banned? by bigiain on Thursday February 11, 2021 @10:38PM (#26110511)
Patterned random numbers become a feature in some games, even if an unintentional one.

I used to be able to play about a dozen Pac-Man levels knowing exactly where every ghost was going to go and every bonus that was going to appear. And I wasn’t a very good player.

Pac-Man would have been a less fun game for many people if it used crypto grade random.

Re:Should random() be banned? by watwut on Friday February 12, 2021 @02:58AM (#26111564)
Pac-Man ghosts did not moved all that much randomly. Each ghost had special follow up strategy. You likely learned their movement patterns without realizing it.
Re:Should random() be banned? by epanchin on Thursday February 11, 2021 @01:10PM (#26105044)
And one would hope secure devs know which random to use, whereas gaming devs have no reason to know.
Re:Should random() be banned? by josephg on Thursday February 11, 2021 @04:08PM (#26107200)
Look up “password generator” or similar terms on npm and take a look at how the packages you find generate random numbers. I did this ~5 years ago and it took until the second page of results before I found any packages that used a crypto-secure rng.
Re:Should random() be banned? by esrauch on Thursday February 11, 2021 @04:30PM (#26107545)
Even that seems unlikely to be problematic for anything short of literally constant seeds AND a generator becoming extremely popular.

The vast majority of people reuse low-entropy passwords, figuring out what password generator someone used would be a much higher bar than figuring out passwords, and just knowing the insecure generator wouldnt reduce the entropy by that much.

Actually, a password generator on GitHub that generates the password that is literally just seconds-since-1970 would still be a good generator for almost all use cases.

Re:Should random() be banned? by fanf2 on Friday February 12, 2021 @08:33AM (#26113427)
This is extremely bad advice. It is shamefully unethical to encourage people to harm their security in this way.
Re:Should random() be banned? by esrauch on Friday February 12, 2021 @06:15PM (#26119692)
Can you explain more? I genuinely don't see any plausible threat model that a user running an Math.random() based custom algorithm password generator would be susceptible to, but the same algorithm using SecureRandom one is not. Both cases are so drastically better than manually thinking up a password that it's not even close.

I think if there's any gap it would be wrong roll your own password generator at all and you only use ones authored by security experts: just using SecureRandom instead of Random isn't going to somehow magically guarantee you didn't mess up another way and write a low-entropy password generator.

Re:Should random() be banned? by ThePadawan on Friday February 12, 2021 @07:29AM (#26112929)
I was trying to generate random string identifiers to make some element ids unique and took the most popular library, which was accidentally crypto levels of secure:

https://blog.prat.ch/2020/11/10/randomstring-bundle-size

Re:Should random() be banned? by bigiain on Thursday February 11, 2021 @10:40PM (#26110526)
Looking for crypto secure code by searching on npm?

:facepalm:

Seriously.

Re:Should random() be banned? by tedunangst on Thursday February 11, 2021 @01:22PM (#26105204)
So a simple linear generator is fine for an online poker game?
Re:Should random() be banned? by btilly on Thursday February 11, 2021 @02:20PM (#26105888)
The claim that most uses of random() are not in places where cryptographic security is needed is not in conflict with any list of examples where it is needed.

Here are the last several times I saw random() used.

Seeding a neural network for a Coursera course. Not only do you need to call random() a bunch of times, but the ability to set a seed and get deterministic results makes grading of the results massively easier.

Creating simulation data used for integration tests on a piece of software.

Picking a few random numbers that I used in an explanation of an answer.

Of course the plural of anecdote is not data. However in my corner of the world it is very rare to need cryptographically secure anything. And when I do, I know better than to code it myself. But it is common to need a lot of cheap numbers in a hurry.

Re:Should random() be banned? by wolf550e on Monday February 15, 2021 @10:48AM (#26143800)
chacha8 (chacha20 but with fewer rounds) seeded with either a deterministic seed or from /dev/urandom (depending on what you need) is a perfectly fine PRNG, and should run at 4 GB/sec on a single core, which is plenty.

Only 7 rounds of chacha are cryptographically broken, so chacha20 has excessive security margin. Using 8 rounds of that everywhere you use random today (and not a CSPRING) should be a no-brainer, I guess.

On hardware with AESNI (like all x86 for the last 10 years), a reduced round AES-CTR should be very fast and also string enough for the cases when you don't need a CSPRING. Full AES-128 outputs 4.7GB/sec on my laptop, recent chips have better AES throughput.

Re:Should random() be banned? by Sohcahtoa82 on Thursday February 11, 2021 @03:33PM (#26106726)
Well of course an online poker game should be using a CSPRNG.

The parent comment said "Most simulations, games ...". Most. Not all. I think it's pretty obvious that Poker would not be included a statement of "Most".

The real litmus test is the question of "What happens if a malicious actor is able to predict the random numbers?"

Re:Should random() be banned? by tedunangst on Thursday February 11, 2021 @04:34PM (#26107605)
The claim was that everything that isn't generating cryptography does not need security.
Re:Should random() be banned? by yellowapple on Thursday February 11, 2021 @06:02PM (#26108647)
I personally parsed the claim as "most (simulations, games, everything)" rather than "(most simulations), games, everything", but I can see how that can go either way.
Re:Should random() be banned? by ChrisLomont on Thursday February 11, 2021 @08:39PM (#26109873)
If you're using it to give hands to people, then no. If you suitably hash or whiten the outcome, then yes.

If you're using it to generate Monte Carlo hand playouts to generate % outcomes to assist such a game, then you most certainly want an extremely fast generator. Only slightly more complex than a LCG is the PCG class (based on a PCG, and it's just as simple and fast in most cases as a LCG). And you'll need such Monte Carlo simulations to detect cheating, among other things.

So even for an online poker game you need to know what you're doing. Neither type of RNG will solve all the issues you need.

So yes, at the base a simple LCG would suffice if you know how to use it.

https://www.pcg-random.org/index.html

Re:Should random() be banned? by bigiain on Thursday February 11, 2021 @10:43PM (#26110538)
If your devs for your online casino aren’t smart and experienced enough to know when blanket advice and recommendations about random numbers don’t apply to them - enjoy going broke, it shouldn’t take long...
Re:Should random() be banned? by bigiain on Thursday February 11, 2021 @10:34PM (#26110492)
I completely disagree here.

I’d bet a lot of money that the number of times random() is used in a non cryptographic context is many orders of magnitude higher than the use of random numbers in crypto code. That make non crypto use “the most universally applicable” case.

I would also agree that “non universally applicable random” such as those used in crypto code should be named accordingly. Which they are. Secure_random() is the right choice for the vanishingly small number of developers writing crypto code. random() is the right name for pretty much everybody except those who need to know a lot of other crypto-specific other things as well. They can’t use random() the same way the cant use non-constant-time comparisons and algorithms that leak side channels via power monitoring or cache hitrates. Fixing random() and letting people who don’t know they should have been calling secure_random() write code in niches they don’t know enough to get everything else right, is without doubt going to end up with way more code that is not “as secure as possible” even if it happens to use secure random numbers.

Re:Should random() be banned? by cogman10 on Friday February 12, 2021 @01:59PM (#26117156)
Why put security second?

The thing is, if you need random numbers fast, then profiling will tell you "Opps, used random() when I should have used fast_random()". That's an easy change to make that'd show up in profiling if performance were an issue.

If random() never comes up in profiling, why care that you are getting the secure version?

The danger of missing "secure_random()" is that it creates a security vulnerability (Potentially leading to loss of money, information, etc). The danger of going the "fast_random()" route is that your application will run slower potentially leading to a dev needing to spend time to swap in "fast_random()" for "random()". That, to me, doesn't seem like a major problem or risk.

Programmers are notoriously bad at predicting when something will end up being a performance issue. So why preoptimize because we assume most people want/need speed?

Re:Should random() be banned? by bigiain on Thursday February 11, 2021 @10:24PM (#26110440)
Then we’ll end up with a csprng getting used in a tight loop iterating over every pixel in a raytracer...

“Lazy people who don’t want to type” are not the sort of people I want writing the code I might use or interact with that requires cryptographically secure random numbers...

Re:Should random() be banned? by zrm on Friday February 12, 2021 @12:08AM (#26110936)
> Then we’ll end up with a csprng getting used in a tight loop iterating over every pixel in a raytracer...

Which will then be conspicuous enough for the developer to notice and fix it.

> “Lazy people who don’t want to type” are not the sort of people I want writing the code I might use or interact with that requires cryptographically secure random numbers...

Bad news though.

Re:Should random() be banned? by cpeterso on Thursday February 11, 2021 @12:37PM (#26104564)
Firefox has a secret setting used in test automation called “turn_off_all_security_so_that_viruses_can_take_over_this_computer”.

https://searchfox.org/mozilla-central/rev/3ff133d19f87da2ba0...

Re:Should random() be banned? by makomk on Thursday February 11, 2021 @01:26PM (#26105264)
Yeah, and if I remember correctly one neat technique for exploiting security vulnerabilities in Firefox was to use them in order to set turn_off_all_security_so_that_viruses_can_take_over_this_computer to true, with obvious results.
Re:Should random() be banned? by wnevets on Thursday February 11, 2021 @12:55PM (#26104831)
hmm I wonder what would happen if I enable this setting...
Re:Should random() be banned? by throwaway744678 on Thursday February 11, 2021 @12:59PM (#26104878)
*** wnevets quit (Connection reset by peer)
Re:Should random() be banned? by nicoburns on Thursday February 11, 2021 @01:09PM (#26105028)
React's dangerouslySetInnerHTML is indeed a good way of handling this kind of thing. Rust's `unsafe` is another example of the same approach.
Re:Should random() be banned? by andoriyu on Thursday February 11, 2021 @06:10PM (#26108749)
uhm no.

Rust's "unsafe" is a pretty bad name and completely different reasoning behind using it. It doesn't mark something as "dangerously unsafe, don't use", to a consumer it indicates "exercise caution" and to a compiler it just allows 5 things:

    Dereference a raw pointer
    Call an unsafe function or method
    Access or modify a mutable static variable
    Implement an unsafe trait
    Access fields of unions
The point of "unsafe" in rust is to highlight which area requires more human attention... not to discourage its usage.

`dangerouslySetInnerHTML` is literally dangerous and allows XSS if used with outside input.

It also is faster than the other variant. The same is true for `random()`. Both can be used when you know what are you doing to gain some performance.

Meanwhile, `unsafe` rust by itself is not different from safe rust in terms of speed. You have no choice, but to use it places it supposed to be used.

Re:Should random() be banned? by nicoburns on Friday February 12, 2021 @07:34AM (#26112960)
The point of `dangerouslySetInnerHTML` is also to highlight an area which requires more human attention. It's perfectly safe if you have otherwise handled escaping or validation of the content. It's just that you want to pay careful attention to that code to ensure that you're doing it correctly, whereas in normal React code you don't have to think about escaping at all because the runtime handles it for you.

Likewise `unsafe` marks areas where you need to be really careful that you upholding the safety invariants yourself, whereas in normal Rust code you don't need to think about that at all.

Re:Should random() be banned? by hyperman1 on Friday February 12, 2021 @06:04AM (#26112380)
Isn't this just part of the world view of Rust?

The word 'safe' in Rust has a very specific, technical meaning. 'unsafe' is simply code that is not automatically 'safe' in the Rust sense.

A non-Rust developer sees safe/unsafe and gets worked up, but that just means that he should put his rust-colored glasses [*] on.

This is not unusual in ICT, known for its colorful language. A non-ICTer hears 'black hat' and thinks about how cool and stylish the hat is. An ICTer hears 'hacker' and thinks about how cool and stylish the hack is.

[*] Thanks, Raymond!

Re:Should random() be banned? by setr on Thursday February 11, 2021 @06:06PM (#26108691)
I don't think rust's unsafe says anything about cryptographic security, beyond pointer-safety implications.
Re:Should random() be banned? by nicoburns on Thursday February 11, 2021 @06:37PM (#26109003)
Neither does React's dangerouslySetInnerHTML. What both of these do is mark a potentially dangerous operation with an in-your-face warning message that you have to go out of your way to ignore. Which in practice is very useful, as often the biggest problem with security issues is not know what you don't know. It's impractical to review the entire codebase on a regular basis, and it can be hard to know which bits to focus on.
Re:Should random() be banned? by shadowgovt on Thursday February 11, 2021 @01:00PM (#26104902)
I agree, with the caveat that use of random for cryptography is actually a domain specific use case.

It's probably okay to leave the function as it is and just drill into people that if you're doing cryptography, you either need to know exactly what you're doing all the way down to the hardware or you need to leave it a task for somebody else more specialized than you. I, for one, never assume random() is cryptographically secure, but it might be because I grew up programming during the era where random was computed off of clock cycles since CPU startup because there wasn't much other cheap entropy to lay a hand on ("battery-backed onboard date clock?! Oh, look who has AKERS money!").

Re:Should random() be banned? by mannerheim on Thursday February 11, 2021 @12:52PM (#26104787)
Re:Should random() be banned? by iratewizard on Thursday February 11, 2021 @12:43PM (#26104661)
Beginner friendliness is something to remember, too. There are half a dozen words you could use to describe pseudoRandom(). Random() is easy for a first year or non-professional to remember.
Re:Should random() be banned? by dariusj18 on Thursday February 11, 2021 @12:46PM (#26104694)
Most of the time the people who write and name the functions don't know it's not secure or safe. So you would still need to ban random when the new name is implemented.
Re:Should random() be banned? by sp332 on Thursday February 11, 2021 @12:43PM (#26104657)
The "ban" can be evaded by telling semgrep to ignore it for one line. https://semgrep.dev/docs/ignoring-findings/ This doesn't really scale though - if someone bans it with a different tool, you'd have to tell each tool to ignore this line.
Re:Should random() be banned? by suzzer99 on Thursday February 11, 2021 @02:40PM (#26106116)
I have required parameter to push our app to production called: YES_I_HAVE_ALREADY_MERGED_THE_LIB_REPOS_AND_WAITED_FOR_THEM_TO_COMPLETE_BEFORE_MERGING_THE_APP_REPOS

Gets the point across and will still work when I'm long gone.

Re:Should random() be banned? by ddlsmurf on Thursday February 11, 2021 @03:56PM (#26107038)
Indeed this is a stupid debate. Knives help us in the kitchen but also sometimes stab people - should we ban knives ?
Re:Should random() be banned? by Spivak on Thursday February 11, 2021 @12:41PM (#26104636)
So you’re not big on bans but if you use dangerouslySetInnerHTML then it’s definitely not getting merged? Is that not a ban? Do you just not like when tooling enforces it?
Re:Should random() be banned? by ThePadawan on Thursday February 11, 2021 @12:47PM (#26104710)
No, as I said, it would raise a red flag. That flag can be lowered by justification, e. g. if you add types or constraints to only allow safe-enough parameters etc.
Re:Should random() be banned? by dariusj18 on Thursday February 11, 2021 @12:46PM (#26104707)
They said "without justification"