[PATCH v2 2/2] f2fs: zero preallocated blocks of pinned files for unprivileged users
From: Hungyu Lin
Date: Fri Dec 04 2026 - 21:21:38 EST
fallocate() on a pinned file allocates blocks without zeroing them, so
reading the file returns whatever the blocks held before.
Documentation/filesystems/f2fs.rst describes this as "zero or random
data". Commit cad3836f9eb2 ("f2fs: allocate blocks for pinned file")
added this while F2FS_IOC_SET_PIN_FILE required CAP_SYS_ADMIN, but
commit b13bdf03bb80 ("f2fs: allow all the users to pin a file") then let
any user pin a file. Since then any user can create a file, pin it,
fallocate() it and read back data of files that other users have
deleted.
For callers without CAP_SYS_ADMIN, zero out the free blocks of the
pinning section before they are allocated to the file, after dropping
the discard commands which are queued for them. The blocks are zeroed
before the allocation rather than after it, so that a concurrent read
never sees the old data and the blocks which the file already had are
not touched.
Fixes: b13bdf03bb80 ("f2fs: allow all the users to pin a file")
Cc: stable@xxxxxxxxxxxxxxx
Suggested-by: Chao Yu <chao@xxxxxxxxxx>
Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>
---
fs/f2fs/f2fs.h | 1 +
fs/f2fs/file.c | 3 +++
fs/f2fs/segment.c | 38 ++++++++++++++++++++++++++++++++++++++
3 files changed, 42 insertions(+)
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index e9ef37be869e..c9fea5a2437d 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4334,6 +4334,7 @@ int f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
unsigned int start, unsigned int end);
int f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi);
+int f2fs_zero_pinning_section(struct f2fs_sb_info *sbi);
int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 2f36869a7cdc..9036c55d3884 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -2194,6 +2194,7 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
if (f2fs_is_pinned_file(inode)) {
block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
block_t sec_len;
+ bool zeroout = !capable(CAP_SYS_ADMIN);
if (map.m_lblk % sec_blks) {
blk_start = rounddown(map.m_lblk, sec_blks);
@@ -2231,6 +2232,8 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
}
err = f2fs_allocate_pinning_section(sbi);
+ if (!err && zeroout)
+ err = f2fs_zero_pinning_section(sbi);
if (err) {
f2fs_up_write(&sbi->pin_sem);
goto out_err;
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 61be4dbf76db..131d6e325f11 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -3567,6 +3567,44 @@ int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi)
return err;
}
+int f2fs_zero_pinning_section(struct f2fs_sb_info *sbi)
+{
+ struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED);
+ block_t blkaddr, blkcnt;
+ int err = 0;
+
+ mutex_lock(&curseg->curseg_mutex);
+ if (curseg->segno == NULL_SEGNO) {
+ mutex_unlock(&curseg->curseg_mutex);
+ return -ENOSPC;
+ }
+ blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
+ blkcnt = START_BLOCK(sbi, GET_START_SEG_FROM_SEC(sbi, curseg->segno)) +
+ BLKS_PER_SEC(sbi) - blkaddr;
+ mutex_unlock(&curseg->curseg_mutex);
+
+ f2fs_drop_discard_cmd_range(sbi, blkaddr, blkcnt);
+
+ while (blkcnt && !err) {
+ struct block_device *bdev;
+ block_t blks = blkcnt;
+ sector_t sector;
+
+ if (f2fs_is_multi_device(sbi)) {
+ int devidx = f2fs_target_device_index(sbi, blkaddr);
+
+ blks = min(blks, FDEV(devidx).end_blk - blkaddr + 1);
+ }
+ bdev = f2fs_target_device(sbi, blkaddr, §or);
+ err = blkdev_issue_zeroout(bdev, sector,
+ SECTOR_FROM_BLOCK(sbi, blks),
+ GFP_NOFS, 0);
+ blkcnt -= blks;
+ blkaddr += blks;
+ }
+ return err;
+}
+
int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
{
int i;
--
2.43.0