From: Wanlong Gao <gaowanlong@cn.fujitsu.com>
To: linux-kernel@vger.kernel.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>,
"open list:MEMORY MANAGEMENT" <linux-mm@kvack.org>,
Wanlong Gao <gaowanlong@cn.fujitsu.com>
Subject: [PATCH] mm: fix mmap overflow checking
Date: Tue, 4 Sep 2012 17:23:00 +0800 [thread overview]
Message-ID: <1346750580-11352-1-git-send-email-gaowanlong@cn.fujitsu.com> (raw)
POSIX said that if the file is a regular file and the value of "off"
plus "len" exceeds the offset maximum established in the open file
description associated with fildes, mmap should return EOVERFLOW.
The following test from LTP can reproduce this bug.
char tmpfname[256];
void *pa = NULL;
void *addr = NULL;
size_t len;
int flag;
int fd;
off_t off = 0;
int prot;
long page_size = sysconf(_SC_PAGE_SIZE);
snprintf(tmpfname, sizeof(tmpfname), "/tmp/mmap_test_%d", getpid());
unlink(tmpfname);
fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
if (fd == -1) {
printf(" Error at open(): %s\n", strerror(errno));
return 1;
}
unlink(tmpfname);
flag = MAP_SHARED;
prot = PROT_READ | PROT_WRITE;
/* len + off > maximum offset */
len = ULONG_MAX;
if (len % page_size) {
/* Lower boundary */
len &= ~(page_size - 1);
}
off = ULONG_MAX;
if (off % page_size) {
/* Lower boundary */
off &= ~(page_size - 1);
}
printf("off: %lx, len: %lx\n", (unsigned long)off, (unsigned long)len);
pa = mmap(addr, len, prot, flag, fd, off);
if (pa == MAP_FAILED && errno == EOVERFLOW) {
printf("Test Pass: Error at mmap: %s\n", strerror(errno));
return 0;
}
if (pa == MAP_FAILED)
perror("Test FAIL: expect EOVERFLOW but get other error");
else
printf("Test FAIL : Expect EOVERFLOW but got no error\n");
close(fd);
munmap(pa, len);
return 1;
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: linux-mm@kvack.org (open list:MEMORY MANAGEMENT)
Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
---
mm/mmap.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/mm/mmap.c b/mm/mmap.c
index ae18a48..5380764 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -980,6 +980,7 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
struct mm_struct * mm = current->mm;
struct inode *inode;
vm_flags_t vm_flags;
+ off_t off = pgoff << PAGE_SHIFT;
/*
* Does the application expect PROT_READ to imply PROT_EXEC?
@@ -1003,8 +1004,8 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
return -ENOMEM;
/* offset overflow? */
- if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
- return -EOVERFLOW;
+ if (off + len < off)
+ return -EOVERFLOW;
/* Too many mappings? */
if (mm->map_count > sysctl_max_map_count)
--
1.7.12
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next reply other threads:[~2012-09-04 9:25 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-04 9:23 Wanlong Gao [this message]
2012-09-04 20:59 ` [PATCH] mm: fix mmap overflow checking Andrew Morton
2012-09-05 3:20 ` Wanlong Gao
2012-09-05 20:41 ` Andrew Morton
2012-09-07 22:38 ` KOSAKI Motohiro
2012-09-08 1:44 ` Wanlong Gao
2012-09-08 1:58 ` KOSAKI Motohiro
2012-09-08 2:07 ` Wanlong Gao
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=1346750580-11352-1-git-send-email-gaowanlong@cn.fujitsu.com \
--to=gaowanlong@cn.fujitsu.com \
--cc=akpm@linux-foundation.org \
--cc=kosaki.motohiro@jp.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).