2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* linux/ipc/shm.c
|
|
|
|
* Copyright (C) 1992, 1993 Krishna Balasubramanian
|
|
|
|
* Many improvements/fixes by Bruno Haible.
|
|
|
|
* Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
|
|
|
|
* Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
|
|
|
|
*
|
|
|
|
* /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
|
|
|
|
* BIGMEM support, Andrea Arcangeli <andrea@suse.de>
|
|
|
|
* SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
|
|
|
|
* HIGHMEM support, Ingo Molnar <mingo@redhat.com>
|
|
|
|
* Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
|
|
|
|
* Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
|
|
|
|
* Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
|
|
|
|
*
|
2006-04-03 01:07:33 +04:00
|
|
|
* support for audit of ipc object properties and permission changes
|
|
|
|
* Dustin Kirkland <dustin.kirkland@us.ibm.com>
|
2006-10-02 13:18:22 +04:00
|
|
|
*
|
|
|
|
* namespaces support
|
|
|
|
* OpenVZ, SWsoft Inc.
|
|
|
|
* Pavel Emelianov <xemul@openvz.org>
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/hugetlb.h>
|
|
|
|
#include <linux/shm.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/mman.h>
|
|
|
|
#include <linux/shmem_fs.h>
|
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/audit.h>
|
2006-01-11 23:17:46 +03:00
|
|
|
#include <linux/capability.h>
|
2005-05-01 19:59:12 +04:00
|
|
|
#include <linux/ptrace.h>
|
2005-09-07 02:17:10 +04:00
|
|
|
#include <linux/seq_file.h>
|
2007-10-19 10:40:54 +04:00
|
|
|
#include <linux/rwsem.h>
|
2006-10-02 13:18:22 +04:00
|
|
|
#include <linux/nsproxy.h>
|
2007-02-21 00:57:53 +03:00
|
|
|
#include <linux/mount.h>
|
2008-02-08 15:18:22 +03:00
|
|
|
#include <linux/ipc_namespace.h>
|
2005-05-01 19:59:12 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
struct shm_file_data {
|
|
|
|
int id;
|
|
|
|
struct ipc_namespace *ns;
|
|
|
|
struct file *file;
|
|
|
|
const struct vm_operations_struct *vm_ops;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
|
|
|
|
|
2007-02-12 11:55:35 +03:00
|
|
|
static const struct file_operations shm_file_operations;
|
2009-09-27 22:29:37 +04:00
|
|
|
static const struct vm_operations_struct shm_vm_ops;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-02-08 15:18:57 +03:00
|
|
|
#define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2006-10-02 13:18:22 +04:00
|
|
|
#define shm_unlock(shp) \
|
|
|
|
ipc_unlock(&(shp)->shm_perm)
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2007-10-19 10:40:49 +04:00
|
|
|
static int newseg(struct ipc_namespace *, struct ipc_params *);
|
2007-02-21 00:57:53 +03:00
|
|
|
static void shm_open(struct vm_area_struct *vma);
|
|
|
|
static void shm_close(struct vm_area_struct *vma);
|
2006-10-02 13:18:22 +04:00
|
|
|
static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
|
2005-04-17 02:20:36 +04:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2005-09-07 02:17:10 +04:00
|
|
|
static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
|
2005-04-17 02:20:36 +04:00
|
|
|
#endif
|
|
|
|
|
2008-02-08 15:18:57 +03:00
|
|
|
void shm_init_ns(struct ipc_namespace *ns)
|
2006-10-02 13:18:22 +04:00
|
|
|
{
|
|
|
|
ns->shm_ctlmax = SHMMAX;
|
|
|
|
ns->shm_ctlall = SHMALL;
|
|
|
|
ns->shm_ctlmni = SHMMNI;
|
|
|
|
ns->shm_tot = 0;
|
2009-01-07 01:42:49 +03:00
|
|
|
ipc_init_ids(&shm_ids(ns));
|
2006-10-02 13:18:22 +04:00
|
|
|
}
|
|
|
|
|
2007-10-19 10:40:53 +04:00
|
|
|
/*
|
2007-10-19 10:40:54 +04:00
|
|
|
* Called with shm_ids.rw_mutex (writer) and the shp structure locked.
|
|
|
|
* Only shm_ids.rw_mutex remains locked on exit.
|
2007-10-19 10:40:53 +04:00
|
|
|
*/
|
2008-02-08 15:18:57 +03:00
|
|
|
static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
|
2006-10-02 13:18:22 +04:00
|
|
|
{
|
2008-02-08 15:18:57 +03:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
|
|
|
|
2006-10-02 13:18:22 +04:00
|
|
|
if (shp->shm_nattch){
|
|
|
|
shp->shm_perm.mode |= SHM_DEST;
|
|
|
|
/* Do not find it any more */
|
|
|
|
shp->shm_perm.key = IPC_PRIVATE;
|
|
|
|
shm_unlock(shp);
|
|
|
|
} else
|
|
|
|
shm_destroy(ns, shp);
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:18:22 +03:00
|
|
|
#ifdef CONFIG_IPC_NS
|
2006-10-02 13:18:22 +04:00
|
|
|
void shm_exit_ns(struct ipc_namespace *ns)
|
|
|
|
{
|
2008-02-08 15:18:57 +03:00
|
|
|
free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
|
2009-12-16 03:47:27 +03:00
|
|
|
idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
|
2006-10-02 13:18:22 +04:00
|
|
|
}
|
2008-02-08 15:18:22 +03:00
|
|
|
#endif
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
void __init shm_init (void)
|
|
|
|
{
|
2008-02-08 15:18:57 +03:00
|
|
|
shm_init_ns(&init_ipc_ns);
|
2005-09-07 02:17:10 +04:00
|
|
|
ipc_init_proc_interface("sysvipc/shm",
|
2010-10-28 02:34:16 +04:00
|
|
|
#if BITS_PER_LONG <= 32
|
|
|
|
" key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
|
|
|
|
#else
|
|
|
|
" key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
|
|
|
|
#endif
|
2006-10-02 13:18:22 +04:00
|
|
|
IPC_SHM_IDS, sysvipc_shm_proc_show);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2007-10-19 10:40:54 +04:00
|
|
|
/*
|
|
|
|
* shm_lock_(check_) routines are called in the paths where the rw_mutex
|
2008-07-25 12:48:03 +04:00
|
|
|
* is not necessarily held.
|
2007-10-19 10:40:54 +04:00
|
|
|
*/
|
2007-10-19 10:40:51 +04:00
|
|
|
static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2007-10-19 10:40:51 +04:00
|
|
|
struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
|
|
|
|
|
IPC: fix error check in all new xxx_lock() and xxx_exit_ns() functions
In the new implementation of the [sem|shm|msg]_lock[_check]() routines, we
use the return value of ipc_lock() in container_of() without any check.
But ipc_lock may return a errcode. The use of this errcode in
container_of() may alter this errcode, and we don't want this.
And in xxx_exit_ns, the pointer return by idr_find is of type 'struct
kern_ipc_per'...
Today, the code will work as is because the member used in these
container_of() is the first member of its container (offset == 0), the
errcode isn't changed then. But in the general case, we can't count on
this assumption and this may lead later to a real bug if we don't correct
this.
Again, the proposed solution is simple and correct. But, as pointed by
Nadia, with this solution, the same check will be done several times (in
all sub-callers...), what is not very funny/optimal...
Signed-off-by: Pierre Peiffer <pierre.peiffer@bull.net>
Cc: Nadia Derbey <Nadia.Derbey@bull.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-02-06 12:36:23 +03:00
|
|
|
if (IS_ERR(ipcp))
|
|
|
|
return (struct shmid_kernel *)ipcp;
|
|
|
|
|
2007-10-19 10:40:51 +04:00
|
|
|
return container_of(ipcp, struct shmid_kernel, shm_perm);
|
2007-10-19 10:40:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
|
|
|
|
int id)
|
|
|
|
{
|
2007-10-19 10:40:51 +04:00
|
|
|
struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
|
|
|
|
|
IPC: fix error check in all new xxx_lock() and xxx_exit_ns() functions
In the new implementation of the [sem|shm|msg]_lock[_check]() routines, we
use the return value of ipc_lock() in container_of() without any check.
But ipc_lock may return a errcode. The use of this errcode in
container_of() may alter this errcode, and we don't want this.
And in xxx_exit_ns, the pointer return by idr_find is of type 'struct
kern_ipc_per'...
Today, the code will work as is because the member used in these
container_of() is the first member of its container (offset == 0), the
errcode isn't changed then. But in the general case, we can't count on
this assumption and this may lead later to a real bug if we don't correct
this.
Again, the proposed solution is simple and correct. But, as pointed by
Nadia, with this solution, the same check will be done several times (in
all sub-callers...), what is not very funny/optimal...
Signed-off-by: Pierre Peiffer <pierre.peiffer@bull.net>
Cc: Nadia Derbey <Nadia.Derbey@bull.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-02-06 12:36:23 +03:00
|
|
|
if (IS_ERR(ipcp))
|
|
|
|
return (struct shmid_kernel *)ipcp;
|
|
|
|
|
2007-10-19 10:40:51 +04:00
|
|
|
return container_of(ipcp, struct shmid_kernel, shm_perm);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2007-10-19 10:40:48 +04:00
|
|
|
static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2007-10-19 10:40:48 +04:00
|
|
|
ipc_rmid(&shm_ids(ns), &s->shm_perm);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
/* This is called by fork, once for every shm attach. */
|
|
|
|
static void shm_open(struct vm_area_struct *vma)
|
2006-10-02 13:18:22 +04:00
|
|
|
{
|
2007-02-21 00:57:53 +03:00
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2005-04-17 02:20:36 +04:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
shp = shm_lock(sfd->ns, sfd->id);
|
2007-10-19 10:40:51 +04:00
|
|
|
BUG_ON(IS_ERR(shp));
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->shm_atim = get_seconds();
|
2007-10-19 10:40:14 +04:00
|
|
|
shp->shm_lprid = task_tgid_vnr(current);
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->shm_nattch++;
|
|
|
|
shm_unlock(shp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shm_destroy - free the struct shmid_kernel
|
|
|
|
*
|
2007-10-19 10:40:53 +04:00
|
|
|
* @ns: namespace
|
2005-04-17 02:20:36 +04:00
|
|
|
* @shp: struct to free
|
|
|
|
*
|
2007-10-19 10:40:54 +04:00
|
|
|
* It has to be called with shp and shm_ids.rw_mutex (writer) locked,
|
2005-04-17 02:20:36 +04:00
|
|
|
* but returns with shp unlocked and freed.
|
|
|
|
*/
|
2006-10-02 13:18:22 +04:00
|
|
|
static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2006-10-02 13:18:22 +04:00
|
|
|
ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
2007-10-19 10:40:48 +04:00
|
|
|
shm_rmid(ns, shp);
|
2005-04-17 02:20:36 +04:00
|
|
|
shm_unlock(shp);
|
|
|
|
if (!is_file_hugepages(shp->shm_file))
|
|
|
|
shmem_lock(shp->shm_file, 0, shp->mlock_user);
|
2009-08-24 19:30:28 +04:00
|
|
|
else if (shp->mlock_user)
|
2006-12-08 13:37:11 +03:00
|
|
|
user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->mlock_user);
|
|
|
|
fput (shp->shm_file);
|
|
|
|
security_shm_free(shp);
|
|
|
|
ipc_rcu_putref(shp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-21 00:57:53 +03:00
|
|
|
* remove the attach descriptor vma.
|
2005-04-17 02:20:36 +04:00
|
|
|
* free memory for segment if it is marked destroyed.
|
|
|
|
* The descriptor has already been removed from the current->mm->mmap list
|
|
|
|
* and will later be kfree()d.
|
|
|
|
*/
|
2007-02-21 00:57:53 +03:00
|
|
|
static void shm_close(struct vm_area_struct *vma)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2007-02-21 00:57:53 +03:00
|
|
|
struct file * file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2005-04-17 02:20:36 +04:00
|
|
|
struct shmid_kernel *shp;
|
2007-02-21 00:57:53 +03:00
|
|
|
struct ipc_namespace *ns = sfd->ns;
|
2006-10-02 13:18:22 +04:00
|
|
|
|
2007-10-19 10:40:54 +04:00
|
|
|
down_write(&shm_ids(ns).rw_mutex);
|
2005-04-17 02:20:36 +04:00
|
|
|
/* remove from the list of attaches of the shm segment */
|
2008-07-25 12:48:03 +04:00
|
|
|
shp = shm_lock(ns, sfd->id);
|
2007-10-19 10:40:51 +04:00
|
|
|
BUG_ON(IS_ERR(shp));
|
2007-10-19 10:40:14 +04:00
|
|
|
shp->shm_lprid = task_tgid_vnr(current);
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->shm_dtim = get_seconds();
|
|
|
|
shp->shm_nattch--;
|
|
|
|
if(shp->shm_nattch == 0 &&
|
2006-01-08 12:02:21 +03:00
|
|
|
shp->shm_perm.mode & SHM_DEST)
|
2006-10-02 13:18:22 +04:00
|
|
|
shm_destroy(ns, shp);
|
2005-04-17 02:20:36 +04:00
|
|
|
else
|
|
|
|
shm_unlock(shp);
|
2007-10-19 10:40:54 +04:00
|
|
|
up_write(&shm_ids(ns).rw_mutex);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:47:03 +04:00
|
|
|
static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
2007-02-21 00:57:53 +03:00
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
|
2007-07-19 12:47:03 +04:00
|
|
|
return sfd->vm_ops->fault(vma, vmf);
|
2007-02-21 00:57:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NUMA
|
2007-10-17 10:26:42 +04:00
|
|
|
static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
|
2007-02-21 00:57:53 +03:00
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
int err = 0;
|
|
|
|
if (sfd->vm_ops->set_policy)
|
|
|
|
err = sfd->vm_ops->set_policy(vma, new);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-10-17 10:26:42 +04:00
|
|
|
static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
|
|
|
|
unsigned long addr)
|
2007-02-21 00:57:53 +03:00
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
struct mempolicy *pol = NULL;
|
|
|
|
|
|
|
|
if (sfd->vm_ops->get_policy)
|
|
|
|
pol = sfd->vm_ops->get_policy(vma, addr);
|
mempolicy: rework mempolicy Reference Counting [yet again]
After further discussion with Christoph Lameter, it has become clear that my
earlier attempts to clean up the mempolicy reference counting were a bit of
overkill in some areas, resulting in superflous ref/unref in what are usually
fast paths. In other areas, further inspection reveals that I botched the
unref for interleave policies.
A separate patch, suitable for upstream/stable trees, fixes up the known
errors in the previous attempt to fix reference counting.
This patch reworks the memory policy referencing counting and, one hopes,
simplifies the code. Maybe I'll get it right this time.
See the update to the numa_memory_policy.txt document for a discussion of
memory policy reference counting that motivates this patch.
Summary:
Lookup of mempolicy, based on (vma, address) need only add a reference for
shared policy, and we need only unref the policy when finished for shared
policies. So, this patch backs out all of the unneeded extra reference
counting added by my previous attempt. It then unrefs only shared policies
when we're finished with them, using the mpol_cond_put() [conditional put]
helper function introduced by this patch.
Note that shmem_swapin() calls read_swap_cache_async() with a dummy vma
containing just the policy. read_swap_cache_async() can call alloc_page_vma()
multiple times, so we can't let alloc_page_vma() unref the shared policy in
this case. To avoid this, we make a copy of any non-null shared policy and
remove the MPOL_F_SHARED flag from the copy. This copy occurs before reading
a page [or multiple pages] from swap, so the overhead should not be an issue
here.
I introduced a new static inline function "mpol_cond_copy()" to copy the
shared policy to an on-stack policy and remove the flags that would require a
conditional free. The current implementation of mpol_cond_copy() assumes that
the struct mempolicy contains no pointers to dynamically allocated structures
that must be duplicated or reference counted during copy.
Signed-off-by: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Andi Kleen <ak@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-28 13:13:16 +04:00
|
|
|
else if (vma->vm_policy)
|
2007-02-21 00:57:53 +03:00
|
|
|
pol = vma->vm_policy;
|
mempolicy: rework mempolicy Reference Counting [yet again]
After further discussion with Christoph Lameter, it has become clear that my
earlier attempts to clean up the mempolicy reference counting were a bit of
overkill in some areas, resulting in superflous ref/unref in what are usually
fast paths. In other areas, further inspection reveals that I botched the
unref for interleave policies.
A separate patch, suitable for upstream/stable trees, fixes up the known
errors in the previous attempt to fix reference counting.
This patch reworks the memory policy referencing counting and, one hopes,
simplifies the code. Maybe I'll get it right this time.
See the update to the numa_memory_policy.txt document for a discussion of
memory policy reference counting that motivates this patch.
Summary:
Lookup of mempolicy, based on (vma, address) need only add a reference for
shared policy, and we need only unref the policy when finished for shared
policies. So, this patch backs out all of the unneeded extra reference
counting added by my previous attempt. It then unrefs only shared policies
when we're finished with them, using the mpol_cond_put() [conditional put]
helper function introduced by this patch.
Note that shmem_swapin() calls read_swap_cache_async() with a dummy vma
containing just the policy. read_swap_cache_async() can call alloc_page_vma()
multiple times, so we can't let alloc_page_vma() unref the shared policy in
this case. To avoid this, we make a copy of any non-null shared policy and
remove the MPOL_F_SHARED flag from the copy. This copy occurs before reading
a page [or multiple pages] from swap, so the overhead should not be an issue
here.
I introduced a new static inline function "mpol_cond_copy()" to copy the
shared policy to an on-stack policy and remove the flags that would require a
conditional free. The current implementation of mpol_cond_copy() assumes that
the struct mempolicy contains no pointers to dynamically allocated structures
that must be duplicated or reference counted during copy.
Signed-off-by: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Andi Kleen <ak@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-28 13:13:16 +04:00
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
return pol;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
static int shm_mmap(struct file * file, struct vm_area_struct * vma)
|
|
|
|
{
|
2007-02-21 00:57:53 +03:00
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2006-01-06 11:11:42 +03:00
|
|
|
int ret;
|
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
ret = sfd->file->f_op->mmap(sfd->file, vma);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
sfd->vm_ops = vma->vm_ops;
|
2007-07-31 11:37:24 +04:00
|
|
|
#ifdef CONFIG_MMU
|
2007-07-19 12:46:59 +04:00
|
|
|
BUG_ON(!sfd->vm_ops->fault);
|
2007-07-31 11:37:24 +04:00
|
|
|
#endif
|
2007-02-21 00:57:53 +03:00
|
|
|
vma->vm_ops = &shm_vm_ops;
|
|
|
|
shm_open(vma);
|
2006-01-06 11:11:42 +03:00
|
|
|
|
|
|
|
return ret;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2006-10-02 13:18:22 +04:00
|
|
|
static int shm_release(struct inode *ino, struct file *file)
|
|
|
|
{
|
2007-02-21 00:57:53 +03:00
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2006-10-02 13:18:22 +04:00
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
put_ipc_ns(sfd->ns);
|
|
|
|
shm_file_data(file) = NULL;
|
|
|
|
kfree(sfd);
|
2006-10-02 13:18:22 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-26 19:53:25 +04:00
|
|
|
static int shm_fsync(struct file *file, int datasync)
|
2007-03-02 02:46:08 +03:00
|
|
|
{
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
|
2010-05-26 19:53:25 +04:00
|
|
|
if (!sfd->file->f_op->fsync)
|
|
|
|
return -EINVAL;
|
|
|
|
return sfd->file->f_op->fsync(sfd->file, datasync);
|
2007-03-02 02:46:08 +03:00
|
|
|
}
|
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
static unsigned long shm_get_unmapped_area(struct file *file,
|
|
|
|
unsigned long addr, unsigned long len, unsigned long pgoff,
|
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2009-11-30 16:38:43 +03:00
|
|
|
return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
|
|
|
|
pgoff, flags);
|
2007-02-21 00:57:53 +03:00
|
|
|
}
|
|
|
|
|
2007-02-12 11:55:35 +03:00
|
|
|
static const struct file_operations shm_file_operations = {
|
2006-10-02 13:18:22 +04:00
|
|
|
.mmap = shm_mmap,
|
2007-03-02 02:46:08 +03:00
|
|
|
.fsync = shm_fsync,
|
2006-10-02 13:18:22 +04:00
|
|
|
.release = shm_release,
|
2010-01-16 04:01:32 +03:00
|
|
|
#ifndef CONFIG_MMU
|
|
|
|
.get_unmapped_area = shm_get_unmapped_area,
|
|
|
|
#endif
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 20:52:59 +04:00
|
|
|
.llseek = noop_llseek,
|
2009-11-30 16:38:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations shm_file_operations_huge = {
|
|
|
|
.mmap = shm_mmap,
|
|
|
|
.fsync = shm_fsync,
|
|
|
|
.release = shm_release,
|
2007-02-21 00:57:53 +03:00
|
|
|
.get_unmapped_area = shm_get_unmapped_area,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 20:52:59 +04:00
|
|
|
.llseek = noop_llseek,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2009-11-30 16:38:43 +03:00
|
|
|
int is_file_shm_hugepages(struct file *file)
|
|
|
|
{
|
|
|
|
return file->f_op == &shm_file_operations_huge;
|
|
|
|
}
|
|
|
|
|
2009-09-27 22:29:37 +04:00
|
|
|
static const struct vm_operations_struct shm_vm_ops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.open = shm_open, /* callback for a new vm-area open */
|
|
|
|
.close = shm_close, /* callback for when the vm-area is released */
|
2007-07-19 12:46:59 +04:00
|
|
|
.fault = shm_fault,
|
2007-02-21 00:57:53 +03:00
|
|
|
#if defined(CONFIG_NUMA)
|
|
|
|
.set_policy = shm_set_policy,
|
|
|
|
.get_policy = shm_get_policy,
|
2005-04-17 02:20:36 +04:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2007-10-19 10:40:53 +04:00
|
|
|
/**
|
|
|
|
* newseg - Create a new shared memory segment
|
|
|
|
* @ns: namespace
|
|
|
|
* @params: ptr to the structure that contains key, size and shmflg
|
|
|
|
*
|
2007-10-19 10:40:54 +04:00
|
|
|
* Called with shm_ids.rw_mutex held as a writer.
|
2007-10-19 10:40:53 +04:00
|
|
|
*/
|
|
|
|
|
2007-10-19 10:40:49 +04:00
|
|
|
static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2007-10-19 10:40:49 +04:00
|
|
|
key_t key = params->key;
|
|
|
|
int shmflg = params->flg;
|
|
|
|
size_t size = params->u.size;
|
2005-04-17 02:20:36 +04:00
|
|
|
int error;
|
|
|
|
struct shmid_kernel *shp;
|
|
|
|
int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
|
|
|
|
struct file * file;
|
|
|
|
char name[13];
|
|
|
|
int id;
|
2009-02-10 17:02:27 +03:00
|
|
|
int acctflag = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2006-10-02 13:18:22 +04:00
|
|
|
if (size < SHMMIN || size > ns->shm_ctlmax)
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EINVAL;
|
|
|
|
|
2007-01-23 21:20:04 +03:00
|
|
|
if (ns->shm_tot + numpages > ns->shm_ctlall)
|
2005-04-17 02:20:36 +04:00
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
shp = ipc_rcu_alloc(sizeof(*shp));
|
|
|
|
if (!shp)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
shp->shm_perm.key = key;
|
2006-01-08 12:02:21 +03:00
|
|
|
shp->shm_perm.mode = (shmflg & S_IRWXUGO);
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->mlock_user = NULL;
|
|
|
|
|
|
|
|
shp->shm_perm.security = NULL;
|
|
|
|
error = security_shm_alloc(shp);
|
|
|
|
if (error) {
|
|
|
|
ipc_rcu_putref(shp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2007-06-16 21:16:16 +04:00
|
|
|
sprintf (name, "SYSV%08x", key);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (shmflg & SHM_HUGETLB) {
|
2009-02-10 17:02:27 +03:00
|
|
|
/* hugetlb_file_setup applies strict accounting */
|
|
|
|
if (shmflg & SHM_NORESERVE)
|
|
|
|
acctflag = VM_NORESERVE;
|
2009-08-24 19:30:28 +04:00
|
|
|
file = hugetlb_file_setup(name, size, acctflag,
|
2009-09-22 04:03:43 +04:00
|
|
|
&shp->mlock_user, HUGETLB_SHMFS_INODE);
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
2005-11-07 11:59:27 +03:00
|
|
|
/*
|
|
|
|
* Do not allow no accounting for OVERCOMMIT_NEVER, even
|
|
|
|
* if it's asked for.
|
|
|
|
*/
|
|
|
|
if ((shmflg & SHM_NORESERVE) &&
|
|
|
|
sysctl_overcommit_memory != OVERCOMMIT_NEVER)
|
2009-02-01 02:08:56 +03:00
|
|
|
acctflag = VM_NORESERVE;
|
2005-11-07 11:59:27 +03:00
|
|
|
file = shmem_file_setup(name, size, acctflag);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
error = PTR_ERR(file);
|
|
|
|
if (IS_ERR(file))
|
|
|
|
goto no_file;
|
|
|
|
|
2008-04-29 12:00:35 +04:00
|
|
|
id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
|
2007-10-19 10:40:57 +04:00
|
|
|
if (id < 0) {
|
|
|
|
error = id;
|
2005-04-17 02:20:36 +04:00
|
|
|
goto no_id;
|
2007-10-19 10:40:57 +04:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2007-10-19 10:40:14 +04:00
|
|
|
shp->shm_cprid = task_tgid_vnr(current);
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->shm_lprid = 0;
|
|
|
|
shp->shm_atim = shp->shm_dtim = 0;
|
|
|
|
shp->shm_ctim = get_seconds();
|
|
|
|
shp->shm_segsz = size;
|
|
|
|
shp->shm_nattch = 0;
|
|
|
|
shp->shm_file = file;
|
2007-06-16 21:15:59 +04:00
|
|
|
/*
|
|
|
|
* shmid gets reported as "inode#" in /proc/pid/maps.
|
|
|
|
* proc-ps tools use this. Changing this will break them.
|
|
|
|
*/
|
2007-10-19 10:40:48 +04:00
|
|
|
file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
|
2005-10-30 04:16:45 +03:00
|
|
|
|
2006-10-02 13:18:22 +04:00
|
|
|
ns->shm_tot += numpages;
|
2007-10-19 10:40:48 +04:00
|
|
|
error = shp->shm_perm.id;
|
2005-04-17 02:20:36 +04:00
|
|
|
shm_unlock(shp);
|
2007-10-19 10:40:48 +04:00
|
|
|
return error;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
no_id:
|
2009-09-12 15:21:27 +04:00
|
|
|
if (is_file_hugepages(file) && shp->mlock_user)
|
2009-08-24 19:30:28 +04:00
|
|
|
user_shm_unlock(size, shp->mlock_user);
|
2005-04-17 02:20:36 +04:00
|
|
|
fput(file);
|
|
|
|
no_file:
|
|
|
|
security_shm_free(shp);
|
|
|
|
ipc_rcu_putref(shp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2007-10-19 10:40:53 +04:00
|
|
|
/*
|
2007-10-19 10:40:54 +04:00
|
|
|
* Called with shm_ids.rw_mutex and ipcp locked.
|
2007-10-19 10:40:53 +04:00
|
|
|
*/
|
2007-10-19 10:40:51 +04:00
|
|
|
static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
|
2007-10-19 10:40:49 +04:00
|
|
|
{
|
2007-10-19 10:40:51 +04:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
|
|
|
|
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
|
|
|
return security_shm_associate(shp, shmflg);
|
2007-10-19 10:40:49 +04:00
|
|
|
}
|
|
|
|
|
2007-10-19 10:40:53 +04:00
|
|
|
/*
|
2007-10-19 10:40:54 +04:00
|
|
|
* Called with shm_ids.rw_mutex and ipcp locked.
|
2007-10-19 10:40:53 +04:00
|
|
|
*/
|
2007-10-19 10:40:51 +04:00
|
|
|
static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
|
|
|
|
struct ipc_params *params)
|
2007-10-19 10:40:49 +04:00
|
|
|
{
|
2007-10-19 10:40:51 +04:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
|
|
|
|
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
|
|
|
if (shp->shm_segsz < params->u.size)
|
2007-10-19 10:40:49 +04:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-14 16:14:27 +03:00
|
|
|
SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2006-10-02 13:18:22 +04:00
|
|
|
struct ipc_namespace *ns;
|
2007-10-19 10:40:49 +04:00
|
|
|
struct ipc_ops shm_ops;
|
|
|
|
struct ipc_params shm_params;
|
2006-10-02 13:18:22 +04:00
|
|
|
|
|
|
|
ns = current->nsproxy->ipc_ns;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2007-10-19 10:40:49 +04:00
|
|
|
shm_ops.getnew = newseg;
|
|
|
|
shm_ops.associate = shm_security;
|
|
|
|
shm_ops.more_checks = shm_more_checks;
|
2007-10-19 10:40:48 +04:00
|
|
|
|
2007-10-19 10:40:49 +04:00
|
|
|
shm_params.key = key;
|
|
|
|
shm_params.flg = shmflg;
|
|
|
|
shm_params.u.size = size;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2007-10-19 10:40:49 +04:00
|
|
|
return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
|
|
|
|
{
|
|
|
|
switch(version) {
|
|
|
|
case IPC_64:
|
|
|
|
return copy_to_user(buf, in, sizeof(*in));
|
|
|
|
case IPC_OLD:
|
|
|
|
{
|
|
|
|
struct shmid_ds out;
|
|
|
|
|
2010-10-30 18:22:49 +04:00
|
|
|
memset(&out, 0, sizeof(out));
|
2005-04-17 02:20:36 +04:00
|
|
|
ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
|
|
|
|
out.shm_segsz = in->shm_segsz;
|
|
|
|
out.shm_atime = in->shm_atime;
|
|
|
|
out.shm_dtime = in->shm_dtime;
|
|
|
|
out.shm_ctime = in->shm_ctime;
|
|
|
|
out.shm_cpid = in->shm_cpid;
|
|
|
|
out.shm_lpid = in->shm_lpid;
|
|
|
|
out.shm_nattch = in->shm_nattch;
|
|
|
|
|
|
|
|
return copy_to_user(buf, &out, sizeof(out));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:00:50 +04:00
|
|
|
static inline unsigned long
|
|
|
|
copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
switch(version) {
|
|
|
|
case IPC_64:
|
2008-04-29 12:00:50 +04:00
|
|
|
if (copy_from_user(out, buf, sizeof(*out)))
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
case IPC_OLD:
|
|
|
|
{
|
|
|
|
struct shmid_ds tbuf_old;
|
|
|
|
|
|
|
|
if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2008-04-29 12:00:50 +04:00
|
|
|
out->shm_perm.uid = tbuf_old.shm_perm.uid;
|
|
|
|
out->shm_perm.gid = tbuf_old.shm_perm.gid;
|
|
|
|
out->shm_perm.mode = tbuf_old.shm_perm.mode;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
|
|
|
|
{
|
|
|
|
switch(version) {
|
|
|
|
case IPC_64:
|
|
|
|
return copy_to_user(buf, in, sizeof(*in));
|
|
|
|
case IPC_OLD:
|
|
|
|
{
|
|
|
|
struct shminfo out;
|
|
|
|
|
|
|
|
if(in->shmmax > INT_MAX)
|
|
|
|
out.shmmax = INT_MAX;
|
|
|
|
else
|
|
|
|
out.shmmax = (int)in->shmmax;
|
|
|
|
|
|
|
|
out.shmmin = in->shmmin;
|
|
|
|
out.shmmni = in->shmmni;
|
|
|
|
out.shmseg = in->shmseg;
|
|
|
|
out.shmall = in->shmall;
|
|
|
|
|
|
|
|
return copy_to_user(buf, &out, sizeof(out));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-28 02:34:16 +04:00
|
|
|
/*
|
|
|
|
* Calculate and add used RSS and swap pages of a shm.
|
|
|
|
* Called with shm_ids.rw_mutex held as a reader
|
|
|
|
*/
|
|
|
|
static void shm_add_rss_swap(struct shmid_kernel *shp,
|
|
|
|
unsigned long *rss_add, unsigned long *swp_add)
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
inode = shp->shm_file->f_path.dentry->d_inode;
|
|
|
|
|
|
|
|
if (is_file_hugepages(shp->shm_file)) {
|
|
|
|
struct address_space *mapping = inode->i_mapping;
|
|
|
|
struct hstate *h = hstate_file(shp->shm_file);
|
|
|
|
*rss_add += pages_per_huge_page(h) * mapping->nrpages;
|
|
|
|
} else {
|
|
|
|
#ifdef CONFIG_SHMEM
|
|
|
|
struct shmem_inode_info *info = SHMEM_I(inode);
|
|
|
|
spin_lock(&info->lock);
|
|
|
|
*rss_add += inode->i_mapping->nrpages;
|
|
|
|
*swp_add += info->swapped;
|
|
|
|
spin_unlock(&info->lock);
|
|
|
|
#else
|
|
|
|
*rss_add += inode->i_mapping->nrpages;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-19 10:40:53 +04:00
|
|
|
/*
|
2007-10-19 10:40:54 +04:00
|
|
|
* Called with shm_ids.rw_mutex held as a reader
|
2007-10-19 10:40:53 +04:00
|
|
|
*/
|
2006-10-02 13:18:22 +04:00
|
|
|
static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
|
|
|
|
unsigned long *swp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2007-10-19 10:40:48 +04:00
|
|
|
int next_id;
|
|
|
|
int total, in_use;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
*rss = 0;
|
|
|
|
*swp = 0;
|
|
|
|
|
2007-10-19 10:40:48 +04:00
|
|
|
in_use = shm_ids(ns).in_use;
|
|
|
|
|
|
|
|
for (total = 0, next_id = 0; total < in_use; next_id++) {
|
2009-04-03 03:58:26 +04:00
|
|
|
struct kern_ipc_perm *ipc;
|
2005-04-17 02:20:36 +04:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
|
2009-04-03 03:58:26 +04:00
|
|
|
ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
|
|
|
|
if (ipc == NULL)
|
2005-04-17 02:20:36 +04:00
|
|
|
continue;
|
2009-04-03 03:58:26 +04:00
|
|
|
shp = container_of(ipc, struct shmid_kernel, shm_perm);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-10-28 02:34:16 +04:00
|
|
|
shm_add_rss_swap(shp, rss, swp);
|
2007-10-19 10:40:48 +04:00
|
|
|
|
|
|
|
total++;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:00:47 +04:00
|
|
|
/*
|
|
|
|
* This function handles some shmctl commands which require the rw_mutex
|
|
|
|
* to be held in write mode.
|
|
|
|
* NOTE: no locks must be held, the rw_mutex is taken inside this function.
|
|
|
|
*/
|
|
|
|
static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
|
|
|
|
struct shmid_ds __user *buf, int version)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-04-29 12:00:47 +04:00
|
|
|
struct kern_ipc_perm *ipcp;
|
2008-04-29 12:00:50 +04:00
|
|
|
struct shmid64_ds shmid64;
|
2008-04-29 12:00:47 +04:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (cmd == IPC_SET) {
|
2008-04-29 12:00:50 +04:00
|
|
|
if (copy_shmid_from_user(&shmid64, buf, version))
|
2008-04-29 12:00:47 +04:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
2011-03-24 02:43:24 +03:00
|
|
|
ipcp = ipcctl_pre_down(ns, &shm_ids(ns), shmid, cmd,
|
|
|
|
&shmid64.shm_perm, 0);
|
2008-04-29 12:00:54 +04:00
|
|
|
if (IS_ERR(ipcp))
|
|
|
|
return PTR_ERR(ipcp);
|
2008-04-29 12:00:47 +04:00
|
|
|
|
2008-04-29 12:00:54 +04:00
|
|
|
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
2008-04-29 12:00:47 +04:00
|
|
|
|
|
|
|
err = security_shm_shmctl(shp, cmd);
|
|
|
|
if (err)
|
|
|
|
goto out_unlock;
|
|
|
|
switch (cmd) {
|
|
|
|
case IPC_RMID:
|
|
|
|
do_shm_rmid(ns, ipcp);
|
|
|
|
goto out_up;
|
|
|
|
case IPC_SET:
|
2008-04-29 12:00:51 +04:00
|
|
|
ipc_update_perm(&shmid64.shm_perm, ipcp);
|
2008-04-29 12:00:47 +04:00
|
|
|
shp->shm_ctim = get_seconds();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
|
|
|
}
|
|
|
|
out_unlock:
|
|
|
|
shm_unlock(shp);
|
|
|
|
out_up:
|
|
|
|
up_write(&shm_ids(ns).rw_mutex);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-01-14 16:14:27 +03:00
|
|
|
SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
|
2008-04-29 12:00:47 +04:00
|
|
|
{
|
2005-04-17 02:20:36 +04:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
int err, version;
|
2006-10-02 13:18:22 +04:00
|
|
|
struct ipc_namespace *ns;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (cmd < 0 || shmid < 0) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
version = ipc_parse_version(&cmd);
|
2006-10-02 13:18:22 +04:00
|
|
|
ns = current->nsproxy->ipc_ns;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
switch (cmd) { /* replace with proc interface ? */
|
|
|
|
case IPC_INFO:
|
|
|
|
{
|
|
|
|
struct shminfo64 shminfo;
|
|
|
|
|
|
|
|
err = security_shm_shmctl(NULL, cmd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2009-01-07 01:42:49 +03:00
|
|
|
memset(&shminfo, 0, sizeof(shminfo));
|
2006-10-02 13:18:22 +04:00
|
|
|
shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
|
|
|
|
shminfo.shmmax = ns->shm_ctlmax;
|
|
|
|
shminfo.shmall = ns->shm_ctlall;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
shminfo.shmmin = SHMMIN;
|
|
|
|
if(copy_shminfo_to_user (buf, &shminfo, version))
|
|
|
|
return -EFAULT;
|
2007-10-19 10:40:53 +04:00
|
|
|
|
2007-10-19 10:40:54 +04:00
|
|
|
down_read(&shm_ids(ns).rw_mutex);
|
2007-10-19 10:40:48 +04:00
|
|
|
err = ipc_get_maxid(&shm_ids(ns));
|
2007-10-19 10:40:54 +04:00
|
|
|
up_read(&shm_ids(ns).rw_mutex);
|
2007-10-19 10:40:53 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
if(err<0)
|
|
|
|
err = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
case SHM_INFO:
|
|
|
|
{
|
|
|
|
struct shm_info shm_info;
|
|
|
|
|
|
|
|
err = security_shm_shmctl(NULL, cmd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2009-01-07 01:42:49 +03:00
|
|
|
memset(&shm_info, 0, sizeof(shm_info));
|
2007-10-19 10:40:54 +04:00
|
|
|
down_read(&shm_ids(ns).rw_mutex);
|
2006-10-02 13:18:22 +04:00
|
|
|
shm_info.used_ids = shm_ids(ns).in_use;
|
|
|
|
shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
|
|
|
|
shm_info.shm_tot = ns->shm_tot;
|
2005-04-17 02:20:36 +04:00
|
|
|
shm_info.swap_attempts = 0;
|
|
|
|
shm_info.swap_successes = 0;
|
2007-10-19 10:40:48 +04:00
|
|
|
err = ipc_get_maxid(&shm_ids(ns));
|
2007-10-19 10:40:54 +04:00
|
|
|
up_read(&shm_ids(ns).rw_mutex);
|
2009-01-07 01:42:49 +03:00
|
|
|
if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
|
2005-04-17 02:20:36 +04:00
|
|
|
err = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = err < 0 ? 0 : err;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
case SHM_STAT:
|
|
|
|
case IPC_STAT:
|
|
|
|
{
|
|
|
|
struct shmid64_ds tbuf;
|
|
|
|
int result;
|
2007-10-19 10:40:51 +04:00
|
|
|
|
|
|
|
if (cmd == SHM_STAT) {
|
|
|
|
shp = shm_lock(ns, shmid);
|
|
|
|
if (IS_ERR(shp)) {
|
|
|
|
err = PTR_ERR(shp);
|
|
|
|
goto out;
|
|
|
|
}
|
2007-10-19 10:40:48 +04:00
|
|
|
result = shp->shm_perm.id;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
2007-10-19 10:40:51 +04:00
|
|
|
shp = shm_lock_check(ns, shmid);
|
|
|
|
if (IS_ERR(shp)) {
|
|
|
|
err = PTR_ERR(shp);
|
|
|
|
goto out;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
result = 0;
|
|
|
|
}
|
2009-01-07 01:42:49 +03:00
|
|
|
err = -EACCES;
|
2011-03-24 02:43:24 +03:00
|
|
|
if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out_unlock;
|
|
|
|
err = security_shm_shmctl(shp, cmd);
|
|
|
|
if (err)
|
|
|
|
goto out_unlock;
|
2007-10-19 10:40:51 +04:00
|
|
|
memset(&tbuf, 0, sizeof(tbuf));
|
2005-04-17 02:20:36 +04:00
|
|
|
kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
|
|
|
|
tbuf.shm_segsz = shp->shm_segsz;
|
|
|
|
tbuf.shm_atime = shp->shm_atim;
|
|
|
|
tbuf.shm_dtime = shp->shm_dtim;
|
|
|
|
tbuf.shm_ctime = shp->shm_ctim;
|
|
|
|
tbuf.shm_cpid = shp->shm_cprid;
|
|
|
|
tbuf.shm_lpid = shp->shm_lprid;
|
2007-02-21 00:57:53 +03:00
|
|
|
tbuf.shm_nattch = shp->shm_nattch;
|
2005-04-17 02:20:36 +04:00
|
|
|
shm_unlock(shp);
|
|
|
|
if(copy_shmid_to_user (buf, &tbuf, version))
|
|
|
|
err = -EFAULT;
|
|
|
|
else
|
|
|
|
err = result;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
case SHM_LOCK:
|
|
|
|
case SHM_UNLOCK:
|
|
|
|
{
|
2008-10-19 07:26:43 +04:00
|
|
|
struct file *uninitialized_var(shm_file);
|
|
|
|
|
|
|
|
lru_add_drain_all(); /* drain pagevecs to lru lists */
|
|
|
|
|
2007-10-19 10:40:51 +04:00
|
|
|
shp = shm_lock_check(ns, shmid);
|
|
|
|
if (IS_ERR(shp)) {
|
|
|
|
err = PTR_ERR(shp);
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-12-10 11:40:06 +03:00
|
|
|
audit_ipc_obj(&(shp->shm_perm));
|
2006-04-03 01:07:33 +04:00
|
|
|
|
2011-03-24 02:43:24 +03:00
|
|
|
if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
|
2008-11-14 02:39:06 +03:00
|
|
|
uid_t euid = current_euid();
|
2005-04-17 02:20:36 +04:00
|
|
|
err = -EPERM;
|
2008-11-14 02:39:06 +03:00
|
|
|
if (euid != shp->shm_perm.uid &&
|
|
|
|
euid != shp->shm_perm.cuid)
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out_unlock;
|
2010-03-11 02:23:05 +03:00
|
|
|
if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK))
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = security_shm_shmctl(shp, cmd);
|
|
|
|
if (err)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
if(cmd==SHM_LOCK) {
|
2008-11-14 02:39:18 +03:00
|
|
|
struct user_struct *user = current_user();
|
2005-04-17 02:20:36 +04:00
|
|
|
if (!is_file_hugepages(shp->shm_file)) {
|
|
|
|
err = shmem_lock(shp->shm_file, 1, user);
|
Fix user struct leakage with locked IPC shem segment
When user locks an ipc shmem segmant with SHM_LOCK ctl and the segment is
already locked the shmem_lock() function returns 0. After this the
subsequent code leaks the existing user struct:
== ipc/shm.c: sys_shmctl() ==
...
err = shmem_lock(shp->shm_file, 1, user);
if (!err) {
shp->shm_perm.mode |= SHM_LOCKED;
shp->mlock_user = user;
}
...
==
Other results of this are:
1. the new shp->mlock_user is not get-ed and will point to freed
memory when the task dies.
2. the RLIMIT_MEMLOCK is screwed on both user structs.
The exploit looks like this:
==
id = shmget(...);
setresuid(uid, 0, 0);
shmctl(id, SHM_LOCK, NULL);
setresuid(uid + 1, 0, 0);
shmctl(id, SHM_LOCK, NULL);
==
My solution is to return 0 to the userspace and do not change the
segment's user.
Signed-off-by: Pavel Emelianov <xemul@openvz.org>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-31 11:38:48 +04:00
|
|
|
if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
|
2006-01-08 12:02:21 +03:00
|
|
|
shp->shm_perm.mode |= SHM_LOCKED;
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->mlock_user = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!is_file_hugepages(shp->shm_file)) {
|
|
|
|
shmem_lock(shp->shm_file, 0, shp->mlock_user);
|
2006-01-08 12:02:21 +03:00
|
|
|
shp->shm_perm.mode &= ~SHM_LOCKED;
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->mlock_user = NULL;
|
|
|
|
}
|
|
|
|
shm_unlock(shp);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
case IPC_RMID:
|
|
|
|
case IPC_SET:
|
2008-04-29 12:00:47 +04:00
|
|
|
err = shmctl_down(ns, shmid, cmd, buf, version);
|
|
|
|
return err;
|
2005-04-17 02:20:36 +04:00
|
|
|
default:
|
2008-04-29 12:00:47 +04:00
|
|
|
return -EINVAL;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
shm_unlock(shp);
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
|
|
|
|
*
|
|
|
|
* NOTE! Despite the name, this is NOT a direct system call entrypoint. The
|
|
|
|
* "raddr" thing points to kernel space, and there has to be a wrapper around
|
|
|
|
* this.
|
|
|
|
*/
|
|
|
|
long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
|
|
|
|
{
|
|
|
|
struct shmid_kernel *shp;
|
|
|
|
unsigned long addr;
|
|
|
|
unsigned long size;
|
|
|
|
struct file * file;
|
|
|
|
int err;
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long prot;
|
|
|
|
int acc_mode;
|
2007-02-21 00:57:53 +03:00
|
|
|
unsigned long user_addr;
|
2006-10-02 13:18:22 +04:00
|
|
|
struct ipc_namespace *ns;
|
2007-02-21 00:57:53 +03:00
|
|
|
struct shm_file_data *sfd;
|
|
|
|
struct path path;
|
2008-09-02 23:28:45 +04:00
|
|
|
fmode_t f_mode;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
err = -EINVAL;
|
|
|
|
if (shmid < 0)
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out;
|
2007-02-21 00:57:53 +03:00
|
|
|
else if ((addr = (ulong)shmaddr)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
if (addr & (SHMLBA-1)) {
|
|
|
|
if (shmflg & SHM_RND)
|
|
|
|
addr &= ~(SHMLBA-1); /* round down */
|
|
|
|
else
|
|
|
|
#ifndef __ARCH_FORCE_SHMLBA
|
|
|
|
if (addr & ~PAGE_MASK)
|
|
|
|
#endif
|
2007-02-21 00:57:53 +03:00
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
flags = MAP_SHARED | MAP_FIXED;
|
|
|
|
} else {
|
|
|
|
if ((shmflg & SHM_REMAP))
|
2007-02-21 00:57:53 +03:00
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
flags = MAP_SHARED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shmflg & SHM_RDONLY) {
|
|
|
|
prot = PROT_READ;
|
|
|
|
acc_mode = S_IRUGO;
|
2007-02-21 00:57:53 +03:00
|
|
|
f_mode = FMODE_READ;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
|
|
|
acc_mode = S_IRUGO | S_IWUGO;
|
2007-02-21 00:57:53 +03:00
|
|
|
f_mode = FMODE_READ | FMODE_WRITE;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
if (shmflg & SHM_EXEC) {
|
|
|
|
prot |= PROT_EXEC;
|
|
|
|
acc_mode |= S_IXUGO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We cannot rely on the fs check since SYSV IPC does have an
|
|
|
|
* additional creator id...
|
|
|
|
*/
|
2006-10-02 13:18:22 +04:00
|
|
|
ns = current->nsproxy->ipc_ns;
|
2007-10-19 10:40:51 +04:00
|
|
|
shp = shm_lock_check(ns, shmid);
|
|
|
|
if (IS_ERR(shp)) {
|
|
|
|
err = PTR_ERR(shp);
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out;
|
2007-10-19 10:40:51 +04:00
|
|
|
}
|
2007-02-21 00:57:53 +03:00
|
|
|
|
|
|
|
err = -EACCES;
|
2011-03-24 02:43:24 +03:00
|
|
|
if (ipcperms(ns, &shp->shm_perm, acc_mode))
|
2007-02-21 00:57:53 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
err = security_shm_shmat(shp, shmaddr, shmflg);
|
2007-02-21 00:57:53 +03:00
|
|
|
if (err)
|
|
|
|
goto out_unlock;
|
|
|
|
|
2009-08-09 00:52:35 +04:00
|
|
|
path = shp->shm_file->f_path;
|
|
|
|
path_get(&path);
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->shm_nattch++;
|
2007-02-21 00:57:53 +03:00
|
|
|
size = i_size_read(path.dentry->d_inode);
|
2005-04-17 02:20:36 +04:00
|
|
|
shm_unlock(shp);
|
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
err = -ENOMEM;
|
|
|
|
sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
|
|
|
|
if (!sfd)
|
2007-10-17 10:31:13 +04:00
|
|
|
goto out_put_dentry;
|
2007-02-21 00:57:53 +03:00
|
|
|
|
2009-08-09 00:52:35 +04:00
|
|
|
file = alloc_file(&path, f_mode,
|
|
|
|
is_file_hugepages(shp->shm_file) ?
|
2009-11-30 16:38:43 +03:00
|
|
|
&shm_file_operations_huge :
|
|
|
|
&shm_file_operations);
|
2007-02-21 00:57:53 +03:00
|
|
|
if (!file)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
file->private_data = sfd;
|
|
|
|
file->f_mapping = shp->shm_file->f_mapping;
|
2007-10-19 10:40:48 +04:00
|
|
|
sfd->id = shp->shm_perm.id;
|
2007-02-21 00:57:53 +03:00
|
|
|
sfd->ns = get_ipc_ns(ns);
|
|
|
|
sfd->file = shp->shm_file;
|
|
|
|
sfd->vm_ops = NULL;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
down_write(¤t->mm->mmap_sem);
|
|
|
|
if (addr && !(shmflg & SHM_REMAP)) {
|
2007-02-21 00:57:53 +03:00
|
|
|
err = -EINVAL;
|
2005-04-17 02:20:36 +04:00
|
|
|
if (find_vma_intersection(current->mm, addr, addr + size))
|
|
|
|
goto invalid;
|
|
|
|
/*
|
|
|
|
* If shm segment goes below stack, make sure there is some
|
|
|
|
* space left for the stack to grow (at least 4 pages).
|
|
|
|
*/
|
|
|
|
if (addr < current->mm->start_stack &&
|
|
|
|
addr > current->mm->start_stack - size - PAGE_SIZE * 5)
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
user_addr = do_mmap (file, addr, size, prot, flags, 0);
|
|
|
|
*raddr = user_addr;
|
|
|
|
err = 0;
|
|
|
|
if (IS_ERR_VALUE(user_addr))
|
|
|
|
err = (long)user_addr;
|
2005-04-17 02:20:36 +04:00
|
|
|
invalid:
|
|
|
|
up_write(¤t->mm->mmap_sem);
|
|
|
|
|
2007-02-21 00:57:53 +03:00
|
|
|
fput(file);
|
|
|
|
|
|
|
|
out_nattch:
|
2007-10-19 10:40:54 +04:00
|
|
|
down_write(&shm_ids(ns).rw_mutex);
|
2008-07-25 12:48:03 +04:00
|
|
|
shp = shm_lock(ns, shmid);
|
2007-10-19 10:40:51 +04:00
|
|
|
BUG_ON(IS_ERR(shp));
|
2005-04-17 02:20:36 +04:00
|
|
|
shp->shm_nattch--;
|
|
|
|
if(shp->shm_nattch == 0 &&
|
2006-01-08 12:02:21 +03:00
|
|
|
shp->shm_perm.mode & SHM_DEST)
|
2006-10-02 13:18:22 +04:00
|
|
|
shm_destroy(ns, shp);
|
2005-04-17 02:20:36 +04:00
|
|
|
else
|
|
|
|
shm_unlock(shp);
|
2007-10-19 10:40:54 +04:00
|
|
|
up_write(&shm_ids(ns).rw_mutex);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
out:
|
|
|
|
return err;
|
2007-02-21 00:57:53 +03:00
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
shm_unlock(shp);
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree(sfd);
|
2007-10-17 10:31:13 +04:00
|
|
|
out_put_dentry:
|
2009-08-09 00:52:35 +04:00
|
|
|
path_put(&path);
|
2007-02-21 00:57:53 +03:00
|
|
|
goto out_nattch;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2009-01-14 16:14:27 +03:00
|
|
|
SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
|
2005-05-01 19:59:12 +04:00
|
|
|
{
|
|
|
|
unsigned long ret;
|
|
|
|
long err;
|
|
|
|
|
|
|
|
err = do_shmat(shmid, shmaddr, shmflg, &ret);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
force_successful_syscall_return();
|
|
|
|
return (long)ret;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* detach and kill segment if marked destroyed.
|
|
|
|
* The work is done in shm_close.
|
|
|
|
*/
|
2009-01-14 16:14:27 +03:00
|
|
|
SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct mm_struct *mm = current->mm;
|
2009-06-10 03:26:23 +04:00
|
|
|
struct vm_area_struct *vma;
|
2005-04-17 02:20:36 +04:00
|
|
|
unsigned long addr = (unsigned long)shmaddr;
|
|
|
|
int retval = -EINVAL;
|
2009-06-10 03:26:23 +04:00
|
|
|
#ifdef CONFIG_MMU
|
|
|
|
loff_t size = 0;
|
|
|
|
struct vm_area_struct *next;
|
|
|
|
#endif
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2006-03-24 14:18:06 +03:00
|
|
|
if (addr & ~PAGE_MASK)
|
|
|
|
return retval;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
down_write(&mm->mmap_sem);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function tries to be smart and unmap shm segments that
|
|
|
|
* were modified by partial mlock or munmap calls:
|
|
|
|
* - It first determines the size of the shm segment that should be
|
|
|
|
* unmapped: It searches for a vma that is backed by shm and that
|
|
|
|
* started at address shmaddr. It records it's size and then unmaps
|
|
|
|
* it.
|
|
|
|
* - Then it unmaps all shm vmas that started at shmaddr and that
|
|
|
|
* are within the initially determined size.
|
|
|
|
* Errors from do_munmap are ignored: the function only fails if
|
|
|
|
* it's called with invalid parameters or if it's called to unmap
|
|
|
|
* a part of a vma. Both calls in this function are for full vmas,
|
|
|
|
* the parameters are directly copied from the vma itself and always
|
|
|
|
* valid - therefore do_munmap cannot fail. (famous last words?)
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* If it had been mremap()'d, the starting address would not
|
|
|
|
* match the usual checks anyway. So assume all vma's are
|
|
|
|
* above the starting address given.
|
|
|
|
*/
|
|
|
|
vma = find_vma(mm, addr);
|
|
|
|
|
2009-01-08 15:04:47 +03:00
|
|
|
#ifdef CONFIG_MMU
|
2005-04-17 02:20:36 +04:00
|
|
|
while (vma) {
|
|
|
|
next = vma->vm_next;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the starting address would match, i.e. it's
|
|
|
|
* a fragment created by mprotect() and/or munmap(), or it
|
|
|
|
* otherwise it starts at this address with no hassles.
|
|
|
|
*/
|
2007-02-21 00:57:53 +03:00
|
|
|
if ((vma->vm_ops == &shm_vm_ops) &&
|
2005-04-17 02:20:36 +04:00
|
|
|
(vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
|
|
|
|
|
|
|
|
|
2006-12-08 13:37:11 +03:00
|
|
|
size = vma->vm_file->f_path.dentry->d_inode->i_size;
|
2005-04-17 02:20:36 +04:00
|
|
|
do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
|
|
|
|
/*
|
|
|
|
* We discovered the size of the shm segment, so
|
|
|
|
* break out of here and fall through to the next
|
|
|
|
* loop that uses the size information to stop
|
|
|
|
* searching for matching vma's.
|
|
|
|
*/
|
|
|
|
retval = 0;
|
|
|
|
vma = next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vma = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need look no further than the maximum address a fragment
|
|
|
|
* could possibly have landed at. Also cast things to loff_t to
|
2011-03-31 05:57:33 +04:00
|
|
|
* prevent overflows and make comparisons vs. equal-width types.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2006-02-10 12:51:12 +03:00
|
|
|
size = PAGE_ALIGN(size);
|
2005-04-17 02:20:36 +04:00
|
|
|
while (vma && (loff_t)(vma->vm_end - addr) <= size) {
|
|
|
|
next = vma->vm_next;
|
|
|
|
|
|
|
|
/* finding a matching vma now does not alter retval */
|
2007-02-21 00:57:53 +03:00
|
|
|
if ((vma->vm_ops == &shm_vm_ops) &&
|
2005-04-17 02:20:36 +04:00
|
|
|
(vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
|
|
|
|
|
|
|
|
do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
|
|
|
|
vma = next;
|
|
|
|
}
|
|
|
|
|
2009-01-08 15:04:47 +03:00
|
|
|
#else /* CONFIG_MMU */
|
|
|
|
/* under NOMMU conditions, the exact address to be destroyed must be
|
|
|
|
* given */
|
|
|
|
retval = -EINVAL;
|
|
|
|
if (vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
|
|
|
|
do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
up_write(&mm->mmap_sem);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2005-09-07 02:17:10 +04:00
|
|
|
static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2005-09-07 02:17:10 +04:00
|
|
|
struct shmid_kernel *shp = it;
|
2010-10-28 02:34:16 +04:00
|
|
|
unsigned long rss = 0, swp = 0;
|
|
|
|
|
|
|
|
shm_add_rss_swap(shp, &rss, &swp);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-06-13 02:21:49 +04:00
|
|
|
#if BITS_PER_LONG <= 32
|
|
|
|
#define SIZE_SPEC "%10lu"
|
|
|
|
#else
|
|
|
|
#define SIZE_SPEC "%21lu"
|
|
|
|
#endif
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-06-13 02:21:49 +04:00
|
|
|
return seq_printf(s,
|
|
|
|
"%10d %10d %4o " SIZE_SPEC " %5u %5u "
|
2010-10-28 02:34:16 +04:00
|
|
|
"%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
|
|
|
|
SIZE_SPEC " " SIZE_SPEC "\n",
|
2005-09-07 02:17:10 +04:00
|
|
|
shp->shm_perm.key,
|
2007-10-19 10:40:48 +04:00
|
|
|
shp->shm_perm.id,
|
2006-01-08 12:02:21 +03:00
|
|
|
shp->shm_perm.mode,
|
2005-09-07 02:17:10 +04:00
|
|
|
shp->shm_segsz,
|
|
|
|
shp->shm_cprid,
|
|
|
|
shp->shm_lprid,
|
2007-02-21 00:57:53 +03:00
|
|
|
shp->shm_nattch,
|
2005-09-07 02:17:10 +04:00
|
|
|
shp->shm_perm.uid,
|
|
|
|
shp->shm_perm.gid,
|
|
|
|
shp->shm_perm.cuid,
|
|
|
|
shp->shm_perm.cgid,
|
|
|
|
shp->shm_atim,
|
|
|
|
shp->shm_dtim,
|
2010-10-28 02:34:16 +04:00
|
|
|
shp->shm_ctim,
|
|
|
|
rss * PAGE_SIZE,
|
|
|
|
swp * PAGE_SIZE);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
#endif
|