Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp447238ybl; Tue, 28 Jan 2020 06:08:46 -0800 (PST) X-Google-Smtp-Source: APXvYqzpvguPrtGKWDv0GA9D3rvEu4NjngA6yYIJGlA2cDkCzAjfUfdP+hgWlor9kVHlG8gH6W3J X-Received: by 2002:a9d:ec7:: with SMTP id 65mr7801109otj.309.1580220525911; Tue, 28 Jan 2020 06:08:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580220525; cv=none; d=google.com; s=arc-20160816; b=gaRHNXtrCM/H8bm2/+ylaHHj47hZv6jsK7A14crjQSpnVXrt+T7mySeYCCuwaMj/6k PD3wNu7mJg+wOZ3vJ6vubOSZch3alKn5YPvwnwiQBSAXt2/V8WG41HWjbUtxilhgdvkg h4WepHcs8bWlBdZK5kDAW8rTmEA8rW/LQSmu/qmDO7jIpRS6dr7ym9OyDzDQf7eZnIey 6ztV3/EH0pGEIQ1+XASaHVUn4MtO8hRSxbqDUQeN5XG+ahtTYPwp5D94mV9RAIcWSD4Y Nh4CYh2RDZeRH+rLJbOHgG7dGmYTReKmLP0Ln26OiXKJVmsdrju2AO9BJ5fiaGkYu7vc rVnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9ukJsZE3QBuShZ4Omgv/B9zn1Fo2l+aLrg+Tgq2j+G8=; b=xm7Awv1SrNyE1Dv/U6COc8cfCQtBBgWucJhmr9AD0wkpQ0wXnUAsSNBTM+q0mrZeSr CFadrvegFPifbujN44zJJ+nviisMFdjL7xNN9ax8r5aZcF1Pb3nqI5WpD45D0pFcPvTT HfKIaUFkNOuEKCrnJK9oR2wUbB8EDcqjYq7pGU3j41kLwLxAGV2GkARYG5lpov+ErMCv 1hWPXZUhpNTnJwSFGelTvnJR8xxeNEpwmbxjWgjinEwWOOwoAI7PlljlFm/ngL1W+j7g 1lGWBQO7nwnPORvYEXXd8w5ZM3rpG0travGmXRV2+X6yHyMYEsZN4Ljc1ziQ+WfVDYNS rcpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JE56JGkh; 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 a8si4144944otp.258.2020.01.28.06.08.28; Tue, 28 Jan 2020 06:08:45 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=JE56JGkh; 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 S1727992AbgA1OEC (ORCPT + 99 others); Tue, 28 Jan 2020 09:04:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:50934 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727266AbgA1OEA (ORCPT ); Tue, 28 Jan 2020 09:04:00 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 02FAA24685; Tue, 28 Jan 2020 14:03:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580220239; bh=tKyRT6zwCMjcUPxdZNGLWuSNWGv/b1e6eBAIZMS8VkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JE56JGkhb/XVk75ZTHZNkAb4Bq95mo4Wc0/QxSOHx2XkqCsW4qB5+hoG/fYOQOvhA S98/MDb9da5lN4cmHt1kMzyA2elNC8Lp1RpFxSjXyFmWL6nolr75/G97fCdSIDgEK7 hgPo7d3vaMPQl8WTU+C7m+KbZReP/sNhNCIelkzA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christophe Leroy , Linus Torvalds Subject: [PATCH 5.4 071/104] lib: Reduce user_access_begin() boundaries in strncpy_from_user() and strnlen_user() Date: Tue, 28 Jan 2020 15:00:32 +0100 Message-Id: <20200128135827.156445118@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200128135817.238524998@linuxfoundation.org> References: <20200128135817.238524998@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Christophe Leroy commit ab10ae1c3bef56c29bac61e1201c752221b87b41 upstream. The range passed to user_access_begin() by strncpy_from_user() and strnlen_user() starts at 'src' and goes up to the limit of userspace although reads will be limited by the 'count' param. On 32 bits powerpc (book3s/32) access has to be granted for each 256Mbytes segment and the cost increases with the number of segments to unlock. Limit the range with 'count' param. Fixes: 594cc251fdd0 ("make 'user_access_begin()' do 'access_ok()'") Signed-off-by: Christophe Leroy Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- lib/strncpy_from_user.c | 14 +++++++------- lib/strnlen_user.c | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) --- a/lib/strncpy_from_user.c +++ b/lib/strncpy_from_user.c @@ -30,13 +30,6 @@ static inline long do_strncpy_from_user( const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; unsigned long res = 0; - /* - * Truncate 'max' to the user-specified limit, so that - * we only have one limit we need to check in the loop - */ - if (max > count) - max = count; - if (IS_UNALIGNED(src, dst)) goto byte_at_a_time; @@ -114,6 +107,13 @@ long strncpy_from_user(char *dst, const unsigned long max = max_addr - src_addr; long retval; + /* + * Truncate 'max' to the user-specified limit, so that + * we only have one limit we need to check in the loop + */ + if (max > count) + max = count; + kasan_check_write(dst, count); check_object_size(dst, count, false); if (user_access_begin(src, max)) { --- a/lib/strnlen_user.c +++ b/lib/strnlen_user.c @@ -27,13 +27,6 @@ static inline long do_strnlen_user(const unsigned long c; /* - * Truncate 'max' to the user-specified limit, so that - * we only have one limit we need to check in the loop - */ - if (max > count) - max = count; - - /* * Do everything aligned. But that means that we * need to also expand the maximum.. */ @@ -109,6 +102,13 @@ long strnlen_user(const char __user *str unsigned long max = max_addr - src_addr; long retval; + /* + * Truncate 'max' to the user-specified limit, so that + * we only have one limit we need to check in the loop + */ + if (max > count) + max = count; + if (user_access_begin(str, max)) { retval = do_strnlen_user(str, count, max); user_access_end();