Release 4.14 arch/x86/kernel/ldt.c
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
* Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
* Copyright (C) 2002 Andi Kleen
*
* This handles calls from both 32bit and 64bit mode.
*/
#include <linux/errno.h>
#include <linux/gfp.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
#include <asm/ldt.h>
#include <asm/desc.h>
#include <asm/mmu_context.h>
#include <asm/syscalls.h>
static void refresh_ldt_segments(void)
{
#ifdef CONFIG_X86_64
unsigned short sel;
/*
* Make sure that the cached DS and ES descriptors match the updated
* LDT.
*/
savesegment(ds, sel);
if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
loadsegment(ds, sel);
savesegment(es, sel);
if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
loadsegment(es, sel);
#endif
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Lutomirski | 66 | 100.00% | 1 | 100.00% |
Total | 66 | 100.00% | 1 | 100.00% |
/* context.lock is held for us, so we don't need any locking. */
static void flush_ldt(void *__mm)
{
struct mm_struct *mm = __mm;
mm_context_t *pc;
if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
return;
pc = &mm->context;
set_ldt(pc->ldt->entries, pc->ldt->nr_entries);
refresh_ldt_segments();
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Lutomirski | 40 | 68.97% | 3 | 60.00% |
Dave Jones | 17 | 29.31% | 1 | 20.00% |
Borislav Petkov | 1 | 1.72% | 1 | 20.00% |
Total | 58 | 100.00% | 5 | 100.00% |
/* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
{
struct ldt_struct *new_ldt;
unsigned int alloc_size;
if (num_entries > LDT_ENTRIES)
return NULL;
new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
if (!new_ldt)
return NULL;
BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
alloc_size = num_entries * LDT_ENTRY_SIZE;
/*
* Xen is very picky: it requires a page-aligned LDT that has no
* trailing nonzero bytes in any page that contains LDT descriptors.
* Keep it simple: zero the whole allocation and never allocate less
* than PAGE_SIZE.
*/
if (alloc_size > PAGE_SIZE)
new_ldt->entries = vzalloc(alloc_size);
else
new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
if (!new_ldt->entries) {
kfree(new_ldt);
return NULL;
}
new_ldt->nr_entries = num_entries;
return new_ldt;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Lutomirski | 69 | 55.65% | 1 | 12.50% |
Dave Jones | 40 | 32.26% | 1 | 12.50% |
Jan Beulich | 6 | 4.84% | 2 | 25.00% |
Borislav Petkov | 5 | 4.03% | 1 | 12.50% |
Cyrill V. Gorcunov | 2 | 1.61% | 1 | 12.50% |
Dan Carpenter | 1 | 0.81% | 1 | 12.50% |
Thomas Gleixner | 1 | 0.81% | 1 | 12.50% |
Total | 124 | 100.00% | 8 | 100.00% |
/* After calling this, the LDT is immutable. */
static void finalize_ldt_struct(struct ldt_struct *ldt)
{
paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Lutomirski | 15 | 68.18% | 1 | 20.00% |
Dave Jones | 3 | 13.64% | 1 | 20.00% |
Jeremy Fitzhardinge | 2 | 9.09% | 1 | 20.00% |
Borislav Petkov | 1 | 4.55% | 1 | 20.00% |
Ingo Molnar | 1 | 4.55% | 1 | 20.00% |
Total | 22 | 100.00% | 5 | 100.00% |
/* context.lock is held */
static void install_ldt(struct mm_struct *current_mm,
struct ldt_struct *ldt)
{
/* Synchronizes with lockless_dereference in load_mm_ldt. */
smp_store_release(¤t_mm->context.ldt, ldt);
/* Activate the LDT for all CPUs using current_mm. */
on_each_cpu_mask(mm_cpumask(current_mm), flush_ldt, current_mm, true);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Lutomirski | 29 | 65.91% | 1 | 20.00% |
Andrew Morton | 7 | 15.91% | 2 | 40.00% |
Dave Jones | 5 | 11.36% | 1 | 20.00% |
Rusty Russell | 3 | 6.82% | 1 | 20.00% |
Total | 44 | 100.00% | 5 | 100.00% |
static void free_ldt_struct(struct ldt_struct *ldt)
{
if (likely(!ldt))
return;
paravirt_free_ldt(ldt->entries, ldt->nr_entries);
if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
vfree_atomic(ldt->entries);
else
free_page((unsigned long)ldt->entries);
kfree(ldt);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Lutomirski | 30 | 46.15% | 1 | 16.67% |
Dave Jones | 21 | 32.31% | 1 | 16.67% |
Jeremy Fitzhardinge | 6 | 9.23% | 1 | 16.67% |
Jan Beulich | 5 | 7.69% | 1 | 16.67% |
Borislav Petkov | 2 | 3.08% | 1 | 16.67% |
Andrey Ryabinin | 1 | 1.54% | 1 | 16.67% |
Total | 65 | 100.00% | 6 | 100.00% |
/*
* we do not have to muck with descriptors here, that is
* done in switch_mm() as needed.
*/
int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
{
struct ldt_struct *new_ldt;
struct mm_struct *old_mm;
int retval = 0;
mutex_init(&mm->context.lock);
old_mm = current->mm;
if (!old_mm) {
mm->context.ldt = NULL;
return 0;
}
mutex_lock(&old_mm->context.lock);
if (!old_mm->context.ldt) {
mm->context.ldt = NULL;
goto out_unlock;
}
new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
if (!new_ldt) {
retval = -ENOMEM;
goto out_unlock;
}
memcpy(new_ldt->entries, old_mm->context.ldt->entries,
new_ldt->nr_entries * LDT_ENTRY_SIZE);
finalize_ldt_struct(new_ldt);
mm->context.ldt = new_ldt;
out_unlock:
mutex_unlock(&old_mm->context.lock);
return retval;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Lutomirski | 89 | 51.45% | 1 | 20.00% |
Dave Jones | 77 | 44.51% | 1 | 20.00% |
Luiz Fernando N. Capitulino | 4 | 2.31% | 1 | 20.00% |
Borislav Petkov | 2 | 1.16% | 1 | 20.00% |
Dave Hansen | 1 | 0.58% | 1 | 20.00% |
Total | 173 | 100.00% | 5 | 100.00% |
/*
* No need to lock the MM as we are the last user
*
* 64bit: Don't touch the LDT register - we're already in the next thread.
*/
void destroy_context_ldt(struct mm_struct *mm)
{
free_ldt_struct(mm->context.ldt);
mm->context.ldt = NULL;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Dave Jones | 17 | 62.96% | 1 | 33.33% |
Andrew Lutomirski | 9 | 33.33% | 1 | 33.33% |
Dave Hansen | 1 | 3.70% | 1 | 33.33% |
Total | 27 | 100.00% | 3 | 100.00% |
static int read_ldt(void __user *ptr, unsigned long bytecount)
{
struct mm_struct *mm = current->mm;
unsigned long entries_size;
int retval;
mutex_lock(&mm->context.lock);
if (!mm->context.ldt) {
retval = 0;
goto out_unlock;
}
if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
if (entries_size > bytecount)
entries_size = bytecount;
if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
retval = -EFAULT;
goto out_unlock;
}
if (entries_size != bytecount) {
/* Zero-fill the rest and pretend we read bytecount bytes. */
if (clear_user(ptr + entries_size, bytecount - entries_size)) {
retval = -EFAULT;
goto out_unlock;
}
}
retval = bytecount;
out_unlock:
mutex_unlock(&mm->context.lock);
return retval;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds (pre-git) | 60 | 34.68% | 7 | 58.33% |
Andrew Lutomirski | 45 | 26.01% | 1 | 8.33% |
Dave Jones | 37 | 21.39% | 1 | 8.33% |
Jesper Juhl | 15 | 8.67% | 1 | 8.33% |
Borislav Petkov | 15 | 8.67% | 1 | 8.33% |
Linus Torvalds | 1 | 0.58% | 1 | 8.33% |
Total | 173 | 100.00% | 12 | 100.00% |
static int read_default_ldt(void __user *ptr, unsigned long bytecount)
{
/* CHECKME: Can we use _one_ random number ? */
#ifdef CONFIG_X86_32
unsigned long size = 5 * sizeof(struct desc_struct);
#else
unsigned long size = 128;
#endif
if (bytecount > size)
bytecount = size;
if (clear_user(ptr, bytecount))
return -EFAULT;
return bytecount;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds | 42 | 62.69% | 2 | 40.00% |
Thomas Gleixner | 22 | 32.84% | 1 | 20.00% |
Dave Jones | 2 | 2.99% | 1 | 20.00% |
Jeremy Fitzhardinge | 1 | 1.49% | 1 | 20.00% |
Total | 67 | 100.00% | 5 | 100.00% |
static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
{
struct mm_struct *mm = current->mm;
struct ldt_struct *new_ldt, *old_ldt;
unsigned int old_nr_entries, new_nr_entries;
struct user_desc ldt_info;
struct desc_struct ldt;
int error;
error = -EINVAL;
if (bytecount != sizeof(ldt_info))
goto out;
error = -EFAULT;
if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
goto out;
error = -EINVAL;
if (ldt_info.entry_number >= LDT_ENTRIES)
goto out;
if (ldt_info.contents == 3) {
if (oldmode)
goto out;
if (ldt_info.seg_not_present == 0)
goto out;
}
if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
LDT_empty(&ldt_info)) {
/* The user wants to clear the entry. */
memset(&ldt, 0, sizeof(ldt));
} else {
if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
error = -EINVAL;
goto out;
}
fill_ldt(&ldt, &ldt_info);
if (oldmode)
ldt.avl = 0;
}
mutex_lock(&mm->context.lock);
old_ldt = mm->context.ldt;
old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
error = -ENOMEM;
new_ldt = alloc_ldt_struct(new_nr_entries);
if (!new_ldt)
goto out_unlock;
if (old_ldt)
memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);
new_ldt->entries[ldt_info.entry_number] = ldt;
finalize_ldt_struct(new_ldt);
install_ldt(mm, new_ldt);
free_ldt_struct(old_ldt);
error = 0;
out_unlock:
mutex_unlock(&mm->context.lock);
out:
return error;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds (pre-git) | 159 | 45.69% | 6 | 35.29% |
Andrew Lutomirski | 102 | 29.31% | 1 | 5.88% |
H. Peter Anvin | 22 | 6.32% | 1 | 5.88% |
Glauber de Oliveira Costa | 21 | 6.03% | 2 | 11.76% |
Thomas Gleixner | 13 | 3.74% | 1 | 5.88% |
Zachary Amsden | 11 | 3.16% | 1 | 5.88% |
Borislav Petkov | 8 | 2.30% | 1 | 5.88% |
Ingo Molnar | 7 | 2.01% | 1 | 5.88% |
Luiz Fernando N. Capitulino | 2 | 0.57% | 1 | 5.88% |
Dave Jones | 2 | 0.57% | 1 | 5.88% |
Linus Torvalds | 1 | 0.29% | 1 | 5.88% |
Total | 348 | 100.00% | 17 | 100.00% |
asmlinkage int sys_modify_ldt(int func, void __user *ptr,
unsigned long bytecount)
{
int ret = -ENOSYS;
switch (func) {
case 0:
ret = read_ldt(ptr, bytecount);
break;
case 1:
ret = write_ldt(ptr, bytecount, 1);
break;
case 2:
ret = read_default_ldt(ptr, bytecount);
break;
case 0x11:
ret = write_ldt(ptr, bytecount, 0);
break;
}
return ret;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds (pre-git) | 75 | 84.27% | 5 | 71.43% |
Linus Torvalds | 14 | 15.73% | 2 | 28.57% |
Total | 89 | 100.00% | 7 | 100.00% |
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Lutomirski | 501 | 38.45% | 3 | 6.25% |
Linus Torvalds (pre-git) | 320 | 24.56% | 15 | 31.25% |
Dave Jones | 222 | 17.04% | 1 | 2.08% |
Linus Torvalds | 58 | 4.45% | 2 | 4.17% |
Thomas Gleixner | 38 | 2.92% | 2 | 4.17% |
Borislav Petkov | 34 | 2.61% | 1 | 2.08% |
H. Peter Anvin | 22 | 1.69% | 1 | 2.08% |
Glauber de Oliveira Costa | 21 | 1.61% | 2 | 4.17% |
Jesper Juhl | 15 | 1.15% | 1 | 2.08% |
Zachary Amsden | 11 | 0.84% | 1 | 2.08% |
Jan Beulich | 11 | 0.84% | 2 | 4.17% |
Jeremy Fitzhardinge | 9 | 0.69% | 2 | 4.17% |
Ingo Molnar | 8 | 0.61% | 2 | 4.17% |
Andrew Morton | 7 | 0.54% | 2 | 4.17% |
Luiz Fernando N. Capitulino | 6 | 0.46% | 1 | 2.08% |
Jaswinder Singh Rajput | 4 | 0.31% | 2 | 4.17% |
Tejun Heo | 3 | 0.23% | 1 | 2.08% |
Rusty Russell | 3 | 0.23% | 1 | 2.08% |
Adrian Bunk | 3 | 0.23% | 1 | 2.08% |
Cyrill V. Gorcunov | 2 | 0.15% | 1 | 2.08% |
Dave Hansen | 2 | 0.15% | 1 | 2.08% |
Greg Kroah-Hartman | 1 | 0.08% | 1 | 2.08% |
Andrey Ryabinin | 1 | 0.08% | 1 | 2.08% |
Dan Carpenter | 1 | 0.08% | 1 | 2.08% |
Total | 1303 | 100.00% | 48 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.