Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

197 wiersze
5.0 KiB

  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build freebsd
  5. // +build freebsd
  6. package unix
  7. import (
  8. "errors"
  9. "fmt"
  10. )
  11. // Go implementation of C mostly found in /usr/src/sys/kern/subr_capability.c
  12. const (
  13. // This is the version of CapRights this package understands. See C implementation for parallels.
  14. capRightsGoVersion = CAP_RIGHTS_VERSION_00
  15. capArSizeMin = CAP_RIGHTS_VERSION_00 + 2
  16. capArSizeMax = capRightsGoVersion + 2
  17. )
  18. var (
  19. bit2idx = []int{
  20. -1, 0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1,
  21. 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  22. }
  23. )
  24. func capidxbit(right uint64) int {
  25. return int((right >> 57) & 0x1f)
  26. }
  27. func rightToIndex(right uint64) (int, error) {
  28. idx := capidxbit(right)
  29. if idx < 0 || idx >= len(bit2idx) {
  30. return -2, fmt.Errorf("index for right 0x%x out of range", right)
  31. }
  32. return bit2idx[idx], nil
  33. }
  34. func caprver(right uint64) int {
  35. return int(right >> 62)
  36. }
  37. func capver(rights *CapRights) int {
  38. return caprver(rights.Rights[0])
  39. }
  40. func caparsize(rights *CapRights) int {
  41. return capver(rights) + 2
  42. }
  43. // CapRightsSet sets the permissions in setrights in rights.
  44. func CapRightsSet(rights *CapRights, setrights []uint64) error {
  45. // This is essentially a copy of cap_rights_vset()
  46. if capver(rights) != CAP_RIGHTS_VERSION_00 {
  47. return fmt.Errorf("bad rights version %d", capver(rights))
  48. }
  49. n := caparsize(rights)
  50. if n < capArSizeMin || n > capArSizeMax {
  51. return errors.New("bad rights size")
  52. }
  53. for _, right := range setrights {
  54. if caprver(right) != CAP_RIGHTS_VERSION_00 {
  55. return errors.New("bad right version")
  56. }
  57. i, err := rightToIndex(right)
  58. if err != nil {
  59. return err
  60. }
  61. if i >= n {
  62. return errors.New("index overflow")
  63. }
  64. if capidxbit(rights.Rights[i]) != capidxbit(right) {
  65. return errors.New("index mismatch")
  66. }
  67. rights.Rights[i] |= right
  68. if capidxbit(rights.Rights[i]) != capidxbit(right) {
  69. return errors.New("index mismatch (after assign)")
  70. }
  71. }
  72. return nil
  73. }
  74. // CapRightsClear clears the permissions in clearrights from rights.
  75. func CapRightsClear(rights *CapRights, clearrights []uint64) error {
  76. // This is essentially a copy of cap_rights_vclear()
  77. if capver(rights) != CAP_RIGHTS_VERSION_00 {
  78. return fmt.Errorf("bad rights version %d", capver(rights))
  79. }
  80. n := caparsize(rights)
  81. if n < capArSizeMin || n > capArSizeMax {
  82. return errors.New("bad rights size")
  83. }
  84. for _, right := range clearrights {
  85. if caprver(right) != CAP_RIGHTS_VERSION_00 {
  86. return errors.New("bad right version")
  87. }
  88. i, err := rightToIndex(right)
  89. if err != nil {
  90. return err
  91. }
  92. if i >= n {
  93. return errors.New("index overflow")
  94. }
  95. if capidxbit(rights.Rights[i]) != capidxbit(right) {
  96. return errors.New("index mismatch")
  97. }
  98. rights.Rights[i] &= ^(right & 0x01FFFFFFFFFFFFFF)
  99. if capidxbit(rights.Rights[i]) != capidxbit(right) {
  100. return errors.New("index mismatch (after assign)")
  101. }
  102. }
  103. return nil
  104. }
  105. // CapRightsIsSet checks whether all the permissions in setrights are present in rights.
  106. func CapRightsIsSet(rights *CapRights, setrights []uint64) (bool, error) {
  107. // This is essentially a copy of cap_rights_is_vset()
  108. if capver(rights) != CAP_RIGHTS_VERSION_00 {
  109. return false, fmt.Errorf("bad rights version %d", capver(rights))
  110. }
  111. n := caparsize(rights)
  112. if n < capArSizeMin || n > capArSizeMax {
  113. return false, errors.New("bad rights size")
  114. }
  115. for _, right := range setrights {
  116. if caprver(right) != CAP_RIGHTS_VERSION_00 {
  117. return false, errors.New("bad right version")
  118. }
  119. i, err := rightToIndex(right)
  120. if err != nil {
  121. return false, err
  122. }
  123. if i >= n {
  124. return false, errors.New("index overflow")
  125. }
  126. if capidxbit(rights.Rights[i]) != capidxbit(right) {
  127. return false, errors.New("index mismatch")
  128. }
  129. if (rights.Rights[i] & right) != right {
  130. return false, nil
  131. }
  132. }
  133. return true, nil
  134. }
  135. func capright(idx uint64, bit uint64) uint64 {
  136. return ((1 << (57 + idx)) | bit)
  137. }
  138. // CapRightsInit returns a pointer to an initialised CapRights structure filled with rights.
  139. // See man cap_rights_init(3) and rights(4).
  140. func CapRightsInit(rights []uint64) (*CapRights, error) {
  141. var r CapRights
  142. r.Rights[0] = (capRightsGoVersion << 62) | capright(0, 0)
  143. r.Rights[1] = capright(1, 0)
  144. err := CapRightsSet(&r, rights)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return &r, nil
  149. }
  150. // CapRightsLimit reduces the operations permitted on fd to at most those contained in rights.
  151. // The capability rights on fd can never be increased by CapRightsLimit.
  152. // See man cap_rights_limit(2) and rights(4).
  153. func CapRightsLimit(fd uintptr, rights *CapRights) error {
  154. return capRightsLimit(int(fd), rights)
  155. }
  156. // CapRightsGet returns a CapRights structure containing the operations permitted on fd.
  157. // See man cap_rights_get(3) and rights(4).
  158. func CapRightsGet(fd uintptr) (*CapRights, error) {
  159. r, err := CapRightsInit(nil)
  160. if err != nil {
  161. return nil, err
  162. }
  163. err = capRightsGet(capRightsGoVersion, int(fd), r)
  164. if err != nil {
  165. return nil, err
  166. }
  167. return r, nil
  168. }