sysv_ipc.c revision 1.17.8.1 1 1.17.8.1 ad /* $NetBSD: sysv_ipc.c,v 1.17.8.1 2007/01/12 01:04:07 ad Exp $ */
2 1.7 cgd
3 1.13 mycroft /*-
4 1.13 mycroft * Copyright (c) 1998 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.6 hpeyerl * 3. All advertising materials mentioning features or use of this software
19 1.6 hpeyerl * must display the following acknowledgement:
20 1.14 christos * This product includes software developed by the NetBSD
21 1.14 christos * Foundation, Inc. and its contributors.
22 1.13 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.13 mycroft * contributors may be used to endorse or promote products derived
24 1.13 mycroft * from this software without specific prior written permission.
25 1.1 cgd *
26 1.13 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.13 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.13 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.13 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.13 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.13 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.13 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.13 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.13 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.13 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.13 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.1 cgd */
38 1.15 lukem
39 1.15 lukem #include <sys/cdefs.h>
40 1.17.8.1 ad __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.17.8.1 2007/01/12 01:04:07 ad Exp $");
41 1.17.8.1 ad
42 1.17.8.1 ad #include "opt_sysv.h"
43 1.1 cgd
44 1.2 mycroft #include <sys/param.h>
45 1.2 mycroft #include <sys/kernel.h>
46 1.2 mycroft #include <sys/proc.h>
47 1.2 mycroft #include <sys/ipc.h>
48 1.17.8.1 ad #ifdef SYSVMSG
49 1.17.8.1 ad #include <sys/msg.h>
50 1.17.8.1 ad #endif
51 1.17.8.1 ad #ifdef SYSVSEM
52 1.17.8.1 ad #include <sys/sem.h>
53 1.17.8.1 ad #endif
54 1.17.8.1 ad #ifdef SYSVSHM
55 1.17.8.1 ad #include <sys/shm.h>
56 1.17.8.1 ad #endif
57 1.4 hpeyerl #include <sys/systm.h>
58 1.17.8.1 ad #include <sys/malloc.h>
59 1.10 mycroft #include <sys/mount.h>
60 1.10 mycroft #include <sys/vnode.h>
61 1.12 mycroft #include <sys/stat.h>
62 1.17.8.1 ad #include <sys/sysctl.h>
63 1.17 elad #include <sys/kauth.h>
64 1.1 cgd
65 1.1 cgd /*
66 1.4 hpeyerl * Check for ipc permission
67 1.1 cgd */
68 1.1 cgd
69 1.4 hpeyerl int
70 1.17 elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
71 1.1 cgd {
72 1.12 mycroft mode_t mask;
73 1.17 elad int ismember = 0;
74 1.12 mycroft
75 1.17.8.1 ad if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0)
76 1.12 mycroft return (0);
77 1.1 cgd
78 1.9 mycroft if (mode == IPC_M) {
79 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
80 1.17 elad kauth_cred_geteuid(cred) == perm->cuid)
81 1.9 mycroft return (0);
82 1.9 mycroft return (EPERM);
83 1.1 cgd }
84 1.4 hpeyerl
85 1.12 mycroft mask = 0;
86 1.12 mycroft
87 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
88 1.17 elad kauth_cred_geteuid(cred) == perm->cuid) {
89 1.12 mycroft if (mode & IPC_R)
90 1.12 mycroft mask |= S_IRUSR;
91 1.12 mycroft if (mode & IPC_W)
92 1.12 mycroft mask |= S_IWUSR;
93 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
94 1.12 mycroft }
95 1.12 mycroft
96 1.17 elad if (kauth_cred_getegid(cred) == perm->gid ||
97 1.17 elad (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
98 1.17 elad kauth_cred_getegid(cred) == perm->cgid ||
99 1.17 elad (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
100 1.12 mycroft if (mode & IPC_R)
101 1.12 mycroft mask |= S_IRGRP;
102 1.12 mycroft if (mode & IPC_W)
103 1.12 mycroft mask |= S_IWGRP;
104 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
105 1.12 mycroft }
106 1.12 mycroft
107 1.12 mycroft if (mode & IPC_R)
108 1.12 mycroft mask |= S_IROTH;
109 1.12 mycroft if (mode & IPC_W)
110 1.12 mycroft mask |= S_IWOTH;
111 1.12 mycroft return ((perm->mode & mask) == mask ? 0 : EACCES);
112 1.1 cgd }
113 1.17.8.1 ad
114 1.17.8.1 ad /*
115 1.17.8.1 ad * sysctl helper routine for kern.ipc.sysvipc_info subtree.
116 1.17.8.1 ad */
117 1.17.8.1 ad
118 1.17.8.1 ad #define FILL_PERM(src, dst) do { \
119 1.17.8.1 ad (dst)._key = (src)._key; \
120 1.17.8.1 ad (dst).uid = (src).uid; \
121 1.17.8.1 ad (dst).gid = (src).gid; \
122 1.17.8.1 ad (dst).cuid = (src).cuid; \
123 1.17.8.1 ad (dst).cgid = (src).cgid; \
124 1.17.8.1 ad (dst).mode = (src).mode; \
125 1.17.8.1 ad (dst)._seq = (src)._seq; \
126 1.17.8.1 ad } while (/*CONSTCOND*/ 0);
127 1.17.8.1 ad
128 1.17.8.1 ad #define FILL_MSG(src, dst) do { \
129 1.17.8.1 ad FILL_PERM((src).msg_perm, (dst).msg_perm); \
130 1.17.8.1 ad (dst).msg_qnum = (src).msg_qnum; \
131 1.17.8.1 ad (dst).msg_qbytes = (src).msg_qbytes; \
132 1.17.8.1 ad (dst)._msg_cbytes = (src)._msg_cbytes; \
133 1.17.8.1 ad (dst).msg_lspid = (src).msg_lspid; \
134 1.17.8.1 ad (dst).msg_lrpid = (src).msg_lrpid; \
135 1.17.8.1 ad (dst).msg_stime = (src).msg_stime; \
136 1.17.8.1 ad (dst).msg_rtime = (src).msg_rtime; \
137 1.17.8.1 ad (dst).msg_ctime = (src).msg_ctime; \
138 1.17.8.1 ad } while (/*CONSTCOND*/ 0)
139 1.17.8.1 ad
140 1.17.8.1 ad #define FILL_SEM(src, dst) do { \
141 1.17.8.1 ad FILL_PERM((src).sem_perm, (dst).sem_perm); \
142 1.17.8.1 ad (dst).sem_nsems = (src).sem_nsems; \
143 1.17.8.1 ad (dst).sem_otime = (src).sem_otime; \
144 1.17.8.1 ad (dst).sem_ctime = (src).sem_ctime; \
145 1.17.8.1 ad } while (/*CONSTCOND*/ 0)
146 1.17.8.1 ad
147 1.17.8.1 ad #define FILL_SHM(src, dst) do { \
148 1.17.8.1 ad FILL_PERM((src).shm_perm, (dst).shm_perm); \
149 1.17.8.1 ad (dst).shm_segsz = (src).shm_segsz; \
150 1.17.8.1 ad (dst).shm_lpid = (src).shm_lpid; \
151 1.17.8.1 ad (dst).shm_cpid = (src).shm_cpid; \
152 1.17.8.1 ad (dst).shm_atime = (src).shm_atime; \
153 1.17.8.1 ad (dst).shm_dtime = (src).shm_dtime; \
154 1.17.8.1 ad (dst).shm_ctime = (src).shm_ctime; \
155 1.17.8.1 ad (dst).shm_nattch = (src).shm_nattch; \
156 1.17.8.1 ad } while (/*CONSTCOND*/ 0)
157 1.17.8.1 ad
158 1.17.8.1 ad static int
159 1.17.8.1 ad sysctl_kern_sysvipc(SYSCTLFN_ARGS)
160 1.17.8.1 ad {
161 1.17.8.1 ad void *where = oldp;
162 1.17.8.1 ad size_t *sizep = oldlenp;
163 1.17.8.1 ad #ifdef SYSVMSG
164 1.17.8.1 ad struct msg_sysctl_info *msgsi = NULL;
165 1.17.8.1 ad #endif
166 1.17.8.1 ad #ifdef SYSVSEM
167 1.17.8.1 ad struct sem_sysctl_info *semsi = NULL;
168 1.17.8.1 ad #endif
169 1.17.8.1 ad #ifdef SYSVSHM
170 1.17.8.1 ad struct shm_sysctl_info *shmsi = NULL;
171 1.17.8.1 ad #endif
172 1.17.8.1 ad size_t infosize, dssize, tsize, buflen;
173 1.17.8.1 ad void *bf = NULL;
174 1.17.8.1 ad char *start;
175 1.17.8.1 ad int32_t nds;
176 1.17.8.1 ad int i, error, ret;
177 1.17.8.1 ad
178 1.17.8.1 ad if (namelen != 1)
179 1.17.8.1 ad return EINVAL;
180 1.17.8.1 ad
181 1.17.8.1 ad start = where;
182 1.17.8.1 ad buflen = *sizep;
183 1.17.8.1 ad
184 1.17.8.1 ad switch (*name) {
185 1.17.8.1 ad case KERN_SYSVIPC_MSG_INFO:
186 1.17.8.1 ad #ifdef SYSVMSG
187 1.17.8.1 ad infosize = sizeof(msgsi->msginfo);
188 1.17.8.1 ad nds = msginfo.msgmni;
189 1.17.8.1 ad dssize = sizeof(msgsi->msgids[0]);
190 1.17.8.1 ad break;
191 1.17.8.1 ad #else
192 1.17.8.1 ad return EINVAL;
193 1.17.8.1 ad #endif
194 1.17.8.1 ad case KERN_SYSVIPC_SEM_INFO:
195 1.17.8.1 ad #ifdef SYSVSEM
196 1.17.8.1 ad infosize = sizeof(semsi->seminfo);
197 1.17.8.1 ad nds = seminfo.semmni;
198 1.17.8.1 ad dssize = sizeof(semsi->semids[0]);
199 1.17.8.1 ad break;
200 1.17.8.1 ad #else
201 1.17.8.1 ad return EINVAL;
202 1.17.8.1 ad #endif
203 1.17.8.1 ad case KERN_SYSVIPC_SHM_INFO:
204 1.17.8.1 ad #ifdef SYSVSHM
205 1.17.8.1 ad infosize = sizeof(shmsi->shminfo);
206 1.17.8.1 ad nds = shminfo.shmmni;
207 1.17.8.1 ad dssize = sizeof(shmsi->shmids[0]);
208 1.17.8.1 ad break;
209 1.17.8.1 ad #else
210 1.17.8.1 ad return EINVAL;
211 1.17.8.1 ad #endif
212 1.17.8.1 ad default:
213 1.17.8.1 ad return EINVAL;
214 1.17.8.1 ad }
215 1.17.8.1 ad /*
216 1.17.8.1 ad * Round infosize to 64 bit boundary if requesting more than just
217 1.17.8.1 ad * the info structure or getting the total data size.
218 1.17.8.1 ad */
219 1.17.8.1 ad if (where == NULL || *sizep > infosize)
220 1.17.8.1 ad infosize = roundup(infosize, sizeof(quad_t));
221 1.17.8.1 ad tsize = infosize + nds * dssize;
222 1.17.8.1 ad
223 1.17.8.1 ad /* Return just the total size required. */
224 1.17.8.1 ad if (where == NULL) {
225 1.17.8.1 ad *sizep = tsize;
226 1.17.8.1 ad return 0;
227 1.17.8.1 ad }
228 1.17.8.1 ad
229 1.17.8.1 ad /* Not enough room for even the info struct. */
230 1.17.8.1 ad if (buflen < infosize) {
231 1.17.8.1 ad *sizep = 0;
232 1.17.8.1 ad return ENOMEM;
233 1.17.8.1 ad }
234 1.17.8.1 ad bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK | M_ZERO);
235 1.17.8.1 ad
236 1.17.8.1 ad switch (*name) {
237 1.17.8.1 ad #ifdef SYSVMSG
238 1.17.8.1 ad case KERN_SYSVIPC_MSG_INFO:
239 1.17.8.1 ad msgsi = (struct msg_sysctl_info *)bf;
240 1.17.8.1 ad msgsi->msginfo = msginfo;
241 1.17.8.1 ad break;
242 1.17.8.1 ad #endif
243 1.17.8.1 ad #ifdef SYSVSEM
244 1.17.8.1 ad case KERN_SYSVIPC_SEM_INFO:
245 1.17.8.1 ad semsi = (struct sem_sysctl_info *)bf;
246 1.17.8.1 ad semsi->seminfo = seminfo;
247 1.17.8.1 ad break;
248 1.17.8.1 ad #endif
249 1.17.8.1 ad #ifdef SYSVSHM
250 1.17.8.1 ad case KERN_SYSVIPC_SHM_INFO:
251 1.17.8.1 ad shmsi = (struct shm_sysctl_info *)bf;
252 1.17.8.1 ad shmsi->shminfo = shminfo;
253 1.17.8.1 ad break;
254 1.17.8.1 ad #endif
255 1.17.8.1 ad }
256 1.17.8.1 ad buflen -= infosize;
257 1.17.8.1 ad
258 1.17.8.1 ad ret = 0;
259 1.17.8.1 ad if (buflen > 0) {
260 1.17.8.1 ad /* Fill in the IPC data structures. */
261 1.17.8.1 ad for (i = 0; i < nds; i++) {
262 1.17.8.1 ad if (buflen < dssize) {
263 1.17.8.1 ad ret = ENOMEM;
264 1.17.8.1 ad break;
265 1.17.8.1 ad }
266 1.17.8.1 ad switch (*name) {
267 1.17.8.1 ad #ifdef SYSVMSG
268 1.17.8.1 ad case KERN_SYSVIPC_MSG_INFO:
269 1.17.8.1 ad FILL_MSG(msqids[i], msgsi->msgids[i]);
270 1.17.8.1 ad break;
271 1.17.8.1 ad #endif
272 1.17.8.1 ad #ifdef SYSVSEM
273 1.17.8.1 ad case KERN_SYSVIPC_SEM_INFO:
274 1.17.8.1 ad FILL_SEM(sema[i], semsi->semids[i]);
275 1.17.8.1 ad break;
276 1.17.8.1 ad #endif
277 1.17.8.1 ad #ifdef SYSVSHM
278 1.17.8.1 ad case KERN_SYSVIPC_SHM_INFO:
279 1.17.8.1 ad FILL_SHM(shmsegs[i], shmsi->shmids[i]);
280 1.17.8.1 ad break;
281 1.17.8.1 ad #endif
282 1.17.8.1 ad }
283 1.17.8.1 ad buflen -= dssize;
284 1.17.8.1 ad }
285 1.17.8.1 ad }
286 1.17.8.1 ad *sizep -= buflen;
287 1.17.8.1 ad error = copyout(bf, start, *sizep);
288 1.17.8.1 ad /* If copyout succeeded, use return code set earlier. */
289 1.17.8.1 ad if (error == 0)
290 1.17.8.1 ad error = ret;
291 1.17.8.1 ad if (bf)
292 1.17.8.1 ad free(bf, M_TEMP);
293 1.17.8.1 ad return error;
294 1.17.8.1 ad }
295 1.17.8.1 ad
296 1.17.8.1 ad #undef FILL_PERM
297 1.17.8.1 ad #undef FILL_MSG
298 1.17.8.1 ad #undef FILL_SEM
299 1.17.8.1 ad #undef FILL_SHM
300 1.17.8.1 ad
301 1.17.8.1 ad SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
302 1.17.8.1 ad {
303 1.17.8.1 ad sysctl_createv(clog, 0, NULL, NULL,
304 1.17.8.1 ad CTLFLAG_PERMANENT,
305 1.17.8.1 ad CTLTYPE_NODE, "kern", NULL,
306 1.17.8.1 ad NULL, 0, NULL, 0,
307 1.17.8.1 ad CTL_KERN, CTL_EOL);
308 1.17.8.1 ad
309 1.17.8.1 ad sysctl_createv(clog, 0, NULL, NULL,
310 1.17.8.1 ad CTLFLAG_PERMANENT,
311 1.17.8.1 ad CTLTYPE_NODE, "ipc",
312 1.17.8.1 ad SYSCTL_DESCR("SysV IPC options"),
313 1.17.8.1 ad NULL, 0, NULL, 0,
314 1.17.8.1 ad CTL_KERN, KERN_SYSVIPC, CTL_EOL);
315 1.17.8.1 ad
316 1.17.8.1 ad sysctl_createv(clog, 0, NULL, NULL,
317 1.17.8.1 ad CTLFLAG_PERMANENT,
318 1.17.8.1 ad CTLTYPE_STRUCT, "sysvipc_info",
319 1.17.8.1 ad SYSCTL_DESCR("System V style IPC information"),
320 1.17.8.1 ad sysctl_kern_sysvipc, 0, NULL, 0,
321 1.17.8.1 ad CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
322 1.17.8.1 ad }
323