Skip to content

WasmFS: A file can be created in a read-only directory #18033

Open

Description

@kd935

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.24-git (6292601a59b6c23382e26a438c9ae3a6c3c4d3e8)
clang version 16.0.0 (https://github.com/llvm/llvm-project 69353e7c82f732ac7303fe94f9a6d527b1b221ab)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\Users\kdots\.emsdk\upstream\bin

Failing command line in full:

#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <cassert>
#include <stdio.h>

int main()
{
  mkdir("test", S_IRUGO);
  int fd = open("test/file", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  perror("Error: ");
  printf("errno - %d\n", errno);
  printf("fd - %d\n", fd);
  assert(errno == EACCES);
  assert(fd < 0);
}

Full link command and output with -v appended:

C:\emscripten>em++ -v C:/build/test.cpp -o C:/build/test.html -s WASMFS=1
 "C:/Users/kdots/.emsdk/upstream/bin\clang.exe" --version
 "C:/Users/kdots/.emsdk/upstream/bin\clang++.exe" -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -DEMSCRIPTEN -IC:\emscripten\cache\sysroot\include\SDL --sysroot=C:\emscripten\cache\sysroot -Xclang -iwithsysroot/include\compat -v C:/build/test.cpp -c -o C:\Users\kdots\AppData\Local\Temp\emscripten_temp_itrpqa3s\test_0.o
clang version 16.0.0 (https://github.com/llvm/llvm-project 69353e7c82f732ac7303fe94f9a6d527b1b221ab)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\Users\kdots\.emsdk\upstream\bin
 (in-process)
 "C:\\Users\\kdots\\.emsdk\\upstream\\bin\\clang++.exe" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -main-file-name test.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v "-fcoverage-compilation-dir=C:\\emscripten" -resource-dir "C:\\Users\\kdots\\.emsdk\\upstream\\lib\\clang\\16.0.0" -D EMSCRIPTEN -I "C:\\emscripten\\cache\\sysroot\\include\\SDL" -isysroot "C:\\emscripten\\cache\\sysroot" -internal-isystem "C:\\emscripten\\cache\\sysroot/include/wasm32-emscripten/c++/v1" -internal-isystem "C:\\emscripten\\cache\\sysroot/include/c++/v1" -internal-isystem "C:\\Users\\kdots\\.emsdk\\upstream\\lib\\clang\\16.0.0\\include" -internal-isystem "C:\\emscripten\\cache\\sysroot/include/wasm32-emscripten" -internal-isystem "C:\\emscripten\\cache\\sysroot/include" -fdeprecated-macro "-fdebug-compilation-dir=C:\\emscripten" -ferror-limit 19 -fmessage-length=167 -fvisibility=default -fgnuc-version=4.2.1 -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics "-iwithsysroot/include\\compat" -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o "C:\\Users\\kdots\\AppData\\Local\\Temp\\emscripten_temp_itrpqa3s\\test_0.o" -x c++ C:/build/test.cpp
clang -cc1 version 16.0.0 based upon LLVM 16.0.0git default target x86_64-pc-windows-msvc
ignoring nonexistent directory "C:\emscripten\cache\sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "C:\emscripten\cache\sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
 C:\emscripten\cache\sysroot\include\SDL
 C:\emscripten\cache\sysroot/include\compat
 C:\emscripten\cache\sysroot/include/c++/v1
 C:\Users\kdots\.emsdk\upstream\lib\clang\16.0.0\include
 C:\emscripten\cache\sysroot/include
End of search list.
 "C:/Users/kdots/.emsdk/upstream/bin\wasm-ld.exe" -o C:/build/test.wasm C:\Users\kdots\AppData\Local\Temp\emscripten_temp_itrpqa3s\test_0.o -LC:\emscripten\cache\sysroot\lib\wasm32-emscripten --whole-archive -lwasmfs-debug --no-whole-archive -lGL -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=_wasmfs_read_file --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=__get_temp_ret --export=__set_temp_ret --export=malloc --export=free --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024
 "C:/Users/kdots/.emsdk/upstream\bin\wasm-emscripten-finalize" --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers C:/build/test.wasm -o C:/build/test.wasm --detect-features
 "C:/Users/kdots/.emsdk/node/14.18.2_64bit/bin/node.exe" C:\emscripten\src\compiler.js C:\Users\kdots\AppData\Local\Temp\tmpxffglsrr.json
 "C:/Users/kdots/.emsdk/upstream/bin\llvm-objcopy.exe" C:/build/test.wasm C:/build/test.wasm --remove-section=.debug* --remove-section=producers
 "C:/Users/kdots/.emsdk/node/14.18.2_64bit/bin/node.exe" C:\emscripten\tools\preprocessor.js C:\Users\kdots\AppData\Local\Temp\emscripten_temp_itrpqa3s\settings.js shell.html

Expected output (when building without wasmfs):

Error: : Permission denied
errno - 2
fd - -1

Actual output:

Error: : No error information
errno - 0
fd - 3
Aborted(Assertion failed: errno == EACCES, at: C:/build/test.cpp,14,main)

Activity

  1. added this to the WasmFS feature parity milestone on Oct 11, 2022
  2. moved this to Todo in WasmFSon Oct 11, 2022
  3. tlively commented on Oct 11, 2022

    @tlively
    Member

    I think the permissions checking is a bit spotty overall. There are probably other related bugs as well.

  4. typedrat commented on Oct 1, 2026

    @typedrat
    Contributor

    This still reproduces on 6.0.9. It's specific to file creation through open (and mknod, which goes through the same doOpen). In the same read-only directory, mkdir and unlink already fail with EACCES (rename has the same check in the source), and the JS file system rejects all three calls the test makes.

    Version of emscripten/emsdk:

    emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 6.0.9-git
    clang version 22.1.8
    Target: wasm32-unknown-emscripten
    Thread model: posix
    InstalledDir: /nix/store/a0h9ym1ai9yawkmymx2wp0vnh64hsv0r-clang-22.1.8/bin
    

    This is the 6.0.9 release tag as packaged by nixpkgs. nixpkgs builds it against its own LLVM 22.1.8, so there is no commit hash. Node.js v24.20.0, x86_64 Linux.

    Test case (create_perm.c):

    #include <errno.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    static void report(const char *what, int r) {
      printf("%-40s %s\n", what, r < 0 ? strerror(errno) : "succeeded");
    }
    
    int main(void) {
      printf("uid %d\n", (int)getuid());
      mkdir("/ro", 0777);
      close(open("/ro/existing", O_CREAT | O_WRONLY, 0666));
      /* Searchable and readable, but not writable. */
      chmod("/ro", 0555);
    
      errno = 0;
      report("mkdir(\"/ro/dir\")", mkdir("/ro/dir", 0777));
      errno = 0;
      report("unlink(\"/ro/existing\")", unlink("/ro/existing"));
      errno = 0;
      int fd = open("/ro/new", O_CREAT | O_WRONLY, 0666);
      report("open(\"/ro/new\", O_CREAT|O_WRONLY)", fd);
      if (fd >= 0)
        close(fd);
      struct stat st;
      printf("/ro/new exists afterwards: %s\n", stat("/ro/new", &st) == 0 ? "yes" : "no");
      return 0;
    }

    Failing command line in full (the same at -O2):

    $ emcc -O0 -sWASMFS create_perm.c -o create_perm.js && node create_perm.js
    uid 0
    mkdir("/ro/dir")                         Permission denied
    unlink("/ro/existing")                   Permission denied
    open("/ro/new", O_CREAT|O_WRONLY)        succeeded
    /ro/new exists afterwards: yes
    

    JS file system, for comparison:

    $ emcc -O0 create_perm.c -o create_perm.js && node create_perm.js
    uid 0
    mkdir("/ro/dir")                         Permission denied
    unlink("/ro/existing")                   Permission denied
    open("/ro/new", O_CREAT|O_WRONLY)        Permission denied
    /ro/new exists afterwards: no
    
    Full compile and link output with -v appended (cache and scratch paths shortened)
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=$EM_CACHE/sysroot -resource-dir=/nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/lib/clang/22/ -idirafter$EM_CACHE/sysroot/include -iwithsysroot/include/c++/v1 -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v -O0 -c create_perm.c -o /tmp/emscripten_temp_6mommx4r/create_perm.o
    clang version 22.1.8
    Target: wasm32-unknown-emscripten
    Thread model: posix
    InstalledDir: /nix/store/a0h9ym1ai9yawkmymx2wp0vnh64hsv0r-clang-22.1.8/bin
     (in-process)
     "/nix/store/a0h9ym1ai9yawkmymx2wp0vnh64hsv0r-clang-22.1.8/bin/clang-22" -cc1 -triple wasm32-unknown-emscripten -O0 -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name create_perm.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -fvisibility=hidden -debugger-tuning=gdb -fdebug-compilation-dir=. -v -fcoverage-compilation-dir=. -resource-dir /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/lib/clang/22/ -idirafter $EM_CACHE/sysroot/include -iwithsysroot /include/c++/v1 -isysroot $EM_CACHE/sysroot -internal-isystem /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/lib/clang/22/include -internal-isystem $EM_CACHE/sysroot/include/wasm32-emscripten -internal-isystem $EM_CACHE/sysroot/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_6mommx4r/create_perm.o -x c create_perm.c
    clang -cc1 version 22.1.8 based upon LLVM 22.1.8 default target x86_64-unknown-linux-gnu
    ignoring nonexistent directory "$EM_CACHE/sysroot/include/wasm32-emscripten"
    ignoring duplicate directory "$EM_CACHE/sysroot/include"
    #include "..." search starts here:
    #include <...> search starts here:
     $EM_CACHE/sysroot/include/c++/v1
     $EM_CACHE/sysroot/include/fakesdl
     $EM_CACHE/sysroot/include/compat
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/lib/clang/22/include
     $EM_CACHE/sysroot/include
    End of search list.
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/bin/clang --version
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/bin/wasm-ld -o create_perm.wasm /tmp/tmpjw032n2mlibemscripten_js_symbols.so -Bstatic --strip-debug --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=wasmfs_flush --export=_emscripten_stack_alloc --export=__wasm_call_ctors --export=_emscripten_stack_restore --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=65536 --no-growable-memory --initial-heap=16777216 --no-entry --stack-first --table-base=1 /tmp/emscripten_temp_6mommx4r/create_perm.o -L$EM_CACHE/sysroot/lib/wasm32-emscripten -L/nix/store/03iyrnrsxnbc64y6r62irmlbnbap5ya2-emscripten-6.0.9/share/emscripten/src/lib -lGL-getprocaddr -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc-debug -lclang_rt.builtins -lc++-debug-noexcept -lc++abi-debug-noexcept -lsockets -lwasmfs_no_fs -lwasmfs-debug -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr
     /nix/store/l05jw62495v8gcd0f2r01klky8i5k52i-emscripten-llvm-6.0.9/bin/llvm-objcopy create_perm.wasm create_perm.wasm '--remove-section=llvm.func_attr.annotate.*' --remove-section=producers --remove-section=name '--remove-section=.debug*'
    

    Where: doOpen has // TODO: Check write permissions on the parent directory. where it creates the file (syscalls.cpp#L478). doMkdir does the check (syscalls.cpp#L634-L636), and so do unlink (#L873) and rename (#L1045).

    Suggested fix: the same check as doMkdir.

    --- a/system/lib/wasmfs/syscalls.cpp
    +++ b/system/lib/wasmfs/syscalls.cpp
    @@ -475,7 +475,9 @@
             backend = parent->getBackend();
           }
    
    -      // TODO: Check write permissions on the parent directory.
    +      if (!(lockedParent.getMode() & WASMFS_PERM_WRITE)) {
    +        return -EACCES;
    +      }
           std::shared_ptr<File> created;
           if (backend == parent->getBackend()) {
             created = lockedParent.insertDataFile(std::string(childName), mode);

    With this applied to 6.0.9, the test case matches the JS FS:

    uid 0
    mkdir("/ro/dir")                         Permission denied
    unlink("/ro/existing")                   Permission denied
    open("/ro/new", O_CREAT|O_WRONLY)        Permission denied
    /ro/new exists afterwards: no
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status
      Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions