php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Uploaded files are owned by root, only after calling fileowner function correct owner is set

Open DevDavido opened this issue 4 years ago • 9 comments

Description

The following code in a relatively minimal docker compose setup of PHP + nginx for reproduction:

src/index.php:

<!DOCTYPE html>
<html>
<head><title>Upload your files</title></head>
<body>
<form enctype="multipart/form-data" action="index.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file" /><br />
    <input type="submit" value="Upload" />
</form>
<?php
error_reporting(E_ALL);
clearstatcache();

file_put_contents("lorem", "ipsum");
var_dump(fileowner("lorem"));
unlink("lorem");

if (!empty($_FILES['uploaded_file'])) {
    $path = './' . basename($_FILES['uploaded_file']['name']);

    if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
        echo "The file " . basename($_FILES['uploaded_file']['name']) . " has been uploaded";
    } else {
        echo "There was an error uploading the file, please try again!";
    }
    //var_dump(fileowner($path));
}
?>
</body>
</html>

docker-compose.yml

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
      - ./default.conf:/etc/nginx/conf.d/default.conf
    links:
      - php-fpm
  php-fpm:
    image: php:8-fpm
    volumes:
      - ./src:/var/www/html

default.conf:

server {
    index index.php;
    server_name phpfpm.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

The uid of the temporary file lorem gets printed correctly (same as php-fpm worker process uid). When uploading a file through the form and checking within the PHP container its uid/gid like docker exec -it <container-name> ls -ls the file owner/group of the uploaded file is root/root. Only after removing the comment of the second var_dump with PHP's fileowner function and uploading a file again, the filesystem returns the correct file owner/group of the uploaded file via docker exec -it <container-name> ls -ls.

Not sure if this is related to PHP or any other involved services. Although only after calling fileowner the correct uid is returned of the uploaded file.

PHP Version

8.x

Operating System

Debian 11

DevDavido avatar Dec 29 '21 03:12 DevDavido

Is this a realpath cache issue? Please call clear_stat_cache(true) to verify.

cmb69 avatar Dec 29 '21 12:12 cmb69

@cmb69 Same output / result after calling clear_stat_cache(true).

DevDavido avatar Dec 29 '21 12:12 DevDavido

Tentatively categorizing as FPM issue.

cmb69 avatar Dec 30 '21 12:12 cmb69

image

I can't reproduce this issue. For me, the permissions are correct with or without the call to fileowner. What do your permissions look like on the host system? Does it make a difference if you save the file to a folder that isn't a mounted volume?

iluuu1994 avatar Feb 03 '22 10:02 iluuu1994

The following output is without the call of fileowner.

On host system:

8 -rw-r--r-- 1 501 20 901 Feb 3 13:28 file.txt

On container system:

4 -rw-r--r-- 1 0 0 901 Feb 3 12:28 file.txt

On container system (non-mounted directory tmp):

4 -rw-r--r-- 1 33 33 901 Feb 3 12:28 file.txt

Indeed it makes a difference based on the result if the volume is mounted.

DevDavido avatar Feb 03 '22 12:02 DevDavido

image

I ran sudo chown -R $(whoami):www-data . on my root directory. Does this work for you? Are the permission of the src directory and the other non-mounted directory you tested with the same? Also, can you use ls -lah to list the directory? It's a little hard to tell what is what with the UID instead of the user name.

iluuu1994 avatar Feb 03 '22 13:02 iluuu1994

I ran sudo chown -R $(whoami):www-data . on my root directory. Does this work for you?

As I don't have a www-data group on my host (MacOS 10.15), it doesn't work. However, all files on host in that directory and its subdirectory src are owned by user devdavido and group staff.

Are the permission of the src directory and the other non-mounted directory you tested with the same?

No, I tested with the tmp directory which has different permissions (777).

Screenshot 2022-02-03

Now I created a random (non-mounted) directory /test on the container system with the same owner root, group root and permissions 755 exactly like the mounted /var/www/html and changed the upload path of index.php accordingly. Uploading a file will result in the error Warning: move_uploaded_file(/test/file.txt): Failed to open stream: Permission denied in /var/www/html/index.php on line 21 then.

Also, can you use ls -lah to list the directory? It's a little hard to tell what is what with the UID instead of the user name.

Yes, here we go: On host system:

drwxr-xr-x 4 devdavido staff 128B Feb 3 19:08 . drwxr-xr-x 5 devdavido staff 160B Feb 3 13:15 .. -rw-r--r-- 1 devdavido staff 901B Feb 3 19:08 file.txt -rw-r--r-- 1 devdavido staff 832B Feb 3 19:07 index.php

On container system:

drwxr-xr-x 4 root root 128 Feb 3 18:08 . drwxr-xr-x 3 root root 4.0K Dec 21 19:30 .. -rw-r--r-- 1 root root 901 Feb 3 18:08 file.txt -rw-r--r-- 1 101 101 832 Feb 3 18:07 index.php

On container system (non-mounted directory tmp):

drwxrwxrwt 1 root root 4.0K Feb 3 18:08 . drwxr-xr-x 1 root root 4.0K Feb 3 17:46 ..

DevDavido avatar Feb 03 '22 18:02 DevDavido

Oh, you're on macOS. I thought Operating System: Debian 11 was your host. I was always puzzled about permissions with Docker back when I used macOS. I'm fairly confident this is a Docker + macOS thing. Potentially related.

iluuu1994 avatar Feb 03 '22 18:02 iluuu1994

Thanks for pointing out the Docker osxfs driver behaviour. It indeed sounds like the underlying file system issue. I'll close this issue then.

DevDavido avatar Feb 03 '22 20:02 DevDavido