1 /* $NetBSD: sysv_ipc.c,v 1.22 2009/01/19 19:39:41 christos 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.22 2009/01/19 19:39:41 christos Exp $"); 34 35 #include "opt_sysv.h" 36 #include "opt_compat_netbsd.h" 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 #ifdef COMPAT_50 59 #include <compat/sys/ipc.h> 60 #endif 61 62 /* 63 * Check for ipc permission 64 */ 65 66 int 67 ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode) 68 { 69 mode_t mask; 70 int ismember = 0; 71 72 if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0) 73 return (0); 74 75 if (mode == IPC_M) { 76 if (kauth_cred_geteuid(cred) == perm->uid || 77 kauth_cred_geteuid(cred) == perm->cuid) 78 return (0); 79 return (EPERM); 80 } 81 82 mask = 0; 83 84 if (kauth_cred_geteuid(cred) == perm->uid || 85 kauth_cred_geteuid(cred) == perm->cuid) { 86 if (mode & IPC_R) 87 mask |= S_IRUSR; 88 if (mode & IPC_W) 89 mask |= S_IWUSR; 90 return ((perm->mode & mask) == mask ? 0 : EACCES); 91 } 92 93 if (kauth_cred_getegid(cred) == perm->gid || 94 (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) || 95 kauth_cred_getegid(cred) == perm->cgid || 96 (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) { 97 if (mode & IPC_R) 98 mask |= S_IRGRP; 99 if (mode & IPC_W) 100 mask |= S_IWGRP; 101 return ((perm->mode & mask) == mask ? 0 : EACCES); 102 } 103 104 if (mode & IPC_R) 105 mask |= S_IROTH; 106 if (mode & IPC_W) 107 mask |= S_IWOTH; 108 return ((perm->mode & mask) == mask ? 0 : EACCES); 109 } 110 111 static int 112 sysctl_kern_sysvipc(SYSCTLFN_ARGS) 113 { 114 void *where = oldp; 115 size_t *sizep = oldlenp; 116 #ifdef SYSVMSG 117 struct msg_sysctl_info *msgsi = NULL; 118 #endif 119 #ifdef SYSVSEM 120 struct sem_sysctl_info *semsi = NULL; 121 #endif 122 #ifdef SYSVSHM 123 struct shm_sysctl_info *shmsi = NULL; 124 #endif 125 size_t infosize, dssize, tsize, buflen; 126 void *bf = NULL; 127 char *start; 128 int32_t nds; 129 int i, error, ret; 130 131 #ifdef COMPAT_50 132 switch ((error = sysctl_kern_sysvipc50(SYSCTLFN_CALL(rnode)))) { 133 case 0: 134 return 0; 135 case EPASSTHROUGH: 136 break; 137 default: 138 return error; 139 } 140 #endif 141 if (namelen != 1) 142 return EINVAL; 143 144 start = where; 145 buflen = *sizep; 146 147 switch (*name) { 148 case KERN_SYSVIPC_MSG_INFO: 149 #ifdef SYSVMSG 150 infosize = sizeof(msgsi->msginfo); 151 nds = msginfo.msgmni; 152 dssize = sizeof(msgsi->msgids[0]); 153 break; 154 #else 155 return EINVAL; 156 #endif 157 case KERN_SYSVIPC_SEM_INFO: 158 #ifdef SYSVSEM 159 infosize = sizeof(semsi->seminfo); 160 nds = seminfo.semmni; 161 dssize = sizeof(semsi->semids[0]); 162 break; 163 #else 164 return EINVAL; 165 #endif 166 case KERN_SYSVIPC_SHM_INFO: 167 #ifdef SYSVSHM 168 infosize = sizeof(shmsi->shminfo); 169 nds = shminfo.shmmni; 170 dssize = sizeof(shmsi->shmids[0]); 171 break; 172 #else 173 return EINVAL; 174 #endif 175 default: 176 return EINVAL; 177 } 178 /* 179 * Round infosize to 64 bit boundary if requesting more than just 180 * the info structure or getting the total data size. 181 */ 182 if (where == NULL || *sizep > infosize) 183 infosize = roundup(infosize, sizeof(quad_t)); 184 tsize = infosize + nds * dssize; 185 186 /* Return just the total size required. */ 187 if (where == NULL) { 188 *sizep = tsize; 189 return 0; 190 } 191 192 /* Not enough room for even the info struct. */ 193 if (buflen < infosize) { 194 *sizep = 0; 195 return ENOMEM; 196 } 197 bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK | M_ZERO); 198 199 switch (*name) { 200 #ifdef SYSVMSG 201 case KERN_SYSVIPC_MSG_INFO: 202 msgsi = (struct msg_sysctl_info *)bf; 203 msgsi->msginfo = msginfo; 204 break; 205 #endif 206 #ifdef SYSVSEM 207 case KERN_SYSVIPC_SEM_INFO: 208 semsi = (struct sem_sysctl_info *)bf; 209 semsi->seminfo = seminfo; 210 break; 211 #endif 212 #ifdef SYSVSHM 213 case KERN_SYSVIPC_SHM_INFO: 214 shmsi = (struct shm_sysctl_info *)bf; 215 shmsi->shminfo = shminfo; 216 break; 217 #endif 218 } 219 buflen -= infosize; 220 221 ret = 0; 222 if (buflen > 0) { 223 /* Fill in the IPC data structures. */ 224 for (i = 0; i < nds; i++) { 225 if (buflen < dssize) { 226 ret = ENOMEM; 227 break; 228 } 229 switch (*name) { 230 #ifdef SYSVMSG 231 case KERN_SYSVIPC_MSG_INFO: 232 mutex_enter(&msgmutex); 233 SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]); 234 mutex_exit(&msgmutex); 235 break; 236 #endif 237 #ifdef SYSVSEM 238 case KERN_SYSVIPC_SEM_INFO: 239 SYSCTL_FILL_SEM(sema[i], semsi->semids[i]); 240 break; 241 #endif 242 #ifdef SYSVSHM 243 case KERN_SYSVIPC_SHM_INFO: 244 SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]); 245 break; 246 #endif 247 } 248 buflen -= dssize; 249 } 250 } 251 *sizep -= buflen; 252 error = copyout(bf, start, *sizep); 253 /* If copyout succeeded, use return code set earlier. */ 254 if (error == 0) 255 error = ret; 256 if (bf) 257 free(bf, M_TEMP); 258 return error; 259 } 260 261 SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup") 262 { 263 sysctl_createv(clog, 0, NULL, NULL, 264 CTLFLAG_PERMANENT, 265 CTLTYPE_NODE, "kern", NULL, 266 NULL, 0, NULL, 0, 267 CTL_KERN, CTL_EOL); 268 269 sysctl_createv(clog, 0, NULL, NULL, 270 CTLFLAG_PERMANENT, 271 CTLTYPE_NODE, "ipc", 272 SYSCTL_DESCR("SysV IPC options"), 273 NULL, 0, NULL, 0, 274 CTL_KERN, KERN_SYSVIPC, CTL_EOL); 275 276 sysctl_createv(clog, 0, NULL, NULL, 277 CTLFLAG_PERMANENT, 278 CTLTYPE_STRUCT, "sysvipc_info", 279 SYSCTL_DESCR("System V style IPC information"), 280 sysctl_kern_sysvipc, 0, NULL, 0, 281 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL); 282 } 283