Correctly handle files smaller than an ELF header; reported by drahn@bitrig
authormiod <miod@openbsd.org>
Sun, 18 May 2014 21:18:07 +0000 (21:18 +0000)
committermiod <miod@openbsd.org>
Sun, 18 May 2014 21:18:07 +0000 (21:18 +0000)
usr.sbin/mkuboot/mkuboot.c

index a3c95b9..8ecb79d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: mkuboot.c,v 1.3 2013/10/28 09:00:06 patrick Exp $     */
+/*     $OpenBSD: mkuboot.c,v 1.4 2014/05/18 21:18:07 miod Exp $        */
 
 /*
  * Copyright (c) 2008 Mark Kettenis
@@ -285,12 +285,10 @@ is_elf(int ifd, const char *iname)
        nbytes = read(ifd, &ehdr, sizeof ehdr);
        if (nbytes == -1)
                err(1, "%s", iname);
-       if (nbytes != sizeof ehdr)
-               return 0;
-
        if (lseek(ifd, 0, SEEK_SET) != 0)
                err(1, "%s", iname);
-       if (!IS_ELF(ehdr))
+
+       if (nbytes != sizeof ehdr || !IS_ELF(ehdr))
                return 0;
 
        if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)