Skip to content

Bug in fcntl and fcntl64 implementations #1157

@emkaix

Description

@emkaix

Describe the bug

https://github.com/qilingframework/qiling/blob/dev/qiling/os/posix/syscall/fcntl.py#L176
The current implementations of fcntl and fcntl64 on the dev branch don't return the "lowest numbered available file descriptor greater than or equal to arg", as described in the man pages https://linux.die.net/man/2/fcntl64. arg is used as the second argument for enumerate, which only affects idx, but not val https://docs.python.org/3/library/functions.html#enumerate. Because of this, idx returned by enumerate doesn't correspond to the real index in ql.os.fd for a given val, in case arg is greater than 0.

Example Scenario:

File descriptor 0: <in use>
File descriptor 1: None
File descriptor 2: <in use>
File descriptor 3: <in use>

fcntl64(fd = 0x3, cmd = F_DUPFD, arg = 0x1) should return 1, but instead 2 is returned, which breaks functionality.

Expected behavior

fcntl64 and fcntl syscalls with cmd==F_DUPFD should return the "lowest numbered available file descriptor greater than or equal to arg", as described in the man pages https://linux.die.net/man/2/fcntl64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions