[RISC-V64/Linux] mprotect(addr, SIZE_MAX, ...) returns success

未关闭
#1,685 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
60/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
linux, python

调研方向

The bug is in the POSIX mprotect handler at qiling/os/posix/syscall/mman.py around lines 65-73, which calls ql.mem.protect(start, mlen, prot) and returns success even when the range is rejected. Start by reading that handler and the memory protect call it depends on, then run the probe from the issue with SIZE_MAX as the length. Done means the SIZE_MAX request returns -1 with errno ENOMEM on RV64 Linux, while the one-page control still returns 0.

由索引模型根据 Issue 内容生成。

描述

Describe the bug

On an RV64 Linux guest, Qiling returns success for mprotect() when the requested length is SIZE_MAX and the start address is a valid mapped page. Linux returns ENOMEM because the range extends beyond the guest address space. A normal one-page protection change succeeds as a control.

Sample Code
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>

int main(void)
{
    void *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (page == MAP_FAILED)
        return 2;

    errno = 0;
    long invalid = syscall(SYS_mprotect, page, (size_t)-1, PROT_READ);
    int invalid_errno = errno;

    void *control_page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
                              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (control_page == MAP_FAILED)
        return 3;
    errno = 0;
    long control = syscall(SYS_mprotect, control_page, 4096, PROT_READ);
    int control_errno = errno;

    printf("invalid=%ld errno=%d control=%ld control_errno=%d\n",
           invalid, invalid_errno, control, control_errno);
    return 0;
}

Compile this as a static RV64 Linux guest and run it with Qiling 1.4.11 and the dev branch:

riscv64-linux-gnu-gcc -static -O2 -Wall -Wextra mprotect_probe.c -o probe.elf
mkdir -p rootfs
cp probe.elf rootfs/probe.elf
python3 -c 'from qiling import Qiling; Qiling(["./rootfs/probe.elf"], "./rootfs").run()'

Qiling returns success for the SIZE_MAX request. Native Linux and QEMU RV64 return -1 with errno=ENOMEM; all tested runtimes return 0 for the one-page control.

Expected behavior

The overflowing range should fail with ENOMEM. The report covers the return value for a page-aligned mapped start and SIZE_MAX length; it does not claim the state of page permissions after the rejected request.

Additional context

Reproduced with Qiling v1.4.11 on RV64 Linux. Current dev commit 23ad4fd369a360ad7992d90cb7577f0241061c2b still calls ql.mem.protect(start, mlen, prot) and returns success if that call silently rejects the invalid range; see mman.py. The current dev source was checked statically, not dynamically run for this report.

主要语言
Python
星标
6.2k
派生
806
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

qilingframework/qiling 的其他 Issue

查看 qilingframework/qiling 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。