From patchwork Mon Apr 4 21:01:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 52629 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E2CC83857C42 for ; Mon, 4 Apr 2022 21:02:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2CC83857C42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1649106122; bh=8iIBfOAg4YOQoF/8NcKAwORdCySDoSqU122HglyBTyE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=FtRBYIA0h+ZrEx9IJl8i6X21ZQsACjkoQInjiGwQ874wPZ0eEr0WvMaCNTb/oEFoP G216HK4qFWypB5cfUBJLT8aeSrTspMuCwFkpBpobtKmtbiE6Ox4waNQGE/qbUaBky0 r55y+yzKyGMGyyqxReNsxtEFFL4ANKlWENkc4t0I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by sourceware.org (Postfix) with ESMTPS id 107133858D37 for ; Mon, 4 Apr 2022 21:01:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 107133858D37 Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-de3ca1efbaso12210896fac.9 for ; Mon, 04 Apr 2022 14:01:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=8iIBfOAg4YOQoF/8NcKAwORdCySDoSqU122HglyBTyE=; b=jQrKHM0temIiBcKtStijDF050ue+OWnnY0wtUU3XUL4Vg96qtssr5OIQsZSaAJ7K9u /kn7Ti4AMpenpYJTct1i8cU5gx8WAWRE1H1QfPLF4lRbj0/cC0EoRCIyyzOdaWdXQR7l mebG8StwWTE3pLWVKsRYj6imSHVftchUC4rI7RU4KJLh0+0wWqU0XVAV6CHsc7dXn6dy HkJEBrxjSgTbItUa3r83ne2leq6wnQCYEOJir5BxvHnseJQRkkWzIqwyLZYs4o8yli3R HfuucfITZzYf6uqbZVpsScGQGk0sppTQrxU6IajowQ2ryY6nze7rP5pF5T3SWSsYGuwq MvxA== X-Gm-Message-State: AOAM533rmk5nwHm1Pz49UmePJ140vx4qMCdSCnB0u57A9+kPnUhkfCTC zVe22rF0r0MWdn65nk3wYvQ7y5Gg6HefqA== X-Google-Smtp-Source: ABdhPJz8ZgJmHUfLGnLDIMOYlIwCv1mz6k7/M4pALt81baZSMl8xYSMvzFk29yzveMWxnmJ8/YnUwg== X-Received: by 2002:a05:6870:3307:b0:e1:fcb9:b217 with SMTP id x7-20020a056870330700b000e1fcb9b217mr47324oae.186.1649106101070; Mon, 04 Apr 2022 14:01:41 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:a6c0:94cf:60bc:16d1:2727]) by smtp.gmail.com with ESMTPSA id fl12-20020a056870494c00b000ddd5a37614sm4659189oab.8.2022.04.04.14.01.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 14:01:40 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3] linux: Fix __closefrom_fallback iterates until max int (BZ#28993) Date: Mon, 4 Apr 2022 18:01:31 -0300 Message-Id: <20220404210131.702898-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "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));