From 008a09355b5bba7a3e65c966d824146791eb7138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sun, 10 Sep 2017 15:01:29 +0000 Subject: [PATCH] If the user tries to set kern.randompid to 1 (which is meaningless), set it to a random value between 100 and 1123, rather than 0 as before. Submitted by: Marie Helene Kvello-Aune MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D5336 --- sys/kern/kern_fork.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index 5fafc6b81671..997a783522ec 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -208,20 +208,26 @@ sysctl_kern_randompid(SYSCTL_HANDLER_ARGS) pid = randompid; error = sysctl_handle_int(oidp, &pid, 0, req); if (error == 0 && req->newptr != NULL) { - if (pid < 0 || pid > pid_max - 100) /* out of range */ - pid = pid_max - 100; - else if (pid < 2) /* NOP */ - pid = 0; - else if (pid < 100) /* Make it reasonable */ - pid = 100; - randompid = pid; + if (pid == 0) + randompid = 0; + else if (pid == 1) + /* generate a random PID modulus between 100 and 1123 */ + randompid = 100 + arc4random() % 1024; + else if (pid < 0 || pid > pid_max - 100) + /* out of range */ + randompid = pid_max - 100; + else if (pid < 100) + /* Make it reasonable */ + randompid = 100; + else + randompid = pid; } sx_xunlock(&allproc_lock); return (error); } SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW, - 0, 0, sysctl_kern_randompid, "I", "Random PID modulus"); + 0, 0, sysctl_kern_randompid, "I", "Random PID modulus. Special values: 0: disable, 1: choose random value"); static int fork_findpid(int flags)