xref: /openbsd-src/regress/sys/kern/pledge/execpromise/execpromise.c (revision 498c7b5e98c82ca5a0055f676f2a6e4214e2d9a8)
1*498c7b5eSderaadt /*	$OpenBSD: execpromise.c,v 1.2 2021/12/13 18:04:28 deraadt Exp $	*/
29b6360c7Sflorian /*
39b6360c7Sflorian  * Copyright (c) 2017 Florian Obser <florian@openbsd.org>
49b6360c7Sflorian  *
59b6360c7Sflorian  * Permission to use, copy, modify, and distribute this software for any
69b6360c7Sflorian  * purpose with or without fee is hereby granted, provided that the above
79b6360c7Sflorian  * copyright notice and this permission notice appear in all copies.
89b6360c7Sflorian  *
99b6360c7Sflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
109b6360c7Sflorian  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119b6360c7Sflorian  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
129b6360c7Sflorian  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139b6360c7Sflorian  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149b6360c7Sflorian  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
159b6360c7Sflorian  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
169b6360c7Sflorian  */
179b6360c7Sflorian 
18*498c7b5eSderaadt #include <sys/types.h>
199b6360c7Sflorian #include <sys/socket.h>
209b6360c7Sflorian 
219b6360c7Sflorian #include <err.h>
229b6360c7Sflorian #include <errno.h>
239b6360c7Sflorian #include <stdio.h>
249b6360c7Sflorian #include <stdlib.h>
259b6360c7Sflorian #include <unistd.h>
269b6360c7Sflorian 
279b6360c7Sflorian int
main(int argc,char ** argv)289b6360c7Sflorian main(int argc, char **argv)
299b6360c7Sflorian {
309b6360c7Sflorian 	int ch, child = 0, s;
319b6360c7Sflorian 	char **oargv = argv;
329b6360c7Sflorian 
339b6360c7Sflorian 	while ((ch = getopt(argc, argv, "C")) != -1) {
349b6360c7Sflorian 		switch (ch) {
359b6360c7Sflorian 		case 'C':
369b6360c7Sflorian 			child = 1;
379b6360c7Sflorian 			break;
389b6360c7Sflorian 		default:
399b6360c7Sflorian 			errx(1, "");
409b6360c7Sflorian 		}
419b6360c7Sflorian 	}
429b6360c7Sflorian 	argc -= optind;
439b6360c7Sflorian 	argv += optind;
449b6360c7Sflorian 
459b6360c7Sflorian 	if (child ==1) {
469b6360c7Sflorian 		warnx("child");
479b6360c7Sflorian 		if (argc > 1)
489b6360c7Sflorian 			errx(1, "argc: %d", argc);
499b6360c7Sflorian 		if (argc == 1) {
509b6360c7Sflorian 			warnx("plege(\"%s\",\"\")", argv[0]);
519b6360c7Sflorian 			if (pledge(argv[0], "") == -1)
529b6360c7Sflorian 				err(24, "child pledge");
539b6360c7Sflorian 		}
549b6360c7Sflorian 
559b6360c7Sflorian 		warnx("trying to open socket");
569b6360c7Sflorian 
579b6360c7Sflorian 		s = socket(AF_INET, SOCK_DGRAM, 0);
589b6360c7Sflorian 		if (s == -1)
599b6360c7Sflorian 			err(23, "open");
609b6360c7Sflorian 		else
619b6360c7Sflorian 			warnx("opened socket");
629b6360c7Sflorian 
639b6360c7Sflorian 		close(s);
649b6360c7Sflorian 		exit(0);
659b6360c7Sflorian 	} else {
669b6360c7Sflorian 		warnx("parent");
679b6360c7Sflorian 		if (argc == 2)
689b6360c7Sflorian 			warnx("execpromise: \"%s\", child pledge: \"%s\"",
699b6360c7Sflorian 			    argv[0], argv[1]);
709b6360c7Sflorian 		else if (argc == 1)
719b6360c7Sflorian 			warnx("execpromise: \"%s\"", argv[0]);
729b6360c7Sflorian 		else
739b6360c7Sflorian 			errx(1, "argc out of range");
749b6360c7Sflorian 
759b6360c7Sflorian 		if (pledge("stdio exec", argv[0]) == -1)
769b6360c7Sflorian 			err(1, "parent pledge");
779b6360c7Sflorian 
789b6360c7Sflorian 		oargv[1] = "-C";
799b6360c7Sflorian 		execvp(oargv[0], &oargv[0]);
809b6360c7Sflorian 		err((errno == ENOENT) ? 127 : 126, "%s", argv[0]);
819b6360c7Sflorian 	}
829b6360c7Sflorian }
83