Changeset View
Changeset View
Standalone View
Standalone View
sys/kern/kern_fork.c
Show First 20 Lines • Show All 202 Lines • ▼ Show 20 Lines | sysctl_kern_randompid(SYSCTL_HANDLER_ARGS) | ||||
error = sysctl_wire_old_buffer(req, sizeof(int)); | error = sysctl_wire_old_buffer(req, sizeof(int)); | ||||
if (error != 0) | if (error != 0) | ||||
return(error); | return(error); | ||||
sx_xlock(&allproc_lock); | sx_xlock(&allproc_lock); | ||||
pid = randompid; | pid = randompid; | ||||
error = sysctl_handle_int(oidp, &pid, 0, req); | error = sysctl_handle_int(oidp, &pid, 0, req); | ||||
if (error == 0 && req->newptr != NULL) { | if (error == 0 && req->newptr != NULL) { | ||||
if (pid < 0 || pid > pid_max - 100) /* out of range */ | if (pid == 0) | ||||
pid = pid_max - 100; | randompid = 0; | ||||
else if (pid < 2) /* NOP */ | else if (pid == 1) | ||||
pid = 0; | /* generate a random PID modulus between 100 and 1123 */ | ||||
else if (pid < 100) /* Make it reasonable */ | randompid = 100 + arc4random() % 1024; | ||||
des: I'm not sure I understand this; `arc4random()` returns a `uint32_t`, so both the `abs()` and… | |||||
pid = 100; | else if (pid < 0 || pid > pid_max - 100) | ||||
/* out of range */ | |||||
randompid = pid_max - 100; | |||||
Done Inline ActionsThis is redundant. des: This is redundant. | |||||
Done Inline ActionsGood catch. Fixed, will update patch in a few. marieheleneka_gmail.com: Good catch. Fixed, will update patch in a few. | |||||
else if (pid < 100) | |||||
/* Make it reasonable */ | |||||
randompid = 100; | |||||
else | |||||
randompid = pid; | randompid = pid; | ||||
} | } | ||||
sx_xunlock(&allproc_lock); | sx_xunlock(&allproc_lock); | ||||
return (error); | return (error); | ||||
} | } | ||||
SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW, | 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 | static int | ||||
fork_findpid(int flags) | fork_findpid(int flags) | ||||
{ | { | ||||
struct proc *p; | struct proc *p; | ||||
int trypid; | int trypid; | ||||
static int pidchecked = 0; | static int pidchecked = 0; | ||||
▲ Show 20 Lines • Show All 884 Lines • Show Last 20 Lines |
I'm not sure I understand this; arc4random() returns a uint32_t, so both the abs() and the cast are unnecessary.
BTW, it is generally better to use a cross-product than modulo, since the latter gives a small bias, but then you have to cast to a wider type so the multiplication won't overflow:
pid = (uint64_t)arc4random() * (999 - 100 + 1) / UINT32_MAX + 100;