mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] f2fs: zero preallocated blocks of pinned files for unprivileged users
@ 2026-12-05  2:21 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 ` [PATCH v2 2/2] f2fs: zero preallocated blocks of pinned files for unprivileged users Hungyu Lin
  0 siblings, 2 replies; 3+ messages in thread
From: Hungyu Lin @ 2026-12-05  2:21 UTC (permalink / raw)
  To: jaegeuk, chao; +Cc: lizhijun3, linux-f2fs-devel, linux-kernel, dennylin0707

fallocate() on a pinned file hands out blocks without zeroing them, and
any user can pin a file, so any user can read back data of files that
other users have deleted. v1 required CAP_SYS_ADMIN for fallocate() on a
pinned file. This version zeroes the blocks for callers without
CAP_SYS_ADMIN instead, so that they can still preallocate.

Patch 1 keeps the pinned log from switching to SSR while checkpoint is
disabled. It is a fix on its own, and patch 2 needs it: the blocks are
zeroed one section at a time before they are allocated, which relies on
the pinned log staying in its section.

Patch 2 zeroes the free blocks of the pinning section.
f2fs_secure_erase() is not used, as it works on blocks which are already
mapped to the file. The blocks are zeroed on the device, so an encrypted
pinned file reads them back as the decryption of zeroes.

Changes since v1:
- Zero the blocks for callers without CAP_SYS_ADMIN instead of refusing
  fallocate(), as suggested by Chao Yu. They are zeroed before the
  allocation rather than after it, so that a concurrent read cannot see
  the old data and the blocks which the file already had are kept.
- Add patch 1.

v1: https://lore.kernel.org/all/20261108075357.43331-1-dennylin0707@gmail.com/

Hungyu Lin (2):
  f2fs: fix to avoid pinfile fragment on checkpoint=disable mode
  f2fs: zero preallocated blocks of pinned files for unprivileged users

 fs/f2fs/f2fs.h    |  1 +
 fs/f2fs/file.c    |  3 +++
 fs/f2fs/segment.c | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+)


base-commit: 029b14ff22f28fddba992d58a216d30bda45586a
-- 
2.43.0


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

* [PATCH v2 1/2] f2fs: fix to avoid pinfile fragment on checkpoint=disable mode
  2026-12-05  2:21 [PATCH v2 0/2] f2fs: zero preallocated blocks of pinned files for unprivileged users Hungyu Lin
@ 2026-12-05  2:21 ` Hungyu Lin
  2026-12-05  2:21 ` [PATCH v2 2/2] f2fs: zero preallocated blocks of pinned files for unprivileged users Hungyu Lin
  1 sibling, 0 replies; 3+ messages in thread
From: Hungyu Lin @ 2026-12-05  2:21 UTC (permalink / raw)
  To: jaegeuk, chao; +Cc: lizhijun3, linux-f2fs-devel, linux-kernel, dennylin0707

fallocate() on pinfile allocates a whole section at a time, so on a
large section device the pinned log moves to the next segment in the
middle of the allocation. If checkpoint is disabled, need_new_seg()
picks an SSR segment at that point instead of the next segment of the
section, so that the rest of the blocks come from dirty segments. It
results in fragment blocks in pinfile, which violates the semantics of
pinfile introduced in commit f5a53edcf01e ("f2fs: support aligned
pinned file"), and fallocate() may also fail with -ENOSPC, as the pinned
log is closed once an SSR segment at the end of a section is used up.

mkfs.f2fs -f -s 4 /dev/vda
mount /dev/vda /mnt/f2fs
for ((i=0;i<32;i++)) do
  dd if=/dev/zero of=/mnt/f2fs/$i bs=1M count=1
done
sync
for ((i=0;i<32;i+=2)) do { rm /mnt/f2fs/$i; } done
sync
mount -o remount,checkpoint=disable:10% /mnt/f2fs
touch /mnt/f2fs/pinfile
f2fs_io pinfile set /mnt/f2fs/pinfile
f2fs_io fallocate 0 0 $((1024*1024*16)) /mnt/f2fs/pinfile
filefrag -v /mnt/f2fs/pinfile

Before, fallocate() fails with -ENOSPC after 2816 blocks and filefrag
shows 9 extents: the first segment of each new section and 256-block
pieces of dirty segments. After, it succeeds and the file is one extent
of 4096 blocks.

Let's always allocate a new segment in LFS manner for pinfile.

Fixes: e1175f022911 ("f2fs: fix to align to section for fallocate() on pinned file")
Cc: stable@vger.kernel.org
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
 fs/f2fs/segment.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 84fa9b95de82..61be4dbf76db 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -3445,6 +3445,8 @@ static bool need_new_seg(struct f2fs_sb_info *sbi, int type)
 {
 	struct curseg_info *curseg = CURSEG_I(sbi, type);
 
+	if (type == CURSEG_COLD_DATA_PINNED)
+		return true;
 	if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
 	    curseg->seg_type == CURSEG_WARM_NODE)
 		return true;
-- 
2.43.0


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

* [PATCH v2 2/2] f2fs: zero preallocated blocks of pinned files for unprivileged users
  2026-12-05  2:21 [PATCH v2 0/2] f2fs: zero preallocated blocks of pinned files for unprivileged users 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
  1 sibling, 0 replies; 3+ messages in thread
From: Hungyu Lin @ 2026-12-05  2:21 UTC (permalink / raw)
  To: jaegeuk, chao; +Cc: lizhijun3, linux-f2fs-devel, linux-kernel, dennylin0707

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, &sector);
+		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


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

end of thread, other threads:[~2026-12-05  2:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-05  2:21 [PATCH v2 0/2] f2fs: zero preallocated blocks of pinned files for unprivileged users 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 ` [PATCH v2 2/2] f2fs: zero preallocated blocks of pinned files for unprivileged users Hungyu Lin

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®