Skip to content

Fix minor random bias#65404

Merged
bartonjs merged 4 commits intodotnet:mainfrom
vcsjones:rand-bias
Feb 28, 2022
Merged

Fix minor random bias#65404
bartonjs merged 4 commits intodotnet:mainfrom
vcsjones:rand-bias

Conversation

@vcsjones
Copy link
Copy Markdown
Member

This corrects two uses of RandomNumberGenerator that resulted in slight bias in their results. After some internal discussion it was determined that the bias did not weaken the randomness to the point where this needed to be treated as a vulnerability.

This also adds some tests just to make sure the random generation is not completely broken.

This corrects two uses of RandomNumberGenerator that resulted in slight
bias in their results.

WIP

Add DirectoryServices tests

Comments
@ghost
Copy link
Copy Markdown

ghost commented Feb 15, 2022

Tagging subscribers to this area: @dotnet/area-system-directoryservices, @jay98014
See info in area-owners.md if you want to be subscribed.

Issue Details

This corrects two uses of RandomNumberGenerator that resulted in slight bias in their results. After some internal discussion it was determined that the bias did not weaken the randomness to the point where this needed to be treated as a vulnerability.

This also adds some tests just to make sure the random generation is not completely broken.

Author: vcsjones
Assignees: -
Labels:

area-System.DirectoryServices

Milestone: -


Debug.Assert(result < toExclusive);
return result;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no way to achieve the desired goals here without implementing a custom non-biased GetInt32 method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub For .NET Standard 2.0, not to my knowledge. Based on an off-GitHub conversation with @bartonjs I think he was on the same page that we'd have to lift and adapt GetInt32.

Copy link
Copy Markdown
Member

@bartonjs bartonjs Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we could trim the alphabet, but that speeds up brute force (especially if we trim it down to 1 😄).

The alternative to dropping this copy here is something like

byte[] random = new byte[SomeNumber];
char[] output = new char[Whatever];
int randomPos = random.Length;

for (int writePos = 0; writePos < output.Length; writePos++)
{
    int randomVal;

    while (true)
    {
        if (randomPos >= random.Length)
        {
            RandomNumberGenerator.GetBytes(random);
            randomPos = 0;
        }

        randomVal = random[randomPos];
        randomPos++;
 
        Debug.Assert(alphabet.Length > 64 && alphabet.Length <= 128);
        randomVal &= 127;

        if (randomVal < alphabet.Length)
        {
            break;
        }
    }

    output[writePos] = alphabet[randomVal];
}

Which amounts to the same thing.

@joperezr
Copy link
Copy Markdown
Member

@vcsjones are we still waiting on a signoff here?

@vcsjones
Copy link
Copy Markdown
Member Author

@joperezr Not sure what's up with the enterprise-linux leg but it seems unrelated. I think this is okay to merge.

@bartonjs
Copy link
Copy Markdown
Member

I haven't hit merge because I don't know what's in the enterprise-linux leg and I've been hoping that its DNS failures would get worked out. If you (@joperezr) are fine ignoring the leg, then I am, too. (I'll admit, I have difficulty understanding what sort of test could be failing due to this change)

@stephentoub
Copy link
Copy Markdown
Member

I haven't hit merge because I don't know what's in the enterprise-linux leg and I've been hoping that its DNS failures would get worked out.

I believe it's fine to ignore. @wfurt can confirm.

@wfurt
Copy link
Copy Markdown
Member

wfurt commented Feb 28, 2022

correct. The enterprise-linux failure is tracked by https://github.com/dotnet/core-eng/issues/15594 and it is environmental regression caused by infrastructure changes.

@bartonjs bartonjs merged commit 3d6781b into dotnet:main Feb 28, 2022
@vcsjones vcsjones deleted the rand-bias branch February 28, 2022 22:31
@ghost ghost locked as resolved and limited conversation to collaborators Mar 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants