From 0451ead2c56f93436ba5a13904e3a943812cae4c Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Wed, 22 May 2024 15:44:34 +0800 Subject: [PATCH] fs/mmap: Ensure anonymous pages are initialized to zero According to the mmap(2) specification, anonymous pages should be initialized to zero unless the MAP_UNINITIALIZED is specified. Signed-off-by: ouyangxiangzhen --- fs/mmap/fs_mmap.c | 11 +++++++++++ include/sys/mman.h | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/fs/mmap/fs_mmap.c b/fs/mmap/fs_mmap.c index 3b40f63049..5d44e604f8 100644 --- a/fs/mmap/fs_mmap.c +++ b/fs/mmap/fs_mmap.c @@ -108,6 +108,17 @@ static int file_mmap_(FAR struct file *filep, FAR void *start, if ((flags & MAP_ANONYMOUS) != 0) { ret = map_anonymous(&entry, kernel); + + /* According to the mmap(2) specification, anonymous pages should be + * initialized to zero unless the MAP_UNINITIALIZED is specified. + */ + + if ((ret == OK) && (flags & MAP_UNINITIALIZED) == 0) + { + DEBUGASSERT(entry.vaddr != NULL); + memset(entry.vaddr, 0, entry.length); + } + goto out; } diff --git a/include/sys/mman.h b/include/sys/mman.h index e3fae5f920..75d7ff3aef 100644 --- a/include/sys/mman.h +++ b/include/sys/mman.h @@ -56,7 +56,7 @@ #define MAP_ANONYMOUS (1 << 4) /* Bit 4: The mapping is not backed by any file */ #define MAP_ANON MAP_ANONYMOUS /* Alias */ -/* These are Linux-specific (none are implemented). */ +/* These are Linux-specific (most are not implemented). */ #define MAP_GROWSDOWN (1 << 5) /* Bit 5: Used to stack allocations */ #define MAP_DENYWRITE (1 << 6) /* Bit 6: Do not permit writes to file */ @@ -66,6 +66,8 @@ #define MAP_POPULATE (1 << 10) /* Bit 10: populate (prefault) page tables */ #define MAP_NONBLOCK (1 << 11) /* Bit 11: Do not block on IO */ +#define MAP_UNINITIALIZED (1 << 26) /* Bit 26: Do not clear the anonymous pages */ + /* Failure return */ #define MAP_FAILED ((FAR void*)-1)