Skip to content

Commit cef05f1

Browse files
authored
Merge pull request #2461 from dmcgowan/seed_rand
Seed random on init
2 parents 8baeaff + 1c6929c commit cef05f1

209 files changed

Lines changed: 30252 additions & 8188 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

archive/compression/compression_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package compression
1818

1919
import (
2020
"bytes"
21-
"crypto/rand"
2221
"io/ioutil"
22+
"math/rand"
2323
"testing"
2424
)
2525

cmd/containerd/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ import (
2121
"os"
2222

2323
"github.com/containerd/containerd/cmd/containerd/command"
24+
"github.com/containerd/containerd/pkg/seed"
2425
)
2526

27+
func init() {
28+
seed.WithTimeAndRand()
29+
}
30+
2631
func main() {
2732
app := command.App()
2833
if err := app.Run(os.Args); err != nil {

cmd/ctr/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ import (
2121
"os"
2222

2323
"github.com/containerd/containerd/cmd/ctr/app"
24+
"github.com/containerd/containerd/pkg/seed"
2425
"github.com/urfave/cli"
2526
)
2627

2728
var pluginCmds = []cli.Command{}
2829

30+
func init() {
31+
seed.WithTimeAndRand()
32+
}
33+
2934
func main() {
3035
app := app.New()
3136
app.Commands = append(app.Commands, pluginCmds...)

content/local/store_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ import (
2020
"bufio"
2121
"bytes"
2222
"context"
23-
"crypto/rand"
2423
_ "crypto/sha256" // required for digest package
2524
"fmt"
2625
"io"
2726
"io/ioutil"
28-
mrand "math/rand"
27+
"math/rand"
2928
"os"
3029
"path/filepath"
3130
"reflect"
@@ -266,9 +265,9 @@ func generateBlobs(t checker, nblobs, maxsize int64) map[digest.Digest][]byte {
266265
blobs := map[digest.Digest][]byte{}
267266

268267
for i := int64(0); i < nblobs; i++ {
269-
p := make([]byte, mrand.Int63n(maxsize))
268+
p := make([]byte, rand.Int63n(maxsize))
270269

271-
if _, err := mrand.Read(p); err != nil {
270+
if _, err := rand.Read(p); err != nil {
272271
t.Fatal(err)
273272
}
274273

pkg/seed/seed.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package seed
18+
19+
import (
20+
"math/rand"
21+
"time"
22+
)
23+
24+
// WithTimeAndRand seeds the global math rand generator with nanoseconds
25+
// XOR'ed with a crypto component if available for uniqueness.
26+
func WithTimeAndRand() {
27+
var (
28+
b [4]byte
29+
u int64
30+
)
31+
32+
tryReadRandom(b[:])
33+
34+
// Set higher 32 bits, bottom 32 will be set with nanos
35+
u |= (int64(b[0]) << 56) | (int64(b[1]) << 48) | (int64(b[2]) << 40) | (int64(b[3]) << 32)
36+
37+
rand.Seed(u ^ time.Now().UnixNano())
38+
}

pkg/seed/seed_linux.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package seed
18+
19+
import "golang.org/x/sys/unix"
20+
21+
func tryReadRandom(p []byte) {
22+
// Ignore errors, just decreases uniqueness of seed
23+
unix.Getrandom(p, unix.GRND_NONBLOCK)
24+
}

pkg/seed/seed_other.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// +build !linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package seed
20+
21+
import (
22+
"crypto/rand"
23+
"io"
24+
)
25+
26+
func tryReadRandom(p []byte) {
27+
io.ReadFull(rand.Reader, p)
28+
}

vendor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ golang.org/x/net b3756b4b77d7b13260a0a2ec658753cf48922eac
2727
google.golang.org/grpc v1.12.0
2828
github.com/pkg/errors v0.8.0
2929
github.com/opencontainers/go-digest c9281466c8b2f606084ac71339773efd177436e7
30-
golang.org/x/sys 314a259e304ff91bd6985da2a7149bbf91237993 https://github.com/golang/sys
30+
golang.org/x/sys 1b2967e3c290b7c545b3db0deeda16e9be4f98a2 https://github.com/golang/sys
3131
github.com/opencontainers/image-spec v1.0.1
3232
golang.org/x/sync 450f422ab23cf9881c94e2db30cac0eb1b7cf80c
3333
github.com/BurntSushi/toml a368813c5e648fee92e5f6c30e3944ff9d5e8895

vendor/golang.org/x/sys/cpu/cpu.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/sys/cpu/cpu_arm.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)