From patchwork Wed Feb 5 00:08:37 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 24154 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f69.google.com (mail-oa0-f69.google.com [209.85.219.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 00361202FA for ; Wed, 5 Feb 2014 00:09:29 +0000 (UTC) Received: by mail-oa0-f69.google.com with SMTP id h16sf41565559oag.4 for ; Tue, 04 Feb 2014 16:09:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=/UVa9iEQQRk5caQvMdtuSqWE9LTM7GI0H1t99F6ghQQ=; b=bAeSGnkSgeSeA44muCIcPi8p148EYIoHQ27XRc6zMQjWpdZ/uJXaH0RXzVhzaA0bgb hPZ4gJVLxyYx50NgjTr5EvrKiB9QNjuitUgWbeJuYTDUFozlMr1Ij6eHtynBMiQRx5u8 rLvg1/q04LsuDlzCCHtgLdCNnCm64ZptvYqSsGl7uQj3C/aRktdPk/Ntp+vBlUZpPE8S ci47J8kgCsGvIEQaoQe8Li2acSJ9kYetLOBriIXfDfQYRO5KK3jWRKHakdWp0kevKm1i UHnqbrsoh50kCZ/6UmAZGaNgpzKxlfSRfGOEOdvLy0gdkJnc4nn+7YYUwrMOGGihEMuS KN8w== X-Gm-Message-State: ALoCoQn5QrVD4aU3oRlAtLK3kvfxvBtzEQ0EhyXMVlpbq9AYqU7B90TWI/GFFGJML4yTLbNPVwJX X-Received: by 10.182.28.36 with SMTP id y4mr5405370obg.46.1391558969009; Tue, 04 Feb 2014 16:09:29 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.35.208 with SMTP id n74ls2660295qgn.65.gmail; Tue, 04 Feb 2014 16:09:28 -0800 (PST) X-Received: by 10.220.99.72 with SMTP id t8mr35154497vcn.10.1391558968494; Tue, 04 Feb 2014 16:09:28 -0800 (PST) Received: from mail-vc0-f177.google.com (mail-vc0-f177.google.com [209.85.220.177]) by mx.google.com with ESMTPS id y6si8946583veb.139.2014.02.04.16.09.28 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 04 Feb 2014 16:09:28 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.177 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.177; Received: by mail-vc0-f177.google.com with SMTP id if11so6300873vcb.22 for ; Tue, 04 Feb 2014 16:09:28 -0800 (PST) X-Received: by 10.220.131.210 with SMTP id y18mr35556361vcs.12.1391558968411; Tue, 04 Feb 2014 16:09:28 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp4096vcz; Tue, 4 Feb 2014 16:09:27 -0800 (PST) X-Received: by 10.68.171.99 with SMTP id at3mr47035218pbc.109.1391558967347; Tue, 04 Feb 2014 16:09:27 -0800 (PST) Received: from mail-pa0-f54.google.com (mail-pa0-f54.google.com [209.85.220.54]) by mx.google.com with ESMTPS id va10si26566588pbc.188.2014.02.04.16.09.26 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 04 Feb 2014 16:09:27 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.54 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) client-ip=209.85.220.54; Received: by mail-pa0-f54.google.com with SMTP id fa1so9247209pad.27 for ; Tue, 04 Feb 2014 16:09:26 -0800 (PST) X-Received: by 10.66.246.229 with SMTP id xz5mr47519572pac.119.1391558957948; Tue, 04 Feb 2014 16:09:17 -0800 (PST) Received: from localhost.localdomain (c-67-170-153-23.hsd1.or.comcast.net. [67.170.153.23]) by mx.google.com with ESMTPSA id g6sm181483447pat.2.2014.02.04.16.09.16 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 04 Feb 2014 16:09:17 -0800 (PST) From: John Stultz To: LKML Cc: Todd Poynor , Greg KH , Colin Cross , Android Kernel Team , John Stultz Subject: [PATCH 4/7] staging: ashmem: Avoid deadlock between read and mmap calls Date: Tue, 4 Feb 2014 16:08:37 -0800 Message-Id: <1391558920-31590-5-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1391558920-31590-1-git-send-email-john.stultz@linaro.org> References: <1391558920-31590-1-git-send-email-john.stultz@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.177 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Todd Poynor Avoid holding ashmem_mutex across code that can page fault. Page faults grab the mmap_sem for the process, which are also held by mmap calls prior to calling ashmem_mmap, which locks ashmem_mutex. The reversed order of locking between the two can deadlock. The calls that can page fault are read() and the ASHMEM_SET_NAME and ASHMEM_GET_NAME ioctls. Move the code that accesses userspace pages outside the ashmem_mutex. Cc: Greg KH Cc: Colin Cross Cc: Android Kernel Team Signed-off-by: Todd Poynor [jstultz: minor commit message tweaks] Signed-off-by: John Stultz --- drivers/staging/android/ashmem.c | 45 +++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/drivers/staging/android/ashmem.c b/drivers/staging/android/ashmem.c index 23948f1..713a972 100644 --- a/drivers/staging/android/ashmem.c +++ b/drivers/staging/android/ashmem.c @@ -295,21 +295,29 @@ static ssize_t ashmem_read(struct file *file, char __user *buf, /* If size is not set, or set to 0, always return EOF. */ if (asma->size == 0) - goto out; + goto out_unlock; if (!asma->file) { ret = -EBADF; - goto out; + goto out_unlock; } - ret = asma->file->f_op->read(asma->file, buf, len, pos); - if (ret < 0) - goto out; + mutex_unlock(&ashmem_mutex); - /** Update backing file pos, since f_ops->read() doesn't */ - asma->file->f_pos = *pos; + /* + * asma and asma->file are used outside the lock here. We assume + * once asma->file is set it will never be changed, and will not + * be destroyed until all references to the file are dropped and + * ashmem_release is called. + */ + ret = asma->file->f_op->read(asma->file, buf, len, pos); + if (ret >= 0) { + /** Update backing file pos, since f_ops->read() doesn't */ + asma->file->f_pos = *pos; + } + return ret; -out: +out_unlock: mutex_unlock(&ashmem_mutex); return ret; } @@ -498,6 +506,7 @@ out: static int set_name(struct ashmem_area *asma, void __user *name) { + int len; int ret = 0; char local_name[ASHMEM_NAME_LEN]; @@ -510,21 +519,19 @@ static int set_name(struct ashmem_area *asma, void __user *name) * variable that does not need protection and later copy the local * variable to the structure member with lock held. */ - if (copy_from_user(local_name, name, ASHMEM_NAME_LEN)) - return -EFAULT; - + len = strncpy_from_user(local_name, name, ASHMEM_NAME_LEN); + if (len < 0) + return len; + if (len == ASHMEM_NAME_LEN) + local_name[ASHMEM_NAME_LEN - 1] = '\0'; mutex_lock(&ashmem_mutex); /* cannot change an existing mapping's name */ - if (unlikely(asma->file)) { + if (unlikely(asma->file)) ret = -EINVAL; - goto out; - } - memcpy(asma->name + ASHMEM_NAME_PREFIX_LEN, - local_name, ASHMEM_NAME_LEN); - asma->name[ASHMEM_FULL_NAME_LEN-1] = '\0'; -out: - mutex_unlock(&ashmem_mutex); + else + strcpy(asma->name + ASHMEM_NAME_PREFIX_LEN, local_name); + mutex_unlock(&ashmem_mutex); return ret; }