From patchwork Tue Jun 18 00:28:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 33168 Received: (qmail 116366 invoked by alias); 18 Jun 2019 00:28:46 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 116353 invoked by uid 89); 18 Jun 2019 00:28:45 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-17.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=HX-Languages-Length:2990, sk:copyrig X-HELO: mx1.redhat.com Date: Mon, 17 Jun 2019 20:28:39 -0400 Message-Id: From: DJ Delorie To: libc-alpha@sourceware.org Subject: nss_db: protect against empty mappings Resolves: #24696 (note to reviewers: Sergi contributed the one-line fix, I did the rest, for the purposes of copyright-line-counts) 2019-06-17 DJ Delorie Sergei Trofimovich * nss/nss_db/db-open.c (internal_endent): Protect against NULL mappings. * nss/tst-nss-db-endgrent.c: New. * nss/tst-nss-db-endgrent.root: New. * nss/Makefile: Add new test. diff --git a/nss/Makefile b/nss/Makefile index 15fc410cf1..a15c3b7d90 100644 --- a/nss/Makefile +++ b/nss/Makefile @@ -62,7 +62,8 @@ xtests = bug-erange tests-container = \ tst-nss-test3 \ tst-nss-files-hosts-long \ - tst-nss-db-endpwent + tst-nss-db-endpwent \ + tst-nss-db-endgrent # Tests which need libdl ifeq (yes,$(build-shared)) diff --git a/nss/nss_db/db-open.c b/nss/nss_db/db-open.c index f7c53b4486..3fa11e9ab0 100644 --- a/nss/nss_db/db-open.c +++ b/nss/nss_db/db-open.c @@ -63,6 +63,9 @@ internal_setent (const char *file, struct nss_db_map *mapping) void internal_endent (struct nss_db_map *mapping) { - munmap (mapping->header, mapping->len); - mapping->header = NULL; + if (mapping->header != NULL) + { + munmap (mapping->header, mapping->len); + mapping->header = NULL; + } } diff --git a/nss/tst-nss-db-endgrent.c b/nss/tst-nss-db-endgrent.c new file mode 100644 index 0000000000..f4c1e14d60 --- /dev/null +++ b/nss/tst-nss-db-endgrent.c @@ -0,0 +1,46 @@ +/* Test for endgrent changing errno. + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +#include +#include + + +static int +do_test (void) +{ + /* Just make sure it's not there, although usually it won't be. */ + unlink ("/var/db/group.db"); + + getgrent (); + + int saved_errno = errno; + + endgrent (); + + if (errno != saved_errno) + FAIL_EXIT1 ("endgrent changed errno from %d o %d\n", saved_errno, errno); + + return 0; +} +#include diff --git a/nss/tst-nss-db-endgrent.root/etc/nsswitch.conf b/nss/tst-nss-db-endgrent.root/etc/nsswitch.conf new file mode 100644 index 0000000000..21471df94f --- /dev/null +++ b/nss/tst-nss-db-endgrent.root/etc/nsswitch.conf @@ -0,0 +1 @@ +group : db files