From 597864b7cde39709624b6e1975b9d6f8ea4ce559 Mon Sep 17 00:00:00 2001 From: krw Date: Thu, 8 Oct 2015 14:50:38 +0000 Subject: [PATCH] Refactor fileprefix() and filecopy() to use warn() instead of err() to display error message, and to return error indications (NULL and -1 respectively). Use the error indications in write_efisystem() to unwind in the face of more error conditions. In other cases just exit(1) to emulation current behaviour. ok deraadt@ --- usr.sbin/installboot/i386_installboot.c | 27 +++++--- usr.sbin/installboot/installboot.c | 12 +++- usr.sbin/installboot/installboot.h | 4 +- usr.sbin/installboot/landisk_installboot.c | 7 +- usr.sbin/installboot/sparc64_installboot.c | 7 +- usr.sbin/installboot/util.c | 78 ++++++++++++++-------- 6 files changed, 92 insertions(+), 43 deletions(-) diff --git a/usr.sbin/installboot/i386_installboot.c b/usr.sbin/installboot/i386_installboot.c index 66333477e21..6b0c96d7edb 100644 --- a/usr.sbin/installboot/i386_installboot.c +++ b/usr.sbin/installboot/i386_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: i386_installboot.c,v 1.11 2015/10/07 03:06:46 krw Exp $ */ +/* $OpenBSD: i386_installboot.c,v 1.12 2015/10/08 14:50:38 krw Exp $ */ /* $NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */ /* @@ -145,11 +145,14 @@ md_installboot(int devfd, char *dev) } bootldr = fileprefix(root, bootldr); + if (bootldr == NULL) + exit(1); if (verbose) fprintf(stderr, "%s %s to %s\n", (nowrite ? "would copy" : "copying"), stage2, bootldr); if (!nowrite) - filecopy(stage2, bootldr); + if (filecopy(stage2, bootldr) == -1) + exit(1); /* Get bootstrap parameters to patch into proto. */ if (getbootparams(bootldr, devfd, &dl) != 0) @@ -318,12 +321,19 @@ write_efisystem(struct disklabel *dl, char part) goto umount; } src = fileprefix(root, "/usr/mdec/BOOTIA32.EFI"); + if (src == NULL) { + rslt = -1; + goto umount; + } srclen = strlen(src); if (verbose) fprintf(stderr, "%s %s to %s\n", (nowrite ? "would copy" : "copying"), src, dst); - if (!nowrite) - filecopy(src, dst); + if (!nowrite) { + rslt = filecopy(src, dst); + if (rslt == -1) + goto umount; + } src[srclen - strlen("/BOOTIA32.EFI")] = '\0'; dst[pathlen] = '\0'; @@ -340,11 +350,12 @@ write_efisystem(struct disklabel *dl, char part) if (verbose) fprintf(stderr, "%s %s to %s\n", (nowrite ? "would copy" : "copying"), src, dst); - if (!nowrite) - filecopy(src, dst); + if (!nowrite) { + rslt = filecopy(src, dst); + if (rslt == -1) + goto umount; + } - free(src); - src = NULL; rslt = 0; umount: diff --git a/usr.sbin/installboot/installboot.c b/usr.sbin/installboot/installboot.c index f650d191a28..f62f6b8c54b 100644 --- a/usr.sbin/installboot/installboot.c +++ b/usr.sbin/installboot/installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.c,v 1.6 2015/10/03 16:56:52 krw Exp $ */ +/* $OpenBSD: installboot.c,v 1.7 2015/10/08 14:50:38 krw Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing @@ -86,10 +86,16 @@ main(int argc, char **argv) /* Prefix stages with root, unless they were user supplied. */ if (verbose) fprintf(stderr, "Using %s as root\n", root); - if (argc <= 1 && stage1 != NULL) + if (argc <= 1 && stage1 != NULL) { stage1 = fileprefix(root, stage1); - if (argc <= 2 && stage2 != NULL) + if (stage1 == NULL) + exit(1); + } + if (argc <= 2 && stage2 != NULL) { stage2 = fileprefix(root, stage2); + if (stage2 == NULL) + exit(1); + } if ((devfd = opendev(dev, (nowrite ? O_RDONLY : O_RDWR), OPENDEV_PART, &realdev)) < 0) diff --git a/usr.sbin/installboot/installboot.h b/usr.sbin/installboot/installboot.h index 73a9594377c..af23ea5f55c 100644 --- a/usr.sbin/installboot/installboot.h +++ b/usr.sbin/installboot/installboot.h @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.h,v 1.4 2014/01/18 03:07:05 jsing Exp $ */ +/* $OpenBSD: installboot.h,v 1.5 2015/10/08 14:50:38 krw Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing * @@ -27,7 +27,7 @@ extern char *stage2; void bootstrap(int, char *, char *); #endif -void filecopy(const char *, const char *); +int filecopy(const char *, const char *); char *fileprefix(const char *, const char *); void md_init(void); diff --git a/usr.sbin/installboot/landisk_installboot.c b/usr.sbin/installboot/landisk_installboot.c index 3783c9325c3..43ef3116990 100644 --- a/usr.sbin/installboot/landisk_installboot.c +++ b/usr.sbin/installboot/landisk_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: landisk_installboot.c,v 1.2 2015/10/05 04:30:35 miod Exp $ */ +/* $OpenBSD: landisk_installboot.c,v 1.3 2015/10/08 14:50:38 krw Exp $ */ /* * Copyright (c) 2013 Joel Sing @@ -42,8 +42,11 @@ md_installboot(int devfd, char *dev) sync(); bootldr = fileprefix(root, bootldr); + if (bootldr == NULL) + exit(1); if (!nowrite) - filecopy(stage2, bootldr); + if (filecopy(stage2, bootldr) == -1) + exit(1); /* Write bootblock into the superblock. */ bootstrap(devfd, dev, stage1); diff --git a/usr.sbin/installboot/sparc64_installboot.c b/usr.sbin/installboot/sparc64_installboot.c index 2c831fe1dfb..f68eec070f7 100644 --- a/usr.sbin/installboot/sparc64_installboot.c +++ b/usr.sbin/installboot/sparc64_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sparc64_installboot.c,v 1.3 2015/01/16 00:05:12 deraadt Exp $ */ +/* $OpenBSD: sparc64_installboot.c,v 1.4 2015/10/08 14:50:38 krw Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing @@ -96,8 +96,11 @@ md_installboot(int devfd, char *dev) sync(); bootldr = fileprefix(root, bootldr); + if (bootldr == NULL) + exit(1); if (!nowrite) - filecopy(stage2, bootldr); + if (filecopy(stage2, bootldr) == -1) + exit(1); /* Write bootblock into the superblock. */ if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE) diff --git a/usr.sbin/installboot/util.c b/usr.sbin/installboot/util.c index 6d939e0902b..ce830ea79d0 100644 --- a/usr.sbin/installboot/util.c +++ b/usr.sbin/installboot/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.6 2015/10/07 03:06:46 krw Exp $ */ +/* $OpenBSD: util.c,v 1.7 2015/10/08 14:50:38 krw Exp $ */ /* * Copyright (c) 2014 Joel Sing @@ -32,40 +32,56 @@ #define BUFSIZE 512 -void +int filecopy(const char *srcfile, const char *dstfile) { struct stat sb; ssize_t sz, n; - int sfd, dfd; + int sfd, dfd, rslt = -1; char *buf; - if ((buf = malloc(BUFSIZE)) == NULL) - err(1, "malloc"); + if ((buf = malloc(BUFSIZE)) == NULL) { + warn("malloc"); + return (-1); + } sfd = open(srcfile, O_RDONLY); - if (sfd == -1) - err(1, "open %s", srcfile); - if (fstat(sfd, &sb) == -1) - err(1, "fstat"); + if (sfd == -1) { + warn("open %s", srcfile); + return (-1); + } + if (fstat(sfd, &sb) == -1) { + warn("fstat"); + return (-1); + } sz = sb.st_size; dfd = open(dstfile, O_WRONLY|O_CREAT); - if (dfd == -1) - err(1, "open %s", dstfile); + if (dfd == -1) { + warn("open %s", dstfile); + return (-1); + } if (fchown(dfd, 0, 0) == -1) - if (errno != EINVAL) - err(1, "chown"); - if (fchmod(dfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) - err(1, "chmod"); + if (errno != EINVAL) { + warn("chown"); + return (-1); + } + if (fchmod(dfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) { + warn("chmod"); + return (-1); + } while (sz > 0) { n = MINIMUM(sz, BUFSIZE); - if ((n = read(sfd, buf, n)) == -1) - err(1, "read"); + if ((n = read(sfd, buf, n)) == -1) { + warn("read"); + return (-1); + } sz -= n; - if (write(dfd, buf, n) != n) - err(1, "write"); + if (write(dfd, buf, n) != n) { + warn("write"); + return (-1); + } } ftruncate(dfd, sb.st_size); @@ -73,6 +89,8 @@ filecopy(const char *srcfile, const char *dstfile) close(dfd); close(sfd); free(buf); + + return (0); } char * @@ -81,16 +99,24 @@ fileprefix(const char *base, const char *path) char *r, *s; int n; - if ((s = malloc(PATH_MAX)) == NULL) - err(1, "malloc"); + if ((s = malloc(PATH_MAX)) == NULL) { + warn("malloc"); + return (NULL); + } n = snprintf(s, PATH_MAX, "%s/%s", base, path); - if (n < 1 || n >= PATH_MAX) - err(1, "snprintf"); - if ((r = realpath(s, NULL)) == NULL) - err(1, "realpath"); + if (n < 1 || n >= PATH_MAX) { + free(s); + warn("snprintf"); + return (NULL); + } + if ((r = realpath(s, NULL)) == NULL) { + free(s); + warn("realpath"); + return (NULL); + } free(s); - return r; + return (r); } /* -- 2.20.1