PHP Fatal error: fread() Argument #2 must be > 0 when reading attack-data.php
-
Hello Wordfence team,
We are experiencing a fatal PHP error caused byfread()being called with a negative length within the Wordfence WAF storage file. This appears to happen during the attack data synchronization/retrieval process. We’ve tried to debug it by editing the following function on the filewp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php/**
* @param resource $fileHandle
* @param int $offset
* @param int $length
* @return bool
*/
public function _getAttackDataRowsNewerThan($fileHandle, $offset, $length) {
fseek($fileHandle, $offset);
self::lock($fileHandle, LOCK_SH);
$binaryTimestamp = fread($fileHandle, 8);
self::lock($fileHandle, LOCK_UN);
$timestamp = wfWAFAttackDataStorageFileEngine::unpackMicrotime($binaryTimestamp);
// --- ADDED IN-DEPTH DEBUG LOGGING BELOW ---
$fread_length = $length - 8;
// Get the file stream metadata to extract the file name
$stream_meta = stream_get_meta_data($fileHandle);
$filename = $stream_meta['uri'];
error_log(
"wfWAFStorageFile DEBUG: " .
"File: " . $filename . // <-- Added file name here
", Offset: " . $offset .
", Total Length: " . $length .
", Binary Timestamp Length: " . strlen($binaryTimestamp) .
", Calculated fread Length: " . $fread_length
);
// --- END OF ADDED DEBUG LOGGING STATEMENT ---
if ($timestamp > $this->attackDataNewerThan) {
$binary = $binaryTimestamp . fread($fileHandle, $fread_length);
$row = wfWAFAttackDataStorageFileEngineRow::unpack($binary);
$data = $this->unserializeRow($row->getData());
if (is_array($data)) {
array_unshift($data, $row->getTimestamp());
$this->attackDataRows[] = $data;
}
return true;
}
return false;
}The log:
[28-Oct-2025 15:56:53 UTC] wfWAFStorageFile DEBUG: File: /code/web/wp-content/wflogs/attack-data.php, Offset: 56112, Total Length: 2025, Binary Timestamp Length: 8, Calculated fread Length: 2017
[28-Oct-2025 15:56:53 UTC] wfWAFStorageFile DEBUG: File: /code/web/wp-content/wflogs/attack-data.php, Offset: 54127, Total Length: 1985, Binary Timestamp Length: 8, Calculated fread Length: 1977
[28-Oct-2025 15:56:53 UTC] wfWAFStorageFile DEBUG: File: /code/web/wp-content/wflogs/attack-data.php, Offset: 52124, Total Length: 2003, Binary Timestamp Length: 8, Calculated fread Length: 1995
[28-Oct-2025 15:56:53 UTC] wfWAFStorageFile DEBUG: File: /code/web/wp-content/wflogs/attack-data.php, Offset: 0, Total Length: 51200, Binary Timestamp Length: 8, Calculated fread Length: 51192
[28-Oct-2025 15:56:53 UTC] wfWAFStorageFile DEBUG: File: /code/web/wp-content/wflogs/attack-data.php, Offset: 0, Total Length: 0, Binary Timestamp Length: 8, Calculated fread Length: -8
[28-Oct-2025 15:56:53 UTC] PHP Fatal error: Uncaught ValueError: fread(): Argument #2 ($length) must be greater than 0 in /code/web/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php:281
Stack trace:
#0 /code/web/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php(281): fread(Resource id #5, -8)
#1 /code/web/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php(1250): wfWAFStorageFile->_getAttackDataRowsNewerThan(Resource id #5, 0, 0)
#2 /code/web/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php(246): wfWAFAttackDataStorageFileEngine->scanRowsReverse(Array)
#3 /code/web/wp-content/plugins/wordfence/lib/wordfenceClass.php(9033): wfWAFStorageFile->getNewestAttackDataArray('0.000000')
#4 /code/web/wp-includes/class-wp-hook.php(322): wordfence::syncAttackData()
#5 /code/web/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#6 /code/web/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#7 /code/web/wp-settings.php(727): do_action('init')
#8 /code/web/wp-config.php(395): require_once('/code/web/wp-se...')
#9 /code/web/wp-load.php(50): require_once('/code/web/wp-co...')
#10 /code/web/wp-blog-header.php(13): require_once('/code/web/wp-lo...')
#11 /code/web/index.php(17): require('/code/web/wp-bl...')
#12 {main}
thrown in /code/web/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php on line 281Thanks for looking into that.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘PHP Fatal error: fread() Argument #2 must be > 0 when reading attack-data.php’ is closed to new replies.