bytecodealliance / bytecodealliance/wasmtime

lseek permission denied?

Open
#7,332 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 18h
Merged PRs (30d)
126

Description

### Steps to Reproduce
(1) The cfile is :
```
#include
#include
#include
#include

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.