Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fixed duplicated seeds. #3671

Merged
merged 3 commits into from
Feb 10, 2019

Conversation

moonming
Copy link
Contributor

What this PR does / why we need it:
ngx.time() + ngx.worker.pid() maybe get duplicated seeds. get from /dev/urandom first.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #

Special notes for your reviewer:

ngx.time() + ngx.worker.pid() maybe get duplicated seeds. get from /dev/urandom first.
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jan 17, 2019
@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Jan 17, 2019
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jan 18, 2019
return
end

seed = get_seed_from_urandom()
if not seed then
seed = ngx.now() * 1000 + pid
Copy link
Member

Choose a reason for hiding this comment

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

Why not ngx.time()? That way you'd not have to multiply by 1000.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ngx.time() returns seconds, and ngx.now() returns seconds and milliseconds, so ngx.time() + pid maybe get the same seed, however ngx.now() * 1000 + pid is less chance to the same seed.

return seed
end

math.randomseed = function()
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather keep the function signature identical to the original randomseed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, I will keep the function signature identical to the original randomseed.

ngx.log(ngx.WARN,
string.format("ignoring math.randomseed(%d) since PRNG is already seeded for worker %d", seed, pid))
Copy link
Member

Choose a reason for hiding this comment

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

Logging user provided seed makes error message clearer, please keep this as is unless you have a reason to remove it.

local seed
local frandom = io.open("/dev/urandom", "rb")
if frandom then
local str = frandom:read(4)
Copy link
Member

@ElvinEfendi ElvinEfendi Jan 18, 2019

Choose a reason for hiding this comment

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

Please make sure you handle log errors returned by :read and .open calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added error info, and the :read will return nil without err if cannot read data.

return
end

seed = get_seed_from_urandom()
if not seed then
Copy link
Member

Choose a reason for hiding this comment

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

Let's log a warning here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added log for that.


if seeds[pid] then
local function get_seed_from_urandom()
Copy link
Member

@ElvinEfendi ElvinEfendi Jan 18, 2019

Choose a reason for hiding this comment

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

Can you explain the rationale behind this function implementation? What's the entropy going to be with this implementation? Why are you reading specifically 4 bytes from /dev/urandom? What does https://github.com/kubernetes/ingress-nginx/pull/3671/files#diff-d2d9d7b4e27b247474196438a3dfbdc3R13 give us?

Why not use rand_bytes from OpenSSL?

Copy link
Contributor Author

@moonming moonming Jan 21, 2019

Choose a reason for hiding this comment

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

/dev/urandom provides an interface to the kernel's random number generator, 4 bytes is good enough for us.

Why not use rand_bytes from OpenSSL?

because the openssl's random is not fork safety(fixed from openssl 1.1.1): https://wiki.openssl.org/index.php/Random_fork-safety.

Copy link
Member

Choose a reason for hiding this comment

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

4 bytes is good enough for us.

How do you decide that? Why not i.e 8 bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

4 bytes is good enough for us.

How do you decide that? Why not i.e 8 bytes?

random in this repo is only for choose balancer, not for encryption, so we don't need to read more bytes. If for encryption, we need read 8 bytes maybe.

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 21, 2019
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 21, 2019
seed = 0
for i = 1, 4 do
seed = 256 * seed + str:byte(i)
end
Copy link
Member

Choose a reason for hiding this comment

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

If I understand this correctly you treat the 4 bytes as a base 256 number (where str:byte(4) is the least significant) and then convert it into decimal. Why? Why not simply concatenate the 4 bytes and then use tonumber cast it to a decimal? (I'm not suggesting/asking any change, just want to know what's going on - I don't have much experience with random numbers and crypto in general).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

both solutions are ok, however, if we used the tonumber, we need one more function call, and one more table to contact the 4 bytes.
BTW, there is 4 bytes, so we don't need to consider number overflow.

Copy link
Member

Choose a reason for hiding this comment

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

This is not a hot code path, I'd be okay to tradeoff performance for readability but reading this again I think treating the bytes we get from /dev/urandom as base 256 number when converting to decimal makes more sense since they are bytes.

@ElvinEfendi
Copy link
Member

@moonming I'd really like to get this PR merged. Can you answer my two questions above?

@moonming
Copy link
Contributor Author

@moonming I'd really like to get this PR merged. Can you answer my two questions above?

sorry for late reply, long holiday for Chinese new year.

@ElvinEfendi
Copy link
Member

/lgtm
/approve

Thanks for the patch and answering my questions @moonming ! And Happy Chinese New Year :)

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 10, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ElvinEfendi, moonming

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 10, 2019
@k8s-ci-robot k8s-ci-robot merged commit d9845c7 into kubernetes:master Feb 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants