From patchwork Mon Aug 28 21:34:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9926299 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 B27DD60375 for ; Mon, 28 Aug 2017 21:40:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A38B328870 for ; Mon, 28 Aug 2017 21:40:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9D9192886C; Mon, 28 Aug 2017 21:40:06 +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=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, URIBL_BLACK autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB124287F1 for ; Mon, 28 Aug 2017 21:40:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751392AbdH1VfT (ORCPT ); Mon, 28 Aug 2017 17:35:19 -0400 Received: from mail-pg0-f45.google.com ([74.125.83.45]:35378 "EHLO mail-pg0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751258AbdH1VfR (ORCPT ); Mon, 28 Aug 2017 17:35:17 -0400 Received: by mail-pg0-f45.google.com with SMTP id 63so4969931pgc.2 for ; Mon, 28 Aug 2017 14:35:17 -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=AItuPkxoXP2lQMo2mwVBJC43AWaOTOyiRxkn5bbYjPo=; b=P82+WhcJ2w4t0itXvdr5hrcwttx2wLYcKQKPfoTJhEa0icshScC90UKxjdjIXCoe8U g4RKsT3grBA80FOefTMgzSptgRah6RR/sPaTKxqVxYDBpob2NGgoAWq8+ytSlOqrT4VK 5CPnJmmrFneXjKqS2iJNja1PXx4WfOPmG7rMI= 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=AItuPkxoXP2lQMo2mwVBJC43AWaOTOyiRxkn5bbYjPo=; b=TDF7Q4uVTSvv2IEYSv/5UWjLRx7dKgF6kBeuwug7ccJvhzpkm/OCQRVmnOG/Xg1vMw KJW/un/TqJcPAj9EPaauIh/6EhI/32ZuvI1KSV0V8k2uN1wBqdDAtG/rGw6cXS32XOAk KoNkJ2YnHYpEK1Kf0MJrjxDI3EU+bp8t7UFtMS31WQwGbcfnjVlDRt8LQGyqPqDgjXD8 sVyRefoqyxaceKbxW3C9TKJjUor1Lq4U+MN8GFiOabe45as55uCxZnj/GYbXQLOlR2Oz UgUYUODVlVDSR2csicFOpTyE191WhKMdATE68Ps1aNpyzLoO5xv53o6ZE47cjF7TcUiy XchA== X-Gm-Message-State: AHYfb5j4vOQNX3z+74kV80U9KZ+RGE0WOxSptIVd9ddFU6TFeFObZwX6 bCHQDpezrAGJj8UL X-Received: by 10.84.215.197 with SMTP id g5mr2335440plj.71.1503956117200; Mon, 28 Aug 2017 14:35:17 -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 u25sm1953438pfk.122.2017.08.28.14.35.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Aug 2017 14:35:15 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , David Windsor , Alexander Viro , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH v2 04/30] dcache: Define usercopy region in dentry_cache slab cache Date: Mon, 28 Aug 2017 14:34:45 -0700 Message-Id: <1503956111-36652-5-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-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: David Windsor When a dentry name is short enough, it can be stored directly in the dentry itself (instead in a separate kmalloc allocation). These dentry short names, stored in struct dentry.d_iname and therefore contained in the dentry_cache slab cache, need to be coped to userspace. cache object allocation: fs/dcache.c: __d_alloc(...): ... dentry = kmem_cache_alloc(dentry_cache, ...); ... dentry->d_name.name = dentry->d_iname; example usage trace: filldir+0xb0/0x140 dcache_readdir+0x82/0x170 iterate_dir+0x142/0x1b0 SyS_getdents+0xb5/0x160 fs/readdir.c: (called via ctx.actor by dir_emit) filldir(..., const char *name, ...): ... copy_to_user(..., name, namlen) fs/libfs.c: dcache_readdir(...): ... next = next_positive(dentry, p, 1) ... dir_emit(..., next->d_name.name, ...) In support of usercopy hardening, this patch defines a region in the dentry_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 hunks for kmalloc-specific things moved later] [kees: adjust commit log, provide usage trace] Cc: Alexander Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Kees Cook --- fs/dcache.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index f90141387f01..5f5e7c1fcf4b 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -3603,8 +3603,9 @@ static void __init dcache_init(void) * but it is probably not worth it because of the cache nature * of the dcache. */ - dentry_cache = KMEM_CACHE(dentry, - SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT); + dentry_cache = KMEM_CACHE_USERCOPY(dentry, + SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT, + d_iname); /* Hash may have been set up in dcache_init_early */ if (!hashdist)