Skip to content

Commit 0868def

Browse files
ebiggersherbertx
authored andcommitted
crypto: blkcipher - fix crash flushing dcache in error path
Like the skcipher_walk case: scatterwalk_done() is only meant to be called after a nonzero number of bytes have been processed, since scatterwalk_pagedone() will flush the dcache of the *previous* page. But in the error case of blkcipher_walk_done(), e.g. if the input wasn't an integer number of blocks, scatterwalk_done() was actually called after advancing 0 bytes. This caused a crash ("BUG: unable to handle kernel paging request") during '!PageSlab(page)' on architectures like arm and arm64 that define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE, provided that the input was page-aligned as in that case walk->offset == 0. Fix it by reorganizing blkcipher_walk_done() to skip the scatterwalk_advance() and scatterwalk_done() if an error has occurred. This bug was found by syzkaller fuzzing. Reproducer, assuming ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE: #include <linux/if_alg.h> #include <sys/socket.h> #include <unistd.h> int main() { struct sockaddr_alg addr = { .salg_type = "skcipher", .salg_name = "ecb(aes-generic)", }; char buffer[4096] __attribute__((aligned(4096))) = { 0 }; int fd; fd = socket(AF_ALG, SOCK_SEQPACKET, 0); bind(fd, (void *)&addr, sizeof(addr)); setsockopt(fd, SOL_ALG, ALG_SET_KEY, buffer, 16); fd = accept(fd, NULL, NULL); write(fd, buffer, 15); read(fd, buffer, 15); } Reported-by: Liu Chao <liuchao741@huawei.com> Fixes: 5cde0af ("[CRYPTO] cipher: Added block cipher type") Cc: <stable@vger.kernel.org> # v2.6.19+ Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 8088d3d commit 0868def

1 file changed

Lines changed: 26 additions & 28 deletions

File tree

‎crypto/blkcipher.c‎

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,18 @@ static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len)
7070
return max(start, end_page);
7171
}
7272

73-
static inline unsigned int blkcipher_done_slow(struct blkcipher_walk *walk,
74-
unsigned int bsize)
73+
static inline void blkcipher_done_slow(struct blkcipher_walk *walk,
74+
unsigned int bsize)
7575
{
7676
u8 *addr;
7777

7878
addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
7979
addr = blkcipher_get_spot(addr, bsize);
8080
scatterwalk_copychunks(addr, &walk->out, bsize, 1);
81-
return bsize;
8281
}
8382

84-
static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk,
85-
unsigned int n)
83+
static inline void blkcipher_done_fast(struct blkcipher_walk *walk,
84+
unsigned int n)
8685
{
8786
if (walk->flags & BLKCIPHER_WALK_COPY) {
8887
blkcipher_map_dst(walk);
@@ -96,49 +95,48 @@ static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk,
9695

9796
scatterwalk_advance(&walk->in, n);
9897
scatterwalk_advance(&walk->out, n);
99-
100-
return n;
10198
}
10299

103100
int blkcipher_walk_done(struct blkcipher_desc *desc,
104101
struct blkcipher_walk *walk, int err)
105102
{
106-
unsigned int nbytes = 0;
103+
unsigned int n; /* bytes processed */
104+
bool more;
107105

108-
if (likely(err >= 0)) {
109-
unsigned int n = walk->nbytes - err;
106+
if (unlikely(err < 0))
107+
goto finish;
110108

111-
if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW)))
112-
n = blkcipher_done_fast(walk, n);
113-
else if (WARN_ON(err)) {
114-
err = -EINVAL;
115-
goto err;
116-
} else
117-
n = blkcipher_done_slow(walk, n);
109+
n = walk->nbytes - err;
110+
walk->total -= n;
111+
more = (walk->total != 0);
118112

119-
nbytes = walk->total - n;
120-
err = 0;
113+
if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW))) {
114+
blkcipher_done_fast(walk, n);
115+
} else {
116+
if (WARN_ON(err)) {
117+
/* unexpected case; didn't process all bytes */
118+
err = -EINVAL;
119+
goto finish;
120+
}
121+
blkcipher_done_slow(walk, n);
121122
}
122123

123-
scatterwalk_done(&walk->in, 0, nbytes);
124-
scatterwalk_done(&walk->out, 1, nbytes);
124+
scatterwalk_done(&walk->in, 0, more);
125+
scatterwalk_done(&walk->out, 1, more);
125126

126-
err:
127-
walk->total = nbytes;
128-
walk->nbytes = nbytes;
129-
130-
if (nbytes) {
127+
if (more) {
131128
crypto_yield(desc->flags);
132129
return blkcipher_walk_next(desc, walk);
133130
}
134-
131+
err = 0;
132+
finish:
133+
walk->nbytes = 0;
135134
if (walk->iv != desc->info)
136135
memcpy(desc->info, walk->iv, walk->ivsize);
137136
if (walk->buffer != walk->page)
138137
kfree(walk->buffer);
139138
if (walk->page)
140139
free_page((unsigned long)walk->page);
141-
142140
return err;
143141
}
144142
EXPORT_SYMBOL_GPL(blkcipher_walk_done);

0 commit comments

Comments
 (0)