/* ### * get the fully qualified ip address from swift daemon * populate given NaClAppThread object with channel info * return 0 when success, otherwise - nacl error code */ int32_t ZMQSysOpen(struct NaClAppThread *natp, char *name, int flags, int mode) { int32_t retval = 0; struct NaClHostDesc *hd; NaClLog(3, "Entered ZMQSysOpen(natp, %s, %X, %X)\n", name, flags, mode); /* ### dummy * just open the local file w/o "http://" prefix to test this function * example of file name: "http://dummy_zmq.dat" (will open "dummy_zmq.dat") */ hd = malloc(sizeof *hd); if (NULL == hd) { return -NACL_ABI_ENOMEM; } /* add extra info to hd */ // strcpy(hd->channel, name); // ### NaClLog(1, "NaClHostDescOpen(0x%08"NACL_PRIxPTR", %s, 0%o, 0%o) returned %d\n", (uintptr_t) hd, name + URL_MARK_SIZE - 1, flags, mode, retval); /* add a new record to the file descriptors table */ if (0 == retval) { retval = NaClSetAvail(natp->nap, ((struct NaClDesc *) NaClDescIoDescMake(hd))); NaClLog(1, "Entered url into open file table at %d\n", retval); } /* ### log to remove. i only use it to debug this class */ // NaClLog(1, "int32_t ZMQSysOpen(struct NaClAppThread *natp, char " // "*pathname, int flags, int mode): channel == %s, desc == %d\n", hd->channel, hd->d); return retval; }
/* * premap given file (channel). return 0 if success, otherwise negative errcode * note: malloc() */ int PremapChannel(struct NaClApp *nap, struct PreOpenedFileDesc* channel) { int desc; struct NaClHostDesc *hd = malloc(sizeof(*hd)); /* debug checks */ COND_ABORT(!hd, "cannot allocate memory to hold channel descriptor\n"); COND_ABORT(!channel, "channel is not constructed\n"); COND_ABORT(channel->mounted != MAPPED, "channel is not supposed to be mapped\n"); COND_ABORT(!channel->name, "cannot resolve channel name\n"); /* open file */ channel->handle = open((char*)channel->name, GetChannelOpenFlags(channel), S_IRWXU); COND_ABORT(channel->handle < 0, "channel open error\n"); /* check if given file in bounds of manifest limits */ channel->fsize = GetFileSize((char*)channel->name); PreallocateChannel(channel); COND_ABORT(channel->max_size < channel->fsize, "channel legnth exceeded policy limit\n"); /* construct nacl descriptor */ hd->d = channel->handle; desc = NaClSetAvail(nap, ((struct NaClDesc *) NaClDescIoDescMake(hd))); /* map whole file into the memory. address cannot be higher than stack */ channel->buffer = NaClCommonSysMmapIntern(nap, NULL, channel->fsize, GetChannelMapProt(channel), GetChannelMapFlags(channel), desc, 0); COND_ABORT((uint32_t)channel->buffer > 0xFF000000, "channel map error\n"); /* mounting finalization */ close(channel->handle); channel->bsize = channel->fsize; /* yes. mapped file always put to memory whole */ channel->handle = -1; /* there is no opened file for mapped channel */ return 0; }
int32_t NaClSysOpen(struct NaClAppThread *natp, char *pathname, int flags, int mode) { struct NaClApp *nap = natp->nap; uint32_t retval = -NACL_ABI_EINVAL; char path[NACL_CONFIG_PATH_MAX]; nacl_host_stat_t stbuf; int allowed_flags; NaClLog(3, "NaClSysOpen(0x%08"NACL_PRIxPTR", " "0x%08"NACL_PRIxPTR", 0x%x, 0x%x)\n", (uintptr_t) natp, (uintptr_t) pathname, flags, mode); retval = CopyPathFromUser(nap, path, sizeof path, (uintptr_t) pathname); if (0 != retval) goto cleanup; allowed_flags = (NACL_ABI_O_ACCMODE | NACL_ABI_O_CREAT | NACL_ABI_O_TRUNC | NACL_ABI_O_APPEND); if (0 != (flags & ~allowed_flags)) { NaClLog(LOG_WARNING, "Invalid open flags 0%o, ignoring extraneous bits\n", flags); flags &= allowed_flags; } if (0 != (mode & ~0600)) { NaClLog(1, "IGNORING Invalid access mode bits 0%o\n", mode); mode &= 0600; } retval = NaClOpenAclCheck(nap, path, flags, mode); if (0 != retval) { NaClLog(3, "Open ACL check rejected \"%s\".\n", path); goto cleanup; } /* * Perform a stat to determine whether the file is a directory. * * NB: it is okay for the stat to fail, since the request may be to * create a new file. * * There is a race conditions here: between the stat and the * open-as-a-file and open-as-a-dir, the type of the object that the * path refers to can change. */ retval = NaClHostDescStat(path, &stbuf); /* Windows does not have S_ISDIR(m) macro */ if (0 == retval && S_IFDIR == (S_IFDIR & stbuf.st_mode)) { struct NaClHostDir *hd; hd = malloc(sizeof *hd); if (NULL == hd) { retval = -NACL_ABI_ENOMEM; goto cleanup; } retval = NaClHostDirOpen(hd, path); NaClLog(1, "NaClHostDirOpen(0x%08"NACL_PRIxPTR", %s) returned %d\n", (uintptr_t) hd, path, retval); if (0 == retval) { retval = NaClSetAvail(nap, ((struct NaClDesc *) NaClDescDirDescMake(hd))); NaClLog(1, "Entered directory into open file table at %d\n", retval); } } else { struct NaClHostDesc *hd; hd = malloc(sizeof *hd); if (NULL == hd) { retval = -NACL_ABI_ENOMEM; goto cleanup; } retval = NaClHostDescOpen(hd, path, flags, mode); NaClLog(1, "NaClHostDescOpen(0x%08"NACL_PRIxPTR", %s, 0%o, 0%o) returned %d\n", (uintptr_t) hd, path, flags, mode, retval); if (0 == retval) { struct NaClDesc *desc = (struct NaClDesc *) NaClDescIoDescMake(hd); if ((flags & NACL_ABI_O_ACCMODE) == NACL_ABI_O_RDONLY) { /* * Let any read-only open be used for PROT_EXEC mmap * calls. Under -a, the user informally warrants that * files' code segments won't be changed after open. */ NaClDescSetFlags(desc, NaClDescGetFlags(desc) | NACL_DESC_FLAGS_MMAP_EXEC_OK); } retval = NaClSetAvail(nap, desc); NaClLog(1, "Entered into open file table at %d\n", retval); } } cleanup: return retval; }