Bundle libseccomp 2.3.1
[linux-seccomp.git] / libseccomp / doc / man / man3 / seccomp_rule_add.3
CommitLineData
8befd5cc
MG
1.TH "seccomp_rule_add" 3 "25 July 2012" "paul@paul-moore.com" "libseccomp Documentation"
2.\" //////////////////////////////////////////////////////////////////////////
3.SH NAME
4.\" //////////////////////////////////////////////////////////////////////////
5seccomp_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 "int seccomp_rule_add(scmp_filter_ctx " ctx ", uint32_t " action ","
26.BI " int " syscall ", unsigned int " arg_cnt ", " ... ");"
27.BI "int seccomp_rule_add_exact(scmp_filter_ctx " ctx ", uint32_t " action ","
28.BI " int " syscall ", unsigned int " arg_cnt ", " ... ");"
29.sp
30.BI "int seccomp_rule_add_array(scmp_filter_ctx " ctx ","
31.BI " uint32_t " action ", int " syscall ","
32.BI " unsigned int " arg_cnt ","
33.BI " const struct scmp_arg_cmp *"arg_array ");"
34.BI "int seccomp_rule_add_exact_array(scmp_filter_ctx " ctx ","
35.BI " uint32_t " action ", int " syscall ","
36.BI " unsigned int " arg_cnt ","
37.BI " const struct scmp_arg_cmp *"arg_array ");"
38.sp
39Link with \fI\-lseccomp\fP.
40.fi
41.\" //////////////////////////////////////////////////////////////////////////
42.SH DESCRIPTION
43.\" //////////////////////////////////////////////////////////////////////////
44.P
45The
46.BR seccomp_rule_add (),
47.BR seccomp_rule_add_array (),
48.BR seccomp_rule_add_exact (),
49and
50.BR seccomp_rule_add_exact_array ()
51functions all add a new filter rule to the current seccomp filter. The
52.BR seccomp_rule_add ()
53and
54.BR seccomp_rule_add_array ()
55functions will make a "best effort" to add the rule as specified, but may alter
56the rule slightly due to architecture specifics, e.g. socket and ipc functions
57on x86. The
58.BR seccomp_rule_add_exact ()
59and
60.BR seccomp_rule_add_exact_array ()
61functions will attempt to add the rule exactly as specified so it may behave
62differently on different architectures. While it does not guarantee a exact
63filter ruleset,
64.BR seccomp_rule_add ()
65and
66.BR seccomp_rule_add_array ()
67do guarantee the same behavior regardless of the architecture.
68.P
69The newly added filter rule does not take effect until the entire filter is
70loaded into the kernel using
71.BR seccomp_load (3).
72.P
73The
74.BR SCMP_CMP ()
75and
76.BR SCMP_A{0-5} ()
77macros generate a scmp_arg_cmp structure for use with the above functions. The
78.BR SCMP_CMP ()
79macro allows the caller to specify an arbitrary argument along with the
80comparison operator, mask, and datum values where the
81.BR SCMP_A{0-5} ()
82macros are specific to a certain argument. See the EXAMPLES section below.
83.P
84While it is possible to specify the
85.I syscall
86value directly using the standard
87.B __NR_syscall
88values, in order to ensure proper operation across multiple architectures it
89is highly recommended to use the
90.BR SCMP_SYS ()
91macro instead. See the EXAMPLES section below.
92.P
93The filter context
94.I ctx
95is the value returned by the call to
96.BR seccomp_init (3).
97.P
98Valid
99.I action
100values are as follows:
101.TP
102.B SCMP_ACT_KILL
103The thread will be killed by the kernel when it calls a syscall that does not
104match any of the configured seccomp filter rules.
105.TP
106.B SCMP_ACT_TRAP
107The thread will throw a SIGSYS signal when it calls a syscall that does not
108match any of the configured seccomp filter rules.
109.TP
110.B SCMP_ACT_ERRNO(uint16_t errno)
111The thread will receive a return value of
112.I errno
113when it calls a syscall that does not match any of the configured seccomp filter
114rules.
115.TP
116.B SCMP_ACT_TRACE(uint16_t msg_num)
117If the thread is being traced and the tracing process specified the
118.B PTRACE_O_TRACESECCOMP
119option in the call to
120.BR ptrace (2),
121the tracing process will be notified, via
122.B PTRACE_EVENT_SECCOMP
123, and the value provided in
124.I msg_num
125can be retrieved using the
126.B PTRACE_GETEVENTMSG
127option.
128.TP
129.B SCMP_ACT_ALLOW
130The seccomp filter will have no effect on the thread calling the syscall if it
131does not match any of the configured seccomp filter rules.
132.P
133Valid comparison
134.I op
135values are as follows:
136.TP
137.B SCMP_CMP_NE
138Matches when the argument value is not equal to the datum value, example:
139.sp
140SCMP_CMP(
141.I arg
142, SCMP_CMP_NE ,
143.I datum
144)
145.TP
146.B SCMP_CMP_LT
147Matches when the argument value is less than the datum value, example:
148.sp
149SCMP_CMP(
150.I arg
151, SCMP_CMP_LT ,
152.I datum
153)
154.TP
155.B SCMP_CMP_LE
156Matches when the argument value is less than or equal to the datum value,
157example:
158.sp
159SCMP_CMP(
160.I arg
161, SCMP_CMP_LE ,
162.I datum
163)
164.TP
165.B SCMP_CMP_EQ
166Matches when the argument value is equal to the datum value, example:
167.sp
168SCMP_CMP(
169.I arg
170, SCMP_CMP_EQ ,
171.I datum
172)
173.TP
174.B SCMP_CMP_GE
175Matches when the argument value is greater than or equal to the datum value,
176example:
177.sp
178SCMP_CMP(
179.I arg
180, SCMP_CMP_GE ,
181.I datum
182)
183.TP
184.B SCMP_CMP_GT
185Matches when the argument value is greater than the datum value, example:
186.sp
187SCMP_CMP(
188.I arg
189, SCMP_CMP_GT ,
190.I datum
191)
192.TP
193.B SCMP_CMP_MASKED_EQ
194Matches when the masked argument value is equal to the masked datum value,
195example:
196.sp
197SCMP_CMP(
198.I arg
199, SCMP_CMP_MASKED_EQ ,
200.I mask
201,
202.I datum
203)
204.\" //////////////////////////////////////////////////////////////////////////
205.SH RETURN VALUE
206.\" //////////////////////////////////////////////////////////////////////////
207The
208.BR seccomp_rule_add (),
209.BR seccomp_rule_add_array (),
210.BR seccomp_rule_add_exact (),
211and
212.BR seccomp_rule_add_exact_array ()
213functions return zero on success, negative errno values on failure.
214.\" //////////////////////////////////////////////////////////////////////////
215.SH EXAMPLES
216.\" //////////////////////////////////////////////////////////////////////////
217.nf
218#include <fcntl.h>
219#include <seccomp.h>
220#include <sys/stat.h>
221#include <sys/types.h>
222
223#define BUF_SIZE 256
224
225int main(int argc, char *argv[])
226{
227 int rc = \-1;
228 scmp_filter_ctx ctx;
229 struct scmp_arg_cmp arg_cmp[] = { SCMP_A0(SCMP_CMP_EQ, 2) };
230 int fd;
231 unsigned char buf[BUF_SIZE];
232
233 ctx = seccomp_init(SCMP_ACT_KILL);
234 if (ctx == NULL)
235 goto out;
236
237 /* ... */
238
239 fd = open("file.txt", 0);
240
241 /* ... */
242
243 rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
244 if (rc < 0)
245 goto out;
246
247 rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 3,
248 SCMP_A0(SCMP_CMP_EQ, fd),
249 SCMP_A1(SCMP_CMP_EQ, (scmp_datum_t)buf),
250 SCMP_A2(SCMP_CMP_LE, BUF_SIZE));
251 if (rc < 0)
252 goto out;
253
254 rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
255 SCMP_CMP(0, SCMP_CMP_EQ, fd));
256 if (rc < 0)
257 goto out;
258
259 rc = seccomp_rule_add_array(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
260 arg_cmp);
261 if (rc < 0)
262 goto out;
263
264 rc = seccomp_load(ctx);
265 if (rc < 0)
266 goto out;
267
268 /* ... */
269
270out:
271 seccomp_release(ctx);
272 return \-rc;
273}
274.fi
275.\" //////////////////////////////////////////////////////////////////////////
276.SH NOTES
277.\" //////////////////////////////////////////////////////////////////////////
278.P
279While the seccomp filter can be generated independent of the kernel, kernel
280support is required to load and enforce the seccomp filter generated by
281libseccomp.
282.P
283The libseccomp project site, with more information and the source code
284repository, can be found at https://github.com/seccomp/libseccomp. This tool,
285as well as the libseccomp library, is currently under development, please
286report any bugs at the project site or directly to the author.
287.\" //////////////////////////////////////////////////////////////////////////
288.SH AUTHOR
289.\" //////////////////////////////////////////////////////////////////////////
290Paul Moore <paul@paul-moore.com>
291.\" //////////////////////////////////////////////////////////////////////////
292.SH SEE ALSO
293.\" //////////////////////////////////////////////////////////////////////////
294.BR seccomp_syscall_priority (3),
295.BR seccomp_load (3)
This page took 0.025962 seconds and 4 git commands to generate.