Beispiel #1
0
void
open_logfiles(void)
{
	int i;

	close_logfiles();

	log_main = fopen(logFileName, "a");

	/* log_main is handled above, so just do the rest */
	for(i = 1; i < LAST_LOGFILE; i++)
	{
		/* reopen those with paths */
		if(!EmptyString(*log_table[i].name))
		{
			verify_logfile_access(*log_table[i].name);
			*log_table[i].logfile = fopen(*log_table[i].name, "a");
		}
	}
}			
Beispiel #2
0
int main(int argc, char* argv[])
{
	int ret;
	unsigned int i;

	printf("Trinity v" __stringify(VERSION) "  Dave Jones <*****@*****.**> 2012\n");

#ifdef __x86_64__
	syscalls = syscalls_x86_64;
	max_nr_syscalls = NR_X86_64_SYSCALLS;
#elif __i386__
	syscalls = syscalls_i386;
	max_nr_syscalls = NR_I386_SYSCALLS;
#elif __powerpc__
	syscalls = syscalls_ppc;
#elif __ia64__
	syscalls = syscalls_ia64;
#elif __sparc__
	syscalls = syscalls_sparc;
#else
	syscalls = syscalls_i386;
#endif

	progname = argv[0];

	parse_args(argc, argv);

	if (getuid() == 0) {
		if (dangerous == 1) {
			printf("DANGER: RUNNING AS ROOT.\n");
			printf("Unless you are running in a virtual machine, this could cause serious problems such as overwriting CMOS\n");
			printf("or similar which could potentially make this machine unbootable without a firmware reset.\n\n");
			printf("ctrl-c now unless you really know what you are doing.\n");
			for (i = 10; i > 0; i--) {
				printf("Continuing in %d seconds.\r", i);
				(void)fflush(stdout);
				sleep(1);
			}
		} else {
			printf("Don't run as root (or pass --dangerous if you know what you are doing).\n");
			exit(EXIT_FAILURE);
		}
	}

	if (create_shm())
		exit(EXIT_FAILURE);

	if (logging != 0)
		open_logfiles();

	max_nr_syscalls = NR_SYSCALLS;
	for (i = 0; i < max_nr_syscalls; i++)
		syscalls[i].entry->number = i;

	if (desired_group == GROUP_VM) {
		struct syscalltable *newsyscalls;
		int count = 0, j = 0;

		for (i = 0; i < max_nr_syscalls; i++) {
			if (syscalls[i].entry->group == GROUP_VM)
				count++;
		}

		newsyscalls = malloc(count * sizeof(struct syscalltable));
		if (newsyscalls == NULL)
			exit(EXIT_FAILURE);

		for (i = 0; i < max_nr_syscalls; i++) {
			if (syscalls[i].entry->group == GROUP_VM)
				newsyscalls[j++].entry = syscalls[i].entry;
		}

		max_nr_syscalls = count;
		syscalls = newsyscalls;
	}


	if (!do_specific_syscall)
		output("Fuzzing %d syscalls.\n", max_nr_syscalls);

	if (do_specific_syscall == 1)
		find_specific_syscall();

	if (do_specific_proto == 1)
		find_specific_proto();

	if (show_syscall_list == 1) {
		syscall_list();
		exit(EXIT_SUCCESS);
	}

	page_size = getpagesize();

	if (!seed)
		seed_from_tod();
	else
		output("[%d] Random seed: %u (0x%x)\n", getpid(), seed, seed);


	init_buffers();

	mask_signals();

	setup_fds();

	if (check_tainted() != 0) {
		output("Kernel was tainted on startup. Will keep running if trinity causes an oops.\n");
		do_check_tainted = 1;
	}

	/* just in case we're not using the test.sh harness. */
	chmod("tmp/", 0755);
	ret = chdir("tmp/");
	if (!ret) {
		/* nothing right now */
	}

	main_loop();

	printf("\nRan %ld syscalls (%ld retries). Successes: %ld  Failures: %ld\n",
		shm->execcount - 1, shm->retries, shm->successes, shm->failures);

	shmdt(shm);

	destroy_maps();

	for (i = 0; i < socks; i++)
		close(socket_fds[i]);

	if (logging != 0)
		close_logfiles();

	exit(EXIT_SUCCESS);
}
Beispiel #3
0
int main(int argc, char* argv[])
{
	int ret = EXIT_SUCCESS;
	int childstatus;
	unsigned int i;

	outputstd("Trinity v" __stringify(VERSION) "  Dave Jones <*****@*****.**>\n");

	progname = argv[0];

	initpid = getpid();

	page_size = getpagesize();
	num_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);

	select_syscall_tables();

	if (create_shm())
		exit(EXIT_FAILURE);

	parse_args(argc, argv);
	outputstd("Done parsing arguments.\n");

	if (kernel_taint_mask != (int)0xFFFFFFFF) {
		outputstd("Custom kernel taint mask has been specified: 0x%08x (%d).\n", kernel_taint_mask, kernel_taint_mask);
	}

	if (user_specified_children != 0)
		max_children = user_specified_children;
	else
		max_children = sysconf(_SC_NPROCESSORS_ONLN);

	if (max_children > MAX_NR_CHILDREN) {
		outputerr("Increase MAX_NR_CHILDREN!\n");
		exit(EXIT_FAILURE);
	}

	setup_shm_postargs();

	if (logging == TRUE)
		open_logfiles();

	if (munge_tables() == FALSE) {
		ret = EXIT_FAILURE;
		goto out;
	}

	if (show_syscall_list == TRUE) {
		dump_syscall_tables();
		goto out;
	}

	init_syscalls();

	if (show_ioctl_list == TRUE) {
		dump_ioctls();
		goto out;
	}

	if (getuid() == 0) {
		if (dangerous == TRUE) {
			outputstd("DANGER: RUNNING AS ROOT.\n");
			outputstd("Unless you are running in a virtual machine, this could cause serious problems such as overwriting CMOS\n");
			outputstd("or similar which could potentially make this machine unbootable without a firmware reset.\n\n");
			outputstd("ctrl-c now unless you really know what you are doing.\n");
			for (i = 10; i > 0; i--) {
				outputstd("Continuing in %d seconds.\r", i);
				(void)fflush(stdout);
				sleep(1);
			}
		} else {
			outputstd("Don't run as root (or pass --dangerous if you know what you are doing).\n");
			exit(EXIT_FAILURE);
		}
	}

	if (do_specific_proto == TRUE)
		find_specific_proto(specific_proto_optarg);

	init_buffers();

	parse_devices();

	pids_init();

	setup_main_signals();

	kernel_taint_initial = check_tainted();
	if (kernel_taint_initial != 0) {
		output(0, "Kernel was tainted on startup. Will ignore flags that are already set.\n");
	}

	change_tmp_dir();

	/* check if we ctrl'c or something went wrong during init. */
	if (shm->exit_reason != STILL_RUNNING)
		goto cleanup_fds;

	init_watchdog();

	do_main_loop();

	/* Shutting down. */
	waitpid(watchdog_pid, &childstatus, 0);

	output(0, "\nRan %ld syscalls. Successes: %ld  Failures: %ld\n",
		shm->total_syscalls_done - 1, shm->successes, shm->failures);

	ret = EXIT_SUCCESS;

cleanup_fds:

	close_sockets();

	destroy_global_mappings();

	if (logging == TRUE)
		close_logfiles();

out:

	exit(ret);
}
Beispiel #4
0
int main(int argc, char* argv[])
{
	int ret = EXIT_SUCCESS;
	unsigned int i;

	printf("Trinity v" __stringify(VERSION) "  Dave Jones <*****@*****.**> 2012\n");

	progname = argv[0];

	setup_syscall_tables();

	parse_args(argc, argv);

	/* If we didn't pass -c or -x, mark all syscalls active. */
	if ((do_specific_syscall == FALSE) && (do_exclude_syscall == FALSE))
		mark_all_syscalls_active();

	if (getuid() == 0) {
		if (dangerous == TRUE) {
			printf("DANGER: RUNNING AS ROOT.\n");
			printf("Unless you are running in a virtual machine, this could cause serious problems such as overwriting CMOS\n");
			printf("or similar which could potentially make this machine unbootable without a firmware reset.\n\n");
			printf("ctrl-c now unless you really know what you are doing.\n");
			for (i = 10; i > 0; i--) {
				printf("Continuing in %d seconds.\r", i);
				(void)fflush(stdout);
				sleep(1);
			}
		} else {
			printf("Don't run as root (or pass --dangerous if you know what you are doing).\n");
			exit(EXIT_FAILURE);
		}
	}

	if (create_shm())
		exit(EXIT_FAILURE);

	/* Set seed in parent thread*/
	set_seed(0);

	if (desired_group != GROUP_NONE) {
		ret = setup_syscall_group(desired_group);
		if (ret == FALSE) {
			ret = EXIT_FAILURE;
			goto cleanup_shm;
		}
	}

	if (show_syscall_list == TRUE) {
		dump_syscall_tables();
		goto cleanup_shm;
	}

	if (validate_syscall_tables() == FALSE) {
		printf("No syscalls were enabled!\n");
		printf("Use 32bit:%d 64bit:%d\n", use_32bit, use_64bit);
		goto cleanup_shm;
	}

	sanity_check_tables();

	if (logging == TRUE)
		open_logfiles();


	if (do_specific_syscall == FALSE) {
		if (biarch == TRUE)
			output(0, "Fuzzing %d 32-bit syscalls & %d 64-bit syscalls.\n",
				max_nr_32bit_syscalls, max_nr_64bit_syscalls);
		else
			output(0, "Fuzzing %d syscalls.\n", max_nr_syscalls);
	}

	if (do_specific_proto == TRUE)
		find_specific_proto(specific_proto_optarg);

	page_size = getpagesize();

	init_buffers();

	mask_signals();

	if (check_tainted() != 0) {
		output(0, "Kernel was tainted on startup. Will keep running if trinity causes an oops.\n");
		do_check_tainted = TRUE;
	}

	/* just in case we're not using the test.sh harness. */
	chmod("tmp/", 0755);
	ret = chdir("tmp/");
	if (!ret) {
		/* nothing right now */
	}

	if (shm->exit_reason != STILL_RUNNING)
		goto cleanup_fds;

	init_watchdog();

	do_main_loop();

	printf("\nRan %ld syscalls. Successes: %ld  Failures: %ld\n",
		shm->total_syscalls_done - 1, shm->successes, shm->failures);

	ret = EXIT_SUCCESS;

cleanup_fds:

	for (i = 0; i < nr_sockets; i++) {
		struct linger ling;

		ling.l_onoff = FALSE;	/* linger active */
		setsockopt(shm->socket_fds[i], SOL_SOCKET, SO_LINGER, &ling, sizeof(struct linger));
		shutdown(shm->socket_fds[i], SHUT_RDWR);
		close(shm->socket_fds[i]);
	}

	destroy_maps();

	if (logging == TRUE)
		close_logfiles();

cleanup_shm:

	if (shm != NULL)
		munmap(shm, sizeof(struct shm_s));

	exit(ret);
}
Beispiel #5
0
int main(int argc, char* argv[])
{
	int ret = EXIT_SUCCESS;
	int childstatus;
	unsigned int i;

	printf("Trinity v" __stringify(VERSION) "  Dave Jones <*****@*****.**>\n");

	progname = argv[0];

	page_size = getpagesize();

	select_syscall_tables();

	if (create_shm())
		exit(EXIT_FAILURE);

	parse_args(argc, argv);
	printf("Done parsing arguments.\n");

	setup_shm_postargs();

	if (logging == TRUE)
		open_logfiles();

	if (munge_tables() == FALSE) {
		ret = EXIT_FAILURE;
		goto out;
	}

	if (show_syscall_list == TRUE) {
		dump_syscall_tables();
		goto out;
	}

	if (show_ioctl_list == TRUE) {
		dump_ioctls();
		goto out;
	}

	if (getuid() == 0) {
		if (dangerous == TRUE) {
			printf("DANGER: RUNNING AS ROOT.\n");
			printf("Unless you are running in a virtual machine, this could cause serious problems such as overwriting CMOS\n");
			printf("or similar which could potentially make this machine unbootable without a firmware reset.\n\n");
			printf("ctrl-c now unless you really know what you are doing.\n");
			for (i = 10; i > 0; i--) {
				printf("Continuing in %d seconds.\r", i);
				(void)fflush(stdout);
				sleep(1);
			}
		} else {
			printf("Don't run as root (or pass --dangerous if you know what you are doing).\n");
			exit(EXIT_FAILURE);
		}
	}

	if (do_specific_proto == TRUE)
		find_specific_proto(specific_proto_optarg);

	init_buffers();

	parse_devices();

	pids_init();

	setup_main_signals();

	if (check_tainted() != 0) {
		output(0, "Kernel was tainted on startup. Will keep running if trinity causes an oops.\n");
		ignore_tainted = TRUE;
	}

	/* just in case we're not using the test.sh harness. */
	chmod("tmp/", 0755);
	ret = chdir("tmp/");
	if (!ret) {
		/* nothing right now */
	}

	if (shm->exit_reason != STILL_RUNNING)
		goto cleanup_fds;

	init_watchdog();

	do_main_loop();

	waitpid(shm->watchdog_pid, &childstatus, 0);

	printf("\nRan %ld syscalls. Successes: %ld  Failures: %ld\n",
		shm->total_syscalls_done - 1, shm->successes, shm->failures);

	ret = EXIT_SUCCESS;

cleanup_fds:

	for (i = 0; i < nr_sockets; i++) {
		struct linger ling;

		ling.l_onoff = FALSE;	/* linger active */
		setsockopt(shm->socket_fds[i], SOL_SOCKET, SO_LINGER, &ling, sizeof(struct linger));
		shutdown(shm->socket_fds[i], SHUT_RDWR);
		close(shm->socket_fds[i]);
	}

	destroy_maps();

	if (logging == TRUE)
		close_logfiles();

out:

	exit(ret);
}
Beispiel #6
0
/**
 * \brief   Main entry of the program
 * \details The 2 variables \b argc and \b argv are used for extracting
 *          command line parameters.
 * \param   argc Number of arguments, at least one as \b argv contains at least the program's name.
 * \param   argv[] Array of character strings containing the arguments.
 * \return  On exit returns EXIT_SUCCESS, EXIT_FAILURE otherwise.
 */
int main(int argc, char** argv)
{
    /* arguments parsing, we need at least "prog_name -i an_input_file"
     * prints some more instructions if needed
     */
    if (argc < 3)
    {
        fprintf(stdout,"[Info] No input file ! \n");
        help(argv);
        return EXIT_SUCCESS;
    }

    uint32_t i;
    char seed[128] = "";
    char inpf[FILENAME_MAX] = "";

    DATA dat ;
    SPDAT spdat = {5,5,0.5,NULL,0};
    ATOM *at = NULL;

    // function pointers for energy and gradient, and trajectory
    get_ENER = NULL;
    get_DV = NULL;
    write_traj= &(write_dcd);

    // arguments parsing
    for (i=1; i<(uint32_t)argc; i++)
    {
        // get name of input file
        if (!strcasecmp(argv[i],"-i"))
        {
            sprintf(inpf,"%s",argv[++i]);
        }
        // get user specified seed, 128 characters max, keep it as a string for the moment
        else if (!strcasecmp(argv[i],"-seed"))
        {
            sprintf(seed,"%s",argv[++i]);
        }
        // reopen stdout to user specified file
        else if (!strcasecmp(argv[i],"-o"))
        {
            freopen(argv[++i],"w",stdout);
            is_stdout_redirected = 1 ;
        }
        // specify the logging level
        else if (!strcasecmp(argv[i],"-log"))
        {
            if (!strcasecmp(argv[++i],"no"))
            {
                LOG_SEVERITY = LOG_NOTHING;
            }
            else if (!strcasecmp(argv[i],"err"))
            {
                LOG_SEVERITY = LOG_ERROR;
            }
            else if (!strcasecmp(argv[i],"warn"))
            {
                LOG_SEVERITY = LOG_WARNING;
            }
            else if (!strcasecmp(argv[i],"info"))
            {
                LOG_SEVERITY = LOG_INFO;
            }
            else if (!strcasecmp(argv[i],"dbg"))
            {
                LOG_SEVERITY = LOG_DEBUG;
            }
            else
                fprintf(stdout,"[Warning] Unknown log level '%s' : default value used.\n\n",argv[i]);
        }
#ifdef _OPENMP
        // if compiled with openMP the user can specify a maximum number of cpus to use
        // check if it is not higher than the real amount of cpus
        else if (!strcasecmp(argv[i],"-np"))
        {
            nthreads=atoi(argv[++i]);
            ncpus=omp_get_num_procs();
            (nthreads < ncpus) ? omp_set_num_threads(nthreads) : omp_set_num_threads(ncpus);
            //omp_set_num_threads(nthreads);
        }
#endif
        // print help and proper exit
        else if ( !strcasecmp(argv[i],"-h") || !strcasecmp(argv[i],"-help") || !strcasecmp(argv[i],"--help") )
        {
            help(argv);
            return EXIT_SUCCESS;
        }
        // error if unknown command line option
        else
        {
            fprintf(stdout,"[Error] Argument '%s' is unknown.\n",argv[i]);
            help(argv);
            exit(-2);
        }
    }

    //prepare log files if necessary
    init_logfiles();

    // Print date and some env. variables
    fprintf(stdout,"Welcome to %s ! Command line arguments succesfully parsed, now intialising parameters...\n\n",argv[0]);
    fprintf(stdout,"Logging level is : %s : see the documentation to see which .log files are generated, and what they contain.\n\n",get_loglevel_string());
    fprintf(stdout,"Now printing some local informations : \n");
    fprintf(stdout,"DATE : %s\n",get_time());
    fprintf(stdout,"HOSTNAME : %s\n",getenv("HOSTNAME"));
    fprintf(stdout,"USER : %s\n",getenv("USER"));
    fprintf(stdout,"PWD : %s\n",getenv("PWD"));

    /*
     * Random numbers can be generated by using the standard functions from the C library (no guarantee on the quality)
     * or by using the dSFMT generator (default, extremely stable, no redudancy )
     * if STDRAND is defined we use the C library.
     *
     * Random numbers are stored in a double array dat.rn, of size dat.nrn
     *
     * If no string seed was passed by command line we generate one by using the unix timestamp
     *  -For STDRAND, this is directly used for srand()
     *  -For dSFMT, an array of integers generated by using the string seed is sent to dsfmt_init_by_array
     *
     */
    if (!strlen(seed))
        sprintf(seed,"%d",(uint32_t)time(NULL)) ;
    LOG_PRINT(LOG_INFO,"seed = %s \n",seed);
    dat.nrn = 2048 ;
    dat.rn = calloc(dat.nrn,sizeof dat.rn);
#ifdef STDRAND
    srand( (uint32_t)labs(atol(seed)) );
#else
    dat.seeds = calloc(strlen(seed),sizeof dat.seeds);
    for (i=0; i<(uint32_t)strlen(seed); i++)
        dat.seeds[i] = (uint32_t) seed[i] << 8;
    for (i=0; i<(uint32_t)strlen(seed); i++)
        dat.seeds[i] *= (dat.seeds[strlen(seed)-1]+i+1);

    dsfmt_init_by_array(&(dat.dsfmt),dat.seeds,(int32_t)strlen(seed));

    for (i=0; i<(uint32_t)strlen(seed); i++)
    {
        LOG_PRINT(LOG_INFO,"dat.seeds[%d] = %d \n",strlen(seed)-1-i,dat.seeds[strlen(seed)-1-i]);
    }
#endif
    
    // parse input file, initialise atom list
    parse_from_file(inpf,&dat,&spdat,&at);

    // set the pointer to the default V and dV functions
    if(get_ENER==NULL)
        get_ENER = &(get_LJ_V);

    if(get_DV==NULL)
        get_DV = &(get_LJ_DV);

    // allocate arrays used by energy minimisation function
    alloc_minim(&dat);

    // sum up parameters to output file

#ifdef _OPENMP
    fprintf(stdout,"\nStarting program with %d threads (%d cpus available)\n\n",nthreads,ncpus);
#else
    fprintf(stdout,"\nStarting program in sequential mode\n\n");
#endif

    fprintf(stdout,"Seed   = %s \n\n",seed);

    if (get_ENER==&(get_LJ_V))
        fprintf(stdout,"Using L-J potential\n");
    else if (get_ENER==&(get_AZIZ_V))
        fprintf(stdout,"Using Aziz potential\n");
#ifdef LUA_PLUGINS
    else if (get_ENER==&(get_lua_V))
        fprintf(stdout,"Using plugin pair potential\n");
    else if (get_ENER==&(get_lua_V_ffi))
        fprintf(stdout,"Using plugin ffi potential\n");
#endif
    
    if (charmm_units)
        fprintf(stdout,"Using CHARMM  units.\n\n");
    else
        fprintf(stdout,"Using REDUCED units.\n\n");

    fprintf(stdout,"Energy      saved each %d  steps in file %s\n",io.esave,io.etitle);
    fprintf(stdout,"Trajectory  saved each %d  steps in file %s\n",io.trsave,io.trajtitle);
    fprintf(stdout,"Initial configuration saved in file %s\n",io.crdtitle_first);
    fprintf(stdout,"Final   configuration saved in file %s\n\n",io.crdtitle_last);

    // get values of best minima for several well known LJ clusters
    getValuesFromDB(&dat);

    // set the inverse temperature depending of the type of units used
    if (charmm_units)
        dat.beta = 1.0/(KBCH*dat.T);
    else
        dat.beta = 1.0/(dat.T);

    // again print parameters
    fprintf(stdout,"method = %s\n",dat.method);
    fprintf(stdout,"natom  = %d\n",dat.natom);
    fprintf(stdout,"nsteps = %"PRIu64"\n",dat.nsteps);
    fprintf(stdout,"T      = %lf \n",dat.T);
    fprintf(stdout,"beta   = %lf\n",dat.beta);

    if(dat.d_max_when==0)
        fprintf(stdout,"dmax   = %lf (fixed) \n\n",dat.d_max);
    else
        fprintf(stdout,"dmax   = %4.2lf updated each %d steps for "
                "targeting %4.2lf %% of acceptance \n\n",dat.d_max,dat.d_max_when,dat.d_max_tgt);

    // then depending of the type of simulation run calculation
    if (strcasecmp(dat.method,"metrop")==0)
    {
        start_classic(&dat,at);
    }
    else if (strcasecmp(dat.method,"spav")==0)
    {
        start_spav(&dat,&spdat,at);
    }
    else
    {
        LOG_PRINT(LOG_ERROR,"Method [%s] unknowm.\n",dat.method);
        free(dat.rn);
#ifndef STDRAND
        free(dat.seeds);
#endif
        exit(-3);
    }

#ifdef __unix__
    // compatible with some unixes-like OS: the struct rusage communicates with the kernel directly.
    struct rusage infos_usage;
    getrusage(RUSAGE_SELF,&infos_usage);
    fprintf(stdout,"Maximum amount of memory used in kBytes is : %ld\n",infos_usage.ru_maxrss);
    fprintf(stdout,"Execution time in Seconds : %lf\n",
            (double)infos_usage.ru_utime.tv_sec+(double)infos_usage.ru_utime.tv_usec/1000000.0 +
            (double)infos_usage.ru_stime.tv_sec+(double)infos_usage.ru_stime.tv_usec/1000000.0
           );
#endif
    fprintf(stdout,"End of program\n");

    // free memory and exit properly
    free(dat.rn);
#ifndef STDRAND
    free(dat.seeds);
#endif
    free(at);
    dealloc_minim();

#ifdef LUA_PLUGINS
    end_lua();
#endif //LUA_PLUGINS

    // closing log files is the last thing to do as errors may occur at the end
    close_logfiles();

    return EXIT_SUCCESS;
}