[RISC-V64/Linux] ftruncate succeeds on an O_RDONLY descriptor

Offen
#1,687 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
66/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python

Rechercherichtung

Start in the ftruncate handler in qiling/os/posix/syscall/unistd.py, which passes the host path to a path-based truncate helper. Check how the guest fd is mapped to its open file and whether its access mode is available there. Reproduce with the C probe from the issue on an RV64 rootfs. Done when ftruncate on an O_RDONLY descriptor returns EBADF and leaves the file size unchanged.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Describe the bug

On an RV64 Linux guest, ftruncate(fd, 0) succeeds when fd was opened with O_RDONLY, and the file is truncated. Linux returns EBADF for ftruncate on a descriptor that is not open for writing and leaves the file unchanged.

Sample Code

Create rootfs/scratch with the contents abc, then run this static RV64 guest:

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

int main(void)
{
    int fd = open("/scratch", O_RDONLY);
    if (fd < 0)
        return 2;

    errno = 0;
    int rc = ftruncate(fd, 0);
    int err = errno;
    struct stat st;
    if (stat("/scratch", &st) != 0)
        return 3;

    printf("rc=%d errno=%d size=%ld\n", rc, err, (long)st.st_size);
    return 0;
}
riscv64-linux-gnu-gcc -static -O2 -Wall -Wextra ftruncate_probe.c -o probe.elf
mkdir -p rootfs
printf abc > rootfs/scratch
cp probe.elf rootfs/probe.elf
python3 -c 'from qiling import Qiling; Qiling(["./rootfs/probe.elf"], "./rootfs").run()'

Linux returns -1/EBADF and leaves the size at 3. Qiling returns success and the size becomes 0.

Expected behavior

Qiling should check that the guest descriptor is open for writing before changing the file length. The current ftruncate handler passes the host path to a path-based truncate helper, which does not check the guest descriptor's access mode.

Additional context

Reproduced on Qiling v1.4.11 and the development snapshot for RV64 Linux. Current dev commit 23ad4fd369a360ad7992d90cb7577f0241061c2b still has the same ftruncate path. The current source was checked statically; full upstream CI has not been run.

Vorherrschende Sprache
Python
Sterne
6.2k
Forks
806
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Entwicklungsumgebung

  • Enthält ein Dockerfile oder eine Docker-Compose-Datei
  • Hat eine Pull-Request-Vorlage
  • Kein Beitragsleitfaden

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus qilingframework/qiling

Alle Issues in qilingframework/qiling

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.