From patchwork Mon Aug 28 21:34:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 806788 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="f4+KzIIL"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xh55P4m6Zz9t2R for ; Tue, 29 Aug 2017 07:49:29 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751582AbdH1Vf1 (ORCPT ); Mon, 28 Aug 2017 17:35:27 -0400 Received: from mail-pg0-f53.google.com ([74.125.83.53]:37241 "EHLO mail-pg0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751499AbdH1VfW (ORCPT ); Mon, 28 Aug 2017 17:35:22 -0400 Received: by mail-pg0-f53.google.com with SMTP id 83so4928364pgb.4 for ; Mon, 28 Aug 2017 14:35:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GwIrOyNMM43PFCitRpZ60qqOK3YH+Fds+Kgy/vS6VOY=; b=f4+KzIILDPsYJ5MFyqO/BQ3bEzybNX5HA7P5QGReuBJqlZiVAFrrhOjWpxeY5kSMbS QCjHkDRZ8+fZGvAwv5zg8L7AAboQRTGAxpEUC1VF2lK6fix6Ml0xeDJ/AHjLB7+IYBea t0q7oAqDpGcBefqsD46dsJkyW4j6IKt8UAg6I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=GwIrOyNMM43PFCitRpZ60qqOK3YH+Fds+Kgy/vS6VOY=; b=q16GZv5pBMZ+0MZ2dgY7aBmh1W4lnzP9vdGcelRyHPBaNCMupsDce1aQasttt5aS34 QRhLoYnrHSg0zdSnoe7Cnq+0Wf/lCjQj8wp3yKKJ4OTOohAZ7tovn00GF5W3C2TtBBZI FJ66Zj2W3WnTdOnFtFYKaUM+1NVSOtKXuTTuGQqtIMSlAg3laJvnseUloKlgEoPJC/p/ u7I300bB0D40IeXkZ7PfvhHLmYbQd9kROCakd2w4TC/BD+1JjXdhdaL6ePB9ZmdR/REs ZkCEiUS18gwMWzfpgE9/2/1mEwEBmEQaHwxSF6KPPBxSPoeiaU/gtnAekAs0dTgWPSsZ mGGg== X-Gm-Message-State: AHYfb5hfRrHHIAZUxt3zqjpXFY64SsRKxGwgrXr4Pj1x6ujUHHEr3XX6 +U7Fl+Jl1r8A5H/B X-Received: by 10.84.210.228 with SMTP id a91mr2362526pli.66.1503956122031; Mon, 28 Aug 2017 14:35:22 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id d67sm2196059pfg.27.2017.08.28.14.35.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Aug 2017 14:35:19 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , David Windsor , "Theodore Ts'o" , Andreas Dilger , linux-ext4@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH v2 07/30] ext4: Define usercopy region in ext4_inode_cache slab cache Date: Mon, 28 Aug 2017 14:34:48 -0700 Message-Id: <1503956111-36652-8-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1503956111-36652-1-git-send-email-keescook@chromium.org> References: <1503956111-36652-1-git-send-email-keescook@chromium.org> Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: David Windsor The ext4 symlink pathnames, stored in struct ext4_inode_info.i_data and therefore contained in the ext4_inode_cache slab cache, need to be copied to/from userspace. cache object allocation: fs/ext4/super.c: ext4_alloc_inode(...): struct ext4_inode_info *ei; ... ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS); ... return &ei->vfs_inode; include/trace/events/ext4.h: #define EXT4_I(inode) \ (container_of(inode, struct ext4_inode_info, vfs_inode)) fs/ext4/namei.c: ext4_symlink(...): ... inode->i_link = (char *)&EXT4_I(inode)->i_data; example usage trace: readlink_copy+0x43/0x70 vfs_readlink+0x62/0x110 SyS_readlinkat+0x100/0x130 fs/namei.c: readlink_copy(..., link): ... copy_to_user(..., link, len) (inlined into vfs_readlink) generic_readlink(dentry, ...): struct inode *inode = d_inode(dentry); const char *link = inode->i_link; ... readlink_copy(..., link); In support of usercopy hardening, this patch defines a region in the ext4_inode_cache slab cache in which userspace copy operations are allowed. This region is known as the slab cache's usercopy region. Slab caches can now check that each copy operation involving cache-managed memory falls entirely within the slab's usercopy region. This patch is modified from Brad Spengler/PaX Team's PAX_USERCOPY whitelisting code in the last public patch of grsecurity/PaX based on my understanding of the code. Changes or omissions from the original code are mine and don't reflect the original grsecurity/PaX code. Signed-off-by: David Windsor [kees: adjust commit log, provide usage trace] Cc: "Theodore Ts'o" Cc: Andreas Dilger Cc: linux-ext4@vger.kernel.org Signed-off-by: Kees Cook --- fs/ext4/super.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 0886fe82e9c4..79c3b1b11364 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -1038,11 +1038,13 @@ static void init_once(void *foo) static int __init init_inodecache(void) { - ext4_inode_cachep = kmem_cache_create("ext4_inode_cache", - sizeof(struct ext4_inode_info), - 0, (SLAB_RECLAIM_ACCOUNT| - SLAB_MEM_SPREAD|SLAB_ACCOUNT), - init_once); + ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache", + sizeof(struct ext4_inode_info), 0, + (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| + SLAB_ACCOUNT), + offsetof(struct ext4_inode_info, i_data), + sizeof_field(struct ext4_inode_info, i_data), + init_once); if (ext4_inode_cachep == NULL) return -ENOMEM; return 0; From patchwork Mon Aug 28 21:34:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 806789 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="jKFan3qB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xh55Y39vqz9t2c for ; Tue, 29 Aug 2017 07:49:37 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751546AbdH1VfZ (ORCPT ); Mon, 28 Aug 2017 17:35:25 -0400 Received: from mail-pg0-f52.google.com ([74.125.83.52]:33779 "EHLO mail-pg0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751194AbdH1VfX (ORCPT ); Mon, 28 Aug 2017 17:35:23 -0400 Received: by mail-pg0-f52.google.com with SMTP id t3so5023914pgt.0 for ; Mon, 28 Aug 2017 14:35:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZZCTRJwpDkZVPOclHxzOkNUiLhC/JhJGmyLmxiRZFcA=; b=jKFan3qBQiq9B9JRZreTDPt1YcHRpTe2T9o9dtineqH/NWxdX5EuB4kNz+SdRqV1o/ 3sgjgLyLbPd45VBm8Yo62+MnAAYTTMGDAxAroZVC35AmDC8zFqlXZlvIFvcX/t+Z+twz DApL3mg7taQ/UI5SXfRFKwH/vajO5fq4spQKs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZZCTRJwpDkZVPOclHxzOkNUiLhC/JhJGmyLmxiRZFcA=; b=qG8TMxLwYBU6aasJIL7k+vsHFdkptDsCl5Zi2CY+A6TVYxCw3DGfdJiIXkQPbowccj SuCLAxKKH1lexpjrGNcicdCRCEKmmSyJgU/jyX8L6PIwTxwyGPp1JqcuW7D5u7gwJHS+ gwv8XhsHb9RW9YgBo16SzQiw2WKY5x9xYKi82uV5yUgM1wE7jjdBevOCZX7/8HgOgssr au0cJV2GJFL7u0Vpt/N0x3gvhcRVEMW0NXbO+e9GkNcekqIeaD/5G831r0+o35AVlCUE 4Rnwf0EqTB8d3xqAC5RcsGEI5awOCz6ht70yPzdYkgnpXQw6RiyPWvWUU2+KTF2Zyka8 /+Tw== X-Gm-Message-State: AHYfb5gn6folo5bEQ6KhlV0RqsbAvOZvhGPm3PRMtui2gIBfiB/se9+y YU1tdrh723Xk5w6R X-Received: by 10.99.180.2 with SMTP id s2mr1790135pgf.183.1503956122755; Mon, 28 Aug 2017 14:35:22 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id b8sm2248006pfl.16.2017.08.28.14.35.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Aug 2017 14:35:19 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , David Windsor , Jan Kara , linux-ext4@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH v2 08/30] ext2: Define usercopy region in ext2_inode_cache slab cache Date: Mon, 28 Aug 2017 14:34:49 -0700 Message-Id: <1503956111-36652-9-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1503956111-36652-1-git-send-email-keescook@chromium.org> References: <1503956111-36652-1-git-send-email-keescook@chromium.org> Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: David Windsor The ext2 symlink pathnames, stored in struct ext2_inode_info.i_data and therefore contained in the ext2_inode_cache slab cache, need to be copied to/from userspace. cache object allocation: fs/ext2/super.c: ext2_alloc_inode(...): struct ext2_inode_info *ei; ... ei = kmem_cache_alloc(ext2_inode_cachep, GFP_NOFS); ... return &ei->vfs_inode; fs/ext2/ext2.h: EXT2_I(struct inode *inode): return container_of(inode, struct ext2_inode_info, vfs_inode); fs/ext2/namei.c: ext2_symlink(...): ... inode->i_link = (char *)&EXT2_I(inode)->i_data; example usage trace: readlink_copy+0x43/0x70 vfs_readlink+0x62/0x110 SyS_readlinkat+0x100/0x130 fs/namei.c: readlink_copy(..., link): ... copy_to_user(..., link, len); (inlined into vfs_readlink) generic_readlink(dentry, ...): struct inode *inode = d_inode(dentry); const char *link = inode->i_link; ... readlink_copy(..., link); In support of usercopy hardening, this patch defines a region in the ext2_inode_cache slab cache in which userspace copy operations are allowed. This region is known as the slab cache's usercopy region. Slab caches can now check that each copy operation involving cache-managed memory falls entirely within the slab's usercopy region. This patch is modified from Brad Spengler/PaX Team's PAX_USERCOPY whitelisting code in the last public patch of grsecurity/PaX based on my understanding of the code. Changes or omissions from the original code are mine and don't reflect the original grsecurity/PaX code. Signed-off-by: David Windsor [kees: adjust commit log, provide usage trace] Cc: Jan Kara Cc: linux-ext4@vger.kernel.org Signed-off-by: Kees Cook Acked-by: Jan Kara --- fs/ext2/super.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/fs/ext2/super.c b/fs/ext2/super.c index 7b1bc9059863..670142cde59d 100644 --- a/fs/ext2/super.c +++ b/fs/ext2/super.c @@ -219,11 +219,13 @@ static void init_once(void *foo) static int __init init_inodecache(void) { - ext2_inode_cachep = kmem_cache_create("ext2_inode_cache", - sizeof(struct ext2_inode_info), - 0, (SLAB_RECLAIM_ACCOUNT| - SLAB_MEM_SPREAD|SLAB_ACCOUNT), - init_once); + ext2_inode_cachep = kmem_cache_create_usercopy("ext2_inode_cache", + sizeof(struct ext2_inode_info), 0, + (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| + SLAB_ACCOUNT), + offsetof(struct ext2_inode_info, i_data), + sizeof_field(struct ext2_inode_info, i_data), + init_once); if (ext2_inode_cachep == NULL) return -ENOMEM; return 0;