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

Ouverte
#1,687 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
66/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
python

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
Python
Étoiles
6.2k
Forks
806
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

  • Fournit un Dockerfile ou un fichier Docker Compose
  • Propose un modèle de pull request
  • Aucun guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de qilingframework/qiling

Toutes les issues de qilingframework/qiling

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.