SlideShare una empresa de Scribd logo
1 de 55
Updates
Progress updates and
scheduling design
reviews will be due
Sunday 11:59pm

Tonight on Colbert Report!

Tuesday’s Class:
Yuchen Zhou on
Authentication using
Single Sign-On

12 November 2013

University of Virginia cs4414

1
Recap: Last Class
Translation Lookaside Buffer (Cache)

Memory

Page

Paging
Unit

Physical Address

Dir

Linear Address

Logical Address

Segmentation Unit

Offset

CR3

Page
Directory

Page Table

Physical Memory

GDTR

Global
Descriptor
Table
12 November 2013

University of Virginia cs4414

2
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
char *s = (char *) malloc (1);
int i= 0;
while (1) {
printf("%d: %xn", i, s[i]);
i += 4;
}
}

12 November 2013

What will this program do?

> ./a.out
0: 0
4: 0
8: 0
12: 0
…1033872: 0
1033876: 0
1033880: 0
1033884: 0
Segmentation fault: 11

University of Virginia cs4414

3
12 November 2013

University of Virginia cs4414

4
> clang segv.c
segv.c:22:8: warning: expression result unused [-Wunused-value]
s[i];
~ ~^
1 warning generated.
> ./a.out
^C

12 November 2013

University of Virginia cs4414

5
$ ./a.out
Caught segv: 11
i = 1033888
Caught segv: 11
i = 1033888
Caught segv: 11
i = 1033888
Caught segv: 11
i = 1033888
Caught segv: 11
i = 1033888
Caught segv: 11
i = 1033888
Caught segv: 11
i = 1033888
…
12 November 2013

University of Virginia cs4414

6
> ulimit -a
core file size
data seg size
file size
max locked memory
max memory size
open files
pipe size
stack size
cpu time
max user processes
virtual memory

12 November 2013

(blocks,
(kbytes,
(blocks,
(kbytes,
(kbytes,

-c)
-d)
-f)
-l)
-m)
(-n)
(512 bytes, -p)
(kbytes, -s)
(seconds, -t)
(-u)
(kbytes, -v)

University of Virginia cs4414

0
unlimited
unlimited
unlimited
unlimited
256
1
8515
unlimited
709
unlimited

7
USENIX Security 2007

12 November 2013

University of Virginia cs4414

8
Rust Runtime

Forking Fork
run::Process::new(program, argv, options)
spawn_process_os(prog, args, env, dir, in_fd, …)
fork()
int 0x80

libc: fork()
jumps into kernel code
sets supervisor mode

linux kernel: fork syscall
12 November 2013

University of Virginia cs4414

9
/*
* linux/kernel/fork.c
*
* Copyright (C) 1991, 1992
*/

Linus Torvalds

/*
* 'fork.c' contains the help-routines for the 'fork' system call
* (see also entry.S and others).
* Fork is rather simple, once you get the hang of it, but the memory
* management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
*/
#include
#include
#include
#include
#include
#include
…

<linux/slab.h>
<linux/init.h>
<linux/unistd.h>
<linux/module.h>
<linux/vmalloc.h>
<linux/completion.h>

1935 total lines
12 November 2013

University of Virginia cs4414

10
/*
* Ok, this is the main fork-routine.
*
* It copies the process, and if successful kick-starts
* it and waits for it to finish using the VM if required.
*/
long do_fork(unsigned long clone_flags,
unsigned long stack_start,
unsigned long stack_size,
int __user *parent_tidptr,
int __user *child_tidptr)
{
struct task_struct *p;
int trace = 0;
long nr;
/*
* Determine whether and which event to report to ptracer. When
* called from kernel_thread or CLONE_UNTRACED is explicitly
* requested, no event is reported; otherwise, report if the event
* for the type of forking is enabled.
*/
if (!(clone_flags & CLONE_UNTRACED)) { … }
12 November 2013

University of Virginia cs4414

11
long do_fork(unsigned long clone_flags,
unsigned long stack_start,
unsigned long stack_size,
int __user *parent_tidptr,
int __user *child_tidptr)
{
struct task_struct *p;
int trace = 0;
long nr;
/* Determine whether and which event to report to ptracer... */
p = copy_process(clone_flags, stack_start, stack_size,
child_tidptr, NULL, trace);
/*
* Do this prior (to) waking up the new thread – the thread pointer
* might get invalid after that point, if the thread exits quickly.
*/
if (!IS_ERR(p)) {
...

12 November 2013

University of Virginia cs4414

12
/*
This creates a new process as a copy of the old one, but does not actually start it yet. It copies
the registers, and all the appropriate parts of the process environment (as per the clone flags).
The actual kick-off is left to the caller.
*/
static struct task_struct *copy_process(unsigned long clone_flags,
unsigned long stack_start,
unsigned long stack_size,
int __user *child_tidptr,
struct pid *pid,
int trace)
{
int retval;
struct task_struct *p;
if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
return ERR_PTR(-EINVAL);
... // lots more error cases based on flags
retval = security_task_create(clone_flags);
if (retval)
goto fork_out;
... // this is the interesting part we will look at next
fork_out:
return ERR_PTR(retval);
}
12 November 2013

University of Virginia cs4414

13
What should be in a task_struct?

“task” here means process (its what copy_process returns), not to be
confused with a Rust task
12 November 2013

University of Virginia cs4414

14
include/linux/sched.h

Definition of task_struct is over 400 lines!

12 November 2013

University of Virginia cs4414

15
Memory Management

mm_struct is another huge data structure…we’ll look at later.

12 November 2013

University of Virginia cs4414

16
12 November 2013

University of Virginia cs4414

17
Stack Canary
arch/x86/include/asm/stackprotector.h

12 November 2013

University of Virginia cs4414

18
Protecting Stack Frames
Saved Registers
Saved Registers
Parameters
Parameters
Return Address
Return Address

gcc –Wstack-protector

Local Variables

Canary

Local Variables

Why does the kernel need code to support this?
12 November 2013

University of Virginia cs4414

19
12 November 2013

University of Virginia cs4414

20
Other things in struct task:

12 November 2013

University of Virginia cs4414

21
static struct task_struct *copy_process(unsigned long clone_flags,
unsigned long stack_start,
unsigned long stack_size,
int __user *child_tidptr,
struct pid *pid,
int trace)
{
int retval;
struct task_struct *p;
... // lots more error cases based on flags

What is current?

retval = security_task_create(clone_flags);
if (retval)
#ifndef _ASM_X86_CURRENT_H
goto fork_out;
retval = -ENOMEM;
p = dup_task_struct(current);
if (!p)
goto fork_out;
...
fork_out:
return ERR_PTR(retval);
}

#define _ASM_X86_CURRENT_H
#include <linux/compiler.h>
#include <asm/percpu.h>
#ifndef __ASSEMBLY__
struct task_struct;
DECLARE_PER_CPU(struct task_struct *, current_task);
static __always_inline
struct task_struct *get_current(void)
{
return percpu_read_stable(current_task);
}
#define current get_current()
#endif /* __ASSEMBLY__ */
#endif /* _ASM_X86_CURRENT_H */

/linux-2.6.32-rc3/arch/x86/include/asm/current.h
12 November 2013

University of Virginia cs4414

22
static struct task_struct *dup_task_struct(struct task_struct *orig)
{
struct task_struct *tsk;
struct thread_info *ti;
unsigned long *stackend;
int node = tsk_fork_get_node(orig);
int err;
tsk = alloc_task_struct_node(node);
if (!tsk)
return NULL;
ti = alloc_thread_info_node(tsk, node);
if (!ti)
goto free_tsk;
err = arch_dup_task_struct(tsk, orig);
if (err)
goto free_ti;
tsk->stack = ti;
setup_thread_stack(tsk, orig);
clear_user_return_notifier(tsk);
clear_tsk_need_resched(tsk);
stackend = end_of_stack(tsk);
*stackend = STACK_END_MAGIC; /* for overflow detection */

#ifdef CONFIG_CC_STACKPROTECTOR
tsk->stack_canary = get_random_int();
#endif
...
12 November 2013

University of Virginia cs4414

23
static struct task_struct *dup_task_struct(struct task_struct *orig)
{
struct task_struct *tsk;
Linux/include/linux/sched.h
struct thread_info *ti;
unsigned long *stackend;
int node = tsk_fork_get_node(orig);
...
int err;
#define task_thread_info(task)((struct thread_info *)(task)->stack)

#define task_stack_page(task)
((task)->stack)
tsk = alloc_task_struct_node(node);
if (!tsk)
static inline void setup_thread_stack(struct task_struct *p,
return NULL;
{

struct task_struct *org)

*task_thread_info(p) = *task_thread_info(org);
ti = alloc_thread_info_node(tsk, node);
if (!ti) task_thread_info(p)->task = p;
goto free_tsk;
}
static inline unsigned long *end_of_stack(struct task_struct
err = arch_dup_task_struct(tsk, orig);
if (err)
{
goto free_ti;
return (unsigned long *)(task_thread_info(p) + 1);

*p)

}
tsk->stack = ti;
setup_thread_stack(tsk, orig);
clear_user_return_notifier(tsk);
clear_tsk_need_resched(tsk);
stackend = end_of_stack(tsk);
*stackend = STACK_END_MAGIC; /* for overflow detection */

#ifdef CONFIG_CC_STACKPROTECTOR
tsk->stack_canary = get_random_int();
#endif
...
12 November 2013

University of Virginia cs4414

24
static struct task_struct *dup_task_struct(struct task_struct *orig)
{
struct task_struct *tsk;
struct thread_info *ti;
unsigned long *stackend;
int node = tsk_fork_get_node(orig);
int err;
tsk = alloc_task_struct_node(node);
if (!tsk)
return NULL;
ti = alloc_thread_info_node(tsk, node);
if (!ti)
goto free_tsk;
err = arch_dup_task_struct(tsk, orig);
if (err)
goto free_ti;
tsk->stack = ti;
setup_thread_stack(tsk, orig);
clear_user_return_notifier(tsk);
clear_tsk_need_resched(tsk);
stackend = end_of_stack(tsk);
*stackend = STACK_END_MAGIC; /* for overflow detection */

#ifdef CONFIG_CC_STACKPROTECTOR
tsk->stack_canary = get_random_int();
#endif
...
12 November 2013

University of Virginia cs4414

25
12 November 2013

University of Virginia cs4414

26
12 November 2013

University of Virginia cs4414

27
12 November 2013

University of Virginia cs4414

28
https://github.com/torvalds/linux/search?q=ST
ACK_END_MAGIC&ref=cmdform

In no_context, called by mm_fault_error

Does this help defend against a stack-smashing buffer overflow attack?
12 November 2013

University of Virginia cs4414

29
12 November 2013

University of Virginia cs4414

30
...
tsk->stack_canary = get_random_int();
...

12 November 2013

University of Virginia cs4414

31
static struct task_struct *dup_task_struct(struct task_struct *orig)
{
...
clear_tsk_need_resched(tsk);
stackend = end_of_stack(tsk);
*stackend = STACK_END_MAGIC; /* for overflow detection */
#ifdef CONFIG_CC_STACKPROTECTOR
tsk->stack_canary = get_random_int();
#endif

/*
* One for us, one for whoever does the "release_task()" (usually
* parent)
*/
atomic_set(&tsk->usage, 2);
#ifdef CONFIG_BLK_DEV_IO_TRACE
tsk->btrace_seq = 0;
#endif
tsk->splice_pipe = NULL;
tsk->task_frag.page = NULL;
account_kernel_stack(ti, 1);
return tsk;
free_ti:
free_thread_info(ti);
free_tsk:
free_task_struct(tsk);
return NULL;
}
12 November 2013

University of Virginia cs4414

32
static struct task_struct *copy_process(...)
{
...
p = dup_task_struct(current);
...
/* Perform scheduler related setup. Assign this task to a CPU. */
sched_fork(p);
...
}
kernel/sched/core.c

12 November 2013

University of Virginia cs4414

33
12 November 2013

University of Virginia cs4414

34
12 November 2013

University of Virginia cs4414

35
include/linux/smp.h

12 November 2013

University of Virginia cs4414

36
http://lxr.free-electrons.com/ident?i=preempt_disable

12 November 2013

University of Virginia cs4414

37
static struct task_struct *copy_process(...)
{
...
p = dup_task_struct(current);
...
/* Perform scheduler related setup. Assign this task to a CPU. */
sched_fork(p);
...
retval = copy_mm(clone_flags, p);
...
}

static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
{
struct mm_struct *mm, *oldmm;
int retval;
...
mm = dup_mm(tsk);
if (!mm)
goto fail_nomem;
good_mm:
tsk->mm = mm;
tsk->active_mm = mm;
return 0;
…
12 November 2013

University of Virginia cs4414

38
/*
* Allocate a new mm structure and copy contents from the
* mm structure of the passed in task structure.
*/
struct mm_struct *dup_mm(struct task_struct *tsk)
{
struct mm_struct *mm, *oldmm = current->mm;
int err;
if (!oldmm)
return NULL;
mm = allocate_mm();
if (!mm)
goto fail_nomem;
memcpy(mm, oldmm, sizeof(*mm));
...
#define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
#define free_mm(mm)
(kmem_cache_free(mm_cachep, (mm)))

12 November 2013

University of Virginia cs4414

39
Three Linux memory allocators:
SLOB = “Simple List of Blocks”
SLAB = allocation with less fragmentation
SLUB = less fragmentation, better reuse (Default)
12 November 2013

University of Virginia cs4414

40
12 November 2013

University of Virginia cs4414

41
12 November 2013

University of Virginia cs4414

42
12 November 2013

University of Virginia cs4414

43
12 November 2013

University of Virginia cs4414

44
12 November 2013

University of Virginia cs4414

45
include/linux/gfp.h

12 November 2013

University of Virginia cs4414

46
12 November 2013

University of Virginia cs4414

47
mm/page_alloc.c

12 November 2013

University of Virginia cs4414

48
Page Table
32-bit linear address
CR3

Dir

Page

10 bits
(1K tables)

Page
Directory

Offset

10 bits
12 bits
(1K entries) (4K pages)

Page Entry

Page Table

Physical
Memory
Page + Offset

CR3+Dir

12 November 2013

University of Virginia cs4414

49
12 November 2013

University of Virginia cs4414

50
arch/x86/include/asm/pgtable.h

12 November 2013

University of Virginia cs4414

51
TLB

Memory

Paging
Unit

Physical Address

Linear Address

Logical Address

Segmentation Unit

32-bit linear address

CR3

What does the
kernel need to
do to flush the
TLB?

Dir
10 bits
(1K tables)

Page
10 bits
(1K entries)

Offset
12 bits
(4K pages)

Page Entry

Page Directory

Page Table

CR3+Dir

12 November 2013

University of Virginia cs4414

52
arch/x86/include/asm/tlbflush.h

arch/x86/include/asm/special_insns.h
12 November 2013

University of Virginia cs4414

53
Charge
Progress updates
and scheduling
design reviews will
be due Sunday
11:59pm
Tuesday’s Class:
Yuchen Zhou on
Authentication using
Single Sign-On
12 November 2013

University of Virginia cs4414

54

Más contenido relacionado

La actualidad más candente

Segmentation Faults, Page Faults, Processes, Threads, and Tasks
Segmentation Faults, Page Faults, Processes, Threads, and TasksSegmentation Faults, Page Faults, Processes, Threads, and Tasks
Segmentation Faults, Page Faults, Processes, Threads, and TasksDavid Evans
 
SSL Failing, Sharing, and Scheduling
SSL Failing, Sharing, and SchedulingSSL Failing, Sharing, and Scheduling
SSL Failing, Sharing, and SchedulingDavid Evans
 
Making a Process (Virtualizing Memory)
Making a Process (Virtualizing Memory)Making a Process (Virtualizing Memory)
Making a Process (Virtualizing Memory)David Evans
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web ServersDavid Evans
 
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataKernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataAnne Nicolas
 
Linux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactLinux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactAlessandro Selli
 
How to write memory efficient code?
How to write memory efficient code?How to write memory efficient code?
How to write memory efficient code?Tier1 app
 
Once Upon a Process
Once Upon a ProcessOnce Upon a Process
Once Upon a ProcessDavid Evans
 
Solaris Kernel Debugging V1.0
Solaris Kernel Debugging V1.0Solaris Kernel Debugging V1.0
Solaris Kernel Debugging V1.0Jarod Wang
 
Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsemBO_Conference
 
How & why-memory-efficient?
How & why-memory-efficient?How & why-memory-efficient?
How & why-memory-efficient?Tier1 app
 
Python twisted
Python twistedPython twisted
Python twistedMahendra M
 
Zabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensZabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensNETWAYS
 
Opendaylight app development
Opendaylight app developmentOpendaylight app development
Opendaylight app developmentvjanandr
 

La actualidad más candente (20)

System Calls
System CallsSystem Calls
System Calls
 
Segmentation Faults, Page Faults, Processes, Threads, and Tasks
Segmentation Faults, Page Faults, Processes, Threads, and TasksSegmentation Faults, Page Faults, Processes, Threads, and Tasks
Segmentation Faults, Page Faults, Processes, Threads, and Tasks
 
SSL Failing, Sharing, and Scheduling
SSL Failing, Sharing, and SchedulingSSL Failing, Sharing, and Scheduling
SSL Failing, Sharing, and Scheduling
 
Making a Process (Virtualizing Memory)
Making a Process (Virtualizing Memory)Making a Process (Virtualizing Memory)
Making a Process (Virtualizing Memory)
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web Servers
 
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataKernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
 
The Internet
The InternetThe Internet
The Internet
 
Linux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactLinux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compact
 
How to write memory efficient code?
How to write memory efficient code?How to write memory efficient code?
How to write memory efficient code?
 
Once Upon a Process
Once Upon a ProcessOnce Upon a Process
Once Upon a Process
 
Solaris Kernel Debugging V1.0
Solaris Kernel Debugging V1.0Solaris Kernel Debugging V1.0
Solaris Kernel Debugging V1.0
 
Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf Tools
 
Lec05 buffers basic_examples
Lec05 buffers basic_examplesLec05 buffers basic_examples
Lec05 buffers basic_examples
 
How & why-memory-efficient?
How & why-memory-efficient?How & why-memory-efficient?
How & why-memory-efficient?
 
Lec09 nbody-optimization
Lec09 nbody-optimizationLec09 nbody-optimization
Lec09 nbody-optimization
 
Lec11 timing
Lec11 timingLec11 timing
Lec11 timing
 
Python twisted
Python twistedPython twisted
Python twisted
 
Zabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensZabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet Mens
 
Opendaylight app development
Opendaylight app developmentOpendaylight app development
Opendaylight app development
 
Lec02 03 opencl_intro
Lec02 03 opencl_introLec02 03 opencl_intro
Lec02 03 opencl_intro
 

Similar a Putting a Fork in Fork (Linux Process and Memory Management)

Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelVitaly Nikolenko
 
grsecurity and PaX
grsecurity and PaXgrsecurity and PaX
grsecurity and PaXKernel TLV
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bChereCheek752
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak   CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak PROIDEA
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...corehard_by
 
CUDA Deep Dive
CUDA Deep DiveCUDA Deep Dive
CUDA Deep Divekrasul
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenPostgresOpen
 
PT-4057, Automated CUDA-to-OpenCL™ Translation with CU2CL: What's Next?, by W...
PT-4057, Automated CUDA-to-OpenCL™ Translation with CU2CL: What's Next?, by W...PT-4057, Automated CUDA-to-OpenCL™ Translation with CU2CL: What's Next?, by W...
PT-4057, Automated CUDA-to-OpenCL™ Translation with CU2CL: What's Next?, by W...AMD Developer Central
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatchcqtt191
 
Lee 2020 what the clock !
Lee 2020  what the clock !Lee 2020  what the clock !
Lee 2020 what the clock !Neil Armstrong
 
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...PROIDEA
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debuggingJungMinSEO5
 
Kapacitor - Real Time Data Processing Engine
Kapacitor - Real Time Data Processing EngineKapacitor - Real Time Data Processing Engine
Kapacitor - Real Time Data Processing EnginePrashant Vats
 
A CTF Hackers Toolbox
A CTF Hackers ToolboxA CTF Hackers Toolbox
A CTF Hackers ToolboxStefan
 
Db2 For I Parallel Data Load
Db2 For I Parallel Data LoadDb2 For I Parallel Data Load
Db2 For I Parallel Data LoadThomas Wolfe
 
2015.07.16 Способы диагностики PostgreSQL
2015.07.16 Способы диагностики PostgreSQL2015.07.16 Способы диагностики PostgreSQL
2015.07.16 Способы диагностики PostgreSQLdev1ant
 
PHP CLI: A Cinderella Story
PHP CLI: A Cinderella StoryPHP CLI: A Cinderella Story
PHP CLI: A Cinderella StoryMike Lively
 

Similar a Putting a Fork in Fork (Linux Process and Memory Management) (20)

Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernel
 
grsecurity and PaX
grsecurity and PaXgrsecurity and PaX
grsecurity and PaX
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak   CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
 
CUDA Deep Dive
CUDA Deep DiveCUDA Deep Dive
CUDA Deep Dive
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
 
PT-4057, Automated CUDA-to-OpenCL™ Translation with CU2CL: What's Next?, by W...
PT-4057, Automated CUDA-to-OpenCL™ Translation with CU2CL: What's Next?, by W...PT-4057, Automated CUDA-to-OpenCL™ Translation with CU2CL: What's Next?, by W...
PT-4057, Automated CUDA-to-OpenCL™ Translation with CU2CL: What's Next?, by W...
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
Lee 2020 what the clock !
Lee 2020  what the clock !Lee 2020  what the clock !
Lee 2020 what the clock !
 
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
Kapacitor - Real Time Data Processing Engine
Kapacitor - Real Time Data Processing EngineKapacitor - Real Time Data Processing Engine
Kapacitor - Real Time Data Processing Engine
 
A CTF Hackers Toolbox
A CTF Hackers ToolboxA CTF Hackers Toolbox
A CTF Hackers Toolbox
 
Db2 For I Parallel Data Load
Db2 For I Parallel Data LoadDb2 For I Parallel Data Load
Db2 For I Parallel Data Load
 
Osol Pgsql
Osol PgsqlOsol Pgsql
Osol Pgsql
 
2015.07.16 Способы диагностики PostgreSQL
2015.07.16 Способы диагностики PostgreSQL2015.07.16 Способы диагностики PostgreSQL
2015.07.16 Способы диагностики PostgreSQL
 
PHP CLI: A Cinderella Story
PHP CLI: A Cinderella StoryPHP CLI: A Cinderella Story
PHP CLI: A Cinderella Story
 

Más de David Evans

Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!David Evans
 
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksTrick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksDavid Evans
 
Hidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeHidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeDavid Evans
 
Anonymity in Bitcoin
Anonymity in BitcoinAnonymity in Bitcoin
Anonymity in BitcoinDavid Evans
 
Midterm Confirmations
Midterm ConfirmationsMidterm Confirmations
Midterm ConfirmationsDavid Evans
 
Scripting Transactions
Scripting TransactionsScripting Transactions
Scripting TransactionsDavid Evans
 
How to Live in Paradise
How to Live in ParadiseHow to Live in Paradise
How to Live in ParadiseDavid Evans
 
Mining Economics
Mining EconomicsMining Economics
Mining EconomicsDavid Evans
 
Becoming More Paranoid
Becoming More ParanoidBecoming More Paranoid
Becoming More ParanoidDavid Evans
 
Asymmetric Key Signatures
Asymmetric Key SignaturesAsymmetric Key Signatures
Asymmetric Key SignaturesDavid Evans
 
Introduction to Cryptography
Introduction to CryptographyIntroduction to Cryptography
Introduction to CryptographyDavid Evans
 
Class 1: What is Money?
Class 1: What is Money?Class 1: What is Money?
Class 1: What is Money?David Evans
 
Multi-Party Computation for the Masses
Multi-Party Computation for the MassesMulti-Party Computation for the Masses
Multi-Party Computation for the MassesDavid Evans
 
Proof of Reserve
Proof of ReserveProof of Reserve
Proof of ReserveDavid Evans
 
Blooming Sidechains!
Blooming Sidechains!Blooming Sidechains!
Blooming Sidechains!David Evans
 
Useful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinUseful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinDavid Evans
 

Más de David Evans (20)

Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!
 
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksTrick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
 
Hidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeHidden Services, Zero Knowledge
Hidden Services, Zero Knowledge
 
Anonymity in Bitcoin
Anonymity in BitcoinAnonymity in Bitcoin
Anonymity in Bitcoin
 
Midterm Confirmations
Midterm ConfirmationsMidterm Confirmations
Midterm Confirmations
 
Scripting Transactions
Scripting TransactionsScripting Transactions
Scripting Transactions
 
How to Live in Paradise
How to Live in ParadiseHow to Live in Paradise
How to Live in Paradise
 
Bitcoin Script
Bitcoin ScriptBitcoin Script
Bitcoin Script
 
Mining Economics
Mining EconomicsMining Economics
Mining Economics
 
Mining
MiningMining
Mining
 
The Blockchain
The BlockchainThe Blockchain
The Blockchain
 
Becoming More Paranoid
Becoming More ParanoidBecoming More Paranoid
Becoming More Paranoid
 
Asymmetric Key Signatures
Asymmetric Key SignaturesAsymmetric Key Signatures
Asymmetric Key Signatures
 
Introduction to Cryptography
Introduction to CryptographyIntroduction to Cryptography
Introduction to Cryptography
 
Class 1: What is Money?
Class 1: What is Money?Class 1: What is Money?
Class 1: What is Money?
 
Multi-Party Computation for the Masses
Multi-Party Computation for the MassesMulti-Party Computation for the Masses
Multi-Party Computation for the Masses
 
Proof of Reserve
Proof of ReserveProof of Reserve
Proof of Reserve
 
Silk Road
Silk RoadSilk Road
Silk Road
 
Blooming Sidechains!
Blooming Sidechains!Blooming Sidechains!
Blooming Sidechains!
 
Useful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinUseful Proofs of Work, Permacoin
Useful Proofs of Work, Permacoin
 

Último

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Último (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Putting a Fork in Fork (Linux Process and Memory Management)

  • 1.
  • 2. Updates Progress updates and scheduling design reviews will be due Sunday 11:59pm Tonight on Colbert Report! Tuesday’s Class: Yuchen Zhou on Authentication using Single Sign-On 12 November 2013 University of Virginia cs4414 1
  • 3. Recap: Last Class Translation Lookaside Buffer (Cache) Memory Page Paging Unit Physical Address Dir Linear Address Logical Address Segmentation Unit Offset CR3 Page Directory Page Table Physical Memory GDTR Global Descriptor Table 12 November 2013 University of Virginia cs4414 2
  • 4. #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char *s = (char *) malloc (1); int i= 0; while (1) { printf("%d: %xn", i, s[i]); i += 4; } } 12 November 2013 What will this program do? > ./a.out 0: 0 4: 0 8: 0 12: 0 …1033872: 0 1033876: 0 1033880: 0 1033884: 0 Segmentation fault: 11 University of Virginia cs4414 3
  • 5. 12 November 2013 University of Virginia cs4414 4
  • 6. > clang segv.c segv.c:22:8: warning: expression result unused [-Wunused-value] s[i]; ~ ~^ 1 warning generated. > ./a.out ^C 12 November 2013 University of Virginia cs4414 5
  • 7. $ ./a.out Caught segv: 11 i = 1033888 Caught segv: 11 i = 1033888 Caught segv: 11 i = 1033888 Caught segv: 11 i = 1033888 Caught segv: 11 i = 1033888 Caught segv: 11 i = 1033888 Caught segv: 11 i = 1033888 … 12 November 2013 University of Virginia cs4414 6
  • 8. > ulimit -a core file size data seg size file size max locked memory max memory size open files pipe size stack size cpu time max user processes virtual memory 12 November 2013 (blocks, (kbytes, (blocks, (kbytes, (kbytes, -c) -d) -f) -l) -m) (-n) (512 bytes, -p) (kbytes, -s) (seconds, -t) (-u) (kbytes, -v) University of Virginia cs4414 0 unlimited unlimited unlimited unlimited 256 1 8515 unlimited 709 unlimited 7
  • 9. USENIX Security 2007 12 November 2013 University of Virginia cs4414 8
  • 10. Rust Runtime Forking Fork run::Process::new(program, argv, options) spawn_process_os(prog, args, env, dir, in_fd, …) fork() int 0x80 libc: fork() jumps into kernel code sets supervisor mode linux kernel: fork syscall 12 November 2013 University of Virginia cs4414 9
  • 11. /* * linux/kernel/fork.c * * Copyright (C) 1991, 1992 */ Linus Torvalds /* * 'fork.c' contains the help-routines for the 'fork' system call * (see also entry.S and others). * Fork is rather simple, once you get the hang of it, but the memory * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' */ #include #include #include #include #include #include … <linux/slab.h> <linux/init.h> <linux/unistd.h> <linux/module.h> <linux/vmalloc.h> <linux/completion.h> 1935 total lines 12 November 2013 University of Virginia cs4414 10
  • 12. /* * Ok, this is the main fork-routine. * * It copies the process, and if successful kick-starts * it and waits for it to finish using the VM if required. */ long do_fork(unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size, int __user *parent_tidptr, int __user *child_tidptr) { struct task_struct *p; int trace = 0; long nr; /* * Determine whether and which event to report to ptracer. When * called from kernel_thread or CLONE_UNTRACED is explicitly * requested, no event is reported; otherwise, report if the event * for the type of forking is enabled. */ if (!(clone_flags & CLONE_UNTRACED)) { … } 12 November 2013 University of Virginia cs4414 11
  • 13. long do_fork(unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size, int __user *parent_tidptr, int __user *child_tidptr) { struct task_struct *p; int trace = 0; long nr; /* Determine whether and which event to report to ptracer... */ p = copy_process(clone_flags, stack_start, stack_size, child_tidptr, NULL, trace); /* * Do this prior (to) waking up the new thread – the thread pointer * might get invalid after that point, if the thread exits quickly. */ if (!IS_ERR(p)) { ... 12 November 2013 University of Virginia cs4414 12
  • 14. /* This creates a new process as a copy of the old one, but does not actually start it yet. It copies the registers, and all the appropriate parts of the process environment (as per the clone flags). The actual kick-off is left to the caller. */ static struct task_struct *copy_process(unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size, int __user *child_tidptr, struct pid *pid, int trace) { int retval; struct task_struct *p; if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) return ERR_PTR(-EINVAL); ... // lots more error cases based on flags retval = security_task_create(clone_flags); if (retval) goto fork_out; ... // this is the interesting part we will look at next fork_out: return ERR_PTR(retval); } 12 November 2013 University of Virginia cs4414 13
  • 15. What should be in a task_struct? “task” here means process (its what copy_process returns), not to be confused with a Rust task 12 November 2013 University of Virginia cs4414 14
  • 16. include/linux/sched.h Definition of task_struct is over 400 lines! 12 November 2013 University of Virginia cs4414 15
  • 17. Memory Management mm_struct is another huge data structure…we’ll look at later. 12 November 2013 University of Virginia cs4414 16
  • 18. 12 November 2013 University of Virginia cs4414 17
  • 19. Stack Canary arch/x86/include/asm/stackprotector.h 12 November 2013 University of Virginia cs4414 18
  • 20. Protecting Stack Frames Saved Registers Saved Registers Parameters Parameters Return Address Return Address gcc –Wstack-protector Local Variables Canary Local Variables Why does the kernel need code to support this? 12 November 2013 University of Virginia cs4414 19
  • 21. 12 November 2013 University of Virginia cs4414 20
  • 22. Other things in struct task: 12 November 2013 University of Virginia cs4414 21
  • 23. static struct task_struct *copy_process(unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size, int __user *child_tidptr, struct pid *pid, int trace) { int retval; struct task_struct *p; ... // lots more error cases based on flags What is current? retval = security_task_create(clone_flags); if (retval) #ifndef _ASM_X86_CURRENT_H goto fork_out; retval = -ENOMEM; p = dup_task_struct(current); if (!p) goto fork_out; ... fork_out: return ERR_PTR(retval); } #define _ASM_X86_CURRENT_H #include <linux/compiler.h> #include <asm/percpu.h> #ifndef __ASSEMBLY__ struct task_struct; DECLARE_PER_CPU(struct task_struct *, current_task); static __always_inline struct task_struct *get_current(void) { return percpu_read_stable(current_task); } #define current get_current() #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_CURRENT_H */ /linux-2.6.32-rc3/arch/x86/include/asm/current.h 12 November 2013 University of Virginia cs4414 22
  • 24. static struct task_struct *dup_task_struct(struct task_struct *orig) { struct task_struct *tsk; struct thread_info *ti; unsigned long *stackend; int node = tsk_fork_get_node(orig); int err; tsk = alloc_task_struct_node(node); if (!tsk) return NULL; ti = alloc_thread_info_node(tsk, node); if (!ti) goto free_tsk; err = arch_dup_task_struct(tsk, orig); if (err) goto free_ti; tsk->stack = ti; setup_thread_stack(tsk, orig); clear_user_return_notifier(tsk); clear_tsk_need_resched(tsk); stackend = end_of_stack(tsk); *stackend = STACK_END_MAGIC; /* for overflow detection */ #ifdef CONFIG_CC_STACKPROTECTOR tsk->stack_canary = get_random_int(); #endif ... 12 November 2013 University of Virginia cs4414 23
  • 25. static struct task_struct *dup_task_struct(struct task_struct *orig) { struct task_struct *tsk; Linux/include/linux/sched.h struct thread_info *ti; unsigned long *stackend; int node = tsk_fork_get_node(orig); ... int err; #define task_thread_info(task)((struct thread_info *)(task)->stack) #define task_stack_page(task) ((task)->stack) tsk = alloc_task_struct_node(node); if (!tsk) static inline void setup_thread_stack(struct task_struct *p, return NULL; { struct task_struct *org) *task_thread_info(p) = *task_thread_info(org); ti = alloc_thread_info_node(tsk, node); if (!ti) task_thread_info(p)->task = p; goto free_tsk; } static inline unsigned long *end_of_stack(struct task_struct err = arch_dup_task_struct(tsk, orig); if (err) { goto free_ti; return (unsigned long *)(task_thread_info(p) + 1); *p) } tsk->stack = ti; setup_thread_stack(tsk, orig); clear_user_return_notifier(tsk); clear_tsk_need_resched(tsk); stackend = end_of_stack(tsk); *stackend = STACK_END_MAGIC; /* for overflow detection */ #ifdef CONFIG_CC_STACKPROTECTOR tsk->stack_canary = get_random_int(); #endif ... 12 November 2013 University of Virginia cs4414 24
  • 26. static struct task_struct *dup_task_struct(struct task_struct *orig) { struct task_struct *tsk; struct thread_info *ti; unsigned long *stackend; int node = tsk_fork_get_node(orig); int err; tsk = alloc_task_struct_node(node); if (!tsk) return NULL; ti = alloc_thread_info_node(tsk, node); if (!ti) goto free_tsk; err = arch_dup_task_struct(tsk, orig); if (err) goto free_ti; tsk->stack = ti; setup_thread_stack(tsk, orig); clear_user_return_notifier(tsk); clear_tsk_need_resched(tsk); stackend = end_of_stack(tsk); *stackend = STACK_END_MAGIC; /* for overflow detection */ #ifdef CONFIG_CC_STACKPROTECTOR tsk->stack_canary = get_random_int(); #endif ... 12 November 2013 University of Virginia cs4414 25
  • 27. 12 November 2013 University of Virginia cs4414 26
  • 28. 12 November 2013 University of Virginia cs4414 27
  • 29. 12 November 2013 University of Virginia cs4414 28
  • 30. https://github.com/torvalds/linux/search?q=ST ACK_END_MAGIC&ref=cmdform In no_context, called by mm_fault_error Does this help defend against a stack-smashing buffer overflow attack? 12 November 2013 University of Virginia cs4414 29
  • 31. 12 November 2013 University of Virginia cs4414 30
  • 32. ... tsk->stack_canary = get_random_int(); ... 12 November 2013 University of Virginia cs4414 31
  • 33. static struct task_struct *dup_task_struct(struct task_struct *orig) { ... clear_tsk_need_resched(tsk); stackend = end_of_stack(tsk); *stackend = STACK_END_MAGIC; /* for overflow detection */ #ifdef CONFIG_CC_STACKPROTECTOR tsk->stack_canary = get_random_int(); #endif /* * One for us, one for whoever does the "release_task()" (usually * parent) */ atomic_set(&tsk->usage, 2); #ifdef CONFIG_BLK_DEV_IO_TRACE tsk->btrace_seq = 0; #endif tsk->splice_pipe = NULL; tsk->task_frag.page = NULL; account_kernel_stack(ti, 1); return tsk; free_ti: free_thread_info(ti); free_tsk: free_task_struct(tsk); return NULL; } 12 November 2013 University of Virginia cs4414 32
  • 34. static struct task_struct *copy_process(...) { ... p = dup_task_struct(current); ... /* Perform scheduler related setup. Assign this task to a CPU. */ sched_fork(p); ... } kernel/sched/core.c 12 November 2013 University of Virginia cs4414 33
  • 35. 12 November 2013 University of Virginia cs4414 34
  • 36. 12 November 2013 University of Virginia cs4414 35
  • 39. static struct task_struct *copy_process(...) { ... p = dup_task_struct(current); ... /* Perform scheduler related setup. Assign this task to a CPU. */ sched_fork(p); ... retval = copy_mm(clone_flags, p); ... } static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) { struct mm_struct *mm, *oldmm; int retval; ... mm = dup_mm(tsk); if (!mm) goto fail_nomem; good_mm: tsk->mm = mm; tsk->active_mm = mm; return 0; … 12 November 2013 University of Virginia cs4414 38
  • 40. /* * Allocate a new mm structure and copy contents from the * mm structure of the passed in task structure. */ struct mm_struct *dup_mm(struct task_struct *tsk) { struct mm_struct *mm, *oldmm = current->mm; int err; if (!oldmm) return NULL; mm = allocate_mm(); if (!mm) goto fail_nomem; memcpy(mm, oldmm, sizeof(*mm)); ... #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 12 November 2013 University of Virginia cs4414 39
  • 41. Three Linux memory allocators: SLOB = “Simple List of Blocks” SLAB = allocation with less fragmentation SLUB = less fragmentation, better reuse (Default) 12 November 2013 University of Virginia cs4414 40
  • 42. 12 November 2013 University of Virginia cs4414 41
  • 43. 12 November 2013 University of Virginia cs4414 42
  • 44. 12 November 2013 University of Virginia cs4414 43
  • 45. 12 November 2013 University of Virginia cs4414 44
  • 46. 12 November 2013 University of Virginia cs4414 45
  • 48. 12 November 2013 University of Virginia cs4414 47
  • 50. Page Table 32-bit linear address CR3 Dir Page 10 bits (1K tables) Page Directory Offset 10 bits 12 bits (1K entries) (4K pages) Page Entry Page Table Physical Memory Page + Offset CR3+Dir 12 November 2013 University of Virginia cs4414 49
  • 51. 12 November 2013 University of Virginia cs4414 50
  • 53. TLB Memory Paging Unit Physical Address Linear Address Logical Address Segmentation Unit 32-bit linear address CR3 What does the kernel need to do to flush the TLB? Dir 10 bits (1K tables) Page 10 bits (1K entries) Offset 12 bits (4K pages) Page Entry Page Directory Page Table CR3+Dir 12 November 2013 University of Virginia cs4414 52
  • 55. Charge Progress updates and scheduling design reviews will be due Sunday 11:59pm Tuesday’s Class: Yuchen Zhou on Authentication using Single Sign-On 12 November 2013 University of Virginia cs4414 54