Bug #79905 [NEW]: fopen() read-only streams in write mode doesn't fail

From: Date: Tue, 28 Jul 2020 11:37:04 +0000
Subject: Bug #79905 [NEW]: fopen() read-only streams in write mode doesn't fail
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228245@lists.php.net to get a copy of this message
From:             chokolatrix at gmail dot com
Operating system: WINDOWS
PHP version:      7.4.8
Package:          Filesystem function related
Bug Type:         Bug
Bug description:fopen() read-only streams in write mode doesn't fail

Description:
------------
PHP's fopen() should fail to:
- open read-only streams in write mode (STDIO in 'w' or other write
modes)
- open write-only streams in read mode (STDOUT, STDERR etc. in 'r' or
other read modes)

This was noticed because of Laravel's logging to stderr failing under
Apache/2.4.29 (Win64) mod_fcgid/2.3.9 and PHP 7.4.8 x64 NTS. 

There is possibly a bug with Apache and FastCGI exposing STDERR as
non-writable (normally writes STDERR to the error log), but Laravel
logging code checks fopen() result being a stream before writing to it.



Test script:
---------------
<?php
# c:\my-site\test.php

error_reporting(E_ALL);
ini_set('display_errors','On');

$fp = fopen('php://stdin', 'w');
var_dump($fp);

$fp = fopen('php://stderr', 'r');
var_dump($fp);

if ($fp) {
    // this then fails on Apache + FastCGI - might be an Apache + FCGI
bug causing stderr not to be writable
    fwrite($fp, "test");
} 


Expected result:
----------------
bool(false)
bool(false)


Actual result:
--------------
Notice: fwrite(): write of 4 bytes failed with errno=9 Bad file
descriptor in test.php on line 7

resource(3) of type (stream)
resource(4) of type (stream)


-- 
Edit bug report at https://bugs.php.net/bug.php?id=79905&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=79905&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=79905&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=79905&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=79905&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=79905&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=79905&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=79905&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=79905&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=79905&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=79905&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79905&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=79905&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=79905&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=79905&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=79905&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=79905&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=79905&r=mysqlcfg


Thread (4 messages)

« previous php.bugs (#228245) next »