From owner-dev-commits-src-main@freebsd.org Sat Jan 2 02:44:27 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 3301B4BF1B8; Sat, 2 Jan 2021 02:44:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4D75nl112nz4kZ5; Sat, 2 Jan 2021 02:44:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 158515956; Sat, 2 Jan 2021 02:44:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1022iRhF026360; Sat, 2 Jan 2021 02:44:27 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1022iRUi026359; Sat, 2 Jan 2021 02:44:27 GMT (envelope-from git) Date: Sat, 2 Jan 2021 02:44:27 GMT Message-Id: <202101020244.1022iRUi026359@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 741d78126b55 - main - rtld: call close(2) after errno is saved MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 741d78126b5584e860811c78f87f51597e375592 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Jan 2021 02:44:27 -0000 The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=741d78126b5584e860811c78f87f51597e375592 commit 741d78126b5584e860811c78f87f51597e375592 Author: Konstantin Belousov AuthorDate: 2021-01-01 22:24:46 +0000 Commit: Konstantin Belousov 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;