Whamcloud - gitweb
LU-9193 security: return security context for metadata ops
[fs/lustre-release.git] / lustre / llite / xattr_security.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see http://www.gnu.org/licenses
18  *
19  * GPL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2014 Bull SAS
24  *
25  * Copyright (c) 2015, 2016, Intel Corporation.
26  * Author: Sebastien Buisson sebastien.buisson@bull.net
27  */
28
29 /*
30  * lustre/llite/xattr_security.c
31  * Handler for storing security labels as extended attributes.
32  */
33
34 #include <linux/types.h>
35 #include <linux/security.h>
36 #include <linux/selinux.h>
37 #include <linux/xattr.h>
38 #include "llite_internal.h"
39
40 #ifndef XATTR_SELINUX_SUFFIX
41 # define XATTR_SELINUX_SUFFIX "selinux"
42 #endif
43
44 #ifndef XATTR_NAME_SELINUX
45 # define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
46 #endif
47
48 /*
49  * Check for LL_SBI_FILE_SECCTX before calling.
50  */
51 int ll_dentry_init_security(struct dentry *dentry, int mode, struct qstr *name,
52                             const char **secctx_name, void **secctx,
53                             __u32 *secctx_size)
54 {
55 #ifdef HAVE_SECURITY_DENTRY_INIT_SECURITY
56         int rc;
57
58         /* security_dentry_init_security() is strange. Like
59          * security_inode_init_security() it may return a context (provided a
60          * Linux security module is enabled) but unlike
61          * security_inode_init_security() it does not return to us the name of
62          * the extended attribute to store the context under (for example
63          * "security.selinux"). So we only call it when we think we know what
64          * the name of the extended attribute will be. This is OK-ish since
65          * SELinux is the only module that implements
66          * security_dentry_init_security(). Note that the NFS client code just
67          * calls it and assumes that if anything is returned then it must come
68          * from SELinux. */
69
70         if (!selinux_is_enabled())
71                 return 0;
72
73         rc = security_dentry_init_security(dentry, mode, name, secctx,
74                                            secctx_size);
75         if (rc < 0)
76                 return rc;
77
78         *secctx_name = XATTR_NAME_SELINUX;
79 #endif /* HAVE_SECURITY_DENTRY_INIT_SECURITY */
80
81         return 0;
82 }
83
84 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
85 /**
86  * A helper function for ll_security_inode_init_security()
87  * that takes care of setting xattrs
88  *
89  * Get security context of @inode from @xattr_array,
90  * and put it in 'security.xxx' xattr of dentry
91  * stored in @fs_info.
92  *
93  * \retval 0        success
94  * \retval -ENOMEM  if no memory could be allocated for xattr name
95  * \retval < 0      failure to set xattr
96  */
97 static int
98 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
99               void *fs_info)
100 {
101         struct dentry *dentry = fs_info;
102         const struct xattr *xattr;
103         int err = 0;
104
105         for (xattr = xattr_array; xattr->name; xattr++) {
106                 char *full_name;
107
108                 full_name = kasprintf(GFP_KERNEL, "%s%s",
109                                       XATTR_SECURITY_PREFIX, xattr->name);
110                 if (!full_name) {
111                         err = -ENOMEM;
112                         break;
113                 }
114
115                 err = __vfs_setxattr(dentry, inode, full_name, xattr->value,
116                                      xattr->value_len, XATTR_CREATE);
117                 kfree(full_name);
118                 if (err < 0)
119                         break;
120         }
121         return err;
122 }
123
124 /**
125  * Initializes security context
126  *
127  * Get security context of @inode in @dir,
128  * and put it in 'security.xxx' xattr of @dentry.
129  *
130  * \retval 0        success, or SELinux is disabled
131  * \retval -ENOMEM  if no memory could be allocated for xattr name
132  * \retval < 0      failure to get security context or set xattr
133  */
134 int
135 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
136                        struct inode *dir)
137 {
138         if (!selinux_is_enabled())
139                 return 0;
140
141         return ll_security_inode_init_security(inode, dir, NULL, NULL, 0,
142                                                &ll_initxattrs, dentry);
143 }
144 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK */
145 /**
146  * Initializes security context
147  *
148  * Get security context of @inode in @dir,
149  * and put it in 'security.xxx' xattr of @dentry.
150  *
151  * \retval 0        success, or SELinux is disabled
152  * \retval -ENOMEM  if no memory could be allocated for xattr name
153  * \retval < 0      failure to get security context or set xattr
154  */
155 int
156 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
157                        struct inode *dir)
158 {
159         char *full_name;
160         void *value;
161         char *name;
162         size_t len;
163         int err;
164
165         if (!selinux_is_enabled())
166                 return 0;
167
168         err = ll_security_inode_init_security(inode, dir, &name, &value, &len,
169                                               NULL, dentry);
170         if (err != 0) {
171                 if (err == -EOPNOTSUPP)
172                         return 0;
173                 return err;
174         }
175
176         full_name = kasprintf(GFP_KERNEL, "%s%s", XATTR_SECURITY_PREFIX, name);
177         if (!full_name)
178                 GOTO(out_free, err = -ENOMEM);
179
180         err = __vfs_setxattr(dentry, inode, full_name, value, len,
181                              XATTR_CREATE);
182         kfree(full_name);
183 out_free:
184         kfree(name);
185         kfree(value);
186
187         return err;
188 }
189 #endif /* HAVE_SECURITY_IINITSEC_CALLBACK */
190
191 /**
192  * Get security context xattr name used by policy.
193  *
194  * \retval >= 0     length of xattr name
195  * \retval < 0      failure to get security context xattr name
196  */
197 int
198 ll_listsecurity(struct inode *inode, char *secctx_name, size_t secctx_name_size)
199 {
200         int rc;
201
202         if (!selinux_is_enabled())
203                 return 0;
204
205 #ifdef HAVE_SECURITY_INODE_LISTSECURITY
206         rc = security_inode_listsecurity(inode, secctx_name, secctx_name_size);
207         if (rc >= secctx_name_size)
208                 rc = -ERANGE;
209         else if (rc >= 0)
210                 secctx_name[rc] = '\0';
211         return rc;
212 #else /* !HAVE_SECURITY_INODE_LISTSECURITY */
213         rc = sizeof(XATTR_NAME_SELINUX);
214         if (secctx_name && rc < secctx_name_size) {
215                 memcpy(secctx_name, XATTR_NAME_SELINUX, rc);
216                 secctx_name[rc] = '\0';
217         } else {
218                 rc = -ERANGE;
219         }
220         return rc;
221 #endif /* HAVE_SECURITY_INODE_LISTSECURITY */
222 }