加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP无法从NFS共享读取会话

发布时间:2020-12-13 17:30:39 所属栏目:PHP教程 来源:网络整理
导读:我将我的 PHP会话文件存储在NFS共享上.问题是,PHP总是创建一个空的会话文件,但不能从中读取/写入,因此对于每个页面重新加载,都会创建一个新文件. 如果我将会话存储路径移动到本地文件夹,会话将正常保存.此外,我正在运行具有相同配置的另一个环境,它运行正常.
我将我的 PHP会话文件存储在NFS共享上.问题是,PHP总是创建一个空的会话文件,但不能从中读取/写入,因此对于每个页面重新加载,都会创建一个新文件.

如果我将会话存储路径移动到本地文件夹,会话将正常保存.此外,我正在运行具有相同配置的另一个环境,它运行正常.

在PHP遇到此问题的同一台服务器上,我能够在保存会话的相同目录中创建/写入/读取文件(使用root,非root用户,特别是www-data用户进行测试).

我在Ubuntu 12.04 LTS上使用PHP 5.5.12,Apache 2.4.9和NFS v3

我的php.ini

session.save_handler = files
session.save_path = "2;/mnt/cache/sessions"
session.use_cookies = 1
session.use_only_cookies = 0
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 2592000
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 2592000
session.bug_compat_42 = Off
session.bug_compat_warn = Off
session.referer_check =
session.entropy_length = 0
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5

我的/ etc / fstab

nfs-srv.local:/export/cache   /mnt/cache   nfs    rw,hard,intr  0  0

我的/ etc / export在nfs-srv.local上

/export/cache 10.1.10.0/24(rw,nohide,insecure,no_subtree_check,async,all_squash,anonuid=33,anongid=33)

会话文件

ls /mnt/cache/sessions/ -l
drwxrwxrwx 34 nobody nogroup 4096 May 16 10:33 0
drwxrwxrwx 34 nobody nogroup 4096 May 16 10:33 1
drwxrwxrwx 34 nobody nogroup 4096 May 16 10:33 2
...

ls /mnt/cache/sessions/m -l
drwxrwxrwx 2 nobody nogroup 4096 May 16 10:33 0
drwxrwxrwx 2 nobody nogroup 4096 May 16 12:18 1
drwxrwxrwx 2 nobody nogroup 4096 May 16 10:33 2
drwxrwxrwx 2 nobody nogroup 4096 May 16 10:33 3
drwxrwxrwx 2 nobody nogroup 4096 May 16 12:16 4
drwxrwxrwx 2 nobody nogroup 4096 May 16 12:14 5
...

ls /mnt/cache/sessions/m/5 -l
-rw------- 1 nobody nogroup 0 May 16 12:14 sess_m5ifehvhkjdisp7dgtiuu601e2

解决方法

我想我找到了这个问题的根本原因,从PHP 5.5.10升级到5.6.5时我也遇到了这个问题.

PHP 5.5.12更改日志列出了following bugfix:

When the session.save_path is a directory that everyone can write into
(like on Debian),even if it’s not possible to find the IDs of
existing sessions,a local attacker can just create a new session file
with malicious session data,chmod it to 666 and access any webapp
hosted on the system with the session ID he chose. The webapp then
opens the session file and treats it as if it had created it. My fix:
fstat() the session,check the uid that created the file. If it’s
neither the result of getuid() nor uid 0,ignore the existing file.

简而言之,如果发现新创建的会话文件不是运行Apache或root用户帐户所拥有的,则会停止编写会话数据.这是相当荒谬的,因为NFS在不同级别实现其安全性,但通常部署有远程UID / GID映射,并且通常被压缩.因此,Apache用户不再拥有该文件所创建的微秒.这意味着,从PHP 5.5.12起(或包含相同“修复”的5.4.28),在大多数库存NFS服务器上存储会话数据变得不可能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读