summaryrefslogtreecommitdiffstats
path: root/mm/mmap.c
diff options
context:
space:
mode:
authorDan Williams <dan.j.williams@intel.com>2017-11-01 11:36:30 -0400
committerDan Williams <dan.j.williams@intel.com>2017-11-03 09:26:22 -0400
commit1c9725974074a047f6080eecc62c50a8e840d050 (patch)
treeaabeb6444ed1294ed3cf12ce4b34c03abdf2452f /mm/mmap.c
parent592e254502041f953e84d091eae2c68cba04c10b (diff)
mm: introduce MAP_SHARED_VALIDATE, a mechanism to safely define new mmap flags
The mmap(2) syscall suffers from the ABI anti-pattern of not validating unknown flags. However, proposals like MAP_SYNC need a mechanism to define new behavior that is known to fail on older kernels without the support. Define a new MAP_SHARED_VALIDATE flag pattern that is guaranteed to fail on all legacy mmap implementations. It is worth noting that the original proposal was for a standalone MAP_VALIDATE flag. However, when that could not be supported by all archs Linus observed: I see why you *think* you want a bitmap. You think you want a bitmap because you want to make MAP_VALIDATE be part of MAP_SYNC etc, so that people can do ret = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_SYNC, fd, 0); and "know" that MAP_SYNC actually takes. And I'm saying that whole wish is bogus. You're fundamentally depending on special semantics, just make it explicit. It's already not portable, so don't try to make it so. Rename that MAP_VALIDATE as MAP_SHARED_VALIDATE, make it have a value of 0x3, and make people do ret = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED_VALIDATE | MAP_SYNC, fd, 0); and then the kernel side is easier too (none of that random garbage playing games with looking at the "MAP_VALIDATE bit", but just another case statement in that map type thing. Boom. Done. Similar to ->fallocate() we also want the ability to validate the support for new flags on a per ->mmap() 'struct file_operations' instance basis. Towards that end arrange for flags to be generically validated against a mmap_supported_flags exported by 'struct file_operations'. By default all existing flags are implicitly supported, but new flags require MAP_SHARED_VALIDATE and per-instance-opt-in. Cc: Jan Kara <jack@suse.cz> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Andy Lutomirski <luto@kernel.org> Cc: Andrew Morton <akpm@linux-foundation.org> Suggested-by: Christoph Hellwig <hch@lst.de> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Reviewed-by: Ross Zwisler <ross.zwisler@linux.intel.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Diffstat (limited to 'mm/mmap.c')
-rw-r--r--mm/mmap.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/mm/mmap.c b/mm/mmap.c
index 680506faceae..924839fac0e6 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1387,9 +1387,24 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
1387 1387
1388 if (file) { 1388 if (file) {
1389 struct inode *inode = file_inode(file); 1389 struct inode *inode = file_inode(file);
1390 unsigned long flags_mask;
1391
1392 flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags;
1390 1393
1391 switch (flags & MAP_TYPE) { 1394 switch (flags & MAP_TYPE) {
1392 case MAP_SHARED: 1395 case MAP_SHARED:
1396 /*
1397 * Force use of MAP_SHARED_VALIDATE with non-legacy
1398 * flags. E.g. MAP_SYNC is dangerous to use with
1399 * MAP_SHARED as you don't know which consistency model
1400 * you will get. We silently ignore unsupported flags
1401 * with MAP_SHARED to preserve backward compatibility.
1402 */
1403 flags &= LEGACY_MAP_MASK;
1404 /* fall through */
1405 case MAP_SHARED_VALIDATE:
1406 if (flags & ~flags_mask)
1407 return -EOPNOTSUPP;
1393 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE)) 1408 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1394 return -EACCES; 1409 return -EACCES;
1395 1410