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
|
;;; emacs-tests.el --- unit tests for emacs.c -*- lexical-binding: t; -*-
;; Copyright (C) 2020-2021 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Unit tests for src/emacs.c.
;;; Code:
(require 'cl-lib)
(require 'ert)
(require 'ert-x) ; ert-with-temp-file
(require 'rx)
(require 'subr-x)
(defconst emacs-tests--lib-src
(substitute-in-file-name "$EMACS_TEST_DIRECTORY/../lib-src/")
"Location of the lib-src directory.")
(ert-deftest emacs-tests/seccomp/absent-file ()
(skip-unless (string-match-p (rx bow "SECCOMP" eow)
system-configuration-features))
(let ((emacs
(expand-file-name invocation-name invocation-directory))
(process-environment nil))
(skip-unless (file-executable-p emacs))
(should-not (file-exists-p "/does-not-exist.bpf"))
(should-not
(eql (call-process emacs nil nil nil
"--quick" "--batch"
"--seccomp=/does-not-exist.bpf")
0))))
(ert-deftest emacs-tests/seccomp/empty-file ()
(skip-unless (string-match-p (rx bow "SECCOMP" eow)
system-configuration-features))
(let ((emacs
(expand-file-name invocation-name invocation-directory))
(process-environment nil))
(skip-unless (file-executable-p emacs))
(ert-with-temp-file filter
:prefix "seccomp-invalid-" :suffix ".bpf"
;; The --seccomp option is processed early, without filename
;; handlers. Therefore remote or quoted filenames wouldn't
;; work.
(should-not (file-remote-p filter))
(cl-callf file-name-unquote filter)
;; According to the Seccomp man page, a filter must have at
;; least one element, so Emacs should reject an empty file.
(should-not
(eql (call-process emacs nil nil nil
"--quick" "--batch"
(concat "--seccomp=" filter))
0)))))
(ert-deftest emacs-tests/seccomp/file-too-large ()
(skip-unless (string-match-p (rx bow "SECCOMP" eow)
system-configuration-features))
(let ((emacs
(expand-file-name invocation-name invocation-directory))
(process-environment nil)
;; This value should be correct on all supported systems.
(ushort-max #xFFFF)
;; Either 8 or 16, but 16 should be large enough in all cases.
(filter-size 16))
(skip-unless (file-executable-p emacs))
(ert-with-temp-file filter
:prefix "seccomp-too-large-" :suffix ".bpf"
:text (make-string (* (1+ ushort-max) filter-size) ?a)
;; The --seccomp option is processed early, without filename
;; handlers. Therefore remote or quoted filenames wouldn't
;; work.
(should-not (file-remote-p filter))
(cl-callf file-name-unquote filter)
;; The filter count must fit into an `unsigned short'. A bigger
;; file should be rejected.
(should-not
(eql (call-process emacs nil nil nil
"--quick" "--batch"
(concat "--seccomp=" filter))
0)))))
(ert-deftest emacs-tests/seccomp/invalid-file-size ()
(skip-unless (string-match-p (rx bow "SECCOMP" eow)
system-configuration-features))
(let ((emacs
(expand-file-name invocation-name invocation-directory))
(process-environment nil))
(skip-unless (file-executable-p emacs))
(ert-with-temp-file filter
:prefix "seccomp-invalid-" :suffix ".bpf" :text "123456"
;; The --seccomp option is processed early, without filename
;; handlers. Therefore remote or quoted filenames wouldn't
;; work.
(should-not (file-remote-p filter))
(cl-callf file-name-unquote filter)
;; The Seccomp filter file must have a file size that's a
;; multiple of the size of struct sock_filter, which is 8 or 16,
;; but never 6.
(should-not
(eql (call-process emacs nil nil nil
"--quick" "--batch"
(concat "--seccomp=" filter))
0)))))
(ert-deftest emacs-tests/seccomp/allows-stdout ()
(skip-unless (string-match-p (rx bow "SECCOMP" eow)
system-configuration-features))
(let ((emacs
(expand-file-name invocation-name invocation-directory))
(filter (expand-file-name "seccomp-filter.bpf"
emacs-tests--lib-src))
(process-environment nil))
(skip-unless (file-executable-p emacs))
(skip-unless (file-readable-p filter))
;; The --seccomp option is processed early, without filename
;; handlers. Therefore remote or quoted filenames wouldn't work.
(should-not (file-remote-p filter))
(cl-callf file-name-unquote filter)
(with-temp-buffer
(let ((start-time (current-time))
(status (call-process
emacs nil t nil
"--quick" "--batch"
(concat "--seccomp=" filter)
(format "--eval=%S" '(message "Hi"))))
(end-time (current-time)))
(ert-info ((emacs-tests--seccomp-debug start-time end-time))
(should (eql status 0)))
(should (equal (string-trim (buffer-string)) "Hi"))))))
(ert-deftest emacs-tests/seccomp/forbids-subprocess ()
(skip-unless (string-match-p (rx bow "SECCOMP" eow)
system-configuration-features))
(let ((emacs
(expand-file-name invocation-name invocation-directory))
(filter (expand-file-name "seccomp-filter.bpf"
emacs-tests--lib-src))
(process-environment nil))
(skip-unless (file-executable-p emacs))
(skip-unless (file-readable-p filter))
;; The --seccomp option is processed early, without filename
;; handlers. Therefore remote or quoted filenames wouldn't work.
(should-not (file-remote-p filter))
(cl-callf file-name-unquote filter)
(with-temp-buffer
(let ((start-time (current-time))
(status
(call-process
emacs nil t nil
"--quick" "--batch"
(concat "--seccomp=" filter)
(format "--eval=%S" `(call-process ,emacs nil nil nil
"--version"))))
(end-time (current-time)))
(ert-info ((emacs-tests--seccomp-debug start-time end-time))
(should-not (eql status 0)))))))
(ert-deftest emacs-tests/bwrap/allows-stdout ()
(let ((bash (executable-find "bash"))
(bwrap (executable-find "bwrap"))
(emacs
(expand-file-name invocation-name invocation-directory))
(filter (expand-file-name "seccomp-filter-exec.bpf"
emacs-tests--lib-src))
(process-environment nil))
(skip-unless bash)
(skip-unless bwrap)
(skip-unless (file-executable-p emacs))
(skip-unless (file-readable-p filter))
(should-not (file-remote-p bwrap))
(should-not (file-remote-p emacs))
(should-not (file-remote-p filter))
(with-temp-buffer
(let* ((command
(concat
(mapconcat #'shell-quote-argument
`(,(file-name-unquote bwrap)
"--ro-bind" "/" "/"
"--seccomp" "20"
"--"
,(file-name-unquote emacs)
"--quick" "--batch"
,(format "--eval=%S" '(message "Hi")))
" ")
" 20< "
(shell-quote-argument (file-name-unquote filter))))
(start-time (current-time))
(status (call-process bash nil t nil "-c" command))
(end-time (current-time)))
(ert-info ((emacs-tests--seccomp-debug start-time end-time))
(should (eql status 0)))
(should (equal (string-trim (buffer-string)) "Hi"))))))
(defun emacs-tests--seccomp-debug (start-time end-time)
"Return potentially useful debugging information for Seccomp.
Assume that the current buffer contains subprocess output for the
failing process. START-TIME and END-TIME are time values between
which the process was running."
;; Add a bit of slack for the timestamps.
(cl-callf time-subtract start-time 5)
(cl-callf time-add end-time 5)
(with-output-to-string
(princ "Process output:")
(terpri)
(princ (buffer-substring-no-properties (point-min) (point-max)))
;; Search audit logs for Seccomp messages.
(when-let ((ausearch (executable-find "ausearch")))
(terpri)
(princ "Potentially relevant Seccomp audit events:")
(terpri)
(let ((process-environment '("LC_TIME=C")))
(call-process ausearch nil standard-output nil
"--message" "SECCOMP"
"--start"
(format-time-string "%D" start-time)
(format-time-string "%T" start-time)
"--end"
(format-time-string "%D" end-time)
(format-time-string "%T" end-time)
"--interpret")))
;; Print coredump information if available.
(when-let ((coredumpctl (executable-find "coredumpctl")))
(terpri)
(princ "Potentially useful coredump information:")
(terpri)
(call-process coredumpctl nil standard-output nil
"info"
"--since" (format-time-string "%F %T" start-time)
"--until" (format-time-string "%F %T" end-time)
"--no-pager"))))
;;; emacs-tests.el ends here
|