[PATCH 0/3] tty: Fix tiocspgrp() related races

Earl Chew posted 3 patches 2 years, 3 months ago
There is a newer version of this series
drivers/tty/tty_jobctrl.c | 44 ++++++++++++++++++++++++++++-----------
1 file changed, 32 insertions(+), 12 deletions(-)
[PATCH 0/3] tty: Fix tiocspgrp() related races
Posted by Earl Chew 2 years, 3 months ago
[ Reposting to linux-kernel@vger.kernel.org ]

This set of patches improves tiocspgrp() use cases, and was
motivated by noticing rare job control SIGTTIN/SIGTTOU races.
Over time, users probably have encountered these races during
interactive use, but quite possibly have simply worked through
them.

The first patch addresses a race that occurs when a job control
shell runs a fg command to move a background job to the foreground.
The shell issues tcsetpgrp() followed by killpg(SIGCONT). In
rare cases this coincides with the new foreground job receiving
a SIGTTIN which gives the appearance that fg stops the job.
The first reproducer below can detect this case:

    while ./first ; do : ; done
    WIFSTOPPED 21

The second patch addresses a race that occurs when a job
control application issues several tcsetpgrp() calls in
parallel. In rare cases, the callers will only partially serialise.
After the first successfully sets the foreground process group,
the remaining should receive SIGTTOU because they are now
background processes that are attempting to change the
foreground process group. Instead, they successfully reconfigure
the foreground process group of the controlling terminal.
The second reproducer below can detect this case:

    while ./second ; do : ; done
    Died at line 86: Exited 2 Stopped 0

The third patch relocates the sampling of task_pgrp() in
__tty_check_change_locked() to place it inside the locked
region, immediately before the value is used. This improves
the consistency of the sampled value, and follows the example
set by __proc_set_tty().

/* FIRST RACE */
    #include <errno.h>
    #include <fcntl.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <time.h>
    #include <unistd.h>

    #include <sys/wait.h>

    #define DIE(...) die(__LINE__, __VA_ARGS__)

    void die(unsigned lineno, const char *fmt, ...)
    {
        va_list argp;

        va_start(argp, fmt);
        vfprintf(stderr, fmt, argp);
        fputc('\n', stderr);
        exit(1);
    }

    void delay(unsigned range, unsigned scale)
    {
        unsigned rnd = random() % range;

        struct timespec delay = {
            .tv_nsec = rnd * scale
        };

        nanosleep(&delay, 0);
    }

    int main()
    {
        const char *ttyname = ctermid(0);
        if (!ttyname) DIE("ctermid");

        int ttyfd = open(ttyname, O_RDONLY | O_NONBLOCK);
        if (-1 == ttyfd) DIE(ttyname);

        typedef void (*sighandler_t)(int);

        sighandler_t sighandler = signal(SIGINT, SIG_DFL);
        if (SIG_ERR == sighandler) DIE("signal");

        sigset_t sigmask;
        if (sigprocmask(SIG_SETMASK, 0, &sigmask)) DIE("sigprocmask");
        if (sigismember(&sigmask, SIGTTIN)) DIE("SIGTTIN");

        pid_t child = fork();

        if (!child) {

            srandom(getpid());

            if (setpgid(0, 0)) DIE("setpgid");

            delay(10000, 1);

            char buf[1];
            int rd = read(ttyfd, buf, 0);
            if (rd) DIE("read");

        } else {

            srandom(getpid());

            if (setpgid(child, child)) DIE("setpgid");

            delay(10000, 1);

            if (tcsetpgrp(ttyfd, child)) DIE("tcsetpgrp");

            if (killpg(child, SIGCONT)) DIE("killpg");

            int status;
            pid_t waited = waitpid(child, &status, WUNTRACED);
            if (child != waited) DIE("waitpid");

            kill(child, SIGKILL);

            if (WIFSTOPPED(status)) DIE("WIFSTOPPED %d", WSTOPSIG(status));
            if (!WIFEXITED(status)) DIE("WEXITED");
            if (WEXITSTATUS(status)) DIE("WEXITSTATUS");
        }

        return 0;
    }
/* FIRST RACE */

/* SECOND RACE */
    #include <errno.h>
    #include <fcntl.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>

    #include <sys/wait.h>

    #define DIE(...) die(__LINE__, __VA_ARGS__)

    void die(unsigned lineno, const char *fmt, ...)
    {
        va_list argp;

        fprintf(stderr, "Died at line %u", lineno);
        if (fmt) {
            fprintf(stderr, ": ");
            va_start(argp, fmt);
            vfprintf(stderr, fmt, argp);
        }
        fputc('\n', stderr);
        exit(1);
    }

    int main()
    {
        const char *ttyname = ctermid(0);
        if (!ttyname) DIE(0);

        int ttyfd = open(ttyname, O_RDONLY | O_NONBLOCK);
        if (-1 == ttyfd) DIE(ttyname);

        sigset_t sigmask;
        if (sigprocmask(SIG_SETMASK, 0, &sigmask)) DIE(0);
        if (sigismember(&sigmask, SIGTTOU)) DIE(0);

        pid_t parentPid = getpid();
        pid_t pgid[2] = { };
        pid_t child[2] = { };

        for (int ix = 0; ix < 2; ++ix) {
            pgid[ix] = fork();
            if (!pgid[ix]) {
                if (setpgid(0, 0)) DIE(0);
                exit(0);
            }
            if (setpgid(pgid[ix], pgid[ix])) DIE(0);

            child[ix] = fork();
            if (!child[ix]) {
                if (kill(getpid(), SIGSTOP)) DIE(0);
                printf("Child %d foreground %d\n", getpid(), pgid[ix]);
                if (tcsetpgrp(ttyfd, pgid[ix])) DIE(0);
                exit(0);
            }

            if (-1 == child[ix]) DIE(0);
            if (child[ix] != waitpid(child[ix], 0, WUNTRACED)) DIE(0);
        }

        if (SIG_ERR == signal(SIGTTOU, SIG_IGN)) DIE(0);
        if (kill(-parentPid, SIGCONT)) DIE(0);

        int status;
        int stopped = 0;
        int exited = 0;

        for (int ix = 0; ix < 2; ++ix) {
            pid_t waited = waitpid(child[ix], &status, WUNTRACED);
            if (waited != child[ix]) DIE("Child %d", child[ix]);

            if (WIFEXITED(status))
                ++exited;
            if (WIFSTOPPED(status))
                ++stopped;
        }

        for (int ix = 0; ix < 2; ++ix) {
            if (kill(pgid[ix], SIGKILL) && ESRCH != errno) DIE(0);
            if (kill(child[ix], SIGKILL) && ESRCH != errno) DIE(0);
        }

        if (2 == exited)
            DIE("Exited %d Stopped %d", exited, stopped);

        return 0;
    }
/* SECOND RACE */

Earl Chew (3):
  tty: Fix __tty_check_change() and tiocspgrp() race
  tty: Serialise racing tiocspgrp() callers
  tty: Move task_pgrp() after tty->ctrl.lock for consistency

 drivers/tty/tty_jobctrl.c | 44 ++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 12 deletions(-)

-- 
2.39.1
[PATCH v2 0/3] tty: Fix tiocspgrp() related races
Posted by Earl Chew 2 years, 2 months ago
This is a resubmission to correct errors and warnings
called out by the kernel test robot.

Earl Chew (3):
  tty: Fix __tty_check_change() and tiocspgrp() race
  tty: Serialise racing tiocspgrp() callers
  tty: Move task_pgrp() after tty->ctrl.lock for consistency

 drivers/tty/tty_jobctrl.c | 47 ++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 13 deletions(-)

-- 
2.39.1
Re: [PATCH v2 0/3] tty: Fix tiocspgrp() related races
Posted by Greg KH 2 years, 2 months ago
On Thu, Sep 28, 2023 at 06:06:56AM -0700, Earl Chew wrote:
> This is a resubmission to correct errors and warnings
> called out by the kernel test robot.
> 
> Earl Chew (3):
>   tty: Fix __tty_check_change() and tiocspgrp() race
>   tty: Serialise racing tiocspgrp() callers
>   tty: Move task_pgrp() after tty->ctrl.lock for consistency
> 
>  drivers/tty/tty_jobctrl.c | 47 ++++++++++++++++++++++++++++-----------
>  1 file changed, 34 insertions(+), 13 deletions(-)
> 
> -- 
> 2.39.1

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- This looks like a new version of a previously submitted patch, but you
  did not list below the --- line any changes from the previous version.
  Please read the section entitled "The canonical patch format" in the
  kernel file, Documentation/process/submitting-patches.rst for what
  needs to be done here to properly describe this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot