Check for null system device

Check for null device in gsi service and return invalid fd
when device is null. Adding checks for invalid fd.

Test: atest vts_gsi_boot_test
Test: m gsi_service_fuzzer && adb sync data && adb shell /data/fuzz/x86_64/gsi_service_fuzzer/gsi_service_fuzzer -runs=10000
Bug: 309822660
Change-Id: Iaea180a0c1dac77bdf99c432c795633ceb849bd1
diff --git a/gsi_service.cpp b/gsi_service.cpp
index 18e97ff..113fb11 100644
--- a/gsi_service.cpp
+++ b/gsi_service.cpp
@@ -505,6 +505,12 @@
         return binder::Status::ok();
     }
     int fd = installer_->GetPartitionFd();
+    if (fd == -1) {
+        *_aidl_return = INSTALL_ERROR_GENERIC;
+        return binder::Status::fromExceptionCode(binder::Status::EX_ILLEGAL_STATE,
+                                                 "Failed to get valid partition fd");
+    }
+
     if (!GetAvbPublicKeyFromFd(fd, dst)) {
         LOG(ERROR) << "Failed to extract AVB public key";
         *_aidl_return = INSTALL_ERROR_GENERIC;
diff --git a/partition_installer.cpp b/partition_installer.cpp
index ebd3b64..126d50d 100644
--- a/partition_installer.cpp
+++ b/partition_installer.cpp
@@ -256,6 +256,9 @@
 }
 
 int PartitionInstaller::GetPartitionFd() {
+    if (!system_device_) {
+        return -1;
+    }
     return system_device_->fd();
 }
 
@@ -313,7 +316,13 @@
                    << (size_ - gsi_bytes_written_) << " bytes";
         return IGsiService::INSTALL_ERROR_GENERIC;
     }
-    if (system_device_ != nullptr && fsync(GetPartitionFd())) {
+    int fd = GetPartitionFd();
+    if (fd == -1) {
+        PLOG(ERROR) << "Failed to get partition fd";
+        return IGsiService::INSTALL_ERROR_GENERIC;
+    }
+
+    if (system_device_ != nullptr && fsync(fd)) {
         PLOG(ERROR) << "fsync failed for " << GetBackingFile(name_);
         return IGsiService::INSTALL_ERROR_GENERIC;
     }