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