Skip to content

Commit fa4134e

Browse files
committed
Add a way to initialize libpreopen.
Revamp the libpreopen code so that we have a way to properly initialize it and add some basic rights-checking code.
1 parent 727e61b commit fa4134e

6 files changed

Lines changed: 143 additions & 44 deletions

File tree

expected/wasm32-wasi/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ __unlist_locked_file
242242
__uselocale
243243
__utc
244244
__wasilibc_fd_renumber
245+
__wasilibc_init_preopen
245246
__wasilibc_register_preopened_fd
246247
__wasilibc_rmdirat
247248
__wasilibc_rmfileat

libc-bottom-half/cloudlibc/src/common/errno.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <wasi/core.h>
99

10+
#ifdef __wasilibc_unmodified_upstream
1011
// Translates ENOTCAPABLE to ENOTDIR if not a directory.
1112
static inline __wasi_errno_t errno_fixup_directory(__wasi_fd_t fd,
1213
__wasi_errno_t error) {
@@ -18,6 +19,13 @@ static inline __wasi_errno_t errno_fixup_directory(__wasi_fd_t fd,
1819
}
1920
return error;
2021
}
22+
#else
23+
// WASI syscalls should just return ENOTDIR if that's what the problem is.
24+
static inline __wasi_errno_t errno_fixup_directory(__wasi_fd_t fd,
25+
__wasi_errno_t error) {
26+
return error;
27+
}
28+
#endif
2129

2230
#ifdef __wasilibc_unmodified_upstream // posix_spawn etc.
2331
// Translates ENOTCAPABLE to EBADF if a regular file or EACCES otherwise.
@@ -48,6 +56,7 @@ static inline __wasi_errno_t errno_fixup_process(__wasi_fd_t fd,
4856
}
4957
#endif
5058

59+
#ifdef __wasilibc_unmodified_upstream
5160
// Translates ENOTCAPABLE to ENOTSOCK if not a socket.
5261
static inline __wasi_errno_t errno_fixup_socket(__wasi_fd_t fd,
5362
__wasi_errno_t error) {
@@ -64,5 +73,12 @@ static inline __wasi_errno_t errno_fixup_socket(__wasi_fd_t fd,
6473
}
6574
return error;
6675
}
76+
#else
77+
// WASI syscalls should just return ENOTSOCK if that's what the problem is.
78+
static inline __wasi_errno_t errno_fixup_socket(__wasi_fd_t fd,
79+
__wasi_errno_t error) {
80+
return error;
81+
}
82+
#endif
6783

6884
#endif

libc-bottom-half/libpreopen/include/libpreopen.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ int po_preopen(struct po_map *map, const char *path, int flags, ...);
163163
* path, relative to the file (or undefined if no match found)
164164
*/
165165
#ifdef __wasilibc_unmodified_upstream
166-
#else
167-
static
168-
#endif
169166
struct po_relpath po_find(struct po_map *map, const char *path,
170167
cap_rights_t *rights);
168+
#else
169+
static struct po_relpath po_find(struct po_map *map, const char *path,
170+
__wasi_rights_t rights);
171+
#endif
171172

172173
#ifdef __wasilibc_unmodified_upstream
173174
/**

libc-bottom-half/libpreopen/lib/internal.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
#endif
4444

4545
#ifdef __wasilibc_unmodified_upstream
46-
#else
47-
#define WITH_CAPSICUM
48-
#endif
4946
#ifdef WITH_CAPSICUM
5047
#include <sys/capsicum.h>
5148
#endif
49+
#else
50+
// We do Capsicum-style rights-checking, though we use the WASI API directly
51+
// rather than the Capsicum API.
52+
#define WITH_CAPSICUM
53+
#endif
5254

5355
#include <assert.h>
5456
#include <stdbool.h>
@@ -74,7 +76,11 @@ struct po_map_entry {
7476

7577
#ifdef WITH_CAPSICUM
7678
/** Capability rights associated with the file descriptor */
79+
#ifdef __wasilibc_unmodified_upstream
7780
cap_rights_t rights;
81+
#else
82+
__wasi_rights_t rights;
83+
#endif
7884
#endif
7985
};
8086

libc-bottom-half/libpreopen/lib/libpreopen.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,35 @@ po_add(struct po_map *map, const char *path, int fd)
7979
entry->fd = fd;
8080

8181
#ifdef WITH_CAPSICUM
82+
#ifdef __wasilibc_unmodified_upstream
8283
if (cap_rights_get(fd, &entry->rights) != 0) {
8384
return (NULL);
8485
}
86+
#else
87+
__wasi_fdstat_t statbuf;
88+
int r = __wasi_fd_stat_get(fd, &statbuf);
89+
if (r != 0) {
90+
errno = r;
91+
return NULL; // fixme: actually there should be an infallible way to get the rights
92+
}
93+
94+
entry->rights = statbuf.fs_rights_base;
8595
#endif
96+
#endif
97+
8698

8799
po_map_assertvalid(map);
88100

89101
return (map);
90102
}
91103

92104
#ifdef __wasilibc_unmodified_upstream
93-
#else
94-
static
95-
#endif
96105
struct po_relpath
97106
po_find(struct po_map* map, const char *path, cap_rights_t *rights)
107+
#else
108+
static struct po_relpath
109+
po_find(struct po_map* map, const char *path, __wasi_rights_t rights)
110+
#endif
98111
{
99112
const char *relpath ;
100113
struct po_relpath match = { .relative_path = NULL, .dirfd = -1 };
@@ -121,7 +134,11 @@ po_find(struct po_map* map, const char *path, cap_rights_t *rights)
121134
}
122135

123136
#ifdef WITH_CAPSICUM
137+
#ifdef __wasilibc_unmodified_upstream
124138
if (rights && !cap_rights_contains(&entry->rights, rights)) {
139+
#else
140+
if ((rights & ~entry->rights) != 0) {
141+
#endif
125142
continue;
126143
}
127144
#endif

0 commit comments

Comments
 (0)