public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Joe Simmons-Talbott <josimmon@redhat.com>
To: libc-alpha@sourceware.org
Cc: Joe Simmons-Talbott <josimmon@redhat.com>
Subject: [PATCH] system: Add "--" after "-c" for sh (BZ #28519)
Date: Wed, 22 Mar 2023 14:04:30 -0400	[thread overview]
Message-ID: <20230322180430.986512-1-josimmon@redhat.com> (raw)

Prevent sh from interpreting a user string as shell options if it
starts with '-' or '+'.  Since the version of /bin/sh used for testing
system() is different from the full-fledged system /bin/sh add support
to it for handling "--" after "-c".  Add a testcase to ensure the
expected behavior.

Signed-off-by: Joe Simmons-Talbott <josimmon@redhat.com>
---
 libio/iopopen.c           |  2 +-
 stdlib/tst-system.c       | 14 ++++++++++++++
 support/shell-container.c |  7 ++++++-
 sysdeps/posix/system.c    |  1 +
 4 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/libio/iopopen.c b/libio/iopopen.c
index d0545ad5ea..eef6d1ef18 100644
--- a/libio/iopopen.c
+++ b/libio/iopopen.c
@@ -89,7 +89,7 @@ spawn_process (posix_spawn_file_actions_t *fa, FILE *fp, const char *command,
     }
 
   err = __posix_spawn (&((_IO_proc_file *) fp)->pid, _PATH_BSHELL, fa, 0,
-		       (char *const[]){ (char*) "sh", (char*) "-c",
+		       (char *const[]){ (char*) "sh", (char*) "-c", (char*) "--",
 		       (char *) command, NULL }, __environ);
   if (err != 0)
     return err;
diff --git a/stdlib/tst-system.c b/stdlib/tst-system.c
index 47a0afe6bf..3a55ec2791 100644
--- a/stdlib/tst-system.c
+++ b/stdlib/tst-system.c
@@ -146,6 +146,20 @@ do_test (void)
     TEST_COMPARE_STRING (result.out.buffer, "...\n");
   }
 
+  {
+    struct support_capture_subprocess result;
+    const char *cmd = "-echo";
+    result = support_capture_subprocess (call_system,
+					 &(struct args) { cmd, 127 });
+    support_capture_subprocess_check (&result, "system", 0, sc_allow_stderr |
+			sc_allow_stdout);
+    char *returnerr = xasprintf ("%s: execing -echo failed: "
+				 "No such file or directory",
+				 basename(_PATH_BSHELL));
+    TEST_COMPARE_STRING (result.err.buffer, returnerr);
+    free (returnerr);
+  }
+
   {
     struct support_capture_subprocess result;
     result = support_capture_subprocess (call_system,
diff --git a/support/shell-container.c b/support/shell-container.c
index b1f9e793c1..28437e4206 100644
--- a/support/shell-container.c
+++ b/support/shell-container.c
@@ -455,7 +455,12 @@ main (int argc, const char **argv)
     dprintf (stderr, "  argv[%d] is `%s'\n", i, argv[i]);
 
   if (strcmp (argv[1], "-c") == 0)
-    run_command_string (argv[2], argv+3);
+    {
+      if (strcmp (argv[2], "--") == 0)
+		run_command_string (argv[3], argv+4);
+      else
+		run_command_string (argv[2], argv+3);
+    }
   else
     run_script (argv[1], argv+2);
 
diff --git a/sysdeps/posix/system.c b/sysdeps/posix/system.c
index d77720a625..488b95163b 100644
--- a/sysdeps/posix/system.c
+++ b/sysdeps/posix/system.c
@@ -147,6 +147,7 @@ do_system (const char *line)
   ret = __posix_spawn (&pid, SHELL_PATH, 0, &spawn_attr,
 		       (char *const[]){ (char *) SHELL_NAME,
 					(char *) "-c",
+					(char *) "--",
 					(char *) line, NULL },
 		       __environ);
   __posix_spawnattr_destroy (&spawn_attr);
-- 
2.39.2


             reply	other threads:[~2023-03-22 18:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22 18:04 Joe Simmons-Talbott [this message]
2023-03-27 18:56 ` Adhemerval Zanella Netto
2023-03-27 19:34   ` Cristian Rodríguez
2023-03-27 19:50     ` Zack Weinberg
2023-03-27 19:56       ` Adhemerval Zanella Netto
2023-03-27 20:14         ` Cristian Rodríguez
2023-03-28 12:52       ` Joe Simmons-Talbott
2023-03-28 13:02         ` Adhemerval Zanella Netto
2023-03-27 20:12     ` Andreas Schwab

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=20230322180430.986512-1-josimmon@redhat.com \
    --to=josimmon@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /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).