From: Hungyu Lin <dennylin0707@gmail.com>
To: jaegeuk@kernel.org, chao@kernel.org
Cc: lizhijun3@xiaomi.com, linux-f2fs-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org, dennylin0707@gmail.com
Subject: [PATCH v2 2/2] f2fs: zero preallocated blocks of pinned files for unprivileged users
Date: Sat, 5 Dec 2026 02:21:11 +0000 [thread overview]
Message-ID: <20261205022111.49900-3-dennylin0707@gmail.com> (raw)
In-Reply-To: <20261205022111.49900-1-dennylin0707@gmail.com>
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@vger.kernel.org
Suggested-by: Chao Yu <chao@kernel.org>
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
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
prev parent reply other threads:[~2026-12-05 2:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 2:21 [PATCH v2 0/2] " Hungyu Lin
2026-12-05 2:21 ` [PATCH v2 1/2] f2fs: fix to avoid pinfile fragment on checkpoint=disable mode Hungyu Lin
2026-12-05 2:21 ` Hungyu Lin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205022111.49900-3-dennylin0707@gmail.com \
--to=dennylin0707@gmail.com \
--cc=chao@kernel.org \
--cc=jaegeuk@kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lizhijun3@xiaomi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®