From patchwork Fri Aug 26 14:31:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 9301391 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 98768601C0 for ; Fri, 26 Aug 2016 14:32:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8B1932960E for ; Fri, 26 Aug 2016 14:32:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7FA3829619; Fri, 26 Aug 2016 14:32:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id A713C2960E for ; Fri, 26 Aug 2016 14:32:07 +0000 (UTC) Received: (qmail 11461 invoked by uid 550); 26 Aug 2016 14:32:06 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: kernel-hardening@lists.openwall.com Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 11435 invoked from network); 26 Aug 2016 14:32:04 -0000 From: Mark Rutland To: linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Cc: Mark Rutland , Andrew Morton , Kees Cook Date: Fri, 26 Aug 2016 15:31:43 +0100 Message-Id: <1472221903-31181-1-git-send-email-mark.rutland@arm.com> X-Mailer: git-send-email 2.7.4 Subject: [kernel-hardening] [PATCH] lib: harden strncpy_from_user X-Virus-Scanned: ClamAV using ClamSMTP The strncpy_from_user() accessor is effectively a copy_from_user() specialised to copy strings, terminating early at a NUL byte if possible. In other respects it is identical, and can be used to copy an arbitrarily large buffer from userspace into the kernel. Conceptually, it exposes a similar attack surface. As with copy_from_user(), we check the destination range when the kernel is built with KASAN, but unlike copy_from_user() we do not check the destination buffer when using HARDENED_USERCOPY. As strncpy_from_user() calls get_user() in a loop, we must call check_object_size() explicitly. This patch adds this instrumentation to strncpy_from_user(), per the same rationale as with the regular copy_from_user(). In the absence of hardened usercopy this will have no impact as the instrumentation expands to an empty static inline function. Signed-off-by: Mark Rutland Cc: Andrew Morton Cc: Kees Cook --- lib/strncpy_from_user.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c index 9c5fe81..7e35fc4 100644 --- a/lib/strncpy_from_user.c +++ b/lib/strncpy_from_user.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -111,6 +112,7 @@ long strncpy_from_user(char *dst, const char __user *src, long count) long retval; kasan_check_write(dst, count); + check_object_size(dst, count, false); user_access_begin(); retval = do_strncpy_from_user(dst, src, count, max); user_access_end();