Project

General

Profile

« Previous | Next » 

Revision 054c2541

Added by Jim Pingle about 2 years ago

Add safety belts around memory size checks. Fixes #14648

  • Add safety checks when fetching the memory size
  • Also ensure the state table size is sane if the memory check fails

View differences:

src/etc/inc/pfsense-utils.inc
1901 1901
	/* get system memory amount */
1902 1902
	$memory = get_memory();
1903 1903
	$physmem = $memory[0];
1904
	/* Be cautious and only allocate 10% of system memory to the state table */
1905
	$max_states = (int) ($physmem/10)*1000;
1904

  
1905
	if ((int) $physmem > 0) {
1906
		/* Be cautious and only allocate 10% of system memory to the state table */
1907
		$max_states = (int) ($physmem/10)*1000;
1908
	} else {
1909
		/* If the memory check result is invalid, use a low but still
1910
		 * somewhat sane default (Equivalent to ~256MB RAM) */
1911
		$max_states = 25600;
1912
	}
1913

  
1906 1914
	return $max_states;
1907 1915
}
1908 1916

  
src/etc/inc/util.inc
2439 2439
function get_memory() {
2440 2440
	$physmem = get_single_sysctl("hw.physmem");
2441 2441
	$realmem = get_single_sysctl("hw.realmem");
2442
	/* Ensure $physmem has a sane value */
2443
	if (!is_numericint($physmem) &&
2444
	    is_numericint($realmem)) {
2445
		$physmem = $realmem;
2446
	}
2447
	/* Ensure $realmem has a sane value */
2448
	if (!is_numericint($realmem) &&
2449
	    is_numericint($physmem)) {
2450
		$realmem = $physmem;
2451
	}
2452
	/* If both are invalid, something deeper is wrong */
2453
	if (!is_numericint($physmem) &&
2454
	    is_numericint($realmem)) {
2455
		/* Try checking by pages instead */
2456
		$membypages = (int) get_single_sysctl("vm.stats.vm.v_page_count") * (int) get_single_sysctl("vm.stats.vm.v_page_size");
2457
		if (is_numericint($membypages)) {
2458
			$physmem = $membypages;
2459
			$realmem = $membypages;
2460
		} else {
2461
			/* Everything failed, return zeroes */
2462
			$physmem = 0;
2463
			$realmem = 0;
2464
		}
2465
	}
2442 2466
	/* convert from bytes to megabytes */
2443
	return array(($physmem/1048576), ($realmem/1048576));
2467
	return array(((int) $physmem/1048576), ((int) $realmem/1048576));
2444 2468
}
2445 2469

  
2446 2470
function mute_kernel_msgs() {

Also available in: Unified diff