123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
#include <linux/err.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/scatterlist.h>
#include <linux/highmem.h>
#include <linux/pagemap.h>
#include <linux/sunrpc/gss_krb5.h>
#include <linux/sunrpc/xdr.h>
#include "gss_krb5_internal.h"
#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
# define RPCDBG_FACILITY RPCDBG_AUTH
#endif
int
xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
{
u8 *p;
if (shiftlen == 0)
return 0;
BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);
p = buf->head[0].iov_base + base;
memmove(p + shiftlen, p, buf->head[0].iov_len - base);
buf->head[0].iov_len += shiftlen;
buf->len += shiftlen;
return 0;
}
u32
gss_krb5_aead_encrypt(struct krb5_ctx *kctx, u32 offset,
struct xdr_buf *buf, struct page **pages)
{
const struct krb5_enctype *krb5 = kctx->krb5e;
struct crypto_aead *aead = kctx->initiate ?
kctx->initiator_enc_aead : kctx->acceptor_enc_aead;
unsigned int conflen = krb5->conf_len;
unsigned int cksum_len = krb5->cksum_len;
unsigned int sec_offset, sec_len, data_len;
struct scatterlist sg[XDR_BUF_TO_SG_NENTS];
struct scatterlist *sg_overflow = NULL;
ssize_t ret;
int nsg;
if (xdr_extend_head(buf, offset + GSS_KRB5_TOK_HDR_LEN, conflen))
return GSS_S_FAILURE;
if (buf->tail[0].iov_base == NULL) {
buf->tail[0].iov_base = buf->head[0].iov_base
+ buf->head[0].iov_len;
buf->tail[0].iov_len = 0;
}
memcpy(buf->tail[0].iov_base + buf->tail[0].iov_len,
buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN);
buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
buf->len += GSS_KRB5_TOK_HDR_LEN;
buf->tail[0].iov_len += cksum_len;
buf->len += cksum_len;
if (pages != buf->pages) {
unsigned int poff = buf->page_base;
unsigned int plen = buf->page_len;
unsigned int i = poff >> PAGE_SHIFT;
unsigned int off = offset_in_page(poff);
while (plen) {
unsigned int n = min_t(unsigned int, plen,
PAGE_SIZE - off);
memcpy_page(buf->pages[i], off, pages[i], off, n);
plen -= n;
i++;
off = 0;
}
}
sec_offset = offset + GSS_KRB5_TOK_HDR_LEN;
sec_len = buf->len - sec_offset;
data_len = sec_len - conflen - cksum_len;
nsg = xdr_buf_to_sg_alloc(buf, sec_offset, sec_len,
sg, ARRAY_SIZE(sg),
&sg_overflow, GFP_NOFS);
if (nsg < 0)
return GSS_S_FAILURE;
ret = crypto_krb5_encrypt(krb5, aead, sg, nsg, sec_len,
conflen, data_len, false);
kfree(sg_overflow);
if (ret < 0)
return GSS_S_FAILURE;
return GSS_S_COMPLETE;
}
u32
gss_krb5_aead_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len,
struct xdr_buf *buf, u32 *headskip, u32 *tailskip)
{
const struct krb5_enctype *krb5 = kctx->krb5e;
struct crypto_aead *aead = kctx->initiate ?
kctx->acceptor_enc_aead : kctx->initiator_enc_aead;
unsigned int sec_offset, sec_len;
size_t data_offset, data_len;
struct scatterlist sg[XDR_BUF_TO_SG_NENTS];
struct scatterlist *sg_overflow = NULL;
int nsg, ret;
sec_offset = offset + GSS_KRB5_TOK_HDR_LEN;
if (len < sec_offset)
return GSS_S_DEFECTIVE_TOKEN;
sec_len = len - sec_offset;
nsg = xdr_buf_to_sg_alloc(buf, sec_offset, sec_len,
sg, ARRAY_SIZE(sg),
&sg_overflow, GFP_NOFS);
if (nsg < 0)
return GSS_S_FAILURE;
data_offset = 0;
data_len = sec_len;
ret = crypto_krb5_decrypt(krb5, aead, sg, nsg,
&data_offset, &data_len);
kfree(sg_overflow);
if (ret < 0)
return gss_krb5_errno_to_status(ret);
*headskip = data_offset;
*tailskip = sec_len - data_offset - data_len;
return GSS_S_COMPLETE;
}
int gss_krb5_mic_build_sg(const struct xdr_buf *body,
void *cksum, unsigned int cksum_len,
void *hdr,
struct scatterlist *sg_head,
struct scatterlist **sg_overflow)
{
struct scatterlist *entry;
int body_max, body_nsg, nsg;
*sg_overflow = NULL;
body_max = 2;
if (body->page_len)
body_max += DIV_ROUND_UP(body->page_len +
offset_in_page(body->page_base),
PAGE_SIZE);
nsg = 1 + body_max + 1;
if (nsg <= XDR_BUF_TO_SG_NENTS) {
sg_init_table(sg_head, nsg);
} else {
unsigned int overflow_nents =
nsg - XDR_BUF_TO_SG_NENTS + 1;
*sg_overflow = kmalloc_objs(**sg_overflow, overflow_nents,
GFP_NOFS);
if (!*sg_overflow)
return -ENOMEM;
sg_init_table(sg_head, XDR_BUF_TO_SG_NENTS);
sg_init_table(*sg_overflow, overflow_nents);
sg_chain(sg_head, XDR_BUF_TO_SG_NENTS, *sg_overflow);
}
sg_set_buf(&sg_head[0], cksum, cksum_len);
body_nsg = xdr_buf_to_sg(body, 0, body->len,
sg_next(&sg_head[0]), body_max);
if (body_nsg < 0)
goto out_err;
if (body_nsg > 0) {
entry = sg_last(sg_next(&sg_head[0]), body_nsg);
sg_unmark_end(entry);
entry = sg_next(entry);
} else {
entry = sg_next(&sg_head[0]);
}
sg_set_buf(entry, hdr, GSS_KRB5_TOK_HDR_LEN);
sg_mark_end(entry);
return 1 + body_nsg + 1;
out_err:
kfree(*sg_overflow);
*sg_overflow = NULL;
return body_nsg;
}