From: gnezdo Date: Sat, 12 Aug 2023 13:43:22 +0000 (+0000) Subject: Check for a full read, avoid warn when errno might be unmodified X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=d4a5aacda412f32f85bea992823bf81bf9364c8e;p=openbsd Check for a full read, avoid warn when errno might be unmodified Promote size from int to size_t. From: lucas at sexy dot is Regress tests by gnezdo@ deraadt: yes --- diff --git a/libexec/ld.so/ldd/ldd.c b/libexec/ld.so/ldd/ldd.c index 9e8c5065cd8..f8f208652d2 100644 --- a/libexec/ld.so/ldd/ldd.c +++ b/libexec/ld.so/ldd/ldd.c @@ -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 * 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; diff --git a/regress/libexec/ld.so/Makefile b/regress/libexec/ld.so/Makefile index e4c908ddaaf..5b746bb347c 100644 --- a/regress/libexec/ld.so/Makefile +++ b/regress/libexec/ld.so/Makefile @@ -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 index 00000000000..b8b79d64d86 --- /dev/null +++ b/regress/libexec/ld.so/ldd/Makefile @@ -0,0 +1,19 @@ +# $OpenBSD: Makefile,v 1.1 2023/08/12 13:43:22 gnezdo Exp $ + +.include + +REGRESS_TARGETS = test + +.include + +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 index 00000000000..2e5ce70cfff --- /dev/null +++ b/regress/libexec/ld.so/ldd/test.sh @@ -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