Linux-Fsdevel Archive on lore.kernel.org
 help / color / Atom feed
From: "Eric W. Biederman" <ebiederm@xmission.com>
To: Al Viro <viro@ZenIV.linux.org.uk>
Cc: <linux-kernel@vger.kernel.org>, <linux-pci@vger.kernel.org>,
	<linux-mm@kvack.org>, <linux-fsdevel@vger.kernel.org>,
	Hugh Dickins <hugh@veritas.com>, Tejun Heo <tj@kernel.org>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Greg Kroah-Hartman <gregkh@suse.de>,
	Nick Piggin <npiggin@suse.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	Christoph Hellwig <hch@infradead.org>,
	"Eric W. Biederman" <ebiederm@maxwell.arastra.com>,
	"Eric W. Biederman" <ebiederm@aristanetworks.com>
Subject: [PATCH 11/23] mm: Teach mmap to use file_hotplug_lock
Date: Mon,  1 Jun 2009 14:50:36 -0700
Message-ID: <1243893048-17031-11-git-send-email-ebiederm@xmission.com> (raw)
In-Reply-To: <m1oct739xu.fsf@fess.ebiederm.org>

From: Eric W. Biederman <ebiederm@maxwell.arastra.com>

Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
---
 mm/mmap.c  |   78 +++++++++++++++++++++++++++++++++++++++--------------------
 mm/nommu.c |   21 +++++++++++++++-
 2 files changed, 71 insertions(+), 28 deletions(-)

diff --git a/mm/mmap.c b/mm/mmap.c
index 6b7b1a9..f13251a 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -914,9 +914,13 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 	struct mm_struct * mm = current->mm;
 	struct inode *inode;
 	unsigned int vm_flags;
-	int error;
+	unsigned long retval;
 	unsigned long reqprot = prot;
 
+	retval = -EIO;
+	if (file && !file_hotplug_read_trylock(file))
+		goto out;
+
 	/*
 	 * Does the application expect PROT_READ to imply PROT_EXEC?
 	 *
@@ -927,35 +931,40 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 		if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
 			prot |= PROT_EXEC;
 
+	retval = -EINVAL;
 	if (!len)
-		return -EINVAL;
+		goto out_unlock;
 
 	if (!(flags & MAP_FIXED))
 		addr = round_hint_to_min(addr);
 
-	error = arch_mmap_check(addr, len, flags);
-	if (error)
-		return error;
+	retval = arch_mmap_check(addr, len, flags);
+	if (retval)
+		goto out_unlock;
 
 	/* Careful about overflows.. */
+	retval = -ENOMEM;
 	len = PAGE_ALIGN(len);
 	if (!len || len > TASK_SIZE)
-		return -ENOMEM;
+		goto out_unlock;
 
 	/* offset overflow? */
+	retval = -EOVERFLOW;
 	if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
-               return -EOVERFLOW;
+		goto out_unlock;
 
 	/* Too many mappings? */
+	retval = -ENOMEM;
 	if (mm->map_count > sysctl_max_map_count)
-		return -ENOMEM;
+		goto out_unlock;
 
 	/* Obtain the address to map to. we verify (or select) it and ensure
 	 * that it represents a valid section of the address space.
 	 */
 	addr = get_unmapped_area(file, addr, len, pgoff, flags);
+	retval = addr;
 	if (addr & ~PAGE_MASK)
-		return addr;
+		goto out_unlock;
 
 	/* Do simple checking here so the lower-level routines won't have
 	 * to. we assume access permissions have been handled by the open
@@ -965,8 +974,9 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 			mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
 
 	if (flags & MAP_LOCKED) {
+		retval = -EPERM;
 		if (!can_do_mlock())
-			return -EPERM;
+			goto out_unlock;
 		vm_flags |= VM_LOCKED;
 	}
 
@@ -977,8 +987,9 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 		locked += mm->locked_vm;
 		lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
 		lock_limit >>= PAGE_SHIFT;
+		retval = -EAGAIN;
 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
-			return -EAGAIN;
+			goto out_unlock;
 	}
 
 	inode = file ? file->f_path.dentry->d_inode : NULL;
@@ -986,21 +997,24 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 	if (file) {
 		switch (flags & MAP_TYPE) {
 		case MAP_SHARED:
+			retval = -EACCES;
 			if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
-				return -EACCES;
+				goto out_unlock;
 
 			/*
 			 * Make sure we don't allow writing to an append-only
 			 * file..
 			 */
+			retval = -EACCES;
 			if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
-				return -EACCES;
+				goto out_unlock;
 
 			/*
 			 * Make sure there are no mandatory locks on the file.
 			 */
+			retval = -EAGAIN;
 			if (locks_verify_locked(inode))
-				return -EAGAIN;
+				goto out_unlock;
 
 			vm_flags |= VM_SHARED | VM_MAYSHARE;
 			if (!(file->f_mode & FMODE_WRITE))
@@ -1008,20 +1022,24 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 
 			/* fall through */
 		case MAP_PRIVATE:
+			retval = -EACCES;
 			if (!(file->f_mode & FMODE_READ))
-				return -EACCES;
+				goto out_unlock;
 			if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
+				retval = -EPERM;
 				if (vm_flags & VM_EXEC)
-					return -EPERM;
+					goto out_unlock;
 				vm_flags &= ~VM_MAYEXEC;
 			}
 
+			retval = -ENODEV;
 			if (!file->f_op || !file->f_op->mmap)
-				return -ENODEV;
+				goto out_unlock;
 			break;
 
 		default:
-			return -EINVAL;
+			retval = -EINVAL;
+			goto out_unlock;
 		}
 	} else {
 		switch (flags & MAP_TYPE) {
@@ -1039,18 +1057,24 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 			pgoff = addr >> PAGE_SHIFT;
 			break;
 		default:
-			return -EINVAL;
+			retval = -EINVAL;
+			goto out_unlock;
 		}
 	}
 
-	error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
-	if (error)
-		return error;
-	error = ima_file_mmap(file, prot);
-	if (error)
-		return error;
+	retval = security_file_mmap(file, reqprot, prot, flags, addr, 0);
+	if (retval)
+		goto out_unlock;
+	retval = ima_file_mmap(file, prot);
+	if (retval)
+		goto out_unlock;
+	retval = mmap_region(file, addr, len, flags, vm_flags, pgoff);
 
-	return mmap_region(file, addr, len, flags, vm_flags, pgoff);
+out_unlock:
+	if (file)
+		file_hotplug_read_unlock(file);
+out:
+	return retval;
 }
 EXPORT_SYMBOL(do_mmap_pgoff);
 
diff --git a/mm/nommu.c b/mm/nommu.c
index b571ef7..08038b7 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1165,7 +1165,7 @@ enomem:
 /*
  * handle mapping creation for uClinux
  */
-unsigned long do_mmap_pgoff(struct file *file,
+static unsigned long __do_mmap_pgoff(struct file *file,
 			    unsigned long addr,
 			    unsigned long len,
 			    unsigned long prot,
@@ -1402,6 +1402,25 @@ error_getting_region:
 	show_free_areas();
 	return -ENOMEM;
 }
+
+unsigned long do_mmap_pgoff(struct file *file,
+			    unsigned long addr,
+			    unsigned long len,
+			    unsigned long prot,
+			    unsigned long flags,
+			    unsigned long pgoff)
+{
+	unsigned long result = -EIO;
+	if (file && !file_hotplug_read_trylock(file))
+		goto out;
+
+	result = __do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
+
+	if (file)
+		file_hotplug_read_unlock(file);
+out:
+	return result;
+}
 EXPORT_SYMBOL(do_mmap_pgoff);
 
 /*
-- 
1.6.3.1.54.g99dd.dirty

  parent reply index

Thread overview: 99+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-11 12:01 [RFC][PATCH 0/9] File descriptor hot-unplug support Eric W. Biederman
2009-04-11 12:03 ` [RFC][PATCH 1/9] mm: Introduce remap_file_mappings Eric W. Biederman
2009-04-11 12:05 ` [RFC][PATCH 2/9] mm: Implement generic support for revoking a mapping Eric W. Biederman
2009-04-11 12:06 ` [RFC][PATCH 3/9] sysfs: Use remap_file_mappings Eric W. Biederman
2009-04-11 12:07 ` [RFC][PATCH 4/9] vfs: Generalize the file_list Eric W. Biederman
2009-04-11 12:08 ` [RFC][PATCH 5/9] vfs: Introduce basic infrastructure for revoking a file Eric W. Biederman
2009-04-14 22:12   ` Jonathan Corbet
2009-04-15  2:55     ` Eric W. Biederman
2009-04-11 12:10 ` [RFC][PATCH 6/9] vfs: Utilize fops_read_lock where appropriate Eric W. Biederman
2009-04-11 12:11 ` [RFC][PATCH 7/9] vfs: Optimize fops_read_lock Eric W. Biederman
2009-04-11 12:13 ` [RFC][PATCH 8/9] vfs: Implement generic revoked file operations Eric W. Biederman
2009-04-12 18:56   ` Jamie Lokier
2009-04-12 20:04     ` Eric W. Biederman
2009-04-12 20:31       ` Jamie Lokier
2009-04-12 21:53         ` Eric W. Biederman
2009-04-12 20:54       ` Eric W. Biederman
2009-04-12 21:02         ` Jamie Lokier
2009-04-12 23:06           ` Eric W. Biederman
2009-04-11 12:14 ` [RFC][PATCH 9/9] proc: Use the generic vfs revoke facility that now exists Eric W. Biederman
2009-04-11 15:58 ` [RFC][PATCH 0/9] File descriptor hot-unplug support Al Viro
2009-04-11 16:49   ` Eric W. Biederman
2009-04-11 16:56     ` Al Viro
2009-04-11 23:57       ` Eric W. Biederman
2009-04-12 20:21       ` Eric W. Biederman
2009-04-14  3:16 ` Tejun Heo
2009-04-14  7:39   ` Eric W. Biederman
2009-04-14  7:45     ` Tejun Heo
2009-04-14  8:27       ` Eric W. Biederman
2009-04-14  8:49         ` Tejun Heo
2009-04-14 15:07         ` Jamie Lokier
2009-04-14 19:09           ` Eric W. Biederman
2009-06-01 21:45 ` [PATCH 0/23] File descriptor hot-unplug support v2 Eric W. Biederman
2009-06-01 21:50   ` [PATCH 01/23] mm: Introduce revoke_file_mappings Eric W. Biederman
2009-06-01 22:25     ` Andrew Morton
2009-06-02  0:12       ` Eric W. Biederman
2009-06-01 21:50   ` [PATCH 02/23] vfs: Implement unpoll_file Eric W. Biederman
2009-06-06  8:08     ` Al Viro
2009-06-01 21:50   ` [PATCH 03/23] vfs: Generalize the file_list Eric W. Biederman
2009-06-02  7:06     ` Nick Piggin
2009-06-05 19:33       ` Eric W. Biederman
2009-06-09 10:38         ` Nick Piggin
2009-06-09 18:38           ` Eric W. Biederman
2009-06-10  6:05             ` Nick Piggin
2009-06-01 21:50   ` [PATCH 04/23] vfs: Introduce infrastructure for revoking a file Eric W. Biederman
2009-06-02  5:16     ` Pekka Enberg
2009-06-02  6:51       ` Eric W. Biederman
2009-06-02  7:08         ` Pekka Enberg
2009-06-02  7:14     ` Nick Piggin
2009-06-02 17:06       ` Linus Torvalds
2009-06-02 20:52         ` Eric W. Biederman
2009-06-03  6:37           ` Nick Piggin
2009-06-02 22:56       ` Eric W. Biederman
2009-06-03  6:38         ` Nick Piggin
2009-06-05  9:03     ` Miklos Szeredi
2009-06-05 19:06       ` Eric W. Biederman
2009-06-01 21:50   ` [PATCH 05/23] vfs: Teach lseek to use file_hotplug_lock Eric W. Biederman
2009-06-01 21:50   ` [PATCH 06/23] vfs: Teach read/write to use file_hotplug_read_lock Eric W. Biederman
2009-06-01 21:50   ` [PATCH 07/23] vfs: Teach sendfile,splice,tee,and vmsplice to use file_hotplug_lock Eric W. Biederman
2009-06-03 23:39     ` Badari Pulavarty
2009-06-05 19:37       ` Eric W. Biederman
2009-06-01 21:50   ` [PATCH 08/23] vfs: Teach readdir " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 09/23] vfs: Teach poll and select " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 10/23] vfs: Teach do_path_lookup " Eric W. Biederman
2009-06-01 21:50   ` Eric W. Biederman [this message]
2009-06-01 21:50   ` [PATCH 12/23] vfs: Teach fcntl " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 13/23] vfs: Teach ioctl " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 14/23] vfs: Teach flock " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 15/23] vfs: Teach fallocate, and filp_close " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 16/23] vfs: Teach fstatfs, fstatfs64, ftruncate, fchdir, fchmod, fchown " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 17/23] proc: Teach /proc/<pid>/fd " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 18/23] vfs: Teach epoll " Eric W. Biederman
2009-06-02 16:51     ` Davide Libenzi
2009-06-02 21:23       ` Eric W. Biederman
2009-06-02 21:52         ` Davide Libenzi
2009-06-02 22:51           ` Eric W. Biederman
2009-06-03 14:57             ` Davide Libenzi
2009-06-03 20:53               ` Eric W. Biederman
2009-06-04  0:50                 ` Davide Libenzi
2009-06-04  1:42                   ` Eric W. Biederman
2009-06-01 21:50   ` [PATCH 19/23] eventpoll: Fix comment Eric W. Biederman
2009-06-01 21:50   ` [PATCH 20/23] vfs: Teach aio to use file_hotplug_lock Eric W. Biederman
2009-06-01 21:50   ` [PATCH 21/23] vfs: Teach fsync " Eric W. Biederman
2009-06-01 21:50   ` [PATCH 22/23] vfs: Teach fadvice to file_hotplug_lock Eric W. Biederman
2009-06-01 21:50   ` [PATCH 23/23] vfs: Teach readahead to use the file_hotplug_lock Eric W. Biederman
2009-06-03 23:25     ` Badari Pulavarty
2009-06-06  8:03   ` [PATCH 0/23] File descriptor hot-unplug support v2 Al Viro
2009-06-08  9:41     ` Miklos Szeredi
2009-06-08 10:24       ` Jamie Lokier
2009-06-08 16:29       ` Al Viro
2009-06-08 16:44         ` Miklos Szeredi
2009-06-08 17:50           ` Al Viro
2009-06-08 18:01             ` Linus Torvalds
2009-06-08 18:50               ` Al Viro
2009-06-08 19:18                 ` Linus Torvalds
2009-06-09  6:42                   ` Eric W. Biederman
2009-06-09 10:52                     ` Nick Piggin
2009-06-09  5:50             ` Miklos Szeredi
2009-06-09  6:31               ` Eric W. Biederman
2009-06-09  6:22     ` Eric W. Biederman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1243893048-17031-11-git-send-email-ebiederm@xmission.com \
    --to=ebiederm@xmission.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=ebiederm@aristanetworks.com \
    --cc=ebiederm@maxwell.arastra.com \
    --cc=gregkh@suse.de \
    --cc=hch@infradead.org \
    --cc=hugh@veritas.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=npiggin@suse.de \
    --cc=tj@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@ZenIV.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Fsdevel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-fsdevel/0 linux-fsdevel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-fsdevel linux-fsdevel/ https://lore.kernel.org/linux-fsdevel \
		linux-fsdevel@vger.kernel.org
	public-inbox-index linux-fsdevel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-fsdevel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git