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

Set right difference? #3534

Open
Userzxcvbvnm opened this issue Jun 16, 2024 · 1 comment
Open

Set right difference? #3534

Userzxcvbvnm opened this issue Jun 16, 2024 · 1 comment

Comments

@Userzxcvbvnm
Copy link

Userzxcvbvnm commented Jun 16, 2024

Subject of the issue

fd_fdstat_set_rights fails.
I'm not sure whether this is a bug.

Test case

#include <stdio.h>
#include <stdlib.h>
#include <wasi/api.h>
#include <fcntl.h>
#include <unistd.h>

int get_fd(const char *filename, int flags) {
    int fd = open(filename, flags);
    
    if (fd == -1) {
        printf("Get file descriptor of file %s failed!\n", filename);
        return -1;
    } else {
        printf("Get file descriptor of file %s succeed!\n", filename);
        return fd;
    }
}


void closebyfd(int fd) {
    if (close(fd) == -1) {
        printf("Close the file %d by descriptor failed!\n", fd);
    }
}


void print_rights(__wasi_rights_t rights) {
    if (rights & __WASI_RIGHTS_FD_READ)
        printf("__WASI_RIGHTS_FD_READ\n");
    if (rights & __WASI_RIGHTS_FD_SEEK)
        printf("__WASI_RIGHTS_FD_SEEK\n");
    if (rights & __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS)
        printf("__WASI_RIGHTS_FD_FDSTAT_SET_FLAGS\n");
    if (rights & __WASI_RIGHTS_FD_SYNC)
        printf("__WASI_RIGHTS_FD_SYNC\n");
    if (rights & __WASI_RIGHTS_FD_TELL)
        printf("__WASI_RIGHTS_FD_TELL\n");
    if (rights & __WASI_RIGHTS_FD_WRITE )
        printf("__WASI_RIGHTS_FD_WRITE \n");
    if (rights & __WASI_RIGHTS_FD_ADVISE )
        printf("__WASI_RIGHTS_FD_ADVISE \n");
    if (rights & __WASI_RIGHTS_FD_ALLOCATE )
        printf("__WASI_RIGHTS_FD_ALLOCATE \n");
    if (rights & __WASI_RIGHTS_PATH_CREATE_DIRECTORY )
        printf("__WASI_RIGHTS_PATH_CREATE_DIRECTORY \n");
    if (rights & __WASI_RIGHTS_PATH_CREATE_FILE )
        printf("__WASI_RIGHTS_PATH_CREATE_FILE \n");   
    if (rights & __WASI_RIGHTS_PATH_LINK_SOURCE )
        printf("__WASI_RIGHTS_PATH_LINK_SOURCE \n");   
    if (rights & __WASI_RIGHTS_PATH_LINK_TARGET )
        printf("__WASI_RIGHTS_PATH_LINK_TARGET \n");   
    if (rights & __WASI_RIGHTS_PATH_OPEN )
        printf("__WASI_RIGHTS_PATH_OPEN \n");  
    if (rights & __WASI_RIGHTS_FD_READDIR )
        printf("__WASI_RIGHTS_FD_READDIR \n");  
    if (rights & __WASI_RIGHTS_PATH_READLINK )
        printf("__WASI_RIGHTS_PATH_READLINK \n");  
    if (rights & __WASI_RIGHTS_PATH_RENAME_SOURCE )
        printf("__WASI_RIGHTS_PATH_RENAME_SOURCE \n"); 
    if (rights & __WASI_RIGHTS_PATH_RENAME_TARGET )
        printf("__WASI_RIGHTS_PATH_RENAME_TARGET \n"); 
    if (rights & __WASI_RIGHTS_PATH_FILESTAT_GET )
        printf("__WASI_RIGHTS_PATH_FILESTAT_GET \n"); 
    if (rights & __WASI_RIGHTS_PATH_FILESTAT_SET_SIZE )
        printf("__WASI_RIGHTS_PATH_FILESTAT_SET_SIZE \n"); 
    if (rights & __WASI_RIGHTS_PATH_FILESTAT_SET_TIMES )
        printf("__WASI_RIGHTS_PATH_FILESTAT_SET_TIMES \n"); 
    if (rights & __WASI_RIGHTS_FD_FILESTAT_GET )
        printf("__WASI_RIGHTS_FD_FILESTAT_GET \n"); 
    if (rights & __WASI_RIGHTS_FD_FILESTAT_SET_SIZE )
        printf("__WASI_RIGHTS_FD_FILESTAT_SET_SIZE \n"); 
    if (rights & __WASI_RIGHTS_FD_FILESTAT_SET_TIMES)
        printf("__WASI_RIGHTS_FD_FILESTAT_SET_TIMES \n");
    if (rights & __WASI_RIGHTS_PATH_SYMLINK)
        printf("__WASI_RIGHTS_PATH_SYMLINK \n");
    if (rights & __WASI_RIGHTS_PATH_REMOVE_DIRECTORY)
        printf("__WASI_RIGHTS_PATH_REMOVE_DIRECTORY \n");
    if (rights & __WASI_RIGHTS_PATH_UNLINK_FILE)
        printf("__WASI_RIGHTS_PATH_UNLINK_FILE \n");
    if (rights & __WASI_RIGHTS_POLL_FD_READWRITE)
        printf("__WASI_RIGHTS_POLL_FD_READWRITE \n");
    if (rights & __WASI_RIGHTS_SOCK_SHUTDOWN)
        printf("__WASI_RIGHTS_SOCK_SHUTDOWN \n");
    if (rights & __WASI_RIGHTS_SOCK_ACCEPT)
        printf("__WASI_RIGHTS_SOCK_ACCEPT \n");
    printf("\n");
}


int fd_fdstat_set_rights_00012_7we45(int fd) {
    printf("Enter function fd_fdstat_set_rights_00012_7we45\n");

    __wasi_rights_t new_rights = __WASI_RIGHTS_FD_WRITE;

    __wasi_fdstat_t fdstat;
    if (__wasi_fd_fdstat_get(fd, &fdstat) != 0) {
        printf("Failed to get fd rights.\n");
        return 1;
    }
    printf("Current file rights: \n");
    print_rights(fdstat.fs_rights_base);
    
    if (__wasi_fd_fdstat_set_rights(fd, new_rights, 0) != 0) {
        printf("Failed to set fd rights.\n");
        return 1;
    }
    printf("New file rights: \n");
    print_rights(new_rights);
    return 0;
}


int main() {
    int fd = get_fd("hello.txt", O_RDONLY); 
    if (fd == -1) {
        return -1;
    }

    fd_fdstat_set_rights_00012_7we45(fd);

    closebyfd(fd);

    return 0;
}


Your environment

Ubuntu 20.04
x86_64
WAMR 1.3.2 and WAMR 1.2.3

Steps to reproduce

Steps to reproduce:
(1)compile to wasm:./wasi-sdk-21.0/bin/clang --target=wasm32-unkown-wasi --sysroot=./wasi-sdk-21.0/share/wasi-sysroot test.c -o test.wasm

(2)Running wasm:
(Before run the Wasm file, hello.txt exists.)
iwasm --dir=. test.wasm

Expected behavior

Successfully set rights and print.

New file rights: 
__WASI_RIGHTS_FD_WRITE 

This is what wasmtime do.

Actual behavior

Failed to set fd rights.
@TianlongLiang
Copy link
Contributor

I think it's not a bug but it should be an expected behavior that is not allowed to be done in such a way, that you are trying to escalate permissions(adding write rights to a file descriptor that was originally opened with read-only permissions).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants