System calls and assembler

Similar documents
CMSC Lecture 03. UMBC, CMSC313, Richard Chang

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

The x86 Architecture

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Program Exploitation Intro

Addressing Modes on the x86

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CSC 8400: Computer Systems. Machine-Level Representation of Programs

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Practical Malware Analysis

x86 Assembly Tutorial COS 318: Fall 2017

Introduction to IA-32. Jo, Heeseung

Credits and Disclaimers

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 03, SPRING 2013

INTRODUCTION TO IA-32. Jo, Heeseung

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly Language Each statement in an assembly language program consists of four parts or fields.

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

Reverse Engineering II: The Basics

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Module 3 Instruction Set Architecture (ISA)

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Computer Architecture and Assembly Language. Practical Session 3

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

Complex Instruction Set Computer (CISC)

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

Chapter 3: Addressing Modes

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

Instruction Set Architectures

Credits and Disclaimers

Basic Execution Environment

Instruction Set Architectures

Reverse Engineering II: The Basics

LAB 5 Arithmetic Operations Simple Calculator

CSCI 192 Engineering Programming 2. Assembly Language

Assembly Language Lab # 9

Assembler Programming. Lecture 2

The Microprocessor and its Architecture

CS 16: Assembly Language Programming for the IBM PC and Compatibles

Hardware and Software Architecture. Chapter 2

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

T Reverse Engineering Malware: Static Analysis I

Lab 3. The Art of Assembly Language (II)

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Assembly Language Programming 64-bit environments

Lab 2: Introduction to Assembly Language Programming

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

CPS104 Recitation: Assembly Programming

W4118: PC Hardware and x86. Junfeng Yang

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

CS241 Computer Organization Spring 2015 IA

Instruction Set Architectures

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture Included elements of the IA-64 bit

EEM336 Microprocessors I. Addressing Modes

x86 assembly CS449 Fall 2017

IA32 Intel 32-bit Architecture

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

Introduction to Reverse Engineering. Alan Padilla, Ricardo Alanis, Stephen Ballenger, Luke Castro, Jake Rawlins

Lecture 3: Instruction Set Architecture

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Exceptions. user mode. software should run in supervisor mode Certain features/privileges il are only allowed to code running in supervisor mode

CSCE 212H, Spring 2008 Lab Assignment 3: Assembly Language Assigned: Feb. 7, Due: Feb. 14, 11:59PM

x86 architecture et similia

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Lecture 2 Assembly Language

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

SOEN228, Winter Revision 1.2 Date: October 25,

EEM336 Microprocessors I. Data Movement Instructions

CS Bootcamp x86-64 Autumn 2015

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

CS642: Computer Security

Machine/Assembler Language Putting It All Together

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Towards the Hardware"

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Advanced Microprocessors

Basic Information. CS318 Project #1. OS Bootup Process. Reboot. Bootup Mechanism

An Introduction to x86 ASM

Ethical Hacking. Assembly Language Tutorial

CSE351 Spring 2018, Midterm Exam April 27, 2018

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Intro to GNU Assembly Language on Intel Processors

COMPUTER ENGINEERING DEPARTMENT

x86 assembly CS449 Spring 2016

Transcription:

System calls and assembler Michal Sojka sojkam1@fel.cvut.cz ČVUT, FEL License: CC-BY-SA 4.0

System calls (repetition from lectures) A way for normal applications to invoke operating system (OS) kernel's services. Applications run in unprivileged CPU mode (user space, user mode) OS kernel runs in privileged CPU mode (kernel mode) System call is a way to securely switch from user to kernel mode.

What is a system call technically? A machine instruction that: Increases the CPU privilege level and Passes the control to a predefined place in the kernel. Arguments are (typically) passed in CPU registers. Instructions: x86: int 0x80, sysenter, syscall MIPS: syscall ARM: swi

x86 user execution environment (32 bit) Basic Program Execution Registers Eight 32-bit Registers Six 16-bit Registers 32-bits 32-bits FPU Registers MMX Registers Eight 80-bit Registers Eight 64-bit Registers General-Purpose Registers Segment Registers EFLAGS Register EIP (Instruction Pointer Register) Floating-Point Data Registers 16 bits Control Register 16 bits Status Register 16 bits Tag Register Opcode Register (11-bits) MMX Registers 2^32-1 48 bits FPU Instruction Pointer Register 48 bits FPU Data (Operand) Pointer Register 0 Address Space* *The address space can be flat or segmented. Using the physical address extension mechanism, a physical address space of 2^36-1 canbe addressed. General-Purpose Registers 31 16 15 8 7 AH BH CH DH BP SI DI SP AL BL CL DL 0 Segment Registers 15 0 CS 16-bit DS SS ES FS GS AX BX CX DX 32-bit EAX EBX ECX EDX EBP ESI EDI ESP XMM Registers Eight 128-bit Registers XMM Registers Program Status and Control Register 31 0 EFLAGS 31 Instruction Pointer 0 EIP 32-bits MXCSR Register Source: Intel

Linux system call ABI (x86, 32-bit) Application Binary Interface Two different interfaces: int 0x80 (older, simpler, slower) System call number in EAX /usr/include/sys/syscall.h /usr/include/asm/unistd_32.h Note: Different architectures (e.g. x86_64) use different system call numbers. Arguments 1 st in EBX, 2 nd in ECX, 3 rd in EDX, 4 th in ESI, 5 th in EDI, 6 th in EBP More arguments need to be passed in memory pointed at by a register Return value: EAX Zero or positive: success Negative: error (see /usr/include/asm-generic/errno.h, errno-base) sysenter (newer, faster, slightly more complicated)

Hello world #include <stdio.h> int main (int argc, char *argv[]) { printf( Hello world\n ); } Hello app main() printf() _start libc write() Let's look how to do it without libc Kernel write system call Documentation: man 2 write ssize_t write(int fd, const void *buf, size_t count); Three arguments _startup symbol

Hello world (taken from lectures) It's simpler in assembler hello:.ascii "Hello world\n".global _start _start: mov $4,%eax mov $1,%ebx AT&T assembler syntax: label: instruction src,dst.directive immediate operands preceded by '$' register operands preceded by '%' label/number without $ or % means reading or writing from/to the given # write # stdout memory address! mov $hello,%ecx # ptr to data mov $12,%edx # length of the data int $0x80 https://sourceware.org/binutils/docs/as/syntax.html https://sourceware.org/binutils/docs/as/i386_002dsyntax.html Compile: gcc -m32 -nostdlib -o hello1 hello1.s Run:./hello1 Why it ends with segmentation fault? Disassemble the binary: objdump -d hello1

Getting rid of the fault We need to tell the OS that we are about to finish with exit syscall. Inspect the syscalls invoked by the program: strace./hello > /dev/null hello: # initialized var.ascii "Hello world\n".global _start _start: mov $4,%eax # write mov $1,%ebx # stdout mov $hello,%ecx # ptr to data mov $12,%edx # length of the d int $0x80 mov $1,%eax mov $0,%ebx int $0x80 # exit # exit code

Assignment Write an assembler equivalent of the program from the next slide. Input: One digit 0 9 read from stdin. Output: Two digit decimal number on stdout it is the Fibonacci number corresponding to the input. You don t need to check for run-time errors. The resulting stripped binary should be smaller than 1 KiB.

fibo.c #include <unistd.h> int fibo(int n) { if (n < 2) return 1; else return fibo(n - 2) + fibo(n - 1); } int main(int argc, char *argv[]) { char n, str[3]; int val; read(0, &n, sizeof(n)); n = n - '0'; val = fibo(n); str[0] = '0' + (val / 10); str[1] = '0' + (val % 10); str[2] = '\n'; write(1, str, sizeof(str)); } return 0;

Useful instructions mov moves data between registers and memory mov $1,%eax # move 1 to register eax n:.int 123 # label n points to an integer # variable mov n,%eax # move value of the variable to eax mov %eax,%ebx # copy the value in eax to ebx push/pop stack manipulation Useful when we need to store data for later and we cannot use registers for that push %eax # push content of eax to the stack pop %ebx # pop a value from the stack to ebx

Useful instructions 2 add adds two operands add $2,%eax # eax = eax + 2 add %eax,%ebx # ebx = ebx + eax sub subtracts two operands sub $2,%eax # eax = eax 2

Useful instructions 3 call calls a subroutine ret returns from a subroutine to the caller plusone: main: add $1,%eax ret mov $12,%eax call plusone...

Useful instructions 4 div integer division (not a simple instruction) http://x86.renejeschke.de/html/file_module_x86_id_72.html 8 bit operand: ax divided by the operand result: al = ax / operand, ah = ax % operand mov $42,%ax mov $12,%bl div %bl # al = 42/12 = 3 16 bit operand: dx:ax divided by the operand result: ax = dx:ax / operand, dx = dx:ax % operand... mov $0x1,%dx mov $0x2345,%ax mov $10,%bx div %bx # ax = 0x12345 / 10

Useful instructions 5 cmp compare two values cmp $2,%eax # compare eax with 2 and set eflags register je label # jump to the label if eax was equal to 2 jl label # jump if eax was less jg label # jump if eax was greater jlelabel # jump if less or equal jge label # jump if greater or equal Example: cmp $0x30,%al jl nodigit cmp %0x39,%al jg nodigit digit:... do something... nodigit:... handle error

Makefile all: myfibo %: %.s # Disable built-in rule %.o: %.s # Assembler rule (produce 32 bit code even on 64 bit system) as --32 -g -o $@ $< %: %.o # Linker rule ld -m elf_i386 -g -o $@ $<

Troubleshooting $./myfibo 9080 segmentation fault./myfibo $ gdb myfibo (gdb) run Program received signal SIGSEGV, Segmentation fault. _start () at myfibo.s:30 30 mov 3,%eax Fix: mov $3,%eax

Other hints man ascii info as gdb: info reg https://en.wikipedia.org/wiki/x86_instruction_listings http://www.intel.com/content/dam/www/public/us/en/do cuments/manuals/64-ia-32-architectures-software-deve loper-instruction-set-reference-manual-325383.pdf Write your code in C and use objdump -d to look at the assembler instructions generated by the compiler.