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