exercise 2: process-, memory- and io-management · code, associated data and a process control...

25
Chair for System Security RUHR-UNIVERSITY BOCHUM Exercises Operating System Security WS2008/09 Exercise 2: Process-, Memory- and IO-Management Introduction In order to manage the system resources of a computer the control structure of an op- erating system (OS) is based on four entities: memory, processes, files and I/O. For managing these entities the OS constructs and maintains tables of information about each of them. In the following we provide an introduction, which should help to solve the tasks given below. The most requirements an OS has to fulfill are related to processes. Hence the manage- ment of processes is the fundamental task of OSs. The OS is responsible for scheduling and dispatching processes for execution by the processor, allocating resources to pro- cesses and responding to requests of user processes. The aim of process management is the efficient management of programs that are executed at the same time. Since a processor can only execute one process at a time, this is achieved by switching the CPU between multiple processes that are interrupted and resumed according to their priority. A process is definded as a ’program in execution’ or in other words ’a unit of activity characterized by the execution of a sequence of instruction, a current state, and an associ- ated set of system resources’. There are three elements characterizing a process: program code, associated data and a process control block (PCB). The process control block is a data structure storing information that allows to uniquely characterize a process. This includes a unique identifier, state, priority, a program counter, memory pointers, context data, I/O status information and accounting information. The data structure of PCBs is the key tool that enables the OS to provide multiprocessing, because it covers all nec- essary information to interrupt a running process and later resume execution as if the interruption did not occurr. Since the information in a PCB is critical for the security of an OS, they must be kept in an area of memory that is protected from normal user access. However, usually each process has its own memory address space that is protected from the access of other processes. Between processes exists a parent/child relationship. A process can have multiple child processes but points to only one parent process. The very first process, called init in UNIX, is started by the kernel at boot time and never terminates. As the creation of a process is usually caused by the user or an application, it can also be useful to let one process (parent process) create another (child process) that runs in parallel. This action is referred to as process spawning. Here, communication and cooperation of processes become important. Enabling processes to share and exchange information, protecting resources of each process from other processes and enabling syn- 1

Upload: others

Post on 14-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

Exercise 2: Process-, Memory- andIO-Management

Introduction

In order to manage the system resources of a computer the control structure of an op-erating system (OS) is based on four entities: memory, processes, files and I/O. Formanaging these entities the OS constructs and maintains tables of information abouteach of them. In the following we provide an introduction, which should help to solvethe tasks given below.

The most requirements an OS has to fulfill are related to processes. Hence the manage-ment of processes is the fundamental task of OSs. The OS is responsible for schedulingand dispatching processes for execution by the processor, allocating resources to pro-cesses and responding to requests of user processes. The aim of process managementis the efficient management of programs that are executed at the same time. Since aprocessor can only execute one process at a time, this is achieved by switching the CPUbetween multiple processes that are interrupted and resumed according to their priority.

A process is definded as a ’program in execution’ or in other words ’a unit of activitycharacterized by the execution of a sequence of instruction, a current state, and an associ-ated set of system resources’. There are three elements characterizing a process: programcode, associated data and a process control block (PCB). The process control block is adata structure storing information that allows to uniquely characterize a process. Thisincludes a unique identifier, state, priority, a program counter, memory pointers, contextdata, I/O status information and accounting information. The data structure of PCBsis the key tool that enables the OS to provide multiprocessing, because it covers all nec-essary information to interrupt a running process and later resume execution as if theinterruption did not occurr. Since the information in a PCB is critical for the security ofan OS, they must be kept in an area of memory that is protected from normal user access.

However, usually each process has its own memory address space that is protectedfrom the access of other processes. Between processes exists a parent/child relationship.A process can have multiple child processes but points to only one parent process. Thevery first process, called init in UNIX, is started by the kernel at boot time and neverterminates.

As the creation of a process is usually caused by the user or an application, it canalso be useful to let one process (parent process) create another (child process) that runsin parallel. This action is referred to as process spawning. Here, communication andcooperation of processes become important. Enabling processes to share and exchangeinformation, protecting resources of each process from other processes and enabling syn-

1

Page 2: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

chronization among processes represent difficult, security-related activities.

Many OS support threads as a further concept for managing task execution. By intro-ducing threads, processes become more complex as presented so far. In general, a processcontains at least one but may also contain multiple threads, that can be executed inde-pendently but share the process’ resources. Operating systems which support multiplethreads within a single process are referred to as multithreaded environments. In thesesystems the processor does not only switch between different processes, but also betweendifferent threads. Processes are typically independent, have separate address spaces (atleast if no explicit methods are used, which require the intervention of the kernel) andonly interact through system-provided inter-process communication mechanisms (IPC).Instead, threads within the same process share memory and files and can communicatewith each other without invoking the kernel. Thus, threads are able to access the samedatastructures without the need of further IPC. But the main advantage over processesis the better performance of threads. Creation and termination of threads takes less timeand also switching between two threads within the same process. Thus, the concept ofthreads is not only beneficial for multiprocessor systems. This comes with the cost ofmore complex code1 which is much more difficult to debug and hence more vulnerableto race-conditions and other security-related bugs.

0.1. Brief Introduction into the Python Programming Language

As the practical assignment requires coding work in python, this subsection aims to give avery short introducing into the necessary parts of Python. (For a complete reference, seethe Python language reference [3].) Wikipedia [1] describes pythons design philosophyas: it “emphasizes programmer productivity and code readability. Python’s core syntaxand semantics are minimalistic, while the standard library is large and comprehensive”and as the syntax is minimalistic, it should be easy enough to learn even if you don’t haveany experiences before. For a more complete introduction into the python programminglanguage, see the Python tutorial [2].

0.1.1. Python-syntax

Preamble Every python file should let the system know which interpreter should beused to execute this script. While on Windows (and DOS), this is traditionally done bythe file extension, on UNIX systems it is done by the first row of the file. If it contains a“she-bang” (the combination #!) followed by a path to the interpreter and is executable,the operating system will try to start the interpreter with the script as a parameter.A typical python preamble would be:

1 #!/ usr / b in /python

1a programmer has to care about locking/unlocking of each part another thread may access

2

Page 3: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

Statements: Python code usually has one statement per line. A statement may befollowed by a semicolon, but a simple newline is sufficient, so the semicolon may (and inmost cases is) omitted.

Comments: In Python, everything in a line following a # is treated as a comment.Thus, the preamble in the last paragraph is interpreted as a comment by the interpreter,too. As described before, it is not ignored by the system.Example:

1 #!/ usr / b in /python23 #t h i s i s a comment45 print ”huhu” # t h i s i s a comment too , but not the p r i n t !

print: The built in command print can be used, to print something to the standard-output (stdout) followed by a newline. It is used in the previous section, thus the scriptin the previous section would print out “huhu” onto the standard-console.

formatted print: Python’s print offers functionality similar to printf. The formatstring is followed by a %-character and a tuple of parameters. An example shouldillustrate this:

1 #!/ usr / b in /python23 temperature = 104 c o n d i t i o n s = ” f i n e ”56 print ”Weather today : i t i s %d degree s c e l s i u s , the c o n d i t i o n s

are %s ” % ( temperature , c o n d i t i o n s )

import: The import statement is used to include other “modules”, comparable to li-brary. Functions of the module can be accessed after importing by the <modulename>.-prefix. Library-functions important for the assignments are described in section 0.1.2.An example for import is this:

1 #!/ usr / b in /python23 import sys #t h i s imports the ‘ ‘ sys ’ ’−module45 sys . s tdout . wr i t e ( ”huhu\n” )6 #does the same as the p r i n t s ta tement above

3

Page 4: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

Variables and Data Types: Though python is a strong typed language, it uses dynamictyping. Variables must not be declared, but can directly be assigned and take the typeof the assignment. Assignments are done by the = operator. Example:

1 #!/ usr / b in /python23 var = ”have you mooed today ?” #t h i s i s a s t r i n g o b j e c t4 var2 = 42 #in t e g e r o b j e c t5 var3 = Exception ( ) #new ob j e c t o f type ”Except ion ”6 var2 = var #var2 i s now a s t r i n g ob j e c t , too

Operators on Variables: In python, every data type is a class, thus every variable (andeven static values) represents an object. The operators are defined by special functionsof that object, for example: var = 1 + 2 is the same as var = 1. add (2). As youcan see, the meaning of the operator can be different depending on the object type youuse it with. For standard number objects, this works as expected, for string-types (andarrays, tuples, etc) the + operator does concatenation. You can redefine the meaning ofthe operator (“operator overloading”) by creating a new class for example with anotheradd function. This could be useful if you want to implement matrix classes and +

should be the matrix addition, * should be the matrixmultiplication and / should be thematrix inversion.

It is obvious that the types of the classes must be compatible. It does not make senseto add a string to an integer. However, sometimes you want to concatenate the stringrepresentation of an integer to a string. This isn’t possible directly (as the object typesaren’t compatible), the non-string object must be converted to its string representationfirst. This can be done by the str function. Consider this example:

1 #!/ usr / b in /python23 var = ”have you mooed today ?\nyes , ”4 var2 = ” times ”5 var3 = 567 print var + var3 . s t r ( ) + var28 print var + 7 . s t r ( ) + var2

Standard Classes:

Integer An integer number.

Float A floating-point number.

String The purpose of string is obvious. It can be accessed as string or as an array ofcharacters. Single characters can be accessed with st[5], substrings with st[5:8].

4

Page 5: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

Tuple A tuple is a set of objects (components), bound to a single name. The set isenclosed by parenthesis and its components are separated by comma. The compo-nents can be accessed by brackets.Example:

1 #!/ usr / b in /python23 i = 54 s t = ” the lazy dog jumps over the . . . ”5 p i = 3.141594767 tup l e = ( i , st , p i )89 print tup l e [ 2 ] #pr i n t s out p i

Once assigned, it is not longer possible to change components. This is a majorrestriction but gives a serious performance-boost.

Array An array is a set of objects (components) bound to a single name. The set isenclosed by brackets and its components are separated by comma. Componentscan be accessed like tuple-components.Example:

1 #!/ usr / b in /python23 i = 54 s t = ” the lazy dog jumps over the . . . ”5 p i = 3.14159476 roundpi = 378 array = [ i , st , p i ]9

10 print array [ 2 ]11 array [ 2 ] = roundpi12 print array [ 2 ]

Dictionary A dictionary is a set of key-value pairs. It manages relations between keyand value of a set of objects (components). The set is defined in the format:key: value,... and is enclosed by brackets. Components are accessed by theirdesignated key.Example:

#!/ usr / bin /python

d i c t i o n a r y = {” p i ” : 3 . 1415947 , ” s t ” : ” the lazy dog jumpsover the . . . ” , ” i ” :5}

5

Page 6: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

pr in t d i c t i o n a r y [ ” p i ” ]

Defining and Calling Functions: Functions are defined by two parts: a signature anda function block. The signature consists of the def keyword, followed by a bracket, a listof parameter names (possible with default values), a bracket and a colon. It is followedby the function block which is a set of statements. A function is called by function name,argument list separated by commas. If no arguments are given, the brackets are stillrequired!

Blocks: Unlike most other languages, python does not use parentheses to define blockstarts and block ends. Blocks are entirely defined by indention2, thus indention is veryimportant in python. Consider the following example:

1 #!/ usr / b in /python23 parameter = ” oink , oink ”45 def func ( parameter , parameter2 = ” t e s t ” ) :6 print parameter27 print parameter89 func ( ” opera t ing system s e c u r i t y rocks ! ” )

It gives the output:

t e s topera t ing system s e c u r i t y rocks !

While this:

1 #!/ usr / b in /python23 parameter = ” oink , oink ”45 def func ( parameter , parameter2 = ” t e s t ” ) :6 print parameter27 print parameter #note t ha t t h i s s ta tement i s not l onger

indented .89 func ( ” opera t ing system s e c u r i t y rocks ! ” )

gives:

2usual coding-convention defines 4 spaces for indention

6

Page 7: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

t e s toink , oink

Returning Values from Functions: A function can return an object. This is done bythe return-statement. After the return-statement, the program-flow leaves the functionand returns to the calling statement. Example:

1 #!/ usr / b in /python23 def func ( ) :4 print ” i am a func t i on ! ”5 return ”Have you mooed today ?”67 x = func ( )8 print x

gives

i am a func t i on !Have you mooed today ?

Conditional Code Sometimes you need conditional codes. The most important ex-ample of conditional code in python is the if statement, possibly followed by an elif(shorthand for else if) and/or an else-statement. The exact syntax is best shown in anexample:

1 #!/ usr / b in /python23 x = 145 i f x == 1 :6 print ” f i r s t b lock : ”7 print ”x = 1”8 e l i f x > 1 :9 print ” second block : ”

10 print ”x > 1”11 else :12 print ” t h i rd block : ”13 print ”x < 1”

Loops: Python has two different types of loops:

7

Page 8: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

for-in-loop: The for in loop is a loop that iterates through objects. Important iterableobjects are strings, arrays, tuples and dictionaries. An example of a for in loop is:

1 #!/ usr / b in /python23 x = ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ) # tup l e wi th 10 numbers45 for i in x : # i t e r a t e s over every component o f the t u p l e . . .6 print ” loop ing . . . ”7 print i

while-loop: The Python while loop is a classical while-loop. It works as the following:

1 #!/ usr / b in /python23 x = 10045 while x > 0 :6 print x7 print x −= 1 # equa l to x = x − 1

range([start], stop, [step]), xrange([start], stop, [range]): The range/xrange-functionis a built in function, that generates iterable objects containing integer-ranges of num-bers. Calling is quite obvious. Start is the start value (the first number in the range) (0 ifomitted), stop (the first number not in the range) is the stop value (cannot be omitted),step defines the interval between the numbers (1 if omitted, must be an integer value).The difference between range and xrange is that range returns a tuple, while xrange re-turns an iterable function. Thus, if you do range(10000000000), your tuple takes quitesome time to generate and uses large amounts of memory, while xrange(10000000000)just returns an iterable object. While this is different, both works equivalent in for-loops.Example:

1 #!/ usr / b in /python23 x = range (0 , 4 , 2 )4 for i in x :5 print i

gives

02

8

Page 9: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

0.1.2. Python Modules

This part introduces some additional functions that should enable you to complete yourassignment. Additional informations of these functions are given in the appendix. Acomplete documentation of the standard library of modules is the Python library refer-ence [4].

os: The os-module provides the interface to the operating-system. It provides wrappersto syscalls (like fork) and other advanced operating system functions (like directoryworking).

os.fork: This is the python-implementation of the fork syscall. It creates a new processclone and runs it independently. It returns 0 to the child process, and the pid of thechild to the parent process. It is called via the os. prefix of the module and the nameof the function + brackets (i.e. os.fork()). It takes no arguments!

os.wait: This is the python implementation of the wait syscall. It waits for a childprocess to exit and retrieves it’s exit status. It takes no arguments!

os.getpid: This function retrieves the current process-id of the process calling from thesystem. Example:

1 #!/ usr / b in /python2 import os34 pid = os . ge tp id ( )5 print pid

os.system: - system(param-string): The system call starts the default shell and letsit execute the parameter-string. An example should make this clear:

1 #!/ usr / b in /python23 import os45 os . system ( ” echo t h i s i s a t e s t > f i l e ” )6 os . system ( ” cat f i l e ” )7 os . system ( ”rm f i l e ” )

It does the following: First creates a “file” named “file” and echos “this is a test” in it,then prints out the contents of the “file” using the cat command and finally removesthe “file” with the rm command.

time: The time module provides an interface to all time-related functions of the system.For instance it converts different time formats.

9

Page 10: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

time.sleep: This function is the only function of the time module relevant for thisassignment. The signature is: sleep(secs). It stops the program flow for secs seconds.

0.1.3. How to Write and Execute Python Scripts

Python scripts can either be written in any texteditor or directly tested in the pythonshell.

Write scripts with a text-editor: Open your favorite texteditor (for example kate [5])and write the script. Make sure not to forget the preamble 0.1.1. After saving the script,you must mark it executable by executing chmod +x <filename>. Now you can executeit with ./<filename> in the directory you saved the script.

Test in the Python-shell: Type python on the shell. It should give you something like:

Python 2 . 5 . 2 ( r252 :60911 , Oct 7 2008 , 1 2 : 4 5 : 4 9 )[GCC 4 . 3 . 1 ] on l inux2Type ” help ” , ” copyr ight ” , ” c r e d i t s ” or ” l i c e n s e ” f o r morein fo rmat ion .>>>

You can type your statements into the console. Single statements are evaluated aftertyping enter, blocks are evaluated after finishing the statement. You can exit the shellwith the exit() statement. Example:

immo@wok ˜ $ pythonPython 2 . 5 . 2 ( r252 :60911 , Oct 7 2008 , 1 2 : 4 5 : 4 9 )[GCC 4 . 3 . 1 ] on l inux2Type ” help ” , ” copyr ight ” , ” c r e d i t s ” or ” l i c e n s e ” f o r morein fo rmat ion .>>> f o r i in range (1 , 5 ) : #range−f unc t i on r e tu rn s tup l e

( 1 , 2 , 3 , 4 , 5 ). . . p r i n t i. . .1234>>> e x i t ( )immo@wok ˜ $

10

Page 11: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

1. Theoretical assignments:

1.1. Python-Questions ( 1π

Points)

You should be able to answer all of these questions before starting the practical exer-cises. Use to disambiguate the indention!

1. How do you define a tuple?

2. Whats the difference between an array and a dictionary?

3. Implement (on paper) a small Python-script, that prints out your name!

4. Implement (on paper) a small Python-script, that counts from 0 to 10. Use a for-loop, conditional code and the range-function! The output should be the following:

0 i s 0 mod 3123 i s 0 mod 3456 i s 0 mod 3789 i s 0 mod 310

5. You have an integer-variable r, containing a random number. Give two possibleprint-statements to print “our random number is -insert content of the randomnumber here-”

1.2. Process-Management (1/2 Point)

1. What is a process?

2. Why does someone want multiple processes? Give examples!

3. What information about processes are necessary for the OS to manage them?

4. What is the difference between processes and threads?

5. Consider the following python-code:

1 #!/ usr / b in /python23 import os , time

11

Page 12: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

4 n=556 for i in xrange (n) :7 os . f o rk ( )89 time . s l e e p (100)

a) How many processes will sleep in line 9?

b) How many processes will sleep in line 9 if n would be 10?

c) Develop a formula for calculating the number of processes for all n ∈ N.Explain the formula!

1.3. Memory Management (1 Point)

1. What is swapping and what is its purpose?

2. Explain the paging-mechanism.

3. Explain why it is a security-problem if some user has read-access to the swapfile!

4. Why can it also be a problem if some user has write-access?

5. Why do OS use virtual addressing?

6. Describe the process-memory-layout on a general x86-system.

7. Where is the stack and what is the stack used for?

8. What is the heap, what does the heap?

9. Consider the following source-code:

1 #include <s t r i n g . h>2 #include <s t d i n t . h>3 #include <s t d l i b . h>45 int func (char∗ s , u i n t 1 6 t i ) {6 return ∗ s + i ;7 }89 int main (void ) {

10 char f oo [ 1 0 ] , ∗bar = mal loc (15) ;11 u i n t 1 6 t foobar ;12 u i n t 3 2 t blah ;1314

12

Page 13: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

15 s t r cpy ( foo , ” 01234567 ” ) ;16 foobar = 65532;17 blah = func ( foo , foobar ) ;1819 /∗ THIS IS THE INTERESTING POSITION ∗/2021 return 0 ;22 }

Let the start-address of the stack of the virtual memory for the process be 0x000f0000.Assume that it is an IA-32 (x86)-architecture and sizeof(void*) is 4 (whichmeans that the size of a pointer is 4 bytes). Assume further the variables are pushedon the stack in the same order as they are declared and assume no padding!3

a) In which direction does the stack grow?

b) Where points bar? What is the address of the first byte of bar (==&bar)?

c) How does the address of foo change in strcpy?

d) What is the address of the first byte of blah (== &blah)?

10. On Linux, if a process tries to access memory it does not belong to it, the kernelsends signal 11 - SIGSEGV and the access fails. The standard behaviour on receiv-ing SIGSEGV is to print out “segmentation fault” and exit the process. Explainwhy this is done and what effects it would have if it have not been implemented.

1.4. I/O Management (π−22π

Points)

1. Distinguish the tree techniques for performing I/O from each other: ProgrammedI/O, Interrupt-driven I/O and DMA.

2. Why is I/O buffering important? Mention the different buffering-schemes that aresupported by OSs and point out their differences.

3. What is the difference between block-oriented devices and stream-oriented devices?Give 1 examples of each.

2. Practical Assignments (∑

1 Point)

2.1. Linux

2.1.1. Process Management

1. Read the process table, find out the process id of a running python shell.

3Note that usual compiler will do padding and possible reorganize, so calculate the solution, do not tryit out!

13

Page 14: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

2. Write a basic python-script that creates a child-process. Both processes (the parentand the child) shall print out its process-id. The parent-process shall wait for itschild to exit and then print something extra.

3. Check whether this program really starts a new process using the ps4 command.

4. Now let the child do some work! Copy the big buck bunny Video from the server/insert/the/path/here to other/path/here. This can be done by using kon-queror (doubleclick the link on the desktop). The Username is labor and the pass-word labor1. Let the child execute mencoder to recode the video “big buck bunny.mkv”to the h264 codec 5. This task requires a lot of computations. Check that the par-ent process is still usable while the child does lots of work. Explain why it isn’tblocked!

5. Change the script, so that one “global” variable is used in both processes, alterthis variable in one process! Has it changed in the other? explain Why/why-not!

2.1.2. Memory Management

1. Use python to place some string in the memory. (start the python shell and assigna string to some variable)

2. Find out the memory-address using hex(id(str)).

3. Use the hexeditor hexedit to find it in /dev/mem. With the “return”-key, youcan jump to the address. Do you find the expected string? Explain!

2.2. Windows

2.2.1. Process Management

1. Run the Process Explorer utility on the desktop. (Process Explorer is a WindowsSysinternals Tool free for download.)

2. Find out which processes are currently running on your system. (No notes needed.)

3. Now click on the System Information button to see details of CPU activity. Howmany processes and how many threads are running?

4. Start multiple notepad sessions in the different ways listed below. Use the ProcessExplorer to observe the differences. Are there differences with respect to thepriviledges?

• Start\All Programs\Accessories\Notepad

• start notepad using Command Prompt

4prints out the current process-list, for further information see man ps5os.system("mencoder -ovc x264 -o -oac pcm -x264encopts bitrate=1000 big buck bunny.h264

big buck bunny.mkv")

14

Page 15: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

• run Command Prompt as administrator (right click Command Promp on thestart menu and select ”run as administrator”) and execute

s ch ta sk s / Create /SC einmal /ST 10:41 /TN Test/TR notepad

(Replace the time 10:41 with the time of your system adding two or threeminutes.)

5. How does the number of threads and the assigned virtual space changes, duringentering some code, saving or opening a notepad document? (Just note the generaltrends.)

6. Start notepad as administrator and try to open C:\Test\security-log.txt.

7. Create a new file ”malware.bat” in C:\Test using notepad and insert the followingcode.

@ECHO OFFC:CD \TESTECHO 1 >> s e cu r i t y−l og . txtPAUSE

8. Open Command Prompt as administrator and check if the size of the file security-log.txt changes by entering:

at 10 :41 ”C:\ Test\malware . bat ”

(Don’t forget to replace the time!)

9. Why does this work? What does this mean for the security of Windows Vista?

2.3. Bonus assignment (+1 Point)

We gathered the contents of a machines memory using DMA and firewire. There wasno time to check on the machine directly, so the only thing we have is the memory-dump. Analyse the memory dump of this machine provided in Desktop/ExerciseData/Exercise 2/memory.dump.

1. Which operating system was this machine running?

2. Search for the processes that have been running at the time when the image wasmade.

3. Reconstruct the users and their passwords.

15

Page 16: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

Appendix

The following information is not mandatory for the assignment. It is meant as back-ground knowledge, if you are interested in one particular detail or have difficulties un-derstand the things above. Many of the details included in the appendix are not com-prehensible given only the information given in the lecture and in the introducion.

A. Syscalls

This is the (complete) documentation of selected syscalls for the linux operating system.Since it is real world documentation, it is quite not easy to understand.

A.1. fork - create a child process

description: fork creates a new process by duplicating the calling process. The newprocess, referred to as the child, is an exact duplicate of the calling process, referred toas the parent, except for the following points:

• The child has its own unique process ID, and this PID does not match the ID ofany existing process group (see man setpgid).

• The child’s parent process ID is the same as the parent’s process ID.

• The child does not inherit its parent’s memory locks (mlock, mlockall).

• Process resource utilizations (getrusage) and CPU time counters (times) are resetto zero in the child.

• The child’s set of pending signals is initially empty

• The child does not inherit semaphore adjustments from its parent

• The child does not inherit record locks from its parent

• The child does not inherit timers from its parent

• The child does not inherit outstanding asynchronous I/O operations from its parent

The process attributes in the preceding list are all specified in POSIX.1-2001. Theparent and child also differ with respect to the following Linux-specific process attributes:

• The child does not inherit directory change notifications (dnotify) from its parent(see the description of F NOTIFY in the man-page of fcntl).

• The prctl PR SET PDEATHSIG setting is reset so that the child does not receive asignal when its parent terminates.

• Memory mappings that have been marked with the madvise MADV DONTFORK flagare not inherited across a fork.

16

Page 17: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

• The termination signal of the child is always SIGCHLD (see the manpage of clone).

Note the following further points:

• The child process is created with a single thread – the one that called fork.The entire virtual address space of the parent is replicated in the child, includ-ing the states of mutexes, condition variables, and other pthreads objects; the useof pthread atfork may be helpful for dealing with problems that this can cause.

• The child inherits copies of the parent’s set of open file descriptors. Each filedescriptor in the child refers to the same open file description (see man open) asthe corresponding file descriptor in the parent. This means that the two descriptorsshare open file status flags, current file offset, and signal-driven I/O attributes (seethe description of F SETOWN and F SETSIG in man fcntl).

• The child inherits copies of the parent’s set of open message queue descriptors (seeman mq overview). Each descriptor in the child refers to the same open messagequeue description as the corresponding descriptor in the parent. This means thatthe two descriptors share the same flags (mq flags).

• The child inherits copies of the parent’s set of open directory streams (see manopendir). POSIX.1-2001 says that the corresponding directory streams in theparent and child may share the directory stream positioning; on Linux/glibc theydo not.

return value: On success, the PID of the child process is returned in the parent, and0 is returned in the child. On failure, -1 is returned in the parent, no child process iscreated, and errno is set appropriately.

errors:

EAGAIN fork cannot allocate sufficient memory to copy the parent’s page tables andallocate a task structure for the child.

EAGAIN It was not possible to create a new process because the caller’s RLIMIT NPROCresource limit was encountered. To exceed this limit, the process must have eitherthe CAP SYS ADMIN or the CAP SYS RESOURCE capability.

ENOMEM fork failed to allocate the necessary kernel structures because memory istight.

conforming to: SVr4, 4.3BSD, POSIX.1-2001.

17

Page 18: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

notes: Under Linux, fork is implemented using copy-on-write pages, so the only penaltythat it incurs is the time and memory required to duplicate the parent’s page tables,and to create a unique task structure for the child.

Since version 2.3.3, rather than invoking the kernel’s fork system call, the glibc forkwrapper that is provided as part of the NPTL threading implementation invokes clonewith flags that provide the same effect as the traditional system call. The glibc wrapperinvokes any fork handlers that have been established using pthread atfork.

A.2. exit - terminate the calling process

description: The function exit terminates the calling process ”immediately”. Anyopen file descriptors belonging to the process are closed; any children of the process areinherited by process 1, init, and the process’s parent is sent a SIGCHLD signal.

The value status is returned to the parent process as the process’s exit status, andcan be collected using one of the wait family of calls.

return value: This function does not return.

conforming to: SVr4, POSIX.1-2001, 4.3BSD.

notes: For a discussion on the effects of an exit, the transmission of exit status, zombieprocesses, signals sent, etc., see the manpage of exit.

The function exit is like exit, but does not call any functions registered with atexitor on exit. Whether it flushes standard I/O buffers and removes temporary files createdwith tmpfile is implementation-dependent. On the other hand, exit does close openfile descriptors, and this may cause an unknown delay, waiting for pending output tofinish. If the delay is undesired, it may be useful to call functions like tcflush beforecalling exit. Whether any pending I/O is canceled, and which pending I/O may becanceled upon exit() is implementation-dependent.

A.3. wait, waitpid, waitid - wait for process to change state

description: All of these system calls are used to wait for state changes in a child ofthe calling process, and obtain information about the child whose state has changed. Astate change is considered to be: the child terminated; the child was stopped by a signal;or the child was resumed by a signal. In the case of a terminated child, performing await allows the system to release the resources associated with the child; if a wait is notperformed, then the terminated child remains in a “zombie” state (see NOTES below).

18

Page 19: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

If a child has already changed state, then these calls return immediately. Otherwisethey block until either a child changes state or a signal handler interrupts the call (as-suming that system calls are not automatically restarted using the SA RESTART flag ofsigaction). In the remainder of this page, a child whose state has changed and whichhas not yet been waited upon by one of these system calls is termed waitable.

wait and waitpid The wait system call suspends execution of the calling process untilone of its children terminates. The call wait(&status); is equivalent to:

waitp id (−1 , &status , 0) ;

The waitpid system call suspends execution of the calling process until a child speci-fied by pid argument has changed state. By default, waitpid waits only for terminatedchildren, but this behavior is modifiable via the options argument, as described below.

The value of pid can be:

< -1 meaning wait for any child process whose process group ID is equal to the absolutevalue of pid.

-1 meaning wait for any child process.

0 meaning wait for any child process whose process group ID is equal to that of thecalling process.

> 0 meaning wait for the child whose process ID is equal to the value of pid.

The value of options is an OR of zero or more of the following constants:

WNOHANG return immediately if no child has exited.

WUNTRACED also return if a child has stopped (but not traced via ptrace). Status fortraced children which have stopped is provided even if this option is not specified.

WCONTINUED (since Linux 2.6.10) also return if a stopped child has been resumedby delivery of SIGCONT.

(For Linux-only options, see below.)

The WUNTRACED and WCONTINUED options are only effective if the SA NOCLDSTOP flaghas not been set for the SIGCHLD signal (see sigaction).

19

Page 20: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

If status is not NULL, wait and waitpid store status information in the int to which itpoints. This integer can be inspected with the following macros (which take the integeritself as an argument, not a pointer to it, as is done in wait and waitpid!):

WIFEXITED(status) returns true if the child terminated normally, that is, by callingexit or exit, or by returning from main().

WEXITSTATUS(status) returns the exit status of the child. This consists of the leastsignificant 8 bits of the status argument that the child specified in a call to exitor exit or as the argument for a return statement in main(). This macro shouldonly be employed if WIFEXITED returned true.

WIFSIGNALED(status) returns true if the child process was terminated by a signal.

WTERMSIG(status) returns the number of the signal that caused the child process toterminate. This macro should only be employed if WIFSIGNALED returned true.

WCOREDUMP(status) returns true if the child produced a core dump. This macroshould only be employed if WIFSIGNALED returned true. This macro is not specifiedin POSIX.1-2001 and is not available on some Unix implementations (e.g., AIX,SunOS). Only use this enclosed in #ifdef WCOREDUMP ... #endif.

WIFSTOPPED(status) returns true if the child process was stopped by delivery of asignal; this is only possible if the call was done using WUNTRACED or when the childis being traced (see manpage of ptrace).

WSTOPSIG(status) returns the number of the signal which caused the child to stop.This macro should only be employed if WIFSTOPPED returned true.

WIFCONTINUED(status) (since Linux 2.6.10) returns true if the child process wasresumed by delivery of SIGCONT.

The waitid() system call (available since Linux 2.6.9) provides more precise control overwhich child state changes to wait for.

The idtype and id arguments select the child(ren) to wait for, as follows:

idtype == P PID Wait for the child whose process ID matches id.

idtype == P PGID Wait for any child whose process group ID matches id.

idtype == P ALL Wait for any child; id is ignored.

The child state changes to wait for are specified by ORing one or more of the followingflags in options:

20

Page 21: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

WEXITED Wait for children that have terminated.

WSTOPPED Wait for children that have been stopped by delivery of a signal.

WCONTINUED Wait for (previously stopped) children that have been resumed bydelivery of SIGCONT.

The following flags may additionally be ORed in options:

WNOHANG As for waitpid.

WNOWAIT Leave the child in a waitable state; a later wait call can be used to againretrieve the child status information.

Upon successful return, waitid fills in the following fields of the siginfo t structurepointed to by infop:

si pid The process ID of the child.

si uid The real user ID of the child. (This field is not set on most other implementations.)

si signo Always set to SIGCHLD.

si status Either the exit status of the child, as given to exit (or exit), or the signalthat caused the child to terminate, stop, or continue. The si code field can beused to determine how to interpret this field.

si code Set to one of: CLD EXITED (child called exit); CLD KILLED (child killed bysignal); CLD STOPPED (child stopped by signal); or CLD CONTINUED (child continuedby SIGCONT).

If WNOHANG was specified in options and there were no children in a waitable state, thenwaitid returns 0 immediately and the state of the siginfo t structure pointed to byinfop is unspecified. To distinguish this case from that where a child was in a waitablestate, zero out the si pid field before the call and check for a non-zero value in this fieldafter the call returns.

return value:

wait on success, returns the process ID of the terminated child; on error, -1 is returned.

waitpid on success, returns the process ID of the child whose state has changed; ifWNOHANG was specified and one or more child(ren) specified by pid exist, but havenot yet changed state, then 0 is returned. On error, -1 is returned.

waitid returns 0 on success or if WNOHANG was specified and no child(ren) specified by idhas yet changed state; on error, -1 is returned.

Each of these calls sets errno to an appropriate value in the case of an error.

21

Page 22: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

errors:

ECHILD (for wait) The calling process does not have any unwaited-for children.

ECHILD (for waitpid or waitid) The process specified by pid (waitpid) or idtype andid (waitid) does not exist or is not a child of the calling process. (This can happenfor one’s own child if the action for SIGCHLD is set to SIG IGN. See also the LinuxNotes section about threads.)

EINTR WNOHANG was not set and an unblocked signal or a SIGCHLD was caught.

EINVAL The options argument was invalid.

conforming to: SVr4, 4.3BSD, POSIX.1-2001.

notes: A child that terminates, but has not been waited for becomes a “zombie”. Thekernel maintains a minimal set of information about the zombie process (PID, termina-tion status, resource usage information) in order to allow the parent to later perform await to obtain information about the child. As long as a zombie is not removed fromthe system via a wait, it will consume a slot in the kernel process table, and if this tablefills, it will not be possible to create further pro- cesses. If a parent process terminates,then its “zombie” children (if any) are adopted by init, which automatically performsa wait to remove the zombies.

POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG IGN or theSA NOCLDWAIT flag is set for SIGCHLD (see manpage of sigaction), then children thatterminate do not become zombies and a call to wait or waitpid will block until allchildren have terminated, and then fail with errno set to ECHILD. (The original POSIXstandard left the behavior of setting SIGCHLD to SIG IGN unspecified. Note that eventhough the default disposition of SIGCHLD is ”ignore”, explicitly setting the dispositionto SIG IGN results in different treatment of zombie process children.) Linux 2.6 conformsto this specification. However, Linux 2.4 (and earlier) does not: if a wait or waitpidcall is made while SIGCHLD is being ignored, the call behaves just as though SIGCHLDwere not being ignored, that is, the call blocks until the next child terminates and thenreturns the process ID and status of that child.

linux notes: In the Linux kernel, a kernel-scheduled thread is not a distinct constructfrom a process. Instead, a thread is simply a process that is created using the Linux-unique clone system call; other routines such as the portable pthread create call areimplemented using clone. Before Linux 2.4, a thread was just a special case of a process,and as a consequence one thread could not wait on the children of another thread, evenwhen the latter belongs to the same thread group. However, POSIX prescribes suchfunctionality, and since Linux 2.4 a thread can, and by default will, wait on children ofother threads in the same thread group.

22

Page 23: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

The following Linux-specific options are for use with children created using clone;they can- not be used with waitid:

WCLONE Wait for “clone” children only. If omitted then wait for “non-clone” chil-dren only. (A ”clone” child is one which delivers no signal, or a signal other thanSIGCHLD to its parent upon termination.) This option is ignored if WALL isalso specified.

WALL (since Linux 2.4) Wait for all children, regardless of type (“clone” or “non-clone”).

WNOTHREAD (since Linux 2.4) Do not wait for children of other threads in thesame thread group. This was the default before Linux 2.4.

example: The following program demonstrates the use of fork and waitpid. The pro-gram creates a child process. If no command-line argument is supplied to the program,then the child suspends its execution using pause, to allow the user to send signalsto the child. Otherwise, if a command-line argument is supplied, then the child exitsimmediately, using the integer supplied on the command line as the exit status. Theparent process executes a loop that monitors the child using waitpid, and uses the W*()macros described above to analyze the wait status value.

The following shell session demonstrates the use of the program:

$ . / a . out &Child PID i s 32360[ 1 ] 32359$ k i l l −STOP 32360stopped by s i g n a l 19$ k i l l −CONT 32360cont inued$ k i l l −TERM 32360k i l l e d by s i g n a l 15[1 ]+ Done . / a . out$

1 #include <sys / wait . h>2 #include <s t d l i b . h>3 #include <uni s td . h>4 #include <s t d i o . h>56 int main ( int argc , char ∗argv [ ] )

23

Page 24: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

7 {8 p i d t cpid , w;9 int s t a t u s ;

1011 cpid = fo rk ( ) ;12 i f ( cpid == −1) {13 pe r ro r ( ” f o rk ” ) ;14 e x i t (EXIT FAILURE) ;15 }1617 i f ( cpid == 0) { /∗ Code executed by c h i l d ∗/18 p r i n t f ( ” Child PID i s %ld \n” , ( long ) ge tp id ( ) ) ;19 i f ( argc == 1)20 pause ( ) ; /∗ Wait f o r s i g n a l s ∗/21 e x i t ( a t o i ( argv [ 1 ] ) ) ;22 } else { /∗ Code executed by parent ∗/23 do {24 w = waitp id ( cpid , &status , WUNTRACED | WCONTINUED) ;25 i f (w == −1) {26 pe r ro r ( ” waitp id ” ) ;27 e x i t (EXIT FAILURE) ;28 }2930 i f (WIFEXITED( s t a t u s ) ) {31 p r i n t f ( ” ex i ted , s t a t u s=%d\n” , WEXITSTATUS( s t a t u s ) ) ;32 } else i f (WIFSIGNALED( s t a t u s ) ) {33 p r i n t f ( ” k i l l e d by s i g n a l %d\n” , WTERMSIG( s t a t u s ) ) ;34 } else i f (WIFSTOPPED( s t a t u s ) ) {35 p r i n t f ( ” stopped by s i g n a l %d\n” , WSTOPSIG( s t a t u s ) ) ;36 } else i f (WIFCONTINUED( s t a t u s ) ) {37 p r i n t f ( ” cont inued \n” ) ;38 }39 } while ( !WIFEXITED( s t a t u s ) && !WIFSIGNALED( s t a t u s ) ) ;40 e x i t (EXIT SUCCESS) ;41 }42 }

A.4. getpid, getppid - get process identification

description: getpid returns the process ID of the calling process. (This is often usedby routines that generate unique temporary filenames.)

24

Page 25: Exercise 2: Process-, Memory- and IO-Management · code, associated data and a process control block (PCB). The process control block is a data structure storing information that

Chair forSystem Security

RUHR-UNIVERSITY BOCHUM

Exercises Operating System SecurityWS2008/09

getppid returns the process ID of the parent of the calling process.

errors: These functions are always successful.

confirming to: POSIX.1-2001, 4.3BSD, SVr4

References

[1] Python (programming language) http://en.wikipedia.org/wiki/Python_(programming_language)

[2] Python Tutorial http://docs.python.org/tutorial/

[3] Python Language Reference http://docs.python.org/reference/

[4] Python Standard Library http://docs.python.org/library/

[5] The Kate Text Editor http://kate-editor.org/

[6] The wikipedia malloc-page http://en.wikipedia.org/wiki/Malloc

25