2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Service Runtime. Directory descriptor / Handle abstraction.
35 * Note that we avoid using the thread-specific data / thread local
36 * storage access to the "errno" variable, and instead use the raw
37 * system call return interface of small negative numbers as errors.
42 #include <sys/types.h>
49 #include <linux/types.h>
50 #include <linux/unistd.h>
52 #include "native_client/include/nacl_platform.h"
54 #include "native_client/service_runtime/nacl_config.h"
55 #include "native_client/service_runtime/nacl_log.h"
56 #include "native_client/service_runtime/nacl_host_desc.h"
57 #include "native_client/service_runtime/nacl_host_dir.h"
58 #include "native_client/service_runtime/sel_util.h"
59 #include "native_client/service_runtime/sel_memory.h"
61 #include "native_client/service_runtime/include/sys/dirent.h"
62 #include "native_client/service_runtime/include/sys/errno.h"
63 #include "native_client/service_runtime/include/sys/fcntl.h"
64 #include "native_client/service_runtime/include/sys/mman.h"
65 #include "native_client/service_runtime/include/sys/stat.h"
68 _syscall3(int, getdents
, uint
, fd
, struct dirent
*, dirp
, uint
, count
)
70 int getdents(unsigned int fd
, struct dirent
* dirp
, unsigned int count
);
72 # include <sys/syscall.h>
73 int getdents(unsigned int fd
, struct dirent
* dirp
, unsigned int count
)
75 return syscall(SYS_getdents
, fd
, dirp
, count
);
79 int NaClHostDirOpen(struct NaClHostDir
*d
,
84 NaClLog(3, "NaClHostDirOpen(0x%08"PRIxPTR
", %s)\n", (uintptr_t) d
, path
);
86 NaClLog(LOG_FATAL
, "NaClHostDirOpen: 'this' is NULL\n");
89 NaClLog(3, "NaClHostDirOpen: invoking open(%s)\n", path
);
90 fd
= open(path
, O_RDONLY
);
91 NaClLog(3, "NaClHostDirOpen: got DIR* %d\n", fd
);
94 "NaClHostDirOpen: open returned -1, errno %d\n", errno
);
95 return -NaClXlateErrno(errno
);
98 NaClLog(3, "NaClHostDirOpen: success.\n");
102 ssize_t
NaClHostDirGetdents(struct NaClHostDir
*d
,
106 struct dirent
*host_direntp
= (struct dirent
*) buf
;
107 struct nacl_abi_dirent
*p
;
112 NaClLog(LOG_FATAL
, "NaClHostDirGetdents: 'this' is NULL\n");
114 NaClLog(3, "NaClHostDirGetdents(0x%08"PRIxPTR
", %u):\n",
115 (uintptr_t) buf
, len
);
116 retval
= getdents(d
->fd
, host_direntp
, len
);
118 return -NaClXlateErrno(errno
);
119 } else if (0 == retval
) {
123 NaClLog(3, "NaClHostDirGetdents: returned %d\n", retval
);
126 * Scrub inode numbers.
130 p
= (struct nacl_abi_dirent
*) (((char *) buf
) + i
);
132 * Make sure we're not tricked into writing outside of the buffer,
133 * because.... (see below)
135 if ((((char *) &p
->nacl_abi_d_ino
) + sizeof(p
->nacl_abi_d_ino
)
136 - (char *) buf
) <= retval
) {
137 p
->nacl_abi_d_ino
= 0x6c43614e;
140 * ... we may pick up a user controlled/bogus value by following p
141 * here, so i could make p point to the last byte of the buffer,
142 * and p->* would be beyond the buffer.
144 i
+= p
->nacl_abi_d_reclen
;
150 int NaClHostDirClose(struct NaClHostDir
*d
)
155 NaClLog(LOG_FATAL
, "NaClHostDirClose: 'this' is NULL\n");
157 NaClLog(3, "NaClHostDirClose(%d)\n", d
->fd
);
158 retval
= close(d
->fd
);
160 return (-1 == retval
) ? -NaClXlateErrno(errno
) : retval
;