From patchwork Thu Jun 15 12:12:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 9788743 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 31F8360384 for ; Thu, 15 Jun 2017 12:13:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1F6F0285EA for ; Thu, 15 Jun 2017 12:13:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1412B285F3; Thu, 15 Jun 2017 12:13:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B99D3285EA for ; Thu, 15 Jun 2017 12:13:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752449AbdFOMNK (ORCPT ); Thu, 15 Jun 2017 08:13:10 -0400 Received: from mail-lf0-f44.google.com ([209.85.215.44]:34035 "EHLO mail-lf0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752393AbdFOMNJ (ORCPT ); Thu, 15 Jun 2017 08:13:09 -0400 Received: by mail-lf0-f44.google.com with SMTP id v20so7981796lfa.1 for ; Thu, 15 Jun 2017 05:13:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FhFz325leF5yGr9DvrBoDl0BnYNQtAqp219xvuB/5cM=; b=coBxgzNtzQSYG9lp5lRBzJ12EY80xsP9X05kN9BebMz/z83T9E1vlJMmAGkwNga60+ xn/ic59QRsWnbImZ+0Q64XNi49K00t2IfMb2A/B8CtvVDhjhPQneOhoMq4snQR8BGR4B mH6l9AhsfJA98Nxrd51mg4f6mQShCIhuo/yZY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FhFz325leF5yGr9DvrBoDl0BnYNQtAqp219xvuB/5cM=; b=l3ZXrKxbA/H42caB+t2+BpetqIxuJwi1G70S6nlAX58944PQ5q2VN9hsPdv7ddtjOP GsiwSkQ4UR8qfBxWcJ20Y/0qRmEd3nmAzkXGSm+1ifqgeToSJ4xgN3k+Al8JSv8bfwpS j4OHZNGJhXlPxKioNIXUFZ1FKst/FWHqRU2Z3hPljT4xHMmjtDQVDmzKkL72TCHDqqRZ YYDKZ/dFyQhMi/JjQoxd+RKTlcnPNAinPoTouUeCAXJxSYl9I+eS6Wk7IODuke3UwlR3 nbPo8oFHp1omBnlel6Sg1iGNmb15QnQ5/tz48Nmy/y3dLGFCGQWgsYVPa6s+83ki/GKv BZsg== X-Gm-Message-State: AKS2vOxOewmOk15j+gCAC8zBqU2bRvYngPlpqM0S36E2oTh/jCaF5Y9N CaTtSwBqhKKT/hzGw8gqxg== X-Received: by 10.25.115.134 with SMTP id h6mr1872063lfk.163.1497528787202; Thu, 15 Jun 2017 05:13:07 -0700 (PDT) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id v1sm597574lja.18.2017.06.15.05.13.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Jun 2017 05:13:05 -0700 (PDT) From: Linus Walleij To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: linux-block@vger.kernel.org, Jens Axboe , Christoph Hellwig , Arnd Bergmann , Bartlomiej Zolnierkiewicz , Paolo Valente , Avri Altman , Adrian Hunter , Linus Walleij , Shawn Lin Subject: [PATCH 1/5] mmc: block: Move duplicate check Date: Thu, 15 Jun 2017 14:12:55 +0200 Message-Id: <20170615121259.8281-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170615121259.8281-1-linus.walleij@linaro.org> References: <20170615121259.8281-1-linus.walleij@linaro.org> Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP mmc_blk_ioctl() calls either mmc_blk_ioctl_cmd() or mmc_blk_ioctl_multi_cmd() and each of these make the same check. Factor it into a new helper function, call it on both branches of the switch() statement and save a chunk of duplicate code. Cc: Shawn Lin Signed-off-by: Linus Walleij Reviewed-by: Shawn Lin --- ChangeLog v1->v2: - We need to check the block device only if an actual well-known ioctl() is coming in, on the path of the switch() statments, only those branches that handle actual ioctl()s. Create a new helper function to check the block device and call that. --- drivers/mmc/core/block.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c index 1ce6012ce3c1..d1b824e65590 100644 --- a/drivers/mmc/core/block.c +++ b/drivers/mmc/core/block.c @@ -566,14 +566,6 @@ static int mmc_blk_ioctl_cmd(struct block_device *bdev, int err = 0, ioc_err = 0; struct request *req; - /* - * The caller must have CAP_SYS_RAWIO, and must be calling this on the - * whole block device, not on a partition. This prevents overspray - * between sibling partitions. - */ - if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) - return -EPERM; - idata = mmc_blk_ioctl_copy_from_user(ic_ptr); if (IS_ERR(idata)) return PTR_ERR(idata); @@ -626,14 +618,6 @@ static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev, __u64 num_of_cmds; struct request *req; - /* - * The caller must have CAP_SYS_RAWIO, and must be calling this on the - * whole block device, not on a partition. This prevents overspray - * between sibling partitions. - */ - if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) - return -EPERM; - if (copy_from_user(&num_of_cmds, &user->num_of_cmds, sizeof(num_of_cmds))) return -EFAULT; @@ -697,14 +681,34 @@ static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev, return ioc_err ? ioc_err : err; } +static int mmc_blk_check_blkdev(struct block_device *bdev) +{ + /* + * The caller must have CAP_SYS_RAWIO, and must be calling this on the + * whole block device, not on a partition. This prevents overspray + * between sibling partitions. + */ + if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) + return -EPERM; + return 0; +} + static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) { + int ret; + switch (cmd) { case MMC_IOC_CMD: + ret = mmc_blk_check_blkdev(bdev); + if (ret) + return ret; return mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg); case MMC_IOC_MULTI_CMD: + ret = mmc_blk_check_blkdev(bdev); + if (ret) + return ret; return mmc_blk_ioctl_multi_cmd(bdev, (struct mmc_ioc_multi_cmd __user *)arg); default: