Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
cnci-agent: Read entropy from /dev/urandom directly
Browse files Browse the repository at this point in the history
This commit reverts to the previous Go 1.8 behaviour in which the TLS
session started by ssntp.Dial received its entropy by reading from
/dev/urandom directly.  The default behaviour has changed in Go 1.9
to retrieve random data by calling getrandom without the non block
flag.  Getrandom will therefore block if the urandom pool is not
initialised which can easily happen when a VM boots for the first time
like a CNCI.

Fixes: #1408

Signed-off-by: Mark Ryan <mark.d.ryan@intel.com>
  • Loading branch information
Mark Ryan authored and rbradford committed Aug 31, 2017
1 parent e1b10d0 commit 30ddabb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion networking/ciao-cnci-agent/client.go
Expand Up @@ -19,6 +19,7 @@ package main
import (
"encoding/json"
"flag"
"io"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -67,6 +68,8 @@ const (
interfacesDir = "/var/lib/ciao/network/interfaces"
)

var cnciRand io.Reader

type cmdWrapper struct {
cmd interface{}
}
Expand Down Expand Up @@ -349,7 +352,7 @@ func connectToServer(db *cnciDatabase, doneCh chan struct{}, statusCh chan struc
}()

cfg := &ssntp.Config{UUID: agentUUID, URI: serverURL, CAcert: serverCertPath, Cert: clientCertPath,
Log: ssntp.Log}
Log: ssntp.Log, Rand: cnciRand}
client := &agentClient{db: db, cmdCh: make(chan *cmdWrapper)}

dialCh := make(chan error)
Expand Down
54 changes: 54 additions & 0 deletions networking/ciao-cnci-agent/rand_linux.go
@@ -0,0 +1,54 @@
//
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
"bufio"
"io"
"os"
"sync"
"syscall"
)

type nbRandReader struct {
f io.Reader
lock sync.Mutex
}

func (r *nbRandReader) Read(p []byte) (int, error) {
r.lock.Lock()
n, err := r.f.Read(p)
r.lock.Unlock()
if re, ok := err.(*os.PathError); ok {
if re, ok := re.Err.(syscall.Errno); ok {
if re == syscall.EAGAIN {
err = nil
}
}
}

return n, err
}

func init() {
r, err := os.Open("/dev/urandom")
if err != nil {
return
}

cnciRand = &nbRandReader{f: bufio.NewReader(r)}
}

0 comments on commit 30ddabb

Please sign in to comment.