1 /*
2 * linux/ipc/sem.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
5 *
6 * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
7 * This code underwent a massive rewrite in order to solve some problems
8 * with the original code. In particular the original code failed to
9 * wake up processes that were waiting for semval to go to 0 if the
10 * value went to 0 and was then incremented rapidly enough. In solving
11 * this problem I have also modified the implementation so that it
12 * processes pending operations in a FIFO manner, thus give a guarantee
13 * that processes waiting for a lock on the semaphore won't starve
14 * unless another locking process fails to unlock.
15 * In addition the following two changes in behavior have been introduced:
16 * - The original implementation of semop returned the value
17 * last semaphore element examined on success. This does not
18 * match the manual page specifications, and effectively
19 * allows the user to read the semaphore even if they do not
20 * have read permissions. The implementation now returns 0
21 * on success as stated in the manual page.
22 * - There is some confusion over whether the set of undo adjustments
23 * to be performed at exit should be done in an atomic manner.
24 * That is, if we are attempting to decrement the semval should we queue
25 * up and wait until we can do so legally?
26 * The original implementation attempted to do this.
27 * The current implementation does not do so. This is because I don't
28 * think it is the right thing (TM) to do, and because I couldn't
29 * see a clean way to get the old behavior with the new design.
30 * The POSIX standard and SVID should be consulted to determine
31 * what behavior is mandated.
32 *
33 * Further notes on refinement (Christoph Rohland, December 1998):
34 * - The POSIX standard says, that the undo adjustments simply should
35 * redo. So the current implementation is o.K.
36 * - The previous code had two flaws:
37 * 1) It actively gave the semaphore to the next waiting process
38 * sleeping on the semaphore. Since this process did not have the
39 * cpu this led to many unnecessary context switches and bad
40 * performance. Now we only check which process should be able to
41 * get the semaphore and if this process wants to reduce some
42 * semaphore value we simply wake it up without doing the
43 * operation. So it has to try to get it later. Thus e.g. the
44 * running process may reacquire the semaphore during the current
45 * time slice. If it only waits for zero or increases the semaphore,
46 * we do the operation in advance and wake it up.
47 * 2) It did not wake up all zero waiting processes. We try to do
48 * better but only get the semops right which only wait for zero or
49 * increase. If there are decrement operations in the operations
50 * array we do the same as before.
51 *
52 * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
53 * check/retry algorithm for waking up blocked processes as the new scheduler
54 * is better at handling thread switch than the old one.
55 *
56 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
57 *
58 * SMP-threaded, sysctl's added
59 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
60 * Enforced range limit on SEM_UNDO
61 * (c) 2001 Red Hat Inc <alan@redhat.com>
62 * Lockless wakeup
63 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
64 *
65 * support for audit of ipc object properties and permission changes
66 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
67 *
68 * namespaces support
69 * OpenVZ, SWsoft Inc.
70 * Pavel Emelianov <xemul@openvz.org>
71 */
72
73 #include <linux/slab.h>
74 #include <linux/spinlock.h>
75 #include <linux/init.h>
76 #include <linux/proc_fs.h>
77 #include <linux/time.h>
78 #include <linux/security.h>
79 #include <linux/syscalls.h>
80 #include <linux/audit.h>
81 #include <linux/capability.h>
82 #include <linux/seq_file.h>
83 #include <linux/rwsem.h>
84 #include <linux/nsproxy.h>
85 #include <linux/ipc_namespace.h>
86
87 #include <asm/uaccess.h>
88 #include "util.h"
89
90 #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
91
92 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
93 #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
94 #define sem_buildid(id, seq) ipc_buildid(id, seq)
95
96 static int newary(struct ipc_namespace *, struct ipc_params *);
97 static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
98 #ifdef CONFIG_PROC_FS
99 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
100 #endif
101
102 #define SEMMSL_FAST 256 /* 512 bytes on stack */
103 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
104
105 /*
106 * linked list protection:
107 * sem_undo.id_next,
108 * sem_array.sem_pending{,last},
109 * sem_array.sem_undo: sem_lock() for read/write
110 * sem_undo.proc_next: only "current" is allowed to read/write that field.
111 *
112 */
113
114 #define sc_semmsl sem_ctls[0]
115 #define sc_semmns sem_ctls[1]
116 #define sc_semopm sem_ctls[2]
117 #define sc_semmni sem_ctls[3]
118
119 void sem_init_ns(struct ipc_namespace *ns)
120 {
121 ns->sc_semmsl = SEMMSL;
122 ns->sc_semmns = SEMMNS;
123 ns->sc_semopm = SEMOPM;
124 ns->sc_semmni = SEMMNI;
125 ns->used_sems = 0;
126 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
127 }
128
129 #ifdef CONFIG_IPC_NS
130 void sem_exit_ns(struct ipc_namespace *ns)
131 {
132 free_ipcs(ns, &sem_ids(ns), freeary);
133 }
134 #endif
135
136 void __init sem_init (void)
137 {
138 sem_init_ns(&init_ipc_ns);
139 ipc_init_proc_interface("sysvipc/sem",
140 " key semid perms nsems uid gid cuid cgid otime ctime\n",
141 IPC_SEM_IDS, sysvipc_sem_proc_show);
142 }
143
144 /*
145 * This routine is called in the paths where the rw_mutex is held to protect
146 * access to the idr tree.
147 */
148 static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
149 int id)
150 {
151 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
152
153 if (IS_ERR(ipcp))
154 return (struct sem_array *)ipcp;
155
156 return container_of(ipcp, struct sem_array, sem_perm);
157 }
158
159 /*
160 * sem_lock_(check_) routines are called in the paths where the rw_mutex
161 * is not held.
162 */
163 static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
164 {
165 struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
166
167 if (IS_ERR(ipcp))
168 return (struct sem_array *)ipcp;
169
170 return container_of(ipcp, struct sem_array, sem_perm);
171 }
172
173 static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
174 int id)
175 {
176 struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
177
178 if (IS_ERR(ipcp))
179 return (struct sem_array *)ipcp;
180
181 return container_of(ipcp, struct sem_array, sem_perm);
182 }
183
184 static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
185 {
186 ipc_rmid(&sem_ids(ns), &s->sem_perm);
187 }
188
189 /*
190 * Lockless wakeup algorithm:
191 * Without the check/retry algorithm a lockless wakeup is possible:
192 * - queue.status is initialized to -EINTR before blocking.
193 * - wakeup is performed by
194 * * unlinking the queue entry from sma->sem_pending
195 * * setting queue.status to IN_WAKEUP
196 * This is the notification for the blocked thread that a
197 * result value is imminent.
198 * * call wake_up_process
199 * * set queue.status to the final value.
200 * - the previously blocked thread checks queue.status:
201 * * if it's IN_WAKEUP, then it must wait until the value changes
202 * * if it's not -EINTR, then the operation was completed by
203 * update_queue. semtimedop can return queue.status without
204 * performing any operation on the sem array.
205 * * otherwise it must acquire the spinlock and check what's up.
206 *
207 * The two-stage algorithm is necessary to protect against the following
208 * races:
209 * - if queue.status is set after wake_up_process, then the woken up idle
210 * thread could race forward and try (and fail) to acquire sma->lock
211 * before update_queue had a chance to set queue.status
212 * - if queue.status is written before wake_up_process and if the
213 * blocked process is woken up by a signal between writing
214 * queue.status and the wake_up_process, then the woken up
215 * process could return from semtimedop and die by calling
216 * sys_exit before wake_up_process is called. Then wake_up_process
217 * will oops, because the task structure is already invalid.
218 * (yes, this happened on s390 with sysv msg).
219 *
220 */
221 #define IN_WAKEUP 1
222
223 /**
224 * newary - Create a new semaphore set
225 * @ns: namespace
226 * @params: ptr to the structure that contains key, semflg and nsems
227 *
228 * Called with sem_ids.rw_mutex held (as a writer)
229 */
230
231 static int newary(struct ipc_namespace *ns, struct ipc_params *params)
232 {
233 int id;
234 int retval;
235 struct sem_array *sma;
236 int size;
237 key_t key = params->key;
238 int nsems = params->u.nsems;
239 int semflg = params->flg;
240
241 if (!nsems)
242 return -EINVAL;
243 if (ns->used_sems + nsems > ns->sc_semmns)
244 return -ENOSPC;
245
246 size = sizeof (*sma) + nsems * sizeof (struct sem);
247 sma = ipc_rcu_alloc(size);
248 if (!sma) {
249 return -ENOMEM;
250 }
251 memset (sma, 0, size);
252
253 sma->sem_perm.mode = (semflg & S_IRWXUGO);
254 sma->sem_perm.key = key;
255
256 sma->sem_perm.security = NULL;
257 retval = security_sem_alloc(sma);
258 if (retval) {
259 ipc_rcu_putref(sma);
260 return retval;
261 }
262
263 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
264 if (id < 0) {
265 security_sem_free(sma);
266 ipc_rcu_putref(sma);
267 return id;
268 }
269 ns->used_sems += nsems;
270
271 sma->sem_perm.id = sem_buildid(id, sma->sem_perm.seq);
272 sma->sem_base = (struct sem *) &sma[1];
273 /* sma->sem_pending = NULL; */
274 sma->sem_pending_last = &sma->sem_pending;
275 /* sma->undo = NULL; */
276 sma->sem_nsems = nsems;
277 sma->sem_ctime = get_seconds();
278 sem_unlock(sma);
279
280 return sma->sem_perm.id;
281 }
282
283
284 /*
285 * Called with sem_ids.rw_mutex and ipcp locked.
286 */
287 static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
288 {
289 struct sem_array *sma;
290
291 sma = container_of(ipcp, struct sem_array, sem_perm);
292 return security_sem_associate(sma, semflg);
293 }
294
295 /*
296 * Called with sem_ids.rw_mutex and ipcp locked.
297 */
298 static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
299 struct ipc_params *params)
300 {
301 struct sem_array *sma;
302
303 sma = container_of(ipcp, struct sem_array, sem_perm);
304 if (params->u.nsems > sma->sem_nsems)
305 return -EINVAL;
306
307 return 0;
308 }
309
310 asmlinkage long sys_semget(key_t key, int nsems, int semflg)
311 {
312 struct ipc_namespace *ns;
313 struct ipc_ops sem_ops;
314 struct ipc_params sem_params;
315
316 ns = current->nsproxy->ipc_ns;
317
318 if (nsems < 0 || nsems > ns->sc_semmsl)
319 return -EINVAL;
320
321 sem_ops.getnew = newary;
322 sem_ops.associate = sem_security;
323 sem_ops.more_checks = sem_more_checks;
324
325 sem_params.key = key;
326 sem_params.flg = semflg;
327 sem_params.u.nsems = nsems;
328
329 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
330 }
331
332 /* Manage the doubly linked list sma->sem_pending as a FIFO:
333 * insert new queue elements at the tail sma->sem_pending_last.
334 */
335 static inline void append_to_queue (struct sem_array * sma,
336 struct sem_queue * q)
337 {
338 *(q->prev = sma->sem_pending_last) = q;
339 *(sma->sem_pending_last = &q->next) = NULL;
340 }
341
342 static inline void prepend_to_queue (struct sem_array * sma,
343 struct sem_queue * q)
344 {
345 q->next = sma->sem_pending;
346 *(q->prev = &sma->sem_pending) = q;
347 if (q->next)
348 q->next->prev = &q->next;
349 else /* sma->sem_pending_last == &sma->sem_pending */
350 sma->sem_pending_last = &q->next;
351 }
352
353 static inline void remove_from_queue (struct sem_array * sma,
354 struct sem_queue * q)
355 {
356 *(q->prev) = q->next;
357 if (q->next)
358 q->next->prev = q->prev;
359 else /* sma->sem_pending_last == &q->next */
360 sma->sem_pending_last = q->prev;
361 q->prev = NULL; /* mark as removed */
362 }
363
364 /*
365 * Determine whether a sequence of semaphore operations would succeed
366 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
367 */
368
369 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
370 int nsops, struct sem_undo *un, int pid)
371 {
372 int result, sem_op;
373 struct sembuf *sop;
374 struct sem * curr;
375
376 for (sop = sops; sop < sops + nsops; sop++) {
377 curr = sma->sem_base + sop->sem_num;
378 sem_op = sop->sem_op;
379 result = curr->semval;
380
381 if (!sem_op && result)
382 goto would_block;
383
384 result += sem_op;
385 if (result < 0)
386 goto would_block;
387 if (result > SEMVMX)
388 goto out_of_range;
389 if (sop->sem_flg & SEM_UNDO) {
390 int undo = un->semadj[sop->sem_num] - sem_op;
391 /*
392 * Exceeding the undo range is an error.
393 */
394 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
395 goto out_of_range;
396 }
397 curr->semval = result;
398 }
399
400 sop--;
401 while (sop >= sops) {
402 sma->sem_base[sop->sem_num].sempid = pid;
403 if (sop->sem_flg & SEM_UNDO)
404 un->semadj[sop->sem_num] -= sop->sem_op;
405 sop--;
406 }
407
408 sma->sem_otime = get_seconds();
409 return 0;
410
411 out_of_range:
412 result = -ERANGE;
413 goto undo;
414
415 would_block:
416 if (sop->sem_flg & IPC_NOWAIT)
417 result = -EAGAIN;
418 else
419 result = 1;
420
421 undo:
422 sop--;
423 while (sop >= sops) {
424 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
425 sop--;
426 }
427
428 return result;
429 }
430
431 /* Go through the pending queue for the indicated semaphore
432 * looking for tasks that can be completed.
433 */
434 static void update_queue (struct sem_array * sma)
435 {
436 int error;
437 struct sem_queue * q;
438
439 q = sma->sem_pending;
440 while(q) {
441 error = try_atomic_semop(sma, q->sops, q->nsops,
442 q->undo, q->pid);
443
444 /* Does q->sleeper still need to sleep? */
445 if (error <= 0) {
446 struct sem_queue *n;
447 remove_from_queue(sma,q);
448 /*
449 * make sure that the wakeup doesnt preempt
450 * _this_ cpu prematurely. (on preempt_rt)
451 */
452 preempt_disable();
453 q->status = IN_WAKEUP;
454 /*
455 * Continue scanning. The next operation
456 * that must be checked depends on the type of the
457 * completed operation:
458 * - if the operation modified the array, then
459 * restart from the head of the queue and
460 * check for threads that might be waiting
461 * for semaphore values to become 0.
462 * - if the operation didn't modify the array,
463 * then just continue.
464 */
465 if (q->alter)
466 n = sma->sem_pending;
467 else
468 n = q->next;
469 wake_up_process(q->sleeper);
470 /* hands-off: q will disappear immediately after
471 * writing q->status.
472 */
473 smp_wmb();
474 q->status = error;
475 preempt_enable();
476 q = n;
477 } else {
478 q = q->next;
479 }
480 }
481 }
482
483 /* The following counts are associated to each semaphore:
484 * semncnt number of tasks waiting on semval being nonzero
485 * semzcnt number of tasks waiting on semval being zero
486 * This model assumes that a task waits on exactly one semaphore.
487 * Since semaphore operations are to be performed atomically, tasks actually
488 * wait on a whole sequence of semaphores simultaneously.
489 * The counts we return here are a rough approximation, but still
490 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
491 */
492 static int count_semncnt (struct sem_array * sma, ushort semnum)
493 {
494 int semncnt;
495 struct sem_queue * q;
496
497 semncnt = 0;
498 for (q = sma->sem_pending; q; q = q->next) {
499 struct sembuf * sops = q->sops;
500 int nsops = q->nsops;
501 int i;
502 for (i = 0; i < nsops; i++)
503 if (sops[i].sem_num == semnum
504 && (sops[i].sem_op < 0)
505 && !(sops[i].sem_flg & IPC_NOWAIT))
506 semncnt++;
507 }
508 return semncnt;
509 }
510 static int count_semzcnt (struct sem_array * sma, ushort semnum)
511 {
512 int semzcnt;
513 struct sem_queue * q;
514
515 semzcnt = 0;
516 for (q = sma->sem_pending; q; q = q->next) {
517 struct sembuf * sops = q->sops;
518 int nsops = q->nsops;
519 int i;
520 for (i = 0; i < nsops; i++)
521 if (sops[i].sem_num == semnum
522 && (sops[i].sem_op == 0)
523 && !(sops[i].sem_flg & IPC_NOWAIT))
524 semzcnt++;
525 }
526 return semzcnt;
527 }
528
529 /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
530 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
531 * remains locked on exit.
532 */
533 static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
534 {
535 struct sem_undo *un;
536 struct sem_queue *q;
537 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
538
539 /* Invalidate the existing undo structures for this semaphore set.
540 * (They will be freed without any further action in exit_sem()
541 * or during the next semop.)
542 */
543 for (un = sma->undo; un; un = un->id_next)
544 un->semid = -1;
545
546 /* Wake up all pending processes and let them fail with EIDRM. */
547 q = sma->sem_pending;
548 while(q) {
549 struct sem_queue *n;
550 /* lazy remove_from_queue: we are killing the whole queue */
551 q->prev = NULL;
552 n = q->next;
553 q->status = IN_WAKEUP;
554 wake_up_process(q->sleeper); /* doesn't sleep */
555 smp_wmb();
556 q->status = -EIDRM; /* hands-off q */
557 q = n;
558 }
559
560 /* Remove the semaphore set from the IDR */
561 sem_rmid(ns, sma);
562 sem_unlock(sma);
563
564 ns->used_sems -= sma->sem_nsems;
565 security_sem_free(sma);
566 ipc_rcu_putref(sma);
567 }
568
569 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
570 {
571 switch(version) {
572 case IPC_64:
573 return copy_to_user(buf, in, sizeof(*in));
574 case IPC_OLD:
575 {
576 struct semid_ds out;
577
578 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
579
580 out.sem_otime = in->sem_otime;
581 out.sem_ctime = in->sem_ctime;
582 out.sem_nsems = in->sem_nsems;
583
584 return copy_to_user(buf, &out, sizeof(out));
585 }
586 default:
587 return -EINVAL;
588 }
589 }
590
591 static int semctl_nolock(struct ipc_namespace *ns, int semid,
592 int cmd, int version, union semun arg)
593 {
594 int err = -EINVAL;
595 struct sem_array *sma;
596
597 switch(cmd) {
598 case IPC_INFO:
599 case SEM_INFO:
600 {
601 struct seminfo seminfo;
602 int max_id;
603
604 err = security_sem_semctl(NULL, cmd);
605 if (err)
606 return err;
607
608 memset(&seminfo,0,sizeof(seminfo));
609 seminfo.semmni = ns->sc_semmni;
610 seminfo.semmns = ns->sc_semmns;
611 seminfo.semmsl = ns->sc_semmsl;
612 seminfo.semopm = ns->sc_semopm;
613 seminfo.semvmx = SEMVMX;
614 seminfo.semmnu = SEMMNU;
615 seminfo.semmap = SEMMAP;
616 seminfo.semume = SEMUME;
617 down_read(&sem_ids(ns).rw_mutex);
618 if (cmd == SEM_INFO) {
619 seminfo.semusz = sem_ids(ns).in_use;
620 seminfo.semaem = ns->used_sems;
621 } else {
622 seminfo.semusz = SEMUSZ;
623 seminfo.semaem = SEMAEM;
624 }
625 max_id = ipc_get_maxid(&sem_ids(ns));
626 up_read(&sem_ids(ns).rw_mutex);
627 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
628 return -EFAULT;
629 return (max_id < 0) ? 0: max_id;
630 }
631 case IPC_STAT:
632 case SEM_STAT:
633 {
634 struct semid64_ds tbuf;
635 int id;
636
637 if (cmd == SEM_STAT) {
638 sma = sem_lock(ns, semid);
639 if (IS_ERR(sma))
640 return PTR_ERR(sma);
641 id = sma->sem_perm.id;
642 } else {
643 sma = sem_lock_check(ns, semid);
644 if (IS_ERR(sma))
645 return PTR_ERR(sma);
646 id = 0;
647 }
648
649 err = -EACCES;
650 if (ipcperms (&sma->sem_perm, S_IRUGO))
651 goto out_unlock;
652
653 err = security_sem_semctl(sma, cmd);
654 if (err)
655 goto out_unlock;
656
657 memset(&tbuf, 0, sizeof(tbuf));
658
659 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
660 tbuf.sem_otime = sma->sem_otime;
661 tbuf.sem_ctime = sma->sem_ctime;
662 tbuf.sem_nsems = sma->sem_nsems;
663 sem_unlock(sma);
664 if (copy_semid_to_user (arg.buf, &tbuf, version))
665 return -EFAULT;
666 return id;
667 }
668 default:
669 return -EINVAL;
670 }
671 return err;
672 out_unlock:
673 sem_unlock(sma);
674 return err;
675 }
676
677 static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
678 int cmd, int version, union semun arg)
679 {
680 struct sem_array *sma;
681 struct sem* curr;
682 int err;
683 ushort fast_sem_io[SEMMSL_FAST];
684 ushort* sem_io = fast_sem_io;
685 int nsems;
686
687 sma = sem_lock_check(ns, semid);
688 if (IS_ERR(sma))
689 return PTR_ERR(sma);
690
691 nsems = sma->sem_nsems;
692
693 err = -EACCES;
694 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
695 goto out_unlock;
696
697 err = security_sem_semctl(sma, cmd);
698 if (err)
699 goto out_unlock;
700
701 err = -EACCES;
702 switch (cmd) {
703 case GETALL:
704 {
705 ushort __user *array = arg.array;
706 int i;
707
708 if(nsems > SEMMSL_FAST) {
709 ipc_rcu_getref(sma);
710 sem_unlock(sma);
711
712 sem_io = ipc_alloc(sizeof(ushort)*nsems);
713 if(sem_io == NULL) {
714 ipc_lock_by_ptr(&sma->sem_perm);
715 ipc_rcu_putref(sma);
716 sem_unlock(sma);
717 return -ENOMEM;
718 }
719
720 ipc_lock_by_ptr(&sma->sem_perm);
721 ipc_rcu_putref(sma);
722 if (sma->sem_perm.deleted) {
723 sem_unlock(sma);
724 err = -EIDRM;
725 goto out_free;
726 }
727 }
728
729 for (i = 0; i < sma->sem_nsems; i++)
730 sem_io[i] = sma->sem_base[i].semval;
731 sem_unlock(sma);
732 err = 0;
733 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
734 err = -EFAULT;
735 goto out_free;
736 }
737 case SETALL:
738 {
739 int i;
740 struct sem_undo *un;
741
742 ipc_rcu_getref(sma);
743 sem_unlock(sma);
744
745 if(nsems > SEMMSL_FAST) {
746 sem_io = ipc_alloc(sizeof(ushort)*nsems);
747 if(sem_io == NULL) {
748 ipc_lock_by_ptr(&sma->sem_perm);
749 ipc_rcu_putref(sma);
750 sem_unlock(sma);
751 return -ENOMEM;
752 }
753 }
754
755 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
756 ipc_lock_by_ptr(&sma->sem_perm);
757 ipc_rcu_putref(sma);
758 sem_unlock(sma);
759 err = -EFAULT;
760 goto out_free;
761 }
762
763 for (i = 0; i < nsems; i++) {
764 if (sem_io[i] > SEMVMX) {
765 ipc_lock_by_ptr(&sma->sem_perm);
766 ipc_rcu_putref(sma);
767 sem_unlock(sma);
768 err = -ERANGE;
769 goto out_free;
770 }
771 }
772 ipc_lock_by_ptr(&sma->sem_perm);
773 ipc_rcu_putref(sma);
774 if (sma->sem_perm.deleted) {
775 sem_unlock(sma);
776 err = -EIDRM;
777 goto out_free;
778 }
779
780 for (i = 0; i < nsems; i++)
781 sma->sem_base[i].semval = sem_io[i];
782 for (un = sma->undo; un; un = un->id_next)
783 for (i = 0; i < nsems; i++)
784 un->semadj[i] = 0;
785 sma->sem_ctime = get_seconds();
786 /* maybe some queued-up processes were waiting for this */
787 update_queue(sma);
788 err = 0;
789 goto out_unlock;
790 }
791 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
792 }
793 err = -EINVAL;
794 if(semnum < 0 || semnum >= nsems)
795 goto out_unlock;
796
797 curr = &sma->sem_base[semnum];
798
799 switch (cmd) {
800 case GETVAL:
801 err = curr->semval;
802 goto out_unlock;
803 case GETPID:
804 err = curr->sempid;
805 goto out_unlock;
806 case GETNCNT:
807 err = count_semncnt(sma,semnum);
808 goto out_unlock;
809 case GETZCNT:
810 err = count_semzcnt(sma,semnum);
811 goto out_unlock;
812 case SETVAL:
813 {
814 int val = arg.val;
815 struct sem_undo *un;
816 err = -ERANGE;
817 if (val > SEMVMX || val < 0)
818 goto out_unlock;
819
820 for (un = sma->undo; un; un = un->id_next)
821 un->semadj[semnum] = 0;
822 curr->semval = val;
823 curr->sempid = task_tgid_vnr(current);
824 sma->sem_ctime = get_seconds();
825 /* maybe some queued-up processes were waiting for this */
826 update_queue(sma);
827 err = 0;
828 goto out_unlock;
829 }
830 }
831 out_unlock:
832 sem_unlock(sma);
833 out_free:
834 if(sem_io != fast_sem_io)
835 ipc_free(sem_io, sizeof(ushort)*nsems);
836 return err;
837 }
838
839 struct sem_setbuf {
840 uid_t uid;
841 gid_t gid;
842 mode_t mode;
843 };
844
845 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
846 {
847 switch(version) {
848 case IPC_64:
849 {
850 struct semid64_ds tbuf;
851
852 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
853 return -EFAULT;
854
855 out->uid = tbuf.sem_perm.uid;
856 out->gid = tbuf.sem_perm.gid;
857 out->mode = tbuf.sem_perm.mode;
858
859 return 0;
860 }
861 case IPC_OLD:
862 {
863 struct semid_ds tbuf_old;
864
865 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
866 return -EFAULT;
867
868 out->uid = tbuf_old.sem_perm.uid;
869 out->gid = tbuf_old.sem_perm.gid;
870 out->mode = tbuf_old.sem_perm.mode;
871
872 return 0;
873 }
874 default:
875 return -EINVAL;
876 }
877 }
878
879 static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
880 int cmd, int version, union semun arg)
881 {
882 struct sem_array *sma;
883 int err;
884 struct sem_setbuf uninitialized_var(setbuf);
885 struct kern_ipc_perm *ipcp;
886
887 if(cmd == IPC_SET) {
888 if(copy_semid_from_user (&setbuf, arg.buf, version))
889 return -EFAULT;
890 }
891 sma = sem_lock_check_down(ns, semid);
892 if (IS_ERR(sma))
893 return PTR_ERR(sma);
894
895 ipcp = &sma->sem_perm;
896
897 err = audit_ipc_obj(ipcp);
898 if (err)
899 goto out_unlock;
900
901 if (cmd == IPC_SET) {
902 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
903 if (err)
904 goto out_unlock;
905 }
906 if (current->euid != ipcp->cuid &&
907 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
908 err=-EPERM;
909 goto out_unlock;
910 }
911
912 err = security_sem_semctl(sma, cmd);
913 if (err)
914 goto out_unlock;
915
916 switch(cmd){
917 case IPC_RMID:
918 freeary(ns, ipcp);
919 err = 0;
920 break;
921 case IPC_SET:
922 ipcp->uid = setbuf.uid;
923 ipcp->gid = setbuf.gid;
924 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
925 | (setbuf.mode & S_IRWXUGO);
926 sma->sem_ctime = get_seconds();
927 sem_unlock(sma);
928 err = 0;
929 break;
930 default:
931 sem_unlock(sma);
932 err = -EINVAL;
933 break;
934 }
935 return err;
936
937 out_unlock:
938 sem_unlock(sma);
939 return err;
940 }
941
942 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
943 {
944 int err = -EINVAL;
945 int version;
946 struct ipc_namespace *ns;
947
948 if (semid < 0)
949 return -EINVAL;
950
951 version = ipc_parse_version(&cmd);
952 ns = current->nsproxy->ipc_ns;
953
954 switch(cmd) {
955 case IPC_INFO:
956 case SEM_INFO:
957 case IPC_STAT:
958 case SEM_STAT:
959 err = semctl_nolock(ns, semid, cmd, version, arg);
960 return err;
961 case GETALL:
962 case GETVAL:
963 case GETPID:
964 case GETNCNT:
965 case GETZCNT:
966 case SETVAL:
967 case SETALL:
968 err = semctl_main(ns,semid,semnum,cmd,version,arg);
969 return err;
970 case IPC_RMID:
971 case IPC_SET:
972 down_write(&sem_ids(ns).rw_mutex);
973 err = semctl_down(ns,semid,semnum,cmd,version,arg);
974 up_write(&sem_ids(ns).rw_mutex);
975 return err;
976 default:
977 return -EINVAL;
978 }
979 }
980
981 /* If the task doesn't already have a undo_list, then allocate one
982 * here. We guarantee there is only one thread using this undo list,
983 * and current is THE ONE
984 *
985 * If this allocation and assignment succeeds, but later
986 * portions of this code fail, there is no need to free the sem_undo_list.
987 * Just let it stay associated with the task, and it'll be freed later
988 * at exit time.
989 *
990 * This can block, so callers must hold no locks.
991 */
992 static inline int get_undo_list(struct sem_undo_list **undo_listp)
993 {
994 struct sem_undo_list *undo_list;
995
996 undo_list = current->sysvsem.undo_list;
997 if (!undo_list) {
998 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
999 if (undo_list == NULL)
1000 return -ENOMEM;
1001 spin_lock_init(&undo_list->lock);
1002 atomic_set(&undo_list->refcnt, 1);
1003 current->sysvsem.undo_list = undo_list;
1004 }
1005 *undo_listp = undo_list;
1006 return 0;
1007 }
1008
1009 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1010 {
1011 struct sem_undo **last, *un;
1012
1013 last = &ulp->proc_list;
1014 un = *last;
1015 while(un != NULL) {
1016 if(un->semid==semid)
1017 break;
1018 if(un->semid==-1) {
1019 *last=un->proc_next;
1020 kfree(un);
1021 } else {
1022 last=&un->proc_next;
1023 }
1024 un=*last;
1025 }
1026 return un;
1027 }
1028
1029 static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
1030 {
1031 struct sem_array *sma;
1032 struct sem_undo_list *ulp;
1033 struct sem_undo *un, *new;
1034 int nsems;
1035 int error;
1036
1037 error = get_undo_list(&ulp);
1038 if (error)
1039 return ERR_PTR(error);
1040
1041 spin_lock(&ulp->lock);
1042 un = lookup_undo(ulp, semid);
1043 spin_unlock(&ulp->lock);
1044 if (likely(un!=NULL))
1045 goto out;
1046
1047 /* no undo structure around - allocate one. */
1048 sma = sem_lock_check(ns, semid);
1049 if (IS_ERR(sma))
1050 return ERR_PTR(PTR_ERR(sma));
1051
1052 nsems = sma->sem_nsems;
1053 ipc_rcu_getref(sma);
1054 sem_unlock(sma);
1055
1056 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1057 if (!new) {
1058 ipc_lock_by_ptr(&sma->sem_perm);
1059 ipc_rcu_putref(sma);
1060 sem_unlock(sma);
1061 return ERR_PTR(-ENOMEM);
1062 }
1063 new->semadj = (short *) &new[1];
1064 new->semid = semid;
1065
1066 spin_lock(&ulp->lock);
1067 un = lookup_undo(ulp, semid);
1068 if (un) {
1069 spin_unlock(&ulp->lock);
1070 kfree(new);
1071 ipc_lock_by_ptr(&sma->sem_perm);
1072 ipc_rcu_putref(sma);
1073 sem_unlock(sma);
1074 goto out;
1075 }
1076 ipc_lock_by_ptr(&sma->sem_perm);
1077 ipc_rcu_putref(sma);
1078 if (sma->sem_perm.deleted) {
1079 sem_unlock(sma);
1080 spin_unlock(&ulp->lock);
1081 kfree(new);
1082 un = ERR_PTR(-EIDRM);
1083 goto out;
1084 }
1085 new->proc_next = ulp->proc_list;
1086 ulp->proc_list = new;
1087 new->id_next = sma->undo;
1088 sma->undo = new;
1089 sem_unlock(sma);
1090 un = new;
1091 spin_unlock(&ulp->lock);
1092 out:
1093 return un;
1094 }
1095
1096 asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1097 unsigned nsops, const struct timespec __user *timeout)
1098 {
1099 int error = -EINVAL;
1100 struct sem_array *sma;
1101 struct sembuf fast_sops[SEMOPM_FAST];
1102 struct sembuf* sops = fast_sops, *sop;
1103 struct sem_undo *un;
1104 int undos = 0, alter = 0, max;
1105 struct sem_queue queue;
1106 unsigned long jiffies_left = 0;
1107 struct ipc_namespace *ns;
1108
1109 ns = current->nsproxy->ipc_ns;
1110
1111 if (nsops < 1 || semid < 0)
1112 return -EINVAL;
1113 if (nsops > ns->sc_semopm)
1114 return -E2BIG;
1115 if(nsops > SEMOPM_FAST) {
1116 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1117 if(sops==NULL)
1118 return -ENOMEM;
1119 }
1120 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1121 error=-EFAULT;
1122 goto out_free;
1123 }
1124 if (timeout) {
1125 struct timespec _timeout;
1126 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1127 error = -EFAULT;
1128 goto out_free;
1129 }
1130 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1131 _timeout.tv_nsec >= 1000000000L) {
1132 error = -EINVAL;
1133 goto out_free;
1134 }
1135 jiffies_left = timespec_to_jiffies(&_timeout);
1136 }
1137 max = 0;
1138 for (sop = sops; sop < sops + nsops; sop++) {
1139 if (sop->sem_num >= max)
1140 max = sop->sem_num;
1141 if (sop->sem_flg & SEM_UNDO)
1142 undos = 1;
1143 if (sop->sem_op != 0)
1144 alter = 1;
1145 }
1146
1147 retry_undos:
1148 if (undos) {
1149 un = find_undo(ns, semid);
1150 if (IS_ERR(un)) {
1151 error = PTR_ERR(un);
1152 goto out_free;
1153 }
1154 } else
1155 un = NULL;
1156
1157 sma = sem_lock_check(ns, semid);
1158 if (IS_ERR(sma)) {
1159 error = PTR_ERR(sma);
1160 goto out_free;
1161 }
1162
1163 /*
1164 * semid identifiers are not unique - find_undo may have
1165 * allocated an undo structure, it was invalidated by an RMID
1166 * and now a new array with received the same id. Check and retry.
1167 */
1168 if (un && un->semid == -1) {
1169 sem_unlock(sma);
1170 goto retry_undos;
1171 }
1172 error = -EFBIG;
1173 if (max >= sma->sem_nsems)
1174 goto out_unlock_free;
1175
1176 error = -EACCES;
1177 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1178 goto out_unlock_free;
1179
1180 error = security_sem_semop(sma, sops, nsops, alter);
1181 if (error)
1182 goto out_unlock_free;
1183
1184 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
1185 if (error <= 0) {
1186 if (alter && error == 0)
1187 update_queue (sma);
1188 goto out_unlock_free;
1189 }
1190
1191 /* We need to sleep on this operation, so we put the current
1192 * task into the pending queue and go to sleep.
1193 */
1194
1195 queue.sma = sma;
1196 queue.sops = sops;
1197 queue.nsops = nsops;
1198 queue.undo = un;
1199 queue.pid = task_tgid_vnr(current);
1200 queue.id = semid;
1201 queue.alter = alter;
1202 if (alter)
1203 append_to_queue(sma ,&queue);
1204 else
1205 prepend_to_queue(sma ,&queue);
1206
1207 queue.status = -EINTR;
1208 queue.sleeper = current;
1209 current->state = TASK_INTERRUPTIBLE;
1210 sem_unlock(sma);
1211
1212 if (timeout)
1213 jiffies_left = schedule_timeout(jiffies_left);
1214 else
1215 schedule();
1216
1217 error = queue.status;
1218 while(unlikely(error == IN_WAKEUP)) {
1219 cpu_relax();
1220 error = queue.status;
1221 }
1222
1223 if (error != -EINTR) {
1224 /* fast path: update_queue already obtained all requested
1225 * resources */
1226 goto out_free;
1227 }
1228
1229 sma = sem_lock(ns, semid);
1230 if (IS_ERR(sma)) {
1231 BUG_ON(queue.prev != NULL);
1232 error = -EIDRM;
1233 goto out_free;
1234 }
1235
1236 /*
1237 * If queue.status != -EINTR we are woken up by another process
1238 */
1239 error = queue.status;
1240 if (error != -EINTR) {
1241 goto out_unlock_free;
1242 }
1243
1244 /*
1245 * If an interrupt occurred we have to clean up the queue
1246 */
1247 if (timeout && jiffies_left == 0)
1248 error = -EAGAIN;
1249 remove_from_queue(sma,&queue);
1250 goto out_unlock_free;
1251
1252 out_unlock_free:
1253 sem_unlock(sma);
1254 out_free:
1255 if(sops != fast_sops)
1256 kfree(sops);
1257 return error;
1258 }
1259
1260 asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1261 {
1262 return sys_semtimedop(semid, tsops, nsops, NULL);
1263 }
1264
1265 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1266 * parent and child tasks.
1267 */
1268
1269 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1270 {
1271 struct sem_undo_list *undo_list;
1272 int error;
1273
1274 if (clone_flags & CLONE_SYSVSEM) {
1275 error = get_undo_list(&undo_list);
1276 if (error)
1277 return error;
1278 atomic_inc(&undo_list->refcnt);
1279 tsk->sysvsem.undo_list = undo_list;
1280 } else
1281 tsk->sysvsem.undo_list = NULL;
1282
1283 return 0;
1284 }
1285
1286 /*
1287 * add semadj values to semaphores, free undo structures.
1288 * undo structures are not freed when semaphore arrays are destroyed
1289 * so some of them may be out of date.
1290 * IMPLEMENTATION NOTE: There is some confusion over whether the
1291 * set of adjustments that needs to be done should be done in an atomic
1292 * manner or not. That is, if we are attempting to decrement the semval
1293 * should we queue up and wait until we can do so legally?
1294 * The original implementation attempted to do this (queue and wait).
1295 * The current implementation does not do so. The POSIX standard
1296 * and SVID should be consulted to determine what behavior is mandated.
1297 */
1298 void exit_sem(struct task_struct *tsk)
1299 {
1300 struct sem_undo_list *undo_list;
1301 struct sem_undo *u, **up;
1302 struct ipc_namespace *ns;
1303
1304 undo_list = tsk->sysvsem.undo_list;
1305 if (!undo_list)
1306 return;
1307
1308 if (!atomic_dec_and_test(&undo_list->refcnt))
1309 return;
1310
1311 ns = tsk->nsproxy->ipc_ns;
1312 /* There's no need to hold the semundo list lock, as current
1313 * is the last task exiting for this undo list.
1314 */
1315 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1316 struct sem_array *sma;
1317 int nsems, i;
1318 struct sem_undo *un, **unp;
1319 int semid;
1320
1321 semid = u->semid;
1322
1323 if(semid == -1)
1324 continue;
1325 sma = sem_lock(ns, semid);
1326 if (IS_ERR(sma))
1327 continue;
1328
1329 if (u->semid == -1)
1330 goto next_entry;
1331
1332 BUG_ON(sem_checkid(sma, u->semid));
1333
1334 /* remove u from the sma->undo list */
1335 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1336 if (u == un)
1337 goto found;
1338 }
1339 printk ("exit_sem undo list error id=%d\n", u->semid);
1340 goto next_entry;
1341 found:
1342 *unp = un->id_next;
1343 /* perform adjustments registered in u */
1344 nsems = sma->sem_nsems;
1345 for (i = 0; i < nsems; i++) {
1346 struct sem * semaphore = &sma->sem_base[i];
1347 if (u->semadj[i]) {
1348 semaphore->semval += u->semadj[i];
1349 /*
1350 * Range checks of the new semaphore value,
1351 * not defined by sus:
1352 * - Some unices ignore the undo entirely
1353 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1354 * - some cap the value (e.g. FreeBSD caps
1355 * at 0, but doesn't enforce SEMVMX)
1356 *
1357 * Linux caps the semaphore value, both at 0
1358 * and at SEMVMX.
1359 *
1360 * Manfred <manfred@colorfullife.com>
1361 */
1362 if (semaphore->semval < 0)
1363 semaphore->semval = 0;
1364 if (semaphore->semval > SEMVMX)
1365 semaphore->semval = SEMVMX;
1366 semaphore->sempid = task_tgid_vnr(current);
1367 }
1368 }
1369 sma->sem_otime = get_seconds();
1370 /* maybe some queued-up processes were waiting for this */
1371 update_queue(sma);
1372 next_entry:
1373 sem_unlock(sma);
1374 }
1375 kfree(undo_list);
1376 }
1377
1378 #ifdef CONFIG_PROC_FS
1379 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1380 {
1381 struct sem_array *sma = it;
1382
1383 return seq_printf(s,
1384 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1385 sma->sem_perm.key,
1386 sma->sem_perm.id,
1387 sma->sem_perm.mode,
1388 sma->sem_nsems,
1389 sma->sem_perm.uid,
1390 sma->sem_perm.gid,
1391 sma->sem_perm.cuid,
1392 sma->sem_perm.cgid,
1393 sma->sem_otime,
1394 sma->sem_ctime);
1395 }
1396 #endif
1397
|
This page was automatically generated by the
LXR engine.
|