summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan Reid <dgreid@chromium.org>2019-06-28 15:19:43 +1000
committerCommit Bot <commit-bot@chromium.org>2019-07-09 17:46:17 +0000
commiteecbccc4d9d70b2fd63681a2b3ced6a6aafe81bb (patch)
tree7f71cbb71c30490e93a95ee6ab7c5fb9b4118ade
parentbb5a4f1843361518412d5cd4036e31fb65f36e33 (diff)
downloadcrosvm-eecbccc4d9d70b2fd63681a2b3ced6a6aafe81bb.tar
crosvm-eecbccc4d9d70b2fd63681a2b3ced6a6aafe81bb.tar.gz
crosvm-eecbccc4d9d70b2fd63681a2b3ced6a6aafe81bb.tar.bz2
crosvm-eecbccc4d9d70b2fd63681a2b3ced6a6aafe81bb.tar.lz
crosvm-eecbccc4d9d70b2fd63681a2b3ced6a6aafe81bb.tar.xz
crosvm-eecbccc4d9d70b2fd63681a2b3ced6a6aafe81bb.tar.zst
crosvm-eecbccc4d9d70b2fd63681a2b3ced6a6aafe81bb.zip
qcow: Avoid overflow when taking ceiling of division
The extra % operation will be slower, but none of these divisions are in
hot paths. They are only used during setup. Many of these operations
take untrusted input from the disk file, so need to be hardened.

BUG=979458
TEST=unit tests still pass

Signed-off-by: Dylan Reid <dgreid@chromium.org>
Change-Id: I0e93c73b345faf643da53ea41bde3349d756bdc7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1679891
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
-rw-r--r--qcow/src/qcow.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/qcow/src/qcow.rs b/qcow/src/qcow.rs
index 7528773..ceaf130 100644
--- a/qcow/src/qcow.rs
+++ b/qcow/src/qcow.rs
@@ -1554,12 +1554,12 @@ fn offset_is_cluster_boundary(offset: u64, cluster_bits: u32) -> Result<()> {
 
 // Ceiling of the division of `dividend`/`divisor`.
 fn div_round_up_u64(dividend: u64, divisor: u64) -> u64 {
-    (dividend + divisor - 1) / divisor
+    dividend / divisor + if dividend % divisor != 0 { 1 } else { 0 }
 }
 
 // Ceiling of the division of `dividend`/`divisor`.
 fn div_round_up_u32(dividend: u32, divisor: u32) -> u32 {
-    (dividend + divisor - 1) / divisor
+    dividend / divisor + if dividend % divisor != 0 { 1 } else { 0 }
 }
 
 fn convert_copy<R, W>(reader: &mut R, writer: &mut W, offset: u64, size: u64) -> Result<()>