disk: limit maximum nesting depth

Impose a limit on the maximum nesting of file formats that can open more
files. For example, a qcow2 file can have a backing file, which could be
another qcow2 file with a backing file (or even the same file as the
original), potentially causing unbounded recursion.

BUG=b:198326611
TEST=cros_fuzz

Change-Id: I2f8573a7c71c6d8b310f2c2a75a240f2f8f0c9b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3146214
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Cody Schuffelen <schuffelen@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
diff --git a/src/main.rs b/src/main.rs
index 9e8190d..7a1dba1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -41,7 +41,7 @@
 use devices::ProtectionType;
 #[cfg(feature = "audio")]
 use devices::{Ac97Backend, Ac97Parameters};
-use disk::QcowFile;
+use disk::{self, QcowFile};
 #[cfg(feature = "composite-disk")]
 use disk::{
     create_composite_disk, create_disk_file, create_zero_filler, ImagePartitionType, PartitionInfo,
@@ -2449,7 +2449,7 @@
             if let [label, path] = partition_arg.split(":").collect::<Vec<_>>()[..] {
                 let partition_file = File::open(path)
                     .map_err(|e| error!("Failed to open partition image: {}", e))?;
-                let size = create_disk_file(partition_file)
+                let size = create_disk_file(partition_file, disk::MAX_NESTING_DEPTH)
                     .map_err(|e| error!("Failed to create DiskFile instance: {}", e))?
                     .get_len()
                     .map_err(|e| error!("Failed to get length of partition image: {}", e))?;
@@ -2559,9 +2559,11 @@
             error!("Failed to create qcow file at '{}': {}", file_path, e);
         })?,
         (None, Some(backing_file)) => {
-            QcowFile::new_from_backing(file, &backing_file).map_err(|e| {
-                error!("Failed to create qcow file at '{}': {}", file_path, e);
-            })?
+            QcowFile::new_from_backing(file, &backing_file, disk::MAX_NESTING_DEPTH).map_err(
+                |e| {
+                    error!("Failed to create qcow file at '{}': {}", file_path, e);
+                },
+            )?
         }
         _ => unreachable!(),
     };