blob: 2fa656416a93d221d83d9bc77511a10432dd1773 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
From 9c8885c6e020451e4a4578be9db318e5c07227ea Mon Sep 17 00:00:00 2001
From: Jan Kiszka <jan.kiszka@siemens.com>
Date: Tue, 4 Jun 2019 14:40:09 +0200
Subject: [PATCH 10/32] uio: Enable read-only mappings
This allows to tag memory regions read-only, denying userspace to map
them writable. Default remains read/write.
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
drivers/uio/uio.c | 9 +++++++++
include/linux/uio_driver.h | 2 ++
2 files changed, 11 insertions(+)
diff --git a/drivers/uio/uio.c b/drivers/uio/uio.c
index a57698985f9c..ac18542ee4fe 100644
--- a/drivers/uio/uio.c
+++ b/drivers/uio/uio.c
@@ -790,6 +790,15 @@ static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
goto out;
}
+ if (idev->info->mem[mi].readonly) {
+ if (vma->vm_flags & VM_WRITE) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ vma->vm_flags &= ~VM_MAYWRITE;
+ }
+
if (idev->info->mmap) {
ret = idev->info->mmap(idev->info, vma);
goto out;
diff --git a/include/linux/uio_driver.h b/include/linux/uio_driver.h
index 01081c4726c0..ebfc06e36ca2 100644
--- a/include/linux/uio_driver.h
+++ b/include/linux/uio_driver.h
@@ -31,6 +31,7 @@ struct uio_map;
* @offs: offset of device memory within the page
* @size: size of IO (multiple of page size)
* @memtype: type of memory addr points to
+ * @readonly: true of region is read-only
* @internal_addr: ioremap-ped version of addr, for driver internal use
* @map: for use by the UIO core only.
*/
@@ -40,6 +41,7 @@ struct uio_mem {
unsigned long offs;
resource_size_t size;
int memtype;
+ bool readonly;
void __iomem *internal_addr;
struct uio_map *map;
};
--
2.11.0
|