]> Git Repo - linux.git/blame - fs/nfs/nfs4idmap.c
Merge tag 'selinux-pr-20190305' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / fs / nfs / nfs4idmap.c
CommitLineData
1da177e4
LT
1/*
2 * fs/nfs/idmap.c
3 *
4 * UID and GID to name mapping for clients.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Marius Aamodt Eriksen <[email protected]>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
5cf36cfd 36#include <linux/types.h>
57e62324
BS
37#include <linux/parser.h>
38#include <linux/fs.h>
57e62324
BS
39#include <net/net_namespace.h>
40#include <linux/sunrpc/rpc_pipe_fs.h>
6926afd1 41#include <linux/nfs_fs.h>
3cd0f37a 42#include <linux/nfs_fs_sb.h>
57e62324 43#include <linux/key.h>
3cd0f37a
BS
44#include <linux/keyctl.h>
45#include <linux/key-type.h>
3cd0f37a 46#include <keys/user-type.h>
822ad64d 47#include <keys/request_key_auth-type.h>
3cd0f37a 48#include <linux/module.h>
57e62324 49
3cd0f37a 50#include "internal.h"
17347d03 51#include "netns.h"
40c64c26 52#include "nfs4idmap.h"
1f2d30b5 53#include "nfs4trace.h"
3cd0f37a
BS
54
55#define NFS_UINT_MAXLEN 11
3cd0f37a 56
17280175
TM
57static const struct cred *id_resolver_cache;
58static struct key_type key_type_id_resolver_legacy;
3cd0f37a 59
c5066945
BS
60struct idmap_legacy_upcalldata {
61 struct rpc_pipe_msg pipe_msg;
62 struct idmap_msg idmap_msg;
822ad64d 63 struct key *authkey;
c5066945
BS
64 struct idmap *idmap;
65};
66
0cac1202 67struct idmap {
2127d82a 68 struct rpc_pipe_dir_object idmap_pdo;
0cac1202
TM
69 struct rpc_pipe *idmap_pipe;
70 struct idmap_legacy_upcalldata *idmap_upcall_data;
71 struct mutex idmap_mutex;
72};
73
6926afd1
TM
74/**
75 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
76 * @fattr: fully initialised struct nfs_fattr
77 * @owner_name: owner name string cache
78 * @group_name: group name string cache
79 */
80void nfs_fattr_init_names(struct nfs_fattr *fattr,
81 struct nfs4_string *owner_name,
82 struct nfs4_string *group_name)
83{
84 fattr->owner_name = owner_name;
85 fattr->group_name = group_name;
86}
87
88static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
89{
90 fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
91 kfree(fattr->owner_name->data);
92}
93
94static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
95{
96 fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
97 kfree(fattr->group_name->data);
98}
99
100static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
101{
102 struct nfs4_string *owner = fattr->owner_name;
9f309c86 103 kuid_t uid;
6926afd1
TM
104
105 if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
106 return false;
107 if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
108 fattr->uid = uid;
109 fattr->valid |= NFS_ATTR_FATTR_OWNER;
110 }
111 return true;
112}
113
114static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
115{
116 struct nfs4_string *group = fattr->group_name;
9f309c86 117 kgid_t gid;
6926afd1
TM
118
119 if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
120 return false;
121 if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
122 fattr->gid = gid;
123 fattr->valid |= NFS_ATTR_FATTR_GROUP;
124 }
125 return true;
126}
127
128/**
129 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
130 * @fattr: a fully initialised nfs_fattr structure
131 */
132void nfs_fattr_free_names(struct nfs_fattr *fattr)
133{
134 if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
135 nfs_fattr_free_owner_name(fattr);
136 if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
137 nfs_fattr_free_group_name(fattr);
138}
139
140/**
141 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
142 * @server: pointer to the filesystem nfs_server structure
143 * @fattr: a fully initialised nfs_fattr structure
144 *
145 * This helper maps the cached NFSv4 owner/group strings in fattr into
146 * their numeric uid/gid equivalents, and then frees the cached strings.
147 */
148void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
149{
150 if (nfs_fattr_map_owner_name(server, fattr))
151 nfs_fattr_free_owner_name(fattr);
152 if (nfs_fattr_map_group_name(server, fattr))
153 nfs_fattr_free_group_name(fattr);
154}
5cf36cfd 155
d67ae825 156int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
5cf36cfd
TM
157{
158 unsigned long val;
159 char buf[16];
160
161 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
162 return 0;
163 memcpy(buf, name, namelen);
164 buf[namelen] = '\0';
7297cb68 165 if (kstrtoul(buf, 0, &val) != 0)
5cf36cfd
TM
166 return 0;
167 *res = val;
168 return 1;
169}
d67ae825 170EXPORT_SYMBOL_GPL(nfs_map_string_to_numeric);
1da177e4 171
f0b85168
TM
172static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
173{
174 return snprintf(buf, buflen, "%u", id);
175}
176
17280175 177static struct key_type key_type_id_resolver = {
955a857e 178 .name = "id_resolver",
f9167789
DH
179 .preparse = user_preparse,
180 .free_preparse = user_free_preparse,
181 .instantiate = generic_key_instantiate,
955a857e
BS
182 .revoke = user_revoke,
183 .destroy = user_destroy,
184 .describe = user_describe,
185 .read = user_read,
186};
187
fb2a525c 188int nfs_idmap_init(void)
955a857e
BS
189{
190 struct cred *cred;
191 struct key *keyring;
192 int ret = 0;
193
f9fd2d9c
WAA
194 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
195 key_type_id_resolver.name);
955a857e
BS
196
197 cred = prepare_kernel_cred(NULL);
198 if (!cred)
199 return -ENOMEM;
200
4e963d4f
EB
201 keyring = keyring_alloc(".id_resolver",
202 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
f8aa23a5
DH
203 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
204 KEY_USR_VIEW | KEY_USR_READ,
5ac7eace 205 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
955a857e
BS
206 if (IS_ERR(keyring)) {
207 ret = PTR_ERR(keyring);
208 goto failed_put_cred;
209 }
210
955a857e
BS
211 ret = register_key_type(&key_type_id_resolver);
212 if (ret < 0)
213 goto failed_put_key;
214
a427b9ec
DH
215 ret = register_key_type(&key_type_id_resolver_legacy);
216 if (ret < 0)
217 goto failed_reg_legacy;
218
700920eb 219 set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
955a857e
BS
220 cred->thread_keyring = keyring;
221 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
222 id_resolver_cache = cred;
223 return 0;
224
a427b9ec
DH
225failed_reg_legacy:
226 unregister_key_type(&key_type_id_resolver);
955a857e
BS
227failed_put_key:
228 key_put(keyring);
229failed_put_cred:
230 put_cred(cred);
231 return ret;
232}
233
fb2a525c 234void nfs_idmap_quit(void)
955a857e
BS
235{
236 key_revoke(id_resolver_cache->thread_keyring);
237 unregister_key_type(&key_type_id_resolver);
a427b9ec 238 unregister_key_type(&key_type_id_resolver_legacy);
955a857e
BS
239 put_cred(id_resolver_cache);
240}
241
242/*
243 * Assemble the description to pass to request_key()
244 * This function will allocate a new string and update dest to point
245 * at it. The caller is responsible for freeing dest.
246 *
247 * On error 0 is returned. Otherwise, the length of dest is returned.
248 */
249static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
250 const char *type, size_t typelen, char **desc)
251{
252 char *cp;
253 size_t desclen = typelen + namelen + 2;
254
255 *desc = kmalloc(desclen, GFP_KERNEL);
8f0d97b4 256 if (!*desc)
955a857e
BS
257 return -ENOMEM;
258
259 cp = *desc;
260 memcpy(cp, type, typelen);
261 cp += typelen;
262 *cp++ = ':';
263
264 memcpy(cp, name, namelen);
265 cp += namelen;
266 *cp = '\0';
267 return desclen;
268}
269
ffa57b9e
BS
270static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
271 const char *type, struct idmap *idmap)
955a857e 272{
955a857e 273 char *desc;
ffa57b9e 274 struct key *rkey;
955a857e
BS
275 ssize_t ret;
276
277 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
cdb2e53f 278 if (ret < 0)
ffa57b9e
BS
279 return ERR_PTR(ret);
280
281 rkey = request_key(&key_type_id_resolver, desc, "");
282 if (IS_ERR(rkey)) {
283 mutex_lock(&idmap->idmap_mutex);
284 rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
285 desc, "", 0, idmap);
286 mutex_unlock(&idmap->idmap_mutex);
287 }
0c7774ab
DH
288 if (!IS_ERR(rkey))
289 set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
ffa57b9e
BS
290
291 kfree(desc);
292 return rkey;
293}
294
295static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
296 const char *type, void *data,
297 size_t data_size, struct idmap *idmap)
298{
299 const struct cred *saved_cred;
300 struct key *rkey;
146aa8b1 301 const struct user_key_payload *payload;
ffa57b9e 302 ssize_t ret;
955a857e
BS
303
304 saved_cred = override_creds(id_resolver_cache);
ffa57b9e 305 rkey = nfs_idmap_request_key(name, namelen, type, idmap);
955a857e 306 revert_creds(saved_cred);
57e62324 307
955a857e
BS
308 if (IS_ERR(rkey)) {
309 ret = PTR_ERR(rkey);
310 goto out;
311 }
312
313 rcu_read_lock();
314 rkey->perm |= KEY_USR_VIEW;
315
316 ret = key_validate(rkey);
317 if (ret < 0)
318 goto out_up;
319
0837e49a 320 payload = user_key_payload_rcu(rkey);
955a857e
BS
321 if (IS_ERR_OR_NULL(payload)) {
322 ret = PTR_ERR(payload);
323 goto out_up;
324 }
325
326 ret = payload->datalen;
327 if (ret > 0 && ret <= data_size)
328 memcpy(data, payload->data, ret);
329 else
330 ret = -EINVAL;
331
332out_up:
333 rcu_read_unlock();
334 key_put(rkey);
335out:
336 return ret;
337}
338
955a857e 339/* ID -> Name */
57e62324
BS
340static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
341 size_t buflen, struct idmap *idmap)
955a857e
BS
342{
343 char id_str[NFS_UINT_MAXLEN];
344 int id_len;
345 ssize_t ret;
346
d6889480 347 id_len = nfs_map_numeric_to_string(id, id_str, sizeof(id_str));
57e62324 348 ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
955a857e
BS
349 if (ret < 0)
350 return -EINVAL;
351 return ret;
352}
353
354/* Name -> ID */
57e62324
BS
355static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
356 __u32 *id, struct idmap *idmap)
955a857e
BS
357{
358 char id_str[NFS_UINT_MAXLEN];
359 long id_long;
360 ssize_t data_size;
361 int ret = 0;
362
57e62324 363 data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
955a857e
BS
364 if (data_size <= 0) {
365 ret = -EINVAL;
366 } else {
7297cb68 367 ret = kstrtol(id_str, 10, &id_long);
4cd1ec95
DC
368 if (!ret)
369 *id = (__u32)id_long;
955a857e
BS
370 }
371 return ret;
372}
373
e6499c6f 374/* idmap classic begins here */
f0b85168 375
57e62324
BS
376enum {
377 Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
1da177e4
LT
378};
379
57e62324
BS
380static const match_table_t nfs_idmap_tokens = {
381 { Opt_find_uid, "uid:%s" },
382 { Opt_find_gid, "gid:%s" },
383 { Opt_find_user, "user:%s" },
384 { Opt_find_group, "group:%s" },
385 { Opt_find_err, NULL }
1da177e4
LT
386};
387
822ad64d 388static int nfs_idmap_legacy_upcall(struct key *, void *);
369af0f1
CL
389static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
390 size_t);
c5066945 391static void idmap_release_pipe(struct inode *);
369af0f1 392static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
1da177e4 393
b693ba4a 394static const struct rpc_pipe_ops idmap_upcall_ops = {
c1225158 395 .upcall = rpc_pipe_generic_upcall,
369af0f1 396 .downcall = idmap_pipe_downcall,
c5066945 397 .release_pipe = idmap_release_pipe,
369af0f1 398 .destroy_msg = idmap_pipe_destroy_msg,
1da177e4
LT
399};
400
17280175 401static struct key_type key_type_id_resolver_legacy = {
a427b9ec 402 .name = "id_legacy",
f9167789
DH
403 .preparse = user_preparse,
404 .free_preparse = user_free_preparse,
405 .instantiate = generic_key_instantiate,
57e62324
BS
406 .revoke = user_revoke,
407 .destroy = user_destroy,
408 .describe = user_describe,
409 .read = user_read,
410 .request_key = nfs_idmap_legacy_upcall,
411};
412
2127d82a
TM
413static void nfs_idmap_pipe_destroy(struct dentry *dir,
414 struct rpc_pipe_dir_object *pdo)
4929d1d3 415{
2127d82a
TM
416 struct idmap *idmap = pdo->pdo_data;
417 struct rpc_pipe *pipe = idmap->idmap_pipe;
418
d7631250 419 if (pipe->dentry) {
4929d1d3 420 rpc_unlink(pipe->dentry);
d7631250
TM
421 pipe->dentry = NULL;
422 }
4929d1d3
SK
423}
424
2127d82a
TM
425static int nfs_idmap_pipe_create(struct dentry *dir,
426 struct rpc_pipe_dir_object *pdo)
4929d1d3 427{
2127d82a
TM
428 struct idmap *idmap = pdo->pdo_data;
429 struct rpc_pipe *pipe = idmap->idmap_pipe;
4929d1d3
SK
430 struct dentry *dentry;
431
432 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
433 if (IS_ERR(dentry))
434 return PTR_ERR(dentry);
435 pipe->dentry = dentry;
436 return 0;
437}
438
2127d82a
TM
439static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
440 .create = nfs_idmap_pipe_create,
441 .destroy = nfs_idmap_pipe_destroy,
442};
4929d1d3 443
b7162792 444int
adfa6f98 445nfs_idmap_new(struct nfs_client *clp)
1da177e4
LT
446{
447 struct idmap *idmap;
c239d83b 448 struct rpc_pipe *pipe;
b7162792 449 int error;
1da177e4 450
369af0f1
CL
451 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
452 if (idmap == NULL)
453 return -ENOMEM;
1da177e4 454
2127d82a
TM
455 rpc_init_pipe_dir_object(&idmap->idmap_pdo,
456 &nfs_idmap_pipe_dir_object_ops,
457 idmap);
458
c239d83b
SK
459 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
460 if (IS_ERR(pipe)) {
461 error = PTR_ERR(pipe);
2127d82a 462 goto err;
c239d83b
SK
463 }
464 idmap->idmap_pipe = pipe;
b1027439 465 mutex_init(&idmap->idmap_mutex);
1da177e4 466
2127d82a
TM
467 error = rpc_add_pipe_dir_object(clp->cl_net,
468 &clp->cl_rpcclient->cl_pipedir_objects,
469 &idmap->idmap_pdo);
470 if (error)
471 goto err_destroy_pipe;
472
1da177e4 473 clp->cl_idmap = idmap;
b7162792 474 return 0;
2127d82a
TM
475err_destroy_pipe:
476 rpc_destroy_pipe_data(idmap->idmap_pipe);
477err:
478 kfree(idmap);
479 return error;
1da177e4
LT
480}
481
482void
adfa6f98 483nfs_idmap_delete(struct nfs_client *clp)
1da177e4
LT
484{
485 struct idmap *idmap = clp->cl_idmap;
486
487 if (!idmap)
488 return;
1da177e4 489 clp->cl_idmap = NULL;
2127d82a
TM
490 rpc_remove_pipe_dir_object(clp->cl_net,
491 &clp->cl_rpcclient->cl_pipedir_objects,
492 &idmap->idmap_pdo);
493 rpc_destroy_pipe_data(idmap->idmap_pipe);
1da177e4
LT
494 kfree(idmap);
495}
496
c5066945
BS
497static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
498 struct idmap_msg *im,
57e62324 499 struct rpc_pipe_msg *msg)
1da177e4 500{
57e62324
BS
501 substring_t substr;
502 int token, ret;
1da177e4 503
57e62324
BS
504 im->im_type = IDMAP_TYPE_GROUP;
505 token = match_token(desc, nfs_idmap_tokens, &substr);
1da177e4 506
57e62324
BS
507 switch (token) {
508 case Opt_find_uid:
509 im->im_type = IDMAP_TYPE_USER;
01e03bdc 510 /* Fall through */
57e62324
BS
511 case Opt_find_gid:
512 im->im_conv = IDMAP_CONV_NAMETOID;
513 ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
514 break;
1da177e4 515
57e62324
BS
516 case Opt_find_user:
517 im->im_type = IDMAP_TYPE_USER;
01e03bdc 518 /* Fall through */
57e62324
BS
519 case Opt_find_group:
520 im->im_conv = IDMAP_CONV_IDTONAME;
521 ret = match_int(&substr, &im->im_id);
12b289cf
CX
522 if (ret)
523 goto out;
57e62324 524 break;
1da177e4 525
57e62324
BS
526 default:
527 ret = -EINVAL;
1da177e4
LT
528 goto out;
529 }
530
57e62324
BS
531 msg->data = im;
532 msg->len = sizeof(struct idmap_msg);
1da177e4 533
57e62324 534out:
369af0f1 535 return ret;
1da177e4
LT
536}
537
e9ab41b6
TM
538static bool
539nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
0cac1202 540 struct idmap_legacy_upcalldata *data)
e9ab41b6 541{
0cac1202 542 if (idmap->idmap_upcall_data != NULL) {
e9ab41b6
TM
543 WARN_ON_ONCE(1);
544 return false;
545 }
0cac1202 546 idmap->idmap_upcall_data = data;
e9ab41b6
TM
547 return true;
548}
549
550static void
551nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
552{
822ad64d 553 struct key *authkey = idmap->idmap_upcall_data->authkey;
e9ab41b6 554
0cac1202
TM
555 kfree(idmap->idmap_upcall_data);
556 idmap->idmap_upcall_data = NULL;
822ad64d
DH
557 complete_request_key(authkey, ret);
558 key_put(authkey);
e9ab41b6
TM
559}
560
561static void
562nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
563{
0cac1202 564 if (idmap->idmap_upcall_data != NULL)
e9ab41b6
TM
565 nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
566}
567
822ad64d 568static int nfs_idmap_legacy_upcall(struct key *authkey, void *aux)
1da177e4 569{
c5066945 570 struct idmap_legacy_upcalldata *data;
822ad64d 571 struct request_key_auth *rka = get_request_key_auth(authkey);
57e62324 572 struct rpc_pipe_msg *msg;
1da177e4 573 struct idmap_msg *im;
57e62324 574 struct idmap *idmap = (struct idmap *)aux;
822ad64d 575 struct key *key = rka->target_key;
49686cbb
EB
576 int ret = -ENOKEY;
577
578 if (!aux)
579 goto out1;
1da177e4 580
57e62324 581 /* msg and im are freed in idmap_pipe_destroy_msg */
49686cbb 582 ret = -ENOMEM;
57a51048 583 data = kzalloc(sizeof(*data), GFP_KERNEL);
c5066945 584 if (!data)
57e62324 585 goto out1;
1da177e4 586
c5066945
BS
587 msg = &data->pipe_msg;
588 im = &data->idmap_msg;
589 data->idmap = idmap;
822ad64d 590 data->authkey = key_get(authkey);
c5066945
BS
591
592 ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
57e62324
BS
593 if (ret < 0)
594 goto out2;
1da177e4 595
e9ab41b6 596 ret = -EAGAIN;
0cac1202 597 if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
0e24d849 598 goto out2;
1da177e4 599
11588f49
BS
600 ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
601 if (ret < 0)
e9ab41b6 602 nfs_idmap_abort_pipe_upcall(idmap, ret);
1da177e4 603
11588f49 604 return ret;
57e62324 605out2:
c5066945 606 kfree(data);
57e62324 607out1:
822ad64d 608 complete_request_key(authkey, ret);
369af0f1 609 return ret;
1da177e4
LT
610}
611
cf4ab538 612static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
1da177e4 613{
cf4ab538 614 return key_instantiate_and_link(key, data, datalen,
57e62324
BS
615 id_resolver_cache->thread_keyring,
616 authkey);
617}
1da177e4 618
0cac1202
TM
619static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
620 struct idmap_msg *upcall,
621 struct key *key, struct key *authkey)
57e62324
BS
622{
623 char id_str[NFS_UINT_MAXLEN];
cf4ab538 624 size_t len;
0cac1202 625 int ret = -ENOKEY;
1da177e4 626
0cac1202
TM
627 /* ret = -ENOKEY */
628 if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
629 goto out;
57e62324
BS
630 switch (im->im_conv) {
631 case IDMAP_CONV_NAMETOID:
0cac1202
TM
632 if (strcmp(upcall->im_name, im->im_name) != 0)
633 break;
cf4ab538 634 /* Note: here we store the NUL terminator too */
d6889480
DW
635 len = 1 + nfs_map_numeric_to_string(im->im_id, id_str,
636 sizeof(id_str));
cf4ab538 637 ret = nfs_idmap_instantiate(key, authkey, id_str, len);
57e62324
BS
638 break;
639 case IDMAP_CONV_IDTONAME:
0cac1202
TM
640 if (upcall->im_id != im->im_id)
641 break;
cf4ab538
TM
642 len = strlen(im->im_name);
643 ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
57e62324 644 break;
0cac1202
TM
645 default:
646 ret = -EINVAL;
1da177e4 647 }
0cac1202 648out:
1da177e4
LT
649 return ret;
650}
651
1da177e4
LT
652static ssize_t
653idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
654{
822ad64d 655 struct request_key_auth *rka;
496ad9aa 656 struct rpc_inode *rpci = RPC_I(file_inode(filp));
1da177e4 657 struct idmap *idmap = (struct idmap *)rpci->private;
822ad64d 658 struct key *authkey;
57e62324 659 struct idmap_msg im;
d24aae41 660 size_t namelen_in;
e9ab41b6 661 int ret = -ENOKEY;
1da177e4 662
a427b9ec
DH
663 /* If instantiation is successful, anyone waiting for key construction
664 * will have been woken up and someone else may now have used
665 * idmap_key_cons - so after this point we may no longer touch it.
666 */
0cac1202 667 if (idmap->idmap_upcall_data == NULL)
e9ab41b6 668 goto out_noupcall;
a427b9ec 669
822ad64d
DH
670 authkey = idmap->idmap_upcall_data->authkey;
671 rka = get_request_key_auth(authkey);
a427b9ec 672
57e62324
BS
673 if (mlen != sizeof(im)) {
674 ret = -ENOSPC;
1da177e4
LT
675 goto out;
676 }
677
57e62324
BS
678 if (copy_from_user(&im, src, mlen) != 0) {
679 ret = -EFAULT;
1da177e4 680 goto out;
57e62324 681 }
1da177e4 682
57e62324 683 if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
12dfd080
BS
684 ret = -ENOKEY;
685 goto out;
1da177e4
LT
686 }
687
57e62324
BS
688 namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
689 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
690 ret = -EINVAL;
1da177e4 691 goto out;
0cac1202 692}
1da177e4 693
0cac1202
TM
694 ret = nfs_idmap_read_and_verify_message(&im,
695 &idmap->idmap_upcall_data->idmap_msg,
822ad64d 696 rka->target_key, authkey);
57e62324 697 if (ret >= 0) {
822ad64d 698 key_set_timeout(rka->target_key, nfs_idmap_cache_timeout);
57e62324
BS
699 ret = mlen;
700 }
701
1da177e4 702out:
e9ab41b6
TM
703 nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
704out_noupcall:
1da177e4
LT
705 return ret;
706}
707
75c96f85 708static void
1da177e4
LT
709idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
710{
c5066945
BS
711 struct idmap_legacy_upcalldata *data = container_of(msg,
712 struct idmap_legacy_upcalldata,
713 pipe_msg);
714 struct idmap *idmap = data->idmap;
e9ab41b6
TM
715
716 if (msg->errno)
717 nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
c5066945
BS
718}
719
720static void
721idmap_release_pipe(struct inode *inode)
722{
723 struct rpc_inode *rpci = RPC_I(inode);
724 struct idmap *idmap = (struct idmap *)rpci->private;
e9ab41b6
TM
725
726 nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
1da177e4
LT
727}
728
9f309c86 729int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
1da177e4 730{
e4fd72a1 731 struct idmap *idmap = server->nfs_client->cl_idmap;
9f309c86
EB
732 __u32 id = -1;
733 int ret = 0;
1da177e4 734
9f309c86
EB
735 if (!nfs_map_string_to_numeric(name, namelen, &id))
736 ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
737 if (ret == 0) {
738 *uid = make_kuid(&init_user_ns, id);
739 if (!uid_valid(*uid))
740 ret = -ERANGE;
741 }
1f2d30b5 742 trace_nfs4_map_name_to_uid(name, namelen, id, ret);
9f309c86 743 return ret;
1da177e4
LT
744}
745
9f309c86 746int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
1da177e4 747{
e4fd72a1 748 struct idmap *idmap = server->nfs_client->cl_idmap;
9f309c86
EB
749 __u32 id = -1;
750 int ret = 0;
1da177e4 751
9f309c86
EB
752 if (!nfs_map_string_to_numeric(name, namelen, &id))
753 ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
754 if (ret == 0) {
755 *gid = make_kgid(&init_user_ns, id);
756 if (!gid_valid(*gid))
757 ret = -ERANGE;
758 }
1f2d30b5 759 trace_nfs4_map_group_to_gid(name, namelen, id, ret);
9f309c86 760 return ret;
1da177e4
LT
761}
762
9f309c86 763int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
1da177e4 764{
e4fd72a1 765 struct idmap *idmap = server->nfs_client->cl_idmap;
b064eca2 766 int ret = -EINVAL;
9f309c86 767 __u32 id;
1da177e4 768
9f309c86 769 id = from_kuid(&init_user_ns, uid);
b064eca2 770 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
9f309c86 771 ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
f0b85168 772 if (ret < 0)
9f309c86 773 ret = nfs_map_numeric_to_string(id, buf, buflen);
1f2d30b5 774 trace_nfs4_map_uid_to_name(buf, ret, id, ret);
f0b85168 775 return ret;
1da177e4 776}
9f309c86 777int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
1da177e4 778{
e4fd72a1 779 struct idmap *idmap = server->nfs_client->cl_idmap;
b064eca2 780 int ret = -EINVAL;
9f309c86 781 __u32 id;
1da177e4 782
9f309c86 783 id = from_kgid(&init_user_ns, gid);
b064eca2 784 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
9f309c86 785 ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
f0b85168 786 if (ret < 0)
9f309c86 787 ret = nfs_map_numeric_to_string(id, buf, buflen);
1f2d30b5 788 trace_nfs4_map_gid_to_group(buf, ret, id, ret);
f0b85168 789 return ret;
1da177e4 790}
This page took 0.963739 seconds and 4 git commands to generate.