From patchwork Wed Oct 14 12:41:58 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Pawelczyk X-Patchwork-Id: 7394341 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C270E9F1B9 for ; Wed, 14 Oct 2015 12:58:37 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BBA76206EB for ; Wed, 14 Oct 2015 12:58:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 11575206E2 for ; Wed, 14 Oct 2015 12:58:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753440AbbJNMmi (ORCPT ); Wed, 14 Oct 2015 08:42:38 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:51516 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753343AbbJNMmc (ORCPT ); Wed, 14 Oct 2015 08:42:32 -0400 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout2.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0NW700MPYNATPI60@mailout2.w1.samsung.com>; Wed, 14 Oct 2015 13:42:29 +0100 (BST) X-AuditID: cbfec7f4-f79c56d0000012ee-06-561e4db5d897 Received: from eusync2.samsung.com ( [203.254.199.212]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 86.C2.04846.5BD4E165; Wed, 14 Oct 2015 13:42:29 +0100 (BST) Received: from amdc2143.DIGITAL.local ([106.120.53.33]) by eusync2.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0NW700BB0NAK6E50@eusync2.samsung.com>; Wed, 14 Oct 2015 13:42:29 +0100 (BST) From: Lukasz Pawelczyk To: "David S. Miller" , "Eric W. Biederman" , "Serge E. Hallyn" , Al Viro , Alexey Dobriyan , Andrew Morton , Andy Lutomirski , Calvin Owens , Casey Schaufler , David Howells , Eric Dumazet , Eric Paris , Greg Kroah-Hartman , James Morris , Jann Horn , Jiri Slaby , Joe Perches , John Johansen , Jonathan Corbet , Kees Cook , Lukasz Pawelczyk , Mauro Carvalho Chehab , NeilBrown , Paul Moore , Serge Hallyn , Stephen Smalley , Tejun Heo , Tetsuo Handa , containers@lists.linuxfoundation.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@tycho.nsa.gov Cc: Lukasz Pawelczyk Subject: [PATCH v4 04/11] lsm: inode_pre_setxattr hook Date: Wed, 14 Oct 2015 14:41:58 +0200 Message-id: <1444826525-9758-5-git-send-email-l.pawelczyk@samsung.com> X-Mailer: git-send-email 2.4.3 In-reply-to: <1444826525-9758-1-git-send-email-l.pawelczyk@samsung.com> References: <1444826525-9758-1-git-send-email-l.pawelczyk@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrHIsWRmVeSWpSXmKPExsVy+t/xK7pbfeXCDP4dFrSY3uhlMWf9GjaL 9QvkLe5t+8VmcfrfCnaLJwfaGS3mnG9hsXjX9JvF4v+2FnaLp8cesVt0PjzLbNG8eD2bxf/X OhZ9j4MsTjx7ymox+/5jFosTX1rZLKZs+MBkcaY71+LMpIVMFgvblrBY7Nl7ksXi8q45bBYf eh6xWZzftZbVYvWzCou98xtYLG5Pms5iMbvlHavF4d2LmS0mPznEYnGu8TmLxfkL59gtfi0/ ymhx/u9xVgdlj1kNvWwesxsusnhsWXmTyWNi8zt2j52z7rJ7LNhU6nFtd6THzrWrmDw2repk 8zgx4zeLx/65a9g9fv+YzOixuG8yq8fHp7eAOvuB6s9+jQqQieKySUnNySxLLdK3S+DKmLNs NWtBl1bFzMsrGRsYdyt1MXJySAiYSNw+9YMZwhaTuHBvPVsXIxeHkMBSRokdS5dDOY1MEod+ TwarYhMwkPh+YS8zSEJEYAKXRMuEK2wgCWYBdYmls5tZQGxhATOJ332bWEFsFgFVic3/1gLV cHDwCrhJvOhPhNgmJ3Fu/RcWkDCngLvEllZbkLAQUMX+CXdZJjDyLmBkWMUomlqaXFCclJ5r qFecmFtcmpeul5yfu4kREstfdjAuPmZ1iFGAg1GJhzdjtWyYEGtiWXFl7iFGCQ5mJRHePwZy YUK8KYmVValF+fFFpTmpxYcYpTlYlMR55+56HyIkkJ5YkpqdmlqQWgSTZeLglGpgrFI5t0Y3 b+rzHw2iPD5uk/ldTGzvPTjQ6/hE8FxBas0tGbUJej/7Nk3YKHTf1uhl3ZHfH3XVHGtM7xmX J29a1LLnv0w0o8PnqNZ1Vy3XK7y7wuF52DX+xk6vE2Uf5tTuvRXFcuLCmfbfexrL5zUt9HP5 26vkKvdke0SxcJrnGeN1ExY8Dd2SpsRSnJFoqMVcVJwIAKtY+gzhAgAA Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a new LSM hook called before inode's setxattr. It is required for LSM to be able to reliably replace the xattr's value to be set to filesystem in __vfs_setxattr_noperm(). Useful for mapped values, like in the upcoming Smack namespace patches. Signed-off-by: Lukasz Pawelczyk Acked-by: Serge Hallyn Acked-by: Casey Schaufler Acked-by: John Johansen --- fs/xattr.c | 10 ++++++++++ include/linux/lsm_hooks.h | 9 +++++++++ include/linux/security.h | 10 ++++++++++ security/security.c | 12 ++++++++++++ 4 files changed, 41 insertions(+) diff --git a/fs/xattr.c b/fs/xattr.c index 072fee1..cbc8d19 100644 --- a/fs/xattr.c +++ b/fs/xattr.c @@ -100,12 +100,22 @@ int __vfs_setxattr_noperm(struct dentry *dentry, const char *name, if (issec) inode->i_flags &= ~S_NOSEC; if (inode->i_op->setxattr) { + bool alloc = false; + + error = security_inode_pre_setxattr(dentry, name, &value, + &size, flags, &alloc); + if (error) + return error; + error = inode->i_op->setxattr(dentry, name, value, size, flags); if (!error) { fsnotify_xattr(dentry); security_inode_post_setxattr(dentry, name, value, size, flags); } + + if (alloc) + kfree(value); } else if (issec) { const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; error = security_inode_setsecurity(inode, suffix, value, diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 4f16640..85bfdde 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -349,6 +349,11 @@ * Check permission before setting the extended attributes * @value identified by @name for @dentry. * Return 0 if permission is granted. + * @inode_pre_setxattr: + * Be able to do some operation before setting the @value identified + * by @name on the filesystem. Replacing the @value and its @size is + * possible. Useful for mapped values. Set @alloc to true if @value + * needs to be kfreed afterwards. * @inode_post_setxattr: * Update inode security field after successful setxattr operation. * @value identified by @name for @dentry. @@ -1448,6 +1453,9 @@ union security_list_options { int (*inode_getattr)(const struct path *path); int (*inode_setxattr)(struct dentry *dentry, const char *name, const void *value, size_t size, int flags); + int (*inode_pre_setxattr)(struct dentry *dentry, const char *name, + const void **value, size_t *size, + int flags, bool *alloc); void (*inode_post_setxattr)(struct dentry *dentry, const char *name, const void *value, size_t size, int flags); @@ -1730,6 +1738,7 @@ struct security_hook_heads { struct list_head inode_setattr; struct list_head inode_getattr; struct list_head inode_setxattr; + struct list_head inode_pre_setxattr; struct list_head inode_post_setxattr; struct list_head inode_getxattr; struct list_head inode_listxattr; diff --git a/include/linux/security.h b/include/linux/security.h index 12bd011..4de4865 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -263,6 +263,9 @@ int security_inode_setattr(struct dentry *dentry, struct iattr *attr); int security_inode_getattr(const struct path *path); int security_inode_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags); +int security_inode_pre_setxattr(struct dentry *dentry, const char *name, + const void **value, size_t *size, int flags, + bool *alloc); void security_inode_post_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags); int security_inode_getxattr(struct dentry *dentry, const char *name); @@ -691,6 +694,13 @@ static inline int security_inode_setxattr(struct dentry *dentry, return cap_inode_setxattr(dentry, name, value, size, flags); } +static inline int security_inode_pre_setxattr(struct dentry *dentry, + const char *name, const void **value, + size_t *size, int flags, bool *alloc) +{ + return 0; +} + static inline void security_inode_post_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags) { } diff --git a/security/security.c b/security/security.c index abfc207..75d46b6 100644 --- a/security/security.c +++ b/security/security.c @@ -644,6 +644,16 @@ int security_inode_setxattr(struct dentry *dentry, const char *name, return evm_inode_setxattr(dentry, name, value, size); } +int security_inode_pre_setxattr(struct dentry *dentry, const char *name, + const void **value, size_t *size, int flags, + bool *alloc) +{ + if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) + return 0; + return call_int_hook(inode_pre_setxattr, 0, dentry, name, value, size, + flags, alloc); +} + void security_inode_post_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags) { @@ -1662,6 +1672,8 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.inode_getattr), .inode_setxattr = LIST_HEAD_INIT(security_hook_heads.inode_setxattr), + .inode_pre_setxattr = + LIST_HEAD_INIT(security_hook_heads.inode_pre_setxattr), .inode_post_setxattr = LIST_HEAD_INIT(security_hook_heads.inode_post_setxattr), .inode_getxattr =