aboutsummaryrefslogtreecommitdiff
path: root/lib/libsys/jail.2
blob: d3f871608c1d34a1c475ae97808f343f49522ae2 (plain) (blame)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
.\" Copyright (c) 1999 Poul-Henning Kamp.
.\" Copyright (c) 2009 James Gritton.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd September 9, 2025
.Dt JAIL 2
.Os
.Sh NAME
.Nm jail ,
.Nm jail_get ,
.Nm jail_set ,
.Nm jail_remove ,
.Nm jail_attach ,
.Nm jail_remove_jd ,
.Nm jail_attach_jd
.Nd create and manage system jails
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/param.h
.In sys/jail.h
.Ft int
.Fn jail "struct jail *jail"
.Ft int
.Fn jail_attach "int jid"
.Ft int
.Fn jail_remove "int jid"
.Ft int
.Fn jail_attach_jd "int fd"
.Ft int
.Fn jail_remove_jd "int fd"
.In sys/uio.h
.Ft int
.Fn jail_get "struct iovec *iov" "u_int niov" "int flags"
.Ft int
.Fn jail_set "struct iovec *iov" "u_int niov" "int flags"
.Sh DESCRIPTION
The
.Fn jail
system call sets up a jail and locks the current process in it.
.Pp
The argument is a pointer to a structure describing the prison:
.Bd -literal -offset indent
struct jail {
	uint32_t	version;
	char		*path;
	char		*hostname;
	char		*jailname;
	unsigned int	ip4s;
	unsigned int	ip6s;
	struct in_addr	*ip4;
	struct in6_addr	*ip6;
};
.Ed
.Pp
.Dq Li version
defines the version of the API in use.
.Dv JAIL_API_VERSION
is defined for the current version.
.Pp
The
.Dq Li path
pointer should be set to the directory which is to be the root of the
prison.
.Pp
The
.Dq Li hostname
pointer can be set to the hostname of the prison.
This can be changed
from the inside of the prison.
.Pp
The
.Dq Li jailname
pointer is an optional name that can be assigned to the jail
for example for management purposes.
.Pp
The
.Dq Li ip4s
and
.Dq Li ip6s
give the numbers of IPv4 and IPv6 addresses that will be passed
via their respective pointers.
.Pp
The
.Dq Li ip4
and
.Dq Li ip6
pointers can be set to an arrays of IPv4 and IPv6 addresses to be assigned to
the prison, or NULL if none.
IPv4 addresses must be in network byte order.
.Pp
This is equivalent to, and deprecated in favor of, the
.Fn jail_set
system call (see below), with the parameters
.Va path ,
.Va host.hostname ,
.Va name ,
.Va ip4.addr ,
and
.Va ip6.addr ,
and with the
.Dv JAIL_ATTACH
flag.
.Pp
The
.Fn jail_set
system call creates a new jail, or modifies an existing one, and optionally
locks the current process in it.
Jail parameters are passed as an array of name-value pairs in the array
.Fa iov ,
containing
.Fa niov
elements.
Parameter names are a null-terminated string, and values may be strings,
integers, or other arbitrary data.
Some parameters are boolean, and do not have a value (their length is zero)
but are set by the name alone with or without a
.Dq no
prefix, e.g.
.Va persist
or
.Va nopersist .
Any parameters not set will be given default values, generally based on
the current environment.
.Pp
Jails have a set of core parameters, and modules can add their own jail
parameters.
The current set of available parameters, and their formats, can be
retrieved via the
.Va security.jail.param
sysctl MIB entry.
Notable parameters include those mentioned in the
.Fn jail
description above, as well as
.Va jid
and
.Va name ,
which identify the jail being created or modified.
See
.Xr jail 8
for more information on the core jail parameters.
.Pp
The
.Fa flags
arguments consists of one or more of the following flags:
.Bl -tag -width indent
.It Dv JAIL_CREATE
Create a new jail.
If a
.Va jid
or
.Va name
parameters exists, they must not refer to an existing jail.
.It Dv JAIL_UPDATE
Modify an existing jail.
One of the
.Va jid
or
.Va name
parameters must exist, and must refer to an existing jail.
If both
.Dv JAIL_CREATE
and
.Dv JAIL_UPDATE
are set, a jail will be created if it does not yet exist, and modified if it
does exist.
.It Dv JAIL_ATTACH
In addition to creating or modifying the jail, attach the current process to
it, as with the
.Fn jail_attach
system call.
.It Dv JAIL_DYING
This is deprecated in
.Fn jail_set
and has no effect.
.It Dv JAIL_USE_DESC
Identify the jail by a descriptor in the
.Va desc
parameter.
.It Dv JAIL_AT_DESC
Operate in the context of the jail described by the
.Va desc
parameter, instead of the current jail.
Only one of
.Dv JAIL_USE_DESC
or
.Dv JAIL_AT_DESC
may be specified.
.It Dv JAIL_GET_DESC
Return a new jail descriptor for the jail in the
.Va desc
parameter.
.It Dv JAIL_OWN_DESC
Return an
.Dq owning
jail descriptor in the
.Va desc
parameter.
.El
.Pp
The
.Fn jail_get
system call retrieves jail parameters, using the same name-value list as
.Fn jail_set
in the
.Fa iov
and
.Fa niov
arguments.
The jail to read can be specified by either
.Va jid
or
.Va name
by including those parameters in the list.
If they are included but are not intended to be the search key, they
should be cleared (zero and the empty string respectively).
.Pp
The special parameter
.Va lastjid
can be used to retrieve a list of all jails.
It will fetch the jail with the jid above and closest to the passed value.
The first jail (usually but not always jid 1) can be found by passing a
.Va lastjid
of zero.
.Pp
The
.Fa flags
arguments consists of one or more following flags:
.Bl -tag -width indent
.It Dv JAIL_DYING
Allow getting a jail that is in the process of being removed.
.It Dv JAIL_USE_DESC , Dv JAIL_AT_DESC , Dv JAIL_GET_DESC , Dv JAIL_OWN_DESC
These have the same meaning as they do in
.Fn jail_set .
.El
.Pp
The
.Fn jail_attach
system call attaches the current process to an existing jail,
identified by
.Fa jid .
It changes the process's root and current directories to the jail's
.Va path
directory.
.Pp
The
.Fn jail_remove
system call removes the jail identified by
.Fa jid .
It will kill all processes belonging to the jail, and remove any children
of that jail.
.Pp
The
.Fn jail_attach_fd
and
.Fn jail_remove_fd
system calls work the same as
.Fn jail_attach
and
.Fn jail_remove ,
except that they operate on the jail identified by jail descriptor
.Fa fd .
.Ss Jail Descriptors
In addition to the jail ID,
jails can be referred to using a jail descriptor,
a type of file descriptor tied to a particular jail.
Jail descriptors are created by calling
.Fn jail_set
or
.Fn jail_get
with the special parameter
.Va desc ,
and either the
.Dv JAIL_GET_DESC
or
.Dv JAIL_OWN_DESC
flags set.
The difference between the two flags is that descriptors created with
.Dv JAIL_OWN_DESC
.Po
called
.Dq owning
descriptors
.Pc
will automatically remove the jail when the descriptor is closed.
.Pp
Jail descriptors can be passed back to
.Fn jail_set
or
.Fm jail_get
with the
.Va desc
parameter,
and either the
.Dv JAIL_USE_DESC
or
.Dv JAIL_AT_DESC
flags set.
With
.Dv JAIL_USE_DESC ,
the descriptor identifies the jail to operate on,
instead of the
.Va jid
or
.Va name
parameter.
With
.Dv JAIL_AT_DESC ,
the descriptor is used in place of the current jail,
allowing accessing or creating jails that are children of the
descriptor jail.
.Pp
The system calls
.Fn jail_attach_jd
and
.Fn jail_aremove_jd
work the same as
.Fn jail_attach
and
.Fn jail_remove ,
except that they operate on the jail referred to by the passed descriptor.
.Sh RETURN VALUES
If successful,
.Fn jail ,
.Fn jail_set ,
and
.Fn jail_get
return a non-negative integer, termed the jail identifier (JID).
They return \-1 on failure, and set
.Va errno
to indicate the error.
.Pp
.Rv -std jail_attach jail_remove jail_attach_jd jail_remove_jd
.Sh ERRORS
The
.Fn jail
system call
will fail if:
.Bl -tag -width Er
.It Bq Er EPERM
This process is not allowed to create a jail, either because it is not
the super-user, or because it would exceed the jail's
.Va children.max
limit.
.It Bq Er EFAULT
.Fa jail
points to an address outside the allocated address space of the process.
.It Bq Er EINVAL
The version number of the argument is not correct.
.It Bq Er EAGAIN
No free JID could be found.
.El
.Pp
The
.Fn jail_set
system call
will fail if:
.Bl -tag -width Er
.It Bq Er EPERM
This process is not allowed to create a jail, either because it is not
the super-user, or because it would exceed the jail's
.Va children.max
limit.
.It Bq Er EPERM
The jail descriptor in the
.Va desc
parameter was created by a user other than the super-user,
and the
.Dv JAIL_USE_DESC
flag was set.
.It Bq Er EPERM
A jail parameter was set to a less restrictive value then the current
environment.
.It Bq Er EFAULT
.Fa Iov ,
or one of the addresses contained within it,
points to an address outside the allocated address space of the process.
.It Bq Er ENOENT
The jail referred to by a
.Va jid
or
.Va name
parameter does not exist, and the
.Dv JAIL_CREATE
flag is not set.
.It Bq Er ENOENT
The jail referred to by a
.Va jid
parameter is not accessible by the process, because the process is in a
different jail.
.It Bq Er ENOENT
The jail referred to by a
.Va desc
parameter has been removed.
.It Bq Er EEXIST
The jail referred to by a
.Va jid
or
.Va name
parameter exists, and the
.Dv JAIL_UPDATE
flag is not set.
.It Bq Er EINVAL
A supplied parameter is the wrong size.
.It Bq Er EINVAL
A supplied parameter is out of range.
.It Bq Er EINVAL
A supplied string parameter is not null-terminated.
.It Bq Er EINVAL
A supplied parameter name does not match any known parameters.
.It Bq Er EINVAL
One of the
.Dv JAIL_CREATE
or
.Dv JAIL_UPDATE
flags is not set.
.It Bq Er ENAMETOOLONG
A supplied string parameter is longer than allowed.
.It Bq Er EAGAIN
There are no jail IDs left.
.It Bq Er EMFILE
A jail descriptor could not be created for the
.Va desc
parameter with either the
.Dv JAIL_GET_DESC
or
.Dv JAIL_OWN_DESC
flag set,
because the process has already reached its limit for open file descriptors.
.It Bq Er ENFILE
A jail descriptor could not be created for the
.Va desc
parameter with either the
.Dv JAIL_GET_DESC
or
.Dv JAIL_OWN_DESC
flag set,
because the system file table is full.
.El
.Pp
The
.Fn jail_get
system call
will fail if:
.Bl -tag -width Er
.It Bq Er ENOENT
The jail referred to by a
.Va jid
or
.Va name
parameter does not exist.
.It Bq Er ENOENT
The jail referred to by a
.Va jid
is not accessible by the process, because the process is in a different
jail.
.It Bq Er ENOENT
The
.Va lastjid
parameter is greater than the highest current jail ID.
.It Bq Er ENOENT
The jail referred to by a
.Va desc
parameter has been removed
.Pq even if the Dv JAIL_CREATE flag has been set .
.It Bq Er EINVAL
A supplied parameter is the wrong size.
.It Bq Er EINVAL
A supplied parameter is out of range.
.It Bq Er EINVAL
A supplied string parameter is not null-terminated.
.It Bq Er EINVAL
A supplied parameter name does not match any known parameters.
.It Bq Er EMFILE
A jail descriptor could not be created for the
.Va desc
parameter with either the
.Dv JAIL_GET_DESC
or
.Dv JAIL_OWN_DESC
flag set,
because the process has already reached its limit for open file descriptors.
.It Bq Er ENFILE
A jail descriptor could not be created for the
.Va desc
parameter with either the
.Dv JAIL_GET_DESC
or
.Dv JAIL_OWN_DESC
flag set,
because the system file table is full.
.El
.Pp
The
.Fn jail_attach
and
.Fn jail_remove
system calls
will fail if:
.Bl -tag -width Er
.It Bq Er EPERM
A user other than the super-user attempted to attach to or remove a jail.
.It Bq Er EINVAL
The jail specified by
.Fa jid
does not exist.
.El
.Pp
The
.Fn jail_attach_jd
and
.Fn jail_remove_jd
system calls
will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
The
.Fa fd
argument is not a valid jail descriptor.
.It Bq Er EPERM
The jail descriptor was created by a user other than the super-user.
.It Bq Er EINVAL
The jail specified by
.Fa jid
has been removed.
.El
.Pp
Further
.Fn jail ,
.Fn jail_set ,
.Fn jail_attach ,
and
.Fn jail_attach_jd
call
.Xr chroot 2
internally, so they can fail for all the same reasons.
Please consult the
.Xr chroot 2
manual page for details.
.Sh SEE ALSO
.Xr chdir 2 ,
.Xr chroot 2 ,
.Xr jail 8
.Sh HISTORY
The
.Fn jail
system call appeared in
.Fx 4.0 .
The
.Fn jail_attach
system call appeared in
.Fx 5.1 .
The
.Fn jail_set ,
.Fn jail_get ,
and
.Fn jail_remove
system calls appeared in
.Fx 8.0 .
.Sh AUTHORS
The jail feature was written by
.An Poul-Henning Kamp
for R&D Associates
who contributed it to
.Fx .
.An James Gritton
added the extensible jail parameters and hierarchical jails.