sysv_ipc.c revision 1.26 1 1.26 pgoyette /* $NetBSD: sysv_ipc.c,v 1.26 2015/05/10 07:41:15 pgoyette 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.26 pgoyette __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.26 2015/05/10 07:41:15 pgoyette 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.26 pgoyette static int (*kern_sysvipc50_sysctl_p)(SYSCTLFN_ARGS)
63 1.26 pgoyette #ifdef COMPAT_50
64 1.26 pgoyette = sysctl_kern_sysvipc50;
65 1.26 pgoyette #else
66 1.26 pgoyette = NULL;
67 1.26 pgoyette #endif
68 1.26 pgoyette
69 1.26 pgoyette void
70 1.26 pgoyette sysvipc50_set_compat_sysctl(int (*compat_sysctl)(SYSCTLFN_PROTO))
71 1.26 pgoyette {
72 1.26 pgoyette
73 1.26 pgoyette kern_sysvipc50_sysctl_p = compat_sysctl;
74 1.26 pgoyette }
75 1.26 pgoyette
76 1.24 elad static kauth_listener_t sysvipc_listener = NULL;
77 1.1 cgd
78 1.24 elad static int
79 1.24 elad sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
80 1.24 elad void *arg0, void *arg1, void *arg2, void *arg3)
81 1.1 cgd {
82 1.12 mycroft mode_t mask;
83 1.17 elad int ismember = 0;
84 1.24 elad struct ipc_perm *perm;
85 1.24 elad int mode;
86 1.24 elad enum kauth_system_req req;
87 1.24 elad
88 1.24 elad req = (enum kauth_system_req)arg0;
89 1.24 elad
90 1.24 elad if (!(action == KAUTH_SYSTEM_SYSVIPC &&
91 1.24 elad req == KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS))
92 1.24 elad return KAUTH_RESULT_DEFER;
93 1.12 mycroft
94 1.24 elad perm = arg1;
95 1.24 elad mode = (int)(uintptr_t)arg2;
96 1.1 cgd
97 1.9 mycroft if (mode == IPC_M) {
98 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
99 1.17 elad kauth_cred_geteuid(cred) == perm->cuid)
100 1.24 elad return (KAUTH_RESULT_ALLOW);
101 1.24 elad return (KAUTH_RESULT_DEFER); /* EPERM */
102 1.1 cgd }
103 1.4 hpeyerl
104 1.12 mycroft mask = 0;
105 1.12 mycroft
106 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
107 1.17 elad kauth_cred_geteuid(cred) == perm->cuid) {
108 1.12 mycroft if (mode & IPC_R)
109 1.12 mycroft mask |= S_IRUSR;
110 1.12 mycroft if (mode & IPC_W)
111 1.12 mycroft mask |= S_IWUSR;
112 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
113 1.12 mycroft }
114 1.12 mycroft
115 1.17 elad if (kauth_cred_getegid(cred) == perm->gid ||
116 1.17 elad (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
117 1.17 elad kauth_cred_getegid(cred) == perm->cgid ||
118 1.17 elad (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
119 1.12 mycroft if (mode & IPC_R)
120 1.12 mycroft mask |= S_IRGRP;
121 1.12 mycroft if (mode & IPC_W)
122 1.12 mycroft mask |= S_IWGRP;
123 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
124 1.12 mycroft }
125 1.12 mycroft
126 1.12 mycroft if (mode & IPC_R)
127 1.12 mycroft mask |= S_IROTH;
128 1.12 mycroft if (mode & IPC_W)
129 1.12 mycroft mask |= S_IWOTH;
130 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
131 1.24 elad }
132 1.24 elad
133 1.24 elad /*
134 1.24 elad * Check for ipc permission
135 1.24 elad */
136 1.24 elad
137 1.24 elad int
138 1.24 elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
139 1.24 elad {
140 1.24 elad int error;
141 1.24 elad
142 1.24 elad error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
143 1.24 elad KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
144 1.24 elad if (error == 0)
145 1.24 elad return (0);
146 1.24 elad
147 1.24 elad /* Adjust EPERM and EACCES errors until there's a better way to do this. */
148 1.24 elad if (mode != IPC_M)
149 1.24 elad error = EACCES;
150 1.24 elad
151 1.24 elad return error;
152 1.24 elad }
153 1.24 elad
154 1.24 elad void
155 1.24 elad sysvipcinit(void)
156 1.24 elad {
157 1.24 elad
158 1.24 elad if (sysvipc_listener != NULL)
159 1.24 elad return;
160 1.24 elad
161 1.24 elad sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
162 1.24 elad sysvipc_listener_cb, NULL);
163 1.1 cgd }
164 1.18 christos
165 1.18 christos static int
166 1.18 christos sysctl_kern_sysvipc(SYSCTLFN_ARGS)
167 1.18 christos {
168 1.18 christos void *where = oldp;
169 1.23 rmind size_t sz, *sizep = oldlenp;
170 1.18 christos #ifdef SYSVMSG
171 1.18 christos struct msg_sysctl_info *msgsi = NULL;
172 1.18 christos #endif
173 1.18 christos #ifdef SYSVSEM
174 1.18 christos struct sem_sysctl_info *semsi = NULL;
175 1.18 christos #endif
176 1.18 christos #ifdef SYSVSHM
177 1.18 christos struct shm_sysctl_info *shmsi = NULL;
178 1.18 christos #endif
179 1.18 christos size_t infosize, dssize, tsize, buflen;
180 1.18 christos void *bf = NULL;
181 1.18 christos char *start;
182 1.18 christos int32_t nds;
183 1.18 christos int i, error, ret;
184 1.18 christos
185 1.26 pgoyette /*
186 1.26 pgoyette * If compat_sysv module has loaded the compat sysctl, call it. If
187 1.26 pgoyette * it handles the request completely (either success or error), just
188 1.26 pgoyette * return. Otherwise fallthrough to the non-compat_sysv sysctl code.
189 1.26 pgoyette */
190 1.26 pgoyette if (kern_sysvipc50_sysctl_p != NULL) {
191 1.26 pgoyette error = (*kern_sysvipc50_sysctl_p)(SYSCTLFN_CALL(rnode));
192 1.26 pgoyette if (error != EPASSTHROUGH)
193 1.26 pgoyette return error;
194 1.22 christos }
195 1.26 pgoyette
196 1.18 christos if (namelen != 1)
197 1.18 christos return EINVAL;
198 1.18 christos
199 1.18 christos start = where;
200 1.18 christos buflen = *sizep;
201 1.18 christos
202 1.18 christos switch (*name) {
203 1.18 christos case KERN_SYSVIPC_MSG_INFO:
204 1.18 christos #ifdef SYSVMSG
205 1.18 christos infosize = sizeof(msgsi->msginfo);
206 1.18 christos nds = msginfo.msgmni;
207 1.18 christos dssize = sizeof(msgsi->msgids[0]);
208 1.18 christos break;
209 1.18 christos #else
210 1.18 christos return EINVAL;
211 1.18 christos #endif
212 1.18 christos case KERN_SYSVIPC_SEM_INFO:
213 1.18 christos #ifdef SYSVSEM
214 1.18 christos infosize = sizeof(semsi->seminfo);
215 1.18 christos nds = seminfo.semmni;
216 1.18 christos dssize = sizeof(semsi->semids[0]);
217 1.18 christos break;
218 1.18 christos #else
219 1.18 christos return EINVAL;
220 1.18 christos #endif
221 1.18 christos case KERN_SYSVIPC_SHM_INFO:
222 1.18 christos #ifdef SYSVSHM
223 1.18 christos infosize = sizeof(shmsi->shminfo);
224 1.18 christos nds = shminfo.shmmni;
225 1.18 christos dssize = sizeof(shmsi->shmids[0]);
226 1.18 christos break;
227 1.18 christos #else
228 1.18 christos return EINVAL;
229 1.18 christos #endif
230 1.18 christos default:
231 1.18 christos return EINVAL;
232 1.18 christos }
233 1.18 christos /*
234 1.18 christos * Round infosize to 64 bit boundary if requesting more than just
235 1.18 christos * the info structure or getting the total data size.
236 1.18 christos */
237 1.18 christos if (where == NULL || *sizep > infosize)
238 1.18 christos infosize = roundup(infosize, sizeof(quad_t));
239 1.18 christos tsize = infosize + nds * dssize;
240 1.18 christos
241 1.18 christos /* Return just the total size required. */
242 1.18 christos if (where == NULL) {
243 1.18 christos *sizep = tsize;
244 1.18 christos return 0;
245 1.18 christos }
246 1.18 christos
247 1.18 christos /* Not enough room for even the info struct. */
248 1.18 christos if (buflen < infosize) {
249 1.18 christos *sizep = 0;
250 1.18 christos return ENOMEM;
251 1.18 christos }
252 1.23 rmind sz = min(tsize, buflen);
253 1.23 rmind bf = kmem_zalloc(sz, KM_SLEEP);
254 1.18 christos
255 1.18 christos switch (*name) {
256 1.18 christos #ifdef SYSVMSG
257 1.18 christos case KERN_SYSVIPC_MSG_INFO:
258 1.18 christos msgsi = (struct msg_sysctl_info *)bf;
259 1.18 christos msgsi->msginfo = msginfo;
260 1.18 christos break;
261 1.18 christos #endif
262 1.18 christos #ifdef SYSVSEM
263 1.18 christos case KERN_SYSVIPC_SEM_INFO:
264 1.18 christos semsi = (struct sem_sysctl_info *)bf;
265 1.18 christos semsi->seminfo = seminfo;
266 1.18 christos break;
267 1.18 christos #endif
268 1.18 christos #ifdef SYSVSHM
269 1.18 christos case KERN_SYSVIPC_SHM_INFO:
270 1.18 christos shmsi = (struct shm_sysctl_info *)bf;
271 1.18 christos shmsi->shminfo = shminfo;
272 1.18 christos break;
273 1.18 christos #endif
274 1.18 christos }
275 1.18 christos buflen -= infosize;
276 1.18 christos
277 1.18 christos ret = 0;
278 1.18 christos if (buflen > 0) {
279 1.18 christos /* Fill in the IPC data structures. */
280 1.18 christos for (i = 0; i < nds; i++) {
281 1.18 christos if (buflen < dssize) {
282 1.18 christos ret = ENOMEM;
283 1.18 christos break;
284 1.18 christos }
285 1.18 christos switch (*name) {
286 1.18 christos #ifdef SYSVMSG
287 1.18 christos case KERN_SYSVIPC_MSG_INFO:
288 1.20 ad mutex_enter(&msgmutex);
289 1.22 christos SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
290 1.20 ad mutex_exit(&msgmutex);
291 1.18 christos break;
292 1.18 christos #endif
293 1.18 christos #ifdef SYSVSEM
294 1.18 christos case KERN_SYSVIPC_SEM_INFO:
295 1.22 christos SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
296 1.18 christos break;
297 1.18 christos #endif
298 1.18 christos #ifdef SYSVSHM
299 1.18 christos case KERN_SYSVIPC_SHM_INFO:
300 1.22 christos SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
301 1.18 christos break;
302 1.18 christos #endif
303 1.18 christos }
304 1.18 christos buflen -= dssize;
305 1.18 christos }
306 1.18 christos }
307 1.18 christos *sizep -= buflen;
308 1.18 christos error = copyout(bf, start, *sizep);
309 1.18 christos /* If copyout succeeded, use return code set earlier. */
310 1.18 christos if (error == 0)
311 1.18 christos error = ret;
312 1.18 christos if (bf)
313 1.23 rmind kmem_free(bf, sz);
314 1.18 christos return error;
315 1.18 christos }
316 1.18 christos
317 1.18 christos SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
318 1.18 christos {
319 1.18 christos
320 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
321 1.18 christos CTLFLAG_PERMANENT,
322 1.18 christos CTLTYPE_NODE, "ipc",
323 1.18 christos SYSCTL_DESCR("SysV IPC options"),
324 1.18 christos NULL, 0, NULL, 0,
325 1.18 christos CTL_KERN, KERN_SYSVIPC, CTL_EOL);
326 1.18 christos
327 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
328 1.18 christos CTLFLAG_PERMANENT,
329 1.18 christos CTLTYPE_STRUCT, "sysvipc_info",
330 1.18 christos SYSCTL_DESCR("System V style IPC information"),
331 1.18 christos sysctl_kern_sysvipc, 0, NULL, 0,
332 1.18 christos CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
333 1.18 christos }
334