Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp73462imm; Thu, 30 Aug 2018 16:11:23 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZ39rFb67T+1/rg6OzbpfF5R5gzfSKEHiLKDohPMX92Z4Mc13FgM1Hm9dHT/r9pTLXTz9Eq X-Received: by 2002:a17:902:7102:: with SMTP id a2-v6mr12349637pll.217.1535670683667; Thu, 30 Aug 2018 16:11:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535670683; cv=none; d=google.com; s=arc-20160816; b=uGNQ8Ry4V5XN6AFf4X7J97sthfcQOIonV7UFdyhPKGq8wC+d+/c0juIjFdvbxLgR9a 1GHxdnKUgRVBj+4vQCyzzq3anngPpEJR4SuRBxDA+NuuBcmnQ7YTyR2CIJEhnmIg6nyh HP5/QsEDS6smZlgUXES88DCaeMRpBbpi21HwOBwOpkVmZrEHcA22Cm0iGfKMM6TPJHHW aMWslzUDhZkqCuFrcZTbrLGovzv+Irlvr/2TB12rZ0oyqvTpAhcslfnqcHP9xMSDTGNA +DKiduv8tobFBW67F1luZT3NOX8OZ9Hm6+kFmbN5LmdmbVXNEfIaB+0F7t6eyN9Zt2LD rmsw== 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=LI6+qHPDRBvyXX9jBnTvMYKMO/1YjzPfSnNJukRSBEc=; b=JfxslNS4B96hJ4Gje1/yOXU8NMMm8JVIxRCpKLzG6Gvdsz6ABfdu+2mt1wNv2TfQG2 NYWMaU9jTuaZU+XZ7/Cm5rLsvEk+cjhHhY7C996LLQVM5H5kCS/QaS1ufnjW2zflVoMm xbW0d/bunsnQmF8mmFbz2DGmAb/yBU8WoNAiAZs9GxBJdkwBirLVfJEVxJZPn4Ltl+z9 pOLfJT+b4+ZtHs2E4yOt4knuk5vpFN3UDq7YHSDpxtO+0tIipOx3bitgm99+42d7hIA4 8Hj3U/HdtgbJcYrX0iH4yfpVYHxHY5j40U3x5pswktnMrcOu0wflqH7QFMuFEVG195Rc zOdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=g+lyFQy4; 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 b128-v6si8172993pfb.352.2018.08.30.16.11.08; Thu, 30 Aug 2018 16:11:23 -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=g+lyFQy4; 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 S1727211AbeHaDOe (ORCPT + 99 others); Thu, 30 Aug 2018 23:14:34 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46140 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725893AbeHaDOe (ORCPT ); Thu, 30 Aug 2018 23:14:34 -0400 Received: by mail-pg1-f196.google.com with SMTP id b129-v6so4543703pga.13 for ; Thu, 30 Aug 2018 16:10:02 -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=LI6+qHPDRBvyXX9jBnTvMYKMO/1YjzPfSnNJukRSBEc=; b=g+lyFQy4q9GU+n1DOCqw3kv7QtxCneYzNVS7NiaKWXRWf7c69jaa+W/jpPfN3CTB4x pFgnxf4NlB/Yx8LwUvhXIPg/z3N+EFqVwpdQdKAWzdykOVNbDp8exmk66ny/6M3Q62Qo OxC+DXJ/IjnO5wHdK7Cb3EjX0JgbCRn2TDVOmAnSg460Efs7Vmn8SVCBrNOUnxefzzNA I4ZdBdzqCneayt3cnJHE9JNa1E40rVSr1P1EnoJuUuQz68l8dTzylArWN0ZT8xh/7ePZ KKOOTscVivNRwc/OMhfb2VwueuqInuYsZStWId9HTnm388W26FchTjMlTwO9h8Nz6hgQ g58A== 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=LI6+qHPDRBvyXX9jBnTvMYKMO/1YjzPfSnNJukRSBEc=; b=jkW7Kxbw98mdtbCMI01ShXt2g6rOndMWKv0fPiLQyi0KhXOyFEM+FsTu6PZaBG/jZl 8Y1kk0LHtHKo7ZFNr/0I8X4nE80zzTjux0x7o02MM2eg/YWKBG9B4aFnB6AI2uIDzS5/ Lu/IpJAVVRG2MDMrdmvEHBk35RrW7DOhA9xs65sqAgOImLP2x0x9XwDX8bJRitA29OPV Bp5hj5CNNGKizQs7miQBm8l/OuYDzXhVRj0InuP0eSWeMQ3ZglZdhHZU57ndxSBoINPC SRfgvyBCpbmiYmsGUkM+QMpesVfhtsn7m7jFCve1CHO1VRrKglvKMQUYCCxgyXGHQEiN 6t6A== X-Gm-Message-State: APzg51A9J/jWFWLpuisyissNQ3LkA2KsFwb2H0foJF/lviBYsHwtK8Kr voUIc6L5mqY2LX25GDzsDIVkw0N8aXg= X-Received: by 2002:a62:c082:: with SMTP id g2-v6mr12725820pfk.72.1535670602158; Thu, 30 Aug 2018 16:10:02 -0700 (PDT) Received: from drosen.mtv.corp.google.com ([2620:0:1000:1612:726:adc3:41a6:c383]) by smtp.gmail.com with ESMTPSA id j15-v6sm10348610pfn.52.2018.08.30.16.10.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Aug 2018 16:10:01 -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] staging: android: ion: check for kref overflow Date: Thu, 30 Aug 2018 16:09:46 -0700 Message-Id: <20180830230946.228701-1-drosen@google.com> X-Mailer: git-send-email 2.19.0.rc0.228.g281dcd1b4d0-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 This patch is against 4.9. 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") Userspace can cause the kref to handles to increment arbitrarily high. Ensure it does not overflow. Signed-off-by: Daniel Rosenberg Signed-off-by: Greg Kroah-Hartman --- 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 6f9974cb0e152..48821948fa487 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -15,6 +15,7 @@ * */ +#include #include #include #include @@ -305,6 +306,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; +} + int ion_handle_put_nolock(struct ion_handle *handle) { return kref_put(&handle->ref, ion_handle_destroy); @@ -347,9 +358,9 @@ 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, @@ -1100,7 +1111,7 @@ struct ion_handle *ion_import_dma_buf(struct ion_client *client, /* 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.19.0.rc0.228.g281dcd1b4d0-goog