sysv_ipc.c revision 1.24.2.1 1 1.24.2.1 tls /* $NetBSD: sysv_ipc.c,v 1.24.2.1 2014/08/20 00:04:29 tls Exp $ */
2 1.7 cgd
3 1.13 mycroft /*-
4 1.20 ad * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
5 1.13 mycroft * All rights reserved.
6 1.13 mycroft *
7 1.13 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.13 mycroft * by Charles M. Hannum.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.6 hpeyerl * 2. Redistributions in binary form must reproduce the above copyright
16 1.6 hpeyerl * notice, this list of conditions and the following disclaimer in the
17 1.6 hpeyerl * documentation and/or other materials provided with the distribution.
18 1.1 cgd *
19 1.13 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.13 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.13 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.13 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.13 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.13 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.13 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.13 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.13 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.13 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.13 mycroft * POSSIBILITY OF SUCH DAMAGE.
30 1.1 cgd */
31 1.15 lukem
32 1.15 lukem #include <sys/cdefs.h>
33 1.24.2.1 tls __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.24.2.1 2014/08/20 00:04:29 tls Exp $");
34 1.18 christos
35 1.18 christos #include "opt_sysv.h"
36 1.22 christos #include "opt_compat_netbsd.h"
37 1.2 mycroft #include <sys/param.h>
38 1.2 mycroft #include <sys/kernel.h>
39 1.2 mycroft #include <sys/proc.h>
40 1.2 mycroft #include <sys/ipc.h>
41 1.18 christos #ifdef SYSVMSG
42 1.18 christos #include <sys/msg.h>
43 1.18 christos #endif
44 1.18 christos #ifdef SYSVSEM
45 1.18 christos #include <sys/sem.h>
46 1.18 christos #endif
47 1.18 christos #ifdef SYSVSHM
48 1.18 christos #include <sys/shm.h>
49 1.18 christos #endif
50 1.4 hpeyerl #include <sys/systm.h>
51 1.23 rmind #include <sys/kmem.h>
52 1.10 mycroft #include <sys/mount.h>
53 1.10 mycroft #include <sys/vnode.h>
54 1.12 mycroft #include <sys/stat.h>
55 1.18 christos #include <sys/sysctl.h>
56 1.17 elad #include <sys/kauth.h>
57 1.1 cgd
58 1.22 christos #ifdef COMPAT_50
59 1.22 christos #include <compat/sys/ipc.h>
60 1.22 christos #endif
61 1.22 christos
62 1.24 elad static kauth_listener_t sysvipc_listener = NULL;
63 1.1 cgd
64 1.24 elad static int
65 1.24 elad sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
66 1.24 elad void *arg0, void *arg1, void *arg2, void *arg3)
67 1.1 cgd {
68 1.12 mycroft mode_t mask;
69 1.17 elad int ismember = 0;
70 1.24 elad struct ipc_perm *perm;
71 1.24 elad int mode;
72 1.24 elad enum kauth_system_req req;
73 1.24 elad
74 1.24 elad req = (enum kauth_system_req)arg0;
75 1.24 elad
76 1.24 elad if (!(action == KAUTH_SYSTEM_SYSVIPC &&
77 1.24 elad req == KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS))
78 1.24 elad return KAUTH_RESULT_DEFER;
79 1.12 mycroft
80 1.24 elad perm = arg1;
81 1.24 elad mode = (int)(uintptr_t)arg2;
82 1.1 cgd
83 1.9 mycroft if (mode == IPC_M) {
84 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
85 1.17 elad kauth_cred_geteuid(cred) == perm->cuid)
86 1.24 elad return (KAUTH_RESULT_ALLOW);
87 1.24 elad return (KAUTH_RESULT_DEFER); /* EPERM */
88 1.1 cgd }
89 1.4 hpeyerl
90 1.12 mycroft mask = 0;
91 1.12 mycroft
92 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
93 1.17 elad kauth_cred_geteuid(cred) == perm->cuid) {
94 1.12 mycroft if (mode & IPC_R)
95 1.12 mycroft mask |= S_IRUSR;
96 1.12 mycroft if (mode & IPC_W)
97 1.12 mycroft mask |= S_IWUSR;
98 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
99 1.12 mycroft }
100 1.12 mycroft
101 1.17 elad if (kauth_cred_getegid(cred) == perm->gid ||
102 1.17 elad (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
103 1.17 elad kauth_cred_getegid(cred) == perm->cgid ||
104 1.17 elad (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
105 1.12 mycroft if (mode & IPC_R)
106 1.12 mycroft mask |= S_IRGRP;
107 1.12 mycroft if (mode & IPC_W)
108 1.12 mycroft mask |= S_IWGRP;
109 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
110 1.12 mycroft }
111 1.12 mycroft
112 1.12 mycroft if (mode & IPC_R)
113 1.12 mycroft mask |= S_IROTH;
114 1.12 mycroft if (mode & IPC_W)
115 1.12 mycroft mask |= S_IWOTH;
116 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
117 1.24 elad }
118 1.24 elad
119 1.24 elad /*
120 1.24 elad * Check for ipc permission
121 1.24 elad */
122 1.24 elad
123 1.24 elad int
124 1.24 elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
125 1.24 elad {
126 1.24 elad int error;
127 1.24 elad
128 1.24 elad error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
129 1.24 elad KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
130 1.24 elad if (error == 0)
131 1.24 elad return (0);
132 1.24 elad
133 1.24 elad /* Adjust EPERM and EACCES errors until there's a better way to do this. */
134 1.24 elad if (mode != IPC_M)
135 1.24 elad error = EACCES;
136 1.24 elad
137 1.24 elad return error;
138 1.24 elad }
139 1.24 elad
140 1.24 elad void
141 1.24 elad sysvipcinit(void)
142 1.24 elad {
143 1.24 elad
144 1.24 elad if (sysvipc_listener != NULL)
145 1.24 elad return;
146 1.24 elad
147 1.24 elad sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
148 1.24 elad sysvipc_listener_cb, NULL);
149 1.1 cgd }
150 1.18 christos
151 1.18 christos static int
152 1.18 christos sysctl_kern_sysvipc(SYSCTLFN_ARGS)
153 1.18 christos {
154 1.18 christos void *where = oldp;
155 1.23 rmind size_t sz, *sizep = oldlenp;
156 1.18 christos #ifdef SYSVMSG
157 1.18 christos struct msg_sysctl_info *msgsi = NULL;
158 1.18 christos #endif
159 1.18 christos #ifdef SYSVSEM
160 1.18 christos struct sem_sysctl_info *semsi = NULL;
161 1.18 christos #endif
162 1.18 christos #ifdef SYSVSHM
163 1.18 christos struct shm_sysctl_info *shmsi = NULL;
164 1.18 christos #endif
165 1.18 christos size_t infosize, dssize, tsize, buflen;
166 1.18 christos void *bf = NULL;
167 1.18 christos char *start;
168 1.18 christos int32_t nds;
169 1.18 christos int i, error, ret;
170 1.18 christos
171 1.22 christos #ifdef COMPAT_50
172 1.22 christos switch ((error = sysctl_kern_sysvipc50(SYSCTLFN_CALL(rnode)))) {
173 1.22 christos case 0:
174 1.22 christos return 0;
175 1.22 christos case EPASSTHROUGH:
176 1.22 christos break;
177 1.22 christos default:
178 1.22 christos return error;
179 1.22 christos }
180 1.22 christos #endif
181 1.18 christos if (namelen != 1)
182 1.18 christos return EINVAL;
183 1.18 christos
184 1.18 christos start = where;
185 1.18 christos buflen = *sizep;
186 1.18 christos
187 1.18 christos switch (*name) {
188 1.18 christos case KERN_SYSVIPC_MSG_INFO:
189 1.18 christos #ifdef SYSVMSG
190 1.18 christos infosize = sizeof(msgsi->msginfo);
191 1.18 christos nds = msginfo.msgmni;
192 1.18 christos dssize = sizeof(msgsi->msgids[0]);
193 1.18 christos break;
194 1.18 christos #else
195 1.18 christos return EINVAL;
196 1.18 christos #endif
197 1.18 christos case KERN_SYSVIPC_SEM_INFO:
198 1.18 christos #ifdef SYSVSEM
199 1.18 christos infosize = sizeof(semsi->seminfo);
200 1.18 christos nds = seminfo.semmni;
201 1.18 christos dssize = sizeof(semsi->semids[0]);
202 1.18 christos break;
203 1.18 christos #else
204 1.18 christos return EINVAL;
205 1.18 christos #endif
206 1.18 christos case KERN_SYSVIPC_SHM_INFO:
207 1.18 christos #ifdef SYSVSHM
208 1.18 christos infosize = sizeof(shmsi->shminfo);
209 1.18 christos nds = shminfo.shmmni;
210 1.18 christos dssize = sizeof(shmsi->shmids[0]);
211 1.18 christos break;
212 1.18 christos #else
213 1.18 christos return EINVAL;
214 1.18 christos #endif
215 1.18 christos default:
216 1.18 christos return EINVAL;
217 1.18 christos }
218 1.18 christos /*
219 1.18 christos * Round infosize to 64 bit boundary if requesting more than just
220 1.18 christos * the info structure or getting the total data size.
221 1.18 christos */
222 1.18 christos if (where == NULL || *sizep > infosize)
223 1.18 christos infosize = roundup(infosize, sizeof(quad_t));
224 1.18 christos tsize = infosize + nds * dssize;
225 1.18 christos
226 1.18 christos /* Return just the total size required. */
227 1.18 christos if (where == NULL) {
228 1.18 christos *sizep = tsize;
229 1.18 christos return 0;
230 1.18 christos }
231 1.18 christos
232 1.18 christos /* Not enough room for even the info struct. */
233 1.18 christos if (buflen < infosize) {
234 1.18 christos *sizep = 0;
235 1.18 christos return ENOMEM;
236 1.18 christos }
237 1.23 rmind sz = min(tsize, buflen);
238 1.23 rmind bf = kmem_zalloc(sz, KM_SLEEP);
239 1.18 christos
240 1.18 christos switch (*name) {
241 1.18 christos #ifdef SYSVMSG
242 1.18 christos case KERN_SYSVIPC_MSG_INFO:
243 1.18 christos msgsi = (struct msg_sysctl_info *)bf;
244 1.18 christos msgsi->msginfo = msginfo;
245 1.18 christos break;
246 1.18 christos #endif
247 1.18 christos #ifdef SYSVSEM
248 1.18 christos case KERN_SYSVIPC_SEM_INFO:
249 1.18 christos semsi = (struct sem_sysctl_info *)bf;
250 1.18 christos semsi->seminfo = seminfo;
251 1.18 christos break;
252 1.18 christos #endif
253 1.18 christos #ifdef SYSVSHM
254 1.18 christos case KERN_SYSVIPC_SHM_INFO:
255 1.18 christos shmsi = (struct shm_sysctl_info *)bf;
256 1.18 christos shmsi->shminfo = shminfo;
257 1.18 christos break;
258 1.18 christos #endif
259 1.18 christos }
260 1.18 christos buflen -= infosize;
261 1.18 christos
262 1.18 christos ret = 0;
263 1.18 christos if (buflen > 0) {
264 1.18 christos /* Fill in the IPC data structures. */
265 1.18 christos for (i = 0; i < nds; i++) {
266 1.18 christos if (buflen < dssize) {
267 1.18 christos ret = ENOMEM;
268 1.18 christos break;
269 1.18 christos }
270 1.18 christos switch (*name) {
271 1.18 christos #ifdef SYSVMSG
272 1.18 christos case KERN_SYSVIPC_MSG_INFO:
273 1.20 ad mutex_enter(&msgmutex);
274 1.22 christos SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
275 1.20 ad mutex_exit(&msgmutex);
276 1.18 christos break;
277 1.18 christos #endif
278 1.18 christos #ifdef SYSVSEM
279 1.18 christos case KERN_SYSVIPC_SEM_INFO:
280 1.22 christos SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
281 1.18 christos break;
282 1.18 christos #endif
283 1.18 christos #ifdef SYSVSHM
284 1.18 christos case KERN_SYSVIPC_SHM_INFO:
285 1.22 christos SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
286 1.18 christos break;
287 1.18 christos #endif
288 1.18 christos }
289 1.18 christos buflen -= dssize;
290 1.18 christos }
291 1.18 christos }
292 1.18 christos *sizep -= buflen;
293 1.18 christos error = copyout(bf, start, *sizep);
294 1.18 christos /* If copyout succeeded, use return code set earlier. */
295 1.18 christos if (error == 0)
296 1.18 christos error = ret;
297 1.18 christos if (bf)
298 1.23 rmind kmem_free(bf, sz);
299 1.18 christos return error;
300 1.18 christos }
301 1.18 christos
302 1.18 christos SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
303 1.18 christos {
304 1.18 christos
305 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
306 1.18 christos CTLFLAG_PERMANENT,
307 1.18 christos CTLTYPE_NODE, "ipc",
308 1.18 christos SYSCTL_DESCR("SysV IPC options"),
309 1.18 christos NULL, 0, NULL, 0,
310 1.18 christos CTL_KERN, KERN_SYSVIPC, CTL_EOL);
311 1.18 christos
312 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
313 1.18 christos CTLFLAG_PERMANENT,
314 1.18 christos CTLTYPE_STRUCT, "sysvipc_info",
315 1.18 christos SYSCTL_DESCR("System V style IPC information"),
316 1.18 christos sysctl_kern_sysvipc, 0, NULL, 0,
317 1.18 christos CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
318 1.18 christos }
319