Check for a full read, avoid warn when errno might be unmodified
authorgnezdo <gnezdo@openbsd.org>
Sat, 12 Aug 2023 13:43:22 +0000 (13:43 +0000)
committergnezdo <gnezdo@openbsd.org>
Sat, 12 Aug 2023 13:43:22 +0000 (13:43 +0000)
Promote size from int to size_t.

From: lucas at sexy dot is

Regress tests by gnezdo@

deraadt: yes

libexec/ld.so/ldd/ldd.c
regress/libexec/ld.so/Makefile
regress/libexec/ld.so/ldd/Makefile [new file with mode: 0644]
regress/libexec/ld.so/ldd/test.sh [new file with mode: 0755]

index 9e8c506..f8f2086 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: ldd.c,v 1.24 2023/07/24 01:02:47 deraadt Exp $        */
+/*     $OpenBSD: ldd.c,v 1.25 2023/08/12 13:43:22 gnezdo Exp $ */
 /*
  * Copyright (c) 2001 Artur Grabowski <art@openbsd.org>
  * All rights reserved.
@@ -96,7 +96,8 @@ doit(char *name)
 {
        Elf_Ehdr ehdr;
        Elf_Phdr *phdr;
-       int fd, i, size, status, interp=0;
+       size_t size;
+       int fd, i, status, interp=0;
        char buf[PATH_MAX];
        struct stat st;
        void * dlhandle;
@@ -118,8 +119,8 @@ doit(char *name)
                return 1;
        }
 
-       if (read(fd, &ehdr, sizeof(ehdr)) < 0) {
-               warn("read(%s)", name);
+       if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
+               warnx("%s: incomplete ELF header", name);
                close(fd);
                return 1;
        }
@@ -141,7 +142,7 @@ doit(char *name)
        size = ehdr.e_phnum * sizeof(Elf_Phdr);
 
        if (pread(fd, phdr, size, ehdr.e_phoff) != size) {
-               warn("read(%s)", name);
+               warnx("%s: incomplete program header", name);
                close(fd);
                free(phdr);
                return 1;
index e4c908d..5b746bb 100644 (file)
@@ -1,10 +1,10 @@
-#      $OpenBSD: Makefile,v 1.19 2022/08/20 14:13:48 sthen Exp $
+#      $OpenBSD: Makefile,v 1.20 2023/08/12 13:43:22 gnezdo Exp $
 
 SUBDIR+= elf hidden weak dlsym dlopen dlclose lazy
 SUBDIR+= constructor 
 SUBDIR+= link-order edgecases initfirst
 SUBDIR+= df_1_noopen randomdata subst dependencies
-SUBDIR+= init-env nodelete noload
+SUBDIR+= init-env nodelete noload ldd
 
 install:
 
diff --git a/regress/libexec/ld.so/ldd/Makefile b/regress/libexec/ld.so/ldd/Makefile
new file mode 100644 (file)
index 0000000..b8b79d6
--- /dev/null
@@ -0,0 +1,19 @@
+# $OpenBSD: Makefile,v 1.1 2023/08/12 13:43:22 gnezdo Exp $
+
+.include <bsd.own.mk>
+
+REGRESS_TARGETS = test
+
+.include <bsd.regress.mk>
+
+CLEANFILES+=empty short
+
+.PHONY: test
+test: empty short
+       ${SHELL} ${.CURDIR}/test.sh
+
+empty:
+       touch empty
+
+short:
+       dd if=/usr/bin/ldd of=short count=128 bs=1
diff --git a/regress/libexec/ld.so/ldd/test.sh b/regress/libexec/ld.so/ldd/test.sh
new file mode 100755 (executable)
index 0000000..2e5ce70
--- /dev/null
@@ -0,0 +1,18 @@
+#!/bin/sh -ex
+# $OpenBSD: test.sh,v 1.1 2023/08/12 13:43:22 gnezdo Exp $
+
+res=0
+
+test() {
+  if eval "$@"; then
+    echo "passed"
+  else
+    echo "FAILED"
+    res=1
+  fi
+}
+
+test "ldd empty 2>&1 | grep 'incomplete ELF header'"
+test "ldd short 2>&1 | grep 'incomplete program header'"
+
+exit $res