1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (C) 2010 Red Hat, Inc. 4f08c3bdfSopenharmony_ci * Copyright (C) 2022 Cyril Hrubis <chrubis@suse.cz> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * A race in pid generation that causes pids to be reused immediately 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * From the mainline commit 5fdee8c4a5e1800489ce61963208f8cc55e42ea1: 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * A program that repeatedly forks and waits is susceptible to having 15f08c3bdfSopenharmony_ci * the same pid repeated, especially when it competes with another 16f08c3bdfSopenharmony_ci * instance of the same program. This is really bad for bash 17f08c3bdfSopenharmony_ci * implementation. Furthermore, many shell scripts assume that pid 18f08c3bdfSopenharmony_ci * numbers will not be used for some length of time. 19f08c3bdfSopenharmony_ci * 20f08c3bdfSopenharmony_ci * [Race Description] 21f08c3bdfSopenharmony_ci * --------------------------------------------------------------------- 22f08c3bdfSopenharmony_ci * A B 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci * // pid == offset == n // pid == offset == n + 1 25f08c3bdfSopenharmony_ci * test_and_set_bit(offset, map->page) 26f08c3bdfSopenharmony_ci * test_and_set_bit(offset, map->page); 27f08c3bdfSopenharmony_ci * pid_ns->last_pid = pid; 28f08c3bdfSopenharmony_ci * pid_ns->last_pid = pid; 29f08c3bdfSopenharmony_ci * // pid == n + 1 is freed (wait()) 30f08c3bdfSopenharmony_ci * 31f08c3bdfSopenharmony_ci * // Next fork()... 32f08c3bdfSopenharmony_ci * last = pid_ns->last_pid; // == n 33f08c3bdfSopenharmony_ci * pid = last + 1; 34f08c3bdfSopenharmony_ci * --------------------------------------------------------------------- 35f08c3bdfSopenharmony_ci */ 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci#include <sys/types.h> 38f08c3bdfSopenharmony_ci#include <sys/stat.h> 39f08c3bdfSopenharmony_ci#include <sys/wait.h> 40f08c3bdfSopenharmony_ci#include <fcntl.h> 41f08c3bdfSopenharmony_ci#include <errno.h> 42f08c3bdfSopenharmony_ci#include <unistd.h> 43f08c3bdfSopenharmony_ci#include <stdio.h> 44f08c3bdfSopenharmony_ci#include <stdlib.h> 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ci#include "tst_test.h" 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ci#define PID_MAX 32768 49f08c3bdfSopenharmony_ci#define PID_MAX_STR "32768" 50f08c3bdfSopenharmony_ci#define RETURN 256 51f08c3bdfSopenharmony_ci#define MAX_ITERATIONS 1000000 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci/* The distance mod PIDMAX between two pids, where the first pid is 54f08c3bdfSopenharmony_ci expected to be smaller than the second. */ 55f08c3bdfSopenharmony_cistatic int pid_distance(pid_t first, pid_t second) 56f08c3bdfSopenharmony_ci{ 57f08c3bdfSopenharmony_ci return (second + PID_MAX - first) % PID_MAX; 58f08c3bdfSopenharmony_ci} 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_cistatic void check(void) 61f08c3bdfSopenharmony_ci{ 62f08c3bdfSopenharmony_ci pid_t prev_pid = 0; 63f08c3bdfSopenharmony_ci pid_t pid; 64f08c3bdfSopenharmony_ci int i, distance, reaped, status, retval; 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci for (i = 0; i < MAX_ITERATIONS; i++) { 67f08c3bdfSopenharmony_ci retval = i % RETURN; 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci pid = SAFE_FORK(); 70f08c3bdfSopenharmony_ci if (!pid) 71f08c3bdfSopenharmony_ci exit(retval); 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci if (prev_pid) { 74f08c3bdfSopenharmony_ci distance = pid_distance(prev_pid, pid); 75f08c3bdfSopenharmony_ci if (distance == 0) { 76f08c3bdfSopenharmony_ci tst_res(TFAIL, 77f08c3bdfSopenharmony_ci "Unexpected pid sequence: prev_pid=%i, pid=%i for iteration=%i", 78f08c3bdfSopenharmony_ci prev_pid, pid, i); 79f08c3bdfSopenharmony_ci return; 80f08c3bdfSopenharmony_ci } 81f08c3bdfSopenharmony_ci } 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci prev_pid = pid; 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci reaped = SAFE_WAITPID(pid, &status, 0); 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_ci if (reaped != pid) { 88f08c3bdfSopenharmony_ci tst_res(TFAIL, 89f08c3bdfSopenharmony_ci "Wrong pid %i returned from waitpid() expected %i", 90f08c3bdfSopenharmony_ci reaped, pid); 91f08c3bdfSopenharmony_ci return; 92f08c3bdfSopenharmony_ci } 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci if (WEXITSTATUS(status) != retval) { 95f08c3bdfSopenharmony_ci tst_res(TFAIL, 96f08c3bdfSopenharmony_ci "Wrong process exit value %i expected %i", 97f08c3bdfSopenharmony_ci WEXITSTATUS(status), retval); 98f08c3bdfSopenharmony_ci return; 99f08c3bdfSopenharmony_ci } 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci if (!tst_remaining_runtime()) { 102f08c3bdfSopenharmony_ci tst_res(TINFO, "Runtime exhausted, exiting..."); 103f08c3bdfSopenharmony_ci break; 104f08c3bdfSopenharmony_ci } 105f08c3bdfSopenharmony_ci } 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci tst_res(TPASS, "%i pids forked, all passed", i); 108f08c3bdfSopenharmony_ci} 109f08c3bdfSopenharmony_ci 110f08c3bdfSopenharmony_cistatic struct tst_test test = { 111f08c3bdfSopenharmony_ci .needs_root = 1, 112f08c3bdfSopenharmony_ci .forks_child = 1, 113f08c3bdfSopenharmony_ci .max_runtime = 600, 114f08c3bdfSopenharmony_ci .test_all = check, 115f08c3bdfSopenharmony_ci .save_restore = (const struct tst_path_val[]) { 116f08c3bdfSopenharmony_ci {"/proc/sys/kernel/pid_max", PID_MAX_STR, TST_SR_TBROK}, 117f08c3bdfSopenharmony_ci {} 118f08c3bdfSopenharmony_ci }, 119f08c3bdfSopenharmony_ci .tags = (const struct tst_tag[]) { 120f08c3bdfSopenharmony_ci {"linux-git", "5fdee8c4a5e1"}, 121f08c3bdfSopenharmony_ci {} 122f08c3bdfSopenharmony_ci } 123f08c3bdfSopenharmony_ci}; 124