CSCE 510 - Systems Programming Lecture 17 Sockets CSCE March 20, 2013.

Slides:



Advertisements
Similar presentations
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Advertisements

Introduction to the Omega Server CSE Overview Intro to Omega Basic Unix Command Files Directories Printing C and C++ compilers GNU Debugger.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
The art of exploitation
Utilizing the GDB debugger to analyze programs Background and application.
Terminal I/O POSIX termios Two terminal I/O modes Canonical and non-canonical Getting and setting terminal attributes Terminal window size: struct winsize.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Cse322, Programming Languages and Compilers 1 6/18/2015 Lecture #16, May 24, 2007 Runtime.c Running the code debugging assembler division strings for println.
CSE 303 Lecture 13a Debugging C programs
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Daemon Processes and inetd Superserver
Memory & Storage Architecture Seoul National University Computer Architecture “ Bomb Lab Hints” 2nd semester, 2014 Modified version : The original.
C Slides and captured lecture (video and sound) are available at:
Server Design Discuss Design issues for Servers Review Server Creation in Linux.
Embedded Systems Principle of Debugger. Reference Materials kl.de/avr_projects/arm_projects/#winarmhttp://
Gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and.
Debugging Cluster Programs using symbolic debuggers.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Goals: To gain an understanding of assembly To get your hands dirty in GDB.
Fall 2012 Chapter 2: x86 Processor Architecture. Irvine, Kip R. Assembly Language for x86 Processors 6/e, Chapter Overview General Concepts IA-32.
The ISA Level The Instruction Set Architecture (ISA) is positioned between the microarchtecture level and the operating system level.  Historically, this.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Carnegie Mellon Recitation: Bomb Lab 21 Sep 2015 Monil Shah, Shelton D’Souza.
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
CNIT 127: Exploit Development Ch 3: Shellcode. Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object.
CSI605 Introduction to gdb. Compiling for gdb Execution I often compile as follows gcc -g -Wall -omyprog myprog.c g++ -g -Wall -omyprog myprog.c It suffices.
Operating Systems Process Creation
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
CSCE Systems Programming Lecture 18 Sockets, really this time CSCE March 25, 2013.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
COP 3530 Spring 12 Discussion Session 1. Agenda 1.Introduction 2.Remote programming 3.Separate code 4.Compile -- g++,makefile 5.Debug -- gdb 6.Questions?
EXPLOITATION CRASH COURSE – FALL 2013 UTD Computer Security Group – Andrew Folloder csg.utdallas.edu (credit: Scott Hand)
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 4, 2010 CSCE 212Honors Computer Organization.
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
CSCI 4061 Recitation 2 1.
DEBUG.
Instructions for test_function
Computer Architecture and Assembly Language
Assembly language.
Computer Architecture and Assembly Language
CSCE 212Honors Computer Organization
Debugging with gdb gdb is the GNU debugger on our CS machines.
Computer Architecture “Bomb Lab Hints”
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Assembly Language Programming II: C Compiler Calling Sequences
CSCE Systems Programming
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
CNT4704: Analysis of Computer Communication Network Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Fall 2011.
Lecture 6: Multiprogramming and Context Switching
Low-Level Thread Dispatching on the x86
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
CSCE 212Honors Computer Organization
Makefiles, GDB, Valgrind
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2010.
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
By Hugues Leger / Intro to GDB debugger By Hugues Leger / 11/16/2019.
Presentation transcript:

CSCE Systems Programming Lecture 17 Sockets CSCE March 20, 2013

Overview Last Time  Classes of signal handling  Job Control  Terminal input char by char (not line)  Shell2 - Program Today  Classes of signal handling  Job Control  Terminal input char by char (not line)  APUE.2e/tty_cbreak  Shell2 - Program

Shell 2 – write up in the mail tonight - CSCE Slide Sigaction/Longjmp  Due Saturday of the week after we are back from Spring break. This is March 23.  multiple pipes  character by character input for filename/command completion  Job control ? (let me think about this one.)

Signals – one more thing - CSCE Slide Advanced Shell Impl.  Signals Not queued  Real time signals are queue

Code/apue.2e/fig18.20.c - cbreak - CSCE Slide Advanced Shell Impl. … static struct termios save_termios; static int ttysavefd = -1; static enum { RESET, RAW, CBREAK } ttystate = RESET; int tty_cbreak(int fd) /* put terminal into a cbreak mode */ { int err; struct termios buf; if (ttystate != RESET) { errno = EINVAL; return(-1); } if (tcgetattr(fd, &buf) < 0) return(-1); save_termios = buf; /* structure copy */ /* Echo off, canonical mode off. */ buf.c_lflag &= ~(ECHO | ICANON); /* Case B: 1 byte at a time, no timer. */ buf.c_cc[VMIN] = 1; buf.c_cc[VTIME] = 0; if (tcsetattr(fd, TCSAFLUSH, &buf) < 0) return(-1); /* Verify that the changes stuck. */ if (tcgetattr(fd, &buf) < 0) { err = errno; tcsetattr(fd, TCSAFLUSH, &save_termios); errno = err; return(-1); } if ((buf.c_lflag & (ECHO | ICANON)) || buf.c_cc[VMIN] != 1 || buf.c_cc[VTIME] != 0) { /*Only some of the changes were made. Restore the originals*/ tcsetattr(fd, TCSAFLUSH, &save_termios); errno = EINVAL; return(-1); } ttystate = CBREAK; ttysavefd = fd; return(0); }

- CSCE Slide Advanced Shell Impl. int tty_raw(int fd) /* put terminal into a raw mode */ { int err; struct termios buf; if (ttystate != RESET) { errno = EINVAL; return(-1); } if (tcgetattr(fd, &buf) < 0) return(-1); save_termios = buf; /* structure copy */ /* * Echo off, canonical mode off, extended input * processing off, signal chars off. */ buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); /* No SIGINT on BREAK, CR-to-NL off, input parity * check off, don't strip 8th bit on input, output * flow control off. */ buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); /* Clear size bits, parity checking off. */ buf.c_cflag &= ~(CSIZE | PARENB); /* Set 8 bits/char. */ buf.c_cflag |= CS8; /* Output processing off. */ buf.c_oflag &= ~(OPOST); /*Case B: 1 byte at a time, no timer.*/ buf.c_cc[VMIN] = 1; buf.c_cc[VTIME] = 0; if (tcsetattr(fd, TCSAFLUSH, &buf) < 0) return(-1); /* * Verify that the changes stuck. * tcsetattr can return 0 on * partial success. */ if (tcgetattr(fd, &buf) < 0) {

- CSCE Slide Advanced Shell Impl. int tty_raw(int fd) /* put terminal into a raw mode */ { int err; struct termios buf; if (ttystate != RESET) { errno = EINVAL; return(-1); } if (tcgetattr(fd, &buf) < 0) return(-1); save_termios = buf; /* structure copy */ /* * Echo off, canonical mode off, extended input * processing off, signal chars off. */ buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); /* No SIGINT on BREAK, CR-to-NL off, input parity * check off, don't strip 8th bit on input, output * flow control off. */ buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); /* Clear size bits, parity checking off. */ buf.c_cflag &= ~(CSIZE | PARENB); /* Set 8 bits/char. */ buf.c_cflag |= CS8; /* Output processing off. */ buf.c_oflag &= ~(OPOST); /*Case B: 1 byte at a time, no timer.*/ buf.c_cc[VMIN] = 1; buf.c_cc[VTIME] = 0; if (tcsetattr(fd, TCSAFLUSH, &buf) < 0) return(-1); /* * Verify that the changes stuck. * tcsetattr can return 0 on * partial success. */ if (tcgetattr(fd, &buf) < 0) {

- CSCE Slide Advanced Shell Impl. int tty_reset(int fd) /* restore terminal's mode */ { if (ttystate == RESET) return(0); if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0) return(-1); ttystate = RESET; return(0); } void tty_atexit(void) /* can be set up by atexit(tty_atexit) */ { if (ttysavefd >= 0) tty_reset(ttysavefd); } struct termios * tty_termios(void) /* let caller see original tty state */ { return(&save_termios); } main(){ char buf[4096]; int i; int numread; printf("Normal mode = reset\n"); numread=read(0, buf, 1024); printf("Numread=%d, buf[0]=%d, buf[0]=%c\n", numread, buf[0], buf[0]); tty_cbreak(0); printf("Entered cbreak mode\n"); for(i=0; i <10; ++i){ numread=read(0, buf, 1024); printf("Numread=%d, buf[0]=%d, buf[0]=%c\n", numread, buf[0], buf[0]); } tty_reset(0); tty_raw(0); printf("Entered raw mode\n"); numread=read(0, buf, 1024); printf("Numread=%d, buf[0]=%d, buf[0]=%c\n", numread, buf[0], buf[0]); tty_reset(0); printf("Resetting mode\n");

Pipes, - CSCE Advanced Shell Implementation  pipe(pfd[2])  mkfifo(1) - Create named pipes (FIFOs) with the given NAMEs  mkfifo mypipe  ls –l  ls –lrt > mypipe &  wc < mypipe  SIGPIPE – one side block and the other side processes all die

Links, symbolic links revisited - CSCE Slide Advanced Shell Impl.  ln tree.c hardlink  ls –i tree.c hardlink  ln pathFarAway lname  … Invalid cross-device link  ln –s pathFarAway lname  ls –l  readlink lname

Gnu Debugger (GDB) Overview Gnu (Gnu is not Unix) Free Software Foundation Richard Stallman – emacs fame Debuggers Allows one to execute a program step by step and observe the changes made during execution Dbx – Berkeley debugger GDB – gnu debugger Compile with the “-g” option  gcc –g source.c

GDB QUICK REFERENCE Essential Commands Starting GDB Stop GDB Getting Help Executing your Program  run arglist Shell Commands  Cd, pwd, make, etc Breakpoints and Watchpoints  b line-number Program Stack  bt (backtrace, i.e. showstack) Execution Control Display Automatic Display Expressions Symbol Table GDB Scripts Signals Debugging Targets Controlling GDB Working Files Source Files GDB under Emacs

GDB - Essential Commands gdb program [core] debug program [using coredump core] b func [file:]function set breakpoint at function [in file] run [arglist] start your program [with arglist] backtrace: display program stack p expr display the value of an expression c continue running your program n next line, stepping over function calls s next line, stepping into function calls sinext assembly instruction info reg/args/locals

GDB - Display print [/f ] [expr] p [/f ] [expr] show value of expr [or last value $] according to format f: call [/f ] expr like print but does not display void

GDB – Display Formats x hexadecimal d signed decimal u unsigned decimal o octal t binary a address, absolute and relative c character f floating point (char *) null terminated string

GDB - Automatic Display Automatic Display display /f exprshow the value of expr each time program stops according to format f Formats – x d u t (binary) c f s (string) i (machine instr) Examples display /i $eip - show the machine instruction everytime gdb stops display /x numshow the value of the variable “num” …

GDB – eXamining Memory x [/Nuf ] expr examine memory at address expr;  Optional format spec follows slash Format N count of how many units to display u unit size; one of  b individual bytes  h halfwords (two bytes)  w words (four bytes)  g giant words (eight bytes)

GDB Example - fibonnaci #include int fib(int n){ int nm1, nm2; int temp, temp2; printf("Calling fib(%d)\n", n); nm1 = n-1; nm2 = n-2; if (n <= 1) return 1; else{ temp = fib(nm1); temp2 = fib(nm2); return temp + temp2; } /* fibonnaci function written with lots of extra variables */ main(){ int i,j; i = 4; j = i+1; i = fib(j); }

Compiling and Running GDB ares> gcc -g fib.c -o fib ares> gdb fib GNU gdb 6.3 Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i486-slackware-linux"...Using host libthread_db library "/lib/libthread_db.so.1". (gdb)

GDB: Run, List gdb > run Starting program: /class/csce /Examples/Lab04/fib Calling fib(5) Calling fib(4) Calling fib(3) Calling fib(2) Calling fib(1) Calling fib(0) Calling fib(1) Calling fib(2) Calling fib(1) Calling fib(0) Calling fib(3) Calling fib(2) Calling fib(1) Calling fib(0) Calling fib(1) Program exited with code 010. (gdb) list 10, /* fibonnaci function written with lots of extra variables */ 12 int fib(int n){ 13 int nm1, nm2; 14 int temp, temp2; 15 printf("Calling fib(%d)\n", n); 16 nm1 = n-1; 17 nm2 = n-2; 18 if (n <= 1) return 1; 19 else{ 20 temp = fib(nm1);

Setting Breakpoints and N, S, C commands Setting breakpoint – b command (gdb) b 17 Breakpoint 1 at 0x80483d5: file fib.c, line 17. Running the program (gdb) run Starting program: /class/csce /Examples/Lab04/fib Calling fib(5) Breakpoint 1, fib (n=5) at fib.c:17 Next command or just n (gdb) n 18 if (n <= 1) return 1; Continue command (gdb) c Continuing. Calling fib(4) Breakpoint 1, fib (n=4) at fib.c:17 17 nm2 = n-2; (gdb) c Continuing. Calling fib(3) Breakpoint 1, fib (n=3) at fib.c:17 17 nm2 = n-2; (gdb) c

Backtrace – Show Activation Records Backtrace (bt) show the stack of activation records (gdb) bt #0 fib (n=2) at fib.c:10 #1 0x080483e7 in fib (n=3) at fib.c:10 #2 0x080483e7 in fib (n=4) at fib.c:10 #3 0x080483e7 in fib (n=5) at fib.c:10 #4 0x in main () at fib.c:24 Up // move up one Activation Record in the stack (gdb) up #1 0x in fib (n=2) at … (gdb) up #2 0x080483f8 in fib (n=3) at … p expr // Print the value of expr (gdb) p nm2 $3 = 1 (gdb) p nm1 $4 = 2 (gdb)

What’s in the registers? (gdb) info reg eax 0x0 0 ecx 0x0 0 edx 0xf 15 ebx 0xb7fcdff esp 0xbffff510 0xbffff510 ebp 0xbffff528 0xbffff528 esi 0xbffff63c edi 0x1 1 eip 0x80483dc 0x80483dc eflags 0x cs 0x ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x0 0 Note ebp – the frame pointer Esp – the stack pointer Eip – the program counter

A look at the stack using X (examine) (gdb) x/30wx 0xbffff528 // the frame pointer (%ebp) 0xbffff528: 0xbffff558 0x080483e7 0x x xbffff538: 0xbffff63c 0x xbffff558 0xb7ee88f0 0xbffff548: 0xb7fce7a0 0x x x xbffff558: 0xbffff588 0x080483e7 0x x xbffff568: 0xbffff63c 0x xbffff588 0xb7ee88f0 0xbffff578: 0xb7fce7a0 0x x x xbffff588: 0xbffff5b8 0x080483e7 0x x xbffff598: 0xbffff5a8 0xb7eb90d5 ebp’s: 0xbffff528, 0xbffff558, 0xbffff588, 0xbffff5b8 Return addresses ebp+4 Argument1’s ebp+8

Daemons - CSCE Slide Advanced Shell Impl.  A daemon is a process that  is long-lived; runs from system start-up till shut- down  runs in background with no controlling terminal  no signals from terminal  typically a “server”  Examples  cron  sshd  httpd  inetd – listens for incoming connections

- CSCE Slide Advanced Shell Impl.  CRON(8) CRON(8)  NAME  cron - daemon to execute scheduled commands (Vixie Cron)  SYNOPSIS  cron [-f] [-l] [-L loglevel]  DESCRIPTION  cron is started automatically from /etc/init.d on entering multi-user runlevels.  /etc/con.d file

Creating a daemon - CSCE Slide Advanced Shell Impl. 1. fork  parent exits  child lives on (adopted by ______)  Why  child continues on in background  child not process-group leader 2. child calls setsid – start new session –free from terminal 3. if daemon opens terminal later it might reaquire a controlling terminal O_NOCTTY on open 4. Clear process umask 5. change cwd typically to root “/” Why? 6. close all open file descriptors 7. open /dev/null, dup2(fd, 0,1,2)

TLPI/daemon - CSCE Slide Advanced Shell Impl.  ls  become_daemon.c  daemon_SIGHUP.c  test_become_daemon.c  become_daemon.h  Makefile  t_syslog.c

TLPI/become_daemon.c - CSCE Slide Advanced Shell Impl. int /* Returns 0 on success, -1 on error */ becomeDaemon(int flags) { int maxfd, fd; switch (fork()) { /* Become background process */ case -1: return -1; case 0: break; /* Child falls through... */ default: _exit(EXIT_SUCCESS); /* while parent terminates */ } if (setsid() == -1) /* Become leader of new session */ return -1; switch (fork()) { /* Ensure we are not session leader */ case -1: return -1; case 0: break; default: _exit(EXIT_SUCCESS); } if (!(flags & BD_NO_UMASK0)) umask(0); /* Clear file mode creation mask */ if (!(flags & BD_NO_CHDIR)) chdir("/"); /* Change to root directory */ if (!(flags & BD_NO_CLOSE_FILES)) { /* Close all open files */ maxfd = sysconf(_SC_OPEN_MAX); if (maxfd == -1) /* Limit is indeterminate... */ maxfd = BD_MAX_CLOSE; /* so take a guess */ for (fd = 0; fd < maxfd; fd++) close(fd); } if (!(flags & BD_NO_REOPEN_STD_FDS)) { close(STDIN_FILENO); /* Reopen standard fd's to /dev/null */ fd = open("/dev/null", O_RDWR ); if (fd != STDIN_FILENO) /* 'fd' should be 0 */ return -1; if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO) return -1; if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) return -1; }

Web Server overview - CSCE Slide Advanced Shell Impl.

Sockets - CSCE Slide Advanced Shell Impl.  TCP/IP  RCFs  internet/history-internet/brief-history-internet

TLPI/sockets - CSCE Slide Advanced Shell Impl.  ls ares> ls *.c i6d_ucase_cl.c is_echo_v2_sv.c scm_cred_recv.c ud_ucase_cl.c i6d_ucase_sv.c is_seqnum_cl.c scm_cred_send.c ud_ucase_sv.c id_echo_cl.c is_seqnum_sv.c scm_rights_recv.c unix_sockets.c id_echo_sv.c is_seqnum_v2_cl.c scm_rights_send.c us_abstract_bind.c inet_sockets.c is_seqnum_v2_sv.c sendfile.c us_xfr_cl.c is_echo_cl.c rdwrn.c socknames.c us_xfr_sv.c is_echo_inetd_sv.c read_line_buf.c t_gethostbyname.c us_xfr_v2_cl.c is_echo_sv.c read_line.c t_getservbyname.c us_xfr_v2_sv.c