Whamcloud - gitweb
LU-16114 build: Update security_dentry_init_security args
[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 #ifdef HAVE_LINUX_SELINUX_IS_ENABLED
37 #include <linux/selinux.h>
38 #endif
39 #include <linux/xattr.h>
40 #include "llite_internal.h"
41
42 #ifndef XATTR_SELINUX_SUFFIX
43 # define XATTR_SELINUX_SUFFIX "selinux"
44 #endif
45
46 #ifndef XATTR_NAME_SELINUX
47 # define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
48 #endif
49
50 /*
51  * Check for LL_SBI_FILE_SECCTX before calling.
52  */
53 int ll_dentry_init_security(struct inode *parent, struct dentry *dentry,
54                             int mode, struct qstr *name,
55                             const char **secctx_name, void **secctx,
56                             __u32 *secctx_size)
57 {
58         int rc;
59
60         /*
61          * security_dentry_init_security() is strange. Like
62          * security_inode_init_security() it may return a context (provided a
63          * Linux security module is enabled) but unlike
64          * security_inode_init_security() it does not return to us the name of
65          * the extended attribute to store the context under (for example
66          * "security.selinux"). So we only call it when we think we know what
67          * the name of the extended attribute will be. This is OK-ish since
68          * SELinux is the only module that implements
69          * security_dentry_init_security(). Note that the NFS client code just
70          * calls it and assumes that if anything is returned then it must come
71          * from SELinux.
72          */
73
74         if (!selinux_is_enabled())
75                 return 0;
76
77         /* fetch length of security xattr name */
78         rc = security_inode_listsecurity(parent, NULL, 0);
79         /* xattr name length == 0 means SELinux is disabled */
80         if (rc == 0)
81                 return 0;
82         /* we support SELinux only */
83         if (rc != strlen(XATTR_NAME_SELINUX) + 1)
84                 return -EOPNOTSUPP;
85
86         rc = security_dentry_init_security(dentry, mode, name,
87 #ifdef HAVE_SECURITY_DENTRY_INIT_WITH_XATTR_NAME_ARG
88                                            secctx_name,
89 #endif
90                                            secctx, secctx_size);
91         /* Usually, security_dentry_init_security() returns -EOPNOTSUPP when
92          * SELinux is disabled.
93          * But on some kernels (e.g. rhel 8.5) it returns 0 when SELinux is
94          * disabled, and in this case the security context is empty.
95          */
96         if (rc == -EOPNOTSUPP || (rc == 0 && *secctx_size == 0))
97                 /* do nothing */
98                 return 0;
99         if (rc < 0)
100                 return rc;
101
102 #ifndef HAVE_SECURITY_DENTRY_INIT_WITH_XATTR_NAME_ARG
103         *secctx_name = XATTR_NAME_SELINUX;
104 #endif
105
106         return 0;
107 }
108
109 /**
110  * A helper function for security_inode_init_security()
111  * that takes care of setting xattrs
112  *
113  * Get security context of @inode from @xattr_array,
114  * and put it in 'security.xxx' xattr of dentry
115  * stored in @fs_info.
116  *
117  * \retval 0        success
118  * \retval -ENOMEM  if no memory could be allocated for xattr name
119  * \retval < 0      failure to set xattr
120  */
121 static int
122 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
123               void *fs_info)
124 {
125         struct dentry *dentry = fs_info;
126         const struct xattr *xattr;
127         int err = 0;
128
129         for (xattr = xattr_array; xattr->name; xattr++) {
130                 char *full_name;
131
132                 full_name = kasprintf(GFP_KERNEL, "%s%s",
133                                       XATTR_SECURITY_PREFIX, xattr->name);
134                 if (!full_name) {
135                         err = -ENOMEM;
136                         break;
137                 }
138
139                 err = ll_vfs_setxattr(dentry, inode, full_name, xattr->value,
140                                       xattr->value_len, XATTR_CREATE);
141                 kfree(full_name);
142                 if (err < 0)
143                         break;
144         }
145         return err;
146 }
147
148 /**
149  * Initializes security context
150  *
151  * Get security context of @inode in @dir,
152  * and put it in 'security.xxx' xattr of @dentry.
153  *
154  * \retval 0        success, or SELinux is disabled
155  * \retval -ENOMEM  if no memory could be allocated for xattr name
156  * \retval < 0      failure to get security context or set xattr
157  */
158 int
159 ll_inode_init_security(struct dentry *dentry, struct inode *inode,
160                        struct inode *dir)
161 {
162         int rc;
163
164         if (!selinux_is_enabled())
165                 return 0;
166
167         rc = security_inode_init_security(inode, dir, NULL,
168                                           &ll_initxattrs, dentry);
169         if (rc == -EOPNOTSUPP)
170                 return 0;
171
172         return rc;
173 }
174
175 /**
176  * Get security context xattr name used by policy.
177  *
178  * \retval >= 0     length of xattr name
179  * \retval < 0      failure to get security context xattr name
180  */
181 int
182 ll_listsecurity(struct inode *inode, char *secctx_name, size_t secctx_name_size)
183 {
184         int rc;
185
186         if (!selinux_is_enabled())
187                 return 0;
188
189         rc = security_inode_listsecurity(inode, secctx_name, secctx_name_size);
190         if (rc >= secctx_name_size)
191                 rc = -ERANGE;
192         else if (rc >= 0)
193                 secctx_name[rc] = '\0';
194         return rc;
195 }