Entropy cannot be saved

Remove non cryptographic randomness.

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
(cherry picked from commit 2df693e533)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Justin Cormack 2019-06-07 11:21:18 +01:00 committed by Sebastiaan van Stijn
parent a8d1b4a1ab
commit 510e79ebe9
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
17 changed files with 34 additions and 78 deletions

View file

@ -136,7 +136,7 @@ func (container *Container) CopyImagePathContent(v volume.Volume, destination st
return err return err
} }
id := stringid.GenerateNonCryptoID() id := stringid.GenerateRandomID()
path, err := v.Mount(id) path, err := v.Mount(id)
if err != nil { if err != nil {
return err return err

View file

@ -41,7 +41,7 @@ func (daemon *Daemon) createContainerOSSpecificSettings(container *container.Con
} }
for spec := range config.Volumes { for spec := range config.Volumes {
name := stringid.GenerateNonCryptoID() name := stringid.GenerateRandomID()
destination := filepath.Clean(spec) destination := filepath.Clean(spec)
// Skip volumes for which we already have something mounted on that // Skip volumes for which we already have something mounted on that

View file

@ -38,7 +38,7 @@ func (daemon *Daemon) createContainerOSSpecificSettings(container *container.Con
// If the mountpoint doesn't have a name, generate one. // If the mountpoint doesn't have a name, generate one.
if len(mp.Name) == 0 { if len(mp.Name) == 0 {
mp.Name = stringid.GenerateNonCryptoID() mp.Name = stringid.GenerateRandomID()
} }
// Skip volumes for which we already have something mounted on that // Skip volumes for which we already have something mounted on that

View file

@ -39,7 +39,7 @@ type Config struct {
// NewConfig initializes the a new exec configuration // NewConfig initializes the a new exec configuration
func NewConfig() *Config { func NewConfig() *Config {
return &Config{ return &Config{
ID: stringid.GenerateNonCryptoID(), ID: stringid.GenerateRandomID(),
StreamConfig: stream.NewConfig(), StreamConfig: stream.NewConfig(),
Started: make(chan struct{}), Started: make(chan struct{}),
} }

View file

@ -731,7 +731,7 @@ func BenchmarkConcurrentAccess(b *testing.B) {
// create a bunch of ids // create a bunch of ids
var ids []string var ids []string
for i := 0; i < numConcurrent; i++ { for i := 0; i < numConcurrent; i++ {
ids = append(ids, stringid.GenerateNonCryptoID()) ids = append(ids, stringid.GenerateRandomID())
} }
if err := d.Create(ids[0], "", nil); err != nil { if err := d.Create(ids[0], "", nil); err != nil {

View file

@ -81,7 +81,7 @@ func makePluginCreator(name string, l logPlugin, scopePath func(s string) string
return nil, err return nil, err
} }
id := stringid.GenerateNonCryptoID() id := stringid.GenerateRandomID()
a := &pluginAdapter{ a := &pluginAdapter{
driverName: name, driverName: name,
id: id, id: id,

View file

@ -38,7 +38,7 @@ func (daemon *Daemon) registerName(container *container.Container) error {
func (daemon *Daemon) generateIDAndName(name string) (string, string, error) { func (daemon *Daemon) generateIDAndName(name string) (string, string, error) {
var ( var (
err error err error
id = stringid.GenerateNonCryptoID() id = stringid.GenerateRandomID()
) )
if name == "" { if name == "" {

View file

@ -558,7 +558,7 @@ func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverCapabilities(c *chec
} }
func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverOutOfBandDelete(c *check.C) { func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverOutOfBandDelete(c *check.C) {
driverName := stringid.GenerateNonCryptoID() driverName := stringid.GenerateRandomID()
p := newVolumePlugin(c, driverName) p := newVolumePlugin(c, driverName)
defer p.Close() defer p.Close()

View file

@ -61,7 +61,7 @@ func TestRenameStoppedContainer(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal("/"+oldName, inspect.Name)) assert.Check(t, is.Equal("/"+oldName, inspect.Name))
newName := "new_name" + stringid.GenerateNonCryptoID() newName := "new_name" + stringid.GenerateRandomID()
err = client.ContainerRename(ctx, oldName, newName) err = client.ContainerRename(ctx, oldName, newName)
assert.NilError(t, err) assert.NilError(t, err)
@ -79,7 +79,7 @@ func TestRenameRunningContainerAndReuse(t *testing.T) {
cID := container.Run(t, ctx, client, container.WithName(oldName)) cID := container.Run(t, ctx, client, container.WithName(oldName))
poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond)) poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
newName := "new_name" + stringid.GenerateNonCryptoID() newName := "new_name" + stringid.GenerateRandomID()
err := client.ContainerRename(ctx, oldName, newName) err := client.ContainerRename(ctx, oldName, newName)
assert.NilError(t, err) assert.NilError(t, err)

View file

@ -2,17 +2,12 @@
package stringid // import "github.com/docker/docker/pkg/stringid" package stringid // import "github.com/docker/docker/pkg/stringid"
import ( import (
cryptorand "crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io"
"math"
"math/big"
"math/rand"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
) )
const shortLen = 12 const shortLen = 12
@ -41,10 +36,11 @@ func TruncateID(id string) string {
return id return id
} }
func generateID(r io.Reader) string { // GenerateRandomID returns a unique id.
func GenerateRandomID() string {
b := make([]byte, 32) b := make([]byte, 32)
for { for {
if _, err := io.ReadFull(r, b); err != nil { if _, err := rand.Read(b); err != nil {
panic(err) // This shouldn't happen panic(err) // This shouldn't happen
} }
id := hex.EncodeToString(b) id := hex.EncodeToString(b)
@ -58,18 +54,6 @@ func generateID(r io.Reader) string {
} }
} }
// GenerateRandomID returns a unique id.
func GenerateRandomID() string {
return generateID(cryptorand.Reader)
}
// GenerateNonCryptoID generates unique id without using cryptographically
// secure sources of random.
// It helps you to save entropy.
func GenerateNonCryptoID() string {
return generateID(readerFunc(rand.Read))
}
// ValidateID checks whether an ID string is a valid image ID. // ValidateID checks whether an ID string is a valid image ID.
func ValidateID(id string) error { func ValidateID(id string) error {
if ok := validHex.MatchString(id); !ok { if ok := validHex.MatchString(id); !ok {
@ -77,23 +61,3 @@ func ValidateID(id string) error {
} }
return nil return nil
} }
func init() {
// safely set the seed globally so we generate random ids. Tries to use a
// crypto seed before falling back to time.
var seed int64
if cryptoseed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64)); err != nil {
// This should not happen, but worst-case fallback to time-based seed.
seed = time.Now().UnixNano()
} else {
seed = cryptoseed.Int64()
}
rand.Seed(seed)
}
type readerFunc func(p []byte) (int, error)
func (fn readerFunc) Read(p []byte) (int, error) {
return fn(p)
}

View file

@ -13,14 +13,6 @@ func TestGenerateRandomID(t *testing.T) {
} }
} }
func TestGenerateNonCryptoID(t *testing.T) {
id := GenerateNonCryptoID()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
func TestShortenId(t *testing.T) { func TestShortenId(t *testing.T) {
id := "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2" id := "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2"
truncID := TruncateID(id) truncID := TruncateID(id)

View file

@ -158,7 +158,7 @@ func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult strin
func BenchmarkTruncIndexAdd100(b *testing.B) { func BenchmarkTruncIndexAdd100(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -174,7 +174,7 @@ func BenchmarkTruncIndexAdd100(b *testing.B) {
func BenchmarkTruncIndexAdd250(b *testing.B) { func BenchmarkTruncIndexAdd250(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 250; i++ { for i := 0; i < 250; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -190,7 +190,7 @@ func BenchmarkTruncIndexAdd250(b *testing.B) {
func BenchmarkTruncIndexAdd500(b *testing.B) { func BenchmarkTruncIndexAdd500(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -207,7 +207,7 @@ func BenchmarkTruncIndexGet100(b *testing.B) {
var testSet []string var testSet []string
var testKeys []string var testKeys []string
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
index := NewTruncIndex([]string{}) index := NewTruncIndex([]string{})
for _, id := range testSet { for _, id := range testSet {
@ -231,7 +231,7 @@ func BenchmarkTruncIndexGet250(b *testing.B) {
var testSet []string var testSet []string
var testKeys []string var testKeys []string
for i := 0; i < 250; i++ { for i := 0; i < 250; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
index := NewTruncIndex([]string{}) index := NewTruncIndex([]string{})
for _, id := range testSet { for _, id := range testSet {
@ -255,7 +255,7 @@ func BenchmarkTruncIndexGet500(b *testing.B) {
var testSet []string var testSet []string
var testKeys []string var testKeys []string
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
index := NewTruncIndex([]string{}) index := NewTruncIndex([]string{})
for _, id := range testSet { for _, id := range testSet {
@ -278,7 +278,7 @@ func BenchmarkTruncIndexGet500(b *testing.B) {
func BenchmarkTruncIndexDelete100(b *testing.B) { func BenchmarkTruncIndexDelete100(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -301,7 +301,7 @@ func BenchmarkTruncIndexDelete100(b *testing.B) {
func BenchmarkTruncIndexDelete250(b *testing.B) { func BenchmarkTruncIndexDelete250(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 250; i++ { for i := 0; i < 250; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -324,7 +324,7 @@ func BenchmarkTruncIndexDelete250(b *testing.B) {
func BenchmarkTruncIndexDelete500(b *testing.B) { func BenchmarkTruncIndexDelete500(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -347,7 +347,7 @@ func BenchmarkTruncIndexDelete500(b *testing.B) {
func BenchmarkTruncIndexNew100(b *testing.B) { func BenchmarkTruncIndexNew100(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -358,7 +358,7 @@ func BenchmarkTruncIndexNew100(b *testing.B) {
func BenchmarkTruncIndexNew250(b *testing.B) { func BenchmarkTruncIndexNew250(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 250; i++ { for i := 0; i < 250; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -369,7 +369,7 @@ func BenchmarkTruncIndexNew250(b *testing.B) {
func BenchmarkTruncIndexNew500(b *testing.B) { func BenchmarkTruncIndexNew500(b *testing.B) {
var testSet []string var testSet []string
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
testSet = append(testSet, stringid.GenerateNonCryptoID()) testSet = append(testSet, stringid.GenerateRandomID())
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -381,7 +381,7 @@ func BenchmarkTruncIndexAddGet100(b *testing.B) {
var testSet []string var testSet []string
var testKeys []string var testKeys []string
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
id := stringid.GenerateNonCryptoID() id := stringid.GenerateRandomID()
testSet = append(testSet, id) testSet = append(testSet, id)
l := rand.Intn(12) + 12 l := rand.Intn(12) + 12
testKeys = append(testKeys, id[:l]) testKeys = append(testKeys, id[:l])
@ -406,7 +406,7 @@ func BenchmarkTruncIndexAddGet250(b *testing.B) {
var testSet []string var testSet []string
var testKeys []string var testKeys []string
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
id := stringid.GenerateNonCryptoID() id := stringid.GenerateRandomID()
testSet = append(testSet, id) testSet = append(testSet, id)
l := rand.Intn(12) + 12 l := rand.Intn(12) + 12
testKeys = append(testKeys, id[:l]) testKeys = append(testKeys, id[:l])
@ -431,7 +431,7 @@ func BenchmarkTruncIndexAddGet500(b *testing.B) {
var testSet []string var testSet []string
var testKeys []string var testKeys []string
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
id := stringid.GenerateNonCryptoID() id := stringid.GenerateRandomID()
testSet = append(testSet, id) testSet = append(testSet, id)
l := rand.Intn(12) + 12 l := rand.Intn(12) + 12
testKeys = append(testKeys, id[:l]) testKeys = append(testKeys, id[:l])

View file

@ -70,7 +70,7 @@ func TestManagerWithPluginMounts(t *testing.T) {
} }
func newTestPlugin(t *testing.T, name, cap, root string) *v2.Plugin { func newTestPlugin(t *testing.T, name, cap, root string) *v2.Plugin {
id := stringid.GenerateNonCryptoID() id := stringid.GenerateRandomID()
rootfs := filepath.Join(root, id) rootfs := filepath.Join(root, id)
if err := os.MkdirAll(rootfs, 0755); err != nil { if err := os.MkdirAll(rootfs, 0755); err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -298,7 +298,7 @@ func (p *linuxParser) parseMountSpec(cfg mount.Mount, validateBindSourceExists b
switch cfg.Type { switch cfg.Type {
case mount.TypeVolume: case mount.TypeVolume:
if cfg.Source == "" { if cfg.Source == "" {
mp.Name = stringid.GenerateNonCryptoID() mp.Name = stringid.GenerateRandomID()
} else { } else {
mp.Name = cfg.Source mp.Name = cfg.Source
} }

View file

@ -125,7 +125,7 @@ func (m *MountPoint) Setup(mountLabel string, rootIDs idtools.Identity, checkFun
if m.Volume != nil { if m.Volume != nil {
id := m.ID id := m.ID
if id == "" { if id == "" {
id = stringid.GenerateNonCryptoID() id = stringid.GenerateRandomID()
} }
path, err := m.Volume.Mount(id) path, err := m.Volume.Mount(id)
if err != nil { if err != nil {

View file

@ -385,7 +385,7 @@ func (p *windowsParser) parseMountSpec(cfg mount.Mount, destRegex string, conver
switch cfg.Type { switch cfg.Type {
case mount.TypeVolume: case mount.TypeVolume:
if cfg.Source == "" { if cfg.Source == "" {
mp.Name = stringid.GenerateNonCryptoID() mp.Name = stringid.GenerateRandomID()
} else { } else {
mp.Name = cfg.Source mp.Name = cfg.Source
} }

View file

@ -63,7 +63,7 @@ func (s *VolumesService) GetDriverList() []string {
// When whatever is going to reference this volume is removed the caller should defeference the volume by calling `Release`. // When whatever is going to reference this volume is removed the caller should defeference the volume by calling `Release`.
func (s *VolumesService) Create(ctx context.Context, name, driverName string, opts ...opts.CreateOption) (*types.Volume, error) { func (s *VolumesService) Create(ctx context.Context, name, driverName string, opts ...opts.CreateOption) (*types.Volume, error) {
if name == "" { if name == "" {
name = stringid.GenerateNonCryptoID() name = stringid.GenerateRandomID()
} }
v, err := s.vs.Create(ctx, name, driverName, opts...) v, err := s.vs.Create(ctx, name, driverName, opts...)
if err != nil { if err != nil {