linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: patches@lists.linux.dev, stable@vger.kernel.org
Cc: Miklos Szeredi <mszeredi@redhat.com>,
	Chunsheng Luo <luochunsheng@ustc.edu>,
	Sasha Levin <sashal@kernel.org>,
	miklos@szeredi.hu, linux-fsdevel@vger.kernel.org
Subject: [PATCH AUTOSEL 6.17-6.6] fuse: zero initialize inode private data
Date: Sat, 25 Oct 2025 11:55:03 -0400	[thread overview]
Message-ID: <20251025160905.3857885-72-sashal@kernel.org> (raw)
In-Reply-To: <20251025160905.3857885-1-sashal@kernel.org>

From: Miklos Szeredi <mszeredi@redhat.com>

[ Upstream commit 3ca1b311181072415b6432a169de765ac2034e5a ]

This is slightly tricky, since the VFS uses non-zeroing allocation to
preserve some fields that are left in a consistent state.

Reported-by: Chunsheng Luo <luochunsheng@ustc.edu>
Closes: https://lore.kernel.org/all/20250818083224.229-1-luochunsheng@ustc.edu/
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---

LLM Generated explanations, may be completely bogus:

YES

- What changed
  - In `fs/fuse/inode.c:98`, `fuse_alloc_inode()` now zero-initializes
    the entire FUSE-private part of `struct fuse_inode` while explicitly
    preserving the embedded VFS `struct inode`:
    - `fs/fuse/inode.c:106` adds a layout check:
      `BUILD_BUG_ON(offsetof(struct fuse_inode, inode) != 0);`
    - `fs/fuse/inode.c:108` zeroes everything beyond `fi->inode`:
      `memset((void *)fi + sizeof(fi->inode), 0, sizeof(*fi) -
      sizeof(fi->inode));`
  - The manual piecemeal initialization of only a handful of fields
    (e.g., `i_time`, `nodeid`, `nlookup`, `attr_version`, `orig_ino`,
    `state`, `submount_lookup`) is removed and replaced by the blanket
    private-data zeroing.
  - The invariants and locks are still set after zeroing:
    - `fi->inval_mask = ~0;` (`fs/fuse/inode.c:110`)
    - `mutex_init(&fi->mutex);` (`fs/fuse/inode.c:111`)
    - `spin_lock_init(&fi->lock);` (`fs/fuse/inode.c:112`)
    - `fi->forget = fuse_alloc_forget();` (`fs/fuse/inode.c:113`)
    - DAX and passthrough helpers remain unchanged
      (`fs/fuse/inode.c:117`, `fs/fuse/inode.c:120`).

- Why this fixes a real bug
  - Inode objects are allocated via `alloc_inode_sb()`, which is a non-
    zeroing slab allocation (`include/linux/fs.h:3407` →
    `kmem_cache_alloc_lru`). This means previously freed memory content
    can persist in new `struct fuse_inode` instances unless explicitly
    cleared.
  - Before this change, FUSE only zeroed a subset of private fields,
    leaving many newly added or less obvious fields uninitialized/stale,
    which can lead to incorrect behavior. Examples:
    - `fi->cached_i_blkbits` is used by cached getattr to compute
      `stat->blksize` without a server roundtrip (`fs/fuse/dir.c:1373`).
      If not initialized, userspace can observe garbage or stale block
      sizes when using cached attributes.
    - `fi->i_time` controls attribute staleness; it must start from a
      known baseline to force initial refresh (it’s now guaranteed
      zeroed before being set; previously it was explicitly written, but
      other related fields were not).
    - Readdir cache state in `fi->rdc.*` (e.g. `cached`, `pos`, `size`,
      `version`) must start clean, and is explicitly initialized only in
      `fuse_init_dir()` (`fs/fuse/dir.c:2266`). Zeroing ensures no stale
      values leak in the interim.
    - File-io cache accounting (`fi->iocachectr`, waitqueues and lists)
      is initialized in `fuse_init_file_inode()`
      (`fs/fuse/file.c:3121`–`fs/fuse/file.c:3136`); zeroing up front
      prevents spurious non-zero counters or garbage pointers before
      that init runs.
    - Passthrough backing file pointer `fi->fb` (present with
      `CONFIG_FUSE_PASSTHROUGH`) is now guaranteed NULL initially; the
      code also explicitly sets it via `fuse_inode_backing_set(fi,
      NULL)` (`fs/fuse/inode.c:120`). Zeroing avoids any transient stale
      pointer exposure.
  - This change conforms to the VFS model of non-zeroing allocation: it
    deliberately preserves `struct inode` (the part the VFS expects to
    keep stable) and only clears the FUSE-private tail. The
    `BUILD_BUG_ON` enforces the assumption that `inode` is the first
    field.

- Scope and risk
  - The fix is small, localized to a single function in FUSE, and does
    not modify any public interfaces or core VFS behavior.
  - It reduces risk by eliminating uninitialized data usage and
    potential state inconsistencies from inode slab reuse.
  - It is defensive across existing and future FUSE private fields,
    avoiding the need to remember to add manual zeroing for every new
    field.

- Dependencies and backport considerations
  - The code relies on standard kernel primitives: `offsetof`,
    `BUILD_BUG_ON`, and existing FUSE helpers. No architectural changes.
  - `alloc_inode_sb()` non-zeroing semantics are already present in
    stable series (see `include/linux/fs.h:3407`), so the bug exists
    there too.
  - The patch does not depend on other new features; it should apply
    cleanly or be trivial to adapt in stable trees that have the nearby
    code structure.

- User impact
  - Prevents user-visible inconsistencies (e.g., wrong `blksize` values)
    and eliminates potential undefined behavior from stale per-inode
    private state across reuse.
  - Also improves robustness against uninitialized reads that could
    manifest as rare warnings or subtle regressions.

Given it fixes a correctness bug with minimal, contained changes and
clear safety benefits, this commit is a good candidate for backporting
to stable trees.

 fs/fuse/inode.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index 7ddfd2b3cc9c4..7c0403a002e75 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -101,14 +101,11 @@ static struct inode *fuse_alloc_inode(struct super_block *sb)
 	if (!fi)
 		return NULL;
 
-	fi->i_time = 0;
+	/* Initialize private data (i.e. everything except fi->inode) */
+	BUILD_BUG_ON(offsetof(struct fuse_inode, inode) != 0);
+	memset((void *) fi + sizeof(fi->inode), 0, sizeof(*fi) - sizeof(fi->inode));
+
 	fi->inval_mask = ~0;
-	fi->nodeid = 0;
-	fi->nlookup = 0;
-	fi->attr_version = 0;
-	fi->orig_ino = 0;
-	fi->state = 0;
-	fi->submount_lookup = NULL;
 	mutex_init(&fi->mutex);
 	spin_lock_init(&fi->lock);
 	fi->forget = fuse_alloc_forget();
-- 
2.51.0


  parent reply	other threads:[~2025-10-25 16:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20251025160905.3857885-1-sashal@kernel.org>
2025-10-25 15:54 ` [PATCH AUTOSEL 6.17-5.4] allow finish_no_open(file, ERR_PTR(-E...)) Sasha Levin
2025-10-25 15:55 ` Sasha Levin [this message]
2025-10-25 15:57 ` [PATCH AUTOSEL 6.17] virtio_fs: fix the hash table using in virtio_fs_enqueue_req() Sasha Levin
2025-10-25 15:58 ` [PATCH AUTOSEL 6.17-6.12] exfat: validate cluster allocation bits of the allocation bitmap Sasha Levin
2025-10-25 16:00 ` [PATCH AUTOSEL 6.17-5.10] exfat: limit log print for IO error Sasha Levin
2025-10-25 16:01 ` [PATCH AUTOSEL 6.17] move_mount(2): take sanity checks in 'beneath' case into do_lock_mount() Sasha Levin

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=20251025160905.3857885-72-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=luochunsheng@ustc.edu \
    --cc=miklos@szeredi.hu \
    --cc=mszeredi@redhat.com \
    --cc=patches@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).