qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Open file as read only on private mapping in qemu_ram_alloc_from_file
@ 2023-07-25 10:52 Thiner Logoer
  2023-07-25 11:42 ` David Hildenbrand
  0 siblings, 1 reply; 6+ messages in thread
From: Thiner Logoer @ 2023-07-25 10:52 UTC (permalink / raw)
  To: qemu-devel; +Cc: david, imammedo, Thiner Logoer

An read only file can be mapped with read write as long as the
mapping is private, which is very common case. Make
qemu_ram_alloc_from_file open file as read only when the
mapping is private, otherwise open will fail when file
does not allow write.

If this file does not exist or is a directory, the flag is not used,
so it should be OK.

from https://gitlab.com/qemu-project/qemu/-/issues/1689

Signed-off-by: Thiner Logoer <logoerthiner1@163.com>
---
 softmmu/physmem.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index 3df73542e1..e8036ee335 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -1945,8 +1945,15 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
     int fd;
     bool created;
     RAMBlock *block;
+    
+    /*
+     * If map is private, the fd does not need to be writable.
+     * This only get effective when the file is existent.
+     */
+    bool open_as_readonly = readonly || !(ram_flags & RAM_SHARED);
 
-    fd = file_ram_open(mem_path, memory_region_name(mr), readonly, &created,
+    fd = file_ram_open(mem_path, memory_region_name(mr),
+                       open_as_readonly, &created,
                        errp);
     if (fd < 0) {
         return NULL;
-- 
2.40.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-07-26 14:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-25 10:52 [PATCH] Open file as read only on private mapping in qemu_ram_alloc_from_file Thiner Logoer
2023-07-25 11:42 ` David Hildenbrand
2023-07-25 16:01   ` ThinerLogoer
2023-07-26  8:11     ` David Hildenbrand
2023-07-26 12:40       ` ThinerLogoer
2023-07-26 13:47       ` Igor Mammedov

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).