code-intelligence.com/cifuzz@v0.40.0/third-party/minijail/minijail0.c (about)

     1  /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
     2   * Use of this source code is governed by a BSD-style license that can be
     3   * found in the LICENSE file.
     4   */
     5  
     6  #include <dlfcn.h>
     7  #include <errno.h>
     8  #include <stdio.h>
     9  #include <stdlib.h>
    10  #include <unistd.h>
    11  
    12  #include "libminijail.h"
    13  
    14  #include "elfparse.h"
    15  #include "minijail0_cli.h"
    16  #include "util.h"
    17  
    18  int main(int argc, char *argv[])
    19  {
    20  	struct minijail *j = minijail_new();
    21  	const char *dl_mesg = NULL;
    22  	const char *preload_path = PRELOADPATH;
    23  	int exit_immediately = 0;
    24  	ElfType elftype = ELFERROR;
    25  	int consumed = parse_args(j, argc, argv, &exit_immediately, &elftype,
    26  				  &preload_path);
    27  	argc -= consumed;
    28  	argv += consumed;
    29  
    30  	/*
    31  	 * Make the process group ID of this process equal to its PID.
    32  	 * In the non-interactive case (e.g. when minijail0 is started from
    33  	 * init) this ensures the parent process and the jailed process
    34  	 * can be killed together.
    35  	 *
    36  	 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
    37  	 * the process is already a process group leader.
    38  	 */
    39  	if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
    40  		if (errno != EPERM) {
    41  			fprintf(stderr, "setpgid(0, 0) failed\n");
    42  			exit(1);
    43  		}
    44  	}
    45  
    46  	if (elftype == ELFSTATIC) {
    47  		/*
    48  		 * Target binary is statically linked so we cannot use
    49  		 * libminijailpreload.so.
    50  		 */
    51  		minijail_run_no_preload(j, argv[0], argv);
    52  	} else if (elftype == ELFDYNAMIC) {
    53  		/*
    54  		 * Target binary is dynamically linked so we can
    55  		 * inject libminijailpreload.so into it.
    56  		 */
    57  
    58  		/* Check that we can dlopen() libminijailpreload.so. */
    59  		if (!dlopen(preload_path, RTLD_LAZY | RTLD_LOCAL)) {
    60  			dl_mesg = dlerror();
    61  			fprintf(stderr, "dlopen(): %s\n", dl_mesg);
    62  			return 1;
    63  		}
    64  		minijail_set_preload_path(j, preload_path);
    65  		minijail_run(j, argv[0], argv);
    66  	} else {
    67  		fprintf(stderr,
    68  			"Target program '%s' is not a valid ELF file.\n",
    69  			argv[0]);
    70  		return 1;
    71  	}
    72  
    73  	if (exit_immediately)
    74  		return 0;
    75  
    76  	int ret = minijail_wait(j);
    77  #if defined(__SANITIZE_ADDRESS__)
    78  	minijail_destroy(j);
    79  #endif /* __SANITIZE_ADDRESS__ */
    80  	return ret;
    81  }