Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid fd_advise on directories #240

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/uvwasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# include <dirent.h>
# include <time.h>
#else
# define _CRT_INTERNAL_NONSTDC_NAMES 1
# include <sys/stat.h>
# include <io.h>
#endif /* _WIN32 */

Expand All @@ -17,6 +19,10 @@
# define UVWASI_FD_READDIR_SUPPORTED 1
#endif

#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif

#include "uvwasi.h"
#include "uvwasi_alloc.h"
#include "uv.h"
Expand Down Expand Up @@ -624,9 +630,10 @@ uvwasi_errno_t uvwasi_fd_advise(uvwasi_t* uvwasi,
uvwasi_advice_t advice) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
uv_fs_t req;
int r;
#ifdef POSIX_FADV_NORMAL
int mapped_advice;
int r;
#endif /* POSIX_FADV_NORMAL */

UVWASI_DEBUG("uvwasi_fd_advise(uvwasi=%p, fd=%d, offset=%"PRIu64", "
Expand Down Expand Up @@ -679,14 +686,27 @@ uvwasi_errno_t uvwasi_fd_advise(uvwasi_t* uvwasi,
if (err != UVWASI_ESUCCESS)
return err;

r = uv_fs_fstat(NULL, &req, wrap->fd, NULL);
if (r == -1) {
err = uvwasi__translate_uv_error(r);
goto exit;
}

if (S_ISDIR(req.statbuf.st_mode)) {
err = UVWASI_EBADF;
goto exit;
}

err = UVWASI_ESUCCESS;

#ifdef POSIX_FADV_NORMAL
r = posix_fadvise(wrap->fd, offset, len, mapped_advice);
if (r != 0)
err = uvwasi__translate_uv_error(uv_translate_sys_error(r));
#endif /* POSIX_FADV_NORMAL */
exit:
uv_mutex_unlock(&wrap->mutex);
uv_fs_req_cleanup(&req);
return err;
}

Expand Down
45 changes: 45 additions & 0 deletions test/test-fd-advise-dir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <assert.h>
#include <stdlib.h>

#include "uv.h"
#include "uvwasi.h"
#include "test-common.h"

#define TEST_TMP_DIR "./out/tmp"
#define TEST_PATH_ADVISE TEST_TMP_DIR "/test_fd_advise_dir"

int main(void) {
#if !defined(_WIN32) && !defined(__ANDROID__)
uvwasi_t uvwasi;
uvwasi_options_t init_options;
uv_fs_t req;
uvwasi_errno_t err;
int r;

setup_test_environment();

r = uv_fs_mkdir(NULL, &req, TEST_TMP_DIR, 0777, NULL);
uv_fs_req_cleanup(&req);
assert(r == 0 || r == UV_EEXIST);

r = uv_fs_mkdir(NULL, &req, TEST_PATH_ADVISE, 0777, NULL);
uv_fs_req_cleanup(&req);
assert(r == 0 || r == UV_EEXIST);

uvwasi_options_init(&init_options);
init_options.preopenc = 1;
init_options.preopens = calloc(1, sizeof(uvwasi_preopen_t));
init_options.preopens[0].mapped_path = "/var";
init_options.preopens[0].real_path = TEST_PATH_ADVISE;

err = uvwasi_init(&uvwasi, &init_options);
assert(err == 0);

err = uvwasi_fd_advise(&uvwasi, 3, 10, 20, UVWASI_ADVICE_DONTNEED);
assert(err == UVWASI_EBADF);

uvwasi_destroy(&uvwasi);
free(init_options.preopens);
#endif /* !defined(_WIN32) && !defined(__ANDROID__) */
return 0;
}
Loading