util: Detect more I/O errors

Many of GRUB's utilities don't check anywhere near all the possible
write errors.  For example, if grub-install runs out of space when
copying a file, it won't notice.  There were missing checks for the
return values of write, fflush, fsync, and close (or the equivalents on
other OSes), all of which must be checked.

I tried to be consistent with the existing logging practices of the
various hostdisk implementations, but they weren't entirely consistent
to start with so I used my judgement.  The result at least looks
reasonable on GNU/Linux when I provoke a write error:

  Installing for x86_64-efi platform.
  grub-install: error: cannot copy `/usr/lib/grub/x86_64-efi-signed/grubx64.efi.signed' to `/boot/efi/EFI/debian/grubx64.efi': No space left on device.

There are more missing checks in other utilities, but this should fix
the most critical ones.

Fixes Debian bug #922741.

Signed-off-by: Colin Watson <cjwatson@ubuntu.com>
Reviewed-by: Steve McIntyre <93sam@debian.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Colin Watson 2019-02-27 09:10:08 +00:00 committed by Daniel Kiper
parent 222cb8f6df
commit 62daa27056
10 changed files with 90 additions and 45 deletions

View file

@ -55,7 +55,8 @@ grub_util_create_envblk_file (const char *name)
strerror (errno));
grub_util_file_sync (fp);
if (grub_util_file_sync (fp) < 0)
grub_util_error (_("cannot sync `%s': %s"), namenew, strerror (errno));
free (buf);
fclose (fp);

View file

@ -197,7 +197,8 @@ write_envblk (const char *name, grub_envblk_t envblk)
grub_util_error (_("cannot write to `%s': %s"), name,
strerror (errno));
grub_util_file_sync (fp);
if (grub_util_file_sync (fp) < 0)
grub_util_error (_("cannot sync `%s': %s"), name, strerror (errno));
fclose (fp);
}

View file

@ -112,11 +112,16 @@ grub_install_copy_file (const char *src,
r = grub_util_fd_read (in, grub_install_copy_buffer, GRUB_INSTALL_COPY_BUFFER_SIZE);
if (r <= 0)
break;
grub_util_fd_write (out, grub_install_copy_buffer, r);
r = grub_util_fd_write (out, grub_install_copy_buffer, r);
if (r <= 0)
break;
}
grub_util_fd_sync (out);
grub_util_fd_close (in);
grub_util_fd_close (out);
if (grub_util_fd_sync (out) < 0)
r = -1;
if (grub_util_fd_close (in) < 0)
r = -1;
if (grub_util_fd_close (out) < 0)
r = -1;
if (r < 0)
grub_util_error (_("cannot copy `%s' to `%s': %s"),
@ -526,7 +531,8 @@ grub_install_make_image_wrap (const char *dir, const char *prefix,
grub_install_make_image_wrap_file (dir, prefix, fp, outname,
memdisk_path, config_path,
mkimage_target, note);
grub_util_file_sync (fp);
if (grub_util_file_sync (fp) < 0)
grub_util_error (_("cannot sync `%s': %s"), outname, strerror (errno));
fclose (fp);
}

View file

@ -311,8 +311,12 @@ main (int argc, char *argv[])
arguments.image_target, arguments.note,
arguments.comp, arguments.dtb);
grub_util_file_sync (fp);
fclose (fp);
if (grub_util_file_sync (fp) < 0)
grub_util_error (_("cannot sync `%s': %s"), arguments.output ? : "stdout",
strerror (errno));
if (fclose (fp) == EOF)
grub_util_error (_("cannot close `%s': %s"), arguments.output ? : "stdout",
strerror (errno));
for (i = 0; i < arguments.nmodules; i++)
free (arguments.modules[i]);

View file

@ -728,8 +728,10 @@ unable_to_embed:
!= GRUB_DISK_SECTOR_SIZE * 2)
grub_util_error (_("cannot write to `%s': %s"),
core_path, strerror (errno));
grub_util_fd_sync (fp);
grub_util_fd_close (fp);
if (grub_util_fd_sync (fp) < 0)
grub_util_error (_("cannot sync `%s': %s"), core_path, strerror (errno));
if (grub_util_fd_close (fp) < 0)
grub_util_error (_("cannot close `%s': %s"), core_path, strerror (errno));
grub_util_biosdisk_flush (root_dev->disk);
grub_disk_cache_invalidate_all ();