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

zram: fix possible use after free in zcomp_create()

zcomp_create() verifies the success of zcomp_strm_{multi,single}_create()
through comp->stream, which can potentially be pointing to memory that
was freed if these functions returned an error.

While at it, replace a 'ERR_PTR(-ENOMEM)' by a more generic
'ERR_PTR(error)' as in the future zcomp_strm_{multi,siggle}_create()
could return other error codes. Function documentation updated
accordingly.

Fixes: beca3ec71fe5 ("zram: add multi stream functionality")
Signed-off-by: Luis Henriques <luis.henriques@canonical.com>
Acked-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Acked-by: Minchan Kim <minchan@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Luis Henriques and committed by
Linus Torvalds
3aaf14da 72714841

+7 -5
+7 -5
drivers/block/zram/zcomp.c
··· 330 330 * allocate new zcomp and initialize it. return compressing 331 331 * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL) 332 332 * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in 333 - * case of allocation error. 333 + * case of allocation error, or any other error potentially 334 + * returned by functions zcomp_strm_{multi,single}_create. 334 335 */ 335 336 struct zcomp *zcomp_create(const char *compress, int max_strm) 336 337 { 337 338 struct zcomp *comp; 338 339 struct zcomp_backend *backend; 340 + int error; 339 341 340 342 backend = find_backend(compress); 341 343 if (!backend) ··· 349 347 350 348 comp->backend = backend; 351 349 if (max_strm > 1) 352 - zcomp_strm_multi_create(comp, max_strm); 350 + error = zcomp_strm_multi_create(comp, max_strm); 353 351 else 354 - zcomp_strm_single_create(comp); 355 - if (!comp->stream) { 352 + error = zcomp_strm_single_create(comp); 353 + if (error) { 356 354 kfree(comp); 357 - return ERR_PTR(-ENOMEM); 355 + return ERR_PTR(error); 358 356 } 359 357 return comp; 360 358 }