Index: libexec/rtld-elf/rtld.c =================================================================== --- libexec/rtld-elf/rtld.c +++ libexec/rtld-elf/rtld.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include @@ -214,6 +215,7 @@ static Obj_Entry obj_rtld; /* The dynamic linker shared object */ static unsigned int obj_count; /* Number of objects in obj_list */ static unsigned int obj_loads; /* Number of loads of objects (gen count) */ +static int jid; /* JID to attach jail to */ static Objlist list_global = /* Objects dlopened with RTLD_GLOBAL */ STAILQ_HEAD_INITIALIZER(list_global); @@ -421,6 +423,7 @@ md_abi_variant_hook(aux_info); fd = -1; + jid = -1; if (aux_info[AT_EXECFD] != NULL) { fd = aux_info[AT_EXECFD]->a_un.a_val; } else { @@ -779,6 +782,13 @@ /* Return the exit procedure and the program entry point. */ *exit_proc = rtld_exit; *objp = obj_main; + + /* Attach to a jail */ + if (jid > 0 && jail_attach(jid) == -1) { + _rtld_error("Could not attach to jail"); + rtld_die(); + } + return (func_ptr_type) obj_main->entry; } @@ -5510,6 +5520,20 @@ } *fdp = fd; break; + } else if (opt == 'j') { + if (j != arglen - 1) { + /* -j must be the last option in, e.g., -abcj */ + _rtld_error("Invalid options: %s", arg); + rtld_die(); + } + i++; + jid = parse_integer(argv[i]); + if (jid == -1) { + _rtld_error("Invalid JID: '%s'", + argv[i]); + rtld_die(); + } + break; } else if (opt == 'p') { *use_pathp = true; } else { @@ -5554,12 +5578,13 @@ print_usage(const char *argv0) { - rtld_printf("Usage: %s [-h] [-f ] [--] []\n" + rtld_printf("Usage: %s [-h] [-f ] [-j ] [--] []\n" "\n" "Options:\n" " -h Display this help message\n" " -p Search in PATH for named binary\n" " -f Execute instead of searching for \n" + " -j Execute after attaching to jail with JID \n" " -- End of RTLD options\n" " Name of process to execute\n" " Arguments to the executed process\n", argv0);