Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2309578imm; Thu, 7 Jun 2018 08:33:38 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ2ZgFNBVz9JzTmIsH83rnNoL/HCoUpf/N72fMOFssrKrgv0wj7Q63rTijtglZnmqyXULmm X-Received: by 2002:a63:ac57:: with SMTP id z23-v6mr1930224pgn.394.1528385618924; Thu, 07 Jun 2018 08:33:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528385618; cv=none; d=google.com; s=arc-20160816; b=P3Q7MomLK96Gid7SvP2FYxN3FaOJQ9YEWgmdriiEdPZ+00mrc9h0pWA4GPUZpko0DW 7yLcPBZ0YPjQU4Yz5gtJP4K1QiIRkfVcSHesPDmSthckA53Bsj2yH2QS7onPtrfCAPJU LeaGOcl2Xm2pwLSSbqsX7pft2JKijA5yvumeYnT3J6ryfSvl5dmFxdbUEBJQ4Qq2T1/f +6k/QAcKm4k/yBbJMsNiUR07wrYuVR2pCVwOpwqapeTlgsYm7Qs0efO8rxaGPQ9fLlYB Q0CbxIB5QD+vb/KRCPcsiedBPZHMkvnXX5K39o2dRYlTMJMio+0aS7WMIiJ2IUX/eFA5 rUVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=EMqB7aa5cxr6paglt76jlXn6BARQt6QQojeUIyqV/Ww=; b=AuBYRQcknzgb3fo4Z2i0yUQk+ig/h06YWJ4tbmDU02yo2zeajFJzBKoTc+Ys2aGSMG 91V+kVI3KG5TgmNutSPLK6vPMXdMdienaJtyxBA/EAg2/GeqFH2FuoK1S2+8Dngdg6RQ bekLVv6jNaE0mC7Uo0l2Oq9AZEjhdA8/SXkFGrpHEAembQKDDBmtvGXK0qdoe6tTc/4Q 62RV/yfGddGPXW5f8TkIeG2VMZUl7n0WRkK1UeD6vCBkbn5AFYMrpwwXwb51kX/NThdn PinkDBEy+8veGWuNQTy/8OleqclBvAQu1Aupf5hXgvjCZOoJx1LozUGIPwee4Vr3Icd6 Q30A== 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 w187-v6si25924002pgb.11.2018.06.07.08.33.24; Thu, 07 Jun 2018 08:33:38 -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 S935121AbeFGPc3 (ORCPT + 99 others); Thu, 7 Jun 2018 11:32:29 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:40922 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934777AbeFGOwt (ORCPT ); Thu, 7 Jun 2018 10:52:49 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvbd-0005Zn-F1; Thu, 07 Jun 2018 15:09:37 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvb8-00034V-2y; Thu, 07 Jun 2018 15:09:06 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Greg Kroah-Hartman" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 248/410] staging: android: ashmem: Fix a race condition in pin ioctls In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Ben Hutchings commit ce8a3a9e76d0193e2e8d74a06d275b3c324ca652 upstream. ashmem_pin_unpin() reads asma->file and asma->size before taking the ashmem_mutex, so it can race with other operations that modify them. Build-tested only. Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- drivers/staging/android/ashmem.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) --- a/drivers/staging/android/ashmem.c +++ b/drivers/staging/android/ashmem.c @@ -703,30 +703,32 @@ static int ashmem_pin_unpin(struct ashme size_t pgstart, pgend; int ret = -EINVAL; + mutex_lock(&ashmem_mutex); + if (unlikely(!asma->file)) - return -EINVAL; + goto out_unlock; - if (unlikely(copy_from_user(&pin, p, sizeof(pin)))) - return -EFAULT; + if (unlikely(copy_from_user(&pin, p, sizeof(pin)))) { + ret = -EFAULT; + goto out_unlock; + } /* per custom, you can pass zero for len to mean "everything onward" */ if (!pin.len) pin.len = PAGE_ALIGN(asma->size) - pin.offset; if (unlikely((pin.offset | pin.len) & ~PAGE_MASK)) - return -EINVAL; + goto out_unlock; if (unlikely(((__u32) -1) - pin.offset < pin.len)) - return -EINVAL; + goto out_unlock; if (unlikely(PAGE_ALIGN(asma->size) < pin.offset + pin.len)) - return -EINVAL; + goto out_unlock; pgstart = pin.offset / PAGE_SIZE; pgend = pgstart + (pin.len / PAGE_SIZE) - 1; - mutex_lock(&ashmem_mutex); - switch (cmd) { case ASHMEM_PIN: ret = ashmem_pin(asma, pgstart, pgend); @@ -739,6 +741,7 @@ static int ashmem_pin_unpin(struct ashme break; } +out_unlock: mutex_unlock(&ashmem_mutex); return ret;