Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2326108imm; Fri, 7 Sep 2018 14:42:52 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYwbhEmbNBJoPf4c2AMzyXc8PgUg9mJ1aHB9wvOCigVO8S+sBRHgAtSHiRBRE1qDWW3eKVr X-Received: by 2002:a17:902:6845:: with SMTP id f5-v6mr10134931pln.17.1536356571958; Fri, 07 Sep 2018 14:42:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536356571; cv=none; d=google.com; s=arc-20160816; b=QF0KQOEaWDaZCM4gioysy+CsyxO8ECJMR2Kdijg4q9IWlY4PxT5rTAlr+ZcsHSRtxG jCmTS2JsrmweAOsqPyF7d98wqvfEsSV+AvpTkOWvO5H2wHRuCNuUNPKb/UM1Ph0ndEa8 HNXypLipxCn0k+NqtaxhT9nDSP1YZtMYxlApZ/j7d3n4coVnmfT+L++N6xGVsyQqhAlm 0KJ5nctqttyuFCaEMLhonLArG31dLyrkfvcrcrDh1eOxEQCtk/wVQzYhgPQ8LLNHE1PL J35SIVjDvd+ZRhlZ7xAhoW119kl5+ZSETxS9pskxzZZqJOUUWEFfairRNx855xpXv3qi qDFQ== 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; bh=Espv6WfMoSnEpaMF+o7wi4zzyIvZyDfFATMEQr7hK4g=; b=m04/hokDbszAttUIP11rILvcuayltp392Q6CT9XRK7PJgHznNcy4FjSW6HkX+bA5N4 Lo81grnheaMAyLkUaa12vVdUKDMMcZM8j75b2Nxz8aGxd0m2HrkH+wOMTmsopNl+ljnk 4RZyWuWkzBk5ZhRrI/sd4JLv1d9W0ZMeMGABg5MXUu7eZNlxyTBhl1ID+eOQh62UM7Zn ar3Uu8c6wYMCjNZTwpamz/eFn/VqHBsstfQ3LqUvq3hUxdk2jQIxqnkgOSxmyfS0ImAn mPVsuF2UkkG/I6Y1dImRYQOkaDFzg5AHSVS3NNtQIowcsPhR2nE6I26SNXdCmYYDl0+9 dCJQ== 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 x37-v6si10112471pgl.544.2018.09.07.14.42.36; Fri, 07 Sep 2018 14:42:51 -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 S1730236AbeIHCYF (ORCPT + 99 others); Fri, 7 Sep 2018 22:24:05 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:54852 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728220AbeIHCYE (ORCPT ); Fri, 7 Sep 2018 22:24:04 -0400 Received: from localhost (ip-213-127-74-90.ip.prioritytelecom.net [213.127.74.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id DCFA61010; Fri, 7 Sep 2018 21:41:08 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Christian Brauner , Serge Hallyn , "Eric W. Biederman" Subject: [PATCH 4.4 34/47] userns: move user access out of the mutex Date: Fri, 7 Sep 2018 23:10:30 +0200 Message-Id: <20180907210908.241189276@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180907210904.607934824@linuxfoundation.org> References: <20180907210904.607934824@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: Jann Horn commit 5820f140edef111a9ea2ef414ab2428b8cb805b1 upstream. The old code would hold the userns_state_mutex indefinitely if memdup_user_nul stalled due to e.g. a userfault region. Prevent that by moving the memdup_user_nul in front of the mutex_lock(). Note: This changes the error precedence of invalid buf/count/*ppos vs map already written / capabilities missing. Fixes: 22d917d80e84 ("userns: Rework the user_namespace adding uid/gid...") Cc: stable@vger.kernel.org Signed-off-by: Jann Horn Acked-by: Christian Brauner Acked-by: Serge Hallyn Signed-off-by: Eric W. Biederman Signed-off-by: Greg Kroah-Hartman --- kernel/user_namespace.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -604,7 +604,16 @@ static ssize_t map_write(struct file *fi struct uid_gid_extent *extent = NULL; unsigned long page = 0; char *kbuf, *pos, *next_line; - ssize_t ret = -EINVAL; + ssize_t ret; + + /* Only allow < page size writes at the beginning of the file */ + if ((*ppos != 0) || (count >= PAGE_SIZE)) + return -EINVAL; + + /* Slurp in the user data */ + if (copy_from_user(kbuf, buf, count)) + return -EFAULT; + kbuf[count] = '\0'; /* * The userns_state_mutex serializes all writes to any given map. @@ -645,17 +654,6 @@ static ssize_t map_write(struct file *fi if (!page) goto out; - /* Only allow < page size writes at the beginning of the file */ - ret = -EINVAL; - if ((*ppos != 0) || (count >= PAGE_SIZE)) - goto out; - - /* Slurp in the user data */ - ret = -EFAULT; - if (copy_from_user(kbuf, buf, count)) - goto out; - kbuf[count] = '\0'; - /* Parse the user data */ ret = -EINVAL; pos = kbuf;