Exemple #1
0
int32_t TrapHandler(struct NaClApp *nap, uint32_t args)
{
  uint64_t *sargs;
  int retcode = 0;
  int i;

  assert(nap != NULL);
  assert(nap->manifest != NULL);

  /*
   * translate address from user space to system
   * note: cannot set "trap error"
   */
  sargs = (uint64_t*)NaClUserToSys(nap, (uintptr_t)args);
  i = FunctionIndexById(*sargs);
  ZLOGS(LOG_DEBUG, "%s called", function[i]);
  ZTrace("untrusted code");

  switch(*sargs)
  {
    case TrapFork:
      if(Daemon(nap) == 0)
      {
        SyscallZTrace(5, function[5]);
        ZVMExitHandle(nap, 0);
      }
      break;
    case TrapExit:
      ZVMExitHandle(nap, (int32_t)sargs[2]);
      break;
    case TrapRead:
      retcode = ZVMReadHandle(nap,
          (int)sargs[2], (char*)sargs[3], (int32_t)sargs[4], sargs[5]);
      break;
    case TrapWrite:
      retcode = ZVMWriteHandle(nap,
          (int)sargs[2], (char*)sargs[3], (int32_t)sargs[4], sargs[5]);
      break;
    case TrapJail:
      retcode = ZVMJailHandle(nap, (uint32_t)sargs[2], (int32_t)sargs[3]);
      break;
    case TrapUnjail:
      retcode = ZVMUnjailHandle(nap, (uint32_t)sargs[2], (int32_t)sargs[3]);
      break;
    default:
      retcode = -EPERM;
      ZLOG(LOG_ERROR, "function %ld is not supported", *sargs);
      break;
  }

  /* report, ztrace and return */
  FastReport();
  ZLOGS(LOG_DEBUG, "%s returned %d", function[i], retcode);
  SyscallZTrace(i, function[i], sargs[2], sargs[3], sargs[4], sargs[5], retcode);
  return retcode;
}
Exemple #2
0
int32_t TrapHandler(struct NaClApp *nap, uint32_t args)
{
  uint64_t *sys_args;
  int retcode = 0;

  assert(nap != NULL);
  assert(nap->system_manifest != NULL);

  /*
   * translate address from user space to system
   * note: cannot set "trap error"
   */
  sys_args = (uint64_t*)NaClUserToSys(nap, (uintptr_t) args);
  ZLOGS(LOG_DEBUG, "%s called", FunctionNameById(sys_args[0]));

  switch(*sys_args)
  {
    case TrapExit:
      retcode = ZVMExitHandle(nap, (int32_t) sys_args[2]);
      break;
    case TrapRead:
      retcode = ZVMReadHandle(nap,
          (int)sys_args[2], (char*)sys_args[3], (int32_t)sys_args[4], sys_args[5]);
      break;
    case TrapWrite:
      retcode = ZVMWriteHandle(nap,
          (int)sys_args[2], (char*)sys_args[3], (int32_t)sys_args[4], sys_args[5]);
      break;
    case TrapJail:
      retcode = ZVMJailHandle(nap, (uint32_t)sys_args[2], (int32_t)sys_args[3]);
      break;
    case TrapUnjail:
      retcode = ZVMUnjailHandle(nap, (uint32_t)sys_args[2], (int32_t)sys_args[3]);
      break;
    default:
      retcode = -EPERM;
      ZLOG(LOG_ERROR, "function %ld is not supported", *sys_args);
      break;
  }

  ZLOGS(LOG_DEBUG, "%s returned %d", FunctionNameById(sys_args[0]), retcode);
  return retcode;
}
Exemple #3
0
int32_t TrapHandler(struct NaClApp *nap, uint32_t args)
{
  uint64_t *sargs;
  int retcode = 0;

  assert(nap != NULL);
  assert(nap->manifest != NULL);

  /*
   * translate address from user space to system
   * note: cannot set "trap error"
   */
  sargs = (uint64_t*)NaClUserToSys(nap, (uintptr_t)args);
  ZLOGS(LOG_DEBUG, "%s called", FunctionName(*sargs));
  ZTrace("untrusted code");

  switch(*sargs)
  {
    case TrapFork:
      retcode = Daemon(nap);
      if(retcode) break;
      SyscallZTrace(*sargs, 0);
      SyscallZTrace(TrapExit, 0);
      ZVMExitHandle(nap, 0);
      break;
    case TrapExit:
      SyscallZTrace(*sargs, sargs[2]);
      ZVMExitHandle(nap, (int32_t)sargs[2]);
      break;
    case TrapRead:
      retcode = ZVMReadHandle(nap,
          (int)sargs[2], (char*)sargs[3], (int32_t)sargs[4], sargs[5]);
      break;
    case TrapWrite:
      retcode = ZVMWriteHandle(nap,
          (int)sargs[2], (char*)sargs[3], (int32_t)sargs[4], sargs[5]);
      break;
    case TrapJail:
      retcode = ZVMJailHandle(nap, (uint32_t)sargs[2], (int32_t)sargs[3]);
      break;
    case TrapUnjail:
      retcode = ZVMUnjailHandle(nap, (uint32_t)sargs[2], (int32_t)sargs[3]);
      break;

#ifdef ZVM_SOCKETS
    case TrapSocket:
      retcode = ZVM_socket((int)sargs[2], (int)sargs[3], (int)sargs[4]);
      break;
    case TrapBind: {
      const struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      retcode = ZVM_bind((int)sargs[2], addr, (socklen_t)sargs[4]);
      break; }
    case TrapConnect: {
      const struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      retcode = ZVM_connect((int)sargs[2], addr, (socklen_t)sargs[4]);
      break; }
    case TrapAccept: {
      struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      socklen_t *len = (void*)NaClUserToSys(nap, (uintptr_t)sargs[4]);
      retcode = ZVM_accept((int)sargs[2], addr, len);
      break; }
    case TrapListen:
      retcode = ZVM_listen((int)sargs[2], (int)sargs[3]);
      break;
    case TrapRecv: {
      void *buf = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      retcode = ZVM_recv((int)sargs[2], buf, (size_t)sargs[4], (int)sargs[5]);
      break; }
    case TrapRecvfrom: {
      void *buf = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[6]);
      socklen_t *len = (void*)NaClUserToSys(nap, (uintptr_t)sargs[7]);
      retcode = ZVM_recvfrom((int)sargs[2], buf, (size_t)sargs[4], (int)sargs[5], addr, len);
      break; }
    case TrapRecvmsg: {
      struct msghdr *msg = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      retcode = ZVM_recvmsg((int)sargs[2], msg, (int)sargs[4]);
      break; }
    case TrapSend: {
      const void *buf = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      retcode = ZVM_send((int)sargs[2], buf, (size_t)sargs[4], (int)sargs[5]);
      break; }
    case TrapSendto: {
      const void *buf = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      const struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[6]);
      retcode = ZVM_sendto((int)sargs[2], buf, (size_t)sargs[4], (int)sargs[5], addr, (socklen_t)sargs[7]);
      break; }
    case TrapSendmsg: {
      const struct msghdr *msg = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      retcode = ZVM_sendmsg((int)sargs[2], msg, (int)sargs[4]);
      break; }
    case TrapGetsockopt: {
      void *optval = (void*)NaClUserToSys(nap, (uintptr_t)sargs[5]);
      socklen_t *len = (void*)NaClUserToSys(nap, (uintptr_t)sargs[6]);
      retcode = ZVM_getsockopt((int)sargs[2], (int)sargs[3], (int)sargs[4], optval, len);
      break; }
    case TrapSetsockopt: {
      const void *optval = (void*)NaClUserToSys(nap, (uintptr_t)sargs[5]);
      retcode = ZVM_setsockopt((int)sargs[2], (int)sargs[3], (int)sargs[4], optval, (socklen_t)sargs[6]);
      break; }
    case TrapSelect: {
      fd_set *readfds = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]);
      fd_set *writefds = (void*)NaClUserToSys(nap, (uintptr_t)sargs[4]);
      fd_set *exceptfds = (void*)NaClUserToSys(nap, (uintptr_t)sargs[5]);
      struct timeval *timeout = (void*)NaClUserToSys(nap, (uintptr_t)sargs[6]);
      retcode = ZVM_select((int)sargs[2], readfds, writefds, exceptfds, timeout);
      break; }
    case TrapPoll: {
      struct pollfd *fds = (void*)NaClUserToSys(nap, (uintptr_t)sargs[2]);
      retcode = ZVM_poll(fds, (nfds_t)sargs[3], (int)sargs[4]);
      break; }
    case TrapGethostbyname: {
      const char *name = (void*)NaClUserToSys(nap, (uintptr_t)sargs[2]);
      retcode = (int)(intptr_t)ZVM_gethostbyname(name);
      break; }
    case TrapGethostbyaddr: {
      const void *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[2]);
      retcode = (int)(intptr_t)ZVM_gethostbyaddr(addr, (socklen_t)sargs[3], (int)sargs[4]);
      break; }
    case TrapClose:
      retcode = ZVM_close((int)sargs[2]);
      break;
#endif

    default:
      retcode = -EPERM;
      ZLOG(LOG_ERROR, "function %ld is not supported", *sargs);
      break;
  }

  /* report, ztrace and return */
  FastReport();
  ZLOGS(LOG_DEBUG, "%s returned %d", FunctionName(*sargs), retcode);
  SyscallZTrace(*sargs, retcode, sargs[2], sargs[3], sargs[4], sargs[5], sargs[6], sargs[7]);
  return retcode;
}