userns: Convert vfs posix_acl support to use kuids and kgids
[profile/ivi/kernel-adaptation-intel-automotive.git] / fs / xattr_acl.c
1 /*
2  * linux/fs/xattr_acl.c
3  *
4  * Almost all from linux/fs/ext2/acl.c:
5  * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
6  */
7
8 #include <linux/export.h>
9 #include <linux/fs.h>
10 #include <linux/posix_acl_xattr.h>
11 #include <linux/gfp.h>
12 #include <linux/user_namespace.h>
13
14 /*
15  * Fix up the uids and gids in posix acl extended attributes in place.
16  */
17 static void posix_acl_fix_xattr_userns(
18         struct user_namespace *to, struct user_namespace *from,
19         void *value, size_t size)
20 {
21         posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
22         posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
23         int count;
24         kuid_t uid;
25         kgid_t gid;
26
27         if (!value)
28                 return;
29         if (size < sizeof(posix_acl_xattr_header))
30                 return;
31         if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
32                 return;
33
34         count = posix_acl_xattr_count(size);
35         if (count < 0)
36                 return;
37         if (count == 0)
38                 return;
39
40         for (end = entry + count; entry != end; entry++) {
41                 switch(le16_to_cpu(entry->e_tag)) {
42                 case ACL_USER:
43                         uid = make_kuid(from, le32_to_cpu(entry->e_id));
44                         entry->e_id = cpu_to_le32(from_kuid(to, uid));
45                         break;
46                 case ACL_GROUP:
47                         gid = make_kgid(from, le32_to_cpu(entry->e_id));
48                         entry->e_id = cpu_to_le32(from_kuid(to, uid));
49                         break;
50                 default:
51                         break;
52                 }
53         }
54 }
55
56 void posix_acl_fix_xattr_from_user(void *value, size_t size)
57 {
58         struct user_namespace *user_ns = current_user_ns();
59         if (user_ns == &init_user_ns)
60                 return;
61         posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
62 }
63
64 void posix_acl_fix_xattr_to_user(void *value, size_t size)
65 {
66         struct user_namespace *user_ns = current_user_ns();
67         if (user_ns == &init_user_ns)
68                 return;
69         posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
70 }
71
72 /*
73  * Convert from extended attribute to in-memory representation.
74  */
75 struct posix_acl *
76 posix_acl_from_xattr(const void *value, size_t size)
77 {
78         posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
79         posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
80         int count;
81         struct posix_acl *acl;
82         struct posix_acl_entry *acl_e;
83
84         if (!value)
85                 return NULL;
86         if (size < sizeof(posix_acl_xattr_header))
87                  return ERR_PTR(-EINVAL);
88         if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
89                 return ERR_PTR(-EOPNOTSUPP);
90
91         count = posix_acl_xattr_count(size);
92         if (count < 0)
93                 return ERR_PTR(-EINVAL);
94         if (count == 0)
95                 return NULL;
96         
97         acl = posix_acl_alloc(count, GFP_NOFS);
98         if (!acl)
99                 return ERR_PTR(-ENOMEM);
100         acl_e = acl->a_entries;
101         
102         for (end = entry + count; entry != end; acl_e++, entry++) {
103                 acl_e->e_tag  = le16_to_cpu(entry->e_tag);
104                 acl_e->e_perm = le16_to_cpu(entry->e_perm);
105
106                 switch(acl_e->e_tag) {
107                         case ACL_USER_OBJ:
108                         case ACL_GROUP_OBJ:
109                         case ACL_MASK:
110                         case ACL_OTHER:
111                                 break;
112
113                         case ACL_USER:
114                                 acl_e->e_uid =
115                                         make_kuid(&init_user_ns,
116                                                   le32_to_cpu(entry->e_id));
117                                 if (!uid_valid(acl_e->e_uid))
118                                         goto fail;
119                                 break;
120                         case ACL_GROUP:
121                                 acl_e->e_gid =
122                                         make_kgid(&init_user_ns,
123                                                   le32_to_cpu(entry->e_id));
124                                 if (!gid_valid(acl_e->e_gid))
125                                         goto fail;
126                                 break;
127
128                         default:
129                                 goto fail;
130                 }
131         }
132         return acl;
133
134 fail:
135         posix_acl_release(acl);
136         return ERR_PTR(-EINVAL);
137 }
138 EXPORT_SYMBOL (posix_acl_from_xattr);
139
140 /*
141  * Convert from in-memory to extended attribute representation.
142  */
143 int
144 posix_acl_to_xattr(const struct posix_acl *acl, void *buffer, size_t size)
145 {
146         posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
147         posix_acl_xattr_entry *ext_entry = ext_acl->a_entries;
148         int real_size, n;
149
150         real_size = posix_acl_xattr_size(acl->a_count);
151         if (!buffer)
152                 return real_size;
153         if (real_size > size)
154                 return -ERANGE;
155         
156         ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
157
158         for (n=0; n < acl->a_count; n++, ext_entry++) {
159                 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
160                 ext_entry->e_tag  = cpu_to_le16(acl_e->e_tag);
161                 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
162                 switch(acl_e->e_tag) {
163                 case ACL_USER:
164                         ext_entry->e_id =
165                                 cpu_to_le32(from_kuid(&init_user_ns, acl_e->e_uid));
166                         break;
167                 case ACL_GROUP:
168                         ext_entry->e_id =
169                                 cpu_to_le32(from_kgid(&init_user_ns, acl_e->e_gid));
170                         break;
171                 default:
172                         ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
173                         break;
174                 }
175         }
176         return real_size;
177 }
178 EXPORT_SYMBOL (posix_acl_to_xattr);