Skip to contentSkip to conversation

smb: client: clear ce->tgthint in free_tgts()

PATCH New Fredric Cover 3 participants 11 messages Jul 24 – Aug 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 smb: client: clear ce->tgthint in free_tgts()

When free_tgts() frees all structures in ce->tlist, ce->tgthint is left pointing to one of the freed cache_dfs_tgt structures.

If ce->tgthint is not reset before it is used later, it results in a use-after-free.

Set ce->tgthint to NULL in free_tgts() after the elements are freed to reflect that no elements remain.

Fixes 54be1f6c1c37 ("cifs: Add DFS cache routines")
---
 fs/smb/client/dfs_cache.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/client/dfs_cache.c +2 -0
index 8cd93cd2f00f..451550eb4023 100644
@@ -122,6 +122,8 @@ static inline void free_tgts(struct cache_entry *ce)
 		kfree(t->name);
 		kfree(t);
 	}
+
+	WRITE_ONCE(ce->tgthint, NULL);
 }
 
 static inline void flush_cache_ent(struct cache_entry *ce)
Signature · 1 line
2.53.0
#1 lore raw

v2 cover smb: client: fix dangling pointer in DFS target hints

This series addresses a Use-After-Free bug where ce->tgthint was left pointing to freed memory after free_tgts() was called.

To fix this, Patch 1 hardens the DFS cache readers against ce->tgthint being NULL. Also, Patch 1 hardens callers of get_tgt_name(), which returns an error pointer when ce->tgthint is NULL.

Patch 2 clears ce->tgthint in free_tgts(), eliminating the dangling pointer.

v1 -> v2:
- Addressed automated review by Sashiko:
https://sashiko.dev/#/patchset/20260724023539.1596955-1-fredric.cover.lkernel%40gmail.com
Split into a 2-patch series to harden readers against NULL and ERR_PTR target hints so that clearing ce->tgthint does not cause NULL pointer dereferences.

Fredric Cover (2):
smb: client: harden DFS cache against invalid target hints
smb: client: clear ce->tgthint in free_tgts()

 fs/smb/client/dfs_cache.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 1/2 smb: client: harden DFS cache against invalid target hints

Currently, get_tgt_name() returns ERR_PTR(-ENOENT) when ce->tgthint is NULL, and dfs_cache_noreq_update_tgthint() assumes ce->tgthint is always valid.

In preparation for clearing ce->tgthint in free_tgts(), harden callers of get_tgt_name() against ERR_PTR results and harden dfs_cache_noreq_update_tgthint() against NULL pointer dereferences.

---
 fs/smb/client/dfs_cache.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)
fs/smb/client/dfs_cache.c +24 -7
index 8cd93cd2f00f..c9ace4326a35 100644
@@ -869,13 +869,22 @@ int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nl
 		goto out_free_path;
 	}
 
-	if (ref)
-		rc = setup_referral(path, ce, ref, get_tgt_name(ce));
-	else
+	if (ref) {
+		char *target = get_tgt_name(ce);
+
+		if (IS_ERR(target)) {
+			rc = PTR_ERR(target);
+			goto out_unlock;
+		}
+		rc = setup_referral(path, ce, ref, target);
+	} else {
 		rc = 0;
+	}
+
 	if (!rc && tgt_list)
 		rc = get_targets(ce, tgt_list);
 
+out_unlock:
 	up_read(&htable_rw_lock);
 
 out_free_path:
@@ -915,10 +924,17 @@ int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
 		goto out_unlock;
 	}
 
-	if (ref)
-		rc = setup_referral(path, ce, ref, get_tgt_name(ce));
-	else
+	if (ref) {
+		char *target = get_tgt_name(ce);
+
+		if (IS_ERR(target)) {
+			rc = PTR_ERR(target);
+			goto out_unlock;
+		}
+		rc = setup_referral(path, ce, ref, target);
+	} else {
 		rc = 0;
+	}
 	if (!rc && tgt_list)
 		rc = get_targets(ce, tgt_list);
 
@@ -959,7 +975,8 @@ void dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt
 
 	t = READ_ONCE(ce->tgthint);
 
-	if (unlikely(!strcasecmp(it->it_name, t->name)))
+	/* Check 't' in case ce->tgthint was cleared by free_tgts() */
+	if (t && unlikely(!strcasecmp(it->it_name, t->name)))
 		goto out_unlock;
 
 	list_for_each_entry(t, &ce->tlist, list) {
Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 2/2 smb: client: clear ce->tgthint in free_tgts()

In reply to Fredric Cover

When free_tgts() frees all structures in ce->tlist, ce->tgthint is left pointing to one of the freed cache_dfs_tgt structures.

If ce->tgthint is not reset before it is used later, it results in a use-after-free.

Set ce->tgthint to NULL in free_tgts() after the elements are freed to reflect that no elements remain.

Fixes 54be1f6c1c37 ("cifs: Add DFS cache routines")
Cc [email protected] # depends on: smb: client: harden DFS cache against invalid target hints
---
 fs/smb/client/dfs_cache.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/client/dfs_cache.c +2 -0
index c9ace4326a35..86dba25b7a5a 100644
@@ -122,6 +122,8 @@ static inline void free_tgts(struct cache_entry *ce)
 		kfree(t->name);
 		kfree(t);
 	}
+
+	WRITE_ONCE(ce->tgthint, NULL);
 }
 
 static inline void flush_cache_ent(struct cache_entry *ce)
Signature · 1 line
2.53.0
maintainer
#4 lore raw

Re: [PATCH v2 0/2] smb: client: fix dangling pointer in DFS target hints

In reply to Fredric Cover
Quoted 18 lines · Fredric Cover

This series addresses a Use-After-Free bug where ce->tgthint was left pointing to freed memory after free_tgts() was called.

To fix this, Patch 1 hardens the DFS cache readers against ce->tgthint being NULL. Also, Patch 1 hardens callers of get_tgt_name(), which returns an error pointer when ce->tgthint is NULL.

Patch 2 clears ce->tgthint in free_tgts(), eliminating the dangling pointer.

v1 -> v2:
- Addressed automated review by Sashiko:
https://sashiko.dev/#/patchset/20260724023539.1596955-1-fredric.cover.lkernel%40gmail.com
Split into a 2-patch series to harden readers against NULL and ERR_PTR target hints so that clearing ce->tgthint does not cause NULL pointer dereferences.

Quoted 3 lines

Fredric Cover (2):
smb: client: harden DFS cache against invalid target hints
smb: client: clear ce->tgthint in free_tgts()

I will apply v3 patch-set to #for-next.
Thanks!

#5 lore raw

Re: [PATCH v2 0/2] smb: client: fix dangling pointer in DFS target hints

Namjae, Paulo, and list,

Checking on the status of this patch series. It appears to not yet be merged into #linux-next, at least on kernel.org. I understand that the branch/maintainer changes may interfere. Thanks for keeping me posted.

Thanks,
Fredric

Quoted 26 lines · Namjae Jeon
Quoted 18 lines · Fredric Cover

This series addresses a Use-After-Free bug where ce->tgthint was left pointing to freed memory after free_tgts() was called.

To fix this, Patch 1 hardens the DFS cache readers against ce->tgthint being NULL. Also, Patch 1 hardens callers of get_tgt_name(), which returns an error pointer when ce->tgthint is NULL.

Patch 2 clears ce->tgthint in free_tgts(), eliminating the dangling pointer.

v1 -> v2:
- Addressed automated review by Sashiko:
https://sashiko.dev/#/patchset/20260724023539.1596955-1-fredric.cover.lkernel%40gmail.com
Split into a 2-patch series to harden readers against NULL and ERR_PTR target hints so that clearing ce->tgthint does not cause NULL pointer dereferences.

Quoted 3 lines

Fredric Cover (2):
smb: client: harden DFS cache against invalid target hints
smb: client: clear ce->tgthint in free_tgts()

I will apply v3 patch-set to #for-next.
Thanks!

#6 lore raw

v3 cover smb: client: fix dangling pointer in DFS target hints

In reply to Fredric Cover

This series addresses a Use-After-Free bug where ce->tgthint was left pointing to freed memory after free_tgts() was called.

To fix this, Patch 1 hardens the DFS cache readers against ce->tgthint being NULL. Also, Patch 1 hardens callers of get_tgt_name(), which returns an error pointer when ce->tgthint is NULL.

Patch 2 clears ce->tgthint in free_tgts(), eliminating the dangling pointer.

v2 -> v3:
- Resent series with proper thread structure and subject headers.

v1 -> v2:
- Addressed automated review by Sashiko:
https://sashiko.dev/#/patchset/20260724023539.1596955-1-fredric.cover.lkernel%40gmail.com
- Split into a 2-patch series to harden readers against NULL and ERR_PTR target hints.

Fredric Cover (2):
smb: client: harden DFS cache against invalid target hints
smb: client: clear ce->tgthint in free_tgts()

 fs/smb/client/dfs_cache.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0
#7 lore raw

PATCH v3 1/2 smb: client: harden DFS cache against invalid target hints

Currently, get_tgt_name() returns ERR_PTR(-ENOENT) when ce->tgthint is NULL, and dfs_cache_noreq_update_tgthint() assumes ce->tgthint is always valid.

In preparation for clearing ce->tgthint in free_tgts(), harden callers of get_tgt_name() against ERR_PTR results and harden dfs_cache_noreq_update_tgthint() against NULL pointer dereferences.

---
 fs/smb/client/dfs_cache.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)
fs/smb/client/dfs_cache.c +24 -7
index 8cd93cd2f00f..c9ace4326a35 100644
@@ -869,13 +869,22 @@ int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nl
 		goto out_free_path;
 	}
 
-	if (ref)
-		rc = setup_referral(path, ce, ref, get_tgt_name(ce));
-	else
+	if (ref) {
+		char *target = get_tgt_name(ce);
+
+		if (IS_ERR(target)) {
+			rc = PTR_ERR(target);
+			goto out_unlock;
+		}
+		rc = setup_referral(path, ce, ref, target);
+	} else {
 		rc = 0;
+	}
+
 	if (!rc && tgt_list)
 		rc = get_targets(ce, tgt_list);
 
+out_unlock:
 	up_read(&htable_rw_lock);
 
 out_free_path:
@@ -915,10 +924,17 @@ int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
 		goto out_unlock;
 	}
 
-	if (ref)
-		rc = setup_referral(path, ce, ref, get_tgt_name(ce));
-	else
+	if (ref) {
+		char *target = get_tgt_name(ce);
+
+		if (IS_ERR(target)) {
+			rc = PTR_ERR(target);
+			goto out_unlock;
+		}
+		rc = setup_referral(path, ce, ref, target);
+	} else {
 		rc = 0;
+	}
 	if (!rc && tgt_list)
 		rc = get_targets(ce, tgt_list);
 
@@ -959,7 +975,8 @@ void dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt
 
 	t = READ_ONCE(ce->tgthint);
 
-	if (unlikely(!strcasecmp(it->it_name, t->name)))
+	/* Check 't' in case ce->tgthint was cleared by free_tgts() */
+	if (t && unlikely(!strcasecmp(it->it_name, t->name)))
 		goto out_unlock;
 
 	list_for_each_entry(t, &ce->tlist, list) {
Signature · 1 line
2.53.0
#8 lore raw

PATCH v3 2/2 smb: client: clear ce->tgthint in free_tgts()

In reply to Fredric Cover

When free_tgts() frees all structures in ce->tlist, ce->tgthint is left pointing to one of the freed cache_dfs_tgt structures.

If ce->tgthint is not reset before it is used later, it results in a use-after-free.

Set ce->tgthint to NULL in free_tgts() after the elements are freed to reflect that no elements remain.

Fixes 54be1f6c1c37 ("cifs: Add DFS cache routines")
Cc [email protected] # depends on: smb: client: harden DFS cache against invalid target hints
---
 fs/smb/client/dfs_cache.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/client/dfs_cache.c +2 -0
index c9ace4326a35..86dba25b7a5a 100644
@@ -122,6 +122,8 @@ static inline void free_tgts(struct cache_entry *ce)
 		kfree(t->name);
 		kfree(t);
 	}
+
+	WRITE_ONCE(ce->tgthint, NULL);
 }
 
 static inline void flush_cache_ent(struct cache_entry *ce)
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH v3 0/2] smb: client: fix dangling pointer in DFS target hints

In reply to Fredric Cover

Steve,
A gentle ping on this patch series. Let me know if any updates or revisions are needed.

Thanks,
Fredric Cover

Quoted 30 lines · Fredric Cover

This series addresses a Use-After-Free bug where ce->tgthint was left pointing to freed memory after free_tgts() was called.

To fix this, Patch 1 hardens the DFS cache readers against ce->tgthint being NULL. Also, Patch 1 hardens callers of get_tgt_name(), which returns an error pointer when ce->tgthint is NULL.

Patch 2 clears ce->tgthint in free_tgts(), eliminating the dangling pointer.

v2 -> v3:
- Resent series with proper thread structure and subject headers.

v1 -> v2:
- Addressed automated review by Sashiko:

   https://sashiko.dev/#/patchset/20260724023539.1596955-1-fredric.cover.lkernel%40gmail.com

- Split into a 2-patch series to harden readers against NULL and ERR_PTR target hints.

Fredric Cover (2):
smb: client: harden DFS cache against invalid target hints
smb: client: clear ce->tgthint in free_tgts()

 fs/smb/client/dfs_cache.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

--
2.53.0

reviewer Reviewed-by on the v3 series
#10 lore raw

Re: [PATCH v3 0/2] smb: client: fix dangling pointer in DFS target hints

In reply to Fredric Cover

It seems that patch 02 should be applied first.

Looks good to me.

Quoted 26 lines · Fredric Cover

This series addresses a Use-After-Free bug where ce->tgthint was left pointing to freed memory after free_tgts() was called.

To fix this, Patch 1 hardens the DFS cache readers against ce->tgthint being NULL. Also, Patch 1 hardens callers of get_tgt_name(), which returns an error pointer when ce->tgthint is NULL.

Patch 2 clears ce->tgthint in free_tgts(), eliminating the dangling pointer.

v2 -> v3:
- Resent series with proper thread structure and subject headers.

v1 -> v2:
- Addressed automated review by Sashiko:

    https://sashiko.dev/#/patchset/20260724023539.1596955-1-fredric.cover.lkernel%40gmail.com

- Split into a 2-patch series to harden readers against NULL and ERR_PTR target hints.

Fredric Cover (2):
smb: client: harden DFS cache against invalid target hints
smb: client: clear ce->tgthint in free_tgts()

  fs/smb/client/dfs_cache.c | 33 ++++++++++++++++++++++++++-------
  1 file changed, 26 insertions(+), 7 deletions(-)
Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en