From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from chameleon.elm.relay.mailchannels.net (chameleon.elm.relay.mailchannels.net [23.83.212.33]) by sourceware.org (Postfix) with ESMTPS id A6C7F396EC43 for ; Wed, 14 Apr 2021 06:21:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A6C7F396EC43 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 68047701D44; Wed, 14 Apr 2021 06:21:38 +0000 (UTC) Received: from pdx1-sub0-mail-a20.g.dreamhost.com (100-101-162-40.trex.outbound.svc.cluster.local [100.101.162.40]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 03315702071; Wed, 14 Apr 2021 06:21:38 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a20.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.101.162.40 (trex/6.1.1); Wed, 14 Apr 2021 06:21:38 +0000 X-MC-Relay: Good X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Reign-Oafish: 0dc6d03f5c35822b_1618381298262_3988347067 X-MC-Loop-Signature: 1618381298262:2119840472 X-MC-Ingress-Time: 1618381298262 Received: from pdx1-sub0-mail-a20.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a20.g.dreamhost.com (Postfix) with ESMTP id AEE477EFBE; Tue, 13 Apr 2021 23:21:37 -0700 (PDT) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a20.g.dreamhost.com (Postfix) with ESMTPSA id 8F8407EFBB; Tue, 13 Apr 2021 23:21:35 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a20 From: Siddhesh Poyarekar To: libc-stable@sourceware.org Cc: Carlos O'Donell Subject: [COMMITTED 2.28 4/7] support: Add capability to fork an sgid child Date: Wed, 14 Apr 2021 11:51:15 +0530 Message-Id: <20210414062118.1411358-4-siddhesh@sourceware.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210414062118.1411358-1-siddhesh@sourceware.org> References: <20210414062118.1411358-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3494.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-stable@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-stable mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Apr 2021 06:21:42 -0000 Add a new function support_capture_subprogram_self_sgid that spawns an sgid child of the running program with its own image and returns the exit code of the child process. This functionality is used by at least three tests in the testsuite at the moment, so it makes sense to consolidate. There is also a new function support_subprogram_wait which should provide simple system() like functionality that does not set up file actions. This is useful in cases where only the return code of the spawned subprocess is interesting. This patch also ports tst-secure-getenv to this new function. A subsequent patch will port other tests. This also brings an important change to tst-secure-getenv behaviour. Now instead of succeeding, the test fails as UNSUPPORTED if it is unable to spawn a setgid child, which is how it should have been in the first place. Reviewed-by: Carlos O'Donell (cherry picked from commit 716a3bdc41b2b4b864dc64475015ba51e35e1273) --- stdlib/tst-secure-getenv.c | 188 +++------------------------ support/capture_subprocess.h | 6 + support/subprocess.h | 5 + support/support_capture_subprocess.c | 126 ++++++++++++++++++ support/support_subprocess.c | 13 ++ 5 files changed, 168 insertions(+), 170 deletions(-) diff --git a/stdlib/tst-secure-getenv.c b/stdlib/tst-secure-getenv.c index a682b7493e..156c92fea2 100644 --- a/stdlib/tst-secure-getenv.c +++ b/stdlib/tst-secure-getenv.c @@ -30,156 +30,12 @@ #include #include =20 +#include #include +#include #include =20 static char MAGIC_ARGUMENT[] =3D "run-actual-test"; -#define MAGIC_STATUS 19 - -/* Return a GID which is not our current GID, but is present in the - supplementary group list. */ -static gid_t -choose_gid (void) -{ - const int count =3D 64; - gid_t groups[count]; - int ret =3D getgroups (count, groups); - if (ret < 0) - { - printf ("getgroups: %m\n"); - exit (1); - } - gid_t current =3D getgid (); - for (int i =3D 0; i < ret; ++i) - { - if (groups[i] !=3D current) - return groups[i]; - } - return 0; -} - - -/* Copies the executable into a restricted directory, so that we can - safely make it SGID with the TARGET group ID. Then runs the - executable. */ -static int -run_executable_sgid (gid_t target) -{ - char *dirname =3D xasprintf ("%s/secure-getenv.%jd", - test_dir, (intmax_t) getpid ()); - char *execname =3D xasprintf ("%s/bin", dirname); - int infd =3D -1; - int outfd =3D -1; - int ret =3D -1; - if (mkdir (dirname, 0700) < 0) - { - printf ("mkdir: %m\n"); - goto err; - } - infd =3D open ("/proc/self/exe", O_RDONLY); - if (infd < 0) - { - printf ("open (/proc/self/exe): %m\n"); - goto err; - } - outfd =3D open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700); - if (outfd < 0) - { - printf ("open (%s): %m\n", execname); - goto err; - } - char buf[4096]; - for (;;) - { - ssize_t rdcount =3D read (infd, buf, sizeof (buf)); - if (rdcount < 0) - { - printf ("read: %m\n"); - goto err; - } - if (rdcount =3D=3D 0) - break; - char *p =3D buf; - char *end =3D buf + rdcount; - while (p !=3D end) - { - ssize_t wrcount =3D write (outfd, buf, end - p); - if (wrcount =3D=3D 0) - errno =3D ENOSPC; - if (wrcount <=3D 0) - { - printf ("write: %m\n"); - goto err; - } - p +=3D wrcount; - } - } - if (fchown (outfd, getuid (), target) < 0) - { - printf ("fchown (%s): %m\n", execname); - goto err; - } - if (fchmod (outfd, 02750) < 0) - { - printf ("fchmod (%s): %m\n", execname); - goto err; - } - if (close (outfd) < 0) - { - printf ("close (outfd): %m\n"); - goto err; - } - if (close (infd) < 0) - { - printf ("close (infd): %m\n"); - goto err; - } - - int kid =3D fork (); - if (kid < 0) - { - printf ("fork: %m\n"); - goto err; - } - if (kid =3D=3D 0) - { - /* Child process. */ - char *args[] =3D { execname, MAGIC_ARGUMENT, NULL }; - execve (execname, args, environ); - printf ("execve (%s): %m\n", execname); - _exit (1); - } - int status; - if (waitpid (kid, &status, 0) < 0) - { - printf ("waitpid: %m\n"); - goto err; - } - if (!WIFEXITED (status) || WEXITSTATUS (status) !=3D MAGIC_STATUS) - { - printf ("Unexpected exit status %d from child process\n", - status); - goto err; - } - ret =3D 0; - -err: - if (outfd >=3D 0) - close (outfd); - if (infd >=3D 0) - close (infd); - if (execname) - { - unlink (execname); - free (execname); - } - if (dirname) - { - rmdir (dirname); - free (dirname); - } - return ret; -} =20 static int do_test (void) @@ -201,15 +57,15 @@ do_test (void) exit (1); } =20 - gid_t target =3D choose_gid (); - if (target =3D=3D 0) - { - fprintf (stderr, - "Could not find a suitable GID for user %jd, skipping test\n", - (intmax_t) getuid ()); - exit (0); - } - return run_executable_sgid (target); + int status =3D support_capture_subprogram_self_sgid (MAGIC_ARGUMENT); + + if (WEXITSTATUS (status) =3D=3D EXIT_UNSUPPORTED) + return EXIT_UNSUPPORTED; + + if (!WIFEXITED (status)) + FAIL_EXIT1 ("Unexpected exit status %d from child process\n", status= ); + + return 0; } =20 static void @@ -218,23 +74,15 @@ alternative_main (int argc, char **argv) if (argc =3D=3D 2 && strcmp (argv[1], MAGIC_ARGUMENT) =3D=3D 0) { if (getgid () =3D=3D getegid ()) - { - /* This can happen if the file system is mounted nosuid. */ - fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n", - (intmax_t) getgid ()); - exit (MAGIC_STATUS); - } + /* This can happen if the file system is mounted nosuid. */ + FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n", + (intmax_t) getgid ()); if (getenv ("PATH") =3D=3D NULL) - { - printf ("PATH variable not present\n"); - exit (3); - } + FAIL_EXIT (3, "PATH variable not present\n"); if (secure_getenv ("PATH") !=3D NULL) - { - printf ("PATH variable not filtered out\n"); - exit (4); - } - exit (MAGIC_STATUS); + FAIL_EXIT (4, "PATH variable not filtered out\n"); + + exit (EXIT_SUCCESS); } } =20 diff --git a/support/capture_subprocess.h b/support/capture_subprocess.h index d5eac84d09..0ed1173a9f 100644 --- a/support/capture_subprocess.h +++ b/support/capture_subprocess.h @@ -41,6 +41,12 @@ struct support_capture_subprocess support_capture_subp= rocess struct support_capture_subprocess support_capture_subprogram (const char *file, char *const argv[]); =20 +/* Copy the running program into a setgid binary and run it with CHILD_I= D + argument. If execution is successful, return the exit status of the = child + program, otherwise return a non-zero failure exit code. */ +int support_capture_subprogram_self_sgid + (char *child_id); + /* Deallocate the subprocess data captured by support_capture_subprocess. */ void support_capture_subprocess_free (struct support_capture_subprocess = *); diff --git a/support/subprocess.h b/support/subprocess.h index c031878d94..a19335ee5d 100644 --- a/support/subprocess.h +++ b/support/subprocess.h @@ -38,6 +38,11 @@ struct support_subprocess support_subprocess struct support_subprocess support_subprogram (const char *file, char *const argv[]); =20 +/* Invoke program FILE with ARGV arguments by using posix_spawn and wait= for it + to complete. Return program exit status. */ +int support_subprogram_wait + (const char *file, char *const argv[]); + /* Wait for the subprocess indicated by PROC::PID. Return the status indicate by waitpid call. */ int support_process_wait (struct support_subprocess *proc); diff --git a/support/support_capture_subprocess.c b/support/support_captu= re_subprocess.c index 7b3d96c305..c64dcf5caf 100644 --- a/support/support_capture_subprocess.c +++ b/support/support_capture_subprocess.c @@ -20,11 +20,14 @@ #include =20 #include +#include #include #include #include #include #include +#include +#include =20 static void transfer (const char *what, struct pollfd *pfd, struct xmemstream *strea= m) @@ -102,6 +105,129 @@ support_capture_subprogram (const char *file, char = *const argv[]) return result; } =20 +/* Copies the executable into a restricted directory, so that we can + safely make it SGID with the TARGET group ID. Then runs the + executable. */ +static int +copy_and_spawn_sgid (char *child_id, gid_t gid) +{ + char *dirname =3D xasprintf ("%s/tst-tunables-setuid.%jd", + test_dir, (intmax_t) getpid ()); + char *execname =3D xasprintf ("%s/bin", dirname); + int infd =3D -1; + int outfd =3D -1; + int ret =3D 1, status =3D 1; + + TEST_VERIFY (mkdir (dirname, 0700) =3D=3D 0); + if (support_record_failure_is_failed ()) + goto err; + + infd =3D open ("/proc/self/exe", O_RDONLY); + if (infd < 0) + FAIL_UNSUPPORTED ("unsupported: Cannot read binary from procfs\n"); + + outfd =3D open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700); + TEST_VERIFY (outfd >=3D 0); + if (support_record_failure_is_failed ()) + goto err; + + char buf[4096]; + for (;;) + { + ssize_t rdcount =3D read (infd, buf, sizeof (buf)); + TEST_VERIFY (rdcount >=3D 0); + if (support_record_failure_is_failed ()) + goto err; + if (rdcount =3D=3D 0) + break; + char *p =3D buf; + char *end =3D buf + rdcount; + while (p !=3D end) + { + ssize_t wrcount =3D write (outfd, buf, end - p); + if (wrcount =3D=3D 0) + errno =3D ENOSPC; + TEST_VERIFY (wrcount > 0); + if (support_record_failure_is_failed ()) + goto err; + p +=3D wrcount; + } + } + TEST_VERIFY (fchown (outfd, getuid (), gid) =3D=3D 0); + if (support_record_failure_is_failed ()) + goto err; + TEST_VERIFY (fchmod (outfd, 02750) =3D=3D 0); + if (support_record_failure_is_failed ()) + goto err; + TEST_VERIFY (close (outfd) =3D=3D 0); + if (support_record_failure_is_failed ()) + goto err; + TEST_VERIFY (close (infd) =3D=3D 0); + if (support_record_failure_is_failed ()) + goto err; + + /* We have the binary, now spawn the subprocess. Avoid using + support_subprogram because we only want the program exit status, no= t the + contents. */ + ret =3D 0; + + char * const args[] =3D {execname, child_id, NULL}; + + status =3D support_subprogram_wait (args[0], args); + +err: + if (outfd >=3D 0) + close (outfd); + if (infd >=3D 0) + close (infd); + if (execname !=3D NULL) + { + unlink (execname); + free (execname); + } + if (dirname !=3D NULL) + { + rmdir (dirname); + free (dirname); + } + + if (ret !=3D 0) + FAIL_EXIT1("Failed to make sgid executable for test\n"); + + return status; +} + +int +support_capture_subprogram_self_sgid (char *child_id) +{ + gid_t target =3D 0; + const int count =3D 64; + gid_t groups[count]; + + /* Get a GID which is not our current GID, but is present in the + supplementary group list. */ + int ret =3D getgroups (count, groups); + if (ret < 0) + FAIL_UNSUPPORTED("Could not get group list for user %jd\n", + (intmax_t) getuid ()); + + gid_t current =3D getgid (); + for (int i =3D 0; i < ret; ++i) + { + if (groups[i] !=3D current) + { + target =3D groups[i]; + break; + } + } + + if (target =3D=3D 0) + FAIL_UNSUPPORTED("Could not find a suitable GID for user %jd\n", + (intmax_t) getuid ()); + + return copy_and_spawn_sgid (child_id, target); +} + void support_capture_subprocess_free (struct support_capture_subprocess *p) { diff --git a/support/support_subprocess.c b/support/support_subprocess.c index ae03c4b473..97e481e2d9 100644 --- a/support/support_subprocess.c +++ b/support/support_subprocess.c @@ -92,6 +92,19 @@ support_subprogram (const char *file, char *const argv= []) return result; } =20 +int +support_subprogram_wait (const char *file, char *const argv[]) +{ + posix_spawn_file_actions_t fa; + + posix_spawn_file_actions_init (&fa); + struct support_subprocess res =3D support_subprocess_init (); + + res.pid =3D xposix_spawn (file, &fa, NULL, argv, environ); + + return support_process_wait (&res); +} + int support_process_wait (struct support_subprocess *proc) { --=20 2.29.2