]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
linux: Fix __closefrom_fallback iterates until max int (BZ#28993)
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 23 Mar 2022 20:40:01 +0000 (17:40 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 5 Apr 2022 11:08:19 +0000 (08:08 -0300)
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.

sysdeps/unix/sysv/linux/closefrom_fallback.c

index 60101aa3ba12298721840efbb7c3df1baaa13833..a9dd0c46b211926961d6300183cb7d96c55b8a7d 100644 (file)
 _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));