Skip to contentSkip to conversation

smb/server: fix some refcount leaks

PATCH Applied [email protected] 4 participants 12 messages Dec 29, 2025 – Feb 27

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

v2 cover smb/server: fix some refcount leaks

From: ChenXiaoSong <[email protected]>

v1: https://lore.kernel.org/linux-cifs/[email protected]/
v1->v2:
- Call ksmbd_put_durable_fd() immediately after ksmbd_vfs_getattr() has completed.

The following patches from v1 have already been merged into ksmbd.git ksmbd-for-next-next:
- 0d762babd1db smb/server: fix refcount leak in parse_durable_handle_context()

My dear team member, ZhangGuoDong, has caught some refcount leak issues.

Other patches pending review (including those from others) can be found at the following link:
https://chenxiaosong.com/en/smb-patch.html

ZhangGuoDong (1):
smb/server: fix refcount leak in smb2_open()

 fs/smb/server/smb2pdu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/1 smb/server: fix refcount leak in smb2_open()

From: ZhangGuoDong <[email protected]>

When ksmbd_vfs_getattr() fails, the reference count of ksmbd_file must be released.

---
 fs/smb/server/smb2pdu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/server/smb2pdu.c +1 -1
index 6a966c696f7d..dc730fe348e4 100644
@@ -3007,10 +3007,10 @@ int smb2_open(struct ksmbd_work *work)
 			file_info = FILE_OPENED;
 
 			rc = ksmbd_vfs_getattr(&fp->filp->f_path, &stat);
+			ksmbd_put_durable_fd(fp);
 			if (rc)
 				goto err_out2;
 
-			ksmbd_put_durable_fd(fp);
 			goto reconnected_fp;
 		}
 	} else if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Signature · 1 line
2.43.0
maintainer Applied
#2 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

Quoted 9 lines · On Mon, Dec 29, 2025 at 12:16 PM <[email protected]> wrote:

From: ZhangGuoDong <[email protected]>

When ksmbd_vfs_getattr() fails, the reference count of ksmbd_file must be released.

Applied it to #ksmbd-for-next-next.
Thanks!

#3 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

In reply to [email protected]

Hi,

Quoted 19 lines · On Mon, Dec 29, 2025 at 11:15:18AM +0800, [email protected]…

From: ZhangGuoDong <[email protected]>

When ksmbd_vfs_getattr() fails, the reference count of ksmbd_file must be released.

---
 fs/smb/server/smb2pdu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/server/smb2pdu.c +0 -0
index 6a966c696f7d..dc730fe348e4 100644
@@ -3007,10 +3007,10 @@ int smb2_open(struct ksmbd_work *work)
 			file_info = FILE_OPENED;

fs/smb/server/smb2pdu.c int smb2_open(struct ksmbd_work *work) · line 3016

Quoted 10 lines
fs/smb/server/smb2pdu.c +1 -1
 
 			rc = ksmbd_vfs_getattr(&fp->filp->f_path, &stat);
+			ksmbd_put_durable_fd(fp);
 			if (rc)
 				goto err_out2;
 
-			ksmbd_put_durable_fd(fp);
 			goto reconnected_fp;
 		}
 	} else if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)

Running an experimental AI agent on this patch produced the following feedback:

This isn't a bug introduced by your patch, but it looks like there is still a resource leak here. If ksmbd_override_fsids() fails, we jump to err_out2. At that point, fp is NULL because it hasn't been assigned dh_info.fp yet, so ksmbd_fd_put(work, fp) will not be called. However, dh_info.fp was already inserted into the session file table by ksmbd_reopen_durable_fd(), so it will leak in the session file table until the session is closed. Should fp = dh_info.fp; be moved before the ksmbd_override_fsids() check?

PTAL and let me know if it has a point or if it is missing something.

Thanks!

Guenter

reviewer
#4 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

Hi Guenter,

Thank you for taking the time to look into this issue.

I reviewed the relevant code in more detail and did not find any leak.

Both ksmbd_put_durable_fd() and ksmbd_fd_put() will eventually call __ksmbd_remove_fd() (remove fd from file table).

If my understanding is incorrect, please let me know.

Thanks,
ChenXiaoSong <[email protected]>

Quoted 12 lines · Guenter Roeck

Running an experimental AI agent on this patch produced the following feedback:

This isn't a bug introduced by your patch, but it looks like there is still a resource leak here. If ksmbd_override_fsids() fails, we jump to err_out2. At that point, fp is NULL because it hasn't been assigned dh_info.fp yet, so ksmbd_fd_put(work, fp) will not be called. However, dh_info.fp was already inserted into the session file table by ksmbd_reopen_durable_fd(), so it will leak in the session file table until the session is closed. Should fp = dh_info.fp; be moved before the ksmbd_override_fsids() check?

PTAL and let me know if it has a point or if it is missing something.

#5 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

Hi,

Quoted 8 lines · ChenXiaoSong

Hi Guenter,

Thank you for taking the time to look into this issue.

I reviewed the relevant code in more detail and did not find any leak.

Both ksmbd_put_durable_fd() and ksmbd_fd_put() will eventually call __ksmbd_remove_fd() (remove fd from file table).

Thanks a lot for taking the time and checking the code.

Quoted 2 lines

If my understanding is incorrect, please let me know.

Just to make sure, I'll have another look at the code and provide your feedback to the AI agent.

Again, thanks a lot for your time.

Guenter

Quoted 17 lines

Thanks,
ChenXiaoSong <[email protected]>

Quoted 12 lines · Guenter Roeck

Running an experimental AI agent on this patch produced the following feedback:

This isn't a bug introduced by your patch, but it looks like there is still a resource leak here. If ksmbd_override_fsids() fails, we jump to err_out2. At that point, fp is NULL because it hasn't been assigned dh_info.fp yet, so ksmbd_fd_put(work, fp) will not be called. However, dh_info.fp was already inserted into the session file table by ksmbd_reopen_durable_fd(), so it will leak in the session file table until the session is closed. Should fp = dh_info.fp; be moved before the ksmbd_override_fsids() check?

PTAL and let me know if it has a point or if it is missing something.

#6 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

In reply to ChenXiaoSong

Hi,

Quoted 8 lines · ChenXiaoSong

Hi Guenter,

Thank you for taking the time to look into this issue.

I reviewed the relevant code in more detail and did not find any leak.

Both ksmbd_put_durable_fd() and ksmbd_fd_put() will eventually call __ksmbd_remove_fd() (remove fd from file table).

Sorry for bothering you again. Are you sure ?

ksmbd_put_durable_fd() calls __ksmbd_close_fd() with NULL first parameter. __ksmbd_close_fd() only calls __ksmbd_remove_fd() if ft (the first parameter) is not NULL.

ksmbd_fd_put() does call __ksmbd_remove_fd() with first parameter, but ...

Quoted 13 lines

If my understanding is incorrect, please let me know.

Thanks,
ChenXiaoSong <[email protected]>

Quoted 7 lines · Guenter Roeck

Running an experimental AI agent on this patch produced the following feedback:

This isn't a bug introduced by your patch, but it looks like there is still a resource leak here. If ksmbd_override_fsids() fails, we jump to err_out2. At that point, fp is NULL because it hasn't been assigned dh_info.fp yet, so ksmbd_fd_put(work, fp) will not be called. However, dh_info.fp was

the AI is trying to make the point that ksmbd_fd_put() will not be called because fp == NULL.

Thanks,
Guenter

Quoted 6 lines
Quoted 5 lines

already inserted into the session file table by ksmbd_reopen_durable_fd(), so it will leak in the session file table until the session is closed. Should fp = dh_info.fp; be moved before the ksmbd_override_fsids() check?

PTAL and let me know if it has a point or if it is missing something.

reviewer
#7 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

Hi Guenter,

Sorry for the late reply. I had some family matters to take care of yesterday.

Please see the following process:

```
smb2_open
  smb2_check_durable_oplock
    opinfo_get(fp) // inc refcount
  ksmbd_reopen_durable_fd
    __open_id(&work->sess->file_table, fp,
      idr_alloc_cyclic(ft->idr, fp, ...)
      __open_id_set(fp, id, type); // insert into file table
  ksmbd_override_fsids // fail
  ksmbd_put_durable_fd
    __ksmbd_close_fd
      __ksmbd_remove_fd  // remove dh_info.fp from file table
  // dh_info.fp has already been removed from the file table
  ksmbd_fd_put(..., fp) // fp == NULL
```

Thanks,
ChenXiaoSong <[email protected]>

Quoted 46 lines · Guenter Roeck

Hi,

Quoted 8 lines · ChenXiaoSong

Hi Guenter,

Thank you for taking the time to look into this issue.

I reviewed the relevant code in more detail and did not find any leak.

Both ksmbd_put_durable_fd() and ksmbd_fd_put() will eventually call __ksmbd_remove_fd() (remove fd from file table).

Sorry for bothering you again. Are you sure ?

ksmbd_put_durable_fd() calls __ksmbd_close_fd() with NULL first parameter. __ksmbd_close_fd() only calls __ksmbd_remove_fd() if ft (the first parameter) is not NULL.

ksmbd_fd_put() does call __ksmbd_remove_fd() with first parameter, but ...

Quoted 13 lines

If my understanding is incorrect, please let me know.

Thanks,
ChenXiaoSong <[email protected]>

Quoted 7 lines · Guenter Roeck

Running an experimental AI agent on this patch produced the following feedback:

This isn't a bug introduced by your patch, but it looks like there is still a resource leak here. If ksmbd_override_fsids() fails, we jump to err_out2. At that point, fp is NULL because it hasn't been assigned dh_info.fp yet, so ksmbd_fd_put(work, fp) will not be called. However, dh_info.fp was

the AI is trying to make the point that ksmbd_fd_put() will not be called because fp == NULL.

Thanks,
Guenter

Quoted 6 lines
Quoted 5 lines

already inserted into the session file table by ksmbd_reopen_durable_fd(), so it will leak in the session file table until the session is closed. Should fp = dh_info.fp; be moved before the ksmbd_override_fsids() check?

PTAL and let me know if it has a point or if it is missing something.

reviewer
#8 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

Hi Namjae,

By the way, smb2_open() is over 900 lines long, and we have already encountered several memory leaks in it, and there may be more. Perhaps we should consider refactoring it to make it easier to maintain.

What do you think?

Thanks,
ChenXiaoSong <[email protected]>

Quoted 21 lines · ChenXiaoSong

Hi Guenter,

Sorry for the late reply. I had some family matters to take care of yesterday.

Please see the following process:

```
smb2_open
   smb2_check_durable_oplock
     opinfo_get(fp) // inc refcount
   ksmbd_reopen_durable_fd
     __open_id(&work->sess->file_table, fp,
       idr_alloc_cyclic(ft->idr, fp, ...)
       __open_id_set(fp, id, type); // insert into file table
   ksmbd_override_fsids // fail
   ksmbd_put_durable_fd
     __ksmbd_close_fd
       __ksmbd_remove_fd  // remove dh_info.fp from file table
   // dh_info.fp has already been removed from the file table
   ksmbd_fd_put(..., fp) // fp == NULL
```
maintainer
#9 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

Quoted 8 lines · ChenXiaoSong

Hi Namjae,

By the way, smb2_open() is over 900 lines long, and we have already encountered several memory leaks in it, and there may be more. Perhaps we should consider refactoring it to make it easier to maintain.

What do you think?

Agreed, I think we need to factor it out with a helper function... Thanks.

Quoted 27 lines

Thanks,
ChenXiaoSong <[email protected]>

Quoted 22 lines · ChenXiaoSong

Hi Guenter,

Sorry for the late reply. I had some family matters to take care of yesterday.

Please see the following process:

```
smb2_open
   smb2_check_durable_oplock
     opinfo_get(fp) // inc refcount
   ksmbd_reopen_durable_fd
     __open_id(&work->sess->file_table, fp,
       idr_alloc_cyclic(ft->idr, fp, ...)
       __open_id_set(fp, id, type); // insert into file table
   ksmbd_override_fsids // fail
   ksmbd_put_durable_fd
     __ksmbd_close_fd
       __ksmbd_remove_fd  // remove dh_info.fp from file table
   // dh_info.fp has already been removed from the file table
   ksmbd_fd_put(..., fp) // fp == NULL
```
#10 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

In reply to ChenXiaoSong
Quoted 6 lines · ChenXiaoSong

Hi Guenter,

Sorry for the late reply. I had some family matters to take care of yesterday.

Please see the following process:

Quoted 12 lines

```
smb2_open
  smb2_check_durable_oplock
    opinfo_get(fp) // inc refcount ksmbd_reopen_durable_fd __open_id(&work->sess->file_table, fp, idr_alloc_cyclic(ft->idr, fp, ...) __open_id_set(fp, id, type); // insert into file table ksmbd_override_fsids // fail
  ksmbd_put_durable_fd
    __ksmbd_close_fd
      __ksmbd_remove_fd  // remove dh_info.fp from file table

Yes, but:

void ksmbd_put_durable_fd(struct ksmbd_file *fp)

{
        if (!atomic_dec_and_test(&fp->refcount))
                return;

        __ksmbd_close_fd(NULL, fp);	// first parameter (ft) is NULL
}

and:

static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) {
...

	if (ft)	// again, ft is NULL, so __ksmbd_remove_fd() is not called.
                __ksmbd_remove_fd(ft, fp);
Quoted 1 line

  // dh_info.fp has already been removed from the file table

I don't think so, but maybe I am missing something. I'll let this go.

Thanks, and sorry for the noise.

Guenter

Quoted 53 lines

  ksmbd_fd_put(..., fp) // fp == NULL
```

Thanks,
ChenXiaoSong <[email protected]>

Quoted 46 lines · Guenter Roeck

Hi,

Quoted 8 lines · ChenXiaoSong

Hi Guenter,

Thank you for taking the time to look into this issue.

I reviewed the relevant code in more detail and did not find any leak.

Both ksmbd_put_durable_fd() and ksmbd_fd_put() will eventually call __ksmbd_remove_fd() (remove fd from file table).

Sorry for bothering you again. Are you sure ?

ksmbd_put_durable_fd() calls __ksmbd_close_fd() with NULL first parameter. __ksmbd_close_fd() only calls __ksmbd_remove_fd() if ft (the first parameter) is not NULL.

ksmbd_fd_put() does call __ksmbd_remove_fd() with first parameter, but ...

Quoted 13 lines

If my understanding is incorrect, please let me know.

Thanks,
ChenXiaoSong <[email protected]>

Quoted 7 lines · Guenter Roeck

Running an experimental AI agent on this patch produced the following feedback:

This isn't a bug introduced by your patch, but it looks like there is still a resource leak here. If ksmbd_override_fsids() fails, we jump to err_out2. At that point, fp is NULL because it hasn't been assigned dh_info.fp yet, so ksmbd_fd_put(work, fp) will not be called. However, dh_info.fp was

the AI is trying to make the point that ksmbd_fd_put() will not be called because fp == NULL.

Thanks,
Guenter

Quoted 6 lines
Quoted 5 lines

already inserted into the session file table by ksmbd_reopen_durable_fd(), so it will leak in the session file table until the session is closed. Should fp = dh_info.fp; be moved before the ksmbd_override_fsids() check?

PTAL and let me know if it has a point or if it is missing something.

reviewer
#11 lore raw

Re: [PATCH v2 1/1] smb/server: fix refcount leak in smb2_open()

Yes, you are correct. Thank you for pointing this out.

Could you submit a patch to fix this?

Thanks,
ChenXiaoSong <[email protected]>

Quoted 8 lines · Guenter Roeck

void ksmbd_put_durable_fd(struct ksmbd_file *fp)
{
        if (!atomic_dec_and_test(&fp->refcount))
                return;

        __ksmbd_close_fd(NULL, fp);    // first parameter (ft) is NULL
}