staging: lustre: add SPDX identifiers to all lustre files
[linux-2.6-block.git] / drivers / staging / lustre / lustre / llite / xattr_security.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * GPL HEADER START
4  *
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 only,
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License version 2 for more details (a copy is included
15  * in the LICENSE file that accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License
18  * version 2 along with this program; If not, see http://www.gnu.org/licenses
19  *
20  * GPL HEADER END
21  */
22
23 /*
24  * Copyright (c) 2014 Bull SAS
25  * Author: Sebastien Buisson sebastien.buisson@bull.net
26  */
27
28 /*
29  * lustre/llite/xattr_security.c
30  * Handler for storing security labels as extended attributes.
31  */
32
33 #include <linux/types.h>
34 #include <linux/security.h>
35 #include <linux/selinux.h>
36 #include <linux/xattr.h>
37 #include "llite_internal.h"
38
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         struct dentry *dentry = fs_info;
56         const struct xattr *xattr;
57         int err = 0;
58
59         for (xattr = xattr_array; xattr->name; xattr++) {
60                 char *full_name;
61
62                 full_name = kasprintf(GFP_KERNEL, "%s%s",
63                                       XATTR_SECURITY_PREFIX, xattr->name);
64                 if (!full_name) {
65                         err = -ENOMEM;
66                         break;
67                 }
68
69                 err = __vfs_setxattr(dentry, inode, full_name, xattr->value,
70                                      xattr->value_len, XATTR_CREATE);
71                 kfree(full_name);
72                 if (err < 0)
73                         break;
74         }
75         return err;
76 }
77
78 /**
79  * Initializes security context
80  *
81  * Get security context of @inode in @dir,
82  * and put it in 'security.xxx' xattr of @dentry.
83  *
84  * \retval 0        success, or SELinux is disabled
85  * \retval -ENOMEM  if no memory could be allocated for xattr name
86  * \retval < 0      failure to get security context or set xattr
87  */
88 int
89 ll_init_security(struct dentry *dentry, struct inode *inode, struct inode *dir)
90 {
91         if (!selinux_is_enabled())
92                 return 0;
93
94         return security_inode_init_security(inode, dir, NULL,
95                                             &ll_initxattrs, dentry);
96 }