ksmbd: vfs: fix truncate lock-range check for shrink/grow and avoid size==0 underflow
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): 2025110803-retrace-unnatural-127f@gregkh
PATCH 1/1 ksmbd: vfs: fix truncate lock-range check for shrink/grow and avoid size==0 underflow
ksmbd_vfs_truncate() uses check_lock_range() with arguments that are incorrect for shrink, and can underflow when size==0:
- For shrink, the code passed [inode->i_size, size-1], which is reversed.
- When size==0, "size-1" underflows to -1, so the range becomes [old_size, -1], effectively skipping the intended [0, old_size-1].
Fix by:
- Rejecting negative size with -EINVAL.
- For shrink (size < old): check [size, old-1].
- For grow (size > old): check [old, size-1].
- Skip lock check when size == old.
- Keep the return value on conflict as -EAGAIN (no noisy pr_err()).
This avoids the size==0 underflow and uses the correct range order, preserving byte-range lock semantics.
--- fs/smb/server/vfs.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
fs/smb/server/vfs.c +19 -9
@@ -825,17 +825,27 @@ int ksmbd_vfs_truncate(struct ksmbd_work *work,if (!work->tcon->posix_extensions) {struct inode *inode =file_inode (filp); -if (size < inode->i_size) { - err =check_lock_range (filp, size, - inode->i_size -1 ,WRITE ); - }else { - err =check_lock_range (filp, inode->i_size, - size -1 ,WRITE ); +loff_t old =i_size_read (inode); +loff_t start =0 , end =-1 ; +bool need_check = false; + +if (size <0 ) +return -EINVAL ; + +if (size < old) { + start = size; + end = old -1 ; + need_check = true; + }else if (size > old) { + start = old; + end = size -1 ; + need_check = true; } -if (err) { -pr_err ("failed due to lock\n" ); -return -EAGAIN ; +if (need_check) { + err =check_lock_range (filp, start, end,WRITE ); +if (err) +return -EAGAIN ; } }
Signature · 1 line
2.34.1
Namjae Jeon
maintainerQuoted 36 lines · Qianchang Zhao
ksmbd_vfs_truncate()usescheck_lock_range()with arguments that are incorrect for shrink, and can underflow when size==0:- For shrink, the code passed [
inode->i_size, size-1], which is reversed.
- When size==0, "size-1" underflows to -1, so the range becomes [old_size, -1], effectively skipping the intended [0, old_size-1].Fix by:
- Rejecting negative size with-EINVAL.
- For shrink (size < old): check [size, old-1].
- For grow (size > old): check [old, size-1].
- Skip lock check when size == old.
- Keep the return value on conflict as-EAGAIN(no noisypr_err()).This avoids the size==0 underflow and uses the correct range order, preserving byte-range lock semantics.
Reported-by Qianchang Zhao <[email protected]>Reported-by Zhitong Liu <[email protected]>Signed-off-by Qianchang Zhao <[email protected]>--- fs/smb/server/vfs.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)fs/smb/server/vfs.c +0
-2@@ -825,17 +825,27 @@ int ksmbd_vfs_truncate(struct ksmbd_work *work,if (!work->tcon->posix_extensions) {struct inode *inode =file_inode (filp); -if (size < inode->i_size) { - err = check_lock_range(filp, size,
fs/smb/server/vfs.c int ksmbd_vfs_truncate(struct ksmbd_work *work, · line 833
Quoted 10 lines
fs/smb/server/vfs.c +6
-4- inode->i_size - 1, WRITE); - }else { - err =check_lock_range (filp, inode->i_size, - size -1 ,WRITE ); +loff_t old =i_size_read (inode); +loff_t start =0 , end =-1 ; +bool need_check = false; + +if (size <0 ) +return -EINVAL ;
There is no case where size variable is negative.
Quoted 10 lines
fs/smb/server/vfs.c +9
-0+ +if (size < old) { + start = size; + end = old -1 ; + need_check = true; + }else if (size > old) { + start = old; + end = size -1 ; + need_check = true; }
fs/smb/server/vfs.c int ksmbd_vfs_truncate(struct ksmbd_work *work, · line 850
Quoted 10 lines
fs/smb/server/vfs.c +4
-3-if (err) { -pr_err ("failed due to lock\n" ); -return -EAGAIN ; +if (need_check) { + err =check_lock_range (filp, start, end,WRITE ); +if (err) +return -EAGAIN ; } }
Can't you just change it like this?
fs/smb/server/vfs.c +1 -1
@@ -828,7 +828,7 @@ int ksmbd_vfs_truncate(struct ksmbd_work *work,if (size < inode->i_size) { err =check_lock_range (filp, size, inode->i_size -1 ,WRITE ); - }else { + }else if (size > inode->i_size) { err =check_lock_range (filp, inode->i_size, size -1 ,WRITE ); }
Thanks.
Quoted 3 lines
fs/smb/server/vfs.c +0
-1--2.34.1
PATCH v2 1/1 ksmbd: vfs: skip lock-range check on equal size to avoid size==0 underflow
When size equals the current i_size (including 0), the code used to call check_lock_range(filp, i_size, size - 1, WRITE), which computes size - 1 and can underflow for size==0. Skip the equal case.
--- fs/smb/server/vfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/server/vfs.c +1 -1
@@ -828,7 +828,7 @@ int ksmbd_vfs_truncate(struct ksmbd_work *work,if (size < inode->i_size) { err =check_lock_range (filp, size, inode->i_size -1 ,WRITE ); - }else { + }else if (size > inode->i_size) { err =check_lock_range (filp, inode->i_size, size -1 ,WRITE ); }
Signature · 1 line
2.34.1
Re: [PATCH v2] ksmbd: vfs: skip lock-range check on equal size to avoid size==0 underflow
Quoted 9 lines · Qianchang Zhao
When size equals the current
i_size(including 0), the code used to callcheck_lock_range(filp, i_size, size - 1, WRITE), which computessize - 1and can underflow for size==0. Skip the equal case.Reported-by Qianchang Zhao <[email protected]>Reported-by Zhitong Liu <[email protected]>Signed-off-by Qianchang Zhao <[email protected]>
Applied it to #ksmbd-for-next-next.
Thanks!
Thanks for the guidance — I’ve sent v2 patch.
Best regards,
Qianchang
Quoted 90 lines · Namjae Jeon
Quoted 36 lines · Qianchang Zhao
ksmbd_vfs_truncate()usescheck_lock_range()with arguments that are incorrect for shrink, and can underflow when size==0:- For shrink, the code passed [
inode->i_size, size-1], which is reversed.
- When size==0, "size-1" underflows to -1, so the range becomes [old_size, -1], effectively skipping the intended [0, old_size-1].Fix by:
- Rejecting negative size with-EINVAL.
- For shrink (size < old): check [size, old-1].
- For grow (size > old): check [old, size-1].
- Skip lock check when size == old.
- Keep the return value on conflict as-EAGAIN(no noisypr_err()).This avoids the size==0 underflow and uses the correct range order, preserving byte-range lock semantics.
Reported-by Qianchang Zhao <[email protected]>Reported-by Zhitong Liu <[email protected]>Signed-off-by Qianchang Zhao <[email protected]>--- fs/smb/server/vfs.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)fs/smb/server/vfs.c +0
-2@@ -825,17 +825,27 @@ int ksmbd_vfs_truncate(struct ksmbd_work *work,if (!work->tcon->posix_extensions) {struct inode *inode =file_inode (filp); -if (size < inode->i_size) { - err = check_lock_range(filp, size,fs/smb/server/vfs.c int ksmbd_vfs_truncate(struct ksmbd_work *work, · line 833
Quoted 10 lines
fs/smb/server/vfs.c +6
-4- inode->i_size - 1, WRITE); - }else { - err =check_lock_range (filp, inode->i_size, - size -1 ,WRITE ); +loff_t old =i_size_read (inode); +loff_t start =0 , end =-1 ; +bool need_check = false; + +if (size <0 ) +return -EINVAL ;There is no case where size variable is negative.
Quoted 10 lines
fs/smb/server/vfs.c +9
-0+ +if (size < old) { + start = size; + end = old -1 ; + need_check = true; + }else if (size > old) { + start = old; + end = size -1 ; + need_check = true; }fs/smb/server/vfs.c int ksmbd_vfs_truncate(struct ksmbd_work *work, · line 850
Quoted 10 lines
fs/smb/server/vfs.c +4
-3-if (err) { -pr_err ("failed due to lock\n" ); -return -EAGAIN ; +if (need_check) { + err =check_lock_range (filp, start, end,WRITE ); +if (err) +return -EAGAIN ; } }Can't you just change it like this?
fs/smb/server/vfs.c +1
-1@@ -828,7 +828,7 @@ int ksmbd_vfs_truncate(struct ksmbd_work *work,if (size < inode->i_size) { err =check_lock_range (filp, size, inode->i_size -1 ,WRITE ); - }else { + }else if (size > inode->i_size) { err =check_lock_range (filp, inode->i_size, size -1 ,WRITE ); }Thanks.
Quoted 3 lines
fs/smb/server/vfs.c +0
-1--2.34.1