forked from VaclavSynacek/cl-openbsd-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl-openbsd-security.lisp
260 lines (194 loc) · 8.37 KB
/
cl-openbsd-security.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#|
| Copyright (c) 2019 Vaclav Synacek
|
| Permission to use, copy, modify, and distribute this software for any
| purpose with or without fee is hereby granted, provided that the above
| copyright notice and this permission notice appear in all copies.
|
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
| WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
| MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
| ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
| WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
| ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
| OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|#
(ql:quickload :cffi)
(defpackage :cl-openbsd-security
(:nicknames :openbsd :obsd)
(:use :common-lisp :cffi)
(:export
:pledge
:pledge-raw
:unveil
:unveil-raw))
(in-package :cl-openbsd-security)
(defparameter pledges (list 'stdio 'rpath 'wpath 'cpath 'dpath 'tmppath 'inet 'mcast 'fattr 'chown 'flock 'unix 'dns 'getpw 'sendfd 'recvfd 'tape 'tty 'proc 'exec 'prot-exec 'settime 'ps 'vminfo 'id 'pf 'audio 'video 'bpf 'unveil))
(define-condition cl-openbsd-security-condition (warning)
((message :initarg :message
:reader message))
(:documentation "General condition for the :cl-openbsd-security package"))
(define-condition runtime-security-condition (cl-openbsd-security-condition)
((message :initarg :message
:reader message))
(:report (lambda (condition stream)
(format stream "Some OpenBSD security mitigation failed.
You probably can continue, but some of the security guarantees will not be honored.
The datail is: ~a~%" (message condition))))
(:documentation "Runtime condition for the :cl-openbsd-security package. Used
when runtime call to pledge(2) or unveil(2) fails."))
(define-condition security-compilation-error (cl-openbsd-security-condition)
((message :initarg :message
:reader message))
(:report (lambda (condition stream)
(format stream "Cannot compile cl-openbsd-security macro.
The datail is: ~a~%" (message condition))))
(:documentation "Compile-time condition for the :cl-openbsd-security package. Used
when pledge(2) or unveil(2) is poorly defined or has typos."))
(defun interpret-result (c-result-code &optional (call "last security"))
(if (zerop c-result-code)
t
(let*
((errno (mem-ref (foreign-funcall "__errno" (:pointer :int)) :int))
(error-message (foreign-funcall "strerror" :int errno :string))
(detail-reason (format nil "~a call failed - ~a" call error-message)))
(cerror
(format nil "Continue while ignoring that ~a" detail-reason)
'runtime-security-condition
:message detail-reason))))
(defcfun (pledge-raw "pledge") :int
"Restrict system operations to promises."
(promises :string)
(execpromises :string))
(defun symbol-to-string (symbol)
(substitute #\_ #\-
(format nil "~(~A~)"
(string symbol))))
(defun symbols-to-promise-long-string (list-of-symbols)
(let
((formated-strings
(mapcar
#'symbol-to-string
list-of-symbols)))
(unless (subsetp
formated-strings
(mapcar #'symbol-to-string pledges)
:test #'equal)
(cerror "Ignore, compile to invalid string, expect runtime errors later."
'security-compilation-error
:message
(format nil
"tryng to pladge invalid promisses: ~{~a~^, ~}"
(set-difference formated-strings pledges :test #'equal))))
(format nil "~{~a~^ ~}" formated-strings)))
(defmacro pledge (&rest all-promisses)
(if (and
(every #'symbolp all-promisses)
(notany #'null all-promisses))
`(pledge ,all-promisses nil)
(let
((promisses (first all-promisses))
(exec-promisses (second all-promisses)))
`(interpret-result
(pledge-raw
,(if promisses
(symbols-to-promise-long-string promisses)
'(null-pointer))
,(if exec-promisses
(symbols-to-promise-long-string exec-promisses)
'(null-pointer)))
"pledge"))))
(defcfun (unveil-raw "unveil") :int
"unveil parts of a restricted filesystem view"
(path :string)
(permissions :string))
(defun permissions-to-valid-string (permissions)
(let
((string-permissions (ctypecase permissions
(string permissions)
(symbol (format nil
"~(~a~)"
(string permissions))))))
(unless (subsetp
(coerce string-permissions 'list)
(list #\r #\w #\x #\c)
:test #'equal)
(cerror "Ignore, compile to invalid string, expect runtime errors later."
'security-compilation-error
:message
(format nil
"tryng to unveil with invalid rigths ~{~a~^, ~}"
(set-difference
(coerce string-permissions 'list)
(list #\r #\w #\x #\c)
:test #'equal))))
string-permissions))
(defmacro unveil (path permissions)
(let
((string-permissions (permissions-to-valid-string permissions))
(orig-path (gensym))
(string-path (gensym)))
`(let*
((,orig-path ,path)
(,string-path (ctypecase ,orig-path
(string ,orig-path)
(pathname (namestring ,orig-path)))))
(interpret-result
(unveil-raw ,string-path ,string-permissions)
"unveil"))))
#|
(uiop:run-program "sbcl
--eval \"(asdf:make :cl-openbsd-security)\""
:ignore-error-status t
:output :string
:force-shell nil
:input (make-string-input-stream
"
(asdf:make :cl-openbsd-security)
;;(obsd:pledge unix)
(obsd:pledge stdio rpath wpath cpath dpath tmppath inet mcast fattr chown flock unix dns getpw sendfd recvfd tape tty proc exec prot_exec settime ps vminfo id pf audio video bpf unveil)
(progn
(princ \"we got past pledge\")
;;(sb-ext:exit :code 5)
(quit))
"))
(prin1 '(print "bla"))
(foreign-symbol-pointer "pledge")
(pledge stdio rpath wpath cpath dpath tmppath inet mcast fattr chown flock unix dns getpw sendfd recvfd tape tty proc exec prot_exec settime ps vminfo id pf audio video bpf unveil)
;; CLISP OK
(pledge stdio rpath wpath cpath dpath tmppath inet mcast flock unix getpw sendfd recvfd tty proc exec prot_exec ps vminfo id unveil)
;; CLISP OK
(pledge stdio inet mcast flock unix getpw sendfd recvfd tty proc exec prot_exec ps vminfo id unveil)
;; CLISP OK
(pledge stdio mcast flock unix getpw sendfd recvfd tty proc exec prot_exec ps vminfo id)
;; CLISP OK
(pledge stdio mcast flock unix getpw tty proc exec prot_exec ps vminfo id)
;; CLISP OK
(pledge stdio mcast flock unix getpw tty proc exec prot_exec)
;; CLISP OK
(pledge stdio mcast flock unix proc tty exec prot_exec)
;; CLISP OK - MINIMAL CLISP PROMISSES
(pledge stdio tty)
(pledge stdio rpath wpath cpath dpath tmppath inet prot_exec)
(pledge blbost)
(macroexpand
'(pledge (stdio rpath) (stdio wpath)))
(pledge stdio rpath exec exec-prot)
(pledge blbost)
(pledge (stdio rpath exec) (stdio))
(macroexpand '(pledge stdio wpath))
(macroexpand '(pledge (stdio) ()))
(macroexpand '(pledge (stdio) nil))
(pledge (stdio) nil)
(macroexpand '(pledge (stdio wpath exec) (stdio rpath)))
(pledge-raw "stdio stdio" (null-pointer))
(pledge-raw "stdio rpath exec prot_exec" (null-pointer))
(pledge-raw "stdio rpath wpath cpath dpath exec proc sendfd recvfd unix fattr" "unix fattr sendfd recvfd stdio rpath wpath cpath dpath exec proc")
(unveil-raw "/home" "rw")
(macroexpand '(unveil (concatenate 'string "/tmp/" "file") rw))
(unveil (concatenate 'string "/tmp/" "file") rw)
(uiop:directory-files "./")
(uiop:directory-files "/tmp/")
(uiop:subdirectories "/home")
(type-of (first (uiop:directory-files "/home/jajis/projects")))
|#