xref: /netbsd-src/crypto/external/bsd/openssh/dist/sandbox-pledge.c (revision 17418e98f281f84e3d22de9717222f9c2ee84d3a)
1*17418e98Schristos /*	$NetBSD: sandbox-pledge.c,v 1.3 2021/03/05 17:47:16 christos Exp $	*/
2*17418e98Schristos /* $OpenBSD: sandbox-pledge.c,v 1.2 2020/10/18 11:32:01 djm Exp $ */
3*17418e98Schristos 
448b02105Schristos /*
548b02105Schristos  * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
648b02105Schristos  *
748b02105Schristos  * Permission to use, copy, modify, and distribute this software for any
848b02105Schristos  * purpose with or without fee is hereby granted, provided that the above
948b02105Schristos  * copyright notice and this permission notice appear in all copies.
1048b02105Schristos  *
1148b02105Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1248b02105Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1348b02105Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1448b02105Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1548b02105Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1648b02105Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1748b02105Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1848b02105Schristos  */
19ffae97bbSchristos #include "includes.h"
20*17418e98Schristos __RCSID("$NetBSD: sandbox-pledge.c,v 1.3 2021/03/05 17:47:16 christos Exp $");
2148b02105Schristos 
2248b02105Schristos #include <sys/types.h>
2348b02105Schristos #include <sys/ioctl.h>
2448b02105Schristos #include <sys/syscall.h>
2548b02105Schristos #include <sys/socket.h>
2648b02105Schristos #include <sys/wait.h>
2748b02105Schristos 
2848b02105Schristos #include <errno.h>
2948b02105Schristos #include <limits.h>
3048b02105Schristos #include <stdarg.h>
3148b02105Schristos #include <stdio.h>
3248b02105Schristos #include <stdlib.h>
3348b02105Schristos #include <unistd.h>
3448b02105Schristos #include <pwd.h>
3548b02105Schristos 
3648b02105Schristos #include "log.h"
3748b02105Schristos #include "ssh-sandbox.h"
3848b02105Schristos #include "xmalloc.h"
3948b02105Schristos 
4048b02105Schristos struct ssh_sandbox {
4148b02105Schristos 	pid_t child_pid;
4248b02105Schristos };
4348b02105Schristos 
4448b02105Schristos struct ssh_sandbox *
ssh_sandbox_init(void)4548b02105Schristos ssh_sandbox_init(void)
4648b02105Schristos {
4748b02105Schristos 	struct ssh_sandbox *box;
4848b02105Schristos 
49*17418e98Schristos 	debug3_f("preparing pledge sandbox");
5048b02105Schristos 	box = xcalloc(1, sizeof(*box));
5148b02105Schristos 	box->child_pid = 0;
5248b02105Schristos 
5348b02105Schristos 	return box;
5448b02105Schristos }
5548b02105Schristos 
5648b02105Schristos void
ssh_sandbox_child(struct ssh_sandbox * box)5748b02105Schristos ssh_sandbox_child(struct ssh_sandbox *box)
5848b02105Schristos {
5948b02105Schristos 	if (pledge("stdio", NULL) == -1)
60*17418e98Schristos 		fatal_f("pledge()");
6148b02105Schristos }
6248b02105Schristos 
6348b02105Schristos void
ssh_sandbox_parent_finish(struct ssh_sandbox * box)6448b02105Schristos ssh_sandbox_parent_finish(struct ssh_sandbox *box)
6548b02105Schristos {
6648b02105Schristos 	free(box);
67*17418e98Schristos 	debug3_f("finished");
6848b02105Schristos }
6948b02105Schristos 
7048b02105Schristos void
ssh_sandbox_parent_preauth(struct ssh_sandbox * box,pid_t child_pid)7148b02105Schristos ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
7248b02105Schristos {
7348b02105Schristos 	box->child_pid = child_pid;
7448b02105Schristos 	/* Nothing to do here */
7548b02105Schristos }
76