ia32_ldt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2001, 2004 Hewlett-Packard Co
  3. * David Mosberger-Tang <davidm@hpl.hp.com>
  4. *
  5. * Adapted from arch/i386/kernel/ldt.c
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/sched.h>
  9. #include <linux/string.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/vmalloc.h>
  14. #include <asm/uaccess.h>
  15. #include "ia32priv.h"
  16. /*
  17. * read_ldt() is not really atomic - this is not a problem since synchronization of reads
  18. * and writes done to the LDT has to be assured by user-space anyway. Writes are atomic,
  19. * to protect the security checks done on new descriptors.
  20. */
  21. static int
  22. read_ldt (void __user *ptr, unsigned long bytecount)
  23. {
  24. unsigned long bytes_left, n;
  25. char __user *src, *dst;
  26. char buf[256]; /* temporary buffer (don't overflow kernel stack!) */
  27. if (bytecount > IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE)
  28. bytecount = IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE;
  29. bytes_left = bytecount;
  30. src = (void __user *) IA32_LDT_OFFSET;
  31. dst = ptr;
  32. while (bytes_left) {
  33. n = sizeof(buf);
  34. if (n > bytes_left)
  35. n = bytes_left;
  36. /*
  37. * We know we're reading valid memory, but we still must guard against
  38. * running out of memory.
  39. */
  40. if (__copy_from_user(buf, src, n))
  41. return -EFAULT;
  42. if (copy_to_user(dst, buf, n))
  43. return -EFAULT;
  44. src += n;
  45. dst += n;
  46. bytes_left -= n;
  47. }
  48. return bytecount;
  49. }
  50. static int
  51. read_default_ldt (void __user * ptr, unsigned long bytecount)
  52. {
  53. unsigned long size;
  54. int err;
  55. /* XXX fix me: should return equivalent of default_ldt[0] */
  56. err = 0;
  57. size = 8;
  58. if (size > bytecount)
  59. size = bytecount;
  60. err = size;
  61. if (clear_user(ptr, size))
  62. err = -EFAULT;
  63. return err;
  64. }
  65. static int
  66. write_ldt (void __user * ptr, unsigned long bytecount, int oldmode)
  67. {
  68. struct ia32_user_desc ldt_info;
  69. __u64 entry;
  70. int ret;
  71. if (bytecount != sizeof(ldt_info))
  72. return -EINVAL;
  73. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  74. return -EFAULT;
  75. if (ldt_info.entry_number >= IA32_LDT_ENTRIES)
  76. return -EINVAL;
  77. if (ldt_info.contents == 3) {
  78. if (oldmode)
  79. return -EINVAL;
  80. if (ldt_info.seg_not_present == 0)
  81. return -EINVAL;
  82. }
  83. if (ldt_info.base_addr == 0 && ldt_info.limit == 0
  84. && (oldmode || (ldt_info.contents == 0 && ldt_info.read_exec_only == 1
  85. && ldt_info.seg_32bit == 0 && ldt_info.limit_in_pages == 0
  86. && ldt_info.seg_not_present == 1 && ldt_info.useable == 0)))
  87. /* allow LDTs to be cleared by the user */
  88. entry = 0;
  89. else
  90. /* we must set the "Accessed" bit as IVE doesn't emulate it */
  91. entry = IA32_SEG_DESCRIPTOR(ldt_info.base_addr, ldt_info.limit,
  92. (((ldt_info.read_exec_only ^ 1) << 1)
  93. | (ldt_info.contents << 2)) | 1,
  94. 1, 3, ldt_info.seg_not_present ^ 1,
  95. (oldmode ? 0 : ldt_info.useable),
  96. ldt_info.seg_32bit,
  97. ldt_info.limit_in_pages);
  98. /*
  99. * Install the new entry. We know we're accessing valid (mapped) user-level
  100. * memory, but we still need to guard against out-of-memory, hence we must use
  101. * put_user().
  102. */
  103. ret = __put_user(entry, (__u64 __user *) IA32_LDT_OFFSET + ldt_info.entry_number);
  104. ia32_load_segment_descriptors(current);
  105. return ret;
  106. }
  107. asmlinkage int
  108. sys32_modify_ldt (int func, unsigned int ptr, unsigned int bytecount)
  109. {
  110. int ret = -ENOSYS;
  111. switch (func) {
  112. case 0:
  113. ret = read_ldt(compat_ptr(ptr), bytecount);
  114. break;
  115. case 1:
  116. ret = write_ldt(compat_ptr(ptr), bytecount, 1);
  117. break;
  118. case 2:
  119. ret = read_default_ldt(compat_ptr(ptr), bytecount);
  120. break;
  121. case 0x11:
  122. ret = write_ldt(compat_ptr(ptr), bytecount, 0);
  123. break;
  124. }
  125. return ret;
  126. }