[3/5] tmpfs: Zero bytes after 'oldsize' if we're expanding the file

Message ID 20230202204428.3267832-4-willy@infradead.org
State New
Headers
Series Fix a minor POSIX conformance problem |

Commit Message

Matthew Wilcox Feb. 2, 2023, 8:44 p.m. UTC
  POSIX requires that "If the file size is increased, the extended area
shall appear as if it were zero-filled".  It is possible to use mmap to
write past EOF and that data will become visible instead of zeroes.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 mm/shmem.c | 2 ++
 1 file changed, 2 insertions(+)
  

Patch

diff --git a/mm/shmem.c b/mm/shmem.c
index 0005ab2c29af..2c8e8b417b00 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1124,6 +1124,8 @@  static int shmem_setattr(struct user_namespace *mnt_userns,
 			if (oldsize > holebegin)
 				unmap_mapping_range(inode->i_mapping,
 							holebegin, 0, 1);
+		} else {
+			shmem_truncate_range(inode, oldsize, newsize);
 		}
 	}