public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: cygwin@cygwin.com
Subject: posix_spawn facility
Date: Sun, 16 Apr 2023 13:46:27 +0200	[thread overview]
Message-ID: <1752276.7aRn1RRit1@nimes> (raw)

Hi,

AFAIU, Cygwin has a working posix_spawn[p] implementation since 2020
(commit 3fbfcd11fb09d5f47af3043ee47ec5c7d863d872, 2020-08-03, Cygwin 3.1.7).

Additionally, Gnulib has a posix_spawn[p] implementation since 2022,
that works on all platforms, including native Windows. Based on it,
I recommend posix_spawn[p] over fork+exec, see
https://savannah.gnu.org/news/?id=10219 . It allows to have a single
application code for spawning subprocesses.

The GNU groff maintainer asks about the performance of posix_spawn[p]
on Cygwin. And here's the problem: While Cygwin has an implementation
that avoids the slow fork(), by calling child_info_spawn::worker more
or less directly, Gnulib prefers its own implementation over the Cygwin
one, and the Gnulib implementation uses slow fork()+exec().

The reason is that we consider posix_spawn[p] unsecure if it will
readily execute plain text files without a #! marker as if they were
shell scripts, usually leading to plenty of syntax errors, but also
exhibiting undefined behaviour.

This reasoning follows what was done in GNU libc:
https://sourceware.org/bugzilla/show_bug.cgi?id=13134
https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=d96de9634a334af16c0ac711074c15ac1762b23c
https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=13adfa34aff03fd9f1c1612b537a0d736ddb6c2b

These are the two configure tests that Gnulib uses:

======================= test secure posix_spawn ==========================
Preparation:
  echo ':' > conftest.scr
  chmod a+x conftest.scr
C program:
       #include <errno.h>
       #include <spawn.h>
       #include <stddef.h>
       #include <sys/types.h>
       #include <sys/wait.h>
       int
       main ()
       {
         const char *prog_path = "./conftest.scr";
         const char *prog_argv[2] = { prog_path, NULL };
         const char *environment[2] = { "PATH=.", NULL };
         pid_t child;
         int status;
         int err = posix_spawn (&child, prog_path, NULL, NULL,
                                (char **) prog_argv, (char **) environment);
         if (err == ENOEXEC)
           return 0;
         if (err != 0)
           return 1;
         status = 0;
         while (waitpid (child, &status, 0) != child)
           ;
         if (!WIFEXITED (status))
           return 2;
         if (WEXITSTATUS (status) != 127)
           return 3;
         return 0;
       }
======================= test secure posix_spawnp =========================
Preparation:
  echo ':' > conftest.scr
  chmod a+x conftest.scr
C program:
       #include <errno.h>
       #include <spawn.h>
       #include <stddef.h>
       #include <sys/types.h>
       #include <sys/wait.h>
       int
       main ()
       {
         const char *prog_path = "./conftest.scr";
         const char *prog_argv[2] = { prog_path, NULL };
         const char *environment[2] = { "PATH=.", NULL };
         pid_t child;
         int status;
         int err = posix_spawnp (&child, prog_path, NULL, NULL,
                                 (char **) prog_argv, (char **) environment);
         if (err == ENOEXEC)
           return 0;
         if (err != 0)
           return 1;
         status = 0;
         while (waitpid (child, &status, 0) != child)
           ;
         if (!WIFEXITED (status))
           return 2;
         if (WEXITSTATUS (status) != 127)
           return 3;
         return 0;
       }
==========================================================================

In Cygwin, the "test secure posix_spawn" recipe succeeds, whereas the
"test secure posix_spawnp" fails; the latter is the obstacle that
prevents Gnulib from using Cygwin's implementation.

Would it be possible to change Cygwin's posix_spawnp implementation,
so that both tests succeed?

Disclaimer: I have done my tests with Cygwin 2.9.0; so, if things have
improved since then, the better!

Bruno




             reply	other threads:[~2023-04-16 11:46 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-16 11:46 Bruno Haible [this message]
2023-04-17  9:18 ` Corinna Vinschen
2023-04-17 18:44   ` Bruno Haible
2023-04-18  9:25     ` Corinna Vinschen
2023-04-18 20:49       ` Eric Blake
2023-04-18 21:00         ` Corinna Vinschen
2023-04-18 22:10         ` Bruno Haible
2023-04-19  2:39           ` Eric Blake
2023-04-19  8:19             ` Corinna Vinschen
2023-04-19 11:56             ` Bruno Haible
2023-04-20  6:52               ` Csaba Raduly
2023-07-31 18:58         ` Eric Blake
2023-07-31 19:12           ` Corinna Vinschen
2023-04-19  8:24     ` Corinna Vinschen
2023-04-19 11:24       ` posix_spawn_file_actions_add[f]chdir_np Bruno Haible
2023-04-19 15:05         ` posix_spawn_file_actions_add[f]chdir_np Corinna Vinschen
2023-04-19 19:13           ` posix_spawn_file_actions_add[f]chdir_np Corinna Vinschen
2023-04-19 21:33             ` posix_spawn_file_actions_add[f]chdir_np Eliot Moss
2023-04-20 10:27             ` posix_spawn_file_actions_add[f]chdir_np Bruno Haible
2023-04-19 10:48     ` posix_spawn facility Bruno Haible
2023-04-20  7:14     ` gs-cygwin.com
2023-04-20  8:35       ` Corinna Vinschen
2023-04-20 10:18         ` Bruno Haible
2023-04-20 14:21           ` Corinna Vinschen
2023-04-20 14:40             ` Corinna Vinschen
2023-04-20 14:58               ` Bruno Haible
2023-04-20 15:40                 ` Corinna Vinschen
2023-04-20 18:46                   ` gs-cygwin.com
2023-04-20 18:40                 ` gs-cygwin.com
2023-04-20 19:31                   ` Bruno Haible
2023-04-20 20:00                     ` gs-cygwin.com
2023-05-10  9:15                       ` gs-cygwin.com
2023-04-20 18:04               ` gs-cygwin.com
2023-05-10  8:59       ` gs-cygwin.com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1752276.7aRn1RRit1@nimes \
    --to=bruno@clisp.org \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).