xref: /freebsd/lib/libsys/cap_rights_limit.2 (revision 725a9f47324d42037db93c27ceb40d4956872f3e)
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 March 9, 2023
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 accept 2 ,
48.Xr accept4 2 ,
49.Xr fhopen 2 ,
50.Xr kqueue 2 ,
51.Xr mq_open 2 ,
52.Xr open 2 ,
53.Xr openat 2 ,
54.Xr pdfork 2 ,
55.Xr pipe 2 ,
56.Xr shm_open 2 ,
57.Xr socket 2
58or
59.Xr socketpair 2 ,
60it is assigned all capability rights.
61Those rights can be reduced (but never expanded) by using the
62.Fn cap_rights_limit
63system call.
64Once capability rights are reduced, operations on the file descriptor will be
65limited to those permitted by
66.Fa rights .
67.Pp
68The
69.Fa rights
70argument should be prepared using
71.Xr cap_rights_init 3
72family of functions.
73.Pp
74Capability rights assigned to a file descriptor can be obtained with the
75.Xr cap_rights_get 3
76function.
77.Pp
78The complete list of the capability rights can be found in the
79.Xr rights 4
80manual page.
81.Sh RETURN VALUES
82.Rv -std
83.Sh EXAMPLES
84The following example demonstrates how to limit file descriptor capability
85rights to allow reading only.
86.Bd -literal
87cap_rights_t setrights;
88char buf[1];
89int fd;
90
91fd = open("/tmp/foo", O_RDWR);
92if (fd < 0)
93	err(1, "open() failed");
94
95if (cap_enter() < 0)
96	err(1, "cap_enter() failed");
97
98cap_rights_init(&setrights, CAP_READ);
99if (cap_rights_limit(fd, &setrights) < 0)
100	err(1, "cap_rights_limit() failed");
101
102buf[0] = 'X';
103
104if (write(fd, buf, sizeof(buf)) > 0)
105	errx(1, "write() succeeded!");
106
107if (read(fd, buf, sizeof(buf)) < 0)
108	err(1, "read() failed");
109.Ed
110.Sh ERRORS
111.Fn cap_rights_limit
112succeeds unless:
113.Bl -tag -width Er
114.It Bq Er EBADF
115The
116.Fa fd
117argument is not a valid active descriptor.
118.It Bq Er EINVAL
119An invalid right has been requested in
120.Fa rights .
121.It Bq Er ENOSYS
122The running kernel was compiled without
123.Cd "options CAPABILITY_MODE" .
124.It Bq Er ENOTCAPABLE
125The
126.Fa rights
127argument contains capability rights not present for the given file descriptor.
128Capability rights list can only be reduced, never expanded.
129.El
130.Sh SEE ALSO
131.Xr accept 2 ,
132.Xr accept4 2 ,
133.Xr cap_enter 2 ,
134.Xr fhopen 2 ,
135.Xr kqueue 2 ,
136.Xr mq_open 2 ,
137.Xr open 2 ,
138.Xr openat 2 ,
139.Xr pdfork 2 ,
140.Xr pipe 2 ,
141.Xr read 2 ,
142.Xr shm_open 2 ,
143.Xr socket 2 ,
144.Xr socketpair 2 ,
145.Xr write 2 ,
146.Xr cap_rights_get 3 ,
147.Xr cap_rights_init 3 ,
148.Xr err 3 ,
149.Xr capsicum 4 ,
150.Xr rights 4
151.Sh HISTORY
152The
153.Fn cap_rights_limit
154function first appeared in
155.Fx 8.3 .
156Support for capabilities and capabilities mode was developed as part of the
157.Tn TrustedBSD
158Project.
159.Sh AUTHORS
160This function was created by
161.An Pawel Jakub Dawidek Aq Mt pawel@dawidek.net
162under sponsorship of the FreeBSD Foundation.
163