git: 741d78126b55 - main - rtld: call close(2) after errno is saved

Konstantin Belousov kib at FreeBSD.org
Sat Jan 2 02:44:27 UTC 2021


The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=741d78126b5584e860811c78f87f51597e375592

commit 741d78126b5584e860811c78f87f51597e375592
Author:     Konstantin Belousov <kib at FreeBSD.org>
AuthorDate: 2021-01-01 22:24:46 +0000
Commit:     Konstantin Belousov <kib at FreeBSD.org>
CommitDate: 2021-01-02 02:43:32 +0000

    rtld: call close(2) after errno is saved
    
    to prevent obliteration of error value from the original syscall.
    Also improve error message for short read.
    
    Submitted by:   Konrad Sewiłło-Jopek
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D27864
---
 libexec/rtld-elf/libmap.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/libexec/rtld-elf/libmap.c b/libexec/rtld-elf/libmap.c
index 8057a0ccf43d..499d2cd2a1d9 100644
--- a/libexec/rtld-elf/libmap.c
+++ b/libexec/rtld-elf/libmap.c
@@ -103,7 +103,7 @@ lmc_parse_file(const char *path)
 	char *lm_map;
 	struct stat st;
 	ssize_t retval;
-	int fd;
+	int fd, saved_errno;
 
 	TAILQ_FOREACH(p, &lmc_head, next) {
 		if (strcmp(p->path, path) == 0)
@@ -117,9 +117,9 @@ lmc_parse_file(const char *path)
 		return;
 	}
 	if (fstat(fd, &st) == -1) {
-		close(fd);
 		dbg("lm_parse_file: fstat(\"%s\") failed, %s", path,
 		    rtld_strerror(errno));
+		close(fd);
 		return;
 	}
 
@@ -132,14 +132,19 @@ lmc_parse_file(const char *path)
 
 	lm_map = xmalloc(st.st_size);
 	retval = read(fd, lm_map, st.st_size);
+	saved_errno = errno;
+	close(fd);
 	if (retval != st.st_size) {
-		close(fd);
+		if (retval == -1) {
+			dbg("lm_parse_file: read(\"%s\") failed, %s", path,
+			    rtld_strerror(saved_errno));
+		} else {
+			dbg("lm_parse_file: short read(\"%s\"), %zd vs %jd",
+			    path, retval, (uintmax_t)st.st_size);
+		}
 		free(lm_map);
-		dbg("lm_parse_file: read(\"%s\") failed, %s", path,
-		    rtld_strerror(errno));
 		return;
 	}
-	close(fd);
 	p = xmalloc(sizeof(struct lmc));
 	p->path = xstrdup(path);
 	p->dev = st.st_dev;


More information about the dev-commits-src-all mailing list