Commit 2a0d3e3a authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] fix in-place de/encryption bug with highmem

From: Christophe Saout <christophe@saout.de>

This patch fixes the bug where in-place encryption was not detected when
the same highmem pages is mapped twice to different virtual addresses.

This adds a parameter to xxx_process to indicate whether this is an
in-place encryption and moves the responsability to the caller using a
helper function scatterwalk.h.
parent 5f9e594b
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
typedef void (cryptfn_t)(void *, u8 *, const u8 *); typedef void (cryptfn_t)(void *, u8 *, const u8 *);
typedef void (procfn_t)(struct crypto_tfm *, u8 *, typedef void (procfn_t)(struct crypto_tfm *, u8 *,
u8*, cryptfn_t, int enc, void *); u8*, cryptfn_t, int enc, void *, int);
static inline void xor_64(u8 *a, const u8 *b) static inline void xor_64(u8 *a, const u8 *b)
{ {
...@@ -78,7 +78,9 @@ static int crypt(struct crypto_tfm *tfm, ...@@ -78,7 +78,9 @@ static int crypt(struct crypto_tfm *tfm,
scatterwalk_copychunks(src_p, &walk_in, bsize, 0); scatterwalk_copychunks(src_p, &walk_in, bsize, 0);
prfn(tfm, dst_p, src_p, crfn, enc, info); prfn(tfm, dst_p, src_p, crfn, enc, info,
scatterwalk_samebuf(&walk_in, &walk_out,
src_p, dst_p));
scatterwalk_done(&walk_in, 0, nbytes); scatterwalk_done(&walk_in, 0, nbytes);
...@@ -92,8 +94,8 @@ static int crypt(struct crypto_tfm *tfm, ...@@ -92,8 +94,8 @@ static int crypt(struct crypto_tfm *tfm,
} }
} }
static void cbc_process(struct crypto_tfm *tfm, static void cbc_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
u8 *dst, u8 *src, cryptfn_t fn, int enc, void *info) cryptfn_t fn, int enc, void *info, int in_place)
{ {
u8 *iv = info; u8 *iv = info;
...@@ -106,9 +108,8 @@ static void cbc_process(struct crypto_tfm *tfm, ...@@ -106,9 +108,8 @@ static void cbc_process(struct crypto_tfm *tfm,
fn(crypto_tfm_ctx(tfm), dst, iv); fn(crypto_tfm_ctx(tfm), dst, iv);
memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm)); memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm));
} else { } else {
const int need_stack = (src == dst); u8 stack[in_place ? crypto_tfm_alg_blocksize(tfm) : 0];
u8 stack[need_stack ? crypto_tfm_alg_blocksize(tfm) : 0]; u8 *buf = in_place ? stack : dst;
u8 *buf = need_stack ? stack : dst;
fn(crypto_tfm_ctx(tfm), buf, src); fn(crypto_tfm_ctx(tfm), buf, src);
tfm->crt_u.cipher.cit_xor_block(buf, iv); tfm->crt_u.cipher.cit_xor_block(buf, iv);
...@@ -119,7 +120,7 @@ static void cbc_process(struct crypto_tfm *tfm, ...@@ -119,7 +120,7 @@ static void cbc_process(struct crypto_tfm *tfm,
} }
static void ecb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src, static void ecb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
cryptfn_t fn, int enc, void *info) cryptfn_t fn, int enc, void *info, int in_place)
{ {
fn(crypto_tfm_ctx(tfm), dst, src); fn(crypto_tfm_ctx(tfm), dst, src);
} }
......
...@@ -33,6 +33,14 @@ static inline struct scatterlist *sg_next(struct scatterlist *sg) ...@@ -33,6 +33,14 @@ static inline struct scatterlist *sg_next(struct scatterlist *sg)
return sg + 1; return sg + 1;
} }
static inline int scatterwalk_samebuf(struct scatter_walk *walk_in,
struct scatter_walk *walk_out,
void *src_p, void *dst_p)
{
return walk_in->page == walk_out->page &&
walk_in->data == src_p && walk_out->data == dst_p;
}
void *scatterwalk_whichbuf(struct scatter_walk *walk, unsigned int nbytes, void *scratch); void *scatterwalk_whichbuf(struct scatter_walk *walk, unsigned int nbytes, void *scratch);
void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg); void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg);
int scatterwalk_copychunks(void *buf, struct scatter_walk *walk, size_t nbytes, int out); int scatterwalk_copychunks(void *buf, struct scatter_walk *walk, size_t nbytes, int out);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment