From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ozlabs.org (ozlabs.org [IPv6:2401:3900:2:1::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3rmgdX22JJzDqpb for ; Sat, 9 Jul 2016 15:58:24 +1000 (AEST) From: Michael Ellerman To: Kees Cook , Christoph Lameter Cc: "kernel-hardening\@lists.openwall.com" , Jan Kara , Catalin Marinas , Will Deacon , Linux-MM , sparclinux , linux-ia64@vger.kernel.org, Andrea Arcangeli , linux-arch , "x86\@kernel.org" , Russell King , PaX Team , Borislav Petkov , Mathias Krause , Fenghua Yu , Rik van Riel , David Rientjes , Tony Luck , Andy Lutomirski , Joonsoo Kim , Dmitry Vyukov , Laura Abbott , Brad Spengler , Ard Biesheuvel , LKML , Pekka Enberg , Case y Sc hauf ler , Andrew Morton , "linuxppc-dev\@lists.ozlabs.org" , "David S. Miller" , "linux-arm-kernel\@lists.infradead.org" Subject: Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support In-Reply-To: References: <577f7e55.4668420a.84f17.5cb9SMTPIN_ADDED_MISSING@mx.google.com> Date: Sat, 09 Jul 2016 15:58:20 +1000 Message-ID: <8737njpd37.fsf@@concordia.ellerman.id.au> MIME-Version: 1.0 Content-Type: text/plain List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Kees Cook writes: > On Fri, Jul 8, 2016 at 1:41 PM, Kees Cook wrote: >> So, as found already, the position in the usercopy check needs to be >> bumped down by red_left_pad, which is what Michael's fix does, so I'll >> include it in the next version. > > Actually, after some offline chats, I think this is better, since it > makes sure the ptr doesn't end up somewhere weird before we start the > calculations. This leaves the pointer as-is, but explicitly handles > the redzone on the offset instead, with no wrapping, etc: > > /* Find offset within object. */ > offset = (ptr - page_address(page)) % s->size; > > + /* Adjust for redzone and reject if within the redzone. */ > + if (s->flags & SLAB_RED_ZONE) { > + if (offset < s->red_left_pad) > + return s->name; > + offset -= s->red_left_pad; > + } > + > /* Allow address range falling entirely within object size. */ > if (offset <= s->object_size && n <= s->object_size - offset) > return NULL; That fixes the case for me in kstrndup(), which allows the system to boot. I then get two hits, which may or may not be valid: [ 2.309556] usercopy: kernel memory overwrite attempt detected to d000000003510028 (kernfs_node_cache) (64 bytes) [ 2.309995] CPU: 7 PID: 2241 Comm: wait-for-root Not tainted 4.7.0-rc3-00099-g97872fc89d41 #64 [ 2.310480] Call Trace: [ 2.310556] [c0000001f4773bf0] [c0000000009bdbe8] dump_stack+0xb0/0xf0 (unreliable) [ 2.311016] [c0000001f4773c30] [c00000000029cf44] __check_object_size+0x74/0x320 [ 2.311472] [c0000001f4773cb0] [c00000000005d4d0] copy_from_user+0x60/0xd4 [ 2.311873] [c0000001f4773cf0] [c0000000008b38f4] __get_filter+0x74/0x160 [ 2.312230] [c0000001f4773d30] [c0000000008b408c] sk_attach_filter+0x2c/0xc0 [ 2.312596] [c0000001f4773d60] [c000000000871c34] sock_setsockopt+0x954/0xc00 [ 2.313021] [c0000001f4773dd0] [c00000000086ac44] SyS_setsockopt+0x134/0x150 [ 2.313380] [c0000001f4773e30] [c000000000009260] system_call+0x38/0x108 [ 2.317045] usercopy: kernel memory overwrite attempt detected to d000000003530028 (kernfs_node_cache) (64 bytes) [ 2.317297] CPU: 10 PID: 2242 Comm: wait-for-root Not tainted 4.7.0-rc3-00099-g97872fc89d41 #64 [ 2.317475] Call Trace: [ 2.317511] [c0000001f471fbf0] [c0000000009bdbe8] dump_stack+0xb0/0xf0 (unreliable) [ 2.317689] [c0000001f471fc30] [c00000000029cf44] __check_object_size+0x74/0x320 [ 2.317861] [c0000001f471fcb0] [c00000000005d4d0] copy_from_user+0x60/0xd4 [ 2.318011] [c0000001f471fcf0] [c0000000008b38f4] __get_filter+0x74/0x160 [ 2.318165] [c0000001f471fd30] [c0000000008b408c] sk_attach_filter+0x2c/0xc0 [ 2.318313] [c0000001f471fd60] [c000000000871c34] sock_setsockopt+0x954/0xc00 [ 2.318485] [c0000001f471fdd0] [c00000000086ac44] SyS_setsockopt+0x134/0x150 [ 2.318632] [c0000001f471fe30] [c000000000009260] system_call+0x38/0x108 With: # zgrep SLUB /proc/config.gz CONFIG_SLUB_DEBUG=y CONFIG_SLUB=y CONFIG_SLUB_CPU_PARTIAL=y CONFIG_SLUB_DEBUG_ON=y # CONFIG_SLUB_STATS is not set cheers From mboxrd@z Thu Jan 1 00:00:00 1970 From: mpe@ellerman.id.au (Michael Ellerman) Date: Sat, 09 Jul 2016 15:58:20 +1000 Subject: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support In-Reply-To: References: <577f7e55.4668420a.84f17.5cb9SMTPIN_ADDED_MISSING@mx.google.com> Message-ID: <8737njpd37.fsf@@concordia.ellerman.id.au> To: linux-arm-kernel@lists.infradead.org List-Id: linux-arm-kernel.lists.infradead.org Kees Cook writes: > On Fri, Jul 8, 2016 at 1:41 PM, Kees Cook wrote: >> So, as found already, the position in the usercopy check needs to be >> bumped down by red_left_pad, which is what Michael's fix does, so I'll >> include it in the next version. > > Actually, after some offline chats, I think this is better, since it > makes sure the ptr doesn't end up somewhere weird before we start the > calculations. This leaves the pointer as-is, but explicitly handles > the redzone on the offset instead, with no wrapping, etc: > > /* Find offset within object. */ > offset = (ptr - page_address(page)) % s->size; > > + /* Adjust for redzone and reject if within the redzone. */ > + if (s->flags & SLAB_RED_ZONE) { > + if (offset < s->red_left_pad) > + return s->name; > + offset -= s->red_left_pad; > + } > + > /* Allow address range falling entirely within object size. */ > if (offset <= s->object_size && n <= s->object_size - offset) > return NULL; That fixes the case for me in kstrndup(), which allows the system to boot. I then get two hits, which may or may not be valid: [ 2.309556] usercopy: kernel memory overwrite attempt detected to d000000003510028 (kernfs_node_cache) (64 bytes) [ 2.309995] CPU: 7 PID: 2241 Comm: wait-for-root Not tainted 4.7.0-rc3-00099-g97872fc89d41 #64 [ 2.310480] Call Trace: [ 2.310556] [c0000001f4773bf0] [c0000000009bdbe8] dump_stack+0xb0/0xf0 (unreliable) [ 2.311016] [c0000001f4773c30] [c00000000029cf44] __check_object_size+0x74/0x320 [ 2.311472] [c0000001f4773cb0] [c00000000005d4d0] copy_from_user+0x60/0xd4 [ 2.311873] [c0000001f4773cf0] [c0000000008b38f4] __get_filter+0x74/0x160 [ 2.312230] [c0000001f4773d30] [c0000000008b408c] sk_attach_filter+0x2c/0xc0 [ 2.312596] [c0000001f4773d60] [c000000000871c34] sock_setsockopt+0x954/0xc00 [ 2.313021] [c0000001f4773dd0] [c00000000086ac44] SyS_setsockopt+0x134/0x150 [ 2.313380] [c0000001f4773e30] [c000000000009260] system_call+0x38/0x108 [ 2.317045] usercopy: kernel memory overwrite attempt detected to d000000003530028 (kernfs_node_cache) (64 bytes) [ 2.317297] CPU: 10 PID: 2242 Comm: wait-for-root Not tainted 4.7.0-rc3-00099-g97872fc89d41 #64 [ 2.317475] Call Trace: [ 2.317511] [c0000001f471fbf0] [c0000000009bdbe8] dump_stack+0xb0/0xf0 (unreliable) [ 2.317689] [c0000001f471fc30] [c00000000029cf44] __check_object_size+0x74/0x320 [ 2.317861] [c0000001f471fcb0] [c00000000005d4d0] copy_from_user+0x60/0xd4 [ 2.318011] [c0000001f471fcf0] [c0000000008b38f4] __get_filter+0x74/0x160 [ 2.318165] [c0000001f471fd30] [c0000000008b408c] sk_attach_filter+0x2c/0xc0 [ 2.318313] [c0000001f471fd60] [c000000000871c34] sock_setsockopt+0x954/0xc00 [ 2.318485] [c0000001f471fdd0] [c00000000086ac44] SyS_setsockopt+0x134/0x150 [ 2.318632] [c0000001f471fe30] [c000000000009260] system_call+0x38/0x108 With: # zgrep SLUB /proc/config.gz CONFIG_SLUB_DEBUG=y CONFIG_SLUB=y CONFIG_SLUB_CPU_PARTIAL=y CONFIG_SLUB_DEBUG_ON=y # CONFIG_SLUB_STATS is not set cheers From mboxrd@z Thu Jan 1 00:00:00 1970 Reply-To: kernel-hardening@lists.openwall.com From: Michael Ellerman In-Reply-To: References: <577f7e55.4668420a.84f17.5cb9SMTPIN_ADDED_MISSING@mx.google.com> Date: Sat, 09 Jul 2016 15:58:20 +1000 Message-ID: <8737njpd37.fsf@@concordia.ellerman.id.au> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support To: Kees Cook , Christoph Lameter Cc: "kernel-hardening@lists.openwall.com" , Jan Kara , Catalin Marinas , Will Deacon , Linux-MM , sparclinux , linux-ia64@vger.kernel.org, Andrea Arcangeli , linux-arch , "x86@kernel.org" , Russell King , PaX Team , Borislav Petkov , Mathias Krause , Fenghua Yu , Rik van Riel , David Rientjes , Tony Luck , Andy Lutomirski , Joonsoo Kim , Dmitry Vyukov , Laura Abbott , Brad Spengler , Ard Biesheuvel , LKML , Pekka Enberg , Case y Sc hauf ler , Andrew Morton , "linuxppc-dev@lists.ozlabs.org" , "David S. Miller" , "linux-arm-kernel@lists.infradead.org" List-ID: Kees Cook writes: > On Fri, Jul 8, 2016 at 1:41 PM, Kees Cook wrote: >> So, as found already, the position in the usercopy check needs to be >> bumped down by red_left_pad, which is what Michael's fix does, so I'll >> include it in the next version. > > Actually, after some offline chats, I think this is better, since it > makes sure the ptr doesn't end up somewhere weird before we start the > calculations. This leaves the pointer as-is, but explicitly handles > the redzone on the offset instead, with no wrapping, etc: > > /* Find offset within object. */ > offset = (ptr - page_address(page)) % s->size; > > + /* Adjust for redzone and reject if within the redzone. */ > + if (s->flags & SLAB_RED_ZONE) { > + if (offset < s->red_left_pad) > + return s->name; > + offset -= s->red_left_pad; > + } > + > /* Allow address range falling entirely within object size. */ > if (offset <= s->object_size && n <= s->object_size - offset) > return NULL; That fixes the case for me in kstrndup(), which allows the system to boot. I then get two hits, which may or may not be valid: [ 2.309556] usercopy: kernel memory overwrite attempt detected to d000000003510028 (kernfs_node_cache) (64 bytes) [ 2.309995] CPU: 7 PID: 2241 Comm: wait-for-root Not tainted 4.7.0-rc3-00099-g97872fc89d41 #64 [ 2.310480] Call Trace: [ 2.310556] [c0000001f4773bf0] [c0000000009bdbe8] dump_stack+0xb0/0xf0 (unreliable) [ 2.311016] [c0000001f4773c30] [c00000000029cf44] __check_object_size+0x74/0x320 [ 2.311472] [c0000001f4773cb0] [c00000000005d4d0] copy_from_user+0x60/0xd4 [ 2.311873] [c0000001f4773cf0] [c0000000008b38f4] __get_filter+0x74/0x160 [ 2.312230] [c0000001f4773d30] [c0000000008b408c] sk_attach_filter+0x2c/0xc0 [ 2.312596] [c0000001f4773d60] [c000000000871c34] sock_setsockopt+0x954/0xc00 [ 2.313021] [c0000001f4773dd0] [c00000000086ac44] SyS_setsockopt+0x134/0x150 [ 2.313380] [c0000001f4773e30] [c000000000009260] system_call+0x38/0x108 [ 2.317045] usercopy: kernel memory overwrite attempt detected to d000000003530028 (kernfs_node_cache) (64 bytes) [ 2.317297] CPU: 10 PID: 2242 Comm: wait-for-root Not tainted 4.7.0-rc3-00099-g97872fc89d41 #64 [ 2.317475] Call Trace: [ 2.317511] [c0000001f471fbf0] [c0000000009bdbe8] dump_stack+0xb0/0xf0 (unreliable) [ 2.317689] [c0000001f471fc30] [c00000000029cf44] __check_object_size+0x74/0x320 [ 2.317861] [c0000001f471fcb0] [c00000000005d4d0] copy_from_user+0x60/0xd4 [ 2.318011] [c0000001f471fcf0] [c0000000008b38f4] __get_filter+0x74/0x160 [ 2.318165] [c0000001f471fd30] [c0000000008b408c] sk_attach_filter+0x2c/0xc0 [ 2.318313] [c0000001f471fd60] [c000000000871c34] sock_setsockopt+0x954/0xc00 [ 2.318485] [c0000001f471fdd0] [c00000000086ac44] SyS_setsockopt+0x134/0x150 [ 2.318632] [c0000001f471fe30] [c000000000009260] system_call+0x38/0x108 With: # zgrep SLUB /proc/config.gz CONFIG_SLUB_DEBUG=y CONFIG_SLUB=y CONFIG_SLUB_CPU_PARTIAL=y CONFIG_SLUB_DEBUG_ON=y # CONFIG_SLUB_STATS is not set cheers