Stream: git-wasmtime

Topic: wasmtime / issue #7332 lseek permission denied?


view this post on Zulip Wasmtime GitHub notifications bot (Oct 23 2023 at 14:52):

orangeC23 added the bug label to Issue #7332.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 23 2023 at 14:52):

orangeC23 opened issue #7332:

Steps to Reproduce

(1) The cfile is :

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

int main() {
    const char* file_name = "Data/hello.txt";
    int open_style= O_RDONLY | O_CREAT;
    int fd = get_fd(file_name, open_style);
    fd_seek9qW7uWsf5B(fd);
    snapshot(fd);
    return 0;
}

int fd_seek9qW7uWsf5B(int fd) {
    int start_value = 32;
    int seek_parameter = SEEK_SET;
    return fd_seek(fd, start_value, seek_parameter);
}

int get_fd(const char* file_name, int open_style){
    // Open a file for reading
    int fd = open(file_name, open_style);
    if (fd == -1) {
        perror("Failed to open the file");
        return 1;
    }

    return fd;
}

int fd_seek(int fd, int start_value, int seek_parameter) {
    printf("Enter fd_seek.\n");
    off_t offset = lseek(fd, start_value, seek_parameter);
    if (offset == -1) {
        perror("lseek");
        close(fd);
        return 1;
    }

    printf("File size: %lld bytes\n", offset);
    printf("Leave fd_seek.\n");
    return fd;
}

int snapshot(int myfd){
    printf("Enter snapshot\n");

    struct stat file_info;
    if (fstat(myfd, &file_info) == -1) {
        perror("Error getting file attributes");
        close(myfd);
        return 1;
    }

    printf("File Size: %lld bytes \n", (long long)file_info.st_size);

    off_t cur_offset = lseek(myfd, 0, SEEK_CUR);
    if (cur_offset == -1) {
        perror("Error getting current offset");
    }
    printf("Current offset: %lld \n", (long long)cur_offset);

    if (close(myfd) == -1) {
        perror("Error closing file");
        return 1;
    }

    printf("Leave snapshot\n");
}

(2)compile the c file into wasm: ./wasi-sdk-16.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-16.0/share/wasi-sysroot lseek.c -o lseek.wasm
(3)exeute open.wasm
wasmtime run --dir=./Data lseek.wasm
The permission of Data/mydir is 0400 or 0500, user1 create the Data/mydir directory before and user1 execute the Wasm file.

Expected Results

Using gcc lseek.c -o lseek and ./lseek to execute get the following result:

Enter fd_seek.
File size: 32 bytes
Leave fd_seek.
Enter snapshot
File Size: 30 bytes
Current offset: 32
Leave snapshot

And wamr, wasmedge,wazero also print the above message.

Actual Results

wasmtime prints:

Enter fd_seek.
Failed to open the file: Permission denied
lseek: Invalid seek
Error getting file attributes: Bad file descriptor

I'm not sure whether wasmtime and native ubuntu and other runtimes get different could be a bug or only difference ?

Versions and Environment

wasmtime 13.0.0
Operating system: Ubuntu 20.04

Architecture: x86_64

view this post on Zulip Wasmtime GitHub notifications bot (Oct 23 2023 at 14:54):

orangeC23 edited issue #7332:

Steps to Reproduce

(1) The cfile is :

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

int main() {
    const char* file_name = "Data/hello.txt";
    int open_style= O_RDONLY | O_CREAT;
    int fd = get_fd(file_name, open_style);
    fd_seek9qW7uWsf5B(fd);
    snapshot(fd);
    return 0;
}

int fd_seek9qW7uWsf5B(int fd) {
    int start_value = 32;
    int seek_parameter = SEEK_SET;
    return fd_seek(fd, start_value, seek_parameter);
}

int get_fd(const char* file_name, int open_style){
    // Open a file for reading
    int fd = open(file_name, open_style);
    if (fd == -1) {
        perror("Failed to open the file");
        return 1;
    }

    return fd;
}

int fd_seek(int fd, int start_value, int seek_parameter) {
    printf("Enter fd_seek.\n");
    off_t offset = lseek(fd, start_value, seek_parameter);
    if (offset == -1) {
        perror("lseek");
        close(fd);
        return 1;
    }

    printf("File size: %lld bytes\n", offset);
    printf("Leave fd_seek.\n");
    return fd;
}

int snapshot(int myfd){
    printf("Enter snapshot\n");

    struct stat file_info;
    if (fstat(myfd, &file_info) == -1) {
        perror("Error getting file attributes");
        close(myfd);
        return 1;
    }

    printf("File Size: %lld bytes \n", (long long)file_info.st_size);

    off_t cur_offset = lseek(myfd, 0, SEEK_CUR);
    if (cur_offset == -1) {
        perror("Error getting current offset");
    }
    printf("Current offset: %lld \n", (long long)cur_offset);

    if (close(myfd) == -1) {
        perror("Error closing file");
        return 1;
    }

    printf("Leave snapshot\n");
}

(2)compile the c file into wasm: ./wasi-sdk-16.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-16.0/share/wasi-sysroot lseek.c -o lseek.wasm
(3)exeute open.wasm
wasmtime run --dir=./Data lseek.wasm
The permission of Data/hello.txt is 0400 or 0500, user1 create the Data/hello.txt directory before and user1 execute the Wasm file.

Expected Results

Using gcc lseek.c -o lseek and ./lseek to execute get the following result:

Enter fd_seek.
File size: 32 bytes
Leave fd_seek.
Enter snapshot
File Size: 30 bytes
Current offset: 32
Leave snapshot

And wamr, wasmedge,wazero also print the above message.

Actual Results

wasmtime prints:

Enter fd_seek.
Failed to open the file: Permission denied
lseek: Invalid seek
Error getting file attributes: Bad file descriptor

I'm not sure whether wasmtime and native ubuntu and other runtimes get different could be a bug or only difference ?

Versions and Environment

wasmtime 13.0.0
Operating system: Ubuntu 20.04

Architecture: x86_64

view this post on Zulip Wasmtime GitHub notifications bot (Oct 23 2023 at 16:26):

alexcrichton commented on issue #7332:

As with prior bugs, I'll note again that the C source as-listed here doesn't compile due to requiring reordering functions. One thing I'd also recommend is improving error handling in the source code. For example the issue here is that the file can't be opened due to permissions, but the program continues thinking that the open file descriptor is "1" which means you're trying to seek stdout which doesn't work.

Otherwise though the main issue here is that with native permission bits of 0400 and 0500 they don't have the write bit. The file is being opened in Wasmtime for writing, though, which is why this is failing. The file is being opened here with O_RDONLY | O_CREAT. That eventually translates to requesting a write permission when opening.

I don't know enough about everything in play here to say definitively which is correct. I understand that Linux seems to allow opening a preexisting read-only file with O_CREAT. Wasmtime doesn't seem to allow this (maybe cap-std? unsure.). I'm also not sure what other platforms do. If you're interested assistance in investigating this would be appreciated.


Last updated: Nov 22 2024 at 16:03 UTC