keys.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //go:build linux
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "os"
  5. "strconv"
  6. "strings"
  7. )
  8. const (
  9. rootKeyFile = "/proc/sys/kernel/keys/root_maxkeys"
  10. rootBytesFile = "/proc/sys/kernel/keys/root_maxbytes"
  11. rootKeyLimit = 1000000
  12. // it is standard configuration to allocate 25 bytes per key
  13. rootKeyByteMultiplier = 25
  14. )
  15. // modifyRootKeyLimit checks to see if the root key limit is set to
  16. // at least 1000000 and changes it to that limit along with the maxbytes
  17. // allocated to the keys at a 25 to 1 multiplier.
  18. func modifyRootKeyLimit() error {
  19. value, err := readRootKeyLimit(rootKeyFile)
  20. if err != nil {
  21. return err
  22. }
  23. if value < rootKeyLimit {
  24. return setRootKeyLimit(rootKeyLimit)
  25. }
  26. return nil
  27. }
  28. func setRootKeyLimit(limit int) error {
  29. keys, err := os.OpenFile(rootKeyFile, os.O_WRONLY, 0)
  30. if err != nil {
  31. return err
  32. }
  33. defer keys.Close()
  34. _, err = keys.WriteString(strconv.Itoa(limit))
  35. if err != nil {
  36. return err
  37. }
  38. bytes, err := os.OpenFile(rootBytesFile, os.O_WRONLY, 0)
  39. if err != nil {
  40. return err
  41. }
  42. defer bytes.Close()
  43. _, err = bytes.WriteString(strconv.Itoa(limit * rootKeyByteMultiplier))
  44. return err
  45. }
  46. func readRootKeyLimit(path string) (int, error) {
  47. data, err := os.ReadFile(path)
  48. if err != nil {
  49. return -1, err
  50. }
  51. return strconv.Atoi(strings.Trim(string(data), "\n"))
  52. }