~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/ipc/shm.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.14 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.57 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.138 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.193 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp