Skip to content

Commit 54102f0

Browse files
authored
Ignore rights in libpreopen. (#129)
Don't ignore paths which don't have the required rights. This means that if the lookup finds a path that doesn't have the required rights, it'll just proceed to the actual operation which will fail with `ENOTCAPABLE`. Intuitively, use cases which would depend on having multiple overlapping matching paths for a given lookup and intelligently picking the one with the required rights seems like they should be uncommon. This is simpler overall, and requires less code.
1 parent 8c9e1c6 commit 54102f0

File tree

2 files changed

+20
-67
lines changed

2 files changed

+20
-67
lines changed

libc-bottom-half/headers/public/wasi/libc-find-relpath.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ extern "C" {
88
/**
99
* Look up the given path in the preopened directory map. If a suitable
1010
* entry is found, return its directory file descriptor, and store the
11-
* computed relative path in *relative_path. Ignore preopened directories
12-
* which don't provide the specified rights.
11+
* computed relative path in *relative_path.
1312
*
1413
* Returns -1 if no directories were suitable.
1514
*/
16-
int __wasilibc_find_relpath(const char *path,
17-
__wasi_rights_t rights_base,
18-
__wasi_rights_t rights_inheriting,
19-
const char **relative_path);
15+
int __wasilibc_find_relpath(const char *path, const char **relative_path);
2016

2117
#ifdef __cplusplus
2218
}

libc-bottom-half/libpreopen/libpreopen.c

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ int
7575
__wasilibc_open_nomode(const char *path, int flags)
7676
{
7777
const char *relative_path;
78-
int dirfd = __wasilibc_find_relpath(path, __WASI_RIGHT_PATH_OPEN, 0,
79-
&relative_path);
78+
int dirfd = __wasilibc_find_relpath(path, &relative_path);
8079

8180
// If we can't find a preopened directory handle to open this file with,
8281
// indicate that the program lacks the capabilities.
@@ -92,8 +91,7 @@ int
9291
access(const char *path, int mode)
9392
{
9493
const char *relative_path;
95-
int dirfd = __wasilibc_find_relpath(path, __WASI_RIGHT_PATH_FILESTAT_GET, 0,
96-
&relative_path);
94+
int dirfd = __wasilibc_find_relpath(path, &relative_path);
9795

9896
// If we can't find a preopened directory handle to open this file with,
9997
// indicate that the program lacks the capabilities.
@@ -109,8 +107,7 @@ int
109107
lstat(const char *path, struct stat *st)
110108
{
111109
const char *relative_path;
112-
int dirfd = __wasilibc_find_relpath(path, __WASI_RIGHT_PATH_FILESTAT_GET, 0,
113-
&relative_path);
110+
int dirfd = __wasilibc_find_relpath(path, &relative_path);
114111

115112
// If we can't find a preopened directory handle to open this file with,
116113
// indicate that the program lacks the capabilities.
@@ -126,12 +123,10 @@ int
126123
rename(const char *from, const char *to)
127124
{
128125
const char *from_relative_path;
129-
int from_dirfd = __wasilibc_find_relpath(from, __WASI_RIGHT_PATH_RENAME_SOURCE, 0,
130-
&from_relative_path);
126+
int from_dirfd = __wasilibc_find_relpath(from, &from_relative_path);
131127

132128
const char *to_relative_path;
133-
int to_dirfd = __wasilibc_find_relpath(to, __WASI_RIGHT_PATH_RENAME_TARGET, 0,
134-
&to_relative_path);
129+
int to_dirfd = __wasilibc_find_relpath(to, &to_relative_path);
135130

136131
// If we can't find a preopened directory handle to open this file with,
137132
// indicate that the program lacks the capabilities.
@@ -147,8 +142,7 @@ int
147142
stat(const char *path, struct stat *st)
148143
{
149144
const char *relative_path;
150-
int dirfd = __wasilibc_find_relpath(path, __WASI_RIGHT_PATH_FILESTAT_GET, 0,
151-
&relative_path);
145+
int dirfd = __wasilibc_find_relpath(path, &relative_path);
152146

153147
// If we can't find a preopened directory handle to open this file with,
154148
// indicate that the program lacks the capabilities.
@@ -164,8 +158,7 @@ int
164158
unlink(const char *path)
165159
{
166160
const char *relative_path;
167-
int dirfd = __wasilibc_find_relpath(path, __WASI_RIGHT_PATH_UNLINK_FILE, 0,
168-
&relative_path);
161+
int dirfd = __wasilibc_find_relpath(path, &relative_path);
169162

170163
// If we can't find a preopened directory handle to open this file with,
171164
// indicate that the program lacks the capabilities.
@@ -184,8 +177,7 @@ int
184177
rmdir(const char *pathname)
185178
{
186179
const char *relative_path;
187-
int dirfd = __wasilibc_find_relpath(pathname, __WASI_RIGHT_PATH_REMOVE_DIRECTORY, 0,
188-
&relative_path);
180+
int dirfd = __wasilibc_find_relpath(pathname, &relative_path);
189181

190182
// If we can't find a preopened directory handle to open this file with,
191183
// indicate that the program lacks the capabilities.
@@ -201,20 +193,14 @@ int
201193
remove(const char *pathname)
202194
{
203195
const char *relative_path;
204-
int dirfd = __wasilibc_find_relpath(pathname,
205-
__WASI_RIGHT_PATH_UNLINK_FILE |
206-
__WASI_RIGHT_PATH_REMOVE_DIRECTORY,
207-
0,
208-
&relative_path);
196+
int dirfd = __wasilibc_find_relpath(pathname, &relative_path);
209197

210198
// If searching for both file and directory rights failed, try searching
211199
// for either individually.
212200
if (dirfd == -1) {
213-
dirfd = __wasilibc_find_relpath(pathname, __WASI_RIGHT_PATH_UNLINK_FILE, 0,
214-
&relative_path);
201+
dirfd = __wasilibc_find_relpath(pathname, &relative_path);
215202
if (dirfd == -1) {
216-
dirfd = __wasilibc_find_relpath(pathname, __WASI_RIGHT_PATH_REMOVE_DIRECTORY, 0,
217-
&relative_path);
203+
dirfd = __wasilibc_find_relpath(pathname, &relative_path);
218204
}
219205
}
220206

@@ -235,12 +221,10 @@ int
235221
link(const char *oldpath, const char *newpath)
236222
{
237223
const char *old_relative_path;
238-
int old_dirfd = __wasilibc_find_relpath(oldpath, __WASI_RIGHT_PATH_LINK_SOURCE, 0,
239-
&old_relative_path);
224+
int old_dirfd = __wasilibc_find_relpath(oldpath, &old_relative_path);
240225

241226
const char *new_relative_path;
242-
int new_dirfd = __wasilibc_find_relpath(newpath, __WASI_RIGHT_PATH_LINK_TARGET, 0,
243-
&new_relative_path);
227+
int new_dirfd = __wasilibc_find_relpath(newpath, &new_relative_path);
244228

245229
// If we can't find a preopened directory handle to open this file with,
246230
// indicate that the program lacks the capabilities.
@@ -256,8 +240,7 @@ int
256240
mkdir(const char *pathname, mode_t mode)
257241
{
258242
const char *relative_path;
259-
int dirfd = __wasilibc_find_relpath(pathname, __WASI_RIGHT_PATH_CREATE_DIRECTORY, 0,
260-
&relative_path);
243+
int dirfd = __wasilibc_find_relpath(pathname, &relative_path);
261244

262245
// If we can't find a preopened directory handle to open this file with,
263246
// indicate that the program lacks the capabilities.
@@ -273,8 +256,7 @@ DIR *
273256
opendir(const char *name)
274257
{
275258
const char *relative_path;
276-
int dirfd = __wasilibc_find_relpath(name, __WASI_RIGHT_PATH_OPEN, 0,
277-
&relative_path);
259+
int dirfd = __wasilibc_find_relpath(name, &relative_path);
278260

279261
// If we can't find a preopened directory handle to open this file with,
280262
// indicate that the program lacks the capabilities.
@@ -290,8 +272,7 @@ ssize_t
290272
readlink(const char *pathname, char *buf, size_t bufsiz)
291273
{
292274
const char *relative_path;
293-
int dirfd = __wasilibc_find_relpath(pathname, __WASI_RIGHT_PATH_READLINK, 0,
294-
&relative_path);
275+
int dirfd = __wasilibc_find_relpath(pathname, &relative_path);
295276

296277
// If we can't find a preopened directory handle to open this file with,
297278
// indicate that the program lacks the capabilities.
@@ -311,10 +292,7 @@ scandir(
311292
int (*compar)(const struct dirent **, const struct dirent **))
312293
{
313294
const char *relative_path;
314-
int dirfd = __wasilibc_find_relpath(dirp,
315-
__WASI_RIGHT_PATH_OPEN,
316-
__WASI_RIGHT_FD_READDIR,
317-
&relative_path);
295+
int dirfd = __wasilibc_find_relpath(dirp, &relative_path);
318296

319297
// If we can't find a preopened directory handle to open this file with,
320298
// indicate that the program lacks the capabilities.
@@ -330,8 +308,7 @@ int
330308
symlink(const char *target, const char *linkpath)
331309
{
332310
const char *relative_path;
333-
int dirfd = __wasilibc_find_relpath(linkpath, __WASI_RIGHT_PATH_SYMLINK, 0,
334-
&relative_path);
311+
int dirfd = __wasilibc_find_relpath(linkpath, &relative_path);
335312

336313
// If we can't find a preopened directory handle to open this file with,
337314
// indicate that the program lacks the capabilities.
@@ -359,10 +336,6 @@ struct po_map_entry {
359336

360337
/// File descriptor (which may be a directory)
361338
int fd;
362-
363-
/// Capability rights associated with the file descriptor
364-
__wasi_rights_t rights_base;
365-
__wasi_rights_t rights_inheriting;
366339
};
367340

368341
/// A vector of po_map_entry.
@@ -472,19 +445,10 @@ internal_register_preopened_fd(int fd, const char *name)
472445
}
473446
}
474447

475-
__wasi_fdstat_t statbuf;
476-
int r = __wasi_fd_fdstat_get((__wasi_fd_t)fd, &statbuf);
477-
if (r != 0) {
478-
errno = r;
479-
return -1; // TODO: Add an infallible way to get the rights?
480-
}
481-
482448
struct po_map_entry *entry = &global_map.entries[global_map.length++];
483449

484450
entry->name = name;
485451
entry->fd = fd;
486-
entry->rights_base = statbuf.fs_rights_base;
487-
entry->rights_inheriting = statbuf.fs_rights_inheriting;
488452

489453
po_map_assertvalid();
490454

@@ -504,8 +468,6 @@ __wasilibc_register_preopened_fd(int fd, const char *path)
504468
int
505469
__wasilibc_find_relpath(
506470
const char *path,
507-
__wasi_rights_t rights_base,
508-
__wasi_rights_t rights_inheriting,
509471
const char **relative_path)
510472
{
511473
size_t bestlen = 0;
@@ -539,11 +501,6 @@ __wasilibc_find_relpath(
539501
continue;
540502
}
541503

542-
if ((rights_base & ~entry->rights_base) != 0 ||
543-
(rights_inheriting & ~entry->rights_inheriting) != 0) {
544-
continue;
545-
}
546-
547504
best = entry->fd;
548505
bestlen = len;
549506
any_matches = true;

0 commit comments

Comments
 (0)