Debian Codegraph @

doc/man/man3/seccomp_rule_add.3

raw
1 .TH "seccomp_rule_add" 3 "30 May 2020" "[email protected]" "libseccomp Documentation"
2 .\" //////////////////////////////////////////////////////////////////////////
3 .SH NAME
4 .\" //////////////////////////////////////////////////////////////////////////
5 seccomp_rule_add, seccomp_rule_add_exact \- Add a seccomp filter rule
6 .\" //////////////////////////////////////////////////////////////////////////
7 .SH SYNOPSIS
8 .\" //////////////////////////////////////////////////////////////////////////
9 .nf
10 .B #include <seccomp.h>
11 .sp
12 .B typedef void * scmp_filter_ctx;
13 .sp
14 .BI "int SCMP_SYS(" syscall_name ");"
15 .sp
16 .BI "struct scmp_arg_cmp SCMP_CMP(unsigned int " arg ","
17 .BI " enum scmp_compare " op ", " ... ");"
18 .BI "struct scmp_arg_cmp SCMP_A0(enum scmp_compare " op ", " ... ");"
19 .BI "struct scmp_arg_cmp SCMP_A1(enum scmp_compare " op ", " ... ");"
20 .BI "struct scmp_arg_cmp SCMP_A2(enum scmp_compare " op ", " ... ");"
21 .BI "struct scmp_arg_cmp SCMP_A3(enum scmp_compare " op ", " ... ");"
22 .BI "struct scmp_arg_cmp SCMP_A4(enum scmp_compare " op ", " ... ");"
23 .BI "struct scmp_arg_cmp SCMP_A5(enum scmp_compare " op ", " ... ");"
24 .sp
25 .BI "struct scmp_arg_cmp SCMP_CMP64(unsigned int " arg ","
26 .BI " enum scmp_compare " op ", " ... ");"
27 .BI "struct scmp_arg_cmp SCMP_A0_64(enum scmp_compare " op ", " ... ");"
28 .BI "struct scmp_arg_cmp SCMP_A1_64(enum scmp_compare " op ", " ... ");"
29 .BI "struct scmp_arg_cmp SCMP_A2_64(enum scmp_compare " op ", " ... ");"
30 .BI "struct scmp_arg_cmp SCMP_A3_64(enum scmp_compare " op ", " ... ");"
31 .BI "struct scmp_arg_cmp SCMP_A4_64(enum scmp_compare " op ", " ... ");"
32 .BI "struct scmp_arg_cmp SCMP_A5_64(enum scmp_compare " op ", " ... ");"
33 .sp
34 .BI "struct scmp_arg_cmp SCMP_CMP32(unsigned int " arg ","
35 .BI " enum scmp_compare " op ", " ... ");"
36 .BI "struct scmp_arg_cmp SCMP_A0_32(enum scmp_compare " op ", " ... ");"
37 .BI "struct scmp_arg_cmp SCMP_A1_32(enum scmp_compare " op ", " ... ");"
38 .BI "struct scmp_arg_cmp SCMP_A2_32(enum scmp_compare " op ", " ... ");"
39 .BI "struct scmp_arg_cmp SCMP_A3_32(enum scmp_compare " op ", " ... ");"
40 .BI "struct scmp_arg_cmp SCMP_A4_32(enum scmp_compare " op ", " ... ");"
41 .BI "struct scmp_arg_cmp SCMP_A5_32(enum scmp_compare " op ", " ... ");"
42 .sp
43 .BI "int seccomp_rule_add(scmp_filter_ctx " ctx ", uint32_t " action ","
44 .BI " int " syscall ", unsigned int " arg_cnt ", " ... ");"
45 .BI "int seccomp_rule_add_exact(scmp_filter_ctx " ctx ", uint32_t " action ","
46 .BI " int " syscall ", unsigned int " arg_cnt ", " ... ");"
47 .sp
48 .BI "int seccomp_rule_add_array(scmp_filter_ctx " ctx ","
49 .BI " uint32_t " action ", int " syscall ","
50 .BI " unsigned int " arg_cnt ","
51 .BI " const struct scmp_arg_cmp *"arg_array ");"
52 .BI "int seccomp_rule_add_exact_array(scmp_filter_ctx " ctx ","
53 .BI " uint32_t " action ", int " syscall ","
54 .BI " unsigned int " arg_cnt ","
55 .BI " const struct scmp_arg_cmp *"arg_array ");"
56 .sp
57 Link with \fI\-lseccomp\fP.
58 .fi
59 .\" //////////////////////////////////////////////////////////////////////////
60 .SH DESCRIPTION
61 .\" //////////////////////////////////////////////////////////////////////////
62 .P
63 The
64 .BR seccomp_rule_add (),
65 .BR seccomp_rule_add_array (),
66 .BR seccomp_rule_add_exact (),
67 and
68 .BR seccomp_rule_add_exact_array ()
69 functions all add a new filter rule to the current seccomp filter. The
70 .BR seccomp_rule_add ()
71 and
72 .BR seccomp_rule_add_array ()
73 functions will make a "best effort" to add the rule as specified, but may alter
74 the rule slightly due to architecture specifics (e.g. internal rewriting of
75 multiplexed syscalls, like socket and ipc functions on x86). The
76 .BR seccomp_rule_add_exact ()
77 and
78 .BR seccomp_rule_add_exact_array ()
79 functions will attempt to add the rule exactly as specified so it may behave
80 differently on different architectures. While it does not guarantee a exact
81 filter ruleset,
82 .BR seccomp_rule_add ()
83 and
84 .BR seccomp_rule_add_array ()
85 do guarantee the same behavior regardless of the architecture.
86 .P
87 The newly added filter rule does not take effect until the entire filter is
88 loaded into the kernel using
89 .BR seccomp_load (3).
90 When adding rules to a filter, it is important to consider the impact of
91 previously loaded filters; see the
92 .BR seccomp_load (3)
93 documentation for more information.
94 .P
95 All of the filter rules supplied by the calling application are combined into
96 a union, with additional logic to eliminate redundant syscall filters. For
97 example, if a rule is added which allows a given syscall with a specific set of
98 argument values and later a rule is added which allows the same syscall
99 regardless the argument values then the first, more specific rule, is
100 effectively dropped from the filter by the second more generic rule.
101 .P
102 The
103 .BR SCMP_CMP (),
104 .BR SCMP_CMP64 (),
105 .BR SCMP_A{0-5} (),
106 and
107 .BR SCMP_A{0-5}_64 ()
108 macros generate a scmp_arg_cmp structure for use with the above functions. The
109 .BR SCMP_CMP ()
110 and
111 .BR SCMP_CMP64 ()
112 macros allows the caller to specify an arbitrary argument along with the
113 comparison operator, 64-bit mask, and 64-bit datum values where the
114 .BR SCMP_A{0-5} ()
115 and
116 .BR SCMP_A{0-5}_64 ()
117 macros are specific to a certain argument.
118 .P
119 The
120 .BR SCMP_CMP32 ()
121 and
122 .BR SCMP_A{0-5}_32 ()
123 macros are similar to the variants above, but they take 32-bit mask and 32-bit
124 datum values.
125 .P
126 It is recommended that whenever possible developers avoid using the
127 .BR SCMP_CMP ()
128 and
129 .BR SCMP_A{0-5} ()
130 macros and use the variants which are explicitly 32 or 64-bit. This should
131 help eliminate problems caused by an unwanted sign extension of negative datum
132 values.
133 .P
134 If syscall argument comparisons are included in the filter rule, all of the
135 comparisons must be true for the rule to match.
136 .P
137 When adding syscall argument comparisons to the filter it is important to
138 remember that while it is possible to have multiple comparisons in a single
139 rule, you can only compare each argument once in a single rule. In other words,
140 you can not have multiple comparisons of the 3rd syscall argument in a single
141 rule.
142 .P
143 In a filter containing multiple architectures, it is an error to add a filter
144 rule for a syscall that does not exist in all of the filter's architectures.
145 .P
146 While it is possible to specify the
147 .I syscall
148 value directly using the standard
149 .B __NR_syscall
150 values, in order to ensure proper operation across multiple architectures it
151 is highly recommended to use the
152 .BR SCMP_SYS ()
153 macro instead. See the EXAMPLES section below. It is also important to
154 remember that regardless of the architectures present in the filter, the
155 syscall numbers used in filter rules are interpreted in the context of the
156 native architecture.
157 .P
158 Starting with Linux v4.8, there may be a need to create a rule with a syscall
159 value of -1 to allow tracing programs to skip a syscall invocation; in order
160 to create a rule with a -1 syscall value it is necessary to first set the
161 .B SCMP_FLTATR_API_TSKIP
162 attribute. See
163 .BR seccomp_attr_set (3)
164 for more information.
165 .P
166 The filter context
167 .I ctx
168 is the value returned by the call to
169 .BR seccomp_init (3).
170 .P
171 Valid
172 .I action
173 values are as follows:
174 .TP
175 .B SCMP_ACT_KILL
176 The thread will be killed by the kernel when it calls a syscall that matches
177 the filter rule.
178 .TP
179 .B SCMP_ACT_KILL_PROCESS
180 The process will be killed by the kernel when it calls a syscall that matches
181 the filter rule.
182 .TP
183 .B SCMP_ACT_TRAP
184 The thread will throw a SIGSYS signal when it calls a syscall that matches the
185 filter rule.
186 .TP
187 .B SCMP_ACT_ERRNO(uint16_t errno)
188 The thread will receive a return value of
189 .I errno
190 when it calls a syscall that matches the filter rule.
191 .TP
192 .B SCMP_ACT_TRACE(uint16_t msg_num)
193 If the thread is being traced and the tracing process specified the
194 .B PTRACE_O_TRACESECCOMP
195 option in the call to
196 .BR ptrace (2),
197 the tracing process will be notified, via
198 .B PTRACE_EVENT_SECCOMP
199 , and the value provided in
200 .I msg_num
201 can be retrieved using the
202 .B PTRACE_GETEVENTMSG
203 option.
204 .TP
205 .B SCMP_ACT_LOG
206 The seccomp filter will have no effect on the thread calling the syscall if it
207 matches the filter rule but the syscall will be logged.
208 .TP
209 .B SCMP_ACT_ALLOW
210 The seccomp filter will have no effect on the thread calling the syscall if it
211 matches the filter rule.
212 .TP
213 .B SCMP_ACT_NOTIFY
214 A monitoring process will be notified when a process running the seccomp
215 filter calls a syscall that matches the filter rule. The process that invokes
216 the syscall waits in the kernel until the monitoring process has responded via
217 .B seccomp_notify_respond (3)
218 \&.
219
220 When a filter utilizing
221 .B SCMP_ACT_NOTIFY
222 is loaded into the kernel, the kernel generates a notification fd that must be
223 used to communicate between the monitoring process and the process(es) being
224 filtered. See
225 .B seccomp_notify_fd (3)
226 for more information.
227
228 .P
229 Valid comparison
230 .I op
231 values are as follows:
232 .TP
233 .B SCMP_CMP_NE
234 Matches when the argument value is not equal to the datum value, example:
235 .sp
236 SCMP_CMP(
237 .I arg
238 , SCMP_CMP_NE ,
239 .I datum
240 )
241 .TP
242 .B SCMP_CMP_LT
243 Matches when the argument value is less than the datum value, example:
244 .sp
245 SCMP_CMP(
246 .I arg
247 , SCMP_CMP_LT ,
248 .I datum
249 )
250 .TP
251 .B SCMP_CMP_LE
252 Matches when the argument value is less than or equal to the datum value,
253 example:
254 .sp
255 SCMP_CMP(
256 .I arg
257 , SCMP_CMP_LE ,
258 .I datum
259 )
260 .TP
261 .B SCMP_CMP_EQ
262 Matches when the argument value is equal to the datum value, example:
263 .sp
264 SCMP_CMP(
265 .I arg
266 , SCMP_CMP_EQ ,
267 .I datum
268 )
269 .TP
270 .B SCMP_CMP_GE
271 Matches when the argument value is greater than or equal to the datum value,
272 example:
273 .sp
274 SCMP_CMP(
275 .I arg
276 , SCMP_CMP_GE ,
277 .I datum
278 )
279 .TP
280 .B SCMP_CMP_GT
281 Matches when the argument value is greater than the datum value, example:
282 .sp
283 SCMP_CMP(
284 .I arg
285 , SCMP_CMP_GT ,
286 .I datum
287 )
288 .TP
289 .B SCMP_CMP_MASKED_EQ
290 Matches when the masked argument value is equal to the masked datum value,
291 example:
292 .sp
293 SCMP_CMP(
294 .I arg
295 , SCMP_CMP_MASKED_EQ ,
296 .I mask
297 ,
298 .I datum
299 )
300 .\" //////////////////////////////////////////////////////////////////////////
301 .SH RETURN VALUE
302 .\" //////////////////////////////////////////////////////////////////////////
303 The
304 .BR SCMP_SYS ()
305 macro returns a value suitable for use as the
306 .I syscall
307 value in the
308 .BR seccomp_rule_add* ()
309 functions. In a similar manner, the
310 .BR SCMP_CMP ()
311 and
312 .BR SCMP_A* ()
313 macros return values suitable for use as argument comparisons in the
314 .BR seccomp_rule_add ()
315 and
316 .BR seccomp_rule_add_exact ()
317 functions.
318 .P
319 The
320 .BR seccomp_rule_add (),
321 .BR seccomp_rule_add_array (),
322 .BR seccomp_rule_add_exact (),
323 and
324 .BR seccomp_rule_add_exact_array ()
325 functions return zero on success or one of the following error codes on
326 failure:
327 .TP
328 .B -EDOM
329 Architecture specific failure.
330 .TP
331 .B -EEXIST
332 The rule already exists.
333 .TP
334 .B -EACCES
335 The rule conflicts with the filter (for example, the rule
336 .I action
337 equals the default action of the filter).
338 .TP
339 .B -EFAULT
340 Internal libseccomp failure.
341 .TP
342 .B -EINVAL
343 Invalid input, either the context or architecture token is invalid.
344 .TP
345 .B -ENOMEM
346 The library was unable to allocate enough memory.
347 .TP
348 .B -EOPNOTSUPP
349 The library doesn't support the particular operation.
350 .\" //////////////////////////////////////////////////////////////////////////
351 .SH EXAMPLES
352 .\" //////////////////////////////////////////////////////////////////////////
353 .nf
354 #include <fcntl.h>
355 #include <seccomp.h>
356 #include <sys/stat.h>
357 #include <sys/types.h>
358 #include <stddef.h>
359
360 #define BUF_SIZE 256
361
362 int main(int argc, char *argv[])
363 {
364 int rc = \-1;
365 scmp_filter_ctx ctx;
366 struct scmp_arg_cmp arg_cmp[] = { SCMP_A0(SCMP_CMP_EQ, 2) };
367 int fd;
368 unsigned char buf[BUF_SIZE];
369
370 ctx = seccomp_init(SCMP_ACT_KILL);
371 if (ctx == NULL)
372 goto out;
373
374 /* ... */
375
376 fd = open("file.txt", 0);
377
378 /* ... */
379
380 rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
381 if (rc < 0)
382 goto out;
383
384 rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
385 if (rc < 0)
386 goto out;
387
388 rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit), 0);
389 if (rc < 0)
390 goto out;
391
392 rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 3,
393 SCMP_A0(SCMP_CMP_EQ, fd),
394 SCMP_A1(SCMP_CMP_EQ, (scmp_datum_t)buf),
395 SCMP_A2(SCMP_CMP_LE, BUF_SIZE));
396 if (rc < 0)
397 goto out;
398
399 rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
400 SCMP_CMP(0, SCMP_CMP_EQ, fd));
401 if (rc < 0)
402 goto out;
403
404 rc = seccomp_rule_add_array(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
405 arg_cmp);
406 if (rc < 0)
407 goto out;
408
409 rc = seccomp_load(ctx);
410 if (rc < 0)
411 goto out;
412
413 /* ... */
414
415 out:
416 seccomp_release(ctx);
417 return \-rc;
418 }
419 .fi
420 .\" //////////////////////////////////////////////////////////////////////////
421 .SH NOTES
422 .\" //////////////////////////////////////////////////////////////////////////
423 .P
424 While the seccomp filter can be generated independent of the kernel, kernel
425 support is required to load and enforce the seccomp filter generated by
426 libseccomp.
427 .P
428 The libseccomp project site, with more information and the source code
429 repository, can be found at https://github.com/seccomp/libseccomp. This tool,
430 as well as the libseccomp library, is currently under development, please
431 report any bugs at the project site or directly to the author.
432 .\" //////////////////////////////////////////////////////////////////////////
433 .SH BUGS
434 .\" //////////////////////////////////////////////////////////////////////////
435 .P
436 The runtime behavior of seccomp filters is dependent upon the kernel
437 version, the processor architecture, and other libraries including libc.
438 This could affect the return code of a seccomp filter.
439
440 .TP
441 .B *
442 PowerPC glibc will not return a negative number when the
443 .B getpid()
444 syscall is invoked. If a seccomp filter has been created where
445 .B getpid()
446 will return a negative number from the kernel, then PowerPC glibc will
447 return the absolute value of the errno. In this case, it is very difficult
448 for an application to distinguish between the errno and a valid pid.
449
450 .\" //////////////////////////////////////////////////////////////////////////
451 .SH AUTHOR
452 .\" //////////////////////////////////////////////////////////////////////////
453 Paul Moore <[email protected]>
454 .\" //////////////////////////////////////////////////////////////////////////
455 .SH SEE ALSO
456 .\" //////////////////////////////////////////////////////////////////////////
457 .BR seccomp_syscall_resolve_name_rewrite (3),
458 .BR seccomp_syscall_priority (3),
459 .BR seccomp_load (3),
460 .BR seccomp_attr_set (3)