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

From: Earl Chew
Date: Thu Aug 31 2023 - 21:51:04 EST


[ Reposting to linux-kernel@xxxxxxxxxxxxxxx ]

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