void remove_mod(char *file) { int ret=0,i=0,j=0,flag=0; ret = check_module(file); if(ret == -1){ cprintf("\n\t***** Module:%s is Not loaded *****\t\n\n",file); return; } if(modules_lkm[ret].load_status != 1){ cprintf("\n\t***** Module:%s is Not loaded *****\t\n\n",file); return; } for(i=0; i<MAX_LKM; i++){ if(dependency_graph[i][ret] == 1 ){ flag=1; break; } } if(flag == 1){ cprintf("\n***Module cannot be removed because following modules are dependent on it ***\n"); for(i=0; i<MAX_LKM; i++){ if(dependency_graph[i][ret] == 1 ){ cprintf("Module Name: %s \n",modules_lkm[i].name); } } return; } modules_lkm[ret].unload_mod(); remove_reverse_dependency(&modules_lkm[ret], ret); flush_module(&modules_lkm[ret], ret); }
Elf_Scn * dwfl_module_address_section (Dwfl_Module *mod, Dwarf_Addr *address, Dwarf_Addr *bias) { if (check_module (mod)) return NULL; int idx = find_section (mod, address); if (idx < 0) return NULL; if (mod->reloc_info->refs[idx].relocs != NULL) { assert (mod->e_type == ET_REL); Elf_Scn *tscn = mod->reloc_info->refs[idx].scn; Elf_Scn *relocscn = mod->reloc_info->refs[idx].relocs; Dwfl_Error result = __libdwfl_relocate_section (mod, mod->main.elf, relocscn, tscn, true); if (likely (result == DWFL_E_NOERROR)) mod->reloc_info->refs[idx].relocs = NULL; else { __libdwfl_seterrno (result); return NULL; } } *bias = dwfl_adjusted_address (mod, 0); return mod->reloc_info->refs[idx].scn; }
int oscap_module_process(struct oscap_module *module, int argc, char **argv) { assert(module != NULL); assert(argv != NULL); int ret = OSCAP_OK; struct oscap_action action; oscap_action_init(&action); optind = 0; while (module) { check_module(module); ++optind; // skip current module key index action.module = module; // update current module getopt_parse_env(module, &argc, &argv); switch (oscap_parse_common_opts(argc, argv)) { case OPT_HELP: oscap_module_print_help(module, stdout); goto cleanup; case OPT_LISTMODS: oscap_print_submodules(module, stdout, "\n", false); goto cleanup; case OPT_LISTALLMODS: oscap_print_submodules(module, stdout, "\n", true); goto cleanup; case OPT_MODTREE: oscap_module_tree(module, stdout, 0); goto cleanup; default: break; } if (module->opt_parser) { if (!module->opt_parser(argc, argv, &action)) { ret = OSCAP_BADARGS; goto cleanup; } module = action.module; // module might have changed } if (module->func) { ret = oscap_module_call(&action); goto cleanup; } else if (module->submodules) { struct oscap_module *old_mod = module; module = oscap_module_find(module->submodules, argv[optind]); if (module == NULL) { if (argv[optind] != NULL) fprintf(stderr, "No such module: %s\n", argv[optind]); else oscap_module_usage(old_mod, stderr, NULL); ret = OSCAP_BADMODULE; } } else { fprintf(stderr, "Module %s does not do anything\n", module->name); ret = OSCAP_UNIMPL_MOD; goto cleanup; } } cleanup: oscap_action_release(&action); return ret; }
void calculate_dependencies(struct LKM_Module *lkm_mod) { int i=0, n=-1; int size = lkm_mod->depd_count; for(i=0; i<size; i++){ n = check_module(dependencies_tmp[i]); if(n != -1){ lkm_mod->depd_mod[n] = 1; } } }
int log_t::log(const char* module, int level, const char* fmt, va_list ap) { int rc; rc = check_module(module); if(rc) return rc; rc = check_log_level(level); if (0 != rc) return rc; time_t timep = time(NULL); struct tm *tmp = localtime(&timep); char logmsg[MAX_LOG_LINE]; format_log_head(logmsg, module, level, tmp); int len = strlen(logmsg); vsnprintf(logmsg + len, MAX_LOG_LINE - len - 2 - strlen(COLOR_END), fmt, ap); strcat(logmsg, "\n"); strcat(logmsg, COLOR_END); logmsg[MAX_LOG_LINE - 1] = 0; std::ostringstream os; os << logmsg; if (m_print_screen_flag) { if (NULL != m_print_screen_callback) { (*m_print_screen_callback)(os.str()); } else { handle_print_screen(os.str()); } } if (m_print_file_flag) { if (NULL != m_print_file_callback) { (*m_print_file_callback)(os.str()); } else { handle_print_file(os.str()); } } return 0; }
void print_mod_dep(char *file) { int ret=0; ret = check_module(file); if(ret == -1){ cprintf("\n\t***** Module:%s is Not loaded *****\t\n",file); return; } if(modules_lkm[ret].depd_count > 0){ cprintf("\n***Module: %s is dependent on Following modules*** \n",file, modules_lkm[ret].depd_count); print_dependencies(&modules_lkm[ret]); }else{ cprintf("\n\t ****** Module : %s has NO DEPENDENCIES ****** \n",file); } cprintf("\n"); }
int dwfl_module_relocate_address (Dwfl_Module *mod, Dwarf_Addr *addr) { if (unlikely (check_module (mod))) return -1; switch (mod->e_type) { case ET_REL: return find_section (mod, addr); case ET_DYN: /* All relative to first and only relocation base: module start. */ *addr -= mod->low_addr; break; default: /* Already absolute, dwfl_module_relocations returned zero. We shouldn't really have been called, but it's a harmless no-op. */ break; } return 0; }
/* * -- do_config * * Apply the configuration on the map. It expects each useful line to * start with a known keyword. Note the unconventional return values. * It returns NULL if successful or an error string in case of error. * */ static char * do_config(struct _como * m, int argc, char *argv[]) { static char errstr[1024]; static int scope = CTX_GLOBAL; /* scope of current keyword */ static module_t * mdl = NULL; /* module currently open */ static int node_id = 0; /* current node */ static alias_t * alias = NULL; /* alias currently open */ keyword_t *t; /* * run some checks on the token (i.e., that it exists * and that we have all the arguments it needs). */ t = match_token(argv[0], keywords); if (t == NULL) { sprintf(errstr, "unknown token \"%s\"\n", argv[0]); return errstr; } if (argc < t->nargs) { sprintf(errstr, "\"%s\", requires at least %d arguments\n", argv[0], t->nargs); return errstr; } /* * Check if the keyword is ok in the current scope. */ if (!(t->scope & scope)) { sprintf(errstr, "\"%s\" out of scope\n", argv[0]); return errstr; } /* * configuration actions */ switch (t->action) { case TOK_DBDIR: if (m->cli_args.dbdir_set == 0) { safe_dup(&m->dbdir, argv[1]); } break; case TOK_QUERYPORT: if (m->cli_args.query_port_set == 0 || node_id > 0) { m->node[node_id].query_port = atoi(argv[1]); } break; case TOK_DESCRIPTION: if (scope == CTX_MODULE) safe_dup(&mdl->description, argv[1]); else safe_dup(&alias->description, argv[1]); break; case TOK_END: if (scope == CTX_MODULE) { /* * "end" of a module configuration. run some checks depending * on context to make sure that all mandatory fields are there * and set default values */ if (check_module(m, mdl)) { remove_module(m, mdl); scope = CTX_GLOBAL; break; } if (m->runmode == RUNMODE_INLINE) m->inline_mdl = mdl; logmsg(LOGUI, "... module%s %s [%d][%d] ", (mdl->running == RUNNING_ON_DEMAND) ? " on-demand" : "", mdl->name, mdl->node, mdl->priority); logmsg(LOGUI, " filter %s; out %s (%uMB)\n", mdl->filter_str, mdl->output, mdl->streamsize/(1024*1024)); } else if (scope == CTX_VIRTUAL) { /* * we are done with this virtual node. let's go back to * the master node (i.e. node_id == 0) */ node_id = 0; } scope = CTX_GLOBAL; break; case TOK_FILTER: if (scope == CTX_MODULE) safe_dup(&mdl->filter_str, argv[1]); else if (scope == CTX_VIRTUAL) safe_dup(&m->node[node_id].filter_str, argv[1]); break; case TOK_HASHSIZE: mdl->ex_hashsize = mdl->ca_hashsize = atoi(argv[1]); break; case TOK_SOURCE: if (scope == CTX_MODULE) { safe_dup(&mdl->source, argv[1]); } else { safe_dup(&m->node[node_id].source, argv[1]); } break; case TOK_LIBRARYDIR: if (m->cli_args.libdir_set == 0) { safe_dup(&m->libdir, argv[1]); } break; case TOK_LOGFLAGS: if (m->cli_args.logflags_set == 0) { m->logflags = set_flags(0, argv[1]); } break; case TOK_MEMSIZE: /* this keyword can be used in two contexts */ if (m->cli_args.mem_size_set == 0) { m->mem_size = atoi(argv[1]); } break; case TOK_MODULE: if (scope == CTX_GLOBAL) { int node = (m->runmode == RUNMODE_INLINE? -1 : 0); mdl = new_module(m, argv[1], node, -1); scope = CTX_MODULE; /* change scope */ } else { safe_dup(&alias->module, argv[1]); } break; case TOK_MODULE_MAX: m->module_max = atoi(argv[1]); m->modules = safe_realloc(m->modules, sizeof(module_t)*m->module_max); break; case TOK_OUTPUT: safe_dup(&mdl->output, argv[1]); break; case TOK_SNIFFER: add_sniffer(m, argv[1], argv[2], argc > 3 ? argv[3] : NULL); break; case TOK_STREAMSIZE: mdl->streamsize = parse_size(argv[1]); break; case TOK_MAXFILESIZE: m->maxfilesize = parse_size(argv[1]); if (m->maxfilesize > 1024*1024*1024) { m->maxfilesize = DEFAULT_FILESIZE; sprintf(errstr, "'filesize' should be < 1GB --> set to %dMB\n", (int)(m->maxfilesize / (1024*1024))); return errstr; } break; case TOK_ARGS: /* copy the arguments. one line may have multiple arguments * starting from argv[1]. that's why we pass the pointer to * argv[1] and reduce argc by one. */ if (scope == CTX_MODULE) mdl->args = copy_args(mdl->args, &argv[1], argc - 1); else if (scope == CTX_VIRTUAL) m->node[node_id].args = copy_args(m->node->args, &argv[1], argc-1); else if (scope == CTX_ALIAS) { alias->args = copy_args(alias->args, &argv[1], argc - 1); alias->ac += argc - 1; } break; case TOK_ARGSFILE: if (scope == CTX_MODULE) { mdl->args = copy_args_from_file(mdl->args, argv[1], NULL); } else if (scope == CTX_VIRTUAL) { m->node[node_id].args = copy_args_from_file(m->node[node_id].args, argv[1], NULL); } else if (scope == CTX_ALIAS) { int count; alias->args = copy_args_from_file(alias->args, argv[1], &count); alias->ac += count; } break; case TOK_PRIORITY: mdl->priority = atoi(argv[1]); break; case TOK_RUNNING: mdl->running = (strcmp(argv[1], "on-demand") == 0) ? RUNNING_ON_DEMAND : RUNNING_NORMAL; break; case TOK_NAME: safe_dup(&m->node[node_id].name, argv[1]); break; case TOK_LOCATION: safe_dup(&m->node[node_id].location, argv[1]); break; case TOK_TYPE: safe_dup(&m->node[node_id].type, argv[1]); break; case TOK_COMMENT: safe_dup(&m->node[node_id].comment, argv[1]); break; case TOK_VIRTUAL: m->node = safe_realloc(m->node, (m->node_count + 1) * sizeof(node_t)); node_id = m->node_count; bzero(&m->node[node_id], sizeof(node_t)); safe_dup(&m->node[node_id].name, argv[1]); m->node[node_id].location = strdup("Unknown"); m->node[node_id].type = strdup("Unknown"); m->node_count++; scope = CTX_VIRTUAL; break; case TOK_ALIAS: alias = safe_calloc(1, sizeof(alias_t)); safe_dup(&alias->name, argv[1]); alias->next = m->aliases; m->aliases = alias; scope = CTX_ALIAS; break; case TOK_ASNFILE: safe_dup(&m->asnfile, argv[1]); break; case TOK_LIVE_THRESH: m->live_thresh = TIME2TS(0, atoi(argv[1])); break; default: sprintf(errstr, "unknown keyword %s\n", argv[0]); return errstr; } return NULL; }
/* * -- do_config * * Apply the configuration on the map. It expects each useful line to * start with a known keyword. Note the unconventional return values. * It returns NULL if successful or an error string in case of error. * */ static char * do_config(struct _como * m, int argc, char *argv[]) { static char errstr[1024]; static int scope = CTX_GLOBAL; /* scope of current keyword */ static module_t * mdl = NULL; /* module currently open */ keyword_t *t; /* * run some checks on the token (i.e., that it exists * and that we have all the arguments it needs). */ t = match_token(argv[0], keywords); if (t == NULL) { sprintf(errstr, "unknown token \"%s\"\n", argv[0]); return errstr; } if (argc < t->nargs) { sprintf(errstr, "\"%s\", requires at least %d arguments\n", argv[0], t->nargs); return errstr; } /* * Check if the keyword is ok in the current scope. */ if (!(t->scope & scope)) { sprintf(errstr, "\"%s\" out of scope\n", argv[0]); return errstr; } /* * configuration actions */ switch (t->action) { case TOK_BASEDIR: safe_dup(&m->basedir, argv[1]); break; case TOK_QUERYPORT: m->node->query_port = atoi(argv[1]); break; case TOK_DESCRIPTION: safe_dup(&mdl->description, argv[1]); break; case TOK_END: if (scope == CTX_MODULE) { /* * "end" of a module configuration. run some checks depending * on context to make sure that all mandatory fields are there * and set default values */ if (check_module(m, mdl)) { remove_module(m, mdl); scope = CTX_GLOBAL; break; } if (m->running == INLINE) m->inline_mdl = mdl; logmsg(LOGUI, "... module%s %s [%d][%d] ", (mdl->running == RUNNING_ON_DEMAND) ? " on-demand" : "", mdl->name, mdl->node, mdl->priority); logmsg(LOGUI, " filter %s; out %s (%uMB)\n", mdl->filter_str, mdl->output, mdl->streamsize/(1024*1024)); if (mdl->description != NULL) logmsg(LOGUI, " -- %s\n", mdl->description); scope = CTX_GLOBAL; } else if (scope == CTX_VIRTUAL) { /* * we are done with this virtual node. let's recover * the master node (associated with the global context) */ node_t *p, *q; for (p = m->node, q = NULL; p->id != 0; q = p, p = p->next) ; if (q) { /* the master node is not at the head of the list. * move it there. */ q->next = p->next; p->next = m->node; m->node = p; } scope = CTX_GLOBAL; } break; case TOK_FILTER: if (scope == CTX_MODULE) safe_dup(&mdl->filter_str, argv[1]); else if (scope == CTX_VIRTUAL) safe_dup(&m->node->filter_str, argv[1]); break; case TOK_HASHSIZE: mdl->ex_hashsize = mdl->ca_hashsize = atoi(argv[1]); break; case TOK_SOURCE: safe_dup(&mdl->source, argv[1]); break; case TOK_LIBRARYDIR: safe_dup(&m->libdir, argv[1]); break; case TOK_LOGFLAGS: m->logflags = set_flags(0, argv[1]); break; case TOK_MEMSIZE: /* this keyword can be used in two contexts */ m->mem_size = atoi(argv[1]); if (m->mem_size <= 0 || m->mem_size > 512) { sprintf(errstr, "invalid memory size %d, range is 1..512\n", m->mem_size); return errstr; } break; case TOK_MODULE: mdl = new_module(m, argv[1], (m->running == INLINE? -1 : 0), -1); scope = CTX_MODULE; /* change scope */ break; case TOK_MODULE_MAX: m->module_max = atoi(argv[1]); m->modules = safe_realloc(m->modules, sizeof(module_t)*m->module_max); break; case TOK_OUTPUT: safe_dup(&mdl->output, argv[1]); break; case TOK_SNIFFER: add_sniffer(m, argv[1], argv[2], argc > 3 ? argv[3] : NULL); break; case TOK_STREAMSIZE: mdl->streamsize = parse_size(argv[1]); break; case TOK_MAXFILESIZE: m->maxfilesize = parse_size(argv[1]); if (m->maxfilesize > 1024*1024*1024) { m->maxfilesize = DEFAULT_FILESIZE; sprintf(errstr, "'filesize' should be < 1GB --> set to %dMB\n", m->maxfilesize / (1024*1024)); return errstr; } break; case TOK_ARGS: do { int i,j; if (mdl->args == NULL) { mdl->args = safe_calloc(argc, sizeof(char *)); j = 0; } else { /* * we need to add the current list of optional arguments * to the list we already have. first, count how many we * have got so far and then reallocate memory accordingly */ for (j = 0; mdl->args[j]; j++) ; mdl->args = safe_realloc(mdl->args, (argc + j) * sizeof(char*)); } for (i = 1; i < argc; i++) mdl->args[i+j-1] = safe_strdup(argv[i]); /* * Last position is set to null to be able to know * when args finish from the modules */ mdl->args[i+j-1] = NULL; } while (0); break; case TOK_ARGSFILE: do { FILE *auxfp; char line[512]; int j; /* open the file */ auxfp = fopen(argv[1], "r"); if (auxfp == NULL) { sprintf(errstr, "opening file %s: %s\n", argv[1], strerror(errno)); return errstr; } /* count the number of arguments we already have */ for (j = 0; mdl->args[j]; j++) ; /* read each line in the file and parse it again */ /* XXX we reallocate mdl->args for each line in the file. * this should be done better in a less expensive way. */ while (fgets(line, sizeof(line), auxfp)) { j++; mdl->args = safe_realloc(mdl->args, j * sizeof(char *)); mdl->args[j - 1] = safe_strdup(line); } /* add the last NULL pointer */ mdl->args = safe_realloc(mdl->args, (j + 1) * sizeof(char *)); mdl->args[j] = NULL; fclose(auxfp); } while (0); break; case TOK_PRIORITY: mdl->priority = atoi(argv[1]); break; case TOK_RUNNING: mdl->running = (strcmp(argv[1], "on-demand") == 0) ? RUNNING_ON_DEMAND : RUNNING_NORMAL; break; case TOK_NAME: safe_dup(&m->node->name, argv[1]); break; case TOK_LOCATION: safe_dup(&m->node->location, argv[1]); break; case TOK_TYPE: safe_dup(&m->node->type, argv[1]); break; case TOK_COMMENT: safe_dup(&m->node->comment, argv[1]); break; case TOK_VIRTUAL: do { node_t * node; node = safe_calloc(1, sizeof(struct _node)); node->id = m->node_count; safe_dup(&node->name, argv[1]); node->next = m->node; m->node = node; m->node_count++; scope = CTX_VIRTUAL; } while (0); break; default: sprintf(errstr, "unknown keyword %s\n", argv[0]); return errstr; } return NULL; }
int load_module(char *buf, char *file) { int ret=0,i=0,j=0,r=0; elf = (struct Elf *)buf; if(elf->e_magic != ELF_MAGIC){ cprintf("Error Occured while Loading the module. ELF Header is:%08x, Required is: %08x\n", elf->e_magic, ELF_MAGIC); return -1; } ret = check_module(file); if(ret != -1){ cprintf("\n\t***** Module:%s is already loaded *****\t\n",file); return -1; } for(i = 0; i < MAX_LKM; i++){ if(modules_lkm[i].load_status == 0){ break; } } if(i == MAX_LKM){ cprintf("\n ***No Space to Load the Module. Maximum Possible Number of Modules are already Loaded ***\n"); return -1; } cprintf("\n\t**********Inserting Module: %s **********\t\n",file); mod_num = i; if(mod_num < 0){ cprintf("\n ***No Space to Load the Module. Maximum Possible Number of Modules are already Loaded ***\n"); return -1; } strncpy(modules_lkm[mod_num].name, file, sizeof(modules_lkm[mod_num].name)-1); modules_lkm[mod_num].tmp_buf = (void *)buf; modules_lkm[mod_num].elf_hdr = (struct Elf *)buf; cprintf("Name of Module: %s\n",modules_lkm[mod_num].name); ret = calculate_sect_hdr(&modules_lkm[mod_num]); if(ret > 0){ calculate_sym_hdr(&modules_lkm[mod_num], ret); }else{ cprintf("Failed to Load Sections and Symbols..\n"); flush_module(&modules_lkm[mod_num], mod_num); return -1; } r = calculate_load_addr(&modules_lkm[mod_num], ret); if(r < 0){ cprintf("Failed to generate Load Address for the Module. Aborting..\n"); flush_module(&modules_lkm[mod_num], mod_num); return -1; } r = prepare_mod_for_load(&modules_lkm[mod_num],modules_lkm[mod_num].sym_tab_index,modules_lkm[mod_num].st_tab_index); if(r < 0){ cprintf("Unable to locate lkm_init or lkm_exit in the Module. Aborting..\n"); flush_module(&modules_lkm[mod_num], mod_num); return -1; } r = allocate_module(&modules_lkm[mod_num],ret); if(r < 0){ cprintf("Failed to allocate Memory for Module. Aborting...\n"); flush_module(&modules_lkm[mod_num], mod_num); return -1; } modules_lkm[mod_num].load_status = 1; r = load_final_module(&modules_lkm[mod_num],ret); if(r < 0){ flush_module(&modules_lkm[mod_num], mod_num); return -1; } calculate_dependencies(&modules_lkm[mod_num]); calculate_reverse_dependency(&modules_lkm[mod_num],mod_num); modules_lkm[mod_num].init_mod(); mod_num = 0; dependent_count = 0; memset(dependencies_tmp, '\0', sizeof(dependencies_tmp)); cprintf("\n\t********** Module: %s Successfully Loaded **********\t\n\n",file); return 0; }
int main( int argc , char ** argv) { exit( check_module( NULL , argv[1] ) ); }