[WASMFS] Enforce file name char limit (#15641)

Relevant Issue: #15041
- Enforce file name character limit
This commit is contained in:
Ethan Lee 2021-11-29 17:19:46 -05:00 коммит произвёл GitHub
Родитель 0b10027619
Коммит ae5c256cac
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 51 добавлений и 2 удалений

Просмотреть файл

@ -27,6 +27,9 @@
// Used to improve readability compared to those in stat.h
#define WASMFS_PERM_WRITE 0222
// In Linux, the maximum length for a filename is 255 bytes.
#define WASMFS_NAME_MAX 255
extern "C" {
using namespace wasmfs;
@ -440,6 +443,9 @@ static __wasi_fd_t doOpen(char* pathname,
}
auto base = pathParts.back();
if (base.size() > WASMFS_NAME_MAX) {
return -ENAMETOOLONG;
}
// Root directory
if (pathParts.size() == 1 && pathParts[0] == "/") {
@ -534,6 +540,9 @@ static long doMkdir(char* path, long mode, backend_t backend = NullBackend) {
}
auto base = pathParts.back();
if (base.size() > WASMFS_NAME_MAX) {
return -ENAMETOOLONG;
}
long err;
auto parentDir = getDir(pathParts.begin(), pathParts.end() - 1, err);
@ -831,7 +840,6 @@ long __syscall_getdents64(long fd, long dirp, long count) {
result->d_reclen = sizeof(dirent);
result->d_type =
curr.file->is<Directory>() ? DT_DIR : DT_REG; // TODO: add symlinks.
// TODO: Enforce that the name can fit in the d_name field.
assert(curr.name.size() + 1 <= sizeof(result->d_name));
strcpy(result->d_name, curr.name.c_str());
++result;
@ -916,6 +924,9 @@ long __syscall_rename(long old_path, long new_path) {
}
auto newBase = newPathParts.back();
if (newBase.size() > WASMFS_NAME_MAX) {
return -ENAMETOOLONG;
}
// oldPath is the forbidden ancestor.
auto newParentDir =

Просмотреть файл

@ -116,6 +116,18 @@ void test() {
assert(err == -1);
assert(errno == EACCES);
// Can't rename a file with a new path name that is longer than WASMFS_NAME_MAX.
#ifdef WASMFS
errno = 0;
rename("dir",
"000000000100000000020000000003000000000400000000050000000006000000000"
"700000000080000000009000000000000000000010000000002000000000300000000"
"040000000005000000000600000000070000000008000000000900000000000000000"
"0010000000002000000000300000000040000000005123456");
assert(errno == ENAMETOOLONG);
#endif
// Can't use an empty path for oldpath.
err = rename("", "test");
assert(err == -1);
@ -133,7 +145,8 @@ void test() {
err = rename("dir", "dir/somename/noexist");
assert(err == -1);
// In the JS file system, this returns ENOENT rather than detecting that dir is an ancestor.
// In the JS file system, this returns ENOENT rather than detecting that dir
// is an ancestor.
#ifdef WASMFS
assert(errno == EINVAL);
#else

Просмотреть файл

@ -87,6 +87,17 @@ int main() {
printf("File contents: %s", buf3);
assert(errno == 0);
// Try to make a file with a name that is greater than WASMFS_NAME_MAX.
errno = 0;
open("00000000010000000002000000000300000000040000000005000000000600000000070"
"00000000800000000090000000000000000000100000000020000000003000000000400"
"00000005000000000600000000070000000008000000000900000000000000000001000"
"0000002000000000300000000040000000005123456",
O_RDWR);
#ifdef WASMFS
assert(errno == ENAMETOOLONG);
#endif
// TODO: use seek to test out of bounds read.
return 0;

Просмотреть файл

@ -105,5 +105,19 @@ int main() {
printf("Errno: %s\n", strerror(errno));
assert(errno == 0);
// Try to make a directory with a name that is longer than WASMFS_NAME_MAX.
// In Linux, creating a directory with a name that is longer than 255
// characters returns ENAMETOOLONG.
errno = 0;
mkdir("/working/"
"0000000001000000000200000000030000000004000000000500000000060000000007"
"0000000008000000000900000000000000000001000000000200000000030000000004"
"0000000005000000000600000000070000000008000000000900000000000000000001"
"0000000002000000000300000000040000000005123456",
0777);
#ifdef WASMFS
assert(errno == ENAMETOOLONG);
#endif
return 0;
}