Skip to contentSkip to conversation

ovl: keep merged and impure readdir caches separate

RFC PATCH In review Nirmoy Das 2 participants 8 messages May 11 – May 14 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Nirmoy Das

author
#0 lore raw

RFC PATCH 1/1 ovl: keep merged and impure readdir caches separate

Overlayfs uses one inode cache slot for two readdir cache users with different lifetime rules. Merged directory iteration pins the cache from open directory files with cache->refcount. Impure real-directory iteration uses the inode cache as an unrefcounted lookup table.

Those caches cannot be reused interchangeably. If merged iteration finds an impure cache in the inode slot, it can pin and seek through a cache that was not built for merged iteration. If impure iteration finds a merged cache, it can walk an object whose lifetime is controlled by open directory files. Either direction can leave ovl_iterate() using stale cache entries.

Add ovl_dir_cache_drop() to detach the inode cache before freeing it. Keep refcounted merged caches alive until ovl_cache_put(), stop publishing new merged caches through the inode slot, and let impure iteration reuse only unrefcounted caches.

Fixes 4edb83bb1041 ("ovl: constant d_ino for non-merge dirs")
Reported-by [email protected]
Assisted-by Codex:GPT-5 [lore] [checkpatch]
---
 fs/overlayfs/readdir.c | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)
fs/overlayfs/readdir.c +26 -12
index 1dcc75b3a90f9..326d8ad173881 100644
@@ -292,6 +292,27 @@ void ovl_dir_cache_free(struct inode *inode)
 	}
 }
 
+static void ovl_dir_cache_drop(struct inode *inode)
+{
+	struct ovl_dir_cache *cache = ovl_dir_cache(inode);
+
+	if (!cache)
+		return;
+
+	ovl_set_dir_cache(inode, NULL);
+
+	/*
+	 * Merged dir caches are refcounted by open directory files.  If the
+	 * inode cache is replaced while such a file still references it, keep
+	 * the old cache alive until ovl_cache_put().
+	 */
+	if (cache->refcount)
+		return;
+
+	ovl_cache_free(&cache->entries);
+	kfree(cache);
+}
+
 static void ovl_cache_put(struct ovl_dir_file *od, struct inode *inode)
 {
 	struct ovl_dir_cache *cache = od->cache;
@@ -485,13 +506,7 @@ static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
 	struct ovl_dir_cache *cache;
 	struct inode *inode = d_inode(dentry);
 
-	cache = ovl_dir_cache(inode);
-	if (cache && ovl_inode_version_get(inode) == cache->version) {
-		WARN_ON(!cache->refcount);
-		cache->refcount++;
-		return cache;
-	}
-	ovl_set_dir_cache(d_inode(dentry), NULL);
+	ovl_dir_cache_drop(inode);
 
 	cache = kzalloc_obj(struct ovl_dir_cache);
 	if (!cache)
@@ -509,7 +524,6 @@ static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
 	}
 
 	cache->version = ovl_inode_version_get(inode);
-	ovl_set_dir_cache(inode, cache);
 
 	return cache;
 }
@@ -699,12 +713,12 @@ static struct ovl_dir_cache *ovl_cache_get_impure(const struct path *path)
 	struct ovl_dir_cache *cache;
 
 	cache = ovl_dir_cache(inode);
-	if (cache && ovl_inode_version_get(inode) == cache->version)
+	if (cache && !cache->refcount &&
+	    ovl_inode_version_get(inode) == cache->version)
 		return cache;
 
-	/* Impure cache is not refcounted, free it here */
-	ovl_dir_cache_free(inode);
-	ovl_set_dir_cache(inode, NULL);
+	/* Drop stale or incompatible inode cache before building impure cache */
+	ovl_dir_cache_drop(inode);
 
 	cache = kzalloc_obj(struct ovl_dir_cache);
 	if (!cache)

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83

Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi Nirmoy,

Thanks for the patch.

Quoted 12 lines · Nirmoy Das

Overlayfs uses one inode cache slot for two readdir cache users with different lifetime rules. Merged directory iteration pins the cache from open directory files with cache->refcount. Impure real-directory iteration uses the inode cache as an unrefcounted lookup table.

Those caches cannot be reused interchangeably. If merged iteration finds an impure cache in the inode slot, it can pin and seek through a cache that was not built for merged iteration. If impure iteration finds a merged cache, it can walk an object whose lifetime is controlled by open directory files. Either direction can leave ovl_iterate() using stale cache entries.

Quoted 3 lines

Add ovl_dir_cache_drop() to detach the inode cache before freeing it. Keep refcounted merged caches alive until ovl_cache_put(), stop publishing new merged caches through the inode slot,

This is unacceptable - invalidating merged dir cache to paper over another root bug, which was not really fixed.

Quoted 2 lines

and let impure iteration reuse only
unrefcounted caches.

All those guards are nice, but how does a directory inode change from being merged to impure or vice versa?
This should never happen.

It took a lot of arguing with Sonnet about wrong leads to find the real bug.

Real bug was introduced by:
b79e05aaa1667 ("ovl: no direct iteration for dir with origin xattr")

It changed the test from:

od->is_real = !OVL_TYPE_MERGE(type);
od->is_upper = OVL_TYPE_UPPER(type);

to:

od->is_real = !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir)); od->is_upper = OVL_TYPE_UPPER(type);

But there is a race window in copy up of a directory where upper is set and published before the OVL_WHITEOUTS flag is set.

an opendir() observing this state inside the race window will wrongly start to iterate_real and another opendir later will observe the flag and start iterate_merged - boom!

Attached is what I think is a correct fix.
WDYT?

Thanks,
Amir.

From 94d75948ceb70d5dc49e9187a742b9a2e42c33c0 Mon Sep 17 00:00:00 2001
From: Amir Goldstein <[email protected]>
Date: Mon, 11 May 2026 22:25:18 +0200
Subject: [PATCH] ovl: fix race between copy-up and open of a directory

Commit b79e05aaa166 ("ovl: no direct iteration for dir with origin xattr") replaced the structural OVL_TYPE_MERGE check in od->is_real with the OVL_WHITEOUTS flag, but set that flag after ovl_inode_update() published the upper dentry via smp_wmb(). A concurrent open() that observes __upperdentry via the smp_rmb pair may not yet see OVL_WHITEOUTS and gets od->is_real=true on what is now a merged directory.

That stale od->is_real causes a subsequent getdents() to call ovl_cache_get_impure(), which frees the merged refcounted cache stored by another fd, resulting in a use-after-free.

Move the flag set into ovl_inode_update() before the smp_wmb() to close the race window.

Fixes b79e05aaa166 ("ovl: no direct iteration for dir with origin xattr")
Reported-by [email protected]
---
 fs/overlayfs/copy_up.c | 2 --
 fs/overlayfs/util.c    | 9 +++++++++
 2 files changed, 9 insertions(+), 2 deletions(-)
fs/overlayfs/copy_up.c +0 -2
index 13cb60b52bd6e..f612ceab30af9 100644
@@ -835,8 +835,6 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
 	if (!c->metacopy)
 		ovl_set_upperdata(inode);
 	ovl_inode_update(inode, temp);
-	if (S_ISDIR(inode->i_mode))
-		ovl_set_flag(OVL_WHITEOUTS, inode);
 out:
 	ovl_end_write(c->dentry);
 
fs/overlayfs/util.c +9 -0
index 7b86a6bac6449..7cd3201ae7497 100644
@@ -595,6 +595,15 @@ void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
 
 	WARN_ON(OVL_I(inode)->__upperdentry);
 
+	/*
+	 * For a directory being copied up, set OVL_WHITEOUTS before publishing
+	 * the upper dentry, so a concurrent open() that observes __upperdentry
+	 * via the smp_wmb/rmb pair is guaranteed to also observe OVL_WHITEOUTS
+	 * and ovl_dir_is_real() returns false.
+	 */
+	if (S_ISDIR(inode->i_mode))
+		ovl_set_flag(OVL_WHITEOUTS, inode);
+
 	/*
 	 * Make sure upperdentry is consistent before making it visible
 	 */
Signature · 1 line
2.54.0
author
#2 lore raw

Hi Amir,

Quoted 47 lines · Amir Goldstein

Hi Nirmoy,

Thanks for the patch.

Quoted 11 lines · Nirmoy Das

Overlayfs uses one inode cache slot for two readdir cache users with different lifetime rules. Merged directory iteration pins the cache from open directory files with cache->refcount. Impure real-directory iteration uses the inode cache as an unrefcounted lookup table.

Those caches cannot be reused interchangeably. If merged iteration finds an impure cache in the inode slot, it can pin and seek through a cache that was not built for merged iteration. If impure iteration finds a merged cache, it can walk an object whose lifetime is controlled by open directory files. Either direction can leave ovl_iterate() using stale cache entries.

Quoted 3 lines

Add ovl_dir_cache_drop() to detach the inode cache before freeing it. Keep refcounted merged caches alive until ovl_cache_put(), stop publishing new merged caches through the inode slot,

This is unacceptable - invalidating merged dir cache to paper over another root bug, which was not really fixed.

Quoted 2 lines

and let impure iteration reuse only
unrefcounted caches.

All those guards are nice, but how does a directory inode change from being merged to impure or vice versa?
This should never happen.

It took a lot of arguing with Sonnet about wrong leads to find the real bug.

Real bug was introduced by:
b79e05aaa1667 ("ovl: no direct iteration for dir with origin xattr")

It changed the test from:

od->is_real = !OVL_TYPE_MERGE(type);
od->is_upper = OVL_TYPE_UPPER(type);

to:

od->is_real = !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir)); od->is_upper = OVL_TYPE_UPPER(type);

But there is a race window in copy up of a directory where upper is set and published before the OVL_WHITEOUTS flag is set.

Quoted 6 lines

an opendir() observing this state inside the race window will wrongly start to iterate_real and another opendir later will observe the flag and start iterate_merged - boom!

Attached is what I think is a correct fix.
WDYT?

This looks correct with my limited understanding of the overlay code. I still see the issue when running with

the syzkaller-derived C reproducer in a loop inside an arm64 virtme/qemu VM with a KASAN/debug kernel.

Let me try to debug it more and get back.

Regards,

Nirmoy

Quoted 3 lines

Thanks,
Amir.

maintainer
#3 lore raw
Quoted 58 lines · Nirmoy Das

Hi Amir,

Quoted 47 lines · Amir Goldstein

Hi Nirmoy,

Thanks for the patch.

Quoted 11 lines · Nirmoy Das

Overlayfs uses one inode cache slot for two readdir cache users with different lifetime rules. Merged directory iteration pins the cache from open directory files with cache->refcount. Impure real-directory iteration uses the inode cache as an unrefcounted lookup table.

Those caches cannot be reused interchangeably. If merged iteration finds an impure cache in the inode slot, it can pin and seek through a cache that was not built for merged iteration. If impure iteration finds a merged cache, it can walk an object whose lifetime is controlled by open directory files. Either direction can leave ovl_iterate() using stale cache entries.

Quoted 3 lines

Add ovl_dir_cache_drop() to detach the inode cache before freeing it. Keep refcounted merged caches alive until ovl_cache_put(), stop publishing new merged caches through the inode slot,

This is unacceptable - invalidating merged dir cache to paper over another root bug, which was not really fixed.

Quoted 2 lines

and let impure iteration reuse only
unrefcounted caches.

All those guards are nice, but how does a directory inode change from being merged to impure or vice versa?
This should never happen.

It took a lot of arguing with Sonnet about wrong leads to find the real bug.

Real bug was introduced by:
b79e05aaa1667 ("ovl: no direct iteration for dir with origin xattr")

It changed the test from:

od->is_real = !OVL_TYPE_MERGE(type);
od->is_upper = OVL_TYPE_UPPER(type);

to:

od->is_real = !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir)); od->is_upper = OVL_TYPE_UPPER(type);

But there is a race window in copy up of a directory where upper is set and published before the OVL_WHITEOUTS flag is set.

Quoted 6 lines

an opendir() observing this state inside the race window will wrongly start to iterate_real and another opendir later will observe the flag and start iterate_merged - boom!

Attached is what I think is a correct fix.
WDYT?

Quoted 8 lines

This looks correct with my limited understanding of the overlay code. I still see the issue when running with

the syzkaller-derived C reproducer in a loop inside an arm64 virtme/qemu VM with a KASAN/debug kernel.

Let me try to debug it more and get back.

Thanks! I appreciate it.
I wasn't sure how reliably the reproducer works.

If you do find a fix to the root cause, do feel free to use my patch as is or modified and post with changes and proper commit message.

b.t.w I am not against adding WARN_ON() assertions in readdir code to make sure we do not hit a problem like this again, but as assertions, not as code that looks like it should really work in this situation (i.e. no dropping of wrong cache just error).

FYI, I had contemplated an alternative fix, but went with the smallest one:

implement ovl_i_path_type(struct inode *inode) and make ovl_path_type(struct dentry *dentry) a wrapper

static inline bool ovl_dir_is_real(struct inode *dir)

{
        return !ovl_test_flag(OVL_WHITEOUTS, dir) &&
                   !OVL_TYPE_MERGE(ovl_i_path_type(dir));
}

This fix is a bit nicer to understand and the OVL_WHITEOUTS flags becomes an optimization for OVL_TYPE_MERGE() in addition to handling the remnant whiteouts case.

If this happens to survive the reproducer for some reason, do feel free to post it instead of my patch.

Thanks,
Amir.

author
#4 lore raw

Hi Amir,

After a lot of debug and traces I found another bug in ovl_iterate_merged(): err is set from PTR_ERR(cache) before the IS_ERR(cache) check, so on success err holds the truncated cache pointer.

Claude generated this:
getdents64

  └── iterate_dir(outer_overlay_file)
      └── ovl_iterate_merged                                    [OUTER]
          │
          ├── cache = ovl_cache_get(dentry):                    [OUTER ovl_cache_get]
          │   │   cache_local = kzalloc(...)
          │   │   res = ovl_dir_read_merged(...)
          │   │   │
          │   │   └── iterate_dir(inner_overlay_file)
          │   │       └── ovl_iterate_merged                    [INNER]
          │   │           ├── cache_inner = ovl_cache_get(...)  valid 0xFFFF8881_CAC4D940
          │   │           ├── err = PTR_ERR(cache_inner)        = -893068992 (low32 of ptr)
          │   │           ├── IS_ERR(cache_inner) → FALSE
          │   │           └── return err;                       ← leaked stale int
          │   │
          │   └── return ERR_PTR(res);                          res = -893068992 (int)
          │                                                     sign-extended →
          │                                                     (void *)0xFFFFFFFF_CAC4D940
          │
          ├── err = PTR_ERR(cache);                             err = -893068992
          ├── if (IS_ERR(cache))   ← FALSE: IS_ERR only trips on top 4K
          │                                  (errno window);
          │                                  0xFFFFFFFF_CAC4D940 sits below
          │       return err;                                    ← skipped
          ├── od->cache = cache;   ★ corrupted pointer stored
          └── ovl_seek_cursor(od, ctx->pos)
                  list_for_each(p, &od->cache->entries)
                  p = *(&od->cache->entries) on bad pointer     ★ PAGE FAULT

Sent it as a separate patch: "[PATCH] ovl: keep err zero after successful ovl_cache_get()".

Let me know what you think.

Regards,
Nirmoy

maintainer
#5 lore raw
Quoted 4 lines · Nirmoy Das

Hi Amir,

After a lot of debug and traces I found another bug in

Thanks for persisting on this problem!

Quoted 15 lines

ovl_iterate_merged(): err is set from PTR_ERR(cache) before the IS_ERR(cache) check, so on success err holds the truncated cache pointer.

Claude generated this:
getdents64

  └── iterate_dir(outer_overlay_file)
      └── ovl_iterate_merged                                    [OUTER]
          │
          ├── cache = ovl_cache_get(dentry):                    [OUTER ovl_cache_get]
          │   │   cache_local = kzalloc(...)
          │   │   res = ovl_dir_read_merged(...)
          │   │   │
          │   │   └── iterate_dir(inner_overlay_file)
          │   │       └── ovl_iterate_merged                    [INNER]
Quoted 8 lines
          │   │           ├── cache_inner = ovl_cache_get(...)  valid 0xFFFF8881_CAC4D940
          │   │           ├── err = PTR_ERR(cache_inner)        = -893068992 (low32 of ptr)
          │   │           ├── IS_ERR(cache_inner) → FALSE
          │   │           └── return err;                       ← leaked stale int
          │   │
          │   └── return ERR_PTR(res);                          res = -893068992 (int)
          │                                                     sign-extended →
          │                                                     (void *)0xFFFFFFFF_CAC4D940

Wow! This is demonic.
I think we need to fortify ERR_PTR() to not accept illegal errno values. I'll try to write a patch.

Quoted 11 lines
          │
          ├── err = PTR_ERR(cache);                             err = -893068992
          ├── if (IS_ERR(cache))   ← FALSE: IS_ERR only trips on top 4K
          │                                  (errno window);
          │                                  0xFFFFFFFF_CAC4D940 sits below
          │       return err;                                    ← skipped
          ├── od->cache = cache;   ★ corrupted pointer stored
          └── ovl_seek_cursor(od, ctx->pos)
                  list_for_each(p, &od->cache->entries)
                  p = *(&od->cache->entries) on bad pointer     ★ PAGE FAULT

Oh boy!

Quoted 4 lines

Sent it as a separate patch: "[PATCH] ovl: keep err zero after successful ovl_cache_get()".

Let me know what you think.

Patch looks great - real root cause, gave you minor comments.

IIUC, as far as you know, the repro never triggered the theoretic OVL_WHITEOUTS race?

It was always just this ERR_PTR(PTR_ERR()) black magic?

Thanks,
Amir.

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#7 lore raw
Quoted 1 line · Nirmoy Das

On 14.05.26 16:09, Amir Goldstein wrote:

...

Quoted 5 lines
Quoted 5 lines

Patch looks great - real root cause, gave you minor comments.

IIUC, as far as you know, the repro never triggered the theoretic OVL_WHITEOUTS race?

Quoted 7 lines
Quoted 1 line

It was always just this ERR_PTR(PTR_ERR()) black magic?

Yes, with this particular syzkaller reproducer, it was always this black magic. I tried with and without your suggested changes and saw no difference in the crash behavior.

But I think we should also have the fix for OVL_WHITEOUTS race as well.

Yeh maybe.
I don't like making fixes to theoretical races without reproducers. I'll think about it.
Maybe I'll try to reproduce synthetically.
Or maybe I'll just do a fortify patch.

Thanks,
Amir.

maintainer
#8 lore raw
Quoted 14 lines · Amir Goldstein
Quoted 1 line · Nirmoy Das

On 14.05.26 16:09, Amir Goldstein wrote:

...

Quoted 10 lines
Quoted 6 lines

Patch looks great - real root cause, gave you minor comments.

IIUC, as far as you know, the repro never triggered the theoretic OVL_WHITEOUTS race?

It was always just this ERR_PTR(PTR_ERR()) black magic?

Yes, with this particular syzkaller reproducer, it was always this black magic. I tried with and without your suggested changes and saw no difference in the crash behavior.

Quoted 8 lines
Quoted 2 lines

But I think we should also have the fix for OVL_WHITEOUTS race as well.

Yeh maybe.
I don't like making fixes to theoretical races without reproducers. I'll think about it.
Maybe I'll try to reproduce synthetically.
Or maybe I'll just do a fortify patch.

FYI pushed readdir cache fortify patch to ovl-fixes in github. It is based on the idea of your first patch, but implementation is different.

Feel free to review.
I will post it later after testing and deciding what to do about the "race fix" patch.

Thanks,
Amir.