Exemple #1
0
/// Receive agent server ack message
/// Return none
static void show_server_ack( void ) {

    int n_rbytes;

    char ack_msg[SZ_MAX_COMMAND];

    bzero( ack_msg, sizeof(ack_msg) );
    n_rbytes = as_read( ac->sock, ack_msg, sizeof(MSG_ACK_PREFIX) );
    if( n_rbytes == sizeof(MSG_ACK_PREFIX) )
       agent_printf( "%s", ack_msg );

    bzero( ack_msg, sizeof(ack_msg) );
    n_rbytes = as_read( ac->sock, ack_msg, SZ_MAX_COMMAND );
    if( n_rbytes == SZ_MAX_COMMAND )
        agent_printf( "%s\n", ack_msg );
} 
Exemple #2
0
/// (E)rror (C)heck (M)essage read
/// A wrapper of receiving error check messages.
///
/// Return TRUE for receiving MSG_REC_SUCCESS and FALSE for receiving MSG_REC_FAIL or failing to receiving
static bool _MOCKABLE(ecm_read)( void ) {

    char errorbuf[sizeof(MSG_REC_SUCCESS)];
    int n_rerrorbytes;

    // --------read the error check message-------- //
    n_rerrorbytes = as_read( ac->sock, errorbuf, sizeof(MSG_REC_SUCCESS) );
    if( n_rerrorbytes != sizeof(MSG_REC_SUCCESS) || strncmp(errorbuf, MSG_REC_SUCCESS, sizeof(MSG_REC_SUCCESS)) != 0 ) 
        return FALSE;

    return TRUE;
}
Exemple #3
0
/// Export the agent server log to host
/// Return AGENT_RET_SUCCESS if succeed or AGENT_RET_EFAIL if fail
static MBA_AGENT_RETURN _MOCKABLE(export_agent_log)( void ) {

    char cmd_emit[SZ_MAX_COMMAND];

    uint64_t fsize;

    char  fbuf[SZ_MAX_FILECHUNK];
    char* fptr;

    int fd = -1;

    int n_rbytes;
    int n_wbytes;

    const char* dst_path = ac->act.dst_path;

    // construct the final command for agent server
    bzero( cmd_emit, SZ_MAX_COMMAND );
    snprintf( cmd_emit, SZ_MAX_COMMAND, "logf" );

    // emit command
    n_wbytes = as_write( ac->sock, cmd_emit, SZ_MAX_COMMAND );
    if( n_wbytes != SZ_MAX_COMMAND ) {
        agent_printf( "Failed to emit command '%s'\n", cmd_emit );
        return AGENT_RET_EFAIL;
    }

    // --------Check if server is able to duplicate log handle, set file pointer and read filesize-------- //
    if ( !ecm_read() ) {
        agent_printf("Error occurs when server duplicate log handle, set file pointer or read filesize.\n");
        return AGENT_RET_EFAIL;
    }

    // receive log file size
    n_rbytes = as_read( ac->sock, &fsize, sizeof(uint64_t) );
    if( n_rbytes != sizeof(uint64_t) ) {
        agent_printf( "Failed to receive log file size\n" );
        return AGENT_RET_EFAIL;
    }

    // open destination file to store the log
    fd = open( dst_path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
    if( fd == -1 ) {
        agent_printf( "Failed to open '%s' for agent log export\n", dst_path );
        ecm_write( FALSE );
        return AGENT_RET_EFAIL;
    }
    ecm_write( TRUE );

    // receive & store file content
    while( fsize ) {

        // measure the maximum bytes should be read
        n_rbytes = (fsize < SZ_MAX_FILECHUNK) ? fsize : SZ_MAX_FILECHUNK;

        // --------Check if server is able to read file-------- //
        if ( !ecm_read() ) {
            agent_printf("Error occurs when server reading file.\n");
            close(fd);
            return AGENT_RET_EFAIL;
        }
    
        // receive file content from agent server
        n_rbytes = as_read( ac->sock, fbuf, n_rbytes );
        if( n_rbytes <= 0 ) {
            agent_printf( "Failed to receive file content\n" );
            close(fd);
            return AGENT_RET_EFAIL;
        }

        fsize -= n_rbytes;

        // store to local file
        fptr = fbuf;
        while( n_rbytes ) {
            n_wbytes = write( fd, fptr, n_rbytes );
            if( n_wbytes == -1 ) {
                agent_printf( "Failed to store file content\n" );
                ecm_write( FALSE );
                close(fd);
                return AGENT_RET_EFAIL;
            }
            ecm_write( TRUE );

            n_rbytes -= n_wbytes;
            fptr     += n_wbytes;
       }
    }

    close( fd );
    return AGENT_RET_SUCCESS;
}
Exemple #4
0
/// Execute a guest command and perform interactive stdin/stdout
/// Return AGENT_RET_SUCCESS if succeed or AGENT_RET_EFAIL if fail
static MBA_AGENT_RETURN _MOCKABLE(execute_guest_cmd_return)( void ) {
    
    char cmd_emit[SZ_MAX_COMMAND];

    uint32_t msize;
    char     msg_chunk[4096];

    int n_rbytes;
    int n_wbytes;

    const char* cmdline = ac->act.cmdline;

    int i;

    // construct the final command for agent server
    bzero( cmd_emit, SZ_MAX_COMMAND );
    snprintf( cmd_emit, SZ_MAX_COMMAND, "exec %s", cmdline );

    // emit command
    n_wbytes = as_write( ac->sock, cmd_emit, SZ_MAX_COMMAND );
    if( n_wbytes != SZ_MAX_COMMAND ) {
        agent_printf( "Failed to emit command '%s'\n", cmd_emit );
        return AGENT_RET_EFAIL;
    }

    // Check if server has finished all the preparations 
    if ( !ecm_read() ) {
        agent_printf("Error occurs when server does preparations for w_exec.\n");
        return AGENT_RET_EFAIL;
    }

    // infinite loop to receive guest output
    msize = 0;
    while( true ) {

        // get output message size
        n_rbytes = as_read( ac->sock, &msize, sizeof(uint32_t) );
        if( n_rbytes != sizeof(uint32_t) ) 
            return AGENT_RET_EFAIL;

        if( msize == 0 )
            break;
    
        // get & print message(may not be null-terminated) content
        while( msize ) {

            // measure the maximum bytes should be read
            n_rbytes = (msize < sizeof(msg_chunk)) ? msize : sizeof(msg_chunk); 

            // read output message
            n_rbytes = as_read( ac->sock, msg_chunk, n_rbytes );
            if( n_rbytes <= 0 ) {
                agent_printf( "Failed to revceive agent exec output\n" );
                return AGENT_RET_EFAIL;
            }

            for( i = 0; i < n_rbytes; ++i )
                agent_printf( "%c", msg_chunk[i] );

            msize -= n_rbytes;
        }
    }

    if ( !ecm_read() ) {
        agent_printf("Agent Server do not finish closing handles after w_exec");
        return AGENT_RET_EFAIL;
    }
   
    return AGENT_RET_SUCCESS;
}
Exemple #5
0
void			test_core_as_copy_01(void)
{
  i_task		task1;
  i_task		task2;
  i_as			as1;
  i_as			as2;
  i_segment		seg1;
  i_segment		seg2;
  i_segment		seg3;
  i_segment		seg4;
  i_segment		seg5;
  i_segment		seg6;
  i_segment		useless;
  i_region		reg;
  t_uint32		i;
  t_uint8		buff[4 * ___kaneton$pagesz];

  TEST_ENTER();

  /*
   * first address space
   */

  if (task_reserve(TASK_CLASS_GUEST,
		   TASK_BEHAVIOUR_INTERACTIVE,
		   TASK_PRIORITY_INTERACTIVE,
		   &task1) != STATUS_OK)
    TEST_ERROR("[task_reserve] error");

  if (as_reserve(task1, &as1) != STATUS_OK)
    TEST_ERROR("[as_reserve] error");

  if (segment_reserve(as1,
		      2 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg1) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as1,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &useless) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as1,
		      4 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg2) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as1,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &useless) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as1,
		      2 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg3) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (region_reserve(as1,
		     seg1,
		     ___kaneton$pagesz,
		     REGION_OPTION_FORCE,
		     0x20000000,
		     ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  if (region_reserve(as1,
		     seg2,
		     ___kaneton$pagesz,
		     REGION_OPTION_FORCE,
		     0x20001000,
		     2 * ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  if (region_reserve(as1,
		     seg3,
		     0,
		     REGION_OPTION_FORCE,
		     0x20003000,
		     ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  /*
   * second address space
   */

  if (task_reserve(TASK_CLASS_GUEST,
		   TASK_BEHAVIOUR_INTERACTIVE,
		   TASK_PRIORITY_INTERACTIVE,
		   &task2) != STATUS_OK)
    TEST_ERROR("[task_reserve] error");

  if (as_reserve(task2, &as2) != STATUS_OK)
    TEST_ERROR("[as_reserve] error");

  if (segment_reserve(as2,
		      2 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg4) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as2,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &useless) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as2,
		      4 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg5) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as2,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &useless) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (segment_reserve(as2,
		      2 * ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_NONE,
		      &seg6) != STATUS_OK)
    TEST_ERROR("[segment_reserve] error");

  if (region_reserve(as2,
		     seg4,
		     0,
		     REGION_OPTION_FORCE,
		     0x40000000,
		     ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  if (region_reserve(as2,
		     seg5,
		     2 * ___kaneton$pagesz,
		     REGION_OPTION_FORCE,
		     0x40001000,
		     ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  if (region_reserve(as2,
		     seg6,
		     0,
		     REGION_OPTION_FORCE,
		     0x40002000,
		     2 * ___kaneton$pagesz,
		     &reg) != STATUS_OK)
    TEST_ERROR("[region_reserve] error");

  /*
   * operations
   */

  for (i = 0; i < 4 * ___kaneton$pagesz; i++)
    buff[i] = (i * 2 + 4) % 256;

  if (as_write(as1, buff, 4 * ___kaneton$pagesz, 0x20000000) != STATUS_OK)
    TEST_ERROR("[as_write] error");

  for (i = 0; i < 4 * ___kaneton$pagesz; i++)
    buff[i] = 0;

  if (as_copy(as1, 0x20000000, as2, 0x40000000, 4 * ___kaneton$pagesz) != STATUS_OK)
    TEST_ERROR("[as_copy] error");

  if (as_read(as2, 0x40000000, 4 * ___kaneton$pagesz, buff) != STATUS_OK)
    TEST_ERROR("[as_read] error");

  for (i = 0; i < 4 * ___kaneton$pagesz; i++)
    if (buff[i] != (i * 2 + 4) % 256)
      TEST_ERROR("the data appears invalid once read from the "
		 "address space\n");

  TEST_SIGNATURE(rr3fiw3w20aafi9gre9g);

  TEST_LEAVE();
}
Exemple #6
0
void			architecture_handler_spurious(t_uint32	n)
{
  i_thread		id;
  o_thread*		thread;
  o_task*		task;
  t_uint32		stack[8];
  as_context		ctx;

  /*
   * 2)
   */

  assert(thread_current(&id) == STATUS_OK);

  /*
   * 3)
   */

  assert(architecture_context_get(id, &ctx) == STATUS_OK);

  /*
   * 4)
   */

  module_call(console, message,
	      '!',
	      "spurious: n(%u) thread(%qu)\n",
	      n, id);

  module_call(console, message,
	      '!',
	      "  eip(0x%08x) ebp(0x%08x) esp(0x%08x) eflags(0x%08x)\n",
	      ctx.eip, ctx.ebp, ctx.esp, ctx.eflags);

  module_call(console, message,
	      '!',
	      "  cs(0x%x) ds(0x%x) ss(0x%x)\n",
	      ctx.cs, ctx.ds, ctx.ss);

  module_call(console, message,
	      '!',
	      "  eax(0x%x) ebx(0x%x) ecx(0x%x) edx(0x%x) "
	      "esi(0x%x) edi(0x%x)\n",
	      ctx.eax, ctx.ebx, ctx.ecx, ctx.edx, ctx.esi, ctx.edi);

  /*
   * 5)
   */

  assert(thread_get(id, &thread) == STATUS_OK);

  assert(task_get(thread->task, &task) == STATUS_OK);

  /*
   * 6)
   */

  assert(as_read(task->as, ctx.esp, sizeof (stack), stack) == STATUS_OK);

  /*
   * 7)
   */

  module_call(console, message,
	      '!',
	      "  stack: \n"
	      "    0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
	      stack[0], stack[1], stack[2], stack[3],
	      stack[4], stack[5], stack[6], stack[7]);
}