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