25 #include <sys/ptrace.h>
54 g_hash_table_iter_init(&iter,
probes);
55 while (g_hash_table_iter_next(&iter,
66 g_hash_table_destroy(
probes);
103 struct value *name_v;
108 rfilter_check(rf,name_v->
buf,&accept,NULL);
120 printf(
"Check found bad %d\t%d\t%s\n",
v_u32(pid_v),
v_u32(uid_v),name_v->
buf);
132 struct value *name_v;
138 rfilter_check(rf,name_v->
buf,&accept,NULL);
150 printf(
"Zombifying %d\t%d\t%s\n",
v_u32(pid_v),
v_u32(uid_v),name_v->
buf);
165 #define LOCAL_NSIG 64
166 #define LOCAL_NSIG_BPW 32
167 #define LOCAL_NSIG_WORDS (LOCAL_NSIG / LOCAL_NSIG_BPW)
177 static struct signame sigmap[] = {
180 {
"SIGQUIT",SIGQUIT },
182 {
"SIGTRAP",SIGTRAP },
183 {
"SIGABRT",SIGABRT },
187 {
"SIGKILL",SIGKILL },
188 {
"SIGUSR1",SIGUSR1 },
189 {
"SIGSEGV",SIGSEGV },
190 {
"SIGUSR2",SIGUSR2 },
191 {
"SIGPIPE",SIGPIPE },
192 {
"SIGALRM",SIGALRM },
193 {
"SIGTERM",SIGTERM },
194 {
"SIGSTKFLT",SIGSTKFLT },
196 {
"SIGCHLD",SIGCHLD },
197 {
"SIGCONT",SIGCONT },
198 {
"SIGSTOP",SIGSTOP },
199 {
"SIGTSTP",SIGTSTP },
200 {
"SIGTTIN",SIGTTIN },
201 {
"SIGTTOU",SIGTTOU },
203 {
"SIGXCPU",SIGXCPU },
204 {
"SIGXFSZ",SIGXFSZ },
205 {
"SIGVTALRM",SIGVTALRM },
206 {
"SIGPROF",SIGPROF },
207 {
"SIGWINCH",SIGWINCH },
208 {
"SIGPOLL",SIGPOLL },
215 static int convert_arg_to_sig(
char *arg) {
218 char *argcopy = NULL;
223 argcopy = strdup(arg);
224 len = strlen(argcopy);
225 for (i = 0; i <
len; ++
i)
226 argcopy[i] = toupper(argcopy[i]);
228 for (i = 0; i <
sizeof(sigmap) /
sizeof(
struct signame); ++
i) {
231 if (strcmp(sigmap[i].name,argcopy) == 0) {
240 len = strlen(arg) + 3 + 1;
241 argcopy = (
char *)
malloc(len);
242 snprintf(argcopy,len,
"SIG%s",arg);
243 for (i = 0; i <
len; ++
i)
244 argcopy[i] = toupper(argcopy[i]);
245 for (i = 0; i <
sizeof(sigmap) /
sizeof(
struct signame); ++
i) {
248 if (strcmp(sigmap[i].name,argcopy) == 0) {
258 static char *convert_sig_to_name(
int signo) {
261 for (i = 0; i <
sizeof(sigmap) /
sizeof(
struct signame); ++
i) {
264 if (sigmap[i].signo == signo)
265 return sigmap[
i].
name;
278 struct value *name_v;
282 struct value *signal_v;
283 struct value *signal_pending_v;
284 struct value *signal_pending_signal_v;
286 int sig = psa_siginfo->
sig;
287 uint32_t sigmask = 1UL << (sig - 1);
288 struct value *thread_info_v;
291 rfilter_check(rf,name_v->
buf,&accept,NULL);
303 printf(
"Sending %s (%d) to %d\t%d\t%s\n",
304 convert_sig_to_name(sig),sig,
v_u32(pid_v),
v_u32(uid_v),name_v->
buf);
326 printf(
" ERROR: could not setup pending signal %d!\n",sig);
379 #define LOCAL_TIF_SIGPENDING (1UL << 2)
384 if (!thread_info_v) {
391 printf(
" ERROR: could not load stack (thread_info) in"
392 " task %d\t%d\t%s!\n",
402 if (!thread_info_v) {
403 printf(
" ERROR: could not load stack (thread_info)"
404 " in task %d\t%d\t%s!\n",
423 struct value *name_v;
424 struct value *signal_v;
425 struct value *signal_pending_v;
426 struct value *signal_pending_signal_v;
428 uint32_t sigkillmask = 1 << 9;
429 struct value *thread_info_v;
438 printf(
"Killing %d\t%d\t%s\n",
v_u32(pid_v),
v_u32(uid_v),name_v->
buf);
460 printf(
" ERROR: could not kill!\n");
471 #define LOCAL_SIGNAL_GROUP_EXIT 0x00000008
491 #define LOCAL_TIF_SIGPENDING (1UL << 2)
496 if (!thread_info_v) {
503 printf(
" ERROR: could not load stack (thread_info) in"
504 " task %d\t%d\t%s!\n",
514 if (!thread_info_v) {
515 printf(
" ERROR: could not load stack (thread_info)"
516 " in task %d\t%d\t%s!\n",
533 struct value *name_v;
538 rfilter_check(rf,name_v->
buf,&accept,NULL);
591 list_add_tail(¤t->
tasks,&((*head)->tasks));
596 current->
comm = strdup(v->
buf);
680 return ARGP_ERR_UNKNOWN;
683 if (state->quoted > 0)
684 opts->
argc = state->quoted - state->next;
686 opts->
argc = state->argc - state->next;
687 if (opts->
argc > 0) {
689 memcpy(opts->
argv,&state->argv[state->next],opts->
argc*
sizeof(
char *));
690 state->next += opts->
argc;
697 case ARGP_KEY_NO_ARGS:
698 case ARGP_KEY_SUCCESS:
706 return ARGP_ERR_UNKNOWN;
716 int main(
int argc,
char **argv) {
722 struct bsymbol *init_task_bsymbol;
760 verror(
"could not parse target arguments!\n");
765 fprintf(stderr,
"ERROR: must supply a command!\n");
771 if (strcmp(command,
"list") == 0
772 || strcmp(command,
"watch") == 0)
774 else if (strcmp(command,
"check") == 0
775 || strcmp(command,
"zombie") == 0
776 || strcmp(command,
"kill") == 0) {
778 fprintf(stderr,
"ERROR: check|zombie|kill commands must"
779 " be followed by an rfilter!\n");
782 rf = rfilter_create_parse(
opts.
argv[1]);
784 fprintf(stderr,
"ERROR: bad rfilter '%s'!\n",
opts.
argv[1]);
788 else if (strcmp(command,
"sig") == 0) {
790 fprintf(stderr,
"ERROR: sig command must"
791 " be followed by a signal name/number and an rfilter!\n");
794 psa_siginfo = (
struct psa_siginfo *)
calloc(1,
sizeof(*psa_siginfo));
795 psa_siginfo->
sig = convert_arg_to_sig(
opts.
argv[1]);
796 if (psa_siginfo->
sig < 1) {
797 fprintf(stderr,
"ERROR: bad signal string '%s'!\n",
opts.
argv[1]);
800 psa_siginfo->
rf = rfilter_create_parse(
opts.
argv[2]);
801 if (!psa_siginfo->
rf) {
802 fprintf(stderr,
"ERROR: bad rfilter '%s'!\n",
opts.
argv[2]);
806 else if (strcmp(command,
"hiercheck") == 0
807 || strcmp(command,
"hierkill") == 0) {
809 fprintf(stderr,
"ERROR: hiercheck|hierkill commands must"
810 " be followed by one or more process hierarchy regexps!\n");
813 regexp_list = array_list_create(
opts.
argc - 1);
816 preg = (regex_t *)
malloc(
sizeof(regex_t));
817 if ((rc = regcomp(preg,
opts.
argv[i],REG_EXTENDED | REG_NOSUB))) {
818 regerror(rc,preg,errbuf,64);
819 fprintf(stderr,
"ERROR: bad regexp '%s': %s\n",
opts.
argv[i],errbuf);
822 array_list_append(regexp_list,preg);
826 else if (strcmp(command,
"dump") == 0) {
828 fprintf(stderr,
"ERROR: dump command must"
829 " be followed by a list of variables to dump!\n");
834 fprintf(stderr,
"ERROR: command must be one of"
835 " list|dump|check|zombie|sig|kill!\n");
841 verror(
"could not instantiate target!\n");
847 fprintf(stderr,
"could not open %s!\n",targetstr);
851 if (strcmp(command,
"dump") == 0) {
855 fprintf(stderr,
"ERROR: could not lookup %s!\n",
opts.
argv[i]);
865 fprintf(stderr,
"ERROR: could not load value for %s!\n",
882 if (!init_task_bsymbol) {
883 fprintf(stderr,
"ERROR: could not find init_task symbol!\n");
890 if (strcmp(command,
"list") == 0) {
891 printf(
"PID\tUID\tProcess Name\n");
896 else if (strcmp(command,
"check") == 0) {
901 else if (strcmp(command,
"zombie") == 0) {
906 else if (strcmp(command,
"sig") == 0) {
911 else if (strcmp(command,
"kill") == 0) {
916 else if (strcmp(command,
"hiercheck") == 0
917 || strcmp(command,
"hierkill") == 0) {
953 if ((csize - clen) < (strlen(tj->
comm) + 2)) {
958 rc = snprintf(ti->
comm_hier + clen,csize - clen,
":%s",
961 rc = snprintf(ti->
comm_hier + clen,csize - clen,
"%s",
975 for (i = 0; i < array_list_len(regexp_list); ++
i) {
976 preg = (regex_t *)array_list_item(regexp_list,i);
977 if (regexec(preg,ti->
comm_hier,0,NULL,0) == 0)
980 if (i == array_list_len(regexp_list)) {
981 if (strcmp(command,
"hiercheck") == 0) {
982 printf(
"Disallowed process: %d\t%d\t%s (not killing)\n",
999 signal(SIGQUIT,
sigh);
1000 signal(SIGABRT,
sigh);
1001 signal(SIGKILL,
sigh);
1002 signal(SIGSEGV,
sigh);
1003 signal(SIGPIPE,
sigh);
1004 signal(SIGALRM,
sigh);
1005 signal(SIGTERM,
sigh);
1006 signal(SIGUSR1,
sigh);
1007 signal(SIGUSR2,
sigh);
1018 fprintf(stdout,
"Starting watch loop!\n");
1026 printf(
"%s interrupted at 0x%" PRIxREGVAL "\n",targetstr,
1033 fprintf(stderr,
"could not resume target domain %s\n",targetstr);
1048 printf(
"%s finished.\n",targetstr);
1052 printf(
"%s monitoring failed!\n",targetstr);
1056 printf(
"%s monitoring failed with %d!\n",targetstr,tstat);
struct value * target_load_symbol(struct target *target, struct target_location_ctxt *tlctxt, struct bsymbol *bsymbol, load_flags_t flags)
struct value * target_load_type(struct target *target, struct symbol *type, ADDR addr, load_flags_t flags)
struct argp_option psa_argp_opts[]
int probe_unregister(struct probe *probe, int force)
int value_update_i32(struct value *value, int32_t v)
void * target_argp_driver_state(struct argp_state *state)
int __ps_kill(struct target *target, struct value *value)
struct target_spec * tspec
static uint64_t unsigned int i
struct bsymbol * target_lookup_sym(struct target *target, const char *name, const char *delim, char *srcfile, symbol_type_flag_t ftype)
target_status_t cleanup()
int target_resume(struct target *target)
struct target_location_ctxt * target_location_ctxt_create_from_bsymbol(struct target *target, tid_t tid, struct bsymbol *bsymbol)
struct linux_task_struct * real_parent
int target_pause(struct target *target)
void value_dump(struct value *value, struct dump_info *ud)
int value_update_num(struct value *value, num_t v)
int target_close(struct target *target)
#define verror(format,...)
int os_linux_list_for_each_struct(struct target *t, struct bsymbol *bsymbol, char *list_head_member_name, int nofree, os_linux_list_iterator_t iterator, void *data)
#define LOCAL_TIF_SIGPENDING
int target_snprintf(struct target *target, char *buf, int bufsiz)
REGVAL target_read_reg(struct target *target, tid_t tid, REG reg)
#define list_for_each_entry(pos, head, member)
int main(int argc, char **argv)
REFCNT bsymbol_release(struct bsymbol *bsymbol)
int probe_free(struct probe *probe, int force)
#define LOCAL_SIGNAL_GROUP_EXIT
int pslist_load(struct target *target, struct value *value, void *data)
int pslist_check(struct target *target, struct value *value, void *data)
void value_free(struct value *value)
struct target_location_ctxt * target_global_tlctxt(struct target *target)
struct value * target_load_value_member(struct target *target, struct target_location_ctxt *tlctxt, struct value *old_value, const char *member, const char *delim, load_flags_t flags)
int target_store_value(struct target *target, struct value *value)
ADDR v_addr(struct value *v)
void * realloc(void *ptr, size_t size)
struct psa_argp_state opts
int pslist_list(struct target *target, struct value *value, void *data)
void * calloc(size_t nmemb, size_t size)
struct target_spec * tspec
struct linux_task_struct * parent
error_t psa_argp_parse_opt(int key, char *arg, struct argp_state *state)
int pslist_sig(struct target *target, struct value *value, void *data)
int target_finalize(struct target *target)
int target_open(struct target *target)
int32_t v_i32(struct value *v)
target_status_t target_monitor(struct target *target)
struct target_spec * target_argp_target_spec(struct argp_state *state)
void target_location_ctxt_free(struct target_location_ctxt *tlctxt)
struct target_spec * target_argp_driver_parse_one(struct argp *driver_parser, void *driver_state, int argc, char **argv, target_type_t target_types, int filter_quoted)
int value_update_u32(struct value *value, uint32_t v)
struct target * target_instantiate(struct target_spec *spec, struct evloop *evloop)
void * malloc(size_t size)
int value_update_zero(struct value *value, const char *buf, int bufsiz)
uint32_t v_u32(struct value *v)
#define INIT_LIST_HEAD(ptr)
ADDR value_addr(struct value *value)
void target_driver_argp_init_children(struct argp_state *state)
int pslist_kill(struct target *target, struct value *value, void *data)
int pslist_zombie(struct target *target, struct value *value, void *data)