ipc: store ipcs into IDRs
[safe/jmp/linux-2.6] / ipc / shm.c
1 /*
2  * linux/ipc/shm.c
3  * Copyright (C) 1992, 1993 Krishna Balasubramanian
4  *       Many improvements/fixes by Bruno Haible.
5  * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
6  * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
7  *
8  * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
9  * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
10  * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
11  * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
12  * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
13  * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
14  * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
15  *
16  * support for audit of ipc object properties and permission changes
17  * Dustin Kirkland <dustin.kirkland@us.ibm.com>
18  *
19  * namespaces support
20  * OpenVZ, SWsoft Inc.
21  * Pavel Emelianov <xemul@openvz.org>
22  */
23
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <linux/hugetlb.h>
27 #include <linux/shm.h>
28 #include <linux/init.h>
29 #include <linux/file.h>
30 #include <linux/mman.h>
31 #include <linux/shmem_fs.h>
32 #include <linux/security.h>
33 #include <linux/syscalls.h>
34 #include <linux/audit.h>
35 #include <linux/capability.h>
36 #include <linux/ptrace.h>
37 #include <linux/seq_file.h>
38 #include <linux/mutex.h>
39 #include <linux/nsproxy.h>
40 #include <linux/mount.h>
41
42 #include <asm/uaccess.h>
43
44 #include "util.h"
45
46 struct shm_file_data {
47         int id;
48         struct ipc_namespace *ns;
49         struct file *file;
50         const struct vm_operations_struct *vm_ops;
51 };
52
53 #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
54
55 static const struct file_operations shm_file_operations;
56 static struct vm_operations_struct shm_vm_ops;
57
58 static struct ipc_ids init_shm_ids;
59
60 #define shm_ids(ns)     (*((ns)->ids[IPC_SHM_IDS]))
61
62 #define shm_lock(ns, id)                \
63         ((struct shmid_kernel*)ipc_lock(&shm_ids(ns),id))
64 #define shm_unlock(shp)                 \
65         ipc_unlock(&(shp)->shm_perm)
66 #define shm_get(ns, id)                 \
67         ((struct shmid_kernel*)ipc_get(&shm_ids(ns),id))
68 #define shm_buildid(ns, id, seq)        \
69         ipc_buildid(&shm_ids(ns), id, seq)
70
71 static int newseg (struct ipc_namespace *ns, key_t key,
72                 int shmflg, size_t size);
73 static void shm_open(struct vm_area_struct *vma);
74 static void shm_close(struct vm_area_struct *vma);
75 static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
76 #ifdef CONFIG_PROC_FS
77 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
78 #endif
79
80 static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
81 {
82         ns->ids[IPC_SHM_IDS] = ids;
83         ns->shm_ctlmax = SHMMAX;
84         ns->shm_ctlall = SHMALL;
85         ns->shm_ctlmni = SHMMNI;
86         ns->shm_tot = 0;
87         ipc_init_ids(ids);
88 }
89
90 static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
91 {
92         if (shp->shm_nattch){
93                 shp->shm_perm.mode |= SHM_DEST;
94                 /* Do not find it any more */
95                 shp->shm_perm.key = IPC_PRIVATE;
96                 shm_unlock(shp);
97         } else
98                 shm_destroy(ns, shp);
99 }
100
101 int shm_init_ns(struct ipc_namespace *ns)
102 {
103         struct ipc_ids *ids;
104
105         ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
106         if (ids == NULL)
107                 return -ENOMEM;
108
109         __shm_init_ns(ns, ids);
110         return 0;
111 }
112
113 void shm_exit_ns(struct ipc_namespace *ns)
114 {
115         struct shmid_kernel *shp;
116         int next_id;
117         int total, in_use;
118
119         mutex_lock(&shm_ids(ns).mutex);
120
121         in_use = shm_ids(ns).in_use;
122
123         for (total = 0, next_id = 0; total < in_use; next_id++) {
124                 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
125                 if (shp == NULL)
126                         continue;
127                 ipc_lock_by_ptr(&shp->shm_perm);
128                 do_shm_rmid(ns, shp);
129                 total++;
130         }
131         mutex_unlock(&shm_ids(ns).mutex);
132
133         kfree(ns->ids[IPC_SHM_IDS]);
134         ns->ids[IPC_SHM_IDS] = NULL;
135 }
136
137 void __init shm_init (void)
138 {
139         __shm_init_ns(&init_ipc_ns, &init_shm_ids);
140         ipc_init_proc_interface("sysvipc/shm",
141                                 "       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime\n",
142                                 IPC_SHM_IDS, sysvipc_shm_proc_show);
143 }
144
145 static inline int shm_checkid(struct ipc_namespace *ns,
146                 struct shmid_kernel *s, int id)
147 {
148         if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id))
149                 return -EIDRM;
150         return 0;
151 }
152
153 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
154 {
155         ipc_rmid(&shm_ids(ns), &s->shm_perm);
156 }
157
158 static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
159 {
160         return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
161 }
162
163
164
165 /* This is called by fork, once for every shm attach. */
166 static void shm_open(struct vm_area_struct *vma)
167 {
168         struct file *file = vma->vm_file;
169         struct shm_file_data *sfd = shm_file_data(file);
170         struct shmid_kernel *shp;
171
172         shp = shm_lock(sfd->ns, sfd->id);
173         BUG_ON(!shp);
174         shp->shm_atim = get_seconds();
175         shp->shm_lprid = task_tgid_vnr(current);
176         shp->shm_nattch++;
177         shm_unlock(shp);
178 }
179
180 /*
181  * shm_destroy - free the struct shmid_kernel
182  *
183  * @shp: struct to free
184  *
185  * It has to be called with shp and shm_ids.mutex locked,
186  * but returns with shp unlocked and freed.
187  */
188 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
189 {
190         ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
191         shm_rmid(ns, shp);
192         shm_unlock(shp);
193         if (!is_file_hugepages(shp->shm_file))
194                 shmem_lock(shp->shm_file, 0, shp->mlock_user);
195         else
196                 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
197                                                 shp->mlock_user);
198         fput (shp->shm_file);
199         security_shm_free(shp);
200         ipc_rcu_putref(shp);
201 }
202
203 /*
204  * remove the attach descriptor vma.
205  * free memory for segment if it is marked destroyed.
206  * The descriptor has already been removed from the current->mm->mmap list
207  * and will later be kfree()d.
208  */
209 static void shm_close(struct vm_area_struct *vma)
210 {
211         struct file * file = vma->vm_file;
212         struct shm_file_data *sfd = shm_file_data(file);
213         struct shmid_kernel *shp;
214         struct ipc_namespace *ns = sfd->ns;
215
216         mutex_lock(&shm_ids(ns).mutex);
217         /* remove from the list of attaches of the shm segment */
218         shp = shm_lock(ns, sfd->id);
219         BUG_ON(!shp);
220         shp->shm_lprid = task_tgid_vnr(current);
221         shp->shm_dtim = get_seconds();
222         shp->shm_nattch--;
223         if(shp->shm_nattch == 0 &&
224            shp->shm_perm.mode & SHM_DEST)
225                 shm_destroy(ns, shp);
226         else
227                 shm_unlock(shp);
228         mutex_unlock(&shm_ids(ns).mutex);
229 }
230
231 static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
232 {
233         struct file *file = vma->vm_file;
234         struct shm_file_data *sfd = shm_file_data(file);
235
236         return sfd->vm_ops->fault(vma, vmf);
237 }
238
239 #ifdef CONFIG_NUMA
240 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
241 {
242         struct file *file = vma->vm_file;
243         struct shm_file_data *sfd = shm_file_data(file);
244         int err = 0;
245         if (sfd->vm_ops->set_policy)
246                 err = sfd->vm_ops->set_policy(vma, new);
247         return err;
248 }
249
250 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
251                                         unsigned long addr)
252 {
253         struct file *file = vma->vm_file;
254         struct shm_file_data *sfd = shm_file_data(file);
255         struct mempolicy *pol = NULL;
256
257         if (sfd->vm_ops->get_policy)
258                 pol = sfd->vm_ops->get_policy(vma, addr);
259         else if (vma->vm_policy)
260                 pol = vma->vm_policy;
261         else
262                 pol = current->mempolicy;
263         return pol;
264 }
265 #endif
266
267 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
268 {
269         struct shm_file_data *sfd = shm_file_data(file);
270         int ret;
271
272         ret = sfd->file->f_op->mmap(sfd->file, vma);
273         if (ret != 0)
274                 return ret;
275         sfd->vm_ops = vma->vm_ops;
276 #ifdef CONFIG_MMU
277         BUG_ON(!sfd->vm_ops->fault);
278 #endif
279         vma->vm_ops = &shm_vm_ops;
280         shm_open(vma);
281
282         return ret;
283 }
284
285 static int shm_release(struct inode *ino, struct file *file)
286 {
287         struct shm_file_data *sfd = shm_file_data(file);
288
289         put_ipc_ns(sfd->ns);
290         shm_file_data(file) = NULL;
291         kfree(sfd);
292         return 0;
293 }
294
295 static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
296 {
297         int (*fsync) (struct file *, struct dentry *, int datasync);
298         struct shm_file_data *sfd = shm_file_data(file);
299         int ret = -EINVAL;
300
301         fsync = sfd->file->f_op->fsync;
302         if (fsync)
303                 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
304         return ret;
305 }
306
307 static unsigned long shm_get_unmapped_area(struct file *file,
308         unsigned long addr, unsigned long len, unsigned long pgoff,
309         unsigned long flags)
310 {
311         struct shm_file_data *sfd = shm_file_data(file);
312         return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
313 }
314
315 int is_file_shm_hugepages(struct file *file)
316 {
317         int ret = 0;
318
319         if (file->f_op == &shm_file_operations) {
320                 struct shm_file_data *sfd;
321                 sfd = shm_file_data(file);
322                 ret = is_file_hugepages(sfd->file);
323         }
324         return ret;
325 }
326
327 static const struct file_operations shm_file_operations = {
328         .mmap           = shm_mmap,
329         .fsync          = shm_fsync,
330         .release        = shm_release,
331         .get_unmapped_area      = shm_get_unmapped_area,
332 };
333
334 static struct vm_operations_struct shm_vm_ops = {
335         .open   = shm_open,     /* callback for a new vm-area open */
336         .close  = shm_close,    /* callback for when the vm-area is released */
337         .fault  = shm_fault,
338 #if defined(CONFIG_NUMA)
339         .set_policy = shm_set_policy,
340         .get_policy = shm_get_policy,
341 #endif
342 };
343
344 static int newseg (struct ipc_namespace *ns, key_t key, int shmflg, size_t size)
345 {
346         int error;
347         struct shmid_kernel *shp;
348         int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
349         struct file * file;
350         char name[13];
351         int id;
352
353         if (size < SHMMIN || size > ns->shm_ctlmax)
354                 return -EINVAL;
355
356         if (ns->shm_tot + numpages > ns->shm_ctlall)
357                 return -ENOSPC;
358
359         shp = ipc_rcu_alloc(sizeof(*shp));
360         if (!shp)
361                 return -ENOMEM;
362
363         shp->shm_perm.key = key;
364         shp->shm_perm.mode = (shmflg & S_IRWXUGO);
365         shp->mlock_user = NULL;
366
367         shp->shm_perm.security = NULL;
368         error = security_shm_alloc(shp);
369         if (error) {
370                 ipc_rcu_putref(shp);
371                 return error;
372         }
373
374         sprintf (name, "SYSV%08x", key);
375         if (shmflg & SHM_HUGETLB) {
376                 /* hugetlb_file_setup takes care of mlock user accounting */
377                 file = hugetlb_file_setup(name, size);
378                 shp->mlock_user = current->user;
379         } else {
380                 int acctflag = VM_ACCOUNT;
381                 /*
382                  * Do not allow no accounting for OVERCOMMIT_NEVER, even
383                  * if it's asked for.
384                  */
385                 if  ((shmflg & SHM_NORESERVE) &&
386                                 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
387                         acctflag = 0;
388                 file = shmem_file_setup(name, size, acctflag);
389         }
390         error = PTR_ERR(file);
391         if (IS_ERR(file))
392                 goto no_file;
393
394         error = -ENOSPC;
395         id = shm_addid(ns, shp);
396         if(id == -1) 
397                 goto no_id;
398
399         shp->shm_cprid = task_tgid_vnr(current);
400         shp->shm_lprid = 0;
401         shp->shm_atim = shp->shm_dtim = 0;
402         shp->shm_ctim = get_seconds();
403         shp->shm_segsz = size;
404         shp->shm_nattch = 0;
405         shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq);
406         shp->shm_file = file;
407         /*
408          * shmid gets reported as "inode#" in /proc/pid/maps.
409          * proc-ps tools use this. Changing this will break them.
410          */
411         file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
412
413         ns->shm_tot += numpages;
414         error = shp->shm_perm.id;
415         shm_unlock(shp);
416         return error;
417
418 no_id:
419         fput(file);
420 no_file:
421         security_shm_free(shp);
422         ipc_rcu_putref(shp);
423         return error;
424 }
425
426 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
427 {
428         struct shmid_kernel *shp;
429         int err;
430         struct ipc_namespace *ns;
431
432         ns = current->nsproxy->ipc_ns;
433
434         err = idr_pre_get(&shm_ids(ns).ipcs_idr, GFP_KERNEL);
435
436         if (key == IPC_PRIVATE) {
437                 if (!err)
438                         err = -ENOMEM;
439                 else {
440                         mutex_lock(&shm_ids(ns).mutex);
441                         err = newseg(ns, key, shmflg, size);
442                         mutex_unlock(&shm_ids(ns).mutex);
443                 }
444         } else {
445                 mutex_lock(&shm_ids(ns).mutex);
446                 shp = (struct shmid_kernel *) ipc_findkey(&shm_ids(ns), key);
447                 if (shp == NULL) {
448                         if (!(shmflg & IPC_CREAT))
449                                 err = -ENOENT;
450                         else if (!err)
451                                 err = -ENOMEM;
452                         else
453                                 err = newseg(ns, key, shmflg, size);
454                 } else {
455                         /* shp has been locked by ipc_findkey() */
456
457                         if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL))
458                                 err = -EEXIST;
459                         else {
460                                 if (shp->shm_segsz < size)
461                                         err = -EINVAL;
462                                 else if (ipcperms(&shp->shm_perm, shmflg))
463                                         err = -EACCES;
464                                 else {
465                                         err = security_shm_associate(shp,
466                                                                 shmflg);
467                                         if (!err)
468                                                 err = shp->shm_perm.id;
469                                 }
470                         }
471                         shm_unlock(shp);
472                 }
473                 mutex_unlock(&shm_ids(ns).mutex);
474         }
475
476         return err;
477 }
478
479 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
480 {
481         switch(version) {
482         case IPC_64:
483                 return copy_to_user(buf, in, sizeof(*in));
484         case IPC_OLD:
485             {
486                 struct shmid_ds out;
487
488                 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
489                 out.shm_segsz   = in->shm_segsz;
490                 out.shm_atime   = in->shm_atime;
491                 out.shm_dtime   = in->shm_dtime;
492                 out.shm_ctime   = in->shm_ctime;
493                 out.shm_cpid    = in->shm_cpid;
494                 out.shm_lpid    = in->shm_lpid;
495                 out.shm_nattch  = in->shm_nattch;
496
497                 return copy_to_user(buf, &out, sizeof(out));
498             }
499         default:
500                 return -EINVAL;
501         }
502 }
503
504 struct shm_setbuf {
505         uid_t   uid;
506         gid_t   gid;
507         mode_t  mode;
508 };      
509
510 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
511 {
512         switch(version) {
513         case IPC_64:
514             {
515                 struct shmid64_ds tbuf;
516
517                 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
518                         return -EFAULT;
519
520                 out->uid        = tbuf.shm_perm.uid;
521                 out->gid        = tbuf.shm_perm.gid;
522                 out->mode       = tbuf.shm_perm.mode;
523
524                 return 0;
525             }
526         case IPC_OLD:
527             {
528                 struct shmid_ds tbuf_old;
529
530                 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
531                         return -EFAULT;
532
533                 out->uid        = tbuf_old.shm_perm.uid;
534                 out->gid        = tbuf_old.shm_perm.gid;
535                 out->mode       = tbuf_old.shm_perm.mode;
536
537                 return 0;
538             }
539         default:
540                 return -EINVAL;
541         }
542 }
543
544 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
545 {
546         switch(version) {
547         case IPC_64:
548                 return copy_to_user(buf, in, sizeof(*in));
549         case IPC_OLD:
550             {
551                 struct shminfo out;
552
553                 if(in->shmmax > INT_MAX)
554                         out.shmmax = INT_MAX;
555                 else
556                         out.shmmax = (int)in->shmmax;
557
558                 out.shmmin      = in->shmmin;
559                 out.shmmni      = in->shmmni;
560                 out.shmseg      = in->shmseg;
561                 out.shmall      = in->shmall; 
562
563                 return copy_to_user(buf, &out, sizeof(out));
564             }
565         default:
566                 return -EINVAL;
567         }
568 }
569
570 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
571                 unsigned long *swp)
572 {
573         int next_id;
574         int total, in_use;
575
576         *rss = 0;
577         *swp = 0;
578
579         in_use = shm_ids(ns).in_use;
580
581         for (total = 0, next_id = 0; total < in_use; next_id++) {
582                 struct shmid_kernel *shp;
583                 struct inode *inode;
584
585                 shp = shm_get(ns, next_id);
586                 if (shp == NULL)
587                         continue;
588
589                 inode = shp->shm_file->f_path.dentry->d_inode;
590
591                 if (is_file_hugepages(shp->shm_file)) {
592                         struct address_space *mapping = inode->i_mapping;
593                         *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
594                 } else {
595                         struct shmem_inode_info *info = SHMEM_I(inode);
596                         spin_lock(&info->lock);
597                         *rss += inode->i_mapping->nrpages;
598                         *swp += info->swapped;
599                         spin_unlock(&info->lock);
600                 }
601
602                 total++;
603         }
604 }
605
606 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
607 {
608         struct shm_setbuf setbuf;
609         struct shmid_kernel *shp;
610         int err, version;
611         struct ipc_namespace *ns;
612
613         if (cmd < 0 || shmid < 0) {
614                 err = -EINVAL;
615                 goto out;
616         }
617
618         version = ipc_parse_version(&cmd);
619         ns = current->nsproxy->ipc_ns;
620
621         switch (cmd) { /* replace with proc interface ? */
622         case IPC_INFO:
623         {
624                 struct shminfo64 shminfo;
625
626                 err = security_shm_shmctl(NULL, cmd);
627                 if (err)
628                         return err;
629
630                 memset(&shminfo,0,sizeof(shminfo));
631                 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
632                 shminfo.shmmax = ns->shm_ctlmax;
633                 shminfo.shmall = ns->shm_ctlall;
634
635                 shminfo.shmmin = SHMMIN;
636                 if(copy_shminfo_to_user (buf, &shminfo, version))
637                         return -EFAULT;
638                 /* reading a integer is always atomic */
639                 err = ipc_get_maxid(&shm_ids(ns));
640                 if(err<0)
641                         err = 0;
642                 goto out;
643         }
644         case SHM_INFO:
645         {
646                 struct shm_info shm_info;
647
648                 err = security_shm_shmctl(NULL, cmd);
649                 if (err)
650                         return err;
651
652                 memset(&shm_info,0,sizeof(shm_info));
653                 mutex_lock(&shm_ids(ns).mutex);
654                 shm_info.used_ids = shm_ids(ns).in_use;
655                 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
656                 shm_info.shm_tot = ns->shm_tot;
657                 shm_info.swap_attempts = 0;
658                 shm_info.swap_successes = 0;
659                 err = ipc_get_maxid(&shm_ids(ns));
660                 mutex_unlock(&shm_ids(ns).mutex);
661                 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
662                         err = -EFAULT;
663                         goto out;
664                 }
665
666                 err = err < 0 ? 0 : err;
667                 goto out;
668         }
669         case SHM_STAT:
670         case IPC_STAT:
671         {
672                 struct shmid64_ds tbuf;
673                 int result;
674                 memset(&tbuf, 0, sizeof(tbuf));
675                 shp = shm_lock(ns, shmid);
676                 if(shp==NULL) {
677                         err = -EINVAL;
678                         goto out;
679                 } else if (cmd == SHM_STAT) {
680                         result = shp->shm_perm.id;
681                 } else {
682                         err = shm_checkid(ns, shp,shmid);
683                         if(err)
684                                 goto out_unlock;
685                         result = 0;
686                 }
687                 err=-EACCES;
688                 if (ipcperms (&shp->shm_perm, S_IRUGO))
689                         goto out_unlock;
690                 err = security_shm_shmctl(shp, cmd);
691                 if (err)
692                         goto out_unlock;
693                 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
694                 tbuf.shm_segsz  = shp->shm_segsz;
695                 tbuf.shm_atime  = shp->shm_atim;
696                 tbuf.shm_dtime  = shp->shm_dtim;
697                 tbuf.shm_ctime  = shp->shm_ctim;
698                 tbuf.shm_cpid   = shp->shm_cprid;
699                 tbuf.shm_lpid   = shp->shm_lprid;
700                 tbuf.shm_nattch = shp->shm_nattch;
701                 shm_unlock(shp);
702                 if(copy_shmid_to_user (buf, &tbuf, version))
703                         err = -EFAULT;
704                 else
705                         err = result;
706                 goto out;
707         }
708         case SHM_LOCK:
709         case SHM_UNLOCK:
710         {
711                 shp = shm_lock(ns, shmid);
712                 if(shp==NULL) {
713                         err = -EINVAL;
714                         goto out;
715                 }
716                 err = shm_checkid(ns, shp,shmid);
717                 if(err)
718                         goto out_unlock;
719
720                 err = audit_ipc_obj(&(shp->shm_perm));
721                 if (err)
722                         goto out_unlock;
723
724                 if (!capable(CAP_IPC_LOCK)) {
725                         err = -EPERM;
726                         if (current->euid != shp->shm_perm.uid &&
727                             current->euid != shp->shm_perm.cuid)
728                                 goto out_unlock;
729                         if (cmd == SHM_LOCK &&
730                             !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
731                                 goto out_unlock;
732                 }
733
734                 err = security_shm_shmctl(shp, cmd);
735                 if (err)
736                         goto out_unlock;
737                 
738                 if(cmd==SHM_LOCK) {
739                         struct user_struct * user = current->user;
740                         if (!is_file_hugepages(shp->shm_file)) {
741                                 err = shmem_lock(shp->shm_file, 1, user);
742                                 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
743                                         shp->shm_perm.mode |= SHM_LOCKED;
744                                         shp->mlock_user = user;
745                                 }
746                         }
747                 } else if (!is_file_hugepages(shp->shm_file)) {
748                         shmem_lock(shp->shm_file, 0, shp->mlock_user);
749                         shp->shm_perm.mode &= ~SHM_LOCKED;
750                         shp->mlock_user = NULL;
751                 }
752                 shm_unlock(shp);
753                 goto out;
754         }
755         case IPC_RMID:
756         {
757                 /*
758                  *      We cannot simply remove the file. The SVID states
759                  *      that the block remains until the last person
760                  *      detaches from it, then is deleted. A shmat() on
761                  *      an RMID segment is legal in older Linux and if 
762                  *      we change it apps break...
763                  *
764                  *      Instead we set a destroyed flag, and then blow
765                  *      the name away when the usage hits zero.
766                  */
767                 mutex_lock(&shm_ids(ns).mutex);
768                 shp = shm_lock(ns, shmid);
769                 err = -EINVAL;
770                 if (shp == NULL) 
771                         goto out_up;
772                 err = shm_checkid(ns, shp, shmid);
773                 if(err)
774                         goto out_unlock_up;
775
776                 err = audit_ipc_obj(&(shp->shm_perm));
777                 if (err)
778                         goto out_unlock_up;
779
780                 if (current->euid != shp->shm_perm.uid &&
781                     current->euid != shp->shm_perm.cuid && 
782                     !capable(CAP_SYS_ADMIN)) {
783                         err=-EPERM;
784                         goto out_unlock_up;
785                 }
786
787                 err = security_shm_shmctl(shp, cmd);
788                 if (err)
789                         goto out_unlock_up;
790
791                 do_shm_rmid(ns, shp);
792                 mutex_unlock(&shm_ids(ns).mutex);
793                 goto out;
794         }
795
796         case IPC_SET:
797         {
798                 if (copy_shmid_from_user (&setbuf, buf, version)) {
799                         err = -EFAULT;
800                         goto out;
801                 }
802                 mutex_lock(&shm_ids(ns).mutex);
803                 shp = shm_lock(ns, shmid);
804                 err=-EINVAL;
805                 if(shp==NULL)
806                         goto out_up;
807                 err = shm_checkid(ns, shp,shmid);
808                 if(err)
809                         goto out_unlock_up;
810                 err = audit_ipc_obj(&(shp->shm_perm));
811                 if (err)
812                         goto out_unlock_up;
813                 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
814                 if (err)
815                         goto out_unlock_up;
816                 err=-EPERM;
817                 if (current->euid != shp->shm_perm.uid &&
818                     current->euid != shp->shm_perm.cuid && 
819                     !capable(CAP_SYS_ADMIN)) {
820                         goto out_unlock_up;
821                 }
822
823                 err = security_shm_shmctl(shp, cmd);
824                 if (err)
825                         goto out_unlock_up;
826                 
827                 shp->shm_perm.uid = setbuf.uid;
828                 shp->shm_perm.gid = setbuf.gid;
829                 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
830                         | (setbuf.mode & S_IRWXUGO);
831                 shp->shm_ctim = get_seconds();
832                 break;
833         }
834
835         default:
836                 err = -EINVAL;
837                 goto out;
838         }
839
840         err = 0;
841 out_unlock_up:
842         shm_unlock(shp);
843 out_up:
844         mutex_unlock(&shm_ids(ns).mutex);
845         goto out;
846 out_unlock:
847         shm_unlock(shp);
848 out:
849         return err;
850 }
851
852 /*
853  * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
854  *
855  * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
856  * "raddr" thing points to kernel space, and there has to be a wrapper around
857  * this.
858  */
859 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
860 {
861         struct shmid_kernel *shp;
862         unsigned long addr;
863         unsigned long size;
864         struct file * file;
865         int    err;
866         unsigned long flags;
867         unsigned long prot;
868         int acc_mode;
869         unsigned long user_addr;
870         struct ipc_namespace *ns;
871         struct shm_file_data *sfd;
872         struct path path;
873         mode_t f_mode;
874
875         err = -EINVAL;
876         if (shmid < 0)
877                 goto out;
878         else if ((addr = (ulong)shmaddr)) {
879                 if (addr & (SHMLBA-1)) {
880                         if (shmflg & SHM_RND)
881                                 addr &= ~(SHMLBA-1);       /* round down */
882                         else
883 #ifndef __ARCH_FORCE_SHMLBA
884                                 if (addr & ~PAGE_MASK)
885 #endif
886                                         goto out;
887                 }
888                 flags = MAP_SHARED | MAP_FIXED;
889         } else {
890                 if ((shmflg & SHM_REMAP))
891                         goto out;
892
893                 flags = MAP_SHARED;
894         }
895
896         if (shmflg & SHM_RDONLY) {
897                 prot = PROT_READ;
898                 acc_mode = S_IRUGO;
899                 f_mode = FMODE_READ;
900         } else {
901                 prot = PROT_READ | PROT_WRITE;
902                 acc_mode = S_IRUGO | S_IWUGO;
903                 f_mode = FMODE_READ | FMODE_WRITE;
904         }
905         if (shmflg & SHM_EXEC) {
906                 prot |= PROT_EXEC;
907                 acc_mode |= S_IXUGO;
908         }
909
910         /*
911          * We cannot rely on the fs check since SYSV IPC does have an
912          * additional creator id...
913          */
914         ns = current->nsproxy->ipc_ns;
915         shp = shm_lock(ns, shmid);
916         if(shp == NULL)
917                 goto out;
918
919         err = shm_checkid(ns, shp,shmid);
920         if (err)
921                 goto out_unlock;
922
923         err = -EACCES;
924         if (ipcperms(&shp->shm_perm, acc_mode))
925                 goto out_unlock;
926
927         err = security_shm_shmat(shp, shmaddr, shmflg);
928         if (err)
929                 goto out_unlock;
930
931         path.dentry = dget(shp->shm_file->f_path.dentry);
932         path.mnt    = shp->shm_file->f_path.mnt;
933         shp->shm_nattch++;
934         size = i_size_read(path.dentry->d_inode);
935         shm_unlock(shp);
936
937         err = -ENOMEM;
938         sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
939         if (!sfd)
940                 goto out_put_dentry;
941
942         err = -ENOMEM;
943
944         file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
945         if (!file)
946                 goto out_free;
947
948         file->private_data = sfd;
949         file->f_mapping = shp->shm_file->f_mapping;
950         sfd->id = shp->shm_perm.id;
951         sfd->ns = get_ipc_ns(ns);
952         sfd->file = shp->shm_file;
953         sfd->vm_ops = NULL;
954
955         down_write(&current->mm->mmap_sem);
956         if (addr && !(shmflg & SHM_REMAP)) {
957                 err = -EINVAL;
958                 if (find_vma_intersection(current->mm, addr, addr + size))
959                         goto invalid;
960                 /*
961                  * If shm segment goes below stack, make sure there is some
962                  * space left for the stack to grow (at least 4 pages).
963                  */
964                 if (addr < current->mm->start_stack &&
965                     addr > current->mm->start_stack - size - PAGE_SIZE * 5)
966                         goto invalid;
967         }
968                 
969         user_addr = do_mmap (file, addr, size, prot, flags, 0);
970         *raddr = user_addr;
971         err = 0;
972         if (IS_ERR_VALUE(user_addr))
973                 err = (long)user_addr;
974 invalid:
975         up_write(&current->mm->mmap_sem);
976
977         fput(file);
978
979 out_nattch:
980         mutex_lock(&shm_ids(ns).mutex);
981         shp = shm_lock(ns, shmid);
982         BUG_ON(!shp);
983         shp->shm_nattch--;
984         if(shp->shm_nattch == 0 &&
985            shp->shm_perm.mode & SHM_DEST)
986                 shm_destroy(ns, shp);
987         else
988                 shm_unlock(shp);
989         mutex_unlock(&shm_ids(ns).mutex);
990
991 out:
992         return err;
993
994 out_unlock:
995         shm_unlock(shp);
996         goto out;
997
998 out_free:
999         kfree(sfd);
1000 out_put_dentry:
1001         dput(path.dentry);
1002         goto out_nattch;
1003 }
1004
1005 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
1006 {
1007         unsigned long ret;
1008         long err;
1009
1010         err = do_shmat(shmid, shmaddr, shmflg, &ret);
1011         if (err)
1012                 return err;
1013         force_successful_syscall_return();
1014         return (long)ret;
1015 }
1016
1017 /*
1018  * detach and kill segment if marked destroyed.
1019  * The work is done in shm_close.
1020  */
1021 asmlinkage long sys_shmdt(char __user *shmaddr)
1022 {
1023         struct mm_struct *mm = current->mm;
1024         struct vm_area_struct *vma, *next;
1025         unsigned long addr = (unsigned long)shmaddr;
1026         loff_t size = 0;
1027         int retval = -EINVAL;
1028
1029         if (addr & ~PAGE_MASK)
1030                 return retval;
1031
1032         down_write(&mm->mmap_sem);
1033
1034         /*
1035          * This function tries to be smart and unmap shm segments that
1036          * were modified by partial mlock or munmap calls:
1037          * - It first determines the size of the shm segment that should be
1038          *   unmapped: It searches for a vma that is backed by shm and that
1039          *   started at address shmaddr. It records it's size and then unmaps
1040          *   it.
1041          * - Then it unmaps all shm vmas that started at shmaddr and that
1042          *   are within the initially determined size.
1043          * Errors from do_munmap are ignored: the function only fails if
1044          * it's called with invalid parameters or if it's called to unmap
1045          * a part of a vma. Both calls in this function are for full vmas,
1046          * the parameters are directly copied from the vma itself and always
1047          * valid - therefore do_munmap cannot fail. (famous last words?)
1048          */
1049         /*
1050          * If it had been mremap()'d, the starting address would not
1051          * match the usual checks anyway. So assume all vma's are
1052          * above the starting address given.
1053          */
1054         vma = find_vma(mm, addr);
1055
1056         while (vma) {
1057                 next = vma->vm_next;
1058
1059                 /*
1060                  * Check if the starting address would match, i.e. it's
1061                  * a fragment created by mprotect() and/or munmap(), or it
1062                  * otherwise it starts at this address with no hassles.
1063                  */
1064                 if ((vma->vm_ops == &shm_vm_ops) &&
1065                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1066
1067
1068                         size = vma->vm_file->f_path.dentry->d_inode->i_size;
1069                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1070                         /*
1071                          * We discovered the size of the shm segment, so
1072                          * break out of here and fall through to the next
1073                          * loop that uses the size information to stop
1074                          * searching for matching vma's.
1075                          */
1076                         retval = 0;
1077                         vma = next;
1078                         break;
1079                 }
1080                 vma = next;
1081         }
1082
1083         /*
1084          * We need look no further than the maximum address a fragment
1085          * could possibly have landed at. Also cast things to loff_t to
1086          * prevent overflows and make comparisions vs. equal-width types.
1087          */
1088         size = PAGE_ALIGN(size);
1089         while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1090                 next = vma->vm_next;
1091
1092                 /* finding a matching vma now does not alter retval */
1093                 if ((vma->vm_ops == &shm_vm_ops) &&
1094                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1095
1096                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1097                 vma = next;
1098         }
1099
1100         up_write(&mm->mmap_sem);
1101         return retval;
1102 }
1103
1104 #ifdef CONFIG_PROC_FS
1105 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1106 {
1107         struct shmid_kernel *shp = it;
1108         char *format;
1109
1110 #define SMALL_STRING "%10d %10d  %4o %10u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1111 #define BIG_STRING   "%10d %10d  %4o %21u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1112
1113         if (sizeof(size_t) <= sizeof(int))
1114                 format = SMALL_STRING;
1115         else
1116                 format = BIG_STRING;
1117         return seq_printf(s, format,
1118                           shp->shm_perm.key,
1119                           shp->shm_perm.id,
1120                           shp->shm_perm.mode,
1121                           shp->shm_segsz,
1122                           shp->shm_cprid,
1123                           shp->shm_lprid,
1124                           shp->shm_nattch,
1125                           shp->shm_perm.uid,
1126                           shp->shm_perm.gid,
1127                           shp->shm_perm.cuid,
1128                           shp->shm_perm.cgid,
1129                           shp->shm_atim,
1130                           shp->shm_dtim,
1131                           shp->shm_ctim);
1132 }
1133 #endif