gss_krb5_wrap.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include <linux/types.h>
  2. #include <linux/slab.h>
  3. #include <linux/jiffies.h>
  4. #include <linux/sunrpc/gss_krb5.h>
  5. #include <linux/random.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/crypto.h>
  8. #ifdef RPC_DEBUG
  9. # define RPCDBG_FACILITY RPCDBG_AUTH
  10. #endif
  11. static inline int
  12. gss_krb5_padding(int blocksize, int length)
  13. {
  14. /* Most of the code is block-size independent but currently we
  15. * use only 8: */
  16. BUG_ON(blocksize != 8);
  17. return 8 - (length & 7);
  18. }
  19. static inline void
  20. gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize)
  21. {
  22. int padding = gss_krb5_padding(blocksize, buf->len - offset);
  23. char *p;
  24. struct kvec *iov;
  25. if (buf->page_len || buf->tail[0].iov_len)
  26. iov = &buf->tail[0];
  27. else
  28. iov = &buf->head[0];
  29. p = iov->iov_base + iov->iov_len;
  30. iov->iov_len += padding;
  31. buf->len += padding;
  32. memset(p, padding, padding);
  33. }
  34. static inline int
  35. gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
  36. {
  37. u8 *ptr;
  38. u8 pad;
  39. size_t len = buf->len;
  40. if (len <= buf->head[0].iov_len) {
  41. pad = *(u8 *)(buf->head[0].iov_base + len - 1);
  42. if (pad > buf->head[0].iov_len)
  43. return -EINVAL;
  44. buf->head[0].iov_len -= pad;
  45. goto out;
  46. } else
  47. len -= buf->head[0].iov_len;
  48. if (len <= buf->page_len) {
  49. unsigned int last = (buf->page_base + len - 1)
  50. >>PAGE_CACHE_SHIFT;
  51. unsigned int offset = (buf->page_base + len - 1)
  52. & (PAGE_CACHE_SIZE - 1);
  53. ptr = kmap_atomic(buf->pages[last], KM_USER0);
  54. pad = *(ptr + offset);
  55. kunmap_atomic(ptr, KM_USER0);
  56. goto out;
  57. } else
  58. len -= buf->page_len;
  59. BUG_ON(len > buf->tail[0].iov_len);
  60. pad = *(u8 *)(buf->tail[0].iov_base + len - 1);
  61. out:
  62. /* XXX: NOTE: we do not adjust the page lengths--they represent
  63. * a range of data in the real filesystem page cache, and we need
  64. * to know that range so the xdr code can properly place read data.
  65. * However adjusting the head length, as we do above, is harmless.
  66. * In the case of a request that fits into a single page, the server
  67. * also uses length and head length together to determine the original
  68. * start of the request to copy the request for deferal; so it's
  69. * easier on the server if we adjust head and tail length in tandem.
  70. * It's not really a problem that we don't fool with the page and
  71. * tail lengths, though--at worst badly formed xdr might lead the
  72. * server to attempt to parse the padding.
  73. * XXX: Document all these weird requirements for gss mechanism
  74. * wrap/unwrap functions. */
  75. if (pad > blocksize)
  76. return -EINVAL;
  77. if (buf->len > pad)
  78. buf->len -= pad;
  79. else
  80. return -EINVAL;
  81. return 0;
  82. }
  83. static inline void
  84. make_confounder(char *p, int blocksize)
  85. {
  86. static u64 i = 0;
  87. u64 *q = (u64 *)p;
  88. /* rfc1964 claims this should be "random". But all that's really
  89. * necessary is that it be unique. And not even that is necessary in
  90. * our case since our "gssapi" implementation exists only to support
  91. * rpcsec_gss, so we know that the only buffers we will ever encrypt
  92. * already begin with a unique sequence number. Just to hedge my bets
  93. * I'll make a half-hearted attempt at something unique, but ensuring
  94. * uniqueness would mean worrying about atomicity and rollover, and I
  95. * don't care enough. */
  96. BUG_ON(blocksize != 8);
  97. *q = i++;
  98. }
  99. /* Assumptions: the head and tail of inbuf are ours to play with.
  100. * The pages, however, may be real pages in the page cache and we replace
  101. * them with scratch pages from **pages before writing to them. */
  102. /* XXX: obviously the above should be documentation of wrap interface,
  103. * and shouldn't be in this kerberos-specific file. */
  104. /* XXX factor out common code with seal/unseal. */
  105. u32
  106. gss_wrap_kerberos(struct gss_ctx *ctx, int offset,
  107. struct xdr_buf *buf, struct page **pages)
  108. {
  109. struct krb5_ctx *kctx = ctx->internal_ctx_id;
  110. char cksumdata[16];
  111. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  112. int blocksize = 0, plainlen;
  113. unsigned char *ptr, *krb5_hdr, *msg_start;
  114. s32 now;
  115. int headlen;
  116. struct page **tmp_pages;
  117. u32 seq_send;
  118. dprintk("RPC: gss_wrap_kerberos\n");
  119. now = get_seconds();
  120. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  121. gss_krb5_add_padding(buf, offset, blocksize);
  122. BUG_ON((buf->len - offset) % blocksize);
  123. plainlen = blocksize + buf->len - offset;
  124. headlen = g_token_size(&kctx->mech_used, 24 + plainlen) -
  125. (buf->len - offset);
  126. ptr = buf->head[0].iov_base + offset;
  127. /* shift data to make room for header. */
  128. /* XXX Would be cleverer to encrypt while copying. */
  129. /* XXX bounds checking, slack, etc. */
  130. memmove(ptr + headlen, ptr, buf->head[0].iov_len - offset);
  131. buf->head[0].iov_len += headlen;
  132. buf->len += headlen;
  133. BUG_ON((buf->len - offset - headlen) % blocksize);
  134. g_make_token_header(&kctx->mech_used, 24 + plainlen, &ptr);
  135. *ptr++ = (unsigned char) ((KG_TOK_WRAP_MSG>>8)&0xff);
  136. *ptr++ = (unsigned char) (KG_TOK_WRAP_MSG&0xff);
  137. /* ptr now at byte 2 of header described in rfc 1964, section 1.2.1: */
  138. krb5_hdr = ptr - 2;
  139. msg_start = krb5_hdr + 24;
  140. *(__be16 *)(krb5_hdr + 2) = htons(SGN_ALG_DES_MAC_MD5);
  141. memset(krb5_hdr + 4, 0xff, 4);
  142. *(__be16 *)(krb5_hdr + 4) = htons(SEAL_ALG_DES);
  143. make_confounder(msg_start, blocksize);
  144. /* XXXJBF: UGH!: */
  145. tmp_pages = buf->pages;
  146. buf->pages = pages;
  147. if (make_checksum("md5", krb5_hdr, 8, buf,
  148. offset + headlen - blocksize, &md5cksum))
  149. return GSS_S_FAILURE;
  150. buf->pages = tmp_pages;
  151. if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
  152. md5cksum.data, md5cksum.len))
  153. return GSS_S_FAILURE;
  154. memcpy(krb5_hdr + 16, md5cksum.data + md5cksum.len - 8, 8);
  155. spin_lock(&krb5_seq_lock);
  156. seq_send = kctx->seq_send++;
  157. spin_unlock(&krb5_seq_lock);
  158. /* XXX would probably be more efficient to compute checksum
  159. * and encrypt at the same time: */
  160. if ((krb5_make_seq_num(kctx->seq, kctx->initiate ? 0 : 0xff,
  161. seq_send, krb5_hdr + 16, krb5_hdr + 8)))
  162. return GSS_S_FAILURE;
  163. if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
  164. pages))
  165. return GSS_S_FAILURE;
  166. return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
  167. }
  168. u32
  169. gss_unwrap_kerberos(struct gss_ctx *ctx, int offset, struct xdr_buf *buf)
  170. {
  171. struct krb5_ctx *kctx = ctx->internal_ctx_id;
  172. int signalg;
  173. int sealalg;
  174. char cksumdata[16];
  175. struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
  176. s32 now;
  177. int direction;
  178. s32 seqnum;
  179. unsigned char *ptr;
  180. int bodysize;
  181. void *data_start, *orig_start;
  182. int data_len;
  183. int blocksize;
  184. dprintk("RPC: gss_unwrap_kerberos\n");
  185. ptr = (u8 *)buf->head[0].iov_base + offset;
  186. if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
  187. buf->len - offset))
  188. return GSS_S_DEFECTIVE_TOKEN;
  189. if ((*ptr++ != ((KG_TOK_WRAP_MSG>>8)&0xff)) ||
  190. (*ptr++ != (KG_TOK_WRAP_MSG &0xff)) )
  191. return GSS_S_DEFECTIVE_TOKEN;
  192. /* XXX sanity-check bodysize?? */
  193. /* get the sign and seal algorithms */
  194. signalg = ptr[0] + (ptr[1] << 8);
  195. if (signalg != SGN_ALG_DES_MAC_MD5)
  196. return GSS_S_DEFECTIVE_TOKEN;
  197. sealalg = ptr[2] + (ptr[3] << 8);
  198. if (sealalg != SEAL_ALG_DES)
  199. return GSS_S_DEFECTIVE_TOKEN;
  200. if ((ptr[4] != 0xff) || (ptr[5] != 0xff))
  201. return GSS_S_DEFECTIVE_TOKEN;
  202. if (gss_decrypt_xdr_buf(kctx->enc, buf,
  203. ptr + 22 - (unsigned char *)buf->head[0].iov_base))
  204. return GSS_S_DEFECTIVE_TOKEN;
  205. if (make_checksum("md5", ptr - 2, 8, buf,
  206. ptr + 22 - (unsigned char *)buf->head[0].iov_base, &md5cksum))
  207. return GSS_S_FAILURE;
  208. if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
  209. md5cksum.data, md5cksum.len))
  210. return GSS_S_FAILURE;
  211. if (memcmp(md5cksum.data + 8, ptr + 14, 8))
  212. return GSS_S_BAD_SIG;
  213. /* it got through unscathed. Make sure the context is unexpired */
  214. now = get_seconds();
  215. if (now > kctx->endtime)
  216. return GSS_S_CONTEXT_EXPIRED;
  217. /* do sequencing checks */
  218. if (krb5_get_seq_num(kctx->seq, ptr + 14, ptr + 6, &direction,
  219. &seqnum))
  220. return GSS_S_BAD_SIG;
  221. if ((kctx->initiate && direction != 0xff) ||
  222. (!kctx->initiate && direction != 0))
  223. return GSS_S_BAD_SIG;
  224. /* Copy the data back to the right position. XXX: Would probably be
  225. * better to copy and encrypt at the same time. */
  226. blocksize = crypto_blkcipher_blocksize(kctx->enc);
  227. data_start = ptr + 22 + blocksize;
  228. orig_start = buf->head[0].iov_base + offset;
  229. data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
  230. memmove(orig_start, data_start, data_len);
  231. buf->head[0].iov_len -= (data_start - orig_start);
  232. buf->len -= (data_start - orig_start);
  233. if (gss_krb5_remove_padding(buf, blocksize))
  234. return GSS_S_DEFECTIVE_TOKEN;
  235. return GSS_S_COMPLETE;
  236. }