Module Name: src
Committed By: mlelstv
Date: Sat Mar 2 08:59:47 UTC 2024
Modified Files:
src/sys/kern: sysv_shm.c
Log Message:
Avoid overflow when computing kern.ipc.shmmax. Keep shmmax (bytes) and
shmall (pages) values aligned and use arithmetic everywhere instead
of shifts.
Should fix PR 57979
To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 src/sys/kern/sysv_shm.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/kern/sysv_shm.c
diff -u src/sys/kern/sysv_shm.c:1.141 src/sys/kern/sysv_shm.c:1.142
--- src/sys/kern/sysv_shm.c:1.141 Wed Oct 9 17:47:13 2019
+++ src/sys/kern/sysv_shm.c Sat Mar 2 08:59:47 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: sysv_shm.c,v 1.141 2019/10/09 17:47:13 chs Exp $ */
+/* $NetBSD: sysv_shm.c,v 1.142 2024/03/02 08:59:47 mlelstv Exp $ */
/*-
* Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.141 2019/10/09 17:47:13 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.142 2024/03/02 08:59:47 mlelstv Exp $");
#ifdef _KERNEL_OPT
#include "opt_sysv.h"
@@ -961,10 +961,10 @@ shminit(void)
ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)));
if (shminfo.shmmax == 0)
- shminfo.shmmax = uimax(physmem / 4, 1024) * PAGE_SIZE;
+ shminfo.shmall = uimax(physmem / 4, 1024);
else
- shminfo.shmmax *= PAGE_SIZE;
- shminfo.shmall = shminfo.shmmax / PAGE_SIZE;
+ shminfo.shmall = shminfo.shmmax / PAGE_SIZE;
+ shminfo.shmmax = (uint64_t)shminfo.shmall * PAGE_SIZE;
for (i = 0; i < shminfo.shmmni; i++) {
cv_init(&shm_cv[i], "shmwait");
@@ -1083,7 +1083,7 @@ sysctl_ipc_shmmax(SYSCTLFN_ARGS)
return EINVAL;
shminfo.shmmax = round_page(newsize);
- shminfo.shmall = shminfo.shmmax >> PAGE_SHIFT;
+ shminfo.shmall = shminfo.shmmax / PAGE_SIZE;
return 0;
}