From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.229.6 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796437287; b=q6wxoj4MLFgnFocUxMQEia2HPmw9+XJXql7VnRcNPC99KBXuW8WqBi+qYZd2exchtYC2 3by1DnmlDOPjV5sgpSueL6WQgyJzR1JDoshhPQva8P4dMP1R7bbiK1LdASJKf2lraClVw MfLVMkncFUE4IO73GkQNknQK/CMvTIor+R8TE8c677wCNkR0md/uA4AzMcCn2cRCMaux5 GijYtRLUWnkgHEvl4UVzeIGric5Yf9fjaq439xNIQHV2f4qqQDY6/CbM6mQaPhVDNjshh 8vgQ5McGXXIUVl3a4dY/8HZBygxShIPCFF3SjYZleow3uBZFrkCXXTT8+Z0q6/cAbDA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796437287; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=ixn572RpN3W2l606NscNB8B40QRxii/XBCwe5Z9rQkE=; b=i5ajKUnp1q7BOsUb8H0ZCx53iFZzSuvQT78fBcXju0lWAulwqKDWvnr1WteLYFhJhDXV 5vmCly//pq3zvTkBn+/3otML3TTrcPMJMWlZt9BixC8ZNrjpmQ0m8qgURhPLj8DQ5dUue rPtbxWRB1VDnI8L4umvVhV7MX8k6e/PT4P5KandeiZiffoGTF7QPxQFeGQ+Du/D5n3ErJ DTU6+9gXA+7FvFb3524jc5c0ZfgKLSVQRiWeHghY2VIB9Q4Qo3FZUFSgJ1L6Nl4elHnqL LcCprswyRXZRsbtj7NUeR+oAQneQWh3GC2o1/BeipjCTlOxwS5qsh3f+qMa65wCCWlg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="t8ABsa/H"; arc=none smtp.remote-ip=74.125.229.6 Received: from mail-dy2-f6.google.com (mail-dy2-f6.google.com [74.125.229.6]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A0BC606A337 for ; Sat, 5 Dec 2026 02:21:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5A0BC606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=t8ABsa/H Received: by mail-dy2-f6.google.com with SMTP id 5a478bee46e88-35adc681e1aso1812631eec.0 for ; Fri, 04 Dec 2026 18:21:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796437286; x=1797042086; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ixn572RpN3W2l606NscNB8B40QRxii/XBCwe5Z9rQkE=; b=t8ABsa/HUHwYCUtATfY3w8ucV2I0CP+Tl9i0C9dj2SqPe2gyHnwL7ZoBtmnx4+k3AC L0FXTclbyzvqonXQo149k+0UfANNSxNLGRZmyugdyJwmU1wuQqI8rtufy8P5eaBkH23d +7Aeju/LYftZ5VZleoufNpEVpbXAZ6frD3nXMtiaZLOfDL/LRxPKxmtB5ZzwZb7RVJ5c p2wA2brOZEJbAoPpZqGQs2jnCVuLKk8rsAOLs697QUH9NSmkdJBZA0rSLxn5nnOVL3V9 mTFxomtGBg56JlcGNY4n3YsvHv08USKw1538cC3ERn1+NeF7WPW9FipFh5UCcLgwvB1G oWvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796437286; x=1797042086; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=ixn572RpN3W2l606NscNB8B40QRxii/XBCwe5Z9rQkE=; b=C0B7AjGjvb8wN1osWaO5ZREMC12FNCcqaYeU2G0n14ouZJ59L06z2MbcUuJx+M+jaF 70khO8xdfolFkRFMN7jWrw3bNMjMyGkuiBwdMZuNRmvNdOKq5epCsTAwMqxLj0IKhboy H0JhK5HraHcAzJTE7OcU+ONOdpgQUAd+MDqdSQkJ0ZXZ3Crm1owKkVSeZy4OFZdehoFp hB+PpnWQckv/rxcJZk6qQeK2yEu+u7JNCappfPDmH7Zii8qtF2TnBOhu80ZhFOALFbjo i/73gO+T7uKTwSelhjG7wJUss1yfS1Gp6sgCe92Re8yZ/vNTsB14LhGHKDR14qh5yYhm OEgw== X-Forwarded-Encrypted: i=1; AL+hErHldE9yDarUWGdUqPwggCS2knOFc4boow6xNsuOK9Lq7jdOIMSBNCskG8/7QZgOpzA7Ii4Obw8sfZ+o7ko=@vger.kernel.org X-Gm-Message-State: AB4q+uF3HVKdY+aDqNPvkAmhnH6qyPiwIxI2meD4WSkQDwVb2+3pbQ2+ 8NhhwZILB+z/U+hEgNx0YS55hjHYNRa/luxuKgBQlXfW3pNxT0iq/TBF X-Gm-Gg: AZFdTZeS5w3VCy6KKWfUi1UpW0R3ycVP4Gli16ZB0H3CDALY81j0Ls0SB55wmuflDja LVOjts7KR9Mc2n3AsXAKVZ1/qSkrUVTlD4xRh7rxirHGbh9JgwbZiH5Tzcd7ySDy3Oe9YqOl+jy u6OP6uWDbFAGGiv9g8Ln6uet5XfyOqpdVN66tfWGcMWZUODEOTjtaYGn9aUDyVx7ypKxNcgwQa3 hWNyTO0LhA0VawE9pNZq51LEriCoTzIHEaEb5Erhrv/V5/VABDsORysAottgkun8fkDHuvsWgFB getZpRv9kdbh+nZT6IoHIHS+MB+Ev6K0Y4C09suyNTIUTawyuqPlEq3mrkTqjc9ctixrMsWY27w x8vYNxcokWXNnnM0YpLWz+rPW9RZQh+3JN5dl/ocVSGBt4gL/YXKnuvkVtVt6fQMZPiLKzwD0Ja tGSBXXY0rgRbupcfi0amKxw8mqE5iRvKWldy5RDXF4IlfTGB/3Pb/WQ3L5LTLRK4yt0YwTsxXfM owKv8b/AoGqUh+Xh0uG9w4Bo0KJhBUY0ymsJ3SgS/3DhRLa9G+U2U83VKwVomtyur2xSQ5CVETk OsCQXFXGaf4vKbJ+mwesZZKFXZu2+yBqn+x+WO8Py8JfRShYol0BXculNWm0VOKW+tD6s0AXR3J kyLmkCzEMbnWIbHNrtId5KMWhyB1UtSL9D9oYuwyV9x1TJKz8S6TeS9oGP49xQw== X-Received: by 2002:a05:693c:864e:10b0:386:ebc5:4747 with SMTP id 5a478bee46e88-387520dbf49mr4155163eec.11.1796437286282; Fri, 04 Dec 2026 18:21:26 -0800 (PST) Received: from localhost.localdomain ([76.33.234.50]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521c17a3sm15373886eec.11.2026.12.04.18.21.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 18:21:26 -0800 (PST) From: Hungyu Lin 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 Message-ID: <20261205022111.49900-3-dennylin0707@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261205022111.49900-1-dennylin0707@gmail.com> References: <20261205022111.49900-1-dennylin0707@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Hungyu Lin --- 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