xref: /freebsd/lib/libcasper/services/cap_sysctl/cap_sysctl.3 (revision 1608c46ea4a10a65095ceff3a2d4a1b4b5b3d408)
1edd09a25SMariusz Zaborski.\" Copyright (c) 2018 Mariusz Zaborski <oshogbo@FreeBSD.org>
2edd09a25SMariusz Zaborski.\" All rights reserved.
3edd09a25SMariusz Zaborski.\"
4edd09a25SMariusz Zaborski.\" Redistribution and use in source and binary forms, with or without
5edd09a25SMariusz Zaborski.\" modification, are permitted provided that the following conditions
6edd09a25SMariusz Zaborski.\" are met:
7edd09a25SMariusz Zaborski.\" 1. Redistributions of source code must retain the above copyright
8edd09a25SMariusz Zaborski.\"    notice, this list of conditions and the following disclaimer.
9edd09a25SMariusz Zaborski.\" 2. Redistributions in binary form must reproduce the above copyright
10edd09a25SMariusz Zaborski.\"    notice, this list of conditions and the following disclaimer in the
11edd09a25SMariusz Zaborski.\"    documentation and/or other materials provided with the distribution.
12edd09a25SMariusz Zaborski.\"
13edd09a25SMariusz Zaborski.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
14edd09a25SMariusz Zaborski.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15edd09a25SMariusz Zaborski.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16edd09a25SMariusz Zaborski.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
17edd09a25SMariusz Zaborski.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18edd09a25SMariusz Zaborski.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19edd09a25SMariusz Zaborski.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20edd09a25SMariusz Zaborski.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21edd09a25SMariusz Zaborski.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22edd09a25SMariusz Zaborski.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23edd09a25SMariusz Zaborski.\" SUCH DAMAGE.
24edd09a25SMariusz Zaborski.\"
25edd09a25SMariusz Zaborski.\" $FreeBSD$
26edd09a25SMariusz Zaborski.\"
27*1608c46eSMark Johnston.Dd May 13, 2019
28edd09a25SMariusz Zaborski.Dt CAP_SYSCTL 3
29edd09a25SMariusz Zaborski.Os
30edd09a25SMariusz Zaborski.Sh NAME
31*1608c46eSMark Johnston.Nm cap_sysctl
32edd09a25SMariusz Zaborski.Nd "library for getting or setting system information in capability mode"
33edd09a25SMariusz Zaborski.Sh LIBRARY
34edd09a25SMariusz Zaborski.Lb libcap_sysctl
35edd09a25SMariusz Zaborski.Sh SYNOPSIS
36edd09a25SMariusz Zaborski.In libcasper.h
37edd09a25SMariusz Zaborski.In casper/cap_sysctl.h
38edd09a25SMariusz Zaborski.Ft int
39*1608c46eSMark Johnston.Fn cap_sysctl "cap_channel_t *chan" "const int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" "const void *newp" "size_t newlen"
40*1608c46eSMark Johnston.Ft int
41edd09a25SMariusz Zaborski.Fn cap_sysctlbyname "cap_channel_t *chan" "const char *name" "void *oldp" "size_t *oldlenp" "const void *newp" "size_t newlen"
42*1608c46eSMark Johnston.Ft int
43*1608c46eSMark Johnston.Fn cap_sysctlnametomib "cap_channel_t *chan" "const char *name" "int *mibp" "size_t *sizep"
44*1608c46eSMark Johnston.Ft void *
45*1608c46eSMark Johnston.Fn cap_sysctl_limit_init "cap_channel_t *chan"
46*1608c46eSMark Johnston.Ft void *
47*1608c46eSMark Johnston.Fn cap_sysctl_limit_name "void *limit" "const char *name" "int flags"
48*1608c46eSMark Johnston.Ft void *
49*1608c46eSMark Johnston.Fn cap_sysctl_limit_mib "void *limit" "int *mibp" "u_int miblen" "int flags"
50*1608c46eSMark Johnston.Ft int
51*1608c46eSMark Johnston.Fn cap_sysctl_limit "void *limit"
52edd09a25SMariusz Zaborski.Sh DESCRIPTION
53*1608c46eSMark JohnstonThe
54*1608c46eSMark Johnston.Fn cap_sysctl ,
55edd09a25SMariusz Zaborski.Fn cap_sysctlbyname
56*1608c46eSMark Johnstonand
57*1608c46eSMark Johnston.Fn cap_sysctlnametomib
58*1608c46eSMark Johnstonfunctions are equivalent to
59*1608c46eSMark Johnston.Xr sysctl 3 ,
60edd09a25SMariusz Zaborski.Xr sysctlbyname 3
61*1608c46eSMark Johnstonand
62*1608c46eSMark Johnston.Xr sysctlnametomib 3 ,
63*1608c46eSMark Johnstonexcept that they are implemented by the
64*1608c46eSMark Johnston.Ql system.sysctl
65*1608c46eSMark Johnston.Xr libcasper 3
66*1608c46eSMark Johnstonservice and require a corresponding
67*1608c46eSMark Johnston.Xr libcasper 3
68*1608c46eSMark Johnstoncapability.
69edd09a25SMariusz Zaborski.Sh LIMITS
70*1608c46eSMark JohnstonBy default, the
71*1608c46eSMark Johnston.Nm
72*1608c46eSMark Johnstoncapability provides unrestricted access to the sysctl namespace.
73*1608c46eSMark JohnstonApplications typically only require access to a small number of sysctl
74*1608c46eSMark Johnstonvariables; the
75*1608c46eSMark Johnston.Fn cap_sysctl_limit
76*1608c46eSMark Johnstoninterface can be used to restrict the sysctls that can be accessed using
77*1608c46eSMark Johnstonthe
78*1608c46eSMark Johnston.Nm
79*1608c46eSMark Johnstoncapability.
80*1608c46eSMark Johnston.Fn cap_sysctl_limit_init
81*1608c46eSMark Johnstonreturns an opaque limit handle used to store a list of permitted sysctls
82*1608c46eSMark Johnstonand access rights.
83*1608c46eSMark JohnstonRights are encoded using the following flags:
84*1608c46eSMark Johnston.Pp
85*1608c46eSMark Johnston.Bd -literal -offset indent -compact
86*1608c46eSMark JohnstonCAP_SYSCTL_READ		allow reads of the sysctl variable
87*1608c46eSMark JohnstonCAP_SYSCTL_WRITE        allow writes of the sysctl variable
88*1608c46eSMark JohnstonCAP_SYSCTL_RDWR         allow reads and writes of the sysctl variable
89*1608c46eSMark JohnstonCAP_RECURSIVE           permit access to any child of the sysctl variable
90*1608c46eSMark Johnston.Ed
91*1608c46eSMark Johnston.Pp
92edd09a25SMariusz ZaborskiThe
93*1608c46eSMark Johnston.Fn cap_sysctl_limit_name
94*1608c46eSMark Johnstonfunction adds the sysctl identified by
95*1608c46eSMark Johnston.Ar name
96*1608c46eSMark Johnstonto the limit list, and
97*1608c46eSMark Johnston.Fn cap_sysctl_limit_mib
98*1608c46eSMark Johnstonfunction adds the sysctl identified by
99*1608c46eSMark Johnston.Ar mibp
100*1608c46eSMark Johnstonto the limit list.
101*1608c46eSMark JohnstonThe access rights for the sysctl are specified in the
102*1608c46eSMark Johnston.Ar flags
103*1608c46eSMark Johnstonparameter; at least one of
104edd09a25SMariusz Zaborski.Dv CAP_SYSCTL_READ ,
105*1608c46eSMark Johnston.Dv CAP_SYSCTL_WRITE
106edd09a25SMariusz Zaborskiand
107edd09a25SMariusz Zaborski.Dv CAP_SYSCTL_RDWR
108*1608c46eSMark Johnstonmust be specified.
109*1608c46eSMark Johnston.Fn cap_sysctl_limit
110*1608c46eSMark Johnstonapplies a set of sysctl limits to the capability, denying access to sysctl
111*1608c46eSMark Johnstonvariables not belonging to the set.
112*1608c46eSMark Johnston.Pp
113*1608c46eSMark JohnstonOnce a set of limits is applied, subsequent calls to
114*1608c46eSMark Johnston.Fn cap_sysctl_limit
115*1608c46eSMark Johnstonwill fail unless the new set is a subset of the current set.
116*1608c46eSMark Johnston.Pp
117*1608c46eSMark Johnston.Fn cap_sysctlnametomib
118*1608c46eSMark Johnstonwill succeed so long as the named sysctl variable is present in the limit set,
119*1608c46eSMark Johnstonregardless of its access rights.
120*1608c46eSMark JohnstonWhen a sysctl variable name is added to a limit set, its MIB identifier is
121*1608c46eSMark Johnstonautomatically added to the set.
122edd09a25SMariusz Zaborski.Sh EXAMPLES
123*1608c46eSMark JohnstonThe following example first opens a capability to casper, uses this
124edd09a25SMariusz Zaborskicapability to create the
125edd09a25SMariusz Zaborski.Nm system.sysctl
126*1608c46eSMark Johnstoncasper service, and then uses the
127*1608c46eSMark Johnston.Nm
128*1608c46eSMark Johnstoncapability to get the value of
129edd09a25SMariusz Zaborski.Dv kern.trap_enotcap .
130edd09a25SMariusz Zaborski.Bd -literal
131edd09a25SMariusz Zaborskicap_channel_t *capcas, *capsysctl;
132edd09a25SMariusz Zaborskiconst char *name = "kern.trap_enotcap";
133*1608c46eSMark Johnstonvoid *limit;
134edd09a25SMariusz Zaborskiint value;
135edd09a25SMariusz Zaborskisize_t size;
136edd09a25SMariusz Zaborski
137edd09a25SMariusz Zaborski/* Open capability to Casper. */
138edd09a25SMariusz Zaborskicapcas = cap_init();
139edd09a25SMariusz Zaborskiif (capcas == NULL)
140edd09a25SMariusz Zaborski	err(1, "Unable to contact Casper");
141edd09a25SMariusz Zaborski
142edd09a25SMariusz Zaborski/* Enter capability mode sandbox. */
143edd09a25SMariusz Zaborskiif (cap_enter() < 0 && errno != ENOSYS)
144edd09a25SMariusz Zaborski	err(1, "Unable to enter capability mode");
145edd09a25SMariusz Zaborski
146edd09a25SMariusz Zaborski/* Use Casper capability to create capability to the system.sysctl service. */
147edd09a25SMariusz Zaborskicapsysctl = cap_service_open(capcas, "system.sysctl");
148edd09a25SMariusz Zaborskiif (capsysctl == NULL)
149edd09a25SMariusz Zaborski	err(1, "Unable to open system.sysctl service");
150edd09a25SMariusz Zaborski
151edd09a25SMariusz Zaborski/* Close Casper capability, we don't need it anymore. */
152edd09a25SMariusz Zaborskicap_close(capcas);
153edd09a25SMariusz Zaborski
154edd09a25SMariusz Zaborski/* Create limit for one MIB with read access only. */
155*1608c46eSMark Johnstonlimit = cap_sysctl_limit_init(capsysctl);
156*1608c46eSMark Johnston(void)cap_sysctl_limit_name(limit, name, CAP_SYSCTL_READ);
157edd09a25SMariusz Zaborski
158edd09a25SMariusz Zaborski/* Limit system.sysctl. */
159*1608c46eSMark Johnstonif (cap_sysctl_limit(limit) < 0)
160edd09a25SMariusz Zaborski	err(1, "Unable to set limits");
161edd09a25SMariusz Zaborski
162edd09a25SMariusz Zaborski/* Fetch value. */
163edd09a25SMariusz Zaborskiif (cap_sysctlbyname(capsysctl, name, &value, &size, NULL, 0) < 0)
164edd09a25SMariusz Zaborski	err(1, "Unable to get value of sysctl");
165edd09a25SMariusz Zaborski
166edd09a25SMariusz Zaborskiprintf("The value of %s is %d.\\n", name, value);
167edd09a25SMariusz Zaborski
168edd09a25SMariusz Zaborskicap_close(capsysctl);
169edd09a25SMariusz Zaborski.Ed
170edd09a25SMariusz Zaborski.Sh SEE ALSO
171edd09a25SMariusz Zaborski.Xr cap_enter 2 ,
172edd09a25SMariusz Zaborski.Xr err 3 ,
173*1608c46eSMark Johnston.Xr sysctl 3 ,
174edd09a25SMariusz Zaborski.Xr sysctlbyname 3 ,
175*1608c46eSMark Johnston.Xr sysctlnametomib 3 ,
176edd09a25SMariusz Zaborski.Xr capsicum 4 ,
177edd09a25SMariusz Zaborski.Xr nv 9
178edd09a25SMariusz Zaborski.Sh AUTHORS
179edd09a25SMariusz ZaborskiThe
180edd09a25SMariusz Zaborski.Nm cap_sysctl
181edd09a25SMariusz Zaborskiservice was implemented by
182edd09a25SMariusz Zaborski.An Pawel Jakub Dawidek Aq Mt pawel@dawidek.net
183edd09a25SMariusz Zaborskiunder sponsorship from the FreeBSD Foundation.
184edd09a25SMariusz Zaborski.Pp
185edd09a25SMariusz ZaborskiThis manual page was written by
186edd09a25SMariusz Zaborski.An Mariusz Zaborski Aq Mt oshogbo@FreeBSD.org .
187