1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/mm.h>
21#include <linux/shm.h>
22#include <linux/init.h>
23#include <linux/msg.h>
24#include <linux/vmalloc.h>
25#include <linux/slab.h>
26#include <linux/capability.h>
27#include <linux/highuid.h>
28#include <linux/security.h>
29#include <linux/rcupdate.h>
30#include <linux/workqueue.h>
31#include <linux/seq_file.h>
32#include <linux/proc_fs.h>
33#include <linux/audit.h>
34#include <linux/nsproxy.h>
35#include <linux/rwsem.h>
36#include <linux/ipc_namespace.h>
37
38#include <asm/unistd.h>
39
40#include "util.h"
41
42struct ipc_proc_iface {
43 const char *path;
44 const char *header;
45 int ids;
46 int (*show)(struct seq_file *, void *);
47};
48
49struct ipc_namespace init_ipc_ns = {
50 .kref = {
51 .refcount = ATOMIC_INIT(2),
52 },
53};
54
55
56
57
58
59
60
61
62static int __init ipc_init(void)
63{
64 sem_init();
65 msg_init();
66 shm_init();
67 return 0;
68}
69__initcall(ipc_init);
70
71
72
73
74
75
76
77
78
79void ipc_init_ids(struct ipc_ids *ids)
80{
81 init_rwsem(&ids->rw_mutex);
82
83 ids->in_use = 0;
84 ids->seq = 0;
85 {
86 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
87 if(seq_limit > USHRT_MAX)
88 ids->seq_max = USHRT_MAX;
89 else
90 ids->seq_max = seq_limit;
91 }
92
93 idr_init(&ids->ipcs_idr);
94}
95
96#ifdef CONFIG_PROC_FS
97static const struct file_operations sysvipc_proc_fops;
98
99
100
101
102
103
104
105void __init ipc_init_proc_interface(const char *path, const char *header,
106 int ids, int (*show)(struct seq_file *, void *))
107{
108 struct proc_dir_entry *pde;
109 struct ipc_proc_iface *iface;
110
111 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
112 if (!iface)
113 return;
114 iface->path = path;
115 iface->header = header;
116 iface->ids = ids;
117 iface->show = show;
118
119 pde = create_proc_entry(path,
120 S_IRUGO,
121 NULL );
122 if (pde) {
123 pde->data = iface;
124 pde->proc_fops = &sysvipc_proc_fops;
125 } else {
126 kfree(iface);
127 }
128}
129#endif
130
131
132
133
134
135
136
137
138
139
140
141
142static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
143{
144 struct kern_ipc_perm *ipc;
145 int next_id;
146 int total;
147
148 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
149 ipc = idr_find(&ids->ipcs_idr, next_id);
150
151 if (ipc == NULL)
152 continue;
153
154 if (ipc->key != key) {
155 total++;
156 continue;
157 }
158
159 ipc_lock_by_ptr(ipc);
160 return ipc;
161 }
162
163 return NULL;
164}
165
166
167
168
169
170
171
172
173int ipc_get_maxid(struct ipc_ids *ids)
174{
175 struct kern_ipc_perm *ipc;
176 int max_id = -1;
177 int total, id;
178
179 if (ids->in_use == 0)
180 return -1;
181
182 if (ids->in_use == IPCMNI)
183 return IPCMNI - 1;
184
185
186 total = 0;
187 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
188 ipc = idr_find(&ids->ipcs_idr, id);
189 if (ipc != NULL) {
190 max_id = id;
191 total++;
192 }
193 }
194 return max_id;
195}
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
212{
213 int id, err;
214
215 if (size > IPCMNI)
216 size = IPCMNI;
217
218 if (ids->in_use >= size)
219 return -ENOSPC;
220
221 err = idr_get_new(&ids->ipcs_idr, new, &id);
222 if (err)
223 return err;
224
225 ids->in_use++;
226
227 new->cuid = new->uid = current->euid;
228 new->gid = new->cgid = current->egid;
229
230 new->seq = ids->seq++;
231 if(ids->seq > ids->seq_max)
232 ids->seq = 0;
233
234 spin_lock_init(&new->lock);
235 new->deleted = 0;
236 rcu_read_lock();
237 spin_lock(&new->lock);
238 return id;
239}
240
241
242
243
244
245
246
247
248
249
250
251static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
252 struct ipc_ops *ops, struct ipc_params *params)
253{
254 int err;
255retry:
256 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
257
258 if (!err)
259 return -ENOMEM;
260
261 down_write(&ids->rw_mutex);
262 err = ops->getnew(ns, params);
263 up_write(&ids->rw_mutex);
264
265 if (err == -EAGAIN)
266 goto retry;
267
268 return err;
269}
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
286 struct ipc_params *params)
287{
288 int err;
289
290 if (ipcperms(ipcp, params->flg))
291 err = -EACCES;
292 else {
293 err = ops->associate(ipcp, params->flg);
294 if (!err)
295 err = ipcp->id;
296 }
297
298 return err;
299}
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
316 struct ipc_ops *ops, struct ipc_params *params)
317{
318 struct kern_ipc_perm *ipcp;
319 int flg = params->flg;
320 int err;
321retry:
322 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
323
324
325
326
327
328 down_write(&ids->rw_mutex);
329 ipcp = ipc_findkey(ids, params->key);
330 if (ipcp == NULL) {
331
332 if (!(flg & IPC_CREAT))
333 err = -ENOENT;
334 else if (!err)
335 err = -ENOMEM;
336 else
337 err = ops->getnew(ns, params);
338 } else {
339
340
341 if (flg & IPC_CREAT && flg & IPC_EXCL)
342 err = -EEXIST;
343 else {
344 err = 0;
345 if (ops->more_checks)
346 err = ops->more_checks(ipcp, params);
347 if (!err)
348
349
350
351
352 err = ipc_check_perms(ipcp, ops, params);
353 }
354 ipc_unlock(ipcp);
355 }
356 up_write(&ids->rw_mutex);
357
358 if (err == -EAGAIN)
359 goto retry;
360
361 return err;
362}
363
364
365
366
367
368
369
370
371
372
373
374void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
375{
376 int lid = ipcid_to_idx(ipcp->id);
377
378 idr_remove(&ids->ipcs_idr, lid);
379
380 ids->in_use--;
381
382 ipcp->deleted = 1;
383
384 return;
385}
386
387
388
389
390
391
392
393
394
395void* ipc_alloc(int size)
396{
397 void* out;
398 if(size > PAGE_SIZE)
399 out = vmalloc(size);
400 else
401 out = kmalloc(size, GFP_KERNEL);
402 return out;
403}
404
405
406
407
408
409
410
411
412
413
414void ipc_free(void* ptr, int size)
415{
416 if(size > PAGE_SIZE)
417 vfree(ptr);
418 else
419 kfree(ptr);
420}
421
422
423
424
425
426
427
428
429
430
431
432struct ipc_rcu_hdr
433{
434 int refcount;
435 int is_vmalloc;
436 void *data[0];
437};
438
439
440struct ipc_rcu_grace
441{
442 struct rcu_head rcu;
443
444 void *data[0];
445};
446
447struct ipc_rcu_sched
448{
449 struct work_struct work;
450
451 void *data[0];
452};
453
454#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
455 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
456#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
457 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
458
459static inline int rcu_use_vmalloc(int size)
460{
461
462 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
463 return 1;
464 return 0;
465}
466
467
468
469
470
471
472
473
474
475
476void* ipc_rcu_alloc(int size)
477{
478 void* out;
479
480
481
482
483 if (rcu_use_vmalloc(size)) {
484 out = vmalloc(HDRLEN_VMALLOC + size);
485 if (out) {
486 out += HDRLEN_VMALLOC;
487 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
488 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
489 }
490 } else {
491 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
492 if (out) {
493 out += HDRLEN_KMALLOC;
494 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
495 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
496 }
497 }
498
499 return out;
500}
501
502void ipc_rcu_getref(void *ptr)
503{
504 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
505}
506
507static void ipc_do_vfree(struct work_struct *work)
508{
509 vfree(container_of(work, struct ipc_rcu_sched, work));
510}
511
512
513
514
515
516
517
518
519static void ipc_schedule_free(struct rcu_head *head)
520{
521 struct ipc_rcu_grace *grace;
522 struct ipc_rcu_sched *sched;
523
524 grace = container_of(head, struct ipc_rcu_grace, rcu);
525 sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
526 data[0]);
527
528 INIT_WORK(&sched->work, ipc_do_vfree);
529 schedule_work(&sched->work);
530}
531
532
533
534
535
536
537
538static void ipc_immediate_free(struct rcu_head *head)
539{
540 struct ipc_rcu_grace *free =
541 container_of(head, struct ipc_rcu_grace, rcu);
542 kfree(free);
543}
544
545void ipc_rcu_putref(void *ptr)
546{
547 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
548 return;
549
550 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
551 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
552 ipc_schedule_free);
553 } else {
554 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
555 ipc_immediate_free);
556 }
557}
558
559
560
561
562
563
564
565
566
567
568int ipcperms (struct kern_ipc_perm *ipcp, short flag)
569{
570 int requested_mode, granted_mode, err;
571
572 if (unlikely((err = audit_ipc_obj(ipcp))))
573 return err;
574 requested_mode = (flag >> 6) | (flag >> 3) | flag;
575 granted_mode = ipcp->mode;
576 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
577 granted_mode >>= 6;
578 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
579 granted_mode >>= 3;
580
581 if ((requested_mode & ~granted_mode & 0007) &&
582 !capable(CAP_IPC_OWNER))
583 return -1;
584
585 return security_ipc_permission(ipcp, flag);
586}
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
604{
605 out->key = in->key;
606 out->uid = in->uid;
607 out->gid = in->gid;
608 out->cuid = in->cuid;
609 out->cgid = in->cgid;
610 out->mode = in->mode;
611 out->seq = in->seq;
612}
613
614
615
616
617
618
619
620
621
622
623void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
624{
625 out->key = in->key;
626 SET_UID(out->uid, in->uid);
627 SET_GID(out->gid, in->gid);
628 SET_UID(out->cuid, in->cuid);
629 SET_GID(out->cgid, in->cgid);
630 out->mode = in->mode;
631 out->seq = in->seq;
632}
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
649{
650 struct kern_ipc_perm *out;
651 int lid = ipcid_to_idx(id);
652
653 down_read(&ids->rw_mutex);
654
655 rcu_read_lock();
656 out = idr_find(&ids->ipcs_idr, lid);
657 if (out == NULL) {
658 rcu_read_unlock();
659 up_read(&ids->rw_mutex);
660 return ERR_PTR(-EINVAL);
661 }
662
663 up_read(&ids->rw_mutex);
664
665 spin_lock(&out->lock);
666
667
668
669
670 if (out->deleted) {
671 spin_unlock(&out->lock);
672 rcu_read_unlock();
673 return ERR_PTR(-EINVAL);
674 }
675
676 return out;
677}
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
693{
694 struct kern_ipc_perm *out;
695 int lid = ipcid_to_idx(id);
696
697 rcu_read_lock();
698 out = idr_find(&ids->ipcs_idr, lid);
699 if (out == NULL) {
700 rcu_read_unlock();
701 return ERR_PTR(-EINVAL);
702 }
703
704 spin_lock(&out->lock);
705
706
707
708
709
710 return out;
711}
712
713struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id)
714{
715 struct kern_ipc_perm *out;
716
717 out = ipc_lock_down(ids, id);
718 if (IS_ERR(out))
719 return out;
720
721 if (ipc_checkid(out, id)) {
722 ipc_unlock(out);
723 return ERR_PTR(-EIDRM);
724 }
725
726 return out;
727}
728
729struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
730{
731 struct kern_ipc_perm *out;
732
733 out = ipc_lock(ids, id);
734 if (IS_ERR(out))
735 return out;
736
737 if (ipc_checkid(out, id)) {
738 ipc_unlock(out);
739 return ERR_PTR(-EIDRM);
740 }
741
742 return out;
743}
744
745
746
747
748
749
750
751
752
753
754
755int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
756 struct ipc_ops *ops, struct ipc_params *params)
757{
758 if (params->key == IPC_PRIVATE)
759 return ipcget_new(ns, ids, ops, params);
760 else
761 return ipcget_public(ns, ids, ops, params);
762}
763
764#ifdef __ARCH_WANT_IPC_PARSE_VERSION
765
766
767
768
769
770
771
772
773
774
775
776int ipc_parse_version (int *cmd)
777{
778 if (*cmd & IPC_64) {
779 *cmd ^= IPC_64;
780 return IPC_64;
781 } else {
782 return IPC_OLD;
783 }
784}
785
786#endif
787
788#ifdef CONFIG_PROC_FS
789struct ipc_proc_iter {
790 struct ipc_namespace *ns;
791 struct ipc_proc_iface *iface;
792};
793
794
795
796
797static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
798 loff_t *new_pos)
799{
800 struct kern_ipc_perm *ipc;
801 int total, id;
802
803 total = 0;
804 for (id = 0; id < pos && total < ids->in_use; id++) {
805 ipc = idr_find(&ids->ipcs_idr, id);
806 if (ipc != NULL)
807 total++;
808 }
809
810 if (total >= ids->in_use)
811 return NULL;
812
813 for ( ; pos < IPCMNI; pos++) {
814 ipc = idr_find(&ids->ipcs_idr, pos);
815 if (ipc != NULL) {
816 *new_pos = pos + 1;
817 ipc_lock_by_ptr(ipc);
818 return ipc;
819 }
820 }
821
822
823 return NULL;
824}
825
826static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
827{
828 struct ipc_proc_iter *iter = s->private;
829 struct ipc_proc_iface *iface = iter->iface;
830 struct kern_ipc_perm *ipc = it;
831
832
833 if (ipc && ipc != SEQ_START_TOKEN)
834 ipc_unlock(ipc);
835
836 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
837}
838
839
840
841
842
843static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
844{
845 struct ipc_proc_iter *iter = s->private;
846 struct ipc_proc_iface *iface = iter->iface;
847 struct ipc_ids *ids;
848
849 ids = &iter->ns->ids[iface->ids];
850
851
852
853
854
855 down_read(&ids->rw_mutex);
856
857
858 if (*pos < 0)
859 return NULL;
860
861
862 if (*pos == 0)
863 return SEQ_START_TOKEN;
864
865
866 return sysvipc_find_ipc(ids, *pos - 1, pos);
867}
868
869static void sysvipc_proc_stop(struct seq_file *s, void *it)
870{
871 struct kern_ipc_perm *ipc = it;
872 struct ipc_proc_iter *iter = s->private;
873 struct ipc_proc_iface *iface = iter->iface;
874 struct ipc_ids *ids;
875
876
877 if (ipc && ipc != SEQ_START_TOKEN)
878 ipc_unlock(ipc);
879
880 ids = &iter->ns->ids[iface->ids];
881
882 up_read(&ids->rw_mutex);
883}
884
885static int sysvipc_proc_show(struct seq_file *s, void *it)
886{
887 struct ipc_proc_iter *iter = s->private;
888 struct ipc_proc_iface *iface = iter->iface;
889
890 if (it == SEQ_START_TOKEN)
891 return seq_puts(s, iface->header);
892
893 return iface->show(s, it);
894}
895
896static struct seq_operations sysvipc_proc_seqops = {
897 .start = sysvipc_proc_start,
898 .stop = sysvipc_proc_stop,
899 .next = sysvipc_proc_next,
900 .show = sysvipc_proc_show,
901};
902
903static int sysvipc_proc_open(struct inode *inode, struct file *file)
904{
905 int ret;
906 struct seq_file *seq;
907 struct ipc_proc_iter *iter;
908
909 ret = -ENOMEM;
910 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
911 if (!iter)
912 goto out;
913
914 ret = seq_open(file, &sysvipc_proc_seqops);
915 if (ret)
916 goto out_kfree;
917
918 seq = file->private_data;
919 seq->private = iter;
920
921 iter->iface = PDE(inode)->data;
922 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
923out:
924 return ret;
925out_kfree:
926 kfree(iter);
927 goto out;
928}
929
930static int sysvipc_proc_release(struct inode *inode, struct file *file)
931{
932 struct seq_file *seq = file->private_data;
933 struct ipc_proc_iter *iter = seq->private;
934 put_ipc_ns(iter->ns);
935 return seq_release_private(inode, file);
936}
937
938static const struct file_operations sysvipc_proc_fops = {
939 .open = sysvipc_proc_open,
940 .read = seq_read,
941 .llseek = seq_lseek,
942 .release = sysvipc_proc_release,
943};
944#endif
945