Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp50057imm; Tue, 21 Aug 2018 14:22:09 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzurVawmHLymNUgYhgFittRJ80CZGEQPD30Z6Jf1ZztWHzT87DVEdjzwcOsYH9Nh1YqkOjT X-Received: by 2002:a63:f60:: with SMTP id 32-v6mr12475350pgp.399.1534886529766; Tue, 21 Aug 2018 14:22:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534886529; cv=none; d=google.com; s=arc-20160816; b=hO3hmqlHaXOOv6cT9sod/3CTCwPtwTTFtWadx4VKzQ/CBC3z3nSIyyhKTBVWjmzTRc S7X8Sb0442TRZHnMZhAK4DseWbwtClazPfhFxnc9SPqQIFrkt//BMcF1hfE1luIxPhRv jXTBUpbFWljEclyzwunlL0ZpAoRJyEt8aWfc1Hg9GrP0f7ymcdbjJBO03cfpo+xUk/Nt A51N9IW6o1d37slzTcmbOUHnmB0IhnehSDEP8CUvvIeRt/SfbuN214m9v/CPmqlOuhqy WSxJ3ItsUVEmpG9yFPzLZiaPsdr0kOkW5TwYBZegUzJwcm35un4bvxT1iRoJQQ1BaLut ojxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=9cjkjlrdY+HGBYI9OzUREJRROMf3EB5erbhTOL6QPK4=; b=Z6s17qzqECLYD1xFB2NSc8bW3zxa15G/2PG2DBzaIo67g2u9cyQVC68bFUefi7dGqR kj1Wc9Eo5sjfGFoffKnfjfPwn5YB/16wGQw52UxPlJ/ihJ5tpXwmgIqKLNfnUDzZ/3GQ Q4T1OjLGxS3QDnyc4EpDoJpljo+Q6MWHTDXPMeWrK9J4t8J2nfZskU4UL0cchgE626SN Q4yTNlchhLJcFCmbnl7H7+fgoew2w35aPyeMG+LD59ge6ZMS7nWBmJ63bwI7rsuLmq/9 GLYa5RQG9zao9HMphZ8oCVcxiHkz3GV2jDnYZPFlUu4zByipmBSkazk+ngR+jud1g4Lh s8qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=eBeyA5h1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 2-v6si13399942plb.444.2018.08.21.14.21.31; Tue, 21 Aug 2018 14:22:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=eBeyA5h1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727735AbeHUXyS (ORCPT + 99 others); Tue, 21 Aug 2018 19:54:18 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:39328 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726800AbeHUXyS (ORCPT ); Tue, 21 Aug 2018 19:54:18 -0400 Received: by mail-pg1-f194.google.com with SMTP id a11-v6so8980651pgw.6 for ; Tue, 21 Aug 2018 13:32:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=9cjkjlrdY+HGBYI9OzUREJRROMf3EB5erbhTOL6QPK4=; b=eBeyA5h1aTUgiScY8bPSLBOswB0d+JclWGGvCnBWSvTNtpx9ZCy4iQfF0QRs3hh0Ct 2TMDM0evFyUXr7SxRyHKm1JC+G/O5rQmZoG7LctvQf6eqwz/1nwYH3X7J4d31JtfKleE yJ9XAJlcOjo7qP5hIWuAzV0B3M8qq7+FqRJkkPFaA7ZDU370boVuumrJd+7jfbnhKZBg 9nU5NRZdkmnAl25QgNU5vYzFZtMwn4/0A2eOsfodS2cctLG3FbIRGMwMB1vRpnrAMdV1 bj4NnkX8MQ2+V27BqboX8ar9LRL0u77p6P5opadNPHO9MfSvVA7/Op2JbTYUx/ZH43oE dQ9w== 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:mime-version :content-transfer-encoding; bh=9cjkjlrdY+HGBYI9OzUREJRROMf3EB5erbhTOL6QPK4=; b=D4yalam3EpiiTlSgk2yYESv9hCv6RUmJvGHnuvYXO0raHmqKyJ0WIgzraBS/PuH8Ry Ru1WzEodMZeT/uwDWmzYieXUsDAg5AVzEFwmJ1Q9V8KMbny1rJjodZCkzfVDO7HB4a1e b3qOtOOaGptYtyktNkRmI0FUi9hVR7xXzyGjhe6LppCAj3jU/Zr4PlyAZ6W8AlnweSQT Glk5QkeMS9LscjHnOLN1QUiAasClP0+vXMI+ngSn2M3f9/7KrxTkAeqkTjKcgiBIGmjP YzHf1okzQqemXLNAhUnmt1u5kErUyneiUauxQwg8H+D2leLWcYafKmuxquh9qYeooNI5 lH7Q== X-Gm-Message-State: AOUpUlFpofHm+Z3fxeXdCSSjbU63yAdso+IrksJLsT3er/YUFy8B1vvf V6EBJ0Svz9bDabeskk+G0gy+dQ== X-Received: by 2002:a63:1e63:: with SMTP id p35-v6mr10088845pgm.376.1534883558432; Tue, 21 Aug 2018 13:32:38 -0700 (PDT) Received: from drosen.mtv.corp.google.com ([2620:0:1000:1612:726:adc3:41a6:c383]) by smtp.gmail.com with ESMTPSA id v82-v6sm22350495pfd.64.2018.08.21.13.32.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 13:32:37 -0700 (PDT) From: Daniel Rosenberg To: stable@vger.kernel.org, Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Daniel Rosenberg Subject: [PATCH v2] staging: android: ion: check for kref overflow Date: Tue, 21 Aug 2018 13:31:50 -0700 Message-Id: <20180821203150.231997-1-drosen@google.com> X-Mailer: git-send-email 2.18.0.865.gffc8e1a3cd6-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Userspace can cause the kref to handles to increment arbitrarily high. Ensure it does not overflow. Signed-off-by: Daniel Rosenberg --- v2: Fixed patch corruption :( This patch is against 4.4. It does not apply to master due to a large rework of ion in 4.12 which removed the affected functions altogther. 4c23cbff073f3b9b ("staging: android: ion: Remove import interface") It applies from 3.18 to 4.11, although with a trivial conflict resolution for the later branches. drivers/staging/android/ion/ion.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c index 374f840f31a48..47cb163da9a07 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -15,6 +15,7 @@ * */ +#include #include #include #include @@ -387,6 +388,16 @@ static void ion_handle_get(struct ion_handle *handle) kref_get(&handle->ref); } +/* Must hold the client lock */ +static struct ion_handle *ion_handle_get_check_overflow( + struct ion_handle *handle) +{ + if (atomic_read(&handle->ref.refcount) + 1 == 0) + return ERR_PTR(-EOVERFLOW); + ion_handle_get(handle); + return handle; +} + static int ion_handle_put_nolock(struct ion_handle *handle) { int ret; @@ -433,9 +444,9 @@ static struct ion_handle *ion_handle_get_by_id_nolock(struct ion_client *client, handle = idr_find(&client->idr, id); if (handle) - ion_handle_get(handle); + return ion_handle_get_check_overflow(handle); - return handle ? handle : ERR_PTR(-EINVAL); + return ERR_PTR(-EINVAL); } struct ion_handle *ion_handle_get_by_id(struct ion_client *client, @@ -1202,7 +1213,7 @@ struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd) /* if a handle exists for this buffer just take a reference to it */ handle = ion_handle_lookup(client, buffer); if (!IS_ERR(handle)) { - ion_handle_get(handle); + handle = ion_handle_get_check_overflow(handle); mutex_unlock(&client->lock); goto end; } -- 2.18.0.865.gffc8e1a3cd6-goog