public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3] linux: Fix __closefrom_fallback iterates until max int (BZ#28993)
@ 2022-04-04 21:01 Adhemerval Zanella
  2022-04-05  7:09 ` Florian Weimer
  0 siblings, 1 reply; 3+ messages in thread
From: Adhemerval Zanella @ 2022-04-04 21:01 UTC (permalink / raw)
  To: libc-alpha

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.

Checked on x86_64-linux-gnu.
---
 sysdeps/unix/sysv/linux/closefrom_fallback.c | 11 ++++++-----
 1 file 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));
-- 
2.32.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] linux: Fix __closefrom_fallback iterates until max int (BZ#28993)
  2022-04-04 21:01 [PATCH v3] linux: Fix __closefrom_fallback iterates until max int (BZ#28993) Adhemerval Zanella
@ 2022-04-05  7:09 ` Florian Weimer
  2022-04-05 11:09   ` Adhemerval Zanella
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Weimer @ 2022-04-05  7:09 UTC (permalink / raw)
  To: Adhemerval Zanella via Libc-alpha

* Adhemerval Zanella via Libc-alpha:

> 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.

The last line no longer matches the patch.

Rest looks okay.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] linux: Fix __closefrom_fallback iterates until max int (BZ#28993)
  2022-04-05  7:09 ` Florian Weimer
@ 2022-04-05 11:09   ` Adhemerval Zanella
  0 siblings, 0 replies; 3+ messages in thread
From: Adhemerval Zanella @ 2022-04-05 11:09 UTC (permalink / raw)
  To: Florian Weimer, Adhemerval Zanella via Libc-alpha



On 05/04/2022 04:09, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
> 
>> 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.
> 
> The last line no longer matches the patch.

Ack, I will update it.

> 
> Rest looks okay.

Thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-04-05 11:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-04 21:01 [PATCH v3] linux: Fix __closefrom_fallback iterates until max int (BZ#28993) Adhemerval Zanella
2022-04-05  7:09 ` Florian Weimer
2022-04-05 11:09   ` Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).