Whamcloud - gitweb
LU-6158 mdt: always shrink_capsule in getxattr_all
[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
33 #include <linux/security.h>
34 #include <linux/xattr.h>
35 #include "llite_internal.h"
36
37 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
38 /**
39  * A helper function for ll_security_inode_init_security()
40  * that takes care of setting xattrs
41  *
42  * Get security context of @inode from @xattr_array,
43  * and put it in 'security.xxx' xattr of dentry
44  * stored in @fs_info.
45  *
46  * \retval 0        success
47  * \retval -ENOMEM  if no memory could be allocated for xattr name
48  * \retval < 0      failure to set xattr
49  */
50 static int
51 ll_initxattrs(struct inode *inode, const struct xattr *xattr_array,
52               void *fs_info)
53 {
54         const struct xattr *xattr;
55         struct dentry *dentry = fs_info;
56         size_t name_len;
57         char *full_name;
58         int err = 0;
59
60         for (xattr = xattr_array; xattr->name != NULL; xattr++) {
61                 name_len = strlen(XATTR_SECURITY_PREFIX) + strlen(xattr->name)
62                            + 1;
63                 OBD_ALLOC(full_name, name_len);
64                 if (full_name == NULL)
65                         return -ENOMEM;
66                 strlcpy(full_name, XATTR_SECURITY_PREFIX, name_len);
67                 strlcat(full_name, xattr->name, name_len);
68
69                 err = ll_setxattr(dentry, full_name, xattr->value,
70                                   xattr->value_len, 0);
71
72                 OBD_FREE(full_name, name_len);
73
74                 if (err < 0)
75                         break;
76         }
77         return err;
78 }
79
80 /**
81  * Initializes security context
82  *
83  * Get security context of @inode in @dir,
84  * and put it in 'security.xxx' xattr of @dentry.
85  *
86  * \retval 0        success, or SELinux is disabled
87  * \retval -ENOMEM  if no memory could be allocated for xattr name
88  * \retval < 0      failure to get security context or set xattr
89  */
90 int
91 ll_init_security(struct dentry *dentry, struct inode *inode, struct inode *dir)
92 {
93         if (!selinux_is_enabled())
94                 return 0;
95
96         return ll_security_inode_init_security(inode, dir, NULL, NULL, 0,
97                                                &ll_initxattrs, dentry);
98 }
99 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK */
100 /**
101  * Initializes security context
102  *
103  * Get security context of @inode in @dir,
104  * and put it in 'security.xxx' xattr of @dentry.
105  *
106  * \retval 0        success, or SELinux is disabled
107  * \retval -ENOMEM  if no memory could be allocated for xattr name
108  * \retval < 0      failure to get security context or set xattr
109  */
110 int
111 ll_init_security(struct dentry *dentry, struct inode *inode, struct inode *dir)
112 {
113         int err;
114         size_t len, name_len;
115         void *value;
116         char *name, *full_name;
117
118         if (!selinux_is_enabled())
119                 return 0;
120
121         err = ll_security_inode_init_security(inode, dir, &name, &value, &len,
122                                               NULL, dentry);
123         if (err != 0) {
124                 if (err == -EOPNOTSUPP)
125                         return 0;
126                 return err;
127         }
128
129         name_len = strlen(XATTR_SECURITY_PREFIX) + strlen(name) + 1;
130         OBD_ALLOC(full_name, name_len);
131         if (full_name == NULL)
132                 GOTO(out_free, err = -ENOMEM);
133         strlcpy(full_name, XATTR_SECURITY_PREFIX, name_len);
134         strlcat(full_name, name, name_len);
135
136         err = ll_setxattr(dentry, full_name, value, len, 0);
137         OBD_FREE(full_name, name_len);
138
139 out_free:
140         kfree(name);
141         kfree(value);
142
143         return err;
144 }
145 #endif /* HAVE_SECURITY_IINITSEC_CALLBACK */