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