Skip to content

Commit 858259c

Browse files
Bodo StroesserLinus Torvalds
Bodo Stroesser
authored and
Linus Torvalds
committed
[PATCH] uml: maintain own LDT entries
Patch imlements full LDT handling in SKAS: * UML holds it's own LDT table, used to deliver data on modify_ldt(READ) * UML disables the default_ldt, inherited from the host (SKAS3) or resets LDT entries, set by host's clib and inherited in SKAS0 * A new global variable skas_needs_stub is inserted, that can be used to decide, whether stub-pages must be supported or not. * Uses the syscall-stub to replace missing PTRACE_LDT (therefore, write_ldt_entry needs to be modified) Signed-off-by: Bodo Stroesser <[email protected]> Signed-off-by: Jeff Dike <[email protected]> Cc: Paolo Giarrusso <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent e763b79 commit 858259c

File tree

13 files changed

+724
-141
lines changed

13 files changed

+724
-141
lines changed

arch/um/kernel/skas/include/mmu-skas.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
#include "linux/config.h"
1010
#include "mm_id.h"
11+
#include "asm/ldt.h"
1112

1213
struct mmu_context_skas {
1314
struct mm_id id;
1415
unsigned long last_page_table;
1516
#ifdef CONFIG_3_LEVEL_PGTABLES
1617
unsigned long last_pmd;
1718
#endif
19+
uml_ldt_t ldt;
1820
};
1921

2022
extern void switch_mm_skas(struct mm_id * mm_idp);

arch/um/kernel/skas/include/skas.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include "sysdep/ptrace.h"
1111

1212
extern int userspace_pid[];
13-
extern int proc_mm, ptrace_faultinfo;
13+
extern int proc_mm, ptrace_faultinfo, ptrace_ldt;
14+
extern int skas_needs_stub;
1415

1516
extern void switch_threads(void *me, void *next);
1617
extern void thread_wait(void *sw, void *fb);

arch/um/kernel/skas/mem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ unsigned long set_task_sizes_skas(int arg, unsigned long *host_size_out,
2020
*task_size_out = CONFIG_HOST_TASK_SIZE;
2121
#else
2222
*host_size_out = top;
23-
if (proc_mm && ptrace_faultinfo)
23+
if (!skas_needs_stub)
2424
*task_size_out = top;
2525
else *task_size_out = CONFIG_STUB_START & PGDIR_MASK;
2626
#endif

arch/um/kernel/skas/mmu.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "asm/mmu.h"
1616
#include "asm/pgalloc.h"
1717
#include "asm/pgtable.h"
18+
#include "asm/ldt.h"
1819
#include "os.h"
1920
#include "skas.h"
2021

@@ -74,13 +75,12 @@ static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
7475

7576
int init_new_context_skas(struct task_struct *task, struct mm_struct *mm)
7677
{
77-
struct mm_struct *cur_mm = current->mm;
78-
struct mm_id *cur_mm_id = &cur_mm->context.skas.id;
79-
struct mm_id *mm_id = &mm->context.skas.id;
78+
struct mmu_context_skas *from_mm = NULL;
79+
struct mmu_context_skas *to_mm = &mm->context.skas;
8080
unsigned long stack = 0;
81-
int from, ret = -ENOMEM;
81+
int from_fd, ret = -ENOMEM;
8282

83-
if(!proc_mm || !ptrace_faultinfo){
83+
if(skas_needs_stub){
8484
stack = get_zeroed_page(GFP_KERNEL);
8585
if(stack == 0)
8686
goto out;
@@ -102,33 +102,43 @@ int init_new_context_skas(struct task_struct *task, struct mm_struct *mm)
102102

103103
mm->nr_ptes--;
104104
}
105-
mm_id->stack = stack;
105+
106+
to_mm->id.stack = stack;
107+
if(current->mm != NULL && current->mm != &init_mm)
108+
from_mm = &current->mm->context.skas;
106109

107110
if(proc_mm){
108-
if((cur_mm != NULL) && (cur_mm != &init_mm))
109-
from = cur_mm_id->u.mm_fd;
110-
else from = -1;
111+
if(from_mm)
112+
from_fd = from_mm->id.u.mm_fd;
113+
else from_fd = -1;
111114

112-
ret = new_mm(from, stack);
115+
ret = new_mm(from_fd, stack);
113116
if(ret < 0){
114117
printk("init_new_context_skas - new_mm failed, "
115118
"errno = %d\n", ret);
116119
goto out_free;
117120
}
118-
mm_id->u.mm_fd = ret;
121+
to_mm->id.u.mm_fd = ret;
119122
}
120123
else {
121-
if((cur_mm != NULL) && (cur_mm != &init_mm))
122-
mm_id->u.pid = copy_context_skas0(stack,
123-
cur_mm_id->u.pid);
124-
else mm_id->u.pid = start_userspace(stack);
124+
if(from_mm)
125+
to_mm->id.u.pid = copy_context_skas0(stack,
126+
from_mm->id.u.pid);
127+
else to_mm->id.u.pid = start_userspace(stack);
128+
}
129+
130+
ret = init_new_ldt(to_mm, from_mm);
131+
if(ret < 0){
132+
printk("init_new_context_skas - init_ldt"
133+
" failed, errno = %d\n", ret);
134+
goto out_free;
125135
}
126136

127137
return 0;
128138

129139
out_free:
130-
if(mm_id->stack != 0)
131-
free_page(mm_id->stack);
140+
if(to_mm->id.stack != 0)
141+
free_page(to_mm->id.stack);
132142
out:
133143
return ret;
134144
}

arch/um/kernel/skas/process.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,9 @@ int copy_context_skas0(unsigned long new_stack, int pid)
381381
}
382382

383383
/*
384-
* This is used only, if proc_mm is available, while PTRACE_FAULTINFO
385-
* isn't. Opening /proc/mm creates a new mm_context, which lacks the stub-pages
386-
* Thus, we map them using /proc/mm-fd
384+
* This is used only, if stub pages are needed, while proc_mm is
385+
* availabl. Opening /proc/mm creates a new mm_context, which lacks
386+
* the stub-pages. Thus, we map them using /proc/mm-fd
387387
*/
388388
void map_stub_pages(int fd, unsigned long code,
389389
unsigned long data, unsigned long stack)

arch/um/kernel/skas/process_kern.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ int new_mm(int from, unsigned long stack)
145145
"err = %d\n", -n);
146146
}
147147

148-
if(!ptrace_faultinfo)
148+
if(skas_needs_stub)
149149
map_stub_pages(fd, CONFIG_STUB_CODE, CONFIG_STUB_DATA, stack);
150150

151151
return(fd);

arch/um/os-Linux/start_up.c

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ static int stop_ptraced_child(int pid, void *stack, int exitcode,
135135
}
136136

137137
int ptrace_faultinfo = 1;
138+
int ptrace_ldt = 1;
138139
int proc_mm = 1;
140+
int skas_needs_stub = 0;
139141

140142
static int __init skas0_cmd_param(char *str, int* add)
141143
{
@@ -352,14 +354,26 @@ __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
352354
" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
353355
" using the current skas3 patch.\n\n");
354356

357+
static int __init noptraceldt_cmd_param(char *str, int* add)
358+
{
359+
ptrace_ldt = 0;
360+
return 0;
361+
}
362+
363+
__uml_setup("noptraceldt", noptraceldt_cmd_param,
364+
"noptraceldt\n"
365+
" Turns off usage of PTRACE_LDT, even if host supports it.\n"
366+
" To support PTRACE_LDT, the host needs to be patched using\n"
367+
" the current skas3 patch.\n\n");
368+
355369
#ifdef UML_CONFIG_MODE_SKAS
356-
static inline void check_skas3_ptrace_support(void)
370+
static inline void check_skas3_ptrace_faultinfo(void)
357371
{
358372
struct ptrace_faultinfo fi;
359373
void *stack;
360374
int pid, n;
361375

362-
printf("Checking for the skas3 patch in the host...");
376+
printf(" - PTRACE_FAULTINFO...");
363377
pid = start_ptraced_child(&stack);
364378

365379
n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
@@ -381,9 +395,49 @@ static inline void check_skas3_ptrace_support(void)
381395
stop_ptraced_child(pid, stack, 1, 1);
382396
}
383397

384-
int can_do_skas(void)
398+
static inline void check_skas3_ptrace_ldt(void)
399+
{
400+
#ifdef PTRACE_LDT
401+
void *stack;
402+
int pid, n;
403+
unsigned char ldtbuf[40];
404+
struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
405+
.func = 2, /* read default ldt */
406+
.ptr = ldtbuf,
407+
.bytecount = sizeof(ldtbuf)};
408+
409+
printf(" - PTRACE_LDT...");
410+
pid = start_ptraced_child(&stack);
411+
412+
n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
413+
if (n < 0) {
414+
if(errno == EIO)
415+
printf("not found\n");
416+
else {
417+
perror("not found");
418+
}
419+
ptrace_ldt = 0;
420+
}
421+
else {
422+
if(ptrace_ldt)
423+
printf("found\n");
424+
else
425+
printf("found, but use is disabled\n");
426+
}
427+
428+
stop_ptraced_child(pid, stack, 1, 1);
429+
#else
430+
/* PTRACE_LDT might be disabled via cmdline option.
431+
* We want to override this, else we might use the stub
432+
* without real need
433+
*/
434+
ptrace_ldt = 1;
435+
#endif
436+
}
437+
438+
static inline void check_skas3_proc_mm(void)
385439
{
386-
printf("Checking for /proc/mm...");
440+
printf(" - /proc/mm...");
387441
if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
388442
proc_mm = 0;
389443
printf("not found\n");
@@ -394,8 +448,19 @@ int can_do_skas(void)
394448
else
395449
printf("found\n");
396450
}
451+
}
452+
453+
int can_do_skas(void)
454+
{
455+
printf("Checking for the skas3 patch in the host:\n");
456+
457+
check_skas3_proc_mm();
458+
check_skas3_ptrace_faultinfo();
459+
check_skas3_ptrace_ldt();
460+
461+
if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
462+
skas_needs_stub = 1;
397463

398-
check_skas3_ptrace_support();
399464
return 1;
400465
}
401466
#else

arch/um/scripts/Makefile.rules

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ define unprofile
2626
$(patsubst -pg,,$(patsubst -fprofile-arcs -ftest-coverage,,$(1)))
2727
endef
2828

29+
# cmd_make_link checks to see if the $(foo-dir) variable starts with a /. If
30+
# so, it's considered to be a path relative to $(srcdir) rather than
31+
# $(srcdir)/arch/$(SUBARCH). This is because x86_64 wants to get ldt.c from
32+
# arch/um/sys-i386 rather than arch/i386 like the other borrowed files. So,
33+
# it sets $(ldt.c-dir) to /arch/um/sys-i386.
2934
quiet_cmd_make_link = SYMLINK $@
30-
cmd_make_link = ln -sf $(srctree)/arch/$(SUBARCH)/$($(notdir $@)-dir)/$(notdir $@) $@
35+
cmd_make_link = rm -f $@; ln -sf $(srctree)$(if $(filter-out /%,$($(notdir $@)-dir)),/arch/$(SUBARCH))/$($(notdir $@)-dir)/$(notdir $@) $@
3136

3237
# this needs to be before the foreach, because targets does not accept
3338
# complete paths like $(obj)/$(f). To make sure this works, use a := assignment

0 commit comments

Comments
 (0)