openbsd_pledge.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2016 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. // +build openbsd
  5. // +build 386 amd64 arm
  6. package unix
  7. import (
  8. "errors"
  9. "fmt"
  10. "strconv"
  11. "syscall"
  12. "unsafe"
  13. )
  14. // Pledge implements the pledge syscall.
  15. //
  16. // The pledge syscall does not accept execpromises on OpenBSD releases
  17. // before 6.3.
  18. //
  19. // execpromises must be empty when Pledge is called on OpenBSD
  20. // releases predating 6.3, otherwise an error will be returned.
  21. //
  22. // For more information see pledge(2).
  23. func Pledge(promises, execpromises string) error {
  24. maj, min, err := majmin()
  25. if err != nil {
  26. return err
  27. }
  28. err = pledgeAvailable(maj, min, execpromises)
  29. if err != nil {
  30. return err
  31. }
  32. pptr, err := syscall.BytePtrFromString(promises)
  33. if err != nil {
  34. return err
  35. }
  36. // This variable will hold either a nil unsafe.Pointer or
  37. // an unsafe.Pointer to a string (execpromises).
  38. var expr unsafe.Pointer
  39. // If we're running on OpenBSD > 6.2, pass execpromises to the syscall.
  40. if maj > 6 || (maj == 6 && min > 2) {
  41. exptr, err := syscall.BytePtrFromString(execpromises)
  42. if err != nil {
  43. return err
  44. }
  45. expr = unsafe.Pointer(exptr)
  46. }
  47. _, _, e := syscall.Syscall(SYS_PLEDGE, uintptr(unsafe.Pointer(pptr)), uintptr(expr), 0)
  48. if e != 0 {
  49. return e
  50. }
  51. return nil
  52. }
  53. // PledgePromises implements the pledge syscall.
  54. //
  55. // This changes the promises and leaves the execpromises untouched.
  56. //
  57. // For more information see pledge(2).
  58. func PledgePromises(promises string) error {
  59. maj, min, err := majmin()
  60. if err != nil {
  61. return err
  62. }
  63. err = pledgeAvailable(maj, min, "")
  64. if err != nil {
  65. return err
  66. }
  67. // This variable holds the execpromises and is always nil.
  68. var expr unsafe.Pointer
  69. pptr, err := syscall.BytePtrFromString(promises)
  70. if err != nil {
  71. return err
  72. }
  73. _, _, e := syscall.Syscall(SYS_PLEDGE, uintptr(unsafe.Pointer(pptr)), uintptr(expr), 0)
  74. if e != 0 {
  75. return e
  76. }
  77. return nil
  78. }
  79. // PledgeExecpromises implements the pledge syscall.
  80. //
  81. // This changes the execpromises and leaves the promises untouched.
  82. //
  83. // For more information see pledge(2).
  84. func PledgeExecpromises(execpromises string) error {
  85. maj, min, err := majmin()
  86. if err != nil {
  87. return err
  88. }
  89. err = pledgeAvailable(maj, min, execpromises)
  90. if err != nil {
  91. return err
  92. }
  93. // This variable holds the promises and is always nil.
  94. var pptr unsafe.Pointer
  95. exptr, err := syscall.BytePtrFromString(execpromises)
  96. if err != nil {
  97. return err
  98. }
  99. _, _, e := syscall.Syscall(SYS_PLEDGE, uintptr(pptr), uintptr(unsafe.Pointer(exptr)), 0)
  100. if e != 0 {
  101. return e
  102. }
  103. return nil
  104. }
  105. // majmin returns major and minor version number for an OpenBSD system.
  106. func majmin() (major int, minor int, err error) {
  107. var v Utsname
  108. err = Uname(&v)
  109. if err != nil {
  110. return
  111. }
  112. major, err = strconv.Atoi(string(v.Release[0]))
  113. if err != nil {
  114. err = errors.New("cannot parse major version number returned by uname")
  115. return
  116. }
  117. minor, err = strconv.Atoi(string(v.Release[2]))
  118. if err != nil {
  119. err = errors.New("cannot parse minor version number returned by uname")
  120. return
  121. }
  122. return
  123. }
  124. // pledgeAvailable checks for availability of the pledge(2) syscall
  125. // based on the running OpenBSD version.
  126. func pledgeAvailable(maj, min int, execpromises string) error {
  127. // If OpenBSD <= 5.9, pledge is not available.
  128. if (maj == 5 && min != 9) || maj < 5 {
  129. return fmt.Errorf("pledge syscall is not available on OpenBSD %d.%d", maj, min)
  130. }
  131. // If OpenBSD <= 6.2 and execpromises is not empty,
  132. // return an error - execpromises is not available before 6.3
  133. if (maj < 6 || (maj == 6 && min <= 2)) && execpromises != "" {
  134. return fmt.Errorf("cannot use execpromises on OpenBSD %d.%d", maj, min)
  135. }
  136. return nil
  137. }