]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/sysv_sem.c
xnu-2422.115.4.tar.gz
[apple/xnu.git] / bsd / kern / sysv_sem.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Implementation of SVID semaphores
30 *
31 * Author: Daniel Boulet
32 *
33 * This software is provided ``AS IS'' without any warranties of any kind.
34 */
9bccf70c
A
35/*
36 * John Bellardo modified the implementation for Darwin. 12/2000
37 */
2d21ac55
A
38/*
39 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
40 * support for mandatory and extensible security protections. This notice
41 * is included in support of clause 2.2 (b) of the Apple Public License,
42 * Version 2.0.
43 * Copyright (c) 2005-2006 SPARTA, Inc.
44 */
1c79356b
A
45
46#include <sys/param.h>
47#include <sys/systm.h>
1c79356b 48#include <sys/kernel.h>
91447636
A
49#include <sys/proc_internal.h>
50#include <sys/kauth.h>
51#include <sys/sem_internal.h>
9bccf70c 52#include <sys/malloc.h>
91447636
A
53#include <mach/mach_types.h>
54
9bccf70c 55#include <sys/filedesc.h>
91447636 56#include <sys/file_internal.h>
55e303ae 57#include <sys/sysctl.h>
91447636
A
58#include <sys/ipcs.h>
59#include <sys/sysent.h>
60#include <sys/sysproto.h>
2d21ac55
A
61#if CONFIG_MACF
62#include <security/mac_framework.h>
63#endif
9bccf70c 64
b0d623f7 65#include <security/audit/audit.h>
e5568f75 66
2d21ac55
A
67#if SYSV_SEM
68
9bccf70c
A
69
70/* Uncomment this line to see the debugging output */
71/* #define SEM_DEBUG */
72
2d21ac55
A
73/* Uncomment this line to see MAC debugging output. */
74/* #define MAC_DEBUG */
75#if CONFIG_MACF_DEBUG
76#define MPRINTF(a) printf(a)
77#else
78#define MPRINTF(a)
79#endif
80
91447636 81#define M_SYSVSEM M_TEMP
9bccf70c 82
1c79356b 83
9bccf70c
A
84/* Hard system limits to avoid resource starvation / DOS attacks.
85 * These are not needed if we can make the semaphore pages swappable.
86 */
87static struct seminfo limitseminfo = {
88 SEMMAP, /* # of entries in semaphore map */
89 SEMMNI, /* # of semaphore identifiers */
90 SEMMNS, /* # of semaphores in system */
91 SEMMNU, /* # of undo structures in system */
92 SEMMSL, /* max # of semaphores per id */
93 SEMOPM, /* max # of operations per semop call */
94 SEMUME, /* max # of undo entries per process */
95 SEMUSZ, /* size in bytes of undo structure */
96 SEMVMX, /* semaphore maximum value */
97 SEMAEM /* adjust on exit max value */
98};
99
100/* Current system allocations. We use this structure to track how many
101 * resources we have allocated so far. This way we can set large hard limits
102 * and not allocate the memory for them up front.
103 */
104struct seminfo seminfo = {
105 SEMMAP, /* Unused, # of entries in semaphore map */
106 0, /* # of semaphore identifiers */
107 0, /* # of semaphores in system */
108 0, /* # of undo entries in system */
109 SEMMSL, /* max # of semaphores per id */
110 SEMOPM, /* max # of operations per semop call */
111 SEMUME, /* max # of undo entries per process */
112 SEMUSZ, /* size in bytes of undo structure */
113 SEMVMX, /* semaphore maximum value */
114 SEMAEM /* adjust on exit max value */
115};
116
1c79356b 117
2d21ac55
A
118static int semu_alloc(struct proc *p);
119static int semundo_adjust(struct proc *p, int *supidx,
91447636
A
120 int semid, int semnum, int adjval);
121static void semundo_clear(int semid, int semnum);
9bccf70c 122
1c79356b
A
123/* XXX casting to (sy_call_t *) is bogus, as usual. */
124static sy_call_t *semcalls[] = {
9bccf70c 125 (sy_call_t *)semctl, (sy_call_t *)semget,
37839358 126 (sy_call_t *)semop
1c79356b
A
127};
128
91447636 129static int semtot = 0; /* # of used semaphores */
2d21ac55 130struct semid_kernel *sema = NULL; /* semaphore id pool */
91447636 131struct sem *sem_pool = NULL; /* semaphore pool */
2d21ac55 132static int semu_list_idx = -1; /* active undo structures */
91447636 133struct sem_undo *semu = NULL; /* semaphore undo pool */
1c79356b 134
1c79356b 135
91447636
A
136void sysv_sem_lock_init(void);
137static lck_grp_t *sysv_sem_subsys_lck_grp;
138static lck_grp_attr_t *sysv_sem_subsys_lck_grp_attr;
139static lck_attr_t *sysv_sem_subsys_lck_attr;
140static lck_mtx_t sysv_sem_subsys_mutex;
141
142#define SYSV_SEM_SUBSYS_LOCK() lck_mtx_lock(&sysv_sem_subsys_mutex)
143#define SYSV_SEM_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_sem_subsys_mutex)
144
145
146__private_extern__ void
147sysv_sem_lock_init( void )
148{
149
150 sysv_sem_subsys_lck_grp_attr = lck_grp_attr_alloc_init();
91447636 151
0c530ab8 152 sysv_sem_subsys_lck_grp = lck_grp_alloc_init("sysv_sem_subsys_lock", sysv_sem_subsys_lck_grp_attr);
91447636
A
153
154 sysv_sem_subsys_lck_attr = lck_attr_alloc_init();
91447636
A
155 lck_mtx_init(&sysv_sem_subsys_mutex, sysv_sem_subsys_lck_grp, sysv_sem_subsys_lck_attr);
156}
157
158static __inline__ user_time_t
159sysv_semtime(void)
160{
161 struct timeval tv;
162 microtime(&tv);
163 return (tv.tv_sec);
164}
165
166/*
167 * XXX conversion of internal user_time_t to external tume_t loses
168 * XXX precision; not an issue for us now, since we are only ever
169 * XXX setting 32 bits worth of time into it.
170 *
171 * pad field contents are not moved correspondingly; contents will be lost
172 *
173 * NOTE: Source and target may *NOT* overlap! (target is smaller)
174 */
175static void
b0d623f7 176semid_ds_kernelto32(struct user_semid_ds *in, struct user32_semid_ds *out)
1c79356b 177{
91447636 178 out->sem_perm = in->sem_perm;
b0d623f7
A
179 out->sem_base = CAST_DOWN_EXPLICIT(__int32_t,in->sem_base);
180 out->sem_nsems = in->sem_nsems;
181 out->sem_otime = in->sem_otime; /* XXX loses precision */
182 out->sem_ctime = in->sem_ctime; /* XXX loses precision */
183}
184
185static void
186semid_ds_kernelto64(struct user_semid_ds *in, struct user64_semid_ds *out)
187{
188 out->sem_perm = in->sem_perm;
189 out->sem_base = CAST_DOWN_EXPLICIT(__int32_t,in->sem_base);
91447636
A
190 out->sem_nsems = in->sem_nsems;
191 out->sem_otime = in->sem_otime; /* XXX loses precision */
192 out->sem_ctime = in->sem_ctime; /* XXX loses precision */
1c79356b
A
193}
194
91447636
A
195/*
196 * pad field contents are not moved correspondingly; contents will be lost
197 *
198 * NOTE: Source and target may are permitted to overlap! (source is smaller);
199 * this works because we copy fields in order from the end of the struct to
200 * the beginning.
201 *
202 * XXX use CAST_USER_ADDR_T() for lack of a CAST_USER_TIME_T(); net effect
203 * XXX is the same.
204 */
205static void
b0d623f7 206semid_ds_32tokernel(struct user32_semid_ds *in, struct user_semid_ds *out)
91447636
A
207{
208 out->sem_ctime = in->sem_ctime;
209 out->sem_otime = in->sem_otime;
210 out->sem_nsems = in->sem_nsems;
b0d623f7
A
211 out->sem_base = (void *)(uintptr_t)in->sem_base;
212 out->sem_perm = in->sem_perm;
213}
214
215static void
216semid_ds_64tokernel(struct user64_semid_ds *in, struct user_semid_ds *out)
217{
218 out->sem_ctime = in->sem_ctime;
219 out->sem_otime = in->sem_otime;
220 out->sem_nsems = in->sem_nsems;
221 out->sem_base = (void *)(uintptr_t)in->sem_base;
91447636
A
222 out->sem_perm = in->sem_perm;
223}
224
225
1c79356b 226/*
b0d623f7
A
227 * semsys
228 *
229 * Entry point for all SEM calls: semctl, semget, semop
230 *
231 * Parameters: p Process requesting the call
232 * uap User argument descriptor (see below)
233 * retval Return value of the selected sem call
234 *
235 * Indirect parameters: uap->which sem call to invoke (index in array of sem calls)
236 * uap->a2 User argument descriptor
237 *
238 * Returns: 0 Success
239 * !0 Not success
240 *
241 * Implicit returns: retval Return value of the selected sem call
242 *
243 * DEPRECATED: This interface should not be used to call the other SEM
244 * functions (semctl, semget, semop). The correct usage is
245 * to call the other SEM functions directly.
9bccf70c 246 *
1c79356b
A
247 */
248int
b0d623f7 249semsys(struct proc *p, struct semsys_args *uap, int32_t *retval)
1c79356b
A
250{
251
9bccf70c 252 /* The individual calls handling the locking now */
1c79356b
A
253
254 if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
255 return (EINVAL);
9bccf70c 256 return ((*semcalls[uap->which])(p, &uap->a2, retval));
1c79356b
A
257}
258
91447636
A
259/*
260 * Expand the semu array to the given capacity. If the expansion fails
9bccf70c
A
261 * return 0, otherwise return 1.
262 *
263 * Assumes we already have the subsystem lock.
264 */
265static int
91447636 266grow_semu_array(int newSize)
9bccf70c 267{
91447636 268 register int i;
9bccf70c 269 register struct sem_undo *newSemu;
91447636 270
9bccf70c 271 if (newSize <= seminfo.semmnu)
91447636 272 return 1;
9bccf70c
A
273 if (newSize > limitseminfo.semmnu) /* enforce hard limit */
274 {
275#ifdef SEM_DEBUG
276 printf("undo structure hard limit of %d reached, requested %d\n",
277 limitseminfo.semmnu, newSize);
278#endif
279 return 0;
280 }
281 newSize = (newSize/SEMMNU_INC + 1) * SEMMNU_INC;
282 newSize = newSize > limitseminfo.semmnu ? limitseminfo.semmnu : newSize;
283
284#ifdef SEM_DEBUG
285 printf("growing semu[] from %d to %d\n", seminfo.semmnu, newSize);
286#endif
3a60a9f5
A
287 MALLOC(newSemu, struct sem_undo *, sizeof (struct sem_undo) * newSize,
288 M_SYSVSEM, M_WAITOK | M_ZERO);
9bccf70c
A
289 if (NULL == newSemu)
290 {
291#ifdef SEM_DEBUG
292 printf("allocation failed. no changes made.\n");
293#endif
294 return 0;
295 }
296
3a60a9f5 297 /* copy the old data to the new array */
9bccf70c
A
298 for (i = 0; i < seminfo.semmnu; i++)
299 {
300 newSemu[i] = semu[i];
9bccf70c 301 }
3a60a9f5
A
302 /*
303 * The new elements (from newSemu[i] to newSemu[newSize-1]) have their
304 * "un_proc" set to 0 (i.e. NULL) by the M_ZERO flag to MALLOC() above,
305 * so they're already marked as "not in use".
306 */
9bccf70c
A
307
308 /* Clean up the old array */
309 if (semu)
310 FREE(semu, M_SYSVSEM);
311
312 semu = newSemu;
313 seminfo.semmnu = newSize;
314#ifdef SEM_DEBUG
315 printf("expansion successful\n");
316#endif
317 return 1;
318}
319
320/*
321 * Expand the sema array to the given capacity. If the expansion fails
322 * we return 0, otherwise we return 1.
323 *
324 * Assumes we already have the subsystem lock.
325 */
326static int
91447636 327grow_sema_array(int newSize)
9bccf70c 328{
2d21ac55 329 register struct semid_kernel *newSema;
9bccf70c
A
330 register int i;
331
332 if (newSize <= seminfo.semmni)
333 return 0;
334 if (newSize > limitseminfo.semmni) /* enforce hard limit */
335 {
336#ifdef SEM_DEBUG
337 printf("identifier hard limit of %d reached, requested %d\n",
338 limitseminfo.semmni, newSize);
339#endif
340 return 0;
341 }
342 newSize = (newSize/SEMMNI_INC + 1) * SEMMNI_INC;
343 newSize = newSize > limitseminfo.semmni ? limitseminfo.semmni : newSize;
344
345#ifdef SEM_DEBUG
346 printf("growing sema[] from %d to %d\n", seminfo.semmni, newSize);
347#endif
2d21ac55
A
348 MALLOC(newSema, struct semid_kernel *,
349 sizeof (struct semid_kernel) * newSize,
3a60a9f5 350 M_SYSVSEM, M_WAITOK | M_ZERO);
9bccf70c
A
351 if (NULL == newSema)
352 {
353#ifdef SEM_DEBUG
354 printf("allocation failed. no changes made.\n");
355#endif
356 return 0;
357 }
358
3a60a9f5 359 /* copy over the old ids */
9bccf70c
A
360 for (i = 0; i < seminfo.semmni; i++)
361 {
362 newSema[i] = sema[i];
363 /* This is a hack. What we really want to be able to
364 * do is change the value a process is waiting on
365 * without waking it up, but I don't know how to do
366 * this with the existing code, so we wake up the
367 * process and let it do a lot of work to determine the
368 * semaphore set is really not available yet, and then
2d21ac55 369 * sleep on the correct, reallocated semid_kernel pointer.
9bccf70c 370 */
2d21ac55 371 if (sema[i].u.sem_perm.mode & SEM_ALLOC)
9bccf70c
A
372 wakeup((caddr_t)&sema[i]);
373 }
2d21ac55
A
374
375#if CONFIG_MACF
376 for (i = seminfo.semmni; i < newSize; i++)
377 {
378 mac_sysvsem_label_init(&newSema[i]);
379 }
380#endif
381
3a60a9f5
A
382 /*
383 * The new elements (from newSema[i] to newSema[newSize-1]) have their
384 * "sem_base" and "sem_perm.mode" set to 0 (i.e. NULL) by the M_ZERO
385 * flag to MALLOC() above, so they're already marked as "not in use".
386 */
9bccf70c
A
387
388 /* Clean up the old array */
389 if (sema)
390 FREE(sema, M_SYSVSEM);
391
392 sema = newSema;
393 seminfo.semmni = newSize;
394#ifdef SEM_DEBUG
395 printf("expansion successful\n");
396#endif
397 return 1;
398}
399
400/*
91447636 401 * Expand the sem_pool array to the given capacity. If the expansion fails
9bccf70c
A
402 * we return 0 (fail), otherwise we return 1 (success).
403 *
404 * Assumes we already hold the subsystem lock.
405 */
406static int
91447636 407grow_sem_pool(int new_pool_size)
9bccf70c 408{
91447636
A
409 struct sem *new_sem_pool = NULL;
410 struct sem *sem_free;
411 int i;
9bccf70c 412
91447636 413 if (new_pool_size < semtot)
9bccf70c 414 return 0;
91447636
A
415 /* enforce hard limit */
416 if (new_pool_size > limitseminfo.semmns) {
9bccf70c
A
417#ifdef SEM_DEBUG
418 printf("semaphore hard limit of %d reached, requested %d\n",
91447636 419 limitseminfo.semmns, new_pool_size);
9bccf70c
A
420#endif
421 return 0;
422 }
91447636
A
423
424 new_pool_size = (new_pool_size/SEMMNS_INC + 1) * SEMMNS_INC;
425 new_pool_size = new_pool_size > limitseminfo.semmns ? limitseminfo.semmns : new_pool_size;
9bccf70c
A
426
427#ifdef SEM_DEBUG
91447636 428 printf("growing sem_pool array from %d to %d\n", seminfo.semmns, new_pool_size);
9bccf70c 429#endif
3a60a9f5
A
430 MALLOC(new_sem_pool, struct sem *, sizeof (struct sem) * new_pool_size,
431 M_SYSVSEM, M_WAITOK | M_ZERO);
91447636 432 if (NULL == new_sem_pool) {
9bccf70c
A
433#ifdef SEM_DEBUG
434 printf("allocation failed. no changes made.\n");
435#endif
436 return 0;
437 }
438
439 /* We have our new memory, now copy the old contents over */
91447636 440 if (sem_pool)
9bccf70c 441 for(i = 0; i < seminfo.semmns; i++)
91447636 442 new_sem_pool[i] = sem_pool[i];
9bccf70c
A
443
444 /* Update our id structures to point to the new semaphores */
91447636 445 for(i = 0; i < seminfo.semmni; i++) {
2d21ac55 446 if (sema[i].u.sem_perm.mode & SEM_ALLOC) /* ID in use */
39236c6e
A
447 sema[i].u.sem_base = new_sem_pool +
448 (sema[i].u.sem_base - sem_pool);
91447636
A
449 }
450
451 sem_free = sem_pool;
452 sem_pool = new_sem_pool;
9bccf70c
A
453
454 /* clean up the old array */
91447636
A
455 if (sem_free != NULL)
456 FREE(sem_free, M_SYSVSEM);
9bccf70c 457
91447636 458 seminfo.semmns = new_pool_size;
9bccf70c
A
459#ifdef SEM_DEBUG
460 printf("expansion complete\n");
461#endif
462 return 1;
463}
464
1c79356b
A
465/*
466 * Allocate a new sem_undo structure for a process
467 * (returns ptr to structure or NULL if no more room)
9bccf70c
A
468 *
469 * Assumes we already hold the subsystem lock.
1c79356b
A
470 */
471
2d21ac55 472static int
91447636 473semu_alloc(struct proc *p)
1c79356b
A
474{
475 register int i;
476 register struct sem_undo *suptr;
2d21ac55 477 int *supidx;
1c79356b
A
478 int attempt;
479
480 /*
481 * Try twice to allocate something.
482 * (we'll purge any empty structures after the first pass so
483 * two passes are always enough)
484 */
485
486 for (attempt = 0; attempt < 2; attempt++) {
487 /*
488 * Look for a free structure.
489 * Fill it in and return it if we find one.
490 */
491
492 for (i = 0; i < seminfo.semmnu; i++) {
493 suptr = SEMU(i);
494 if (suptr->un_proc == NULL) {
2d21ac55
A
495 suptr->un_next_idx = semu_list_idx;
496 semu_list_idx = i;
1c79356b 497 suptr->un_cnt = 0;
91447636 498 suptr->un_ent = NULL;
1c79356b 499 suptr->un_proc = p;
2d21ac55 500 return i;
1c79356b
A
501 }
502 }
503
504 /*
505 * We didn't find a free one, if this is the first attempt
506 * then try to free some structures.
507 */
508
509 if (attempt == 0) {
510 /* All the structures are in use - try to free some */
511 int did_something = 0;
512
2d21ac55
A
513 supidx = &semu_list_idx;
514 while (*supidx != -1) {
515 suptr = SEMU(*supidx);
1c79356b
A
516 if (suptr->un_cnt == 0) {
517 suptr->un_proc = NULL;
2d21ac55 518 *supidx = suptr->un_next_idx;
1c79356b
A
519 did_something = 1;
520 } else
2d21ac55 521 supidx = &(suptr->un_next_idx);
1c79356b
A
522 }
523
9bccf70c
A
524 /* If we didn't free anything. Try expanding
525 * the semu[] array. If that doesn't work
526 * then fail. We expand last to get the
527 * most reuse out of existing resources.
528 */
1c79356b 529 if (!did_something)
9bccf70c 530 if (!grow_semu_array(seminfo.semmnu + 1))
2d21ac55 531 return -1;
1c79356b
A
532 } else {
533 /*
534 * The second pass failed even though we freed
535 * something after the first pass!
536 * This is IMPOSSIBLE!
537 */
538 panic("semu_alloc - second attempt failed");
539 }
540 }
2d21ac55 541 return -1;
1c79356b
A
542}
543
544/*
545 * Adjust a particular entry for a particular proc
9bccf70c
A
546 *
547 * Assumes we already hold the subsystem lock.
1c79356b 548 */
1c79356b 549static int
2d21ac55 550semundo_adjust(struct proc *p, int *supidx, int semid,
91447636 551 int semnum, int adjval)
1c79356b
A
552{
553 register struct sem_undo *suptr;
2d21ac55 554 int suidx;
91447636 555 register struct undo *sueptr, **suepptr, *new_sueptr;
1c79356b
A
556 int i;
557
3a60a9f5
A
558 /*
559 * Look for and remember the sem_undo if the caller doesn't provide it
560 */
1c79356b 561
2d21ac55
A
562 suidx = *supidx;
563 if (suidx == -1) {
564 for (suidx = semu_list_idx; suidx != -1;
565 suidx = suptr->un_next_idx) {
566 suptr = SEMU(suidx);
1c79356b 567 if (suptr->un_proc == p) {
2d21ac55 568 *supidx = suidx;
1c79356b
A
569 break;
570 }
571 }
2d21ac55 572 if (suidx == -1) {
1c79356b
A
573 if (adjval == 0)
574 return(0);
2d21ac55
A
575 suidx = semu_alloc(p);
576 if (suidx == -1)
1c79356b 577 return(ENOSPC);
2d21ac55 578 *supidx = suidx;
1c79356b
A
579 }
580 }
581
582 /*
583 * Look for the requested entry and adjust it (delete if adjval becomes
584 * 0).
585 */
2d21ac55 586 suptr = SEMU(suidx);
91447636 587 new_sueptr = NULL;
91447636
A
588 for (i = 0, suepptr = &suptr->un_ent, sueptr = suptr->un_ent;
589 i < suptr->un_cnt;
590 i++, suepptr = &sueptr->une_next, sueptr = sueptr->une_next) {
591 if (sueptr->une_id != semid || sueptr->une_num != semnum)
1c79356b
A
592 continue;
593 if (adjval == 0)
91447636 594 sueptr->une_adjval = 0;
1c79356b 595 else
91447636
A
596 sueptr->une_adjval += adjval;
597 if (sueptr->une_adjval == 0) {
1c79356b 598 suptr->un_cnt--;
91447636
A
599 *suepptr = sueptr->une_next;
600 FREE(sueptr, M_SYSVSEM);
601 sueptr = NULL;
602 }
3a60a9f5 603 return 0;
1c79356b
A
604 }
605
606 /* Didn't find the right entry - create it */
91447636 607 if (adjval == 0) {
3a60a9f5
A
608 /* no adjustment: no need for a new entry */
609 return 0;
91447636
A
610 }
611
3a60a9f5
A
612 if (suptr->un_cnt == limitseminfo.semume) {
613 /* reached the limit number of semaphore undo entries */
614 return EINVAL;
91447636
A
615 }
616
3a60a9f5
A
617 /* allocate a new semaphore undo entry */
618 MALLOC(new_sueptr, struct undo *, sizeof (struct undo),
619 M_SYSVSEM, M_WAITOK);
620 if (new_sueptr == NULL) {
621 return ENOMEM;
622 }
623
624 /* fill in the new semaphore undo entry */
625 new_sueptr->une_next = suptr->un_ent;
626 suptr->un_ent = new_sueptr;
627 suptr->un_cnt++;
628 new_sueptr->une_adjval = adjval;
629 new_sueptr->une_id = semid;
630 new_sueptr->une_num = semnum;
631
632 return 0;
1c79356b
A
633}
634
9bccf70c
A
635/* Assumes we already hold the subsystem lock.
636 */
1c79356b 637static void
91447636 638semundo_clear(int semid, int semnum)
1c79356b 639{
91447636 640 struct sem_undo *suptr;
2d21ac55 641 int suidx;
1c79356b 642
2d21ac55 643 for (suidx = semu_list_idx; suidx != -1; suidx = suptr->un_next_idx) {
91447636
A
644 struct undo *sueptr;
645 struct undo **suepptr;
646 int i = 0;
1c79356b 647
2d21ac55 648 suptr = SEMU(suidx);
91447636
A
649 sueptr = suptr->un_ent;
650 suepptr = &suptr->un_ent;
1c79356b 651 while (i < suptr->un_cnt) {
91447636
A
652 if (sueptr->une_id == semid) {
653 if (semnum == -1 || sueptr->une_num == semnum) {
1c79356b 654 suptr->un_cnt--;
91447636
A
655 *suepptr = sueptr->une_next;
656 FREE(sueptr, M_SYSVSEM);
657 sueptr = *suepptr;
658 continue;
1c79356b
A
659 }
660 if (semnum != -1)
661 break;
662 }
91447636
A
663 i++;
664 suepptr = &sueptr->une_next;
665 sueptr = sueptr->une_next;
1c79356b
A
666 }
667 }
668}
669
670/*
91447636
A
671 * Note that the user-mode half of this passes a union coerced to a
672 * user_addr_t. The union contains either an int or a pointer, and
673 * so we have to coerce it back, variant on whether the calling
674 * process is 64 bit or not. The coercion works for the 'val' element
675 * because the alignment is the same in user and kernel space.
1c79356b 676 */
1c79356b 677int
b0d623f7 678semctl(struct proc *p, struct semctl_args *uap, int32_t *retval)
1c79356b
A
679{
680 int semid = uap->semid;
681 int semnum = uap->semnum;
682 int cmd = uap->cmd;
91447636
A
683 user_semun_t user_arg = (user_semun_t)uap->arg;
684 kauth_cred_t cred = kauth_cred_get();
1c79356b 685 int i, rval, eval;
91447636 686 struct user_semid_ds sbuf;
2d21ac55 687 struct semid_kernel *semakptr;
91447636 688
1c79356b 689
55e303ae
A
690 AUDIT_ARG(svipc_cmd, cmd);
691 AUDIT_ARG(svipc_id, semid);
91447636
A
692
693 SYSV_SEM_SUBSYS_LOCK();
694
1c79356b 695#ifdef SEM_DEBUG
91447636 696 printf("call to semctl(%d, %d, %d, 0x%qx)\n", semid, semnum, cmd, user_arg);
1c79356b
A
697#endif
698
699 semid = IPCID_TO_IX(semid);
91447636
A
700
701 if (semid < 0 || semid >= seminfo.semmni) {
9bccf70c
A
702#ifdef SEM_DEBUG
703 printf("Invalid semid\n");
704#endif
91447636
A
705 eval = EINVAL;
706 goto semctlout;
707 }
1c79356b 708
2d21ac55
A
709 semakptr = &sema[semid];
710 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
711 semakptr->u.sem_perm._seq != IPCID_TO_SEQ(uap->semid)) {
91447636
A
712 eval = EINVAL;
713 goto semctlout;
714 }
2d21ac55
A
715#if CONFIG_MACF
716 eval = mac_sysvsem_check_semctl(cred, semakptr, cmd);
717 if (eval)
718 goto semctlout;
719#endif
1c79356b
A
720
721 eval = 0;
722 rval = 0;
723
724 switch (cmd) {
725 case IPC_RMID:
2d21ac55 726 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_M)))
91447636
A
727 goto semctlout;
728
2d21ac55
A
729 semakptr->u.sem_perm.cuid = kauth_cred_getuid(cred);
730 semakptr->u.sem_perm.uid = kauth_cred_getuid(cred);
731 semtot -= semakptr->u.sem_nsems;
732 for (i = semakptr->u.sem_base - sem_pool; i < semtot; i++)
733 sem_pool[i] = sem_pool[i + semakptr->u.sem_nsems];
1c79356b 734 for (i = 0; i < seminfo.semmni; i++) {
2d21ac55
A
735 if ((sema[i].u.sem_perm.mode & SEM_ALLOC) &&
736 sema[i].u.sem_base > semakptr->u.sem_base)
737 sema[i].u.sem_base -= semakptr->u.sem_nsems;
1c79356b 738 }
2d21ac55
A
739 semakptr->u.sem_perm.mode = 0;
740#if CONFIG_MACF
741 mac_sysvsem_label_recycle(semakptr);
742#endif
1c79356b 743 semundo_clear(semid, -1);
2d21ac55 744 wakeup((caddr_t)semakptr);
1c79356b
A
745 break;
746
747 case IPC_SET:
2d21ac55 748 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_M)))
91447636
A
749 goto semctlout;
750
91447636 751 if (IS_64BIT_PROCESS(p)) {
b0d623f7
A
752 struct user64_semid_ds ds64;
753 eval = copyin(user_arg.buf, &ds64, sizeof(ds64));
754 semid_ds_64tokernel(&ds64, &sbuf);
91447636 755 } else {
b0d623f7
A
756 struct user32_semid_ds ds32;
757 eval = copyin(user_arg.buf, &ds32, sizeof(ds32));
758 semid_ds_32tokernel(&ds32, &sbuf);
91447636
A
759 }
760
3a60a9f5
A
761 if (eval != 0) {
762 goto semctlout;
763 }
91447636 764
2d21ac55
A
765 semakptr->u.sem_perm.uid = sbuf.sem_perm.uid;
766 semakptr->u.sem_perm.gid = sbuf.sem_perm.gid;
767 semakptr->u.sem_perm.mode = (semakptr->u.sem_perm.mode &
768 ~0777) | (sbuf.sem_perm.mode & 0777);
769 semakptr->u.sem_ctime = sysv_semtime();
1c79356b
A
770 break;
771
772 case IPC_STAT:
2d21ac55 773 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_R)))
91447636 774 goto semctlout;
b0d623f7 775
91447636 776 if (IS_64BIT_PROCESS(p)) {
b0d623f7 777 struct user64_semid_ds semid_ds64;
39236c6e 778 bzero(&semid_ds64, sizeof(semid_ds64));
b0d623f7
A
779 semid_ds_kernelto64(&semakptr->u, &semid_ds64);
780 eval = copyout(&semid_ds64, user_arg.buf, sizeof(semid_ds64));
91447636 781 } else {
b0d623f7 782 struct user32_semid_ds semid_ds32;
39236c6e 783 bzero(&semid_ds32, sizeof(semid_ds32));
b0d623f7
A
784 semid_ds_kernelto32(&semakptr->u, &semid_ds32);
785 eval = copyout(&semid_ds32, user_arg.buf, sizeof(semid_ds32));
91447636 786 }
1c79356b
A
787 break;
788
789 case GETNCNT:
2d21ac55 790 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_R)))
91447636 791 goto semctlout;
2d21ac55 792 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
91447636
A
793 eval = EINVAL;
794 goto semctlout;
795 }
2d21ac55 796 rval = semakptr->u.sem_base[semnum].semncnt;
1c79356b
A
797 break;
798
799 case GETPID:
2d21ac55 800 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_R)))
91447636 801 goto semctlout;
2d21ac55 802 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
91447636
A
803 eval = EINVAL;
804 goto semctlout;
805 }
2d21ac55 806 rval = semakptr->u.sem_base[semnum].sempid;
1c79356b
A
807 break;
808
809 case GETVAL:
2d21ac55 810 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_R)))
91447636 811 goto semctlout;
2d21ac55 812 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
91447636
A
813 eval = EINVAL;
814 goto semctlout;
815 }
2d21ac55 816 rval = semakptr->u.sem_base[semnum].semval;
1c79356b
A
817 break;
818
819 case GETALL:
2d21ac55 820 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_R)))
91447636
A
821 goto semctlout;
822/* XXXXXXXXXXXXXXXX TBD XXXXXXXXXXXXXXXX */
2d21ac55 823 for (i = 0; i < semakptr->u.sem_nsems; i++) {
91447636 824 /* XXX could be done in one go... */
2d21ac55 825 eval = copyout((caddr_t)&semakptr->u.sem_base[i].semval,
91447636
A
826 user_arg.array + (i * sizeof(unsigned short)),
827 sizeof(unsigned short));
1c79356b
A
828 if (eval != 0)
829 break;
830 }
831 break;
832
833 case GETZCNT:
2d21ac55 834 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_R)))
91447636 835 goto semctlout;
2d21ac55 836 if (semnum < 0 || semnum >= semakptr->u.sem_nsems) {
91447636
A
837 eval = EINVAL;
838 goto semctlout;
839 }
2d21ac55 840 rval = semakptr->u.sem_base[semnum].semzcnt;
1c79356b
A
841 break;
842
843 case SETVAL:
2d21ac55 844 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_W)))
9bccf70c
A
845 {
846#ifdef SEM_DEBUG
847 printf("Invalid credentials for write\n");
848#endif
91447636 849 goto semctlout;
9bccf70c 850 }
2d21ac55 851 if (semnum < 0 || semnum >= semakptr->u.sem_nsems)
9bccf70c
A
852 {
853#ifdef SEM_DEBUG
854 printf("Invalid number out of range for set\n");
855#endif
91447636
A
856 eval = EINVAL;
857 goto semctlout;
9bccf70c 858 }
39236c6e 859
91447636
A
860 /*
861 * Cast down a pointer instead of using 'val' member directly
862 * to avoid introducing endieness and a pad field into the
863 * header file. Ugly, but it works.
864 */
39236c6e
A
865 u_int newsemval = CAST_DOWN_EXPLICIT(u_int, user_arg.buf);
866
867 /*
868 * The check is being performed as unsigned values to match
869 * eventual destination
870 */
871 if (newsemval > (u_int)seminfo.semvmx)
872 {
873#ifdef SEM_DEBUG
874 printf("Out of range sem value for set\n");
875#endif
876 eval = ERANGE;
877 goto semctlout;
878 }
879 semakptr->u.sem_base[semnum].semval = newsemval;
2d21ac55
A
880 semakptr->u.sem_base[semnum].sempid = p->p_pid;
881 /* XXX scottl Should there be a MAC call here? */
1c79356b 882 semundo_clear(semid, semnum);
2d21ac55 883 wakeup((caddr_t)semakptr);
1c79356b
A
884 break;
885
886 case SETALL:
2d21ac55 887 if ((eval = ipcperm(cred, &semakptr->u.sem_perm, IPC_W)))
91447636
A
888 goto semctlout;
889/*** XXXXXXXXXXXX TBD ********/
2d21ac55 890 for (i = 0; i < semakptr->u.sem_nsems; i++) {
91447636
A
891 /* XXX could be done in one go... */
892 eval = copyin(user_arg.array + (i * sizeof(unsigned short)),
2d21ac55 893 (caddr_t)&semakptr->u.sem_base[i].semval,
91447636 894 sizeof(unsigned short));
1c79356b
A
895 if (eval != 0)
896 break;
2d21ac55 897 semakptr->u.sem_base[i].sempid = p->p_pid;
1c79356b 898 }
2d21ac55 899 /* XXX scottl Should there be a MAC call here? */
1c79356b 900 semundo_clear(semid, -1);
2d21ac55 901 wakeup((caddr_t)semakptr);
1c79356b
A
902 break;
903
904 default:
91447636
A
905 eval = EINVAL;
906 goto semctlout;
1c79356b
A
907 }
908
909 if (eval == 0)
9bccf70c 910 *retval = rval;
91447636
A
911semctlout:
912 SYSV_SEM_SUBSYS_UNLOCK();
913 return(eval);
1c79356b
A
914}
915
1c79356b 916int
b0d623f7 917semget(__unused struct proc *p, struct semget_args *uap, int32_t *retval)
1c79356b
A
918{
919 int semid, eval;
920 int key = uap->key;
921 int nsems = uap->nsems;
922 int semflg = uap->semflg;
91447636 923 kauth_cred_t cred = kauth_cred_get();
1c79356b
A
924
925#ifdef SEM_DEBUG
9bccf70c
A
926 if (key != IPC_PRIVATE)
927 printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
928 else
929 printf("semget(IPC_PRIVATE, %d, 0%o)\n", nsems, semflg);
1c79356b 930#endif
91447636
A
931
932
3a60a9f5 933 SYSV_SEM_SUBSYS_LOCK();
91447636 934
9bccf70c 935
1c79356b
A
936 if (key != IPC_PRIVATE) {
937 for (semid = 0; semid < seminfo.semmni; semid++) {
2d21ac55
A
938 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) &&
939 sema[semid].u.sem_perm._key == key)
1c79356b
A
940 break;
941 }
942 if (semid < seminfo.semmni) {
943#ifdef SEM_DEBUG
944 printf("found public key\n");
945#endif
2d21ac55 946 if ((eval = ipcperm(cred, &sema[semid].u.sem_perm,
1c79356b 947 semflg & 0700)))
91447636 948 goto semgetout;
2d21ac55 949 if (nsems < 0 || sema[semid].u.sem_nsems < nsems) {
1c79356b
A
950#ifdef SEM_DEBUG
951 printf("too small\n");
952#endif
91447636
A
953 eval = EINVAL;
954 goto semgetout;
1c79356b
A
955 }
956 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
957#ifdef SEM_DEBUG
958 printf("not exclusive\n");
959#endif
91447636
A
960 eval = EEXIST;
961 goto semgetout;
1c79356b 962 }
2d21ac55
A
963#if CONFIG_MACF
964 eval = mac_sysvsem_check_semget(cred, &sema[semid]);
965 if (eval)
966 goto semgetout;
967#endif
1c79356b
A
968 goto found;
969 }
970 }
971
972#ifdef SEM_DEBUG
9bccf70c 973 printf("need to allocate an id for the request\n");
1c79356b
A
974#endif
975 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
55e303ae 976 if (nsems <= 0 || nsems > limitseminfo.semmsl) {
1c79356b
A
977#ifdef SEM_DEBUG
978 printf("nsems out of range (0<%d<=%d)\n", nsems,
979 seminfo.semmsl);
980#endif
91447636
A
981 eval = EINVAL;
982 goto semgetout;
1c79356b
A
983 }
984 if (nsems > seminfo.semmns - semtot) {
985#ifdef SEM_DEBUG
986 printf("not enough semaphores left (need %d, got %d)\n",
987 nsems, seminfo.semmns - semtot);
988#endif
91447636 989 if (!grow_sem_pool(semtot + nsems)) {
9bccf70c
A
990#ifdef SEM_DEBUG
991 printf("failed to grow the sem array\n");
992#endif
91447636
A
993 eval = ENOSPC;
994 goto semgetout;
9bccf70c 995 }
1c79356b
A
996 }
997 for (semid = 0; semid < seminfo.semmni; semid++) {
2d21ac55 998 if ((sema[semid].u.sem_perm.mode & SEM_ALLOC) == 0)
1c79356b
A
999 break;
1000 }
1001 if (semid == seminfo.semmni) {
1002#ifdef SEM_DEBUG
9bccf70c 1003 printf("no more id's available\n");
1c79356b 1004#endif
9bccf70c
A
1005 if (!grow_sema_array(seminfo.semmni + 1))
1006 {
1007#ifdef SEM_DEBUG
1008 printf("failed to grow sema array\n");
1009#endif
91447636
A
1010 eval = ENOSPC;
1011 goto semgetout;
9bccf70c 1012 }
1c79356b
A
1013 }
1014#ifdef SEM_DEBUG
1015 printf("semid %d is available\n", semid);
1016#endif
2d21ac55
A
1017 sema[semid].u.sem_perm._key = key;
1018 sema[semid].u.sem_perm.cuid = kauth_cred_getuid(cred);
1019 sema[semid].u.sem_perm.uid = kauth_cred_getuid(cred);
6d2010ae
A
1020 sema[semid].u.sem_perm.cgid = kauth_cred_getgid(cred);
1021 sema[semid].u.sem_perm.gid = kauth_cred_getgid(cred);
2d21ac55
A
1022 sema[semid].u.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
1023 sema[semid].u.sem_perm._seq =
1024 (sema[semid].u.sem_perm._seq + 1) & 0x7fff;
1025 sema[semid].u.sem_nsems = nsems;
1026 sema[semid].u.sem_otime = 0;
1027 sema[semid].u.sem_ctime = sysv_semtime();
1028 sema[semid].u.sem_base = &sem_pool[semtot];
1c79356b 1029 semtot += nsems;
2d21ac55
A
1030 bzero(sema[semid].u.sem_base,
1031 sizeof(sema[semid].u.sem_base[0])*nsems);
1032#if CONFIG_MACF
1033 mac_sysvsem_label_associate(cred, &sema[semid]);
1034#endif
1c79356b 1035#ifdef SEM_DEBUG
2d21ac55 1036 printf("sembase = 0x%x, next = 0x%x\n", sema[semid].u.sem_base,
91447636 1037 &sem_pool[semtot]);
1c79356b
A
1038#endif
1039 } else {
1040#ifdef SEM_DEBUG
1041 printf("didn't find it and wasn't asked to create it\n");
1042#endif
91447636
A
1043 eval = ENOENT;
1044 goto semgetout;
1c79356b
A
1045 }
1046
1047found:
2d21ac55 1048 *retval = IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
55e303ae 1049 AUDIT_ARG(svipc_id, *retval);
9bccf70c
A
1050#ifdef SEM_DEBUG
1051 printf("semget is done, returning %d\n", *retval);
1052#endif
91447636 1053 eval = 0;
1c79356b 1054
91447636
A
1055semgetout:
1056 SYSV_SEM_SUBSYS_UNLOCK();
1057 return(eval);
1058}
1c79356b
A
1059
1060int
b0d623f7 1061semop(struct proc *p, struct semop_args *uap, int32_t *retval)
1c79356b
A
1062{
1063 int semid = uap->semid;
1064 int nsops = uap->nsops;
316670eb 1065 struct sembuf sops[seminfo.semopm];
2d21ac55 1066 register struct semid_kernel *semakptr;
91447636
A
1067 register struct sembuf *sopptr = NULL; /* protected by 'semptr' */
1068 register struct sem *semptr = NULL; /* protected by 'if' */
2d21ac55 1069 int supidx = -1;
1c79356b
A
1070 int i, j, eval;
1071 int do_wakeup, do_undos;
1072
55e303ae 1073 AUDIT_ARG(svipc_id, uap->semid);
91447636
A
1074
1075 SYSV_SEM_SUBSYS_LOCK();
1076
1c79356b
A
1077#ifdef SEM_DEBUG
1078 printf("call to semop(%d, 0x%x, %d)\n", semid, sops, nsops);
1079#endif
1080
1081 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
1082
91447636
A
1083 if (semid < 0 || semid >= seminfo.semmni) {
1084 eval = EINVAL;
1085 goto semopout;
1086 }
1c79356b 1087
2d21ac55
A
1088 semakptr = &sema[semid];
1089 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
91447636
A
1090 eval = EINVAL;
1091 goto semopout;
1092 }
2d21ac55 1093 if (semakptr->u.sem_perm._seq != IPCID_TO_SEQ(uap->semid)) {
91447636
A
1094 eval = EINVAL;
1095 goto semopout;
1096 }
1c79356b 1097
2d21ac55 1098 if ((eval = ipcperm(kauth_cred_get(), &semakptr->u.sem_perm, IPC_W))) {
1c79356b
A
1099#ifdef SEM_DEBUG
1100 printf("eval = %d from ipaccess\n", eval);
1101#endif
91447636 1102 goto semopout;
1c79356b
A
1103 }
1104
316670eb 1105 if (nsops < 0 || nsops > seminfo.semopm) {
cf7d32b8 1106#ifdef SEM_DEBUG
316670eb
A
1107 printf("too many sops (max=%d, nsops=%d)\n",
1108 seminfo.semopm, nsops);
cf7d32b8
A
1109#endif
1110 eval = E2BIG;
1111 goto semopout;
1112 }
316670eb 1113
6d2010ae
A
1114 /* OK for LP64, since sizeof(struct sembuf) is currently invariant */
1115 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(struct sembuf))) != 0) {
1116#ifdef SEM_DEBUG
1117 printf("eval = %d from copyin(%08x, %08x, %ld)\n", eval,
1118 uap->sops, &sops, nsops * sizeof(struct sembuf));
1119#endif
1120 goto semopout;
1121 }
1122
2d21ac55
A
1123#if CONFIG_MACF
1124 /*
1125 * Initial pass thru sops to see what permissions are needed.
1126 */
1127 j = 0; /* permission needed */
1128 for (i = 0; i < nsops; i++)
1129 j |= (sops[i].sem_op == 0) ? SEM_R : SEM_A;
1130
1131 /*
1132 * The MAC hook checks whether the thread has read (and possibly
1133 * write) permissions to the semaphore array based on the
1134 * sopptr->sem_op value.
1135 */
1136 eval = mac_sysvsem_check_semop(kauth_cred_get(), semakptr, j);
1137 if (eval)
1138 goto semopout;
1139#endif
1140
1c79356b
A
1141 /*
1142 * Loop trying to satisfy the vector of requests.
1143 * If we reach a point where we must wait, any requests already
1144 * performed are rolled back and we go to sleep until some other
1145 * process wakes us up. At this point, we start all over again.
1146 *
1147 * This ensures that from the perspective of other tasks, a set
1148 * of requests is atomic (never partially satisfied).
1149 */
1150 do_undos = 0;
1151
1152 for (;;) {
1153 do_wakeup = 0;
1154
1155 for (i = 0; i < nsops; i++) {
1156 sopptr = &sops[i];
1157
2d21ac55 1158 if (sopptr->sem_num >= semakptr->u.sem_nsems) {
91447636
A
1159 eval = EFBIG;
1160 goto semopout;
1161 }
1c79356b 1162
2d21ac55 1163 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1c79356b
A
1164
1165#ifdef SEM_DEBUG
2d21ac55
A
1166 printf("semop: semakptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
1167 semakptr, semakptr->u.sem_base, semptr,
1c79356b
A
1168 sopptr->sem_num, semptr->semval, sopptr->sem_op,
1169 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
1170#endif
1171
1172 if (sopptr->sem_op < 0) {
1173 if (semptr->semval + sopptr->sem_op < 0) {
1174#ifdef SEM_DEBUG
1175 printf("semop: can't do it now\n");
1176#endif
1177 break;
1178 } else {
1179 semptr->semval += sopptr->sem_op;
1180 if (semptr->semval == 0 &&
1181 semptr->semzcnt > 0)
1182 do_wakeup = 1;
1183 }
1184 if (sopptr->sem_flg & SEM_UNDO)
1185 do_undos = 1;
1186 } else if (sopptr->sem_op == 0) {
1187 if (semptr->semval > 0) {
1188#ifdef SEM_DEBUG
1189 printf("semop: not zero now\n");
1190#endif
1191 break;
1192 }
1193 } else {
1194 if (semptr->semncnt > 0)
1195 do_wakeup = 1;
1196 semptr->semval += sopptr->sem_op;
1197 if (sopptr->sem_flg & SEM_UNDO)
1198 do_undos = 1;
1199 }
1200 }
1201
1202 /*
1203 * Did we get through the entire vector?
1204 */
1205 if (i >= nsops)
1206 goto done;
1207
1208 /*
1209 * No ... rollback anything that we've already done
1210 */
1211#ifdef SEM_DEBUG
1212 printf("semop: rollback 0 through %d\n", i-1);
1213#endif
1214 for (j = 0; j < i; j++)
2d21ac55 1215 semakptr->u.sem_base[sops[j].sem_num].semval -=
1c79356b
A
1216 sops[j].sem_op;
1217
1218 /*
1219 * If the request that we couldn't satisfy has the
1220 * NOWAIT flag set then return with EAGAIN.
1221 */
91447636
A
1222 if (sopptr->sem_flg & IPC_NOWAIT) {
1223 eval = EAGAIN;
1224 goto semopout;
1225 }
1c79356b
A
1226
1227 if (sopptr->sem_op == 0)
1228 semptr->semzcnt++;
1229 else
1230 semptr->semncnt++;
1231
1232#ifdef SEM_DEBUG
1233 printf("semop: good night!\n");
1234#endif
9bccf70c
A
1235 /* Release our lock on the semaphore subsystem so
1236 * another thread can get at the semaphore we are
1237 * waiting for. We will get the lock back after we
1238 * wake up.
1239 */
2d21ac55 1240 eval = msleep((caddr_t)semakptr, &sysv_sem_subsys_mutex , (PZERO - 4) | PCATCH,
1c79356b 1241 "semwait", 0);
9bccf70c 1242
1c79356b
A
1243#ifdef SEM_DEBUG
1244 printf("semop: good morning (eval=%d)!\n", eval);
1245#endif
91447636 1246 if (eval != 0) {
91447636 1247 eval = EINTR;
91447636 1248 }
1c79356b 1249
3a60a9f5
A
1250 /*
1251 * IMPORTANT: while we were asleep, the semaphore array might
1252 * have been reallocated somewhere else (see grow_sema_array()).
1253 * When we wake up, we have to re-lookup the semaphore
1254 * structures and re-validate them.
1255 */
1256
2d21ac55 1257 semptr = NULL;
9bccf70c 1258
1c79356b
A
1259 /*
1260 * Make sure that the semaphore still exists
2d21ac55
A
1261 *
1262 * XXX POSIX: Third test this 'if' and 'EINTR' precedence may
1263 * fail testing; if so, we will need to revert this code.
1c79356b 1264 */
2d21ac55
A
1265 semakptr = &sema[semid]; /* sema may have been reallocated */
1266 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
1267 semakptr->u.sem_perm._seq != IPCID_TO_SEQ(uap->semid) ||
1268 sopptr->sem_num >= semakptr->u.sem_nsems) {
1269 /* The man page says to return EIDRM. */
1270 /* Unfortunately, BSD doesn't define that code! */
3a60a9f5
A
1271 if (eval == EINTR) {
1272 /*
1273 * EINTR takes precedence over the fact that
1274 * the semaphore disappeared while we were
1275 * sleeping...
1276 */
1277 } else {
1c79356b 1278#ifdef EIDRM
3a60a9f5 1279 eval = EIDRM;
1c79356b 1280#else
2d21ac55 1281 eval = EINVAL; /* Ancient past */
1c79356b 1282#endif
3a60a9f5
A
1283 }
1284 goto semopout;
1c79356b
A
1285 }
1286
1287 /*
1288 * The semaphore is still alive. Readjust the count of
9bccf70c
A
1289 * waiting processes. semptr needs to be recomputed
1290 * because the sem[] may have been reallocated while
1291 * we were sleeping, updating our sem_base pointer.
1c79356b 1292 */
2d21ac55 1293 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1c79356b
A
1294 if (sopptr->sem_op == 0)
1295 semptr->semzcnt--;
1296 else
1297 semptr->semncnt--;
3a60a9f5
A
1298
1299 if (eval != 0) { /* EINTR */
1300 goto semopout;
1301 }
1c79356b
A
1302 }
1303
1304done:
1305 /*
1306 * Process any SEM_UNDO requests.
1307 */
1308 if (do_undos) {
1309 for (i = 0; i < nsops; i++) {
1310 /*
1311 * We only need to deal with SEM_UNDO's for non-zero
1312 * op's.
1313 */
1314 int adjval;
1315
1316 if ((sops[i].sem_flg & SEM_UNDO) == 0)
1317 continue;
1318 adjval = sops[i].sem_op;
1319 if (adjval == 0)
1320 continue;
2d21ac55 1321 eval = semundo_adjust(p, &supidx, semid,
1c79356b
A
1322 sops[i].sem_num, -adjval);
1323 if (eval == 0)
1324 continue;
1325
1326 /*
1327 * Oh-Oh! We ran out of either sem_undo's or undo's.
1328 * Rollback the adjustments to this point and then
1329 * rollback the semaphore ups and down so we can return
1330 * with an error with all structures restored. We
1331 * rollback the undo's in the exact reverse order that
1332 * we applied them. This guarantees that we won't run
1333 * out of space as we roll things back out.
1334 */
1335 for (j = i - 1; j >= 0; j--) {
1336 if ((sops[j].sem_flg & SEM_UNDO) == 0)
1337 continue;
1338 adjval = sops[j].sem_op;
1339 if (adjval == 0)
1340 continue;
2d21ac55 1341 if (semundo_adjust(p, &supidx, semid,
1c79356b
A
1342 sops[j].sem_num, adjval) != 0)
1343 panic("semop - can't undo undos");
1344 }
1345
1346 for (j = 0; j < nsops; j++)
2d21ac55 1347 semakptr->u.sem_base[sops[j].sem_num].semval -=
1c79356b
A
1348 sops[j].sem_op;
1349
1350#ifdef SEM_DEBUG
1351 printf("eval = %d from semundo_adjust\n", eval);
1352#endif
91447636 1353 goto semopout;
1c79356b
A
1354 } /* loop through the sops */
1355 } /* if (do_undos) */
1356
1357 /* We're definitely done - set the sempid's */
1358 for (i = 0; i < nsops; i++) {
1359 sopptr = &sops[i];
2d21ac55 1360 semptr = &semakptr->u.sem_base[sopptr->sem_num];
1c79356b
A
1361 semptr->sempid = p->p_pid;
1362 }
2d21ac55 1363 semakptr->u.sem_otime = sysv_semtime();
1c79356b 1364
1c79356b
A
1365 if (do_wakeup) {
1366#ifdef SEM_DEBUG
1367 printf("semop: doing wakeup\n");
1368#ifdef SEM_WAKEUP
2d21ac55 1369 sem_wakeup((caddr_t)semakptr);
1c79356b 1370#else
2d21ac55 1371 wakeup((caddr_t)semakptr);
1c79356b
A
1372#endif
1373 printf("semop: back from wakeup\n");
1374#else
2d21ac55 1375 wakeup((caddr_t)semakptr);
1c79356b
A
1376#endif
1377 }
1378#ifdef SEM_DEBUG
1379 printf("semop: done\n");
1380#endif
9bccf70c 1381 *retval = 0;
91447636
A
1382 eval = 0;
1383semopout:
1384 SYSV_SEM_SUBSYS_UNLOCK();
1385 return(eval);
1c79356b
A
1386}
1387
1388/*
1389 * Go through the undo structures for this process and apply the adjustments to
1390 * semaphores.
1391 */
1392void
91447636 1393semexit(struct proc *p)
1c79356b 1394{
2d21ac55
A
1395 register struct sem_undo *suptr = NULL;
1396 int suidx;
1397 int *supidx;
1c79356b
A
1398 int did_something;
1399
9bccf70c
A
1400 /* If we have not allocated our semaphores yet there can't be
1401 * anything to undo, but we need the lock to prevent
1402 * dynamic memory race conditions.
1c79356b 1403 */
91447636
A
1404 SYSV_SEM_SUBSYS_LOCK();
1405
1406 if (!sem_pool)
9bccf70c 1407 {
91447636 1408 SYSV_SEM_SUBSYS_UNLOCK();
9bccf70c 1409 return;
1c79356b 1410 }
1c79356b
A
1411 did_something = 0;
1412
1413 /*
1414 * Go through the chain of undo vectors looking for one
1415 * associated with this process.
1416 */
1417
2d21ac55
A
1418 for (supidx = &semu_list_idx; (suidx = *supidx) != -1;
1419 supidx = &suptr->un_next_idx) {
1420 suptr = SEMU(suidx);
1c79356b
A
1421 if (suptr->un_proc == p)
1422 break;
1423 }
1424
2d21ac55 1425 if (suidx == -1)
1c79356b
A
1426 goto unlock;
1427
1428#ifdef SEM_DEBUG
1429 printf("proc @%08x has undo structure with %d entries\n", p,
1430 suptr->un_cnt);
1431#endif
1432
1433 /*
1434 * If there are any active undo elements then process them.
1435 */
1436 if (suptr->un_cnt > 0) {
91447636
A
1437 while (suptr->un_ent != NULL) {
1438 struct undo *sueptr;
1439 int semid;
1440 int semnum;
1441 int adjval;
2d21ac55 1442 struct semid_kernel *semakptr;
1c79356b 1443
91447636
A
1444 sueptr = suptr->un_ent;
1445 semid = sueptr->une_id;
1446 semnum = sueptr->une_num;
1447 adjval = sueptr->une_adjval;
1c79356b 1448
2d21ac55
A
1449 semakptr = &sema[semid];
1450 if ((semakptr->u.sem_perm.mode & SEM_ALLOC) == 0)
1c79356b 1451 panic("semexit - semid not allocated");
2d21ac55 1452 if (semnum >= semakptr->u.sem_nsems)
1c79356b
A
1453 panic("semexit - semnum out of range");
1454
1455#ifdef SEM_DEBUG
1456 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
91447636
A
1457 suptr->un_proc,
1458 semid,
1459 semnum,
1460 adjval,
2d21ac55 1461 semakptr->u.sem_base[semnum].semval);
1c79356b
A
1462#endif
1463
1464 if (adjval < 0) {
2d21ac55
A
1465 if (semakptr->u.sem_base[semnum].semval < -adjval)
1466 semakptr->u.sem_base[semnum].semval = 0;
1c79356b 1467 else
2d21ac55 1468 semakptr->u.sem_base[semnum].semval +=
1c79356b
A
1469 adjval;
1470 } else
2d21ac55 1471 semakptr->u.sem_base[semnum].semval += adjval;
1c79356b 1472
2d21ac55 1473 /* Maybe we should build a list of semakptr's to wake
9bccf70c
A
1474 * up, finish all access to data structures, release the
1475 * subsystem lock, and wake all the processes. Something
1476 * to think about. It wouldn't buy us anything unless
1477 * wakeup had the potential to block, or the syscall
1478 * funnel state was changed to allow multiple threads
1479 * in the BSD code at once.
1480 */
1c79356b 1481#ifdef SEM_WAKEUP
2d21ac55 1482 sem_wakeup((caddr_t)semakptr);
1c79356b 1483#else
2d21ac55 1484 wakeup((caddr_t)semakptr);
1c79356b
A
1485#endif
1486#ifdef SEM_DEBUG
1487 printf("semexit: back from wakeup\n");
1488#endif
91447636
A
1489 suptr->un_cnt--;
1490 suptr->un_ent = sueptr->une_next;
1491 FREE(sueptr, M_SYSVSEM);
1492 sueptr = NULL;
1c79356b
A
1493 }
1494 }
1495
1496 /*
1497 * Deallocate the undo vector.
1498 */
1499#ifdef SEM_DEBUG
1500 printf("removing vector\n");
1501#endif
1502 suptr->un_proc = NULL;
2d21ac55 1503 *supidx = suptr->un_next_idx;
1c79356b
A
1504
1505unlock:
1506 /*
9bccf70c
A
1507 * There is a semaphore leak (i.e. memory leak) in this code.
1508 * We should be deleting the IPC_PRIVATE semaphores when they are
1509 * no longer needed, and we dont. We would have to track which processes
1510 * know about which IPC_PRIVATE semaphores, updating the list after
1511 * every fork. We can't just delete them semaphore when the process
1512 * that created it dies, because that process may well have forked
1513 * some children. So we need to wait until all of it's children have
1514 * died, and so on. Maybe we should tag each IPC_PRIVATE sempahore
1515 * with the creating group ID, count the number of processes left in
1516 * that group, and delete the semaphore when the group is gone.
1517 * Until that code gets implemented we will leak IPC_PRIVATE semaphores.
1518 * There is an upper bound on the size of our semaphore array, so
1519 * leaking the semaphores should not work as a DOS attack.
1520 *
1521 * Please note that the original BSD code this file is based on had the
1522 * same leaky semaphore problem.
1523 */
1524
91447636 1525 SYSV_SEM_SUBSYS_UNLOCK();
1c79356b 1526}
91447636
A
1527
1528
55e303ae
A
1529/* (struct sysctl_oid *oidp, void *arg1, int arg2, \
1530 struct sysctl_req *req) */
1531static int
91447636
A
1532sysctl_seminfo(__unused struct sysctl_oid *oidp, void *arg1,
1533 __unused int arg2, struct sysctl_req *req)
55e303ae
A
1534{
1535 int error = 0;
1536
1537 error = SYSCTL_OUT(req, arg1, sizeof(int));
91447636 1538 if (error || req->newptr == USER_ADDR_NULL)
55e303ae
A
1539 return(error);
1540
91447636
A
1541 SYSV_SEM_SUBSYS_LOCK();
1542
55e303ae 1543 /* Set the values only if shared memory is not initialised */
91447636
A
1544 if ((sem_pool == NULL) &&
1545 (sema == NULL) &&
1546 (semu == NULL) &&
2d21ac55 1547 (semu_list_idx == -1)) {
91447636 1548 if ((error = SYSCTL_IN(req, arg1, sizeof(int)))) {
55e303ae
A
1549 goto out;
1550 }
1551 } else
1552 error = EINVAL;
1553out:
91447636 1554 SYSV_SEM_SUBSYS_UNLOCK();
55e303ae
A
1555 return(error);
1556
1557}
1558
1559/* SYSCTL_NODE(_kern, KERN_SYSV, sysv, CTLFLAG_RW, 0, "SYSV"); */
1560extern struct sysctl_oid_list sysctl__kern_sysv_children;
6d2010ae 1561SYSCTL_PROC(_kern_sysv, OID_AUTO, semmni, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
55e303ae
A
1562 &limitseminfo.semmni, 0, &sysctl_seminfo ,"I","semmni");
1563
6d2010ae 1564SYSCTL_PROC(_kern_sysv, OID_AUTO, semmns, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
55e303ae
A
1565 &limitseminfo.semmns, 0, &sysctl_seminfo ,"I","semmns");
1566
6d2010ae 1567SYSCTL_PROC(_kern_sysv, OID_AUTO, semmnu, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
55e303ae
A
1568 &limitseminfo.semmnu, 0, &sysctl_seminfo ,"I","semmnu");
1569
6d2010ae 1570SYSCTL_PROC(_kern_sysv, OID_AUTO, semmsl, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
55e303ae
A
1571 &limitseminfo.semmsl, 0, &sysctl_seminfo ,"I","semmsl");
1572
6d2010ae 1573SYSCTL_PROC(_kern_sysv, OID_AUTO, semume, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
55e303ae
A
1574 &limitseminfo.semume, 0, &sysctl_seminfo ,"I","semume");
1575
9bccf70c 1576
91447636
A
1577static int
1578IPCS_sem_sysctl(__unused struct sysctl_oid *oidp, __unused void *arg1,
1579 __unused int arg2, struct sysctl_req *req)
1580{
1581 int error;
1582 int cursor;
1583 union {
b0d623f7 1584 struct user32_IPCS_command u32;
91447636
A
1585 struct user_IPCS_command u64;
1586 } ipcs;
b0d623f7
A
1587 struct user32_semid_ds semid_ds32; /* post conversion, 32 bit version */
1588 struct user64_semid_ds semid_ds64; /* post conversion, 64 bit version */
91447636 1589 void *semid_dsp;
b0d623f7
A
1590 size_t ipcs_sz;
1591 size_t semid_ds_sz;
91447636
A
1592 struct proc *p = current_proc();
1593
b0d623f7
A
1594 if (IS_64BIT_PROCESS(p)) {
1595 ipcs_sz = sizeof(struct user_IPCS_command);
1596 semid_ds_sz = sizeof(struct user64_semid_ds);
1597 } else {
1598 ipcs_sz = sizeof(struct user32_IPCS_command);
1599 semid_ds_sz = sizeof(struct user32_semid_ds);
2d21ac55
A
1600 }
1601
91447636
A
1602 /* Copy in the command structure */
1603 if ((error = SYSCTL_IN(req, &ipcs, ipcs_sz)) != 0) {
1604 return(error);
1605 }
1606
2d21ac55
A
1607 if (!IS_64BIT_PROCESS(p)) /* convert in place */
1608 ipcs.u64.ipcs_data = CAST_USER_ADDR_T(ipcs.u32.ipcs_data);
91447636
A
1609
1610 /* Let us version this interface... */
1611 if (ipcs.u64.ipcs_magic != IPCS_MAGIC) {
1612 return(EINVAL);
1613 }
1614
1615 SYSV_SEM_SUBSYS_LOCK();
1616 switch(ipcs.u64.ipcs_op) {
1617 case IPCS_SEM_CONF: /* Obtain global configuration data */
1618 if (ipcs.u64.ipcs_datalen != sizeof(struct seminfo)) {
1619 error = ERANGE;
1620 break;
1621 }
1622 if (ipcs.u64.ipcs_cursor != 0) { /* fwd. compat. */
1623 error = EINVAL;
1624 break;
1625 }
91447636 1626 error = copyout(&seminfo, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
91447636
A
1627 break;
1628
1629 case IPCS_SEM_ITER: /* Iterate over existing segments */
1630 cursor = ipcs.u64.ipcs_cursor;
1631 if (cursor < 0 || cursor >= seminfo.semmni) {
1632 error = ERANGE;
1633 break;
1634 }
1635 if (ipcs.u64.ipcs_datalen != (int)semid_ds_sz ) {
1636 error = EINVAL;
1637 break;
1638 }
1639 for( ; cursor < seminfo.semmni; cursor++) {
2d21ac55 1640 if (sema[cursor].u.sem_perm.mode & SEM_ALLOC)
91447636
A
1641 break;
1642 continue;
1643 }
1644 if (cursor == seminfo.semmni) {
1645 error = ENOENT;
1646 break;
1647 }
1648
2d21ac55 1649 semid_dsp = &sema[cursor].u; /* default: 64 bit */
91447636
A
1650
1651 /*
1652 * If necessary, convert the 64 bit kernel segment
1653 * descriptor to a 32 bit user one.
1654 */
1655 if (!IS_64BIT_PROCESS(p)) {
39236c6e 1656 bzero(&semid_ds32, sizeof(semid_ds32));
b0d623f7 1657 semid_ds_kernelto32(semid_dsp, &semid_ds32);
91447636 1658 semid_dsp = &semid_ds32;
b0d623f7 1659 } else {
39236c6e 1660 bzero(&semid_ds64, sizeof(semid_ds64));
b0d623f7
A
1661 semid_ds_kernelto64(semid_dsp, &semid_ds64);
1662 semid_dsp = &semid_ds64;
91447636 1663 }
b0d623f7 1664
91447636
A
1665 error = copyout(semid_dsp, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
1666 if (!error) {
1667 /* update cursor */
1668 ipcs.u64.ipcs_cursor = cursor + 1;
b0d623f7
A
1669
1670 if (!IS_64BIT_PROCESS(p)) /* convert in place */
1671 ipcs.u32.ipcs_data = CAST_DOWN_EXPLICIT(user32_addr_t,ipcs.u64.ipcs_data);
1672
91447636
A
1673 error = SYSCTL_OUT(req, &ipcs, ipcs_sz);
1674 }
91447636
A
1675 break;
1676
1677 default:
1678 error = EINVAL;
1679 break;
1680 }
1681 SYSV_SEM_SUBSYS_UNLOCK();
1682 return(error);
1683}
1684
1685SYSCTL_DECL(_kern_sysv_ipcs);
6d2010ae 1686SYSCTL_PROC(_kern_sysv_ipcs, OID_AUTO, sem, CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
91447636
A
1687 0, 0, IPCS_sem_sysctl,
1688 "S,IPCS_sem_command",
1689 "ipcs sem command interface");
2d21ac55
A
1690
1691#endif /* SYSV_SEM */