pledge_openbsd.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. package unix
  5. import (
  6. "errors"
  7. "fmt"
  8. "strconv"
  9. )
  10. // Pledge implements the pledge syscall.
  11. //
  12. // This changes both the promises and execpromises; use PledgePromises or
  13. // PledgeExecpromises to only change the promises or execpromises
  14. // respectively.
  15. //
  16. // For more information see pledge(2).
  17. func Pledge(promises, execpromises string) error {
  18. if err := pledgeAvailable(); err != nil {
  19. return err
  20. }
  21. pptr, err := BytePtrFromString(promises)
  22. if err != nil {
  23. return err
  24. }
  25. exptr, err := BytePtrFromString(execpromises)
  26. if err != nil {
  27. return err
  28. }
  29. return pledge(pptr, exptr)
  30. }
  31. // PledgePromises implements the pledge syscall.
  32. //
  33. // This changes the promises and leaves the execpromises untouched.
  34. //
  35. // For more information see pledge(2).
  36. func PledgePromises(promises string) error {
  37. if err := pledgeAvailable(); err != nil {
  38. return err
  39. }
  40. pptr, err := BytePtrFromString(promises)
  41. if err != nil {
  42. return err
  43. }
  44. return pledge(pptr, nil)
  45. }
  46. // PledgeExecpromises implements the pledge syscall.
  47. //
  48. // This changes the execpromises and leaves the promises untouched.
  49. //
  50. // For more information see pledge(2).
  51. func PledgeExecpromises(execpromises string) error {
  52. if err := pledgeAvailable(); err != nil {
  53. return err
  54. }
  55. exptr, err := BytePtrFromString(execpromises)
  56. if err != nil {
  57. return err
  58. }
  59. return pledge(nil, exptr)
  60. }
  61. // majmin returns major and minor version number for an OpenBSD system.
  62. func majmin() (major int, minor int, err error) {
  63. var v Utsname
  64. err = Uname(&v)
  65. if err != nil {
  66. return
  67. }
  68. major, err = strconv.Atoi(string(v.Release[0]))
  69. if err != nil {
  70. err = errors.New("cannot parse major version number returned by uname")
  71. return
  72. }
  73. minor, err = strconv.Atoi(string(v.Release[2]))
  74. if err != nil {
  75. err = errors.New("cannot parse minor version number returned by uname")
  76. return
  77. }
  78. return
  79. }
  80. // pledgeAvailable checks for availability of the pledge(2) syscall
  81. // based on the running OpenBSD version.
  82. func pledgeAvailable() error {
  83. maj, min, err := majmin()
  84. if err != nil {
  85. return err
  86. }
  87. // Require OpenBSD 6.4 as a minimum.
  88. if maj < 6 || (maj == 6 && min <= 3) {
  89. return fmt.Errorf("cannot call Pledge on OpenBSD %d.%d", maj, min)
  90. }
  91. return nil
  92. }