Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1337938imm; Thu, 23 Aug 2018 01:04:40 -0700 (PDT) X-Google-Smtp-Source: ANB0VdY1JnpP/ewNoKP2NwOpFtLJq4PqVY9ksgRBqPjRNG98sIHjX5HIHpkWybfOG3ALF7GP4QpP X-Received: by 2002:a62:4f0b:: with SMTP id d11-v6mr1320919pfb.132.1535011480837; Thu, 23 Aug 2018 01:04:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535011480; cv=none; d=google.com; s=arc-20160816; b=LB8FVymswHXyXBqydE1XdkKTAASfroPiBqKXFj/dhYxOU8O3kcoUEMvkuY5hfMXqc1 B+dHueCq43HWfUdWn3VuWNyt8LKNMEhpoGbQbCOk673mosgK6l+ZbgqOi4oL6g/gpBNi 8xWUyrbtSYx6PUn/un866G0wVkoW6Qs87yPgZa45Wh8/bdOmsz+OIHo/MIKPEZKPJe2w 0qQ7J6H9tBpKLadicGu9WaA5UdpKVZ+9bQQlTyGWyOZJMv2+NdChrsXkZ+krlhIL2EdS s5A+26Ac40kq1Ff0qXtPKGsXw0Xuc3BdDkmFTAq4s53w7d7HW5Wbq+gKyz8Yo2UZqyfL o7ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=k3nbICFYULPlQ9uk4DPbc1v8OongCR3drd34tqiM70U=; b=qMNlcCHVEQm+203w/P7MiHo5p+xXf5mlBmvZbwwB0lHnkR8q+gUHXXqY3DokMl2Mwt JYftDYpGx9VbVF+lvMUVdabl+VZbpETu3jntRWJvIRGu+7coJCTShe2L39ZYi/aDYbE2 rR1DZmD3AzuxgYQ6l6zgZs09KdTQSEQ4D1HdsRbgKsrpkjsyx3wAHscTkWhlVmcpcVW3 KJMth4Av9dJubrhSWiuGutQC1IriMauUDt7DioEYHYWD5WdZfR4IovoHcmLpZPBRAR1j RVqM2J+Vv2wb+q5e+XogtnkzDQdk9lWwLBVKnpLcSE2IVBkCJevp+obT/UMIfyJUqSik 3T1A== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k1-v6si3841988pgh.65.2018.08.23.01.04.26; Thu, 23 Aug 2018 01:04:40 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728887AbeHWL3i (ORCPT + 99 others); Thu, 23 Aug 2018 07:29:38 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:44370 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727846AbeHWL3h (ORCPT ); Thu, 23 Aug 2018 07:29:37 -0400 Received: from localhost (5355525A.cm-6-6b.dynamic.ziggo.nl [83.85.82.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id DBBE9CA0; Thu, 23 Aug 2018 08:01:13 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "linux-kernel@vger.kernel.org, kernel-team@android.com, Daniel Rosenberg" , Daniel Rosenberg Subject: [PATCH 4.4 66/79] staging: android: ion: check for kref overflow Date: Thu, 23 Aug 2018 09:53:42 +0200 Message-Id: <20180823074923.483861489@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180823074918.641878835@linuxfoundation.org> References: <20180823074918.641878835@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Daniel Rosenberg 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") 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 --- v2: Fixed patch corruption :( 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(-) --- 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_ha 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 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(st /* 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; }