Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <CADFzTtdU2h6pG3B+bdvUjFfDy=GxJ-poC18QpjaRinJchayNAA@mail.gmail.com>
Date: Tue, 19 Jan 2021 10:18:04 -0800
From: Rasmus Andersson <rasmus@...s.me>
To: musl@...ts.openwall.com
Subject: waitpid (wait4) on Linux 5 returns invalid values

Hello!
I'm having an issue with musl (at git master.) It appears as the
waitpid[1] implementation assumes that the syscall returns values
matching the waitpid specification, but it does not! This causes some
programs to hang.

Runit's "runsv" program is one example. It does something like this:

for (;;) {
  child = waitpid(-1, &wstat, WNOHANG);
  if (!child) break;
  if ((child == -1) && (errno != EINTR)) break;
  if (child == svd[0].pid) {
    // do things with child
  }
}

When I inspect a hung runsv process with strace I find it calling
wait4 at full speed, stuck in this loop. wait4 comes from calling the
waitpid function which in musl performs the wait4 syscall and returns
the value.

The waitpid spec[2] says its return value is either the PID, -1 with
errno set to EINTR or 0 in WNOHANG mode. So, the expected returns
values are: >0, 0, -1.

However the wait4 syscall[3] in Linux 5 returns other values,
specifically it returns errors as negative values. The error that
trips up programs like runit's runsv is ECHILD (-10) which wait4
returns when there are no children (i.e. they have exited.)

I propose that you change the waitpid implementation to handle this.
Something like this:

pid_t waitpid(pid_t pid, int *status, int options)
{
  pid_t r = syscall_cp(SYS_wait4, pid, status, options, 0);
  if (r < 0) {
    errno = -r;
    r = -1;
  }
  return r;
}

-- Rasmus

[1] waitpid in musl:
http://git.musl-libc.org/cgit/musl/tree/src/process/waitpid.c
[2] waitpid spec:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html
[2] waitpid in glibc: https://man7.org/linux/man-pages/man2/waitpid.2.html
[3] wait4 syscall implementation in Linux 5.10.1: kernel/exit.c:1638
and kernel/exit.c:1579 (online:
https://elixir.bootlin.com/linux/v5.10.1/source/kernel/exit.c#L1579)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.