Skip to content

Infinite loop from redox-rt in OpenSSH

This code is extracted from OpenSSH

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  // Required for close(), sysconf()
#include <errno.h>   // Required for errno

/*
 * Close all file descriptors greater than or equal to lowfd.
 */
static void
closefrom_fallback(int lowfd)
{
    long fd, maxfd;

    maxfd = sysconf(_SC_OPEN_MAX);
    printf("Closing into fd %d\n", (int)maxfd);
    for (fd = lowfd; fd < maxfd; fd++)
        (void) close((int) fd);
}

int main() {
    // Close all potentially open file descriptors above stderr (2).
    printf("Closing file descriptors from %d upwards...\n", STDERR_FILENO + 1);
    closefrom_fallback(STDERR_FILENO + 1);
    printf("Done\n");
    return 0;
}

In Linux this executes fine and exit cleanly

Closing file descriptors from 3 upwards...
Closing into fd 1024
Done

In Redox it creates an infinite loop in stderr

$ gcc -o tes tes.c
$ ./tes > a
RELIBC PANIC: panicked at redox-rt/src/sys.rs:325:6:
failed to call proc mgr with Exit: Bad file number
RELIBC PANIC: panicked at redox-rt/src/sys.rs:325:6:
failed to call proc mgr with Exit: Bad file number
RELIBC PANIC: panicked at redox-rt/src/sys.rs:325:6:
failed to call proc mgr with Exit: Bad file number

Despite it can execute normally

$ cat a
Closing file descriptors from 3 upwards...
Closing into fd 1024
Done
Edited by Wildan Mubarok