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 March 27, 2014 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/capsicum.h 45.Ft int 46.Fn cap_rights_limit "int fd" "const cap_rights_t *rights" 47.Sh DESCRIPTION 48When 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 60or 61.Xr socketpair 2 , 62it is assigned all capability rights. 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 ENOTCAPABLE 124The 125.Fa rights 126argument contains capability rights not present for the given file descriptor. 127Capability 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 151Support for capabilities and capabilities mode was developed as part of the 152.Tn TrustedBSD 153Project. 154.Sh AUTHORS 155This function was created by 156.An Pawel Jakub Dawidek Aq pawel@dawidek.net 157under sponsorship of the FreeBSD Foundation. 158