@@ -14,43 +14,28 @@ import (
14
14
15
15
// Pledge implements the pledge syscall.
16
16
//
17
- // The pledge syscall does not accept execpromises on OpenBSD releases
18
- // before 6.3.
19
- //
20
- // execpromises must be empty when Pledge is called on OpenBSD
21
- // releases predating 6.3, otherwise an error will be returned.
17
+ // This changes both the promises and execpromises; use PledgePromises or
18
+ // PledgeExecpromises to only change the promises or execpromises
19
+ // respectively.
22
20
//
23
21
// For more information see pledge(2).
24
22
func Pledge (promises , execpromises string ) error {
25
- maj , min , err := majmin ()
26
- if err != nil {
23
+ if err := pledgeAvailable (); err != nil {
27
24
return err
28
25
}
29
26
30
- err = pledgeAvailable ( maj , min , execpromises )
27
+ pptr , err := syscall . BytePtrFromString ( promises )
31
28
if err != nil {
32
29
return err
33
30
}
34
31
35
- pptr , err := syscall .BytePtrFromString (promises )
32
+ exptr , err := syscall .BytePtrFromString (execpromises )
36
33
if err != nil {
37
34
return err
38
35
}
39
36
40
- // This variable will hold either a nil unsafe.Pointer or
41
- // an unsafe.Pointer to a string (execpromises).
42
- var expr unsafe.Pointer
43
-
44
- // If we're running on OpenBSD > 6.2, pass execpromises to the syscall.
45
- if maj > 6 || (maj == 6 && min > 2 ) {
46
- exptr , err := syscall .BytePtrFromString (execpromises )
47
- if err != nil {
48
- return err
49
- }
50
- expr = unsafe .Pointer (exptr )
51
- }
52
-
53
- _ , _ , e := syscall .Syscall (SYS_PLEDGE , uintptr (unsafe .Pointer (pptr )), uintptr (expr ), 0 )
37
+ _ , _ , e := syscall .Syscall (SYS_PLEDGE , uintptr (unsafe .Pointer (pptr )),
38
+ uintptr (unsafe .Pointer (exptr )), 0 )
54
39
if e != 0 {
55
40
return e
56
41
}
@@ -64,13 +49,7 @@ func Pledge(promises, execpromises string) error {
64
49
//
65
50
// For more information see pledge(2).
66
51
func PledgePromises (promises string ) error {
67
- maj , min , err := majmin ()
68
- if err != nil {
69
- return err
70
- }
71
-
72
- err = pledgeAvailable (maj , min , "" )
73
- if err != nil {
52
+ if err := pledgeAvailable (); err != nil {
74
53
return err
75
54
}
76
55
@@ -82,7 +61,8 @@ func PledgePromises(promises string) error {
82
61
return err
83
62
}
84
63
85
- _ , _ , e := syscall .Syscall (SYS_PLEDGE , uintptr (unsafe .Pointer (pptr )), uintptr (expr ), 0 )
64
+ _ , _ , e := syscall .Syscall (SYS_PLEDGE , uintptr (unsafe .Pointer (pptr )),
65
+ uintptr (expr ), 0 )
86
66
if e != 0 {
87
67
return e
88
68
}
@@ -96,13 +76,7 @@ func PledgePromises(promises string) error {
96
76
//
97
77
// For more information see pledge(2).
98
78
func PledgeExecpromises (execpromises string ) error {
99
- maj , min , err := majmin ()
100
- if err != nil {
101
- return err
102
- }
103
-
104
- err = pledgeAvailable (maj , min , execpromises )
105
- if err != nil {
79
+ if err := pledgeAvailable (); err != nil {
106
80
return err
107
81
}
108
82
@@ -114,7 +88,8 @@ func PledgeExecpromises(execpromises string) error {
114
88
return err
115
89
}
116
90
117
- _ , _ , e := syscall .Syscall (SYS_PLEDGE , uintptr (pptr ), uintptr (unsafe .Pointer (exptr )), 0 )
91
+ _ , _ , e := syscall .Syscall (SYS_PLEDGE , uintptr (pptr ),
92
+ uintptr (unsafe .Pointer (exptr )), 0 )
118
93
if e != 0 {
119
94
return e
120
95
}
@@ -147,16 +122,15 @@ func majmin() (major int, minor int, err error) {
147
122
148
123
// pledgeAvailable checks for availability of the pledge(2) syscall
149
124
// based on the running OpenBSD version.
150
- func pledgeAvailable (maj , min int , execpromises string ) error {
151
- // If OpenBSD <= 5.9, pledge is not available.
152
- if ( maj == 5 && min != 9 ) || maj < 5 {
153
- return fmt . Errorf ( "pledge syscall is not available on OpenBSD %d.%d" , maj , min )
125
+ func pledgeAvailable () error {
126
+ maj , min , err := majmin ()
127
+ if err != nil {
128
+ return err
154
129
}
155
130
156
- // If OpenBSD <= 6.2 and execpromises is not empty,
157
- // return an error - execpromises is not available before 6.3
158
- if (maj < 6 || (maj == 6 && min <= 2 )) && execpromises != "" {
159
- return fmt .Errorf ("cannot use execpromises on OpenBSD %d.%d" , maj , min )
131
+ // Require OpenBSD 6.4 as a minimum.
132
+ if maj < 6 || (maj == 6 && min <= 3 ) {
133
+ return fmt .Errorf ("cannot call Pledge on OpenBSD %d.%d" , maj , min )
160
134
}
161
135
162
136
return nil
0 commit comments