From patchwork Fri Jul 24 10:04:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Pawelczyk X-Patchwork-Id: 6859021 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 E27B49F380 for ; Fri, 24 Jul 2015 10:09:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DDF1020631 for ; Fri, 24 Jul 2015 10:09:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BE7F720609 for ; Fri, 24 Jul 2015 10:09:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753826AbbGXKIp (ORCPT ); Fri, 24 Jul 2015 06:08:45 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:31781 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752409AbbGXKFb (ORCPT ); Fri, 24 Jul 2015 06:05:31 -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 <0NRZ000H5LD4RQ90@mailout2.w1.samsung.com>; Fri, 24 Jul 2015 11:05:28 +0100 (BST) X-AuditID: cbfec7f4-f79c56d0000012ee-81-55b20de793cb Received: from eusync2.samsung.com ( [203.254.199.212]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id E8.FE.04846.7ED02B55; Fri, 24 Jul 2015 11:05:27 +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 <0NRZ00EPNLC23A60@eusync2.samsung.com>; Fri, 24 Jul 2015 11:05:27 +0100 (BST) From: Lukasz Pawelczyk To: "Eric W. Biederman" , "Serge E. Hallyn" , Al Viro , Alexey Dobriyan , Andrew Morton , Andy Lutomirski , Arnd Bergmann , Casey Schaufler , David Howells , Eric Dumazet , Eric Paris , Fabian Frederick , Greg KH , James Morris , Jiri Slaby , Joe Perches , John Johansen , Jonathan Corbet , Kees Cook , Lukasz Pawelczyk , Mauro Carvalho Chehab , NeilBrown , Oleg Nesterov , Paul Moore , Stephen Smalley , Tetsuo Handa , Zefan Li , 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: havner@gmail.com Subject: [PATCH v3 04/11] lsm: inode_pre_setxattr hook Date: Fri, 24 Jul 2015 12:04:38 +0200 Message-id: <1437732285-11524-5-git-send-email-l.pawelczyk@samsung.com> X-Mailer: git-send-email 2.4.3 In-reply-to: <1437732285-11524-1-git-send-email-l.pawelczyk@samsung.com> References: <1437732285-11524-1-git-send-email-l.pawelczyk@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrOIsWRmVeSWpSXmKPExsVy+t/xK7rPeTeFGix7bWExvdHLYs76NWwW fycdY7e4t+0Xm8WTA+2MFu+afrNY/N/Wwm7x9NgjdovOh2eZLbp+/2SzaF68ns3i/2sdi77H QRaz7z9msTjxpZXNYsqGD0wWZ7pzLc5MWshksbBtCYvFnr0nWSwu75rDZvGh5xGbxaSOXnaL 1WsbWC1WP6uw2Du/gcVi6/5WRovbk6azWMxuecdqcXj3YmaLyU8OsVicv3CO3eL83+OsDooe 99/8ZfH4/WsSo8eshl42j9kNF1k8ds66y+6xYFOpx7XdkR4tR96yeuxcu4rJ48SM3ywe++eu YfdY3DeZ1ePj01ssHlv6gUrPfo3yWLv3BWOAdBSXTUpqTmZZapG+XQJXxo+Pt1kLrmpWHNm0 jq2BsVupi5GTQ0LAROLwpzWsELaYxIV769m6GLk4hASWMkpcXXwGymlkklj1azMLSBWbgIHE 9wt7mUESIgJbOSUmbFjLCJJgFhCR+PPuIzuILSxgJvH59Skwm0VAVeLl7D4wm1fAXaLvfCcz xDo5iXPrv4AN5RTwkDh9+RNYjRBQTfesOewTGHkXMDKsYhRNLU0uKE5KzzXUK07MLS7NS9dL zs/dxAiJ3i87GBcfszrEKMDBqMTDe2DSxlAh1sSy4srcQ4wSHMxKIrwMx4BCvCmJlVWpRfnx RaU5qcWHGKU5WJTEeefueh8iJJCeWJKanZpakFoEk2Xi4JRqYPRP+HguMcas7o2+5LVNRtkV Z7Jq789e8aMjI/U4g/DFK9GT1nNufyGy57d5/uMu6/OuBhmfZkxy/Hm3cUOWUPDiN4vMpJqm 35CfILnHfLtoucIZ70NVRSr7pNZ6yDI7cL+3d00xO6cgbPNPqvGFf/Ibs41BX5Yxz2c4bcqQ dOTbynKD7NLWSCWW4oxEQy3mouJEAP0ZuLPaAgAA Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 --- 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 1751864..0aeed91 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 f0d2914..24f91e0 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 88a3b78..e1d2c6f 100644 --- a/security/security.c +++ b/security/security.c @@ -649,6 +649,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) { @@ -1666,6 +1676,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 =