182
Linux Project 中中中中中中中 中中中中中 中中中

Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Embed Size (px)

Citation preview

Page 1: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Linux Project

中央大學資工系 碩士二年級江瑞敏

Page 2: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Outline

• How to compile linux kernel• How to add a new system call• Some Projects Example and Way to Solve it

– System Call Hooking by Module– Project about Memory– Project about Process

Page 3: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Download Link

• wget https://kernel.org/pub/linux/kernel/linux-2.6.18.tar.bz2• tar xvf linux-2.6.18.tar.bz2

Page 4: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

The Beginning of everything

Page 5: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Compile Linux Kernel

Page 6: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

It is Hard?

Page 7: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

No, If you understand the concept

Page 8: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

The Basic Process

• 0. make mrproper• 1. make oldconfig• 2. make –j[n]• 3. make modules_install• 4. make install• 5. reboot

Page 9: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Do You Know What It Means?

Page 10: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

make mrproer

• Clean up the environment• Will Remove almost everything, except….

Page 11: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

make clean

• Almost the same as make mrproper.

Page 12: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

make oldconfig

• Use the configuration file the current kernel is using.

• Some other alternative options.– Make menuconfig– …

Page 13: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Is config File Important?

Page 14: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Config file

• Determine which kind of kernel you are compiling

• Determine which modules you want the kernel to compile.

• Misconfiguration will lead to kernel crash.

Page 15: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

make –j[n]

• Compile the whole source code according to your configuration

Page 16: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

make modules_install

• Install the modules into the necessary folder.– /lib/modules/`uname –r`/

Page 17: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

make install

• Install the image into the boot directory.• Sometimes, update grub is necessary.

Page 18: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

What Is System Call

Page 19: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

It’s a Bridge

Page 20: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Between

User

Device

Device

Device

Device

Page 21: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Why System Call

Page 22: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Pop Quiz :Write A Program To Print “Hello World”

Page 23: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 24: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

What You May Write

Page 25: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

What Actually Happened ….

Page 26: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

User Application

Kernel Code

SystemCall

libc.soPrintf

Device DriverIO Device

Page 27: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

What If There Is No System Call

Page 28: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Everything Will Bex86 instruction in and out

Page 29: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Let’s Focus On …User Application

Kernel Code

SystemCall

libc.soPrintf

Device DriverIO Device

Page 30: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Magic int 0x80

Page 31: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Before We Talk Further,Let’s Talk About X86 Architecture

Page 32: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

X86 Architecture Is Interrupt Driven

Page 33: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU 8259 PIC

Kernel

Device DeviceDevice

User Application

Device

Device Driver

Page 34: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 35: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How The CPU Find The Address of The Device Driver Code

Page 36: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Callback Mechanism

Page 37: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU 8259 PIC

Device DeviceDevice Device

Device Driver

Device Driver

Device Driver

Device Driver

Interrupt Descriptor Table

…..

Kernel

Physical Device

Page 38: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How About System Call

Page 39: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Magic int 0x80

Page 40: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU 8259 PIC

Device DeviceDevice DevicePhysical Device

syscall_tableInterrupt Descriptor Table

…..

…..

0x80…..

System Call

Handler

System Call

Handler

System Call

Handler

int 0x80

Page 41: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU

Kernel

User Applicationint 0x80

csdsss

espeip…

Stack

cpu

Page 42: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU

User Applicationint 0x80

GDT

Get TSS

TSS

csdsss

espeip…

Stack

cpu

Page 43: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU

User Applicationint 0x80

GDT

Get TSS

TSS

csdsss

espeip…

Stack

cpu

Page 44: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU

User Applicationint 0x80

IDT

Get IDT

csdsss

espeip…

Stack

0x80

ENTRY(system_call)

cpu

sys_call_table

Page 45: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU

User Applicationint 0x80

IDT

Get IDT

csdsss

espeip…

Stack

0x80

ENTRY(system_call)

cpu

sys_call_table

Page 46: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

CPU

User Applicationint 0x80

IDT

Get IDT

csdsss

espeip…

ssesp

eflagscseip…

Stack

0x80

ENTRY(system_call)

sys_call_table

cpu

Page 47: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How To Add A System Call

Page 48: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Add a System Call

• 1. cd $kernel_src• 2. cd arch/i386/kernel/syscall_table.S• 3. • ….• .long sys_tee /* 315 */

.long sys_vmsplice

.long sys_move_pages

.long sys_project /* 318 */• Kernel.org/pub/linux/kernel

Page 49: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Add a System Call

• cd linux/include/asm-i386/unistd.h• #define __NR_vmsplice 316

#define __NR_move_pages 317#define __NR_project 318

#ifdef __KERNEL__

#define NR_syscalls 319

Page 50: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Add a System Call• cd linux/include/linux/syscalls.h• asmlinkage long sys_set_robust_list(struct robust_list_head __user *head,

size_t len);asmlinkage long sys_project( int i );

#endif

Page 51: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Add a System Call

• cd linux/kernel• touch project.c• Makefile• obj-y = project.o sched.o fork.o

exec_domain.o panic.o printk.o profile.o

Page 52: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Add a System Call

• Project.c• #include <linux/linkage.h>

#include <linux/kernel.h>

asmlinkage long sys_project( int i ){ printk( "Success!! -- %d\n", i ); return 0;}

Page 53: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Add a System Call

• Recompile linux kernel• Reboot• Create a new file “test.c”

• #include<syscall.h>

int main(){ syscall( 318, 2 ); return 0;}

Page 54: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Add a System call

• http://in1.csie.ncu.edu.tw/~hsufh/COURSES/FALL2007/syscall.html

Page 55: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

About 64 bits

• The Idea is the same• There are many online references • Therefore, I will not cover in this ppt.

Page 56: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

System Call Hooking by Module

Page 57: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

System Call Hooking

57

sys_call_table

正常的 execve程式碼

Usermode 程式呼叫系統呼叫 NR_execve

Page 58: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

System Call Hooking

58

sys_call_table

正常的 execve程式碼

Usermode 程式呼叫系統呼叫 NR_execve

Hooking Code

Page 59: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

System Call Hooking

59

sys_call_table

Usermode 程式呼叫系統呼叫 NR_execve

Hooking Code

正常的 execve程式碼

Modified execve

Page 60: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Source code links

• http://pastebin.com/rShUxvB5• http://pastebin.com/KEJxgLGq

Page 61: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Project about Memory

Page 62: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Level 1:Dump the virtual address of a process

Page 63: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Some Question U may Ask

Page 64: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Where to Start?

Page 65: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Maybe Add a New System Call

Page 66: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

1. How to find the process you want?

Page 67: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Process List

• task_struct• for_each_process()• If u pay attention in class, these two are not

stranger.

Page 68: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

2. How about Virtual Address that is being used by the current process?

Page 69: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

The Data Structure

• mm_struct• vm_area_structlxr.linux.no

Page 70: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How it looks like

Page 71: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

The rest is some basic programming skill

Page 72: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 73: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Too easy,Let’s make it a little bit harder

Page 74: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Level 2:Dump the physical frame that is

associate with the virtual address.

Page 75: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

New Problem, New question

Page 76: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How to transfer Virtual Address to Physical Address?

Page 77: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Some Reminder and Hints

Page 78: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Some Reminder and Hints

Page 79: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Where is CR3?

Page 80: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 81: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 82: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Now We Have CR3,Then?

Page 83: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 84: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Calculate By Yourselfor

Page 85: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Something Smarter

Page 86: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

follow_page()

Page 87: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 88: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Push Yourself More

Page 89: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Level 3:Log these information to a file

Page 90: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Ok, let’s type

Page 91: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

dmesg || grep “myproject” >> log.txt

Page 92: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

DudeAre you…

Page 93: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

…. From Kernel of course

Page 94: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Can We Do That???

Page 95: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How to write file in User Mode

Page 96: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• fd = open(filename, “w”);• write(ptr, string, strlen(string));• close(fd);

Page 97: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How about Kernel Mode

Page 98: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

open -> do_sys_open

Page 99: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Write -> sys_write()

Page 100: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Close -> sys_close()

Page 101: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Is that all?

Page 102: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

The magic __user

Page 103: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 104: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

It tell kernel that the parameter should pass from user mode

Page 105: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

It’s a protection mechanism

Page 106: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 107: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Final Step About this Project

Page 108: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Level 4:Modify The PTE r/w flagfrom read/write to read

Page 109: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

http://in1.csie.ncu.edu.tw/~hsufh/COURSES/FALL2012/linux_project1.html

Page 110: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Structures of Page Directories And Page Tables Entries

Page 111: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Wow, Looks Simple :D

Page 112: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Basic Idea

Page 113: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• 1. loop through the translation table of a process according to the virtual address.

• 2. After finding the pte, change the read/write flag

• 3. Done

Page 114: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• pte_wrprotect()

Code Implement

Page 115: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• for(loop_count = addr; loop_count < end; loop_count+=PAGE_SIZE){• pgd = pgd_offset(mm, loop_count);• if (pgd_none(*pgd)){• printk("pgd none happened\n");• continue;• }• pud = pud_offset(pgd, loop_count);• pmd = pmd_offset(pud, loop_count);• pte = pte_offset_map_lock(mm, pmd, loop_count, &ptl);• if(operation == 1){• *pte = pte_mkwrite(*pte);• } else{• *pte = pte_wrprotect(*pte);• }

Code Implement(Cont. )

Page 116: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Result

Page 117: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Result

Page 118: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

What!?

Page 119: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Use Printk to Verify

Page 120: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Printk Tell Us Two Things

Page 121: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

1. we have change the pte r/w flag

Page 122: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

2. only one entry being change back, other didn’t in most cases.

Page 123: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Magic Happened ?

Page 124: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Now,Imagine you are CPU

Page 125: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 126: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

What will happened whensome process try to access a read

only area

Page 127: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Page Fault Happened

Page 128: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

The Question Becomes,How Linux Handle Page Fault

Page 129: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

U might Ask,What is Page Fault

Page 130: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

From CPU point of view

Page 131: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

1. present flag of pgd or pte is clear.2. code running in user mode attempts to write to a read only page.– More detailed check intel programmer manual.

Page 132: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

From Kernel Point of View

Page 133: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

1. present flag is clear:• A. Access the first time.• B. Page is being swap out.

2. write to a read only page:• A. is a process really write to a read only page • B. is a page-fault optimization such as copy on write.

Page 134: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How Does Linux Kernel Determine These Kind of Difference

Page 135: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Well, First….

Page 136: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

And This

Page 137: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Then This

Page 138: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

What The FxxK…….

Page 139: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

This Time Let’s Look Closer

Page 140: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 141: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 142: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 143: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Now We Know An Important Thing

Page 144: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Linux Kernel Will Compare The vm_flag

Page 145: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Some Useful Knowledge

Page 146: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

How Linux Implement COW

Page 147: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Cow??

Moo ?

Page 148: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• 1. COW refer to copy on write• 2. google and wiki are your friend• 3. how linux implement copy on write.

– A. pte r/w flag disable– B. vm_flag & VM_WRITE == true

Page 149: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Our project accidently match the above conditions!

Page 150: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• 1. same page table entry of parent and child process point to the same pfn

• 2. set r/w flag of both pte to read only• 3. when page fault happened, page fault handler will

check the vm_flag of the current virtual address.• 4. if vm_flag has VM_WRITE, page fault handler will

refer this situation as a COW condition.• 5. assign a new pfn with r/w flag enable if there are

two pte point to it.

Page 151: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Copy on Write linux implement

parent

child

Task_struct

Task_struct

pte

ptePhysical address

Pfn N

Pfn (N+1)

Pfn (N+2)Fork(…)pgd

pgd

R/W RO

RO

Parent AccessPage Fault

VM_WRITE

VM_WRITE

_map_count=01

Child Access

R/W

Page 152: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

A New Idea of The Project

Page 153: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

1. Change PTE r/w flag as we just did

Page 154: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

2. Change the vm_flag as well

Page 155: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• down_write(&current->mm->mmap_sem);• vma = find_vma(mm, addr);• vm_start = vma->vm_start;• vm_end = vma->vm_end;• mask = VM_READ|VM_WRITE|VM_EXEC|VM_SHARED;• new_flags = VM_READ;• old_flags = vma->vm_flags;• if(old_flags&VM_WRITE){• old_flags &= ~(VM_WRITE);• new_flags |= old_flags;• } else{• new_flags |= old_flags;• } • prot = protection_map[new_flags & mask];• vma->vm_flags = new_flags;• vma->vm_page_prot = prot;• up_write(&current->mm->mmap_sem);• addr &= PAGE_MASK; • change_pte(addr, end, operation);

Code Implementation

Page 156: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Result

Page 157: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Where is the “press enter to continue” ?

Page 158: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

It’s time to use GDB

Page 159: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Set a break point before syscall happened

Seems like this time printf cause the error

Page 160: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Here is the problem.

Page 161: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Think Slowly

Page 162: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Calling printf will need to push some parameters

Page 163: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Recall From The Last Code

Page 164: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• we have changed vm_flag for the whole vm_area_struct which means the entire block of linear address.

• Address of the array is not always align to 4kb.

Page 165: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Consider The following Conditions

Page 166: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Start address alignEnd address align

Page 167: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 168: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Start Address AlignEnd Address Not Align

Page 169: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Start addr

End addr Total need 3 pages

Area problem may occur

Test_array

low

high

Page 170: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Start Address Not AlignEnd Address Align

Page 171: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Start addr

End addrTotal need 3 pages

Area problem may occur

low

high

Test array

Page 172: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Start Address Not AlignEnd Address Not Align

Page 173: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Start addr

End addr

Area problem may occur

Area problem may occur

Total need 4 pages

Test_array

low

high

Page 174: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Our case

The parameter is right hereSince the page is RO.

low

high

Assembly code:…..Call syscall;Push $string;Call printf;

R/WR only

DOOMED

Page 175: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• Rewrite the user mode program. This time use malloc instead of local variable.(Heap instead of stack)

• Char *test_array;• Test_array = (char *)malloc(ARRAY_SIZE)

Verify Our Thoughts (Test case 1)

Page 176: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Test Case 1 Result

Page 177: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 178: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• Char test1[0x2000];• Char test_array[ARRAY_SIZE];• Char test2[0x2000];• This can also bypass the conditions that I just

mentioned.

Verify Our Thoughts (Test case 2)

Page 179: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Test Case 2 Result

Also work~~

Page 180: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System
Page 181: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

• 1. basically, the idea is the same. – A. change vm_flag– B. change pte r/w flag

• 2. Some hints:– A. Strongly recommend reading Text Book

• Chapter 8: Memory Management• Chapter 9: Process Address Space

– B. code to change vma_flag is in mprotect_fixup(). – C. the code to loop through the translation table starts from

change_protection(….) -> change_pud(….) -> change_pmd(…..) -> change_pte_range(…..)

How About Mprotect.c

Page 182: Linux Project 中央大學資工系 碩士二年級 江瑞敏. Outline How to compile linux kernel How to add a new system call Some Projects Example and Way to Solve it – System

Full Source

• Level 1 and 2 :http://pastebin.com/wEVLaQyg• Level 3:http://pastebin.com/HFW8WTN5