From patchwork Tue Jul 13 14:17:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44336 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 BC806395CCBB for ; Tue, 13 Jul 2021 14:20:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC806395CCBB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1626186030; bh=yllS/JnDKxzuKlg0rCvzPWtVGyo9iwyBmSBwkYg7Tws=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=p4SAq/BsNiQEAvqXCh+vMNr7Q5vZnoX8rLyjDlDGGWB7D+/c//nzLI+h4hCENfAGF iGPTiidGs1poOgXIZLwaZ3y+bFFEhOjiJlB8AD9D7zMYAPCHZrEeK6ZQHYDC4xyazz 1I9zV+nDMkhSU2Uzh+8E6ciLj+HUX5/5bYf43F3Y= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by sourceware.org (Postfix) with ESMTPS id 8944E395BC45 for ; Tue, 13 Jul 2021 14:17:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8944E395BC45 Received: by mail-pj1-x1035.google.com with SMTP id n11so12238831pjo.1 for ; Tue, 13 Jul 2021 07:17:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=yllS/JnDKxzuKlg0rCvzPWtVGyo9iwyBmSBwkYg7Tws=; b=XOX7JLg5kGBWe1O96nZzA1dhqijMB5DPx1XyAHtPlKIno9fPETI6i7EDcy7w7qORHB r31Wl2i88buncGBIzBiIxGdj/yXqHplX1RzWKuV1BGdFglI17eLyuGenIkDPGWU3j/cU 0ePU+MZWForzskpAI0i5sVg2wc1eCTuDMwL/vHfOrVTiCey2zFhi4myR1vCdZHYnL4lP GzD3zhhxPz04tq4Bsa5nsx2nG2JZ9mEuX0DkOXjWMoSwKNEuaFWgIbM3SDDKzC61KIb/ m6hr//LWUXGBxJnRvjtRbhdf6IDNOjZqyJLmFwWJhbV/h+4uyVArZD92OzqyWmBYeUr9 xX4w== X-Gm-Message-State: AOAM531/Eu40AdEAu+2LcUR/XyerbUnoV2pakz5X6Opw8WPxws/DsyEQ oEEwFjbTsXA8TWRcf8ambGqX8CnctWC/tQ== X-Google-Smtp-Source: ABdhPJzAYju2nVgxh+xF8p8DEhloqEwi9rcFDL8FMoJItn4zqAho8F+pTmSE35CzecCVY4Z6jlvGzw== X-Received: by 2002:a17:902:f284:b029:12a:dd51:f37a with SMTP id k4-20020a170902f284b029012add51f37amr3927275plc.31.1626185873428; Tue, 13 Jul 2021 07:17:53 -0700 (PDT) Received: from birita.. ([177.194.59.218]) by smtp.gmail.com with ESMTPSA id u16sm3664282pfh.205.2021.07.13.07.17.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jul 2021 07:17:53 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] posix: Ignore non opened files on tst-spawn5 Date: Tue, 13 Jul 2021 11:17:49 -0300 Message-Id: <20210713141749.2717687-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The make program might open a pipe for its job server, which triggers an invalid check on the spawned process. This patch now passes the lowest file descriptor as ithe first argument, so only the range that was actually opened is checked. Checked on x86_64-linux-gnu and i686-linux-gnu and centos7 (which triggers the issue). --- posix/tst-spawn5.c | 55 +++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/posix/tst-spawn5.c b/posix/tst-spawn5.c index 88d8c94c78..ffa3219c76 100644 --- a/posix/tst-spawn5.c +++ b/posix/tst-spawn5.c @@ -39,7 +39,7 @@ static int restart; /* Hold the four initial argument used to respawn the process, plus the extra '--direct' and '--restart', and a final NULL. */ -static char *initial_argv[7]; +static char *initial_argv[8]; static int initial_argv_count; #define CMDLINE_OPTIONS \ @@ -58,11 +58,24 @@ open_multiple_temp_files (void) return lowfd; } +static int +parse_fd (const char *str) +{ + char *endptr; + long unsigned int fd = strtoul (str, &endptr, 10); + if (*endptr != '\0' || fd > INT_MAX) + FAIL_EXIT1 ("invalid file descriptor value: %s", str); + return fd; +} + /* Called on process re-execution. The arguments are the expected opened file descriptors. */ _Noreturn static void handle_restart (int argc, char *argv[]) { + TEST_VERIFY (argc > 0); + int lowfd = parse_fd (argv[0]); + size_t nfds = argc > 1 ? argc - 1 : 0; struct fd_t { @@ -71,12 +84,7 @@ handle_restart (int argc, char *argv[]) } *fds = xmalloc (sizeof (struct fd_t) * nfds); for (int i = 0; i < nfds; i++) { - char *endptr; - long unsigned int fd = strtoul (argv[i+1], &endptr, 10); - if (*endptr != '\0' || fd > INT_MAX) - FAIL_EXIT1 ("argv[%d]: invalid file descriptor value: %s", i, argv[i]); - - fds[i].fd = fd; + fds[i].fd = parse_fd (argv[i + 1]); fds[i].found = false; } @@ -104,12 +112,9 @@ handle_restart (int argc, char *argv[]) FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s", e->d_name); - /* Skip the descriptor which is used to enumerate the descriptors. */ - if (fd == dirfd (dirp) - || fd == STDIN_FILENO - || fd == STDOUT_FILENO - || fd == STDERR_FILENO) - continue; + /* Ignore the descriptors not in the range of the opened files. */ + if (fd < lowfd || fd == dirfd (dirp)) + continue; bool found = false; for (int i = 0; i < nfds; i++) @@ -117,7 +122,11 @@ handle_restart (int argc, char *argv[]) fds[i].found = found = true; if (!found) - FAIL_EXIT1 ("unexpected open file descriptor: %ld", fd); + { + char *path = xasprintf ("/proc/self/fd/%s", e->d_name); + char *resolved = xreadlink (path); + FAIL_EXIT1 ("unexpected open file descriptor %ld: %s", fd, resolved); + } } closedir (dirp); @@ -134,15 +143,17 @@ static void spawn_closefrom_test (posix_spawn_file_actions_t *fa, int lowfd, int highfd, int *extrafds, size_t nextrafds) { - /* 3 or 6 elements from initial_argv: + /* 3 or 7 elements from initial_argv: + path to ld.so optional + --library-path optional + the library path optional + application name + --direct + --restart - up to 2 * maximum_fd arguments (the expected open file descriptors), - plus NULL. */ + + lowest opened file descriptor + + up to 2 * maximum_fd arguments (the expected open file descriptors), + plus NULL. */ + int argv_size = initial_argv_count + 2 * NFDS + 1; char *args[argv_size]; int argc = 0; @@ -150,6 +161,8 @@ spawn_closefrom_test (posix_spawn_file_actions_t *fa, int lowfd, int highfd, for (char **arg = initial_argv; *arg != NULL; arg++) args[argc++] = *arg; + args[argc++] = xasprintf ("%d", lowfd); + for (int i = lowfd; i < highfd; i++) args[argc++] = xasprintf ("%d", i); @@ -265,14 +278,16 @@ do_test (int argc, char *argv[]) - six parameters left if called through re-execution: + argv[1]: the application name - + argv[2]: first expected open file descriptor - + argv[n]: last expected open file descritptor + + argv[2]: the lowest file descriptor expected + + argv[3]: first expected open file descriptor optional + + argv[n]: last expected open file descritptor optional * When built with --enable-hardcoded-path-in-tests or issued without using the loader directly. */ if (restart) - handle_restart (argc, argv); + /* Ignore the application name. */ + handle_restart (argc - 1, &argv[1]); TEST_VERIFY_EXIT (argc == 2 || argc == 5);