kaslr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2019
  4. */
  5. #include <linux/pgtable.h>
  6. #include <asm/physmem_info.h>
  7. #include <asm/cpacf.h>
  8. #include <asm/timex.h>
  9. #include <asm/sclp.h>
  10. #include <asm/kasan.h>
  11. #include "decompressor.h"
  12. #include "boot.h"
  13. #define PRNG_MODE_TDES 1
  14. #define PRNG_MODE_SHA512 2
  15. #define PRNG_MODE_TRNG 3
  16. struct prno_parm {
  17. u32 res;
  18. u32 reseed_counter;
  19. u64 stream_bytes;
  20. u8 V[112];
  21. u8 C[112];
  22. };
  23. struct prng_parm {
  24. u8 parm_block[32];
  25. u32 reseed_counter;
  26. u64 byte_counter;
  27. };
  28. static int check_prng(void)
  29. {
  30. if (!cpacf_query_func(CPACF_KMC, CPACF_KMC_PRNG)) {
  31. boot_printk("KASLR disabled: CPU has no PRNG\n");
  32. return 0;
  33. }
  34. if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG))
  35. return PRNG_MODE_TRNG;
  36. if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_SHA512_DRNG_GEN))
  37. return PRNG_MODE_SHA512;
  38. else
  39. return PRNG_MODE_TDES;
  40. }
  41. int get_random(unsigned long limit, unsigned long *value)
  42. {
  43. struct prng_parm prng = {
  44. /* initial parameter block for tdes mode, copied from libica */
  45. .parm_block = {
  46. 0x0F, 0x2B, 0x8E, 0x63, 0x8C, 0x8E, 0xD2, 0x52,
  47. 0x64, 0xB7, 0xA0, 0x7B, 0x75, 0x28, 0xB8, 0xF4,
  48. 0x75, 0x5F, 0xD2, 0xA6, 0x8D, 0x97, 0x11, 0xFF,
  49. 0x49, 0xD8, 0x23, 0xF3, 0x7E, 0x21, 0xEC, 0xA0
  50. },
  51. };
  52. unsigned long seed, random;
  53. struct prno_parm prno;
  54. __u64 entropy[4];
  55. int mode, i;
  56. mode = check_prng();
  57. seed = get_tod_clock_fast();
  58. switch (mode) {
  59. case PRNG_MODE_TRNG:
  60. cpacf_trng(NULL, 0, (u8 *) &random, sizeof(random));
  61. break;
  62. case PRNG_MODE_SHA512:
  63. cpacf_prno(CPACF_PRNO_SHA512_DRNG_SEED, &prno, NULL, 0,
  64. (u8 *) &seed, sizeof(seed));
  65. cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN, &prno, (u8 *) &random,
  66. sizeof(random), NULL, 0);
  67. break;
  68. case PRNG_MODE_TDES:
  69. /* add entropy */
  70. *(unsigned long *) prng.parm_block ^= seed;
  71. for (i = 0; i < 16; i++) {
  72. cpacf_kmc(CPACF_KMC_PRNG, prng.parm_block,
  73. (u8 *) entropy, (u8 *) entropy,
  74. sizeof(entropy));
  75. memcpy(prng.parm_block, entropy, sizeof(entropy));
  76. }
  77. random = seed;
  78. cpacf_kmc(CPACF_KMC_PRNG, prng.parm_block, (u8 *) &random,
  79. (u8 *) &random, sizeof(random));
  80. break;
  81. default:
  82. return -1;
  83. }
  84. *value = random % limit;
  85. return 0;
  86. }
  87. static void sort_reserved_ranges(struct reserved_range *res, unsigned long size)
  88. {
  89. struct reserved_range tmp;
  90. int i, j;
  91. for (i = 1; i < size; i++) {
  92. tmp = res[i];
  93. for (j = i - 1; j >= 0 && res[j].start > tmp.start; j--)
  94. res[j + 1] = res[j];
  95. res[j + 1] = tmp;
  96. }
  97. }
  98. static unsigned long iterate_valid_positions(unsigned long size, unsigned long align,
  99. unsigned long _min, unsigned long _max,
  100. struct reserved_range *res, size_t res_count,
  101. bool pos_count, unsigned long find_pos)
  102. {
  103. unsigned long start, end, tmp_end, range_pos, pos = 0;
  104. struct reserved_range *res_end = res + res_count;
  105. struct reserved_range *skip_res;
  106. int i;
  107. align = max(align, 8UL);
  108. _min = round_up(_min, align);
  109. for_each_physmem_usable_range(i, &start, &end) {
  110. if (_min >= end)
  111. continue;
  112. start = round_up(start, align);
  113. if (start >= _max)
  114. break;
  115. start = max(_min, start);
  116. end = min(_max, end);
  117. while (start + size <= end) {
  118. /* skip reserved ranges below the start */
  119. while (res && res->end <= start) {
  120. res++;
  121. if (res >= res_end)
  122. res = NULL;
  123. }
  124. skip_res = NULL;
  125. tmp_end = end;
  126. /* has intersecting reserved range */
  127. if (res && res->start < end) {
  128. skip_res = res;
  129. tmp_end = res->start;
  130. }
  131. if (start + size <= tmp_end) {
  132. range_pos = (tmp_end - start - size) / align + 1;
  133. if (pos_count) {
  134. pos += range_pos;
  135. } else {
  136. if (range_pos >= find_pos)
  137. return start + (find_pos - 1) * align;
  138. find_pos -= range_pos;
  139. }
  140. }
  141. if (!skip_res)
  142. break;
  143. start = round_up(skip_res->end, align);
  144. }
  145. }
  146. return pos_count ? pos : 0;
  147. }
  148. /*
  149. * Two types of decompressor memory allocations/reserves are considered
  150. * differently.
  151. *
  152. * "Static" or "single" allocations are done via physmem_alloc_range() and
  153. * physmem_reserve(), and they are listed in physmem_info.reserved[]. Each
  154. * type of "static" allocation can only have one allocation per type and
  155. * cannot have chains.
  156. *
  157. * On the other hand, "dynamic" or "repetitive" allocations are done via
  158. * physmem_alloc_top_down(). These allocations are tightly packed together
  159. * top down from the end of online memory. physmem_alloc_pos represents
  160. * current position where those allocations start.
  161. *
  162. * Functions randomize_within_range() and iterate_valid_positions()
  163. * only consider "dynamic" allocations by never looking above
  164. * physmem_alloc_pos. "Static" allocations, however, are explicitly
  165. * considered by checking the "res" (reserves) array. The first
  166. * reserved_range of a "dynamic" allocation may also be checked along the
  167. * way, but it will always be above the maximum value anyway.
  168. */
  169. unsigned long randomize_within_range(unsigned long size, unsigned long align,
  170. unsigned long min, unsigned long max)
  171. {
  172. struct reserved_range res[RR_MAX];
  173. unsigned long max_pos, pos;
  174. memcpy(res, physmem_info.reserved, sizeof(res));
  175. sort_reserved_ranges(res, ARRAY_SIZE(res));
  176. max = min(max, get_physmem_alloc_pos());
  177. max_pos = iterate_valid_positions(size, align, min, max, res, ARRAY_SIZE(res), true, 0);
  178. if (!max_pos)
  179. return 0;
  180. if (get_random(max_pos, &pos))
  181. return 0;
  182. return iterate_valid_positions(size, align, min, max, res, ARRAY_SIZE(res), false, pos + 1);
  183. }