From patchwork Sun Aug 20 21:39:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 9911453 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 B2E50601D4 for ; Sun, 20 Aug 2017 21:39:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A8CE0286C5 for ; Sun, 20 Aug 2017 21:39:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9DEFC286CF; Sun, 20 Aug 2017 21:39:43 +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.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham 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 AB93C286C5 for ; Sun, 20 Aug 2017 21:39:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753300AbdHTVja (ORCPT ); Sun, 20 Aug 2017 17:39:30 -0400 Received: from mail-lf0-f54.google.com ([209.85.215.54]:35545 "EHLO mail-lf0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753351AbdHTVj3 (ORCPT ); Sun, 20 Aug 2017 17:39:29 -0400 Received: by mail-lf0-f54.google.com with SMTP id k186so4087512lfe.2 for ; Sun, 20 Aug 2017 14:39:29 -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=C7tW05enFyJsFu2EyJ+DPU8/Hd86fO3eEvzgLL6QkEA=; b=PHYonxzdsEfVnVGt0qfKgujlOmrzKJNZJQ1IG0kTglGv2SasFFpvL5JIkL8IQQvECr IlPA+5AxXcObBiYJHAoYqtht+P2tsLIQ2UMKdOoOjrK99LY/MefKp6iRKyp1oR9b3DW3 +3jc3VfVQMqb8/PsU4LDKaCtHS949mo+5NnC0= 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=C7tW05enFyJsFu2EyJ+DPU8/Hd86fO3eEvzgLL6QkEA=; b=GGmaJK6f6jBSUUPLCkS8IrMLfV6eiN8EqESyQgewi8XrYR051BVMsONBVBLToDtm4U kaAfeiBoPnXlvsXoRA6BKlcFqGMZyQkm+fN4ZSsEToCJ4A7O4kEDUqisssS2bu4qSQ9V uFQLTlTbRF7UmPVDQedwPZsPVRRohlBg/3JzBr2s2mg+u9Ok343UuODJ3LAdqswU94Ox aPbclxlfpC4sXgRzRP7rPUDbCGNsKSzGTEoqUPNzlPAfeZhNsap94aS2KObTOjHPTGaL 93WixH4sfEsCk2Hc5EC6DYaN/yFslkHyXG1aiCgFk3BoCJGeNuZxuEWFKhUp0/PHgCtn QHBw== X-Gm-Message-State: AHYfb5iDh4A0yZgxxNPg/PJwae47MAz+Fp4y9bFYslTR3QPsHAK0JCX2 ak1TSW5u7d1N6a8P9iFfRQ== X-Received: by 10.25.20.226 with SMTP id 95mr5723848lfu.194.1503265168117; Sun, 20 Aug 2017 14:39:28 -0700 (PDT) Received: from genomnajs.bredbandsbolaget.se (c-097b71d5.014-348-6c756e10.cust.bredbandsbolaget.se. [213.113.123.9]) by smtp.gmail.com with ESMTPSA id f136sm2381830lff.34.2017.08.20.14.39.27 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 20 Aug 2017 14:39:27 -0700 (PDT) From: Linus Walleij To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: Linus Walleij , Shawn Lin Subject: [PATCH 4/8 v5] mmc: block: Move duplicate check Date: Sun, 20 Aug 2017 23:39:09 +0200 Message-Id: <20170820213913.9737-5-linus.walleij@linaro.org> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20170820213913.9737-1-linus.walleij@linaro.org> References: <20170820213913.9737-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 --- ChangeLog v2->v5: - Rebase, put on top of the other cleanups. - Number v5 to keep together with the rest of the series. 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 6c91e125ab72..b0585f7971f1 100644 --- a/drivers/mmc/core/block.c +++ b/drivers/mmc/core/block.c @@ -564,14 +564,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); @@ -624,14 +616,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; @@ -698,14 +682,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: