Skip to content

Commit

Permalink
BSD platforms: Reject negative and zero KERN_FSCALE factors
Browse files Browse the repository at this point in the history
The "fscale" value, retrieved by sysctl() in BSD platforms, is used for
computing CPU percentages of the processes. To prevent a division by
zero, we should reject a zero "fscale" value. (A negative "fscale"
value will not make sense either.)

For DragonFlyBSD and FreeBSD, this would fall back to the hard-coded
default scale.
For NetBSD and OpenBSD, there is no hard-coded default value, so the
zero or negative "fscale" is now a fatal error.

Signed-off-by: Kang-Che Sung <[email protected]>
  • Loading branch information
Explorer09 committed Aug 3, 2023
1 parent b3e26bc commit a1a8887
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dragonflybsd/DragonFlyBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
}

len = sizeof(this->kernelFScale);
if (sysctlbyname("kern.fscale", &this->kernelFScale, &len, NULL, 0) == -1) {
if (sysctlbyname("kern.fscale", &this->kernelFScale, &len, NULL, 0) == -1 || this->kernelFScale <= 0) {
//sane default for kernel provided CPU percentage scaling, at least on x86 machines, in case this sysctl call failed
this->kernelFScale = 2048;
}
Expand Down
2 changes: 1 addition & 1 deletion freebsd/FreeBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
}

len = sizeof(this->kernelFScale);
if (sysctlbyname("kern.fscale", &this->kernelFScale, &len, NULL, 0) == -1) {
if (sysctlbyname("kern.fscale", &this->kernelFScale, &len, NULL, 0) == -1 || this->kernelFScale <= 0) {
//sane default for kernel provided CPU percentage scaling, at least on x86 machines, in case this sysctl call failed
this->kernelFScale = 2048;
}
Expand Down
2 changes: 1 addition & 1 deletion netbsd/NetBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
NetBSDMachine_updateCPUcount(this);

size = sizeof(this->fscale);
if (sysctl(fmib, 2, &this->fscale, &size, NULL, 0) < 0) {
if (sysctl(fmib, 2, &this->fscale, &size, NULL, 0) < 0 || this->fscale <= 0) {
CRT_fatalError("fscale sysctl call failed");
}

Expand Down
2 changes: 1 addition & 1 deletion openbsd/OpenBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
OpenBSDProcessList_updateCPUcount(this);

size = sizeof(this->fscale);
if (sysctl(fmib, 2, &this->fscale, &size, NULL, 0) < 0) {
if (sysctl(fmib, 2, &this->fscale, &size, NULL, 0) < 0 || this->fscale <= 0) {
CRT_fatalError("fscale sysctl call failed");
}

Expand Down

0 comments on commit a1a8887

Please sign in to comment.