sysv_ipc.c revision 1.20.44.1 1 1.20.44.1 yamt /* $NetBSD: sysv_ipc.c,v 1.20.44.1 2008/05/16 02:25:27 yamt 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.20.44.1 yamt __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.20.44.1 2008/05/16 02:25:27 yamt Exp $");
34 1.18 christos
35 1.18 christos #include "opt_sysv.h"
36 1.1 cgd
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.18 christos #include <sys/malloc.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.1 cgd /*
59 1.4 hpeyerl * Check for ipc permission
60 1.1 cgd */
61 1.1 cgd
62 1.4 hpeyerl int
63 1.17 elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
64 1.1 cgd {
65 1.12 mycroft mode_t mask;
66 1.17 elad int ismember = 0;
67 1.12 mycroft
68 1.19 elad if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0)
69 1.12 mycroft return (0);
70 1.1 cgd
71 1.9 mycroft if (mode == IPC_M) {
72 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
73 1.17 elad kauth_cred_geteuid(cred) == perm->cuid)
74 1.9 mycroft return (0);
75 1.9 mycroft return (EPERM);
76 1.1 cgd }
77 1.4 hpeyerl
78 1.12 mycroft mask = 0;
79 1.12 mycroft
80 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
81 1.17 elad kauth_cred_geteuid(cred) == perm->cuid) {
82 1.12 mycroft if (mode & IPC_R)
83 1.12 mycroft mask |= S_IRUSR;
84 1.12 mycroft if (mode & IPC_W)
85 1.12 mycroft mask |= S_IWUSR;
86 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
87 1.12 mycroft }
88 1.12 mycroft
89 1.17 elad if (kauth_cred_getegid(cred) == perm->gid ||
90 1.17 elad (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
91 1.17 elad kauth_cred_getegid(cred) == perm->cgid ||
92 1.17 elad (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
93 1.12 mycroft if (mode & IPC_R)
94 1.12 mycroft mask |= S_IRGRP;
95 1.12 mycroft if (mode & IPC_W)
96 1.12 mycroft mask |= S_IWGRP;
97 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
98 1.12 mycroft }
99 1.12 mycroft
100 1.12 mycroft if (mode & IPC_R)
101 1.12 mycroft mask |= S_IROTH;
102 1.12 mycroft if (mode & IPC_W)
103 1.12 mycroft mask |= S_IWOTH;
104 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
105 1.1 cgd }
106 1.18 christos
107 1.18 christos /*
108 1.18 christos * sysctl helper routine for kern.ipc.sysvipc_info subtree.
109 1.18 christos */
110 1.18 christos
111 1.18 christos #define FILL_PERM(src, dst) do { \
112 1.18 christos (dst)._key = (src)._key; \
113 1.18 christos (dst).uid = (src).uid; \
114 1.18 christos (dst).gid = (src).gid; \
115 1.18 christos (dst).cuid = (src).cuid; \
116 1.18 christos (dst).cgid = (src).cgid; \
117 1.18 christos (dst).mode = (src).mode; \
118 1.18 christos (dst)._seq = (src)._seq; \
119 1.18 christos } while (/*CONSTCOND*/ 0);
120 1.18 christos
121 1.18 christos #define FILL_MSG(src, dst) do { \
122 1.18 christos FILL_PERM((src).msg_perm, (dst).msg_perm); \
123 1.18 christos (dst).msg_qnum = (src).msg_qnum; \
124 1.18 christos (dst).msg_qbytes = (src).msg_qbytes; \
125 1.18 christos (dst)._msg_cbytes = (src)._msg_cbytes; \
126 1.18 christos (dst).msg_lspid = (src).msg_lspid; \
127 1.18 christos (dst).msg_lrpid = (src).msg_lrpid; \
128 1.18 christos (dst).msg_stime = (src).msg_stime; \
129 1.18 christos (dst).msg_rtime = (src).msg_rtime; \
130 1.18 christos (dst).msg_ctime = (src).msg_ctime; \
131 1.18 christos } while (/*CONSTCOND*/ 0)
132 1.18 christos
133 1.18 christos #define FILL_SEM(src, dst) do { \
134 1.18 christos FILL_PERM((src).sem_perm, (dst).sem_perm); \
135 1.18 christos (dst).sem_nsems = (src).sem_nsems; \
136 1.18 christos (dst).sem_otime = (src).sem_otime; \
137 1.18 christos (dst).sem_ctime = (src).sem_ctime; \
138 1.18 christos } while (/*CONSTCOND*/ 0)
139 1.18 christos
140 1.18 christos #define FILL_SHM(src, dst) do { \
141 1.18 christos FILL_PERM((src).shm_perm, (dst).shm_perm); \
142 1.18 christos (dst).shm_segsz = (src).shm_segsz; \
143 1.18 christos (dst).shm_lpid = (src).shm_lpid; \
144 1.18 christos (dst).shm_cpid = (src).shm_cpid; \
145 1.18 christos (dst).shm_atime = (src).shm_atime; \
146 1.18 christos (dst).shm_dtime = (src).shm_dtime; \
147 1.18 christos (dst).shm_ctime = (src).shm_ctime; \
148 1.18 christos (dst).shm_nattch = (src).shm_nattch; \
149 1.18 christos } while (/*CONSTCOND*/ 0)
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.18 christos size_t *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.18 christos if (namelen != 1)
172 1.18 christos return EINVAL;
173 1.18 christos
174 1.18 christos start = where;
175 1.18 christos buflen = *sizep;
176 1.18 christos
177 1.18 christos switch (*name) {
178 1.18 christos case KERN_SYSVIPC_MSG_INFO:
179 1.18 christos #ifdef SYSVMSG
180 1.18 christos infosize = sizeof(msgsi->msginfo);
181 1.18 christos nds = msginfo.msgmni;
182 1.18 christos dssize = sizeof(msgsi->msgids[0]);
183 1.18 christos break;
184 1.18 christos #else
185 1.18 christos return EINVAL;
186 1.18 christos #endif
187 1.18 christos case KERN_SYSVIPC_SEM_INFO:
188 1.18 christos #ifdef SYSVSEM
189 1.18 christos infosize = sizeof(semsi->seminfo);
190 1.18 christos nds = seminfo.semmni;
191 1.18 christos dssize = sizeof(semsi->semids[0]);
192 1.18 christos break;
193 1.18 christos #else
194 1.18 christos return EINVAL;
195 1.18 christos #endif
196 1.18 christos case KERN_SYSVIPC_SHM_INFO:
197 1.18 christos #ifdef SYSVSHM
198 1.18 christos infosize = sizeof(shmsi->shminfo);
199 1.18 christos nds = shminfo.shmmni;
200 1.18 christos dssize = sizeof(shmsi->shmids[0]);
201 1.18 christos break;
202 1.18 christos #else
203 1.18 christos return EINVAL;
204 1.18 christos #endif
205 1.18 christos default:
206 1.18 christos return EINVAL;
207 1.18 christos }
208 1.18 christos /*
209 1.18 christos * Round infosize to 64 bit boundary if requesting more than just
210 1.18 christos * the info structure or getting the total data size.
211 1.18 christos */
212 1.18 christos if (where == NULL || *sizep > infosize)
213 1.18 christos infosize = roundup(infosize, sizeof(quad_t));
214 1.18 christos tsize = infosize + nds * dssize;
215 1.18 christos
216 1.18 christos /* Return just the total size required. */
217 1.18 christos if (where == NULL) {
218 1.18 christos *sizep = tsize;
219 1.18 christos return 0;
220 1.18 christos }
221 1.18 christos
222 1.18 christos /* Not enough room for even the info struct. */
223 1.18 christos if (buflen < infosize) {
224 1.18 christos *sizep = 0;
225 1.18 christos return ENOMEM;
226 1.18 christos }
227 1.18 christos bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK | M_ZERO);
228 1.18 christos
229 1.18 christos switch (*name) {
230 1.18 christos #ifdef SYSVMSG
231 1.18 christos case KERN_SYSVIPC_MSG_INFO:
232 1.18 christos msgsi = (struct msg_sysctl_info *)bf;
233 1.18 christos msgsi->msginfo = msginfo;
234 1.18 christos break;
235 1.18 christos #endif
236 1.18 christos #ifdef SYSVSEM
237 1.18 christos case KERN_SYSVIPC_SEM_INFO:
238 1.18 christos semsi = (struct sem_sysctl_info *)bf;
239 1.18 christos semsi->seminfo = seminfo;
240 1.18 christos break;
241 1.18 christos #endif
242 1.18 christos #ifdef SYSVSHM
243 1.18 christos case KERN_SYSVIPC_SHM_INFO:
244 1.18 christos shmsi = (struct shm_sysctl_info *)bf;
245 1.18 christos shmsi->shminfo = shminfo;
246 1.18 christos break;
247 1.18 christos #endif
248 1.18 christos }
249 1.18 christos buflen -= infosize;
250 1.18 christos
251 1.18 christos ret = 0;
252 1.18 christos if (buflen > 0) {
253 1.18 christos /* Fill in the IPC data structures. */
254 1.18 christos for (i = 0; i < nds; i++) {
255 1.18 christos if (buflen < dssize) {
256 1.18 christos ret = ENOMEM;
257 1.18 christos break;
258 1.18 christos }
259 1.18 christos switch (*name) {
260 1.18 christos #ifdef SYSVMSG
261 1.18 christos case KERN_SYSVIPC_MSG_INFO:
262 1.20 ad mutex_enter(&msgmutex);
263 1.20 ad FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
264 1.20 ad mutex_exit(&msgmutex);
265 1.18 christos break;
266 1.18 christos #endif
267 1.18 christos #ifdef SYSVSEM
268 1.18 christos case KERN_SYSVIPC_SEM_INFO:
269 1.18 christos FILL_SEM(sema[i], semsi->semids[i]);
270 1.18 christos break;
271 1.18 christos #endif
272 1.18 christos #ifdef SYSVSHM
273 1.18 christos case KERN_SYSVIPC_SHM_INFO:
274 1.18 christos FILL_SHM(shmsegs[i], shmsi->shmids[i]);
275 1.18 christos break;
276 1.18 christos #endif
277 1.18 christos }
278 1.18 christos buflen -= dssize;
279 1.18 christos }
280 1.18 christos }
281 1.18 christos *sizep -= buflen;
282 1.18 christos error = copyout(bf, start, *sizep);
283 1.18 christos /* If copyout succeeded, use return code set earlier. */
284 1.18 christos if (error == 0)
285 1.18 christos error = ret;
286 1.18 christos if (bf)
287 1.18 christos free(bf, M_TEMP);
288 1.18 christos return error;
289 1.18 christos }
290 1.18 christos
291 1.18 christos #undef FILL_PERM
292 1.18 christos #undef FILL_MSG
293 1.18 christos #undef FILL_SEM
294 1.18 christos #undef FILL_SHM
295 1.18 christos
296 1.18 christos SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
297 1.18 christos {
298 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
299 1.18 christos CTLFLAG_PERMANENT,
300 1.18 christos CTLTYPE_NODE, "kern", NULL,
301 1.18 christos NULL, 0, NULL, 0,
302 1.18 christos CTL_KERN, CTL_EOL);
303 1.18 christos
304 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
305 1.18 christos CTLFLAG_PERMANENT,
306 1.18 christos CTLTYPE_NODE, "ipc",
307 1.18 christos SYSCTL_DESCR("SysV IPC options"),
308 1.18 christos NULL, 0, NULL, 0,
309 1.18 christos CTL_KERN, KERN_SYSVIPC, CTL_EOL);
310 1.18 christos
311 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
312 1.18 christos CTLFLAG_PERMANENT,
313 1.18 christos CTLTYPE_STRUCT, "sysvipc_info",
314 1.18 christos SYSCTL_DESCR("System V style IPC information"),
315 1.18 christos sysctl_kern_sysvipc, 0, NULL, 0,
316 1.18 christos CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
317 1.18 christos }
318