xref: /freebsd/lib/libsys/cap_rights_limit.2 (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1.\"
2.\" Copyright (c) 2008-2010 Robert N. M. Watson
3.\" Copyright (c) 2012-2013 The FreeBSD Foundation
4.\" All rights reserved.
5.\"
6.\" This software was developed at the University of Cambridge Computer
7.\" Laboratory with support from a grant from Google, Inc.
8.\"
9.\" Portions of this documentation were written by Pawel Jakub Dawidek
10.\" under sponsorship from the FreeBSD Foundation.
11.\"
12.\" Redistribution and use in source and binary forms, with or without
13.\" modification, are permitted provided that the following conditions
14.\" are met:
15.\" 1. Redistributions of source code must retain the above copyright
16.\"    notice, this list of conditions and the following disclaimer.
17.\" 2. Redistributions in binary form must reproduce the above copyright
18.\"    notice, this list of conditions and the following disclaimer in the
19.\"    documentation and/or other materials provided with the distribution.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.Dd April 27, 2024
34.Dt CAP_RIGHTS_LIMIT 2
35.Os
36.Sh NAME
37.Nm cap_rights_limit
38.Nd limit capability rights
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In sys/capsicum.h
43.Ft int
44.Fn cap_rights_limit "int fd" "const cap_rights_t *rights"
45.Sh DESCRIPTION
46When a file descriptor is created by a function such as
47.Xr fhopen 2 ,
48.Xr kqueue 2 ,
49.Xr mq_open 2 ,
50.Xr open 2 ,
51.Xr pdfork 2 ,
52.Xr pipe 2 ,
53.Xr shm_open 2 ,
54.Xr socket 2
55or
56.Xr socketpair 2 ,
57it is assigned all capability rights; for
58.Xr accept 2 ,
59.Xr accept4 2
60or
61.Xr openat 2 ,
62it inherits capability rights from the "parent" file descriptor.
63Those rights can be reduced (but never expanded) by using the
64.Fn cap_rights_limit
65system call.
66Once capability rights are reduced, operations on the file descriptor will be
67limited to those permitted by
68.Fa rights .
69.Pp
70The
71.Fa rights
72argument should be prepared using
73.Xr cap_rights_init 3
74family of functions.
75.Pp
76Capability rights assigned to a file descriptor can be obtained with the
77.Xr cap_rights_get 3
78function.
79.Pp
80The complete list of the capability rights can be found in the
81.Xr rights 4
82manual page.
83.Sh RETURN VALUES
84.Rv -std
85.Sh EXAMPLES
86The following example demonstrates how to limit file descriptor capability
87rights to allow reading only.
88.Bd -literal
89cap_rights_t setrights;
90char buf[1];
91int fd;
92
93fd = open("/tmp/foo", O_RDWR);
94if (fd < 0)
95	err(1, "open() failed");
96
97if (cap_enter() < 0)
98	err(1, "cap_enter() failed");
99
100cap_rights_init(&setrights, CAP_READ);
101if (cap_rights_limit(fd, &setrights) < 0)
102	err(1, "cap_rights_limit() failed");
103
104buf[0] = 'X';
105
106if (write(fd, buf, sizeof(buf)) > 0)
107	errx(1, "write() succeeded!");
108
109if (read(fd, buf, sizeof(buf)) < 0)
110	err(1, "read() failed");
111.Ed
112.Sh ERRORS
113.Fn cap_rights_limit
114succeeds unless:
115.Bl -tag -width Er
116.It Bq Er EBADF
117The
118.Fa fd
119argument is not a valid active descriptor.
120.It Bq Er EINVAL
121An invalid right has been requested in
122.Fa rights .
123.It Bq Er ENOSYS
124The running kernel was compiled without
125.Cd "options CAPABILITY_MODE" .
126.It Bq Er ENOTCAPABLE
127The
128.Fa rights
129argument contains capability rights not present for the given file descriptor.
130Capability rights list can only be reduced, never expanded.
131.El
132.Sh SEE ALSO
133.Xr accept 2 ,
134.Xr accept4 2 ,
135.Xr cap_enter 2 ,
136.Xr fhopen 2 ,
137.Xr kqueue 2 ,
138.Xr mq_open 2 ,
139.Xr open 2 ,
140.Xr openat 2 ,
141.Xr pdfork 2 ,
142.Xr pipe 2 ,
143.Xr read 2 ,
144.Xr shm_open 2 ,
145.Xr socket 2 ,
146.Xr socketpair 2 ,
147.Xr write 2 ,
148.Xr cap_rights_get 3 ,
149.Xr cap_rights_init 3 ,
150.Xr err 3 ,
151.Xr capsicum 4 ,
152.Xr rights 4
153.Sh HISTORY
154The
155.Fn cap_rights_limit
156function first appeared in
157.Fx 8.3 .
158Support for capabilities and capabilities mode was developed as part of the
159.Tn TrustedBSD
160Project.
161.Sh AUTHORS
162This function was created by
163.An Pawel Jakub Dawidek Aq Mt pawel@dawidek.net
164under sponsorship of the FreeBSD Foundation.
165