fdmem: always map dmabuf with PROT_WRITE

This is to enable zero copy inference on dmabufs. Gasket kernel driver
requires all user pointers passed to it to be writable in
drivers/staging/gasket/gasket_page_table.c gasket_perform_mapping
even if it's not actually writing to the buffer. If we map a buffer
with GST_MAP_READ it'll be mmapped with PROT_READ and gasket will
error out with -EFAULT.

While a more correct solution is to fix the gasket driver the needed
linux/mm APIs aren't there in the current version of the kernel.
We'd need
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=73b0140bf0fe9df90fb267c00673c4b9bf285430
which is for a much later kernel version.

This change may look dangerous but what it really does is to make
dmabuf protection equal to buffers allocated by the system memory
allocator; i.e. even if you map a buffer with GST_MAP_READ only
you can, but should not, write to it.

BUG: 142164990

Change-Id: Ie615f923c44969a3606d8856311ffe853a0dd742
diff --git a/gst-libs/gst/allocators/gstdmabuf.c b/gst-libs/gst/allocators/gstdmabuf.c
index a9a0775..c2175c0 100644
--- a/gst-libs/gst/allocators/gstdmabuf.c
+++ b/gst-libs/gst/allocators/gstdmabuf.c
@@ -100,7 +100,8 @@
 {
   g_return_val_if_fail (GST_IS_DMABUF_ALLOCATOR (allocator), NULL);
 
-  return gst_fd_allocator_alloc (allocator, fd, size, GST_FD_MEMORY_FLAG_KEEP_MAPPED);
+  return gst_fd_allocator_alloc (allocator, fd, size,
+      GST_FD_MEMORY_FLAG_KEEP_MAPPED | GST_FD_MEMORY_FLAG_PROT_WRITE);
 }
 
 /**
diff --git a/gst-libs/gst/allocators/gstfdmemory.c b/gst-libs/gst/allocators/gstfdmemory.c
index ee2b945..6b1632c 100644
--- a/gst-libs/gst/allocators/gstfdmemory.c
+++ b/gst-libs/gst/allocators/gstfdmemory.c
@@ -88,6 +88,7 @@
 
   prot = flags & GST_MAP_READ ? PROT_READ : 0;
   prot |= flags & GST_MAP_WRITE ? PROT_WRITE : 0;
+  prot |= mem->flags & GST_FD_MEMORY_FLAG_PROT_WRITE ? PROT_WRITE : 0;
 
   g_mutex_lock (&mem->lock);
   /* do not mmap twice the buffer */
diff --git a/gst-libs/gst/allocators/gstfdmemory.h b/gst-libs/gst/allocators/gstfdmemory.h
index 6693593..6cdc605 100644
--- a/gst-libs/gst/allocators/gstfdmemory.h
+++ b/gst-libs/gst/allocators/gstfdmemory.h
@@ -58,6 +58,7 @@
   GST_FD_MEMORY_FLAG_KEEP_MAPPED = (1 << 0),
   GST_FD_MEMORY_FLAG_MAP_PRIVATE = (1 << 1),
   GST_FD_MEMORY_FLAG_DONT_CLOSE  = (1 << 2),
+  GST_FD_MEMORY_FLAG_PROT_WRITE  = (1 << 31),
 } GstFdMemoryFlags;
 
 /**