]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - ipc/shm.c
[PATCH] IPC: access to unmapped vmalloc area in grow_ary()
[karo-tx-linux.git] / 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  */
17
18 #include <linux/config.h>
19 #include <linux/slab.h>
20 #include <linux/mm.h>
21 #include <linux/hugetlb.h>
22 #include <linux/shm.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/mman.h>
26 #include <linux/shmem_fs.h>
27 #include <linux/security.h>
28 #include <linux/syscalls.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/ptrace.h>
32 #include <linux/seq_file.h>
33
34 #include <asm/uaccess.h>
35
36 #include "util.h"
37
38 static struct file_operations shm_file_operations;
39 static struct vm_operations_struct shm_vm_ops;
40
41 static struct ipc_ids shm_ids;
42
43 #define shm_lock(id)    ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
44 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
45 #define shm_get(id)     ((struct shmid_kernel*)ipc_get(&shm_ids,id))
46 #define shm_buildid(id, seq) \
47         ipc_buildid(&shm_ids, id, seq)
48
49 static int newseg (key_t key, int shmflg, size_t size);
50 static void shm_open (struct vm_area_struct *shmd);
51 static void shm_close (struct vm_area_struct *shmd);
52 #ifdef CONFIG_PROC_FS
53 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
54 #endif
55
56 size_t  shm_ctlmax = SHMMAX;
57 size_t  shm_ctlall = SHMALL;
58 int     shm_ctlmni = SHMMNI;
59
60 static int shm_tot; /* total number of shared memory pages */
61
62 void __init shm_init (void)
63 {
64         ipc_init_ids(&shm_ids, 1);
65         ipc_init_proc_interface("sysvipc/shm",
66                                 "       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime\n",
67                                 &shm_ids,
68                                 sysvipc_shm_proc_show);
69 }
70
71 static inline int shm_checkid(struct shmid_kernel *s, int id)
72 {
73         if (ipc_checkid(&shm_ids,&s->shm_perm,id))
74                 return -EIDRM;
75         return 0;
76 }
77
78 static inline struct shmid_kernel *shm_rmid(int id)
79 {
80         return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
81 }
82
83 static inline int shm_addid(struct shmid_kernel *shp)
84 {
85         return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
86 }
87
88
89
90 static inline void shm_inc (int id) {
91         struct shmid_kernel *shp;
92
93         if(!(shp = shm_lock(id)))
94                 BUG();
95         shp->shm_atim = get_seconds();
96         shp->shm_lprid = current->tgid;
97         shp->shm_nattch++;
98         shm_unlock(shp);
99 }
100
101 /* This is called by fork, once for every shm attach. */
102 static void shm_open (struct vm_area_struct *shmd)
103 {
104         shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
105 }
106
107 /*
108  * shm_destroy - free the struct shmid_kernel
109  *
110  * @shp: struct to free
111  *
112  * It has to be called with shp and shm_ids.sem locked,
113  * but returns with shp unlocked and freed.
114  */
115 static void shm_destroy (struct shmid_kernel *shp)
116 {
117         shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
118         shm_rmid (shp->id);
119         shm_unlock(shp);
120         if (!is_file_hugepages(shp->shm_file))
121                 shmem_lock(shp->shm_file, 0, shp->mlock_user);
122         else
123                 user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
124                                                 shp->mlock_user);
125         fput (shp->shm_file);
126         security_shm_free(shp);
127         ipc_rcu_putref(shp);
128 }
129
130 /*
131  * remove the attach descriptor shmd.
132  * free memory for segment if it is marked destroyed.
133  * The descriptor has already been removed from the current->mm->mmap list
134  * and will later be kfree()d.
135  */
136 static void shm_close (struct vm_area_struct *shmd)
137 {
138         struct file * file = shmd->vm_file;
139         int id = file->f_dentry->d_inode->i_ino;
140         struct shmid_kernel *shp;
141
142         down (&shm_ids.sem);
143         /* remove from the list of attaches of the shm segment */
144         if(!(shp = shm_lock(id)))
145                 BUG();
146         shp->shm_lprid = current->tgid;
147         shp->shm_dtim = get_seconds();
148         shp->shm_nattch--;
149         if(shp->shm_nattch == 0 &&
150            shp->shm_perm.mode & SHM_DEST)
151                 shm_destroy (shp);
152         else
153                 shm_unlock(shp);
154         up (&shm_ids.sem);
155 }
156
157 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
158 {
159         int ret;
160
161         ret = shmem_mmap(file, vma);
162         if (ret == 0) {
163                 vma->vm_ops = &shm_vm_ops;
164                 if (!(vma->vm_flags & VM_WRITE))
165                         vma->vm_flags &= ~VM_MAYWRITE;
166                 shm_inc(file->f_dentry->d_inode->i_ino);
167         }
168
169         return ret;
170 }
171
172 static struct file_operations shm_file_operations = {
173         .mmap   = shm_mmap,
174 #ifndef CONFIG_MMU
175         .get_unmapped_area = shmem_get_unmapped_area,
176 #endif
177 };
178
179 static struct vm_operations_struct shm_vm_ops = {
180         .open   = shm_open,     /* callback for a new vm-area open */
181         .close  = shm_close,    /* callback for when the vm-area is released */
182         .nopage = shmem_nopage,
183 #if defined(CONFIG_NUMA) && defined(CONFIG_SHMEM)
184         .set_policy = shmem_set_policy,
185         .get_policy = shmem_get_policy,
186 #endif
187 };
188
189 static int newseg (key_t key, int shmflg, size_t size)
190 {
191         int error;
192         struct shmid_kernel *shp;
193         int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
194         struct file * file;
195         char name[13];
196         int id;
197
198         if (size < SHMMIN || size > shm_ctlmax)
199                 return -EINVAL;
200
201         if (shm_tot + numpages >= shm_ctlall)
202                 return -ENOSPC;
203
204         shp = ipc_rcu_alloc(sizeof(*shp));
205         if (!shp)
206                 return -ENOMEM;
207
208         shp->shm_perm.key = key;
209         shp->shm_perm.mode = (shmflg & S_IRWXUGO);
210         shp->mlock_user = NULL;
211
212         shp->shm_perm.security = NULL;
213         error = security_shm_alloc(shp);
214         if (error) {
215                 ipc_rcu_putref(shp);
216                 return error;
217         }
218
219         if (shmflg & SHM_HUGETLB) {
220                 /* hugetlb_zero_setup takes care of mlock user accounting */
221                 file = hugetlb_zero_setup(size);
222                 shp->mlock_user = current->user;
223         } else {
224                 int acctflag = VM_ACCOUNT;
225                 /*
226                  * Do not allow no accounting for OVERCOMMIT_NEVER, even
227                  * if it's asked for.
228                  */
229                 if  ((shmflg & SHM_NORESERVE) &&
230                                 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
231                         acctflag = 0;
232                 sprintf (name, "SYSV%08x", key);
233                 file = shmem_file_setup(name, size, acctflag);
234         }
235         error = PTR_ERR(file);
236         if (IS_ERR(file))
237                 goto no_file;
238
239         error = -ENOSPC;
240         id = shm_addid(shp);
241         if(id == -1) 
242                 goto no_id;
243
244         shp->shm_cprid = current->tgid;
245         shp->shm_lprid = 0;
246         shp->shm_atim = shp->shm_dtim = 0;
247         shp->shm_ctim = get_seconds();
248         shp->shm_segsz = size;
249         shp->shm_nattch = 0;
250         shp->id = shm_buildid(id,shp->shm_perm.seq);
251         shp->shm_file = file;
252         file->f_dentry->d_inode->i_ino = shp->id;
253
254         /* Hugetlb ops would have already been assigned. */
255         if (!(shmflg & SHM_HUGETLB))
256                 file->f_op = &shm_file_operations;
257
258         shm_tot += numpages;
259         shm_unlock(shp);
260         return shp->id;
261
262 no_id:
263         fput(file);
264 no_file:
265         security_shm_free(shp);
266         ipc_rcu_putref(shp);
267         return error;
268 }
269
270 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
271 {
272         struct shmid_kernel *shp;
273         int err, id = 0;
274
275         down(&shm_ids.sem);
276         if (key == IPC_PRIVATE) {
277                 err = newseg(key, shmflg, size);
278         } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
279                 if (!(shmflg & IPC_CREAT))
280                         err = -ENOENT;
281                 else
282                         err = newseg(key, shmflg, size);
283         } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
284                 err = -EEXIST;
285         } else {
286                 shp = shm_lock(id);
287                 if(shp==NULL)
288                         BUG();
289                 if (shp->shm_segsz < size)
290                         err = -EINVAL;
291                 else if (ipcperms(&shp->shm_perm, shmflg))
292                         err = -EACCES;
293                 else {
294                         int shmid = shm_buildid(id, shp->shm_perm.seq);
295                         err = security_shm_associate(shp, shmflg);
296                         if (!err)
297                                 err = shmid;
298                 }
299                 shm_unlock(shp);
300         }
301         up(&shm_ids.sem);
302
303         return err;
304 }
305
306 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
307 {
308         switch(version) {
309         case IPC_64:
310                 return copy_to_user(buf, in, sizeof(*in));
311         case IPC_OLD:
312             {
313                 struct shmid_ds out;
314
315                 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
316                 out.shm_segsz   = in->shm_segsz;
317                 out.shm_atime   = in->shm_atime;
318                 out.shm_dtime   = in->shm_dtime;
319                 out.shm_ctime   = in->shm_ctime;
320                 out.shm_cpid    = in->shm_cpid;
321                 out.shm_lpid    = in->shm_lpid;
322                 out.shm_nattch  = in->shm_nattch;
323
324                 return copy_to_user(buf, &out, sizeof(out));
325             }
326         default:
327                 return -EINVAL;
328         }
329 }
330
331 struct shm_setbuf {
332         uid_t   uid;
333         gid_t   gid;
334         mode_t  mode;
335 };      
336
337 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
338 {
339         switch(version) {
340         case IPC_64:
341             {
342                 struct shmid64_ds tbuf;
343
344                 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
345                         return -EFAULT;
346
347                 out->uid        = tbuf.shm_perm.uid;
348                 out->gid        = tbuf.shm_perm.gid;
349                 out->mode       = tbuf.shm_perm.mode;
350
351                 return 0;
352             }
353         case IPC_OLD:
354             {
355                 struct shmid_ds tbuf_old;
356
357                 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
358                         return -EFAULT;
359
360                 out->uid        = tbuf_old.shm_perm.uid;
361                 out->gid        = tbuf_old.shm_perm.gid;
362                 out->mode       = tbuf_old.shm_perm.mode;
363
364                 return 0;
365             }
366         default:
367                 return -EINVAL;
368         }
369 }
370
371 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
372 {
373         switch(version) {
374         case IPC_64:
375                 return copy_to_user(buf, in, sizeof(*in));
376         case IPC_OLD:
377             {
378                 struct shminfo out;
379
380                 if(in->shmmax > INT_MAX)
381                         out.shmmax = INT_MAX;
382                 else
383                         out.shmmax = (int)in->shmmax;
384
385                 out.shmmin      = in->shmmin;
386                 out.shmmni      = in->shmmni;
387                 out.shmseg      = in->shmseg;
388                 out.shmall      = in->shmall; 
389
390                 return copy_to_user(buf, &out, sizeof(out));
391             }
392         default:
393                 return -EINVAL;
394         }
395 }
396
397 static void shm_get_stat(unsigned long *rss, unsigned long *swp) 
398 {
399         int i;
400
401         *rss = 0;
402         *swp = 0;
403
404         for (i = 0; i <= shm_ids.max_id; i++) {
405                 struct shmid_kernel *shp;
406                 struct inode *inode;
407
408                 shp = shm_get(i);
409                 if(!shp)
410                         continue;
411
412                 inode = shp->shm_file->f_dentry->d_inode;
413
414                 if (is_file_hugepages(shp->shm_file)) {
415                         struct address_space *mapping = inode->i_mapping;
416                         *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
417                 } else {
418                         struct shmem_inode_info *info = SHMEM_I(inode);
419                         spin_lock(&info->lock);
420                         *rss += inode->i_mapping->nrpages;
421                         *swp += info->swapped;
422                         spin_unlock(&info->lock);
423                 }
424         }
425 }
426
427 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
428 {
429         struct shm_setbuf setbuf;
430         struct shmid_kernel *shp;
431         int err, version;
432
433         if (cmd < 0 || shmid < 0) {
434                 err = -EINVAL;
435                 goto out;
436         }
437
438         version = ipc_parse_version(&cmd);
439
440         switch (cmd) { /* replace with proc interface ? */
441         case IPC_INFO:
442         {
443                 struct shminfo64 shminfo;
444
445                 err = security_shm_shmctl(NULL, cmd);
446                 if (err)
447                         return err;
448
449                 memset(&shminfo,0,sizeof(shminfo));
450                 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
451                 shminfo.shmmax = shm_ctlmax;
452                 shminfo.shmall = shm_ctlall;
453
454                 shminfo.shmmin = SHMMIN;
455                 if(copy_shminfo_to_user (buf, &shminfo, version))
456                         return -EFAULT;
457                 /* reading a integer is always atomic */
458                 err= shm_ids.max_id;
459                 if(err<0)
460                         err = 0;
461                 goto out;
462         }
463         case SHM_INFO:
464         {
465                 struct shm_info shm_info;
466
467                 err = security_shm_shmctl(NULL, cmd);
468                 if (err)
469                         return err;
470
471                 memset(&shm_info,0,sizeof(shm_info));
472                 down(&shm_ids.sem);
473                 shm_info.used_ids = shm_ids.in_use;
474                 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
475                 shm_info.shm_tot = shm_tot;
476                 shm_info.swap_attempts = 0;
477                 shm_info.swap_successes = 0;
478                 err = shm_ids.max_id;
479                 up(&shm_ids.sem);
480                 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
481                         err = -EFAULT;
482                         goto out;
483                 }
484
485                 err = err < 0 ? 0 : err;
486                 goto out;
487         }
488         case SHM_STAT:
489         case IPC_STAT:
490         {
491                 struct shmid64_ds tbuf;
492                 int result;
493                 memset(&tbuf, 0, sizeof(tbuf));
494                 shp = shm_lock(shmid);
495                 if(shp==NULL) {
496                         err = -EINVAL;
497                         goto out;
498                 } else if(cmd==SHM_STAT) {
499                         err = -EINVAL;
500                         if (shmid > shm_ids.max_id)
501                                 goto out_unlock;
502                         result = shm_buildid(shmid, shp->shm_perm.seq);
503                 } else {
504                         err = shm_checkid(shp,shmid);
505                         if(err)
506                                 goto out_unlock;
507                         result = 0;
508                 }
509                 err=-EACCES;
510                 if (ipcperms (&shp->shm_perm, S_IRUGO))
511                         goto out_unlock;
512                 err = security_shm_shmctl(shp, cmd);
513                 if (err)
514                         goto out_unlock;
515                 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
516                 tbuf.shm_segsz  = shp->shm_segsz;
517                 tbuf.shm_atime  = shp->shm_atim;
518                 tbuf.shm_dtime  = shp->shm_dtim;
519                 tbuf.shm_ctime  = shp->shm_ctim;
520                 tbuf.shm_cpid   = shp->shm_cprid;
521                 tbuf.shm_lpid   = shp->shm_lprid;
522                 if (!is_file_hugepages(shp->shm_file))
523                         tbuf.shm_nattch = shp->shm_nattch;
524                 else
525                         tbuf.shm_nattch = file_count(shp->shm_file) - 1;
526                 shm_unlock(shp);
527                 if(copy_shmid_to_user (buf, &tbuf, version))
528                         err = -EFAULT;
529                 else
530                         err = result;
531                 goto out;
532         }
533         case SHM_LOCK:
534         case SHM_UNLOCK:
535         {
536                 shp = shm_lock(shmid);
537                 if(shp==NULL) {
538                         err = -EINVAL;
539                         goto out;
540                 }
541                 err = shm_checkid(shp,shmid);
542                 if(err)
543                         goto out_unlock;
544
545                 if (!capable(CAP_IPC_LOCK)) {
546                         err = -EPERM;
547                         if (current->euid != shp->shm_perm.uid &&
548                             current->euid != shp->shm_perm.cuid)
549                                 goto out_unlock;
550                         if (cmd == SHM_LOCK &&
551                             !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
552                                 goto out_unlock;
553                 }
554
555                 err = security_shm_shmctl(shp, cmd);
556                 if (err)
557                         goto out_unlock;
558                 
559                 if(cmd==SHM_LOCK) {
560                         struct user_struct * user = current->user;
561                         if (!is_file_hugepages(shp->shm_file)) {
562                                 err = shmem_lock(shp->shm_file, 1, user);
563                                 if (!err) {
564                                         shp->shm_perm.mode |= SHM_LOCKED;
565                                         shp->mlock_user = user;
566                                 }
567                         }
568                 } else if (!is_file_hugepages(shp->shm_file)) {
569                         shmem_lock(shp->shm_file, 0, shp->mlock_user);
570                         shp->shm_perm.mode &= ~SHM_LOCKED;
571                         shp->mlock_user = NULL;
572                 }
573                 shm_unlock(shp);
574                 goto out;
575         }
576         case IPC_RMID:
577         {
578                 /*
579                  *      We cannot simply remove the file. The SVID states
580                  *      that the block remains until the last person
581                  *      detaches from it, then is deleted. A shmat() on
582                  *      an RMID segment is legal in older Linux and if 
583                  *      we change it apps break...
584                  *
585                  *      Instead we set a destroyed flag, and then blow
586                  *      the name away when the usage hits zero.
587                  */
588                 down(&shm_ids.sem);
589                 shp = shm_lock(shmid);
590                 err = -EINVAL;
591                 if (shp == NULL) 
592                         goto out_up;
593                 err = shm_checkid(shp, shmid);
594                 if(err)
595                         goto out_unlock_up;
596
597                 if (current->euid != shp->shm_perm.uid &&
598                     current->euid != shp->shm_perm.cuid && 
599                     !capable(CAP_SYS_ADMIN)) {
600                         err=-EPERM;
601                         goto out_unlock_up;
602                 }
603
604                 err = security_shm_shmctl(shp, cmd);
605                 if (err)
606                         goto out_unlock_up;
607
608                 if (shp->shm_nattch){
609                         shp->shm_perm.mode |= SHM_DEST;
610                         /* Do not find it any more */
611                         shp->shm_perm.key = IPC_PRIVATE;
612                         shm_unlock(shp);
613                 } else
614                         shm_destroy (shp);
615                 up(&shm_ids.sem);
616                 goto out;
617         }
618
619         case IPC_SET:
620         {
621                 if (copy_shmid_from_user (&setbuf, buf, version)) {
622                         err = -EFAULT;
623                         goto out;
624                 }
625                 if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
626                         return err;
627                 down(&shm_ids.sem);
628                 shp = shm_lock(shmid);
629                 err=-EINVAL;
630                 if(shp==NULL)
631                         goto out_up;
632                 err = shm_checkid(shp,shmid);
633                 if(err)
634                         goto out_unlock_up;
635                 err=-EPERM;
636                 if (current->euid != shp->shm_perm.uid &&
637                     current->euid != shp->shm_perm.cuid && 
638                     !capable(CAP_SYS_ADMIN)) {
639                         goto out_unlock_up;
640                 }
641
642                 err = security_shm_shmctl(shp, cmd);
643                 if (err)
644                         goto out_unlock_up;
645                 
646                 shp->shm_perm.uid = setbuf.uid;
647                 shp->shm_perm.gid = setbuf.gid;
648                 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
649                         | (setbuf.mode & S_IRWXUGO);
650                 shp->shm_ctim = get_seconds();
651                 break;
652         }
653
654         default:
655                 err = -EINVAL;
656                 goto out;
657         }
658
659         err = 0;
660 out_unlock_up:
661         shm_unlock(shp);
662 out_up:
663         up(&shm_ids.sem);
664         goto out;
665 out_unlock:
666         shm_unlock(shp);
667 out:
668         return err;
669 }
670
671 /*
672  * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
673  *
674  * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
675  * "raddr" thing points to kernel space, and there has to be a wrapper around
676  * this.
677  */
678 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
679 {
680         struct shmid_kernel *shp;
681         unsigned long addr;
682         unsigned long size;
683         struct file * file;
684         int    err;
685         unsigned long flags;
686         unsigned long prot;
687         unsigned long o_flags;
688         int acc_mode;
689         void *user_addr;
690
691         if (shmid < 0) {
692                 err = -EINVAL;
693                 goto out;
694         } else if ((addr = (ulong)shmaddr)) {
695                 if (addr & (SHMLBA-1)) {
696                         if (shmflg & SHM_RND)
697                                 addr &= ~(SHMLBA-1);       /* round down */
698                         else
699 #ifndef __ARCH_FORCE_SHMLBA
700                                 if (addr & ~PAGE_MASK)
701 #endif
702                                         return -EINVAL;
703                 }
704                 flags = MAP_SHARED | MAP_FIXED;
705         } else {
706                 if ((shmflg & SHM_REMAP))
707                         return -EINVAL;
708
709                 flags = MAP_SHARED;
710         }
711
712         if (shmflg & SHM_RDONLY) {
713                 prot = PROT_READ;
714                 o_flags = O_RDONLY;
715                 acc_mode = S_IRUGO;
716         } else {
717                 prot = PROT_READ | PROT_WRITE;
718                 o_flags = O_RDWR;
719                 acc_mode = S_IRUGO | S_IWUGO;
720         }
721         if (shmflg & SHM_EXEC) {
722                 prot |= PROT_EXEC;
723                 acc_mode |= S_IXUGO;
724         }
725
726         /*
727          * We cannot rely on the fs check since SYSV IPC does have an
728          * additional creator id...
729          */
730         shp = shm_lock(shmid);
731         if(shp == NULL) {
732                 err = -EINVAL;
733                 goto out;
734         }
735         err = shm_checkid(shp,shmid);
736         if (err) {
737                 shm_unlock(shp);
738                 goto out;
739         }
740         if (ipcperms(&shp->shm_perm, acc_mode)) {
741                 shm_unlock(shp);
742                 err = -EACCES;
743                 goto out;
744         }
745
746         err = security_shm_shmat(shp, shmaddr, shmflg);
747         if (err) {
748                 shm_unlock(shp);
749                 return err;
750         }
751                 
752         file = shp->shm_file;
753         size = i_size_read(file->f_dentry->d_inode);
754         shp->shm_nattch++;
755         shm_unlock(shp);
756
757         down_write(&current->mm->mmap_sem);
758         if (addr && !(shmflg & SHM_REMAP)) {
759                 user_addr = ERR_PTR(-EINVAL);
760                 if (find_vma_intersection(current->mm, addr, addr + size))
761                         goto invalid;
762                 /*
763                  * If shm segment goes below stack, make sure there is some
764                  * space left for the stack to grow (at least 4 pages).
765                  */
766                 if (addr < current->mm->start_stack &&
767                     addr > current->mm->start_stack - size - PAGE_SIZE * 5)
768                         goto invalid;
769         }
770                 
771         user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
772
773 invalid:
774         up_write(&current->mm->mmap_sem);
775
776         down (&shm_ids.sem);
777         if(!(shp = shm_lock(shmid)))
778                 BUG();
779         shp->shm_nattch--;
780         if(shp->shm_nattch == 0 &&
781            shp->shm_perm.mode & SHM_DEST)
782                 shm_destroy (shp);
783         else
784                 shm_unlock(shp);
785         up (&shm_ids.sem);
786
787         *raddr = (unsigned long) user_addr;
788         err = 0;
789         if (IS_ERR(user_addr))
790                 err = PTR_ERR(user_addr);
791 out:
792         return err;
793 }
794
795 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
796 {
797         unsigned long ret;
798         long err;
799
800         err = do_shmat(shmid, shmaddr, shmflg, &ret);
801         if (err)
802                 return err;
803         force_successful_syscall_return();
804         return (long)ret;
805 }
806
807 /*
808  * detach and kill segment if marked destroyed.
809  * The work is done in shm_close.
810  */
811 asmlinkage long sys_shmdt(char __user *shmaddr)
812 {
813         struct mm_struct *mm = current->mm;
814         struct vm_area_struct *vma, *next;
815         unsigned long addr = (unsigned long)shmaddr;
816         loff_t size = 0;
817         int retval = -EINVAL;
818
819         down_write(&mm->mmap_sem);
820
821         /*
822          * This function tries to be smart and unmap shm segments that
823          * were modified by partial mlock or munmap calls:
824          * - It first determines the size of the shm segment that should be
825          *   unmapped: It searches for a vma that is backed by shm and that
826          *   started at address shmaddr. It records it's size and then unmaps
827          *   it.
828          * - Then it unmaps all shm vmas that started at shmaddr and that
829          *   are within the initially determined size.
830          * Errors from do_munmap are ignored: the function only fails if
831          * it's called with invalid parameters or if it's called to unmap
832          * a part of a vma. Both calls in this function are for full vmas,
833          * the parameters are directly copied from the vma itself and always
834          * valid - therefore do_munmap cannot fail. (famous last words?)
835          */
836         /*
837          * If it had been mremap()'d, the starting address would not
838          * match the usual checks anyway. So assume all vma's are
839          * above the starting address given.
840          */
841         vma = find_vma(mm, addr);
842
843         while (vma) {
844                 next = vma->vm_next;
845
846                 /*
847                  * Check if the starting address would match, i.e. it's
848                  * a fragment created by mprotect() and/or munmap(), or it
849                  * otherwise it starts at this address with no hassles.
850                  */
851                 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
852                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
853
854
855                         size = vma->vm_file->f_dentry->d_inode->i_size;
856                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
857                         /*
858                          * We discovered the size of the shm segment, so
859                          * break out of here and fall through to the next
860                          * loop that uses the size information to stop
861                          * searching for matching vma's.
862                          */
863                         retval = 0;
864                         vma = next;
865                         break;
866                 }
867                 vma = next;
868         }
869
870         /*
871          * We need look no further than the maximum address a fragment
872          * could possibly have landed at. Also cast things to loff_t to
873          * prevent overflows and make comparisions vs. equal-width types.
874          */
875         size = PAGE_ALIGN(size);
876         while (vma && (loff_t)(vma->vm_end - addr) <= size) {
877                 next = vma->vm_next;
878
879                 /* finding a matching vma now does not alter retval */
880                 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
881                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
882
883                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
884                 vma = next;
885         }
886
887         up_write(&mm->mmap_sem);
888         return retval;
889 }
890
891 #ifdef CONFIG_PROC_FS
892 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
893 {
894         struct shmid_kernel *shp = it;
895         char *format;
896
897 #define SMALL_STRING "%10d %10d  %4o %10u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
898 #define BIG_STRING   "%10d %10d  %4o %21u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
899
900         if (sizeof(size_t) <= sizeof(int))
901                 format = SMALL_STRING;
902         else
903                 format = BIG_STRING;
904         return seq_printf(s, format,
905                           shp->shm_perm.key,
906                           shp->id,
907                           shp->shm_perm.mode,
908                           shp->shm_segsz,
909                           shp->shm_cprid,
910                           shp->shm_lprid,
911                           is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
912                           shp->shm_perm.uid,
913                           shp->shm_perm.gid,
914                           shp->shm_perm.cuid,
915                           shp->shm_perm.cgid,
916                           shp->shm_atim,
917                           shp->shm_dtim,
918                           shp->shm_ctim);
919 }
920 #endif