From patchwork Mon Feb 3 18:16:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 24050 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ob0-f198.google.com (mail-ob0-f198.google.com [209.85.214.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1CEC8202B2 for ; Mon, 3 Feb 2014 18:16:46 +0000 (UTC) Received: by mail-ob0-f198.google.com with SMTP id wp4sf29147184obc.9 for ; Mon, 03 Feb 2014 10:16:46 -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=K40H/YfVMcT4r2BXS92yAks0PvHJSWfH3gxQRBy4h7WFkxkZNHY1jwW8/gNM8z1Vkx sOUqWPgX2pbM9FKTZkAg4DCOKQOtrcTeYEBCGnYvrtRv7X+cTI6fkPojlk3DUo/z+3em T3O1dTEIu2/fNqzsWnE8h1lFIG9G3npStD4QWjv8uhvjv7uKCP6C5DLZl20darLCGjdb KOif4TQwSwJHs1samICRlfCjJV+lju47yVCmagDMfDD51zTDBcL/BJR6nD5SYZyKYRGx 659uTlI5ldD+hDve/7NdJI96GPaIv6UlJQO7VCxExeJ4ljVTFSLpqOAxPSE6h+HGdmUF aevA== X-Gm-Message-State: ALoCoQm5wg8zjmcLqHhE9x0EzaGRDwVtaYc9jFXFtENCa57+kYb0gTPlO8Cy3GNw87g+dRLJxoyd X-Received: by 10.182.74.226 with SMTP id x2mr14066036obv.1.1391451405973; Mon, 03 Feb 2014 10:16:45 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.97.102 with SMTP id l93ls2082961qge.93.gmail; Mon, 03 Feb 2014 10:16:45 -0800 (PST) X-Received: by 10.52.248.10 with SMTP id yi10mr905252vdc.39.1391451405878; Mon, 03 Feb 2014 10:16:45 -0800 (PST) Received: from mail-ve0-f179.google.com (mail-ve0-f179.google.com [209.85.128.179]) by mx.google.com with ESMTPS id yb7si7038779vec.39.2014.02.03.10.16.45 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 03 Feb 2014 10:16:45 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.179 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.179; Received: by mail-ve0-f179.google.com with SMTP id jx11so5176042veb.38 for ; Mon, 03 Feb 2014 10:16:45 -0800 (PST) X-Received: by 10.221.29.196 with SMTP id rz4mr14821041vcb.8.1391451405788; Mon, 03 Feb 2014 10:16:45 -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 u4csp174177vcz; Mon, 3 Feb 2014 10:16:45 -0800 (PST) X-Received: by 10.68.0.35 with SMTP id 3mr38482026pbb.52.1391451404763; Mon, 03 Feb 2014 10:16:44 -0800 (PST) Received: from mail-pa0-f52.google.com (mail-pa0-f52.google.com [209.85.220.52]) by mx.google.com with ESMTPS id nf8si21510265pbc.120.2014.02.03.10.16.44 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 03 Feb 2014 10:16:44 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.52 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) client-ip=209.85.220.52; Received: by mail-pa0-f52.google.com with SMTP id bj1so7366450pad.25 for ; Mon, 03 Feb 2014 10:16:44 -0800 (PST) X-Received: by 10.68.130.169 with SMTP id of9mr38837888pbb.79.1391451404222; Mon, 03 Feb 2014 10:16:44 -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 y9sm150496099pas.10.2014.02.03.10.16.42 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 03 Feb 2014 10:16:43 -0800 (PST) From: John Stultz To: LKML Cc: Todd Poynor , Greg KH , Colin Cross , Android Kernel Team , John Stultz Subject: [PATCH 08/16] staging: ashmem: Avoid deadlock between read and mmap calls Date: Mon, 3 Feb 2014 10:16:20 -0800 Message-Id: <1391451388-23906-9-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1391451388-23906-1-git-send-email-john.stultz@linaro.org> References: <1391451388-23906-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.128.179 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; }