Whamcloud - gitweb
LU-5560 security: send file security context for creates
[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  * Author: Sebastien Buisson sebastien.buisson@bull.net
25  */
26
27 /*
28  * lustre/llite/xattr_security.c
29  * Handler for storing security labels as extended attributes.
30  */
31
32 #include <linux/types.h>
33 #include <linux/security.h>
34 #include <linux/selinux.h>
35 #include <linux/xattr.h>
36 #include "llite_internal.h"
37
38 #ifndef XATTR_SELINUX_SUFFIX
39 # define XATTR_SELINUX_SUFFIX "selinux"
40 #endif
41
42 #ifndef XATTR_NAME_SELINUX
43 # define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
44 #endif
45
46 /*
47  * Check for LL_SBI_FILE_SECCTX before calling.
48  */
49 int ll_dentry_init_security(struct dentry *dentry, int mode, struct qstr *name,
50                             const char **secctx_name, void **secctx,
51                             __u32 *secctx_size)
52 {
53 #ifdef HAVE_SECURITY_DENTRY_INIT_SECURITY
54         int rc;
55
56         /* security_dentry_init_security() is strange. Like
57          * security_inode_init_security() it may return a context (provided a
58          * Linux security module is enabled) but unlike
59          * security_inode_init_security() it does not return to us the name of
60          * the extended attribute to store the context under (for example
61          * "security.selinux"). So we only call it when we think we know what
62          * the name of the extended attribute will be. This is OK-ish since
63          * SELinux is the only module that implements
64          * security_dentry_init_security(). Note that the NFS client code just
65          * calls it and assumes that if anything is returned then it must come
66          * from SELinux. */
67
68         if (!selinux_is_enabled())
69                 return 0;
70
71         rc = security_dentry_init_security(dentry, mode, name, secctx,
72                                            secctx_size);
73         if (rc < 0)
74                 return rc;
75
76         *secctx_name = XATTR_NAME_SELINUX;
77 #endif /* HAVE_SECURITY_DENTRY_INIT_SECURITY */
78
79         return 0;
80 }
81
82 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
83 /**
84  * A helper function for ll_security_inode_init_security()
85  * that takes care of setting xattrs
86  *
87  * Get security context of @inode from @xattr_array,
88  * and put it in 'security.xxx' xattr of dentry
89  * stored in @fs_info.
90  *
91  * \retval 0        success
92  * \retval -ENOMEM  if no memory could be allocated for xattr name
93  * \retval < 0      failure to set xattr
94  */
95 static int
96 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
97               void *fs_info)
98 {
99         const struct xattr *xattr;
100         struct dentry *dentry = fs_info;
101         size_t name_len;
102         char *full_name;
103         int err = 0;
104
105         for (xattr = xattr_array; xattr->name != NULL; xattr++) {
106                 name_len = strlen(XATTR_SECURITY_PREFIX) + strlen(xattr->name)
107                            + 1;
108                 OBD_ALLOC(full_name, name_len);
109                 if (full_name == NULL)
110                         return -ENOMEM;
111                 strlcpy(full_name, XATTR_SECURITY_PREFIX, name_len);
112                 strlcat(full_name, xattr->name, name_len);
113
114                 err = ll_setxattr(dentry, full_name, xattr->value,
115                                   xattr->value_len, 0);
116
117                 OBD_FREE(full_name, name_len);
118
119                 if (err < 0)
120                         break;
121         }
122         return err;
123 }
124
125 /**
126  * Initializes security context
127  *
128  * Get security context of @inode in @dir,
129  * and put it in 'security.xxx' xattr of @dentry.
130  *
131  * \retval 0        success, or SELinux is disabled
132  * \retval -ENOMEM  if no memory could be allocated for xattr name
133  * \retval < 0      failure to get security context or set xattr
134  */
135 int
136 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
137                        struct inode *dir)
138 {
139         if (!selinux_is_enabled())
140                 return 0;
141
142         return ll_security_inode_init_security(inode, dir, NULL, NULL, 0,
143                                                &ll_initxattrs, dentry);
144 }
145 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK */
146 /**
147  * Initializes security context
148  *
149  * Get security context of @inode in @dir,
150  * and put it in 'security.xxx' xattr of @dentry.
151  *
152  * \retval 0        success, or SELinux is disabled
153  * \retval -ENOMEM  if no memory could be allocated for xattr name
154  * \retval < 0      failure to get security context or set xattr
155  */
156 int
157 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
158                        struct inode *dir)
159 {
160         int err;
161         size_t len, name_len;
162         void *value;
163         char *name, *full_name;
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         name_len = strlen(XATTR_SECURITY_PREFIX) + strlen(name) + 1;
177         OBD_ALLOC(full_name, name_len);
178         if (full_name == NULL)
179                 GOTO(out_free, err = -ENOMEM);
180         strlcpy(full_name, XATTR_SECURITY_PREFIX, name_len);
181         strlcat(full_name, name, name_len);
182
183         err = ll_setxattr(dentry, full_name, value, len, 0);
184         OBD_FREE(full_name, name_len);
185
186 out_free:
187         kfree(name);
188         kfree(value);
189
190         return err;
191 }
192 #endif /* HAVE_SECURITY_IINITSEC_CALLBACK */