Harden open_basedir restrictions in various extensions (or even deprecate open_basedir)
还没有人认领这个 Issue。
- 主要语言
- C
- 星标
- 40.4k
- 派生
- 8.1k
- 平均合并
- 2 天 13 小时
- 30 天内合并 PR
- 96
描述
Description
Now, several extensions in the php codebase has functions with the ability to read/write outside the open_basedir restrictions.
That is because, the open_basedir restrictions only works on PHP streams, and some of our extensions read/write without it, and therefore bypassing the open_basedir check.
Examples found while auditing similar open_basedir behavior:
ext/dba: non-stream DBA handlers such asgdbm,qdbm, orlmdbopen paths through their backend libraries, e.g.gdbm_open(),dpopen(),mdb_env_open().ext/gettext:bindtextdomain()resolves a directory and then passes it to libc/gettext, which later loads.mofiles from that location.ext/openssl: SSL context options such ascafile,capath, anddh_parammay be passed to OpenSSL APIs such asSSL_CTX_load_verify_locations()orBIO_new_file().ext/gd: FreeType font loading can pass the resolved font path toFT_New_Face()after locating it withaccess().ext/standard:stream_resolve_include_path
IMO, all of them are supposed to be fixed, that they should align with the expected open_basedir behavior. I know that the above only work with conditions (e.g. know dba keys for dpopen, or being a .mo file for bindtextdomain) but all of them should only works under the restrictions of open_basedir due to serious safety concerns. Which is simply by adding
#include "main/fopen_wrappers.h"
if (php_check_open_basedir(path)) {
RETURN_FALSE;
}
/* then call native/library open */
I don't think this requires a RFC so I would like to directly open this issue to discuss about this. cc @iluuu1994 . Thanks!
Below are some example payloads:
<?php
ini_set('open_basedir', __DIR__ . '/allowed');
$db = dba_open('/tmp/outside.gdbm', 'r', 'gdbm');
var_dump(dba_fetch('secret', $db));
and
<?php
ini_set('open_basedir', __DIR__ . '/allowed');
bindtextdomain('leak', '/tmp/outside-locale');
textdomain('leak');
echo gettext('secret_key'), "\n";
and (this can only check if a file exists, but without any restrictions)
<?php
ini_set('open_basedir', __DIR__ . '/allowed');
ini_set('include_path', '/etc');
var_dump(stream_resolve_include_path('passwd'));
var_dump(@file_get_contents('passwd', use_include_path: true));
I don't sure if we should treat this as a security issue :) This research is done with @q1uf3ng
TL;DR some of the functions use zend_resolve_path for file IO, the API doesn't check if it fits in the open_basedir restrictions.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 main/fopen_wrappers.h,并审查 ext/dba、ext/gettext、ext/openssl、ext/gd 和 ext/standard 中列出的调用点,以查找原生文件访问。使用提供的 DBA、gettext 和 stream_resolve_include_path 示例进行复现。受影响的函数始终遵守 open_basedir 限制,并且针对所演示路径具有回归测试覆盖,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, php
- 领域
- security
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 冷清
- 描述清晰度
- 需要澄清
- 新手友好度
- 35/100