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: 105643 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp772124qgd; Thu, 15 Jun 2017 05:13:11 -0700 (PDT) X-Received: by 10.84.231.141 with SMTP id g13mr5851878plk.157.1497528791476; Thu, 15 Jun 2017 05:13:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1497528791; cv=none; d=google.com; s=arc-20160816; b=GVbGDkVlnbTSegYgQb6GUiXCrLh4HUchaoAN+J1VlNEFbPNugs15sa/zISN5FaPe6O zUw4LxS0zYK2I5HDji/euxsgYIJMRcVpOv5pwT9+uKc9NiKKn1nbebGmIK4xhpPP4JZw zAy+TtwKu3PRYKRhlW5TEibQfaa/eWK0kNMLretGdn05Rr46mKs2UnP8q21hHEkguYlP O7M/AZiLTz8kCaXJmqM4BzpAIXqf4KjoDzDClHHoPcAS3dDrvxu/g5cc6MJK8o1k7M1F 0mV0+uBv9IXpyquCBvjw9IaqmbzpkV0EHL3pDxF8WJUeHeWbO/aAtGJUnkZJIRALDMbq ELTw== 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=CGdZOq8LiLgOTOYeFls05XHSAOKXY5tNJK6yC7wa0Xc=; b=iEXsluSrObq+241BC84cHI9J7RwWfi+PNkTGqxOHG/ZY/1gGygnXN57vkb8VwXtV2d uUDq/UkMLUSDgi0lWccrFetVQMjTSCAj+Y6BxYQhJXJf/kT3e79ZWhGMWzcz5GD++FPC gCQ2+W7H66+XEFUsil+JNuX0yb5okVnPn3FUi+khZ4pJkyiJ9lcTYYX0KaIIWb3Snc0Z P2Uqp9iR1e3rnlLqirMy/N6GITq/LfM1QAqR3ZCDg/RkVcAJ6qHj5kkeDzZoKwWXe6ua s0SENd+K03CBjWZ0iW1/kP8ugT9j7cKRQHRNKstd1fEI2GI1+dZlH/4/Dq5AIEvA7u/Y rn3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.b=coBxgzNt; 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 y92si2162397plb.230.2017.06.15.05.13.11; Thu, 15 Jun 2017 05:13:11 -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.b=coBxgzNt; 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 S1752449AbdFOMNK (ORCPT + 6 others); 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 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 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.9.4 -- 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 Reviewed-by: Shawn Lin 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: