Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: pcrypt - fix freeing pcrypt instances

pcrypt is using the old way of freeing instances, where the ->free()
method specified in the 'struct crypto_template' is passed a pointer to
the 'struct crypto_instance'. But the crypto_instance is being
kfree()'d directly, which is incorrect because the memory was actually
allocated as an aead_instance, which contains the crypto_instance at a
nonzero offset. Thus, the wrong pointer was being kfree()'d.

Fix it by switching to the new way to free aead_instance's where the
->free() method is specified in the aead_instance itself.

Reported-by: syzbot <syzkaller@googlegroups.com>
Fixes: 0496f56065e0 ("crypto: pcrypt - Add support for new AEAD interface")
Cc: <stable@vger.kernel.org> # v4.2+
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Eric Biggers and committed by
Herbert Xu
d76c6810 203f4500

+10 -9
+10 -9
crypto/pcrypt.c
··· 254 254 crypto_free_aead(ctx->child); 255 255 } 256 256 257 + static void pcrypt_free(struct aead_instance *inst) 258 + { 259 + struct pcrypt_instance_ctx *ctx = aead_instance_ctx(inst); 260 + 261 + crypto_drop_aead(&ctx->spawn); 262 + kfree(inst); 263 + } 264 + 257 265 static int pcrypt_init_instance(struct crypto_instance *inst, 258 266 struct crypto_alg *alg) 259 267 { ··· 327 319 inst->alg.encrypt = pcrypt_aead_encrypt; 328 320 inst->alg.decrypt = pcrypt_aead_decrypt; 329 321 322 + inst->free = pcrypt_free; 323 + 330 324 err = aead_register_instance(tmpl, inst); 331 325 if (err) 332 326 goto out_drop_aead; ··· 357 347 } 358 348 359 349 return -EINVAL; 360 - } 361 - 362 - static void pcrypt_free(struct crypto_instance *inst) 363 - { 364 - struct pcrypt_instance_ctx *ctx = crypto_instance_ctx(inst); 365 - 366 - crypto_drop_aead(&ctx->spawn); 367 - kfree(inst); 368 350 } 369 351 370 352 static int pcrypt_cpumask_change_notify(struct notifier_block *self, ··· 471 469 static struct crypto_template pcrypt_tmpl = { 472 470 .name = "pcrypt", 473 471 .create = pcrypt_create, 474 - .free = pcrypt_free, 475 472 .module = THIS_MODULE, 476 473 }; 477 474