diff mbox series

[v2,CIFS] avoid signed integer overflow in calculating blocks

Message ID CAH2r5muan8hTn6Wn2NGgPM3NP5MQ_nd4HU6D7ofLgJk9vsb6ag@mail.gmail.com (mailing list archive)
State New, archived
Headers show
Series [v2,CIFS] avoid signed integer overflow in calculating blocks | expand

Commit Message

Steve French Aug. 12, 2021, 1:20 p.m. UTC
Adding fix for one more place where the same error can occur

xfstest generic/525 can generate the following warning:

 UBSAN: signed-integer-overflow in fs/cifs/file.c:2644:31
 9223372036854775807 + 511 cannot be represented in type 'long long int'

 Call Trace:
  dump_stack+0x8d/0xb5
  ubsan_epilogue+0x5/0x50
  handle_overflow+0xa3/0xb0
  cifs_write_end+0x424/0x440 [cifs]
  generic_perform_write+0xef/0x190

due to overflowing loff_t (a signed 64 bit) when it is rounded up
to calculate number of 512 byte blocks in a file in two places.

Signed-off-by: Steve French <stfrench@microsoft.com>
---
 fs/cifs/file.c  | 3 ++-
 fs/cifs/inode.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

  * The man page of truncate says if the size changed,
diff mbox series

Patch

diff --git a/fs/cifs/file.c b/fs/cifs/file.c
index 0166f39f1888..3cc17871471a 100644
--- a/fs/cifs/file.c
+++ b/fs/cifs/file.c
@@ -2641,7 +2641,8 @@  static int cifs_write_end(struct file *file,
struct address_space *mapping,
  spin_lock(&inode->i_lock);
  if (pos > inode->i_size) {
  i_size_write(inode, pos);
- inode->i_blocks = (512 - 1 + pos) >> 9;
+ /* round up to block boundary, avoid overflow loff_t */
+ inode->i_blocks = ((__u64)pos + (512 - 1)) >> 9;
  }
  spin_unlock(&inode->i_lock);
  }
diff --git a/fs/cifs/inode.c b/fs/cifs/inode.c
index 65f8a70cece3..f1dbcbc79abb 100644
--- a/fs/cifs/inode.c
+++ b/fs/cifs/inode.c
@@ -2631,7 +2631,7 @@  cifs_set_file_size(struct inode *inode, struct
iattr *attrs,
  * this is best estimate we have for blocks allocated for a file
  * Number of blocks must be rounded up so size 1 is not 0 blocks
  */
- inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
+ inode->i_blocks = ((__u64)attrs->ia_size + (512 - 1)) >> 9;

  /*