ia32_ldt.c 3.5 KB

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