]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libc/sys/cap_rights_limit.2
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / libc / sys / cap_rights_limit.2
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 .\" $FreeBSD$
34 .\"
35 .Dd February 23, 2013
36 .Dt CAP_RIGHTS_LIMIT 2
37 .Os
38 .Sh NAME
39 .Nm cap_rights_limit
40 .Nd limit capability rights
41 .Sh LIBRARY
42 .Lb libc
43 .Sh SYNOPSIS
44 .In sys/capability.h
45 .Ft int
46 .Fn cap_rights_limit "int fd" "const cap_rights_t *rights"
47 .Sh DESCRIPTION
48 When a file descriptor is created by a function such as
49 .Xr accept 2 ,
50 .Xr accept4 2 ,
51 .Xr fhopen 2 ,
52 .Xr kqueue 2 ,
53 .Xr mq_open 2 ,
54 .Xr open 2 ,
55 .Xr openat 2 ,
56 .Xr pdfork 2 ,
57 .Xr pipe 2 ,
58 .Xr shm_open 2 ,
59 .Xr socket 2
60 or
61 .Xr socketpair 2 ,
62 it is assigned all capability rights.
63 Those rights can be reduced (but never expanded) by using the
64 .Fn cap_rights_limit
65 system call.
66 Once capability rights are reduced, operations on the file descriptor will be
67 limited to those permitted by
68 .Fa rights .
69 .Pp
70 The
71 .Fa rights
72 argument should be prepared using
73 .Xr cap_rights_init 3
74 family of functions.
75 .Pp
76 Capability rights assigned to a file descriptor can be obtained with the
77 .Xr cap_rights_get 3
78 function.
79 .Pp
80 The complete list of the capability rights can be found in the
81 .Xr rights 4
82 manual page.
83 .Sh RETURN VALUES
84 .Rv -std
85 .Sh EXAMPLES
86 The following example demonstrates how to limit file descriptor capability
87 rights to allow reading only.
88 .Bd -literal
89 cap_rights_t rights;
90 char buf[1];
91 int fd;
92
93 fd = open("/tmp/foo", O_RDWR);
94 if (fd < 0)
95         err(1, "open() failed");
96
97 if (cap_enter() < 0)
98         err(1, "cap_enter() failed");
99
100 cap_rights_init(&setrights, CAP_READ);
101 if (cap_rights_limit(fd, &setrights) < 0)
102         err(1, "cap_rights_limit() failed");
103
104 buf[0] = 'X';
105
106 if (write(fd, buf, sizeof(buf)) > 0)
107         errx(1, "write() succeeded!");
108
109 if (read(fd, buf, sizeof(buf)) < 0)
110         err(1, "read() failed");
111 .Ed
112 .Sh ERRORS
113 .Fn cap_rights_limit
114 succeeds unless:
115 .Bl -tag -width Er
116 .It Bq Er EBADF
117 The
118 .Fa fd
119 argument is not a valid active descriptor.
120 .It Bq Er EINVAL
121 An invalid right has been requested in
122 .Fa rights .
123 .It Bq Er ENOTCAPABLE
124 The
125 .Fa rights
126 argument contains capability rights not present for the given file descriptor.
127 Capability rights list can only be reduced, never expanded.
128 .El
129 .Sh SEE ALSO
130 .Xr accept 2 ,
131 .Xr accept4 2 ,
132 .Xr cap_enter 2 ,
133 .Xr fhopen 2 ,
134 .Xr kqueue 2 ,
135 .Xr mq_open 2 ,
136 .Xr open 2 ,
137 .Xr openat 2 ,
138 .Xr pdfork 2 ,
139 .Xr pipe 2 ,
140 .Xr read 2 ,
141 .Xr shm_open 2 ,
142 .Xr socket 2 ,
143 .Xr socketpair 2 ,
144 .Xr write 2 ,
145 .Xr cap_rights_get 3 ,
146 .Xr cap_rights_init 3 ,
147 .Xr err 3 ,
148 .Xr capsicum 4 ,
149 .Xr rights 4
150 .Sh HISTORY
151 Support for capabilities and capabilities mode was developed as part of the
152 .Tn TrustedBSD
153 Project.
154 .Sh AUTHORS
155 This function was created by
156 .An Pawel Jakub Dawidek Aq pawel@dawidek.net
157 under sponsorship of the FreeBSD Foundation.