[Linux/RISC-V64] pread64 on an O_WRONLY descriptor returns EPERM instead of EBADF

Đang mở Phù hợp với người mới
#1,686 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
80/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, python
Lĩnh vực
reverse-engineering

Hướng nghiên cứu

Lỗi nằm ở trình xử lý pread64 trong qiling/os/posix/syscall/unistd.py, khoảng dòng 373-400, nơi OSError của máy chủ bị bắt và trả về -1 thô mà không kèm errno. Hãy bắt đầu bằng cách biên dịch đoạn probe C trong issue cho riscv64 và chạy nó dưới Qiling với probe.bin trong rootfs để xác nhận EPERM. Hoàn thành khi pread64 với O_WRONLY báo EBADF, đối chứng có thể đọc vẫn đọc được một byte và byte đầu ra không thay đổi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Describe the bug

For a valid file descriptor opened with O_WRONLY, Linux pread64 returns EBADF because the descriptor is not open for reading. Qiling catches the host OSError but returns raw -1, which the RV64 guest observes as EPERM.

Sample Code

Place a file named probe.bin in the guest rootfs and run this static RV64 guest:

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>

int main(void)
{
    char byte = 0x55;
    int write_only = open("/probe.bin", O_WRONLY);
    if (write_only < 0)
        return 2;

    errno = 0;
    long bad = syscall(SYS_pread64, write_only, &byte, 1, 0);
    int bad_errno = errno;
    char after_bad = byte;

    int readable = open("/probe.bin", O_RDONLY);
    if (readable < 0)
        return 3;
    long good = syscall(SYS_pread64, readable, &byte, 1, 0);

    printf("write_only=%ld errno=%d after_bad=%02x readable=%ld byte=%02x\n",
           bad, bad_errno, (unsigned char)after_bad, good,
           (unsigned char)byte);
    return 0;
}
riscv64-linux-gnu-gcc -static -O2 -Wall -Wextra pread64_probe.c -o probe.elf
mkdir -p rootfs
printf X > rootfs/probe.bin
cp probe.elf rootfs/probe.elf
python3 -c 'from qiling import Qiling; Qiling(["./rootfs/probe.elf"], "./rootfs").run()'

The readable descriptor is a success control and should read one byte. The O_WRONLY call should leave the output byte unchanged and report EBADF.

Expected behavior

The guest should observe -1 with errno=EBADF for the O_WRONLY descriptor. Qiling v1.4.11 returns raw -1, which becomes EPERM in the guest. The affected handler catches OSError without preserving its errno in unistd.py.

Additional context

The baseline behavior was reproduced on RV64 Linux with Qiling v1.4.11. The current dev commit 23ad4fd369a360ad7992d90cb7577f0241061c2b has the same exception path; this current-head check was static. The read-only/write-only semantics are covered by Linux pread64(2).

Ngôn ngữ chính
Python
Star
6.2k
Fork
806
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

  • Có Dockerfile hoặc tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của qilingframework/qiling

Tất cả issue của qilingframework/qiling

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.