about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2022-03-23 17:40:01 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2022-04-05 08:08:19 -0300
commit053fe273434056f551ed8f81daf750db9dab5931 (patch)
treeaa523a541ce952fc531e0200eb61e1a918390be0
parent3ee318c9233ce77dee099f2830e8e29a0c572ca7 (diff)
downloadglibc-053fe273434056f551ed8f81daf750db9dab5931.tar.gz
glibc-053fe273434056f551ed8f81daf750db9dab5931.tar.xz
glibc-053fe273434056f551ed8f81daf750db9dab5931.zip
linux: Fix __closefrom_fallback iterates until max int (BZ#28993)
The __closefrom_fallback tries to get a available file descriptor
if the initial open ("/proc/self/fd/", ...) fails.  It assumes the
failure would be only if procfs is not mount (ENOENT), however if
the the proc file is not accessible (due some other kernel filtering
such apparmor) it will iterate over a potentially large file set
issuing close calls.

It should only try the close fallback if open returns EMFILE,
ENFILE, or ENOMEM.

Checked on x86_64-linux-gnu.
-rw-r--r--sysdeps/unix/sysv/linux/closefrom_fallback.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/sysdeps/unix/sysv/linux/closefrom_fallback.c b/sysdeps/unix/sysv/linux/closefrom_fallback.c
index 60101aa3ba..a9dd0c46b2 100644
--- a/sysdeps/unix/sysv/linux/closefrom_fallback.c
+++ b/sysdeps/unix/sysv/linux/closefrom_fallback.c
@@ -30,16 +30,16 @@
 _Bool
 __closefrom_fallback (int from, _Bool dirfd_fallback)
 {
-  bool ret = false;
-
   int dirfd = __open_nocancel (FD_TO_FILENAME_PREFIX, O_RDONLY | O_DIRECTORY,
                                0);
   if (dirfd == -1)
     {
-      /* The closefrom should work even when process can't open new files.  */
-      if (errno == ENOENT || !dirfd_fallback)
-        goto err;
+      /* Return if procfs can not be opened for some reason.  */
+      if ((errno != EMFILE && errno != ENFILE && errno != ENOMEM)
+	  || !dirfd_fallback)
+	return false;
 
+      /* The closefrom should work even when process can't open new files.  */
       for (int i = from; i < INT_MAX; i++)
         {
           int r = __close_nocancel (i);
@@ -54,6 +54,7 @@ __closefrom_fallback (int from, _Bool dirfd_fallback)
     }
 
   char buffer[1024];
+  bool ret = false;
   while (true)
     {
       ssize_t ret = __getdents64 (dirfd, buffer, sizeof (buffer));