786673
From 23ed36735af09c258e542266aaed92cdd8571c6c Mon Sep 17 00:00:00 2001
786673
From: Florian Weimer <fweimer@redhat.com>
786673
Date: Thu, 16 Jul 2020 16:21:28 +0200
786673
Subject: [PATCH 02/11] nss_compat: Do not use mmap to read database files (bug
786673
 26258)
786673
786673
This avoids crashes in case the files are truncated for some reason.
786673
For typically file sizes, it is also going to be slightly faster.
786673
Using __nss_files_fopen instead mirrors what nss_files does.
786673
786673
Tested-by: Carlos O'Donell <carlos@redhat.com>
786673
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
786673
---
786673
 nss/nss_compat/compat-grp.c        | 6 ++----
786673
 nss/nss_compat/compat-initgroups.c | 6 ++----
786673
 nss/nss_compat/compat-pwd.c        | 6 ++----
786673
 nss/nss_compat/compat-spwd.c       | 6 ++----
786673
 4 files changed, 8 insertions(+), 16 deletions(-)
786673
786673
diff -rup a/nss/nss_compat/compat-grp.c b/nss/nss_compat/compat-grp.c
786673
--- a/nss/nss_compat/compat-grp.c	2020-09-14 15:49:18.248178627 -0400
786673
+++ b/nss/nss_compat/compat-grp.c	2020-09-14 17:18:22.514977541 -0400
786673
@@ -26,6 +26,7 @@
786673
 #include <string.h>
786673
 #include <libc-lock.h>
786673
 #include <kernel-features.h>
786673
+#include <nss_files.h>
786673
 
786673
 static service_user *ni;
786673
 static enum nss_status (*nss_setgrent) (int stayopen);
786673
@@ -106,13 +107,10 @@ internal_setgrent (ent_t *ent, int stayo
786673
 
786673
   if (ent->stream == NULL)
786673
     {
786673
-      ent->stream = fopen ("/etc/group", "rme");
786673
+      ent->stream = __nss_files_fopen ("/etc/group");
786673
 
786673
       if (ent->stream == NULL)
786673
 	status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
786673
-      else
786673
-	/* We take care of locking ourself.  */
786673
-	__fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
786673
     }
786673
   else
786673
     rewind (ent->stream);
786673
diff -rup a/nss/nss_compat/compat-initgroups.c b/nss/nss_compat/compat-initgroups.c
786673
--- a/nss/nss_compat/compat-initgroups.c	2020-09-14 15:49:18.255178892 -0400
786673
+++ b/nss/nss_compat/compat-initgroups.c	2020-09-14 17:18:22.519977728 -0400
786673
@@ -29,6 +29,7 @@
786673
 #include <libc-lock.h>
786673
 #include <kernel-features.h>
786673
 #include <scratch_buffer.h>
786673
+#include <nss_files.h>
786673
 
786673
 static service_user *ni;
786673
 /* Type of the lookup function.  */
786673
@@ -121,13 +122,10 @@ internal_setgrent (ent_t *ent)
786673
   else
786673
     ent->blacklist.current = 0;
786673
 
786673
-  ent->stream = fopen ("/etc/group", "rme");
786673
+  ent->stream = __nss_files_fopen ("/etc/group");
786673
 
786673
   if (ent->stream == NULL)
786673
     status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
786673
-  else
786673
-    /* We take care of locking ourself.  */
786673
-    __fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
786673
 
786673
   return status;
786673
 }
786673
diff -rup a/nss/nss_compat/compat-pwd.c b/nss/nss_compat/compat-pwd.c
786673
--- a/nss/nss_compat/compat-pwd.c	2020-09-14 15:49:18.260179081 -0400
786673
+++ b/nss/nss_compat/compat-pwd.c	2020-09-14 17:18:22.523977879 -0400
786673
@@ -27,6 +27,7 @@
786673
 #include <string.h>
786673
 #include <libc-lock.h>
786673
 #include <kernel-features.h>
786673
+#include <nss_files.h>
786673
 
786673
 #include "netgroup.h"
786673
 #include "nisdomain.h"
786673
@@ -221,13 +222,10 @@ internal_setpwent (ent_t *ent, int stayo
786673
 
786673
   if (ent->stream == NULL)
786673
     {
786673
-      ent->stream = fopen ("/etc/passwd", "rme");
786673
+      ent->stream = __nss_files_fopen ("/etc/passwd");
786673
 
786673
       if (ent->stream == NULL)
786673
 	status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
786673
-      else
786673
-	/* We take care of locking ourself.  */
786673
-	__fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
786673
     }
786673
   else
786673
     rewind (ent->stream);
786673
diff -rup a/nss/nss_compat/compat-spwd.c b/nss/nss_compat/compat-spwd.c
786673
--- a/nss/nss_compat/compat-spwd.c	2020-09-14 15:49:18.264179232 -0400
786673
+++ b/nss/nss_compat/compat-spwd.c	2020-09-14 17:18:22.527978029 -0400
786673
@@ -27,6 +27,7 @@
786673
 #include <string.h>
786673
 #include <libc-lock.h>
786673
 #include <kernel-features.h>
786673
+#include <nss_files.h>
786673
 
786673
 #include "netgroup.h"
786673
 #include "nisdomain.h"
786673
@@ -177,13 +178,10 @@ internal_setspent (ent_t *ent, int stayo
786673
 
786673
   if (ent->stream == NULL)
786673
     {
786673
-      ent->stream = fopen ("/etc/shadow", "rme");
786673
+      ent->stream = __nss_files_fopen ("/etc/shadow");
786673
 
786673
       if (ent->stream == NULL)
786673
 	status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
786673
-      else
786673
-	/* We take care of locking ourself.  */
786673
-	__fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
786673
     }
786673
   else
786673
     rewind (ent->stream);