From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 139DF3858C2C for ; Thu, 24 Mar 2022 11:09:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 139DF3858C2C Received: by mail-ot1-x32e.google.com with SMTP id n19-20020a9d7113000000b005cd9cff76c3so3052760otj.1 for ; Thu, 24 Mar 2022 04:09:14 -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:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8qpU2s9dUOG9NM9yfoPN3kbHdQLd+IMPNwMfE8aQqEM=; b=ENzn8fKUI2/UC9hAMKkU70H95NZUBN5QPpDM7Qj1tRpIHIugRSCiRFs0uDZtRFGASO cRsJlWqpxiBMrUh2/J7AvLBTZorAdpjb6qkIbgXDe750e+FN9RH3cO8EdQdOu+VYJ46J 8SNhIHOQKh/lzmZR/CRZ3h+kviE7Dw33xwy43CDP05ZLpBngwkG25v4lcsO3/ANGocbv QvjJ7kDPqbjA+5qLEjYutzcRAm48BekOlNFQedFwgxWo8879fyHG3wAXQwwvoE5CGk4D ro6gz5S89Q8hn7+WdClEWLT87kEdLDzwMgTgQd8g0qXjOH29Y0GLw19GhkPM8IaCrMbY ZkTg== X-Gm-Message-State: AOAM532Qk2uRW0WuQqpoFKtafl4uQIrC37eXcV9XX8jFraJkTSE/1tZU GGYRbOUq5W6jRl+vgsF8Mo/vVPwDTOIN+g== X-Google-Smtp-Source: ABdhPJwJQDUaXy0PL+DiaYUqAp72b6pOh75UTbo9VJdTrf9F2o9OFxOfSqbF5vMm32UezCtiu+rZ/g== X-Received: by 2002:a9d:332:0:b0:5b2:420c:ca87 with SMTP id 47-20020a9d0332000000b005b2420cca87mr1788373otv.149.1648120152891; Thu, 24 Mar 2022 04:09:12 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:f4a6:3bdd:d081:25d0]) by smtp.gmail.com with ESMTPSA id i28-20020a056870891c00b000de777f1a41sm1153673oao.46.2022.03.24.04.09.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 04:09:12 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2] linux: Fix __closefrom_fallback iterates until max int (BZ#28993) Date: Thu, 24 Mar 2022 08:09:07 -0300 Message-Id: <20220324110907.169470-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.9 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-List-Received-Date: Thu, 24 Mar 2022 11:09:19 -0000 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. --- v2: Fix wrong call of __close_nocancel if case of failure. --- sysdeps/unix/sysv/linux/closefrom_fallback.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sysdeps/unix/sysv/linux/closefrom_fallback.c b/sysdeps/unix/sysv/linux/closefrom_fallback.c index 60101aa3ba..da1f29752d 100644 --- a/sysdeps/unix/sysv/linux/closefrom_fallback.c +++ b/sysdeps/unix/sysv/linux/closefrom_fallback.c @@ -30,15 +30,13 @@ _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; + if (errno != EMFILE || !dirfd_fallback) + return false; for (int i = from; i < INT_MAX; i++) { @@ -54,6 +52,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