From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752516AbbIGUsh (ORCPT ); Mon, 7 Sep 2015 16:48:37 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:42657 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751162AbbIGUse (ORCPT ); Mon, 7 Sep 2015 16:48:34 -0400 From: Luis Henriques To: Minchan Kim , Nitin Gupta , Sergey Senozhatsky Cc: linux-kernel@vger.kernel.org Subject: [PATCH] zram: don't copy invalid compression algorithms Date: Mon, 7 Sep 2015 21:48:30 +0100 Message-Id: <1441658910-10226-1-git-send-email-luis.henriques@canonical.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Validate the new compression algorithm before copying it into the zram 'compressor' field, keeping the old one if it's invalid. The error path code is also slightly refactored. Signed-off-by: Luis Henriques --- drivers/block/zram/zram_drv.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 9c01f5bfa33f..33551ec9e7f5 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -367,10 +367,15 @@ static ssize_t comp_algorithm_store(struct device *dev, down_write(&zram->init_lock); if (init_done(zram)) { - up_write(&zram->init_lock); pr_info("Can't change algorithm for initialized device\n"); - return -EBUSY; + len = -EBUSY; + goto out; + } + if (!zcomp_available_algorithm(buf)) { + len = -EINVAL; + goto out; } + strlcpy(zram->compressor, buf, sizeof(zram->compressor)); /* ignore trailing newline */ @@ -378,9 +383,7 @@ static ssize_t comp_algorithm_store(struct device *dev, if (sz > 0 && zram->compressor[sz - 1] == '\n') zram->compressor[sz - 1] = 0x00; - if (!zcomp_available_algorithm(zram->compressor)) - len = -EINVAL; - +out: up_write(&zram->init_lock); return len; }