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: 110487 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp521454qge; Sun, 20 Aug 2017 14:39:31 -0700 (PDT) X-Received: by 10.84.209.202 with SMTP id y68mr16763709plh.474.1503265171344; Sun, 20 Aug 2017 14:39:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503265171; cv=none; d=google.com; s=arc-20160816; b=cGzPG+Ry0ruf3XIs325WnZQSnQ9tcg3dL2k/iA2o9Yl+LrjuHW4+DbEAMMamKzxCNA wixi2GI+rrI3R8qglDRy/JlwoqgdQCX0o/KvcdFuziHtma+Jx0mukJErynUB4T8F2ygR QoF17JX88pT0IXDR/C59VferWrro4BWV7MKd331HtZdeSG80kQPQ30BPEEHhjtC68WOs Mz2iSEphiOFaRP5vH0/1G47fhcCSo7yiuQ78vNoq89s9nmoTuKwy1rDe9RyKVYSgoPr7 t4YAT9flnlyZR/7Kgsj5is19K6gpxajPCmKzm3JDXhcPeJPFPKON+Cp2MmT4un8JAqDv PcFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=nUDY9eAUi130DBzQ/5I1z/KYPpZSsx8N+hJCqjPgU60=; b=wckyN9P90GhKVsGGwB73z7XZbtVAml5KlCxXkJbNkXZ8J6XCmFuFExoiMfG6+4FYsL m+Nzn3WMvS9JfpcSP/zs3HJdUO6vQkAf/z2jQXpai9EA+Dc9nf6sz78T3tvY4Ewd16PL WwHK1gjP3BU30BS/kn7YnxNEfyhLZNbvqlRBEoGvYEja/1JcJ6Uwi0hMQa7JlS3U9BJg PvSVv+EFvVv+yUEaYewd4YvmTbLqVvgFhSJO7rK7cj44Qmc0ZqgbM3TWpVXmB2yO6jZU n/k9f3FnJPJvvnLuMyYlhi2R4toE7gO3a2+8Oer8GxAVkTZuFtVksGy2NLC0YJCFHexV +dMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=PHYonxzd; spf=pass (google.com: best guess record for domain of linux-mmc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-mmc-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z1si3544613pgn.611.2017.08.20.14.39.31; Sun, 20 Aug 2017 14:39:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-mmc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=PHYonxzd; spf=pass (google.com: best guess record for domain of linux-mmc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-mmc-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753300AbdHTVja (ORCPT + 6 others); 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 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(-) -- 2.13.5 -- To unsubscribe from this list: send the line "unsubscribe linux-mmc" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html 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: