Skip to content

Commit 309b44e

Browse files
verivusaismfrench
authored andcommitted
ksmbd: fix memory leaks and NULL deref in smb2_lock()
smb2_lock() has three error handling issues after list_del() detaches smb_lock from lock_list at no_check_cl: 1) If vfs_lock_file() returns an unexpected error in the non-UNLOCK path, goto out leaks smb_lock and its flock because the out: handler only iterates lock_list and rollback_list, neither of which contains the detached smb_lock. 2) If vfs_lock_file() returns -ENOENT in the UNLOCK path, goto out leaks smb_lock and flock for the same reason. The error code returned to the dispatcher is also stale. 3) In the rollback path, smb_flock_init() can return NULL on allocation failure. The result is dereferenced unconditionally, causing a kernel NULL pointer dereference. Add a NULL check to prevent the crash and clean up the bookkeeping; the VFS lock itself cannot be rolled back without the allocation and will be released at file or connection teardown. Fix cases 1 and 2 by hoisting the locks_free_lock()/kfree() to before the if(!rc) check in the UNLOCK branch so all exit paths share one free site, and by freeing smb_lock and flock before goto out in the non-UNLOCK branch. Propagate the correct error code in both cases. Fix case 3 by wrapping the VFS unlock in an if(rlock) guard and adding a NULL check for locks_free_lock(rlock) in the shared cleanup. Found via call-graph analysis using sqry. Fixes: e2f3448 ("cifsd: add server-side procedures for SMB3") Cc: [email protected] Suggested-by: ChenXiaoSong <[email protected]> Signed-off-by: Werner Kasselman <[email protected]> Reviewed-by: ChenXiaoSong <[email protected]> Acked-by: Namjae Jeon <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 48623ec commit 309b44e

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

fs/smb/server/smb2pdu.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7592,14 +7592,15 @@ int smb2_lock(struct ksmbd_work *work)
75927592
rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
75937593
skip:
75947594
if (smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) {
7595+
locks_free_lock(flock);
7596+
kfree(smb_lock);
75957597
if (!rc) {
75967598
ksmbd_debug(SMB, "File unlocked\n");
75977599
} else if (rc == -ENOENT) {
75987600
rsp->hdr.Status = STATUS_NOT_LOCKED;
7601+
err = rc;
75997602
goto out;
76007603
}
7601-
locks_free_lock(flock);
7602-
kfree(smb_lock);
76037604
} else {
76047605
if (rc == FILE_LOCK_DEFERRED) {
76057606
void **argv;
@@ -7668,6 +7669,9 @@ int smb2_lock(struct ksmbd_work *work)
76687669
spin_unlock(&work->conn->llist_lock);
76697670
ksmbd_debug(SMB, "successful in taking lock\n");
76707671
} else {
7672+
locks_free_lock(flock);
7673+
kfree(smb_lock);
7674+
err = rc;
76717675
goto out;
76727676
}
76737677
}
@@ -7698,13 +7702,17 @@ int smb2_lock(struct ksmbd_work *work)
76987702
struct file_lock *rlock = NULL;
76997703

77007704
rlock = smb_flock_init(filp);
7701-
rlock->c.flc_type = F_UNLCK;
7702-
rlock->fl_start = smb_lock->start;
7703-
rlock->fl_end = smb_lock->end;
7705+
if (rlock) {
7706+
rlock->c.flc_type = F_UNLCK;
7707+
rlock->fl_start = smb_lock->start;
7708+
rlock->fl_end = smb_lock->end;
77047709

7705-
rc = vfs_lock_file(filp, F_SETLK, rlock, NULL);
7706-
if (rc)
7707-
pr_err("rollback unlock fail : %d\n", rc);
7710+
rc = vfs_lock_file(filp, F_SETLK, rlock, NULL);
7711+
if (rc)
7712+
pr_err("rollback unlock fail : %d\n", rc);
7713+
} else {
7714+
pr_err("rollback unlock alloc failed\n");
7715+
}
77087716

77097717
list_del(&smb_lock->llist);
77107718
spin_lock(&work->conn->llist_lock);
@@ -7714,7 +7722,8 @@ int smb2_lock(struct ksmbd_work *work)
77147722
spin_unlock(&work->conn->llist_lock);
77157723

77167724
locks_free_lock(smb_lock->fl);
7717-
locks_free_lock(rlock);
7725+
if (rlock)
7726+
locks_free_lock(rlock);
77187727
kfree(smb_lock);
77197728
}
77207729
out2:

0 commit comments

Comments
 (0)