2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux
|
2016-06-02 00:29:06 +00:00
|
|
|
// +build linux
|
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-06-02 00:29:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
rootKeyFile = "/proc/sys/kernel/keys/root_maxkeys"
|
|
|
|
rootBytesFile = "/proc/sys/kernel/keys/root_maxbytes"
|
|
|
|
rootKeyLimit = 1000000
|
|
|
|
// it is standard configuration to allocate 25 bytes per key
|
|
|
|
rootKeyByteMultiplier = 25
|
|
|
|
)
|
|
|
|
|
2020-11-09 14:34:26 +00:00
|
|
|
// modifyRootKeyLimit checks to see if the root key limit is set to
|
2016-06-02 00:29:06 +00:00
|
|
|
// at least 1000000 and changes it to that limit along with the maxbytes
|
|
|
|
// allocated to the keys at a 25 to 1 multiplier.
|
2020-11-09 14:34:26 +00:00
|
|
|
func modifyRootKeyLimit() error {
|
2016-06-02 00:29:06 +00:00
|
|
|
value, err := readRootKeyLimit(rootKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if value < rootKeyLimit {
|
|
|
|
return setRootKeyLimit(rootKeyLimit)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setRootKeyLimit(limit int) error {
|
|
|
|
keys, err := os.OpenFile(rootKeyFile, os.O_WRONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer keys.Close()
|
2022-10-05 14:28:35 +00:00
|
|
|
_, err = keys.WriteString(strconv.Itoa(limit))
|
|
|
|
if err != nil {
|
2016-06-02 00:29:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
bytes, err := os.OpenFile(rootBytesFile, os.O_WRONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer bytes.Close()
|
2022-10-05 14:28:35 +00:00
|
|
|
_, err = bytes.WriteString(strconv.Itoa(limit * rootKeyByteMultiplier))
|
2016-06-02 00:29:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func readRootKeyLimit(path string) (int, error) {
|
2021-08-24 10:10:50 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2016-06-02 00:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return strconv.Atoi(strings.Trim(string(data), "\n"))
|
|
|
|
}
|