From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from butterfly.larch.relay.mailchannels.net (butterfly.larch.relay.mailchannels.net [23.83.213.27]) by sourceware.org (Postfix) with ESMTPS id AA0F2397300D for ; Wed, 14 Apr 2021 05:41:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org AA0F2397300D 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 37B0B36270B; Wed, 14 Apr 2021 05:41:49 +0000 (UTC) Received: from pdx1-sub0-mail-a20.g.dreamhost.com (100-98-55-74.trex.outbound.svc.cluster.local [100.98.55.74]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id D2A7F362550; Wed, 14 Apr 2021 05:41:46 +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.98.55.74 (trex/6.1.1); Wed, 14 Apr 2021 05:41:49 +0000 X-MC-Relay: Good X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Cooperative-Company: 52f6e27c1ad1a00b_1618378909035_3612833628 X-MC-Loop-Signature: 1618378909035:1382737594 X-MC-Ingress-Time: 1618378909034 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 944F77EFC2; Tue, 13 Apr 2021 22:41:46 -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 B84AC7EFBE; Tue, 13 Apr 2021 22:41:44 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a20 From: Siddhesh Poyarekar To: libc-stable@sourceware.org Cc: Carlos O'Donell Subject: [COMMITTED 2.29 4/6] tst-env-setuid: Use support_capture_subprogram_self_sgid Date: Wed, 14 Apr 2021 11:11:26 +0530 Message-Id: <20210414054128.1249310-4-siddhesh@sourceware.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210414054128.1249310-1-siddhesh@sourceware.org> References: <20210414054128.1249310-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_H3, 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 05:41:52 -0000 Use the support_capture_subprogram_self_sgid to spawn an sgid child. Reviewed-by: Carlos O'Donell (cherry picked from commit ca335281068a1ed549a75ee64f90a8310755956f) --- elf/tst-env-setuid.c | 197 +++---------------------------------------- 1 file changed, 14 insertions(+), 183 deletions(-) diff --git a/elf/tst-env-setuid.c b/elf/tst-env-setuid.c index a080d2fa1c..2350976f8c 100644 --- a/elf/tst-env-setuid.c +++ b/elf/tst-env-setuid.c @@ -29,173 +29,12 @@ #include #include =20 +#include #include #include +#include =20 static char SETGID_CHILD[] =3D "setgid-child"; -#define CHILD_STATUS 42 - -/* 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; -} - -/* Spawn and execute a program and verify that it returns the CHILD_STAT= US. */ -static pid_t -do_execve (char **args) -{ - pid_t kid =3D vfork (); - - if (kid < 0) - { - printf ("vfork: %m\n"); - return -1; - } - - if (kid =3D=3D 0) - { - /* Child process. */ - execve (args[0], args, environ); - _exit (-errno); - } - - if (kid < 0) - return 1; - - int status; - - if (waitpid (kid, &status, 0) < 0) - { - printf ("waitpid: %m\n"); - return 1; - } - - if (WEXITSTATUS (status) =3D=3D EXIT_UNSUPPORTED) - return EXIT_UNSUPPORTED; - - if (!WIFEXITED (status) || WEXITSTATUS (status) !=3D CHILD_STATUS) - { - printf ("Unexpected exit status %d from child process\n", - WEXITSTATUS (status)); - return 1; - } - 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/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 0; - 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; - } - - char *args[] =3D {execname, SETGID_CHILD, NULL}; - - ret =3D do_execve (args); - -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 #ifndef test_child static int @@ -256,40 +95,32 @@ do_test (int argc, char **argv) if (argc =3D=3D 2 && strcmp (argv[1], SETGID_CHILD) =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 (EXIT_UNSUPPORTED); - } + /* This can happen if the file system is mounted nosuid. */ + FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n", + (intmax_t) getgid ()); =20 int ret =3D test_child (); =20 if (ret !=3D 0) exit (1); =20 - exit (CHILD_STATUS); + exit (EXIT_SUCCESS); } else { if (test_parent () !=3D 0) exit (1); =20 - /* Try running a setgid program. */ - 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); - } + int status =3D support_capture_subprogram_self_sgid (SETGID_CHILD)= ; =20 - return run_executable_sgid (target); - } + if (WEXITSTATUS (status) =3D=3D EXIT_UNSUPPORTED) + return EXIT_UNSUPPORTED; + + if (!WIFEXITED (status)) + FAIL_EXIT1 ("Unexpected exit status %d from child process\n", status); =20 - /* Something went wrong and our argv was corrupted. */ - _exit (1); + return 0; + } } =20 #define TEST_FUNCTION_ARGV do_test --=20 2.29.2