capabilities.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2021 Jeffrey H. Johnson <trnsz@pobox.com>
  2. // Copyright 2018 psgo 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. // Package capabilities provides a mapping from common kernel bit masks to
  16. // the alphanumerical representation of kernel capabilities.
  17. // See capabilities(7) for additional information.
  18. package capabilities
  19. var (
  20. // capabilities are a mapping from a numerical value to the textual
  21. // representation of a given capability. A map allows to easily check
  22. // if a given value is included or not.
  23. //
  24. // NOTE: this map must be maintained and kept in sync with the
  25. // ./include/uapi/linux/capability.h kernel header.
  26. capabilities = map[uint]string{
  27. 0: "CHOWN",
  28. 1: "DAC_OVERRIDE",
  29. 2: "DAC_READ_SEARCH",
  30. 3: "FOWNER",
  31. 4: "FSETID",
  32. 5: "KILL",
  33. 6: "SETGID",
  34. 7: "SETUID",
  35. 8: "SETPCAP",
  36. 9: "LINUX_IMMUTABLE",
  37. 10: "NET_BIND_SERVICE",
  38. 11: "NET_BROADCAST",
  39. 12: "NET_ADMIN",
  40. 13: "NET_RAW",
  41. 14: "IPC_LOCK",
  42. 15: "IPC_OWNER",
  43. 16: "SYS_MODULE",
  44. 17: "SYS_RAWIO",
  45. 18: "SYS_CHROOT",
  46. 19: "SYS_PTRACE",
  47. 20: "SYS_PACCT",
  48. 21: "SYS_ADMIN",
  49. 22: "SYS_BOOT",
  50. 23: "SYS_NICE",
  51. 24: "SYS_RESOURCE",
  52. 25: "SYS_TIME",
  53. 26: "SYS_TTY_CONFIG",
  54. 27: "MKNOD",
  55. 28: "LEASE",
  56. 29: "AUDIT_WRITE",
  57. 30: "AUDIT_CONTROL",
  58. 31: "SETFCAP",
  59. 32: "MAC_OVERRIDE",
  60. 33: "MAC_ADMIN",
  61. 34: "SYSLOG",
  62. 35: "WAKE_ALARM",
  63. 36: "BLOCK_SUSPEND",
  64. 37: "AUDIT_READ",
  65. }
  66. // FullCAPs represents a bitmask with a full capability set.
  67. FullCAPs = uint64(0x3FFFFFFFFF)
  68. )
  69. // TranslateMask iterates over mask and returns a slice of corresponding
  70. // capabilities. If a bit is out of range of known capabilities, it is set
  71. // as "unknown" to catch potential regressions when new capabilities are
  72. // added to the kernel.
  73. func TranslateMask(mask uint64) []string {
  74. caps := []string{}
  75. for i := uint(0); i < 64; i++ {
  76. if (mask>>i)&0x1 == 1 {
  77. c, known := capabilities[i]
  78. if !known {
  79. c = "unknown"
  80. }
  81. caps = append(caps, c)
  82. }
  83. }
  84. return caps
  85. }